RCU mechanism Linux device drivers

Read - copy - update (read-copy-update, RCU) is a high-level mutual exclusion mechanism, under the right conditions, can achieve high performance;

RCU-protected data structures it has done some limited, but it is rarely the case for the read write occurs frequently optimized, protected resources should be accessed through pointers, and references to the code must have only made up of atoms of these resources, when modify the data structure, the write thread first copy, and then modify the copy, and then replace the associated pointer with the new version, when the older version of the kernel sure there are no other references; in the code range RCU-protected can not go to sleep you can release the old version;

Use RCU code should contain <linux / rcupdate.h> header file;

RCU use the following read operation, note pointer dereferencing RCU not be used outside the lock;

1 rcu_read_lock();
2 
3 p = rcu_dereference(ptr);
4 if (p != NULL) {
5     /* 读取操作 */
6 }
7 
8 rcu_read_unlock();

 

RCU write operation use is as follows, first need to assign a new structure, if necessary, from the old copy data structure, then the read pointer replace the code can be seen;

1 struct some_struct * new_ptr = kmalloc(...);
2 
3 new_prt->xxx = xx;
4 
5 rcu_assign_pointer(ptr, new_ptr);

After the completion of the write end, all that remains is to release the old data structure; of course, this time the code running on other processors may throw in references to the old data, and therefore can not immediately release the old structure; write code that must wait until We can be confident that such a reference does not exist;

Because they have a reference to the data structure of the code must be atomic, so we can know, once the system each processor called at least once, all references will be gone; therefore, RCU do is set a callback and wait for all of the processors are scheduled after the completion of clean-up work by the callback function;

RCU provides two functions are synchronous and asynchronous wait for the end to read all waiting to release resources after the end of the reading of the old structure;

Synchronization: synchronize_rcu (), this function is called to wait for the end of all the modifications after reading cited references, the old resources to be released after the function returns;

1 void synchronize_rcu(void);

Asynchronous: call_rcu (), to register a callback function to release the old resources through this function, after all the read access is completed, the registered callback function, a release the old resources;

1 void call_rcu(struct rcu_head *head, rcu_callback_t func);

 

Guess you like

Origin www.cnblogs.com/wanpengcoder/p/11760776.html