找回密码
 注册
关于网站域名变更的通知
查看: 596|回复: 2
打印 上一主题 下一主题

Linux内核里的“智能指针” (续)

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2021-4-27 18:12 | 只看该作者 回帖奖励 |正序浏览 |阅读模式

EDA365欢迎您登录!

您需要 登录 才可以下载或查看,没有帐号?注册

x

4 k0 c& v2 H* a5 d  E在上一篇文章:Linux内核里的“智能指针”里介绍了Linux内核如何使用引用计数来更加安全的管理内存,本文承接前篇,主要介绍几点使用kref时的注意事项。
- g5 R7 Q. Y% {8 ?! p& |: x, T0 X8 [4 Q5 U# z& o4 W
Linux内核文档kref.txt罗列了三条规则,我们在使用kref时必须遵守。* W, S( H! ^* Q7 ]* ?6 N% |1 B$ s

& m# F7 N) _& t4 P" I& ?& b6 B2 b7 h规则一:; D% v1 V9 c/ ?  K! I  C
1 u/ n: o$ b9 X$ R8 B. u/ P! ]
If you make a non-temporary copy of a pointer, especially if  it can be passed to another thread of execution, you must  increment the refcount with kref_get() before passing it off;
6 p# N  q- o7 O1 }) g1 D1 E5 @) ~4 e" q" Z; T1 N
规则二:
* x6 N' r! F3 c3 R
1 B, F$ d' U. l" X& p5 EWhen you are done with a pointer, you must call kref_put();  ?) R5 b, }0 e% b

. ~. V7 |* h2 z6 Y1 j规则三:/ z& r' S& D+ j

/ Y) l' j: O" M& g" Q9 mIf the code attempts to gain a reference to a kref-ed structure without already holding a valid pointer, it must serialize access where a kref_put() cannot occur during the kref_get(), and the   structure must remain valid during the kref_get().( q( t, W# F, m
. b0 \4 G, q8 M

$ V# A' \8 s' _. e1 ^" D( I: S
1 S" f* v0 {" V9 W( ~* F, f对于规则一,其实主要是针对多条执行路径(比如另起一个线程)的情况。如果是在单一的执行路径里,比如把指针传递给一个函数,是不需要使用kref_get的。看下面这个例子:
% ~3 e; F9 V" ]1 a9 `; C' x' \* U$ d6 _( A
  • kref_init(&obj->ref);
  • // do something here
  • // ...
  • kref_get(&obj->ref);
  • call_something(obj);
  • kref_put(&obj->ref);
  • // do something here
  • // ...
  • kref_put(&obj->ref);1 ~, \& j+ T5 E% e+ `& A

6 F. ]( T  m8 y* x+ X% b- }* p% h- e$ m% v
    您是不是觉得call_something前后的一对kref_get和kref_put很多余呢?obj并没有逃出我们的掌控,所以它们确实是没有必要的。) J/ I2 r8 \9 s

1 p3 `% O4 H/ L9 K, M+ G" V但是当遇到多条执行路径的情况就完全不一样了,我们必须遵守规则一。下面是摘自内核文档里的一个例子:) c$ v# {3 h' r# Z/ J
" B( Q! v3 ~3 R# H$ |
  • struct my_data
  • {
  •     .
  •     .
  •     struct kref refcount;
  •     .
  •     .
  • };
  • void data_release(struct kref *ref)
  • {
  •     struct my_data *data = container_of(ref, struct my_data, refcount);
  •     kfree(data);
  • }
  • void more_data_handling(void *cb_data)
  • {
  •     struct my_data *data = cb_data;
  •     .
  •     . do stuff with data here
  •     .
  •     kref_put(&data->refcount, data_release);
  • }
  • int my_data_handler(void)
  • {
  •     int rv = 0;
  •     struct my_data *data;
  •     struct task_struct *task;
  •     data = kmalloc(sizeof(*data), GFP_KERNEL);
  •     if (!data)
  •         return -ENOMEM;
  •     kref_init(&data->refcount);
  •     kref_get(&data->refcount);
  •     task = kthread_run(more_data_handling, data, "more_data_handling");
  •     if (task == ERR_PTR(-ENOMEM)) {
  •         rv = -ENOMEM;
  •         goto out;
  •     }
  •     .
  •     . do stuff with data here
  •     .
  • out:
  •     kref_put(&data->refcount, data_release);
  •     return rv;
  • }% l, j: T& Z' m" I
* \8 \! a$ r$ ?1 B* @6 b) x5 L

  [4 L# O, `3 O3 ?, M5 I     因为我们并不知道线程more_data_handling何时结束,所以要用kref_get来保护我们的数据。
  a" o( M! g  y5 P( a9 T8 k
+ O4 X7 F) }+ N2 s注意规则一里的那个单词“before",kref_get必须是在传递指针之前进行,在本例里就是在调用kthread_run之前就要执行kref_get,否则,何谈保护呢?4 w8 I8 ]% i6 M% n8 F
# e# H3 v, x5 d
! O3 h) u# {) s

; q# n* t! V$ K4 G; q" T' n, t( g9 O$ i对于规则二我们就不必多说了,前面调用了kref_get,自然要配对使用kref_put。/ x3 w4 f- X7 q: L( Z  p) z
& E) o, Z9 X  T+ g* y

& o8 m3 W+ L( i. X! B8 _5 o) j) d& o( P- e$ T
规则三主要是处理遇到链表的情况。我们假设一个情景,如果有一个链表摆在你的面前,链表里的节点是用引用计数保护的,那你如何操作呢?首先我们需要获得节点的指针,然后才可能调用kref_get来增加该节点的引用计数。根据规则三,这种情况下我们要对上述的两个动作串行化处理,一般我们可以用mutex来实现。请看下面这个例子:) o+ m! T* `$ t- M- P; l: B

. Z+ l! l7 m! O. s$ b
  • static DEFINE_MUTEX(mutex);
  • static LIST_HEAD(q);
  • struct my_data
  • {
  •     struct kref  refcount;
  •     struct list_head link;
  • };
  • static struct my_data *get_entry()
  • {
  •     struct my_data *entry = NULL;
  •     mutex_lock(&mutex);
  •     if (!list_empty(&q)) {
  •         entry = container_of(q.next, struct my_q_entry, link);
  •         kref_get(&entry->refcount);
  •     }
  •     mutex_unlock(&mutex);
  •     return entry;
  • }
  • static void release_entry(struct kref *ref)
  • {
  •     struct my_data *entry = container_of(ref, struct my_data, refcount);
  •     list_del(&entry->link);
  •     kfree(entry);
  • }
  • static void put_entry(struct my_data *entry)
  • {
  •     mutex_lock(&mutex);
  •     kref_put(&entry->refcount, release_entry);
  •     mutex_unlock(&mutex);
  • }
    ( s3 X0 Y2 s$ F
* X: I- q# T* B( v+ m% N

9 V$ w4 a3 _- K' ?5 E7 \4 t    这个例子里已经用mutex来进行保护了,假如我们把mutex拿掉,会出现什么情况?记住,我们遇到的很可能是多线程操作。如果线程A在用container_of取得entry指针之后、调用kref_get之前,被线程B抢先执行,而线程B碰巧又做的是kref_put的操作,当线程A恢复执行时一定会出现内存访问的错误,所以,遇到这种情况一定要串行化处理。
8 R! x+ w! D5 s/ ^1 }7 M4 q; G) q: v* J, i

4 h, o" u. W0 w* g) d) J& I9 e3 T3 P
我们在使用kref的时候要严格遵循这三条规则,才能安全有效的管理数据。

该用户从未签到

3#
发表于 2021-4-28 18:32 | 只看该作者
Linux内核里的“智能指针” (续)

该用户从未签到

2#
发表于 2021-4-27 18:45 | 只看该作者
Linux内核里的“智能指针” (续)
您需要登录后才可以回帖 登录 | 注册

本版积分规则

关闭

推荐内容上一条 /1 下一条

EDA365公众号

关于我们|手机版|EDA365电子论坛网 ( 粤ICP备18020198号-1 )

GMT+8, 2025-11-24 13:32 , Processed in 0.156250 second(s), 24 queries , Gzip On.

深圳市墨知创新科技有限公司

地址:深圳市南山区科技生态园2栋A座805 电话:19926409050

快速回复 返回顶部 返回列表