免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 3448 | 回复: 11
打印 上一主题 下一主题

[进程管理] 关于struct pid中struct upid numbers[1]形式上只有一个,但实际上可以根据需要扩展? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-11-11 19:59 |只看该作者 |倒序浏览
  1. struct pid
  2. {
  3.         atomic_t count;
  4.         unsigned int level;
  5.         /* lists of tasks that use this pid */
  6.         struct hlist_head tasks[PIDTYPE_MAX];
  7.         struct rcu_head rcu;
  8.         struct upid numbers[1];
  9. };
复制代码
numbers[1]形式上只有一个数组项,但实际上可以根据需要进行扩展。
在struct pid *alloc_pid(struct pid_namespace *ns)函数中分配一个
struct pid结构体pid = kmem_cache_alloc(ns->pid_cachep, GFP_KERNEL);
但是之后就直接通过i来使用numbers[],如下
  1. struct pid *alloc_pid(struct pid_namespace *ns)
  2. {
  3.         struct pid *pid;
  4.         enum pid_type type;
  5.         int i, nr;
  6.         struct pid_namespace *tmp;
  7.         struct upid *upid;

  8.         pid = kmem_cache_alloc(ns->pid_cachep, GFP_KERNEL);
  9.         if (!pid)
  10.                 goto out;

  11.         tmp = ns;
  12.         for (i = ns->level; i >= 0; i--) {
  13.                 nr = alloc_pidmap(tmp);
  14.                 if (nr < 0)
  15.                         goto out_free;

  16.                 pid->numbers[i].nr = nr;
  17.                 pid->numbers[i].ns = tmp;
  18.                 tmp = tmp->parent;
  19.         }

  20.         get_pid_ns(ns);
  21.         pid->level = ns->level;
  22.         atomic_set(&pid->count, 1);
  23.         for (type = 0; type < PIDTYPE_MAX; ++type)
  24.                 INIT_HLIST_HEAD(&pid->tasks[type]);

  25.         upid = pid->numbers + ns->level;
  26.         spin_lock_irq(&pidmap_lock);
  27.         for ( ; upid >= pid->numbers; --upid)
  28.                 hlist_add_head_rcu(&upid->pid_chain,
  29.                                 &pid_hash[pid_hashfn(upid->nr, upid->ns)]);
  30.         spin_unlock_irq(&pidmap_lock);

  31. out:
  32.         return pid;

  33. out_free:
  34.         while (++i <= ns->level)
  35.                 free_pidmap(pid->numbers + i);

  36.         kmem_cache_free(ns->pid_cachep, pid);
  37.         pid = NULL;
  38.         goto out;
  39. }
复制代码
  1. struct pid *find_pid_ns(int nr, struct pid_namespace *ns)
  2. {
  3.         struct hlist_node *elem;
  4.         struct upid *pnr;

  5.         hlist_for_each_entry_rcu(pnr, elem, &pid_hash[pid_hashfn(nr, ns)], pid_chain)
  6.                 if (pnr->nr == nr && pnr->ns == ns)
  7.                         return container_of(pnr, struct pid,
  8.                                         numbers[ns->level]);

  9.         return NULL;
  10. }
复制代码
假设ns->level等于3,那么下面pid->numbers.nr中numbers引用的内存在哪里?
“numbers[1]形式上只有一个数组项,但实际上可以根据需要进行扩展”这句话怎么理解?
需要时进行扩展是怎么扩展的?
在函数


直接用container_of(pnr, struct pid,numbers[ns->level])来通过numbers[]中的upid来找到
upid相关的pid,这里假设ontainer_of(pnr, struct pid, numbers[ns->level]);中的ns->level等于3
,那么numbers[3]在哪里,numbers[]是怎么进行扩展的?

总之    ”numbers[1]形式上只有一个数组项,但实际上可以根据需要进行扩展“  怎么个扩展法?
-----以上代码都摘自Linux源代码

论坛徽章:
3
双鱼座
日期:2013-09-04 19:47:39天蝎座
日期:2013-12-11 20:30:532015年亚洲杯之澳大利亚
日期:2015-04-20 00:28:02
2 [报告]
发表于 2013-11-11 22:42 |只看该作者

        pid = kmem_cache_alloc(ns->pid_cachep, GFP_KERNEL); 申请一个pid object的时候, 实际上分配的内存比struct pid{}还要大

具体见  create_pid_cachep()

    .....
      cachep = kmem_cache_create(pcache->name, sizeof(struct pid) + (nr_ids -1) * sizeof(struct upid));

论坛徽章:
0
3 [报告]
发表于 2013-11-12 09:36 |只看该作者
  1. #define UTS_RELEASE "2.6.35.7"
  2.         init_pid_ns.pid_cachep = KMEM_CACHE(pid, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
  3. #define KMEM_CACHE(__struct, __flags) kmem_cache_create(#__struct,\
  4.                 sizeof(struct __struct), __alignof__(struct __struct),\
  5.                 (__flags), NULL)
复制代码
我这边的是不是没有分配额外的空间啊?

论坛徽章:
0
4 [报告]
发表于 2013-11-12 09:36 |只看该作者
  1. #define UTS_RELEASE "2.6.35.7"
  2.         init_pid_ns.pid_cachep = KMEM_CACHE(pid, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
  3. #define KMEM_CACHE(__struct, __flags) kmem_cache_create(#__struct,\
  4.                 sizeof(struct __struct), __alignof__(struct __struct),\
  5.                 (__flags), NULL)
复制代码
我这边的是不是没有分配额外的空间啊?

论坛徽章:
0
5 [报告]
发表于 2013-11-12 09:36 |只看该作者
  1. #define UTS_RELEASE "2.6.35.7"
  2.         init_pid_ns.pid_cachep = KMEM_CACHE(pid, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
  3. #define KMEM_CACHE(__struct, __flags) kmem_cache_create(#__struct,\
  4.                 sizeof(struct __struct), __alignof__(struct __struct),\
  5.                 (__flags), NULL)
复制代码
我这边的是不是没有分配额外的空间啊?

论坛徽章:
0
6 [报告]
发表于 2013-11-12 09:36 |只看该作者
  1. #define UTS_RELEASE "2.6.35.7"
  2.         init_pid_ns.pid_cachep = KMEM_CACHE(pid, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
  3. #define KMEM_CACHE(__struct, __flags) kmem_cache_create(#__struct,\
  4.                 sizeof(struct __struct), __alignof__(struct __struct),\
  5.                 (__flags), NULL)
复制代码
我这边的是不是没有分配额外的空间啊?

论坛徽章:
0
7 [报告]
发表于 2013-11-12 09:36 |只看该作者
  1. #define UTS_RELEASE "2.6.35.7"
  2.         init_pid_ns.pid_cachep = KMEM_CACHE(pid, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
  3. #define KMEM_CACHE(__struct, __flags) kmem_cache_create(#__struct,\
  4.                 sizeof(struct __struct), __alignof__(struct __struct),\
  5.                 (__flags), NULL)
复制代码
我这边的是不是没有分配额外的空间啊?

论坛徽章:
0
8 [报告]
发表于 2013-11-12 09:36 |只看该作者
  1. #define UTS_RELEASE "2.6.35.7"
  2.         init_pid_ns.pid_cachep = KMEM_CACHE(pid, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
  3. #define KMEM_CACHE(__struct, __flags) kmem_cache_create(#__struct,\
  4.                 sizeof(struct __struct), __alignof__(struct __struct),\
  5.                 (__flags), NULL)
复制代码
我这边的是不是没有分配额外的空间啊?

论坛徽章:
0
9 [报告]
发表于 2013-11-12 09:36 |只看该作者
  1. #define UTS_RELEASE "2.6.35.7"
  2.         init_pid_ns.pid_cachep = KMEM_CACHE(pid, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
  3. #define KMEM_CACHE(__struct, __flags) kmem_cache_create(#__struct,\
  4.                 sizeof(struct __struct), __alignof__(struct __struct),\
  5.                 (__flags), NULL)
复制代码
我这边的是不是没有分配额外的空间啊?

论坛徽章:
3
双鱼座
日期:2013-09-04 19:47:39天蝎座
日期:2013-12-11 20:30:532015年亚洲杯之澳大利亚
日期:2015-04-20 00:28:02
10 [报告]
发表于 2013-11-12 15:15 |只看该作者
回复 9# hellozhoup


    你发的是创建pid_namespace 描述符的slab cache, 我说的是创建pid描述符的slab cache;

    不是在pid_namespace_init()函数, 在create_pid_cachep()函数
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP