|
|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
: C5 T+ e; ]% C4 V! C0 U& w在上一篇文章:Linux内核里的“智能指针”里介绍了Linux内核如何使用引用计数来更加安全的管理内存,本文承接前篇,主要介绍几点使用kref时的注意事项。
; t2 C: r! r7 [3 n1 a$ i3 t/ n& ^
Linux内核文档kref.txt罗列了三条规则,我们在使用kref时必须遵守。
7 l8 @: G. l+ h7 U
" b5 @- D! H4 V$ K7 [( t规则一:3 a# E; s3 x; z' }% e$ n/ O
& g F# t8 Q8 v# G' g5 iIf 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;. S2 B0 w4 e2 X* t! ]$ m. C
0 q/ }4 g0 |0 L7 Y V% G! I8 t1 ~ Y规则二:% n$ C {- H5 x! N8 J$ t
5 L6 z3 C! [" ^) A1 s/ v! d. p
When you are done with a pointer, you must call kref_put();
( x4 a K; j- ~3 f
4 y( h; M Y" [规则三:# J7 X8 T. x1 l( Z: } r
. U' k( A7 x- [! b
If 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().
! p) ~4 A( @. I. @: Y( S1 A3 `$ @: H4 l/ D+ n, @: Y- Y- l! H u
4 _. W, X i( k$ ^' M7 d! e' [# |6 T7 i- T( b* @6 c
对于规则一,其实主要是针对多条执行路径(比如另起一个线程)的情况。如果是在单一的执行路径里,比如把指针传递给一个函数,是不需要使用kref_get的。看下面这个例子:
% ~: a. Q W, s6 L
U" u$ A" r: ?* b' W- T- 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);
" B/ l5 b3 o2 D- O. f& Y 6 \+ U* `0 l7 i, q; k/ N. v
# E1 z; \" \1 ~5 Z
您是不是觉得call_something前后的一对kref_get和kref_put很多余呢?obj并没有逃出我们的掌控,所以它们确实是没有必要的。
\" N$ ]/ U$ O5 @$ V# |. r0 o0 X" g0 q T1 e
但是当遇到多条执行路径的情况就完全不一样了,我们必须遵守规则一。下面是摘自内核文档里的一个例子:
z8 |; p" p3 K3 r; u J4 ?* D: [4 `* s+ @
- 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;
- }
4 A( I! t- M) m8 Y0 L! { \& c) c1 l3 _4 Y% h. p( B: b
& Y; Y6 _4 ]: @! P$ A
因为我们并不知道线程more_data_handling何时结束,所以要用kref_get来保护我们的数据。
- c6 z# t6 J; K3 v$ x& ]! W0 H' m4 L* z! i. z6 F; _' Z8 [
注意规则一里的那个单词“before",kref_get必须是在传递指针之前进行,在本例里就是在调用kthread_run之前就要执行kref_get,否则,何谈保护呢?
# M, i) h2 S& i; P+ W* I$ G* t) h$ q' Z( v
+ \ \6 ~! I* a& p, b* Q& ?
% c5 E5 ]2 e( ]对于规则二我们就不必多说了,前面调用了kref_get,自然要配对使用kref_put。- b6 v% c9 c9 y3 Y6 s( a
0 m9 H1 V$ n$ R, W" T. p+ F + R/ @0 A0 J# v- m$ U! n
* n; C0 O3 w0 Y; J3 x0 G
规则三主要是处理遇到链表的情况。我们假设一个情景,如果有一个链表摆在你的面前,链表里的节点是用引用计数保护的,那你如何操作呢?首先我们需要获得节点的指针,然后才可能调用kref_get来增加该节点的引用计数。根据规则三,这种情况下我们要对上述的两个动作串行化处理,一般我们可以用mutex来实现。请看下面这个例子:2 D: T3 N" c: p3 r9 t
" |# q- O9 V( G! H- X0 C- 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);
- }: w2 W8 K& i3 o5 {: T6 ?
! i9 O2 j8 I. O9 O6 [
# l1 J) r+ p+ l! W2 t
这个例子里已经用mutex来进行保护了,假如我们把mutex拿掉,会出现什么情况?记住,我们遇到的很可能是多线程操作。如果线程A在用container_of取得entry指针之后、调用kref_get之前,被线程B抢先执行,而线程B碰巧又做的是kref_put的操作,当线程A恢复执行时一定会出现内存访问的错误,所以,遇到这种情况一定要串行化处理。6 ^; L( @0 L/ I
( _0 v2 P! J$ w& U / J3 b% l/ K( L. K r# s# W6 W
( R' F+ Z- Z7 k
我们在使用kref的时候要严格遵循这三条规则,才能安全有效的管理数据。 |
|