1RCU and lockdep checking 2 3All flavors of RCU have lockdep checking available, so that lockdep is 4aware of when each task enters and leaves any flavor of RCU read-side 5critical section. Each flavor of RCU is tracked separately (but note 6that this is not the case in 2.6.32 and earlier). This allows lockdep's 7tracking to include RCU state, which can sometimes help when debugging 8deadlocks and the like. 9 10In addition, RCU provides the following primitives that check lockdep's 11state: 12 13 rcu_read_lock_held() for normal RCU. 14 rcu_read_lock_bh_held() for RCU-bh. 15 rcu_read_lock_sched_held() for RCU-sched. 16 srcu_read_lock_held() for SRCU. 17 18These functions are conservative, and will therefore return 1 if they 19aren't certain (for example, if CONFIG_DEBUG_LOCK_ALLOC is not set). 20This prevents things like WARN_ON(!rcu_read_lock_held()) from giving false 21positives when lockdep is disabled. 22 23In addition, a separate kernel config parameter CONFIG_PROVE_RCU enables 24checking of rcu_dereference() primitives: 25 26 rcu_dereference(p): 27 Check for RCU read-side critical section. 28 rcu_dereference_bh(p): 29 Check for RCU-bh read-side critical section. 30 rcu_dereference_sched(p): 31 Check for RCU-sched read-side critical section. 32 srcu_dereference(p, sp): 33 Check for SRCU read-side critical section. 34 rcu_dereference_check(p, c): 35 Use explicit check expression "c" along with 36 rcu_read_lock_held(). This is useful in code that is 37 invoked by both RCU readers and updaters. 38 rcu_dereference_bh_check(p, c): 39 Use explicit check expression "c" along with 40 rcu_read_lock_bh_held(). This is useful in code that 41 is invoked by both RCU-bh readers and updaters. 42 rcu_dereference_sched_check(p, c): 43 Use explicit check expression "c" along with 44 rcu_read_lock_sched_held(). This is useful in code that 45 is invoked by both RCU-sched readers and updaters. 46 srcu_dereference_check(p, c): 47 Use explicit check expression "c" along with 48 srcu_read_lock_held()(). This is useful in code that 49 is invoked by both SRCU readers and updaters. 50 rcu_dereference_raw(p): 51 Don't check. (Use sparingly, if at all.) 52 rcu_dereference_protected(p, c): 53 Use explicit check expression "c", and omit all barriers 54 and compiler constraints. This is useful when the data 55 structure cannot change, for example, in code that is 56 invoked only by updaters. 57 rcu_access_pointer(p): 58 Return the value of the pointer and omit all barriers, 59 but retain the compiler constraints that prevent duplicating 60 or coalescsing. This is useful when when testing the 61 value of the pointer itself, for example, against NULL. 62 63The rcu_dereference_check() check expression can be any boolean 64expression, but would normally include a lockdep expression. However, 65any boolean expression can be used. For a moderately ornate example, 66consider the following: 67 68 file = rcu_dereference_check(fdt->fd[fd], 69 lockdep_is_held(&files->file_lock) || 70 atomic_read(&files->count) == 1); 71 72This expression picks up the pointer "fdt->fd[fd]" in an RCU-safe manner, 73and, if CONFIG_PROVE_RCU is configured, verifies that this expression 74is used in: 75 761. An RCU read-side critical section (implicit), or 772. with files->file_lock held, or 783. on an unshared files_struct. 79 80In case (1), the pointer is picked up in an RCU-safe manner for vanilla 81RCU read-side critical sections, in case (2) the ->file_lock prevents 82any change from taking place, and finally, in case (3) the current task 83is the only task accessing the file_struct, again preventing any change 84from taking place. If the above statement was invoked only from updater 85code, it could instead be written as follows: 86 87 file = rcu_dereference_protected(fdt->fd[fd], 88 lockdep_is_held(&files->file_lock) || 89 atomic_read(&files->count) == 1); 90 91This would verify cases #2 and #3 above, and furthermore lockdep would 92complain if this was used in an RCU read-side critical section unless one 93of these two cases held. Because rcu_dereference_protected() omits all 94barriers and compiler constraints, it generates better code than do the 95other flavors of rcu_dereference(). On the other hand, it is illegal 96to use rcu_dereference_protected() if either the RCU-protected pointer 97or the RCU-protected data that it points to can change concurrently. 98 99Like rcu_dereference(), when lockdep is enabled, RCU list and hlist 100traversal primitives check for being called from within an RCU read-side 101critical section. However, a lockdep expression can be passed to them 102as a additional optional argument. With this lockdep expression, these 103traversal primitives will complain only if the lockdep expression is 104false and they are called from outside any RCU read-side critical section. 105 106For example, the workqueue for_each_pwq() macro is intended to be used 107either within an RCU read-side critical section or with wq->mutex held. 108It is thus implemented as follows: 109 110 #define for_each_pwq(pwq, wq) 111 list_for_each_entry_rcu((pwq), &(wq)->pwqs, pwqs_node, 112 lock_is_held(&(wq->mutex).dep_map)) 113