|
|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
4 |: s- l7 j1 u, n4 e1 _在上一篇文章:Linux内核里的“智能指针”里介绍了Linux内核如何使用引用计数来更加安全的管理内存,本文承接前篇,主要介绍几点使用kref时的注意事项。& ] l! R5 I6 O/ {: [8 J9 G
; M5 V* [& F2 D" \' G# I
Linux内核文档kref.txt罗列了三条规则,我们在使用kref时必须遵守。
[/ z; o: Z9 K/ w' x: \+ |! v* o
+ H" A$ w, s7 d8 p# A7 B规则一:, m. D- ]# Y* C) @; R" f
. q# }# C9 f* v1 B S" L& J+ B* OIf 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;
& Z/ N9 _& A6 J
1 j0 H$ u1 \2 G9 u% o/ a规则二:
9 |5 v) u% O0 @" A' v
2 B' m& b4 c% G! B/ ^; vWhen you are done with a pointer, you must call kref_put();
1 H0 i1 A$ U3 o& U& v! h. y( U& h( @) q9 f9 ~: A0 \
规则三:, U7 S$ S" |& i' l j/ {9 @2 r7 f R
9 p- \: C1 r4 a% I1 R. O8 V$ O3 p: kIf 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().' V: |. R& ]$ ~% Q: |
9 V* q+ j" ]: a+ w1 V
) _) t. L% w; Y
$ R9 W5 v9 U" A对于规则一,其实主要是针对多条执行路径(比如另起一个线程)的情况。如果是在单一的执行路径里,比如把指针传递给一个函数,是不需要使用kref_get的。看下面这个例子:
7 x4 L, g; V6 N% }( ?1 _6 l2 {* U) E; G) g
- 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);
' G( Q' U' k/ Q5 V/ q 2 P) d2 @9 F8 Y) P
; T- m3 K) T+ r/ h) f c2 T 您是不是觉得call_something前后的一对kref_get和kref_put很多余呢?obj并没有逃出我们的掌控,所以它们确实是没有必要的。1 j3 Z4 G; L! a% g
: N! m& j, @. g但是当遇到多条执行路径的情况就完全不一样了,我们必须遵守规则一。下面是摘自内核文档里的一个例子:
2 G" [4 R' A" Y$ t0 j/ B2 v, J6 O
& v' x5 g* F: i8 T. j- 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;
- }% J5 F- G+ L- `4 L5 v1 C
- E4 k7 @, u. q/ v! \. G4 j) |+ B% L" X* u S4 G& o
因为我们并不知道线程more_data_handling何时结束,所以要用kref_get来保护我们的数据。
) B- t* ]" Y% X+ a0 e8 }/ ?- }' g" L& \- I8 q& t) U8 O
注意规则一里的那个单词“before",kref_get必须是在传递指针之前进行,在本例里就是在调用kthread_run之前就要执行kref_get,否则,何谈保护呢?
% n7 r* w- y$ P6 f1 o, t, Y3 [0 {4 \- V9 q, Z# ?& O0 Z' ]0 w: [$ ?
, [5 p/ P# e$ \
$ G7 b3 n' }2 e1 Q- X; h+ b3 u对于规则二我们就不必多说了,前面调用了kref_get,自然要配对使用kref_put。
1 Y* \6 q0 v( |, j: t; _% E/ N
8 v' z6 h9 U) j$ X . z0 u [) g5 d' C( G. a. Y
2 g5 P6 q( E% L$ S. m
规则三主要是处理遇到链表的情况。我们假设一个情景,如果有一个链表摆在你的面前,链表里的节点是用引用计数保护的,那你如何操作呢?首先我们需要获得节点的指针,然后才可能调用kref_get来增加该节点的引用计数。根据规则三,这种情况下我们要对上述的两个动作串行化处理,一般我们可以用mutex来实现。请看下面这个例子:
: s2 |; h2 A! k* `3 }$ t3 V# r7 M3 w1 v
- 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);
- }
- N- P, [+ g- B0 Q, @ 5 S% A; @& V) D1 P2 c2 H
7 M% p/ S: Y4 i# M) Q6 } 这个例子里已经用mutex来进行保护了,假如我们把mutex拿掉,会出现什么情况?记住,我们遇到的很可能是多线程操作。如果线程A在用container_of取得entry指针之后、调用kref_get之前,被线程B抢先执行,而线程B碰巧又做的是kref_put的操作,当线程A恢复执行时一定会出现内存访问的错误,所以,遇到这种情况一定要串行化处理。, {' C; J5 n* k8 Q, \8 {
2 U1 o8 N$ W6 ~: z5 s( V3 K5 j
+ z/ ]1 J" A: F6 @* N8 C& X I3 K$ P& T3 j( |/ y
我们在使用kref的时候要严格遵循这三条规则,才能安全有效的管理数据。 |
|