120 lines · plain
1.. SPDX-License-Identifier: GPL-2.02 3========================4RCU and lockdep checking5========================6 7All flavors of RCU have lockdep checking available, so that lockdep is8aware of when each task enters and leaves any flavor of RCU read-side9critical section. Each flavor of RCU is tracked separately (but note10that this is not the case in 2.6.32 and earlier). This allows lockdep's11tracking to include RCU state, which can sometimes help when debugging12deadlocks and the like.13 14In addition, RCU provides the following primitives that check lockdep's15state::16 17 rcu_read_lock_held() for normal RCU.18 rcu_read_lock_bh_held() for RCU-bh.19 rcu_read_lock_sched_held() for RCU-sched.20 rcu_read_lock_any_held() for any of normal RCU, RCU-bh, and RCU-sched.21 srcu_read_lock_held() for SRCU.22 rcu_read_lock_trace_held() for RCU Tasks Trace.23 24These functions are conservative, and will therefore return 1 if they25aren't certain (for example, if CONFIG_DEBUG_LOCK_ALLOC is not set).26This prevents things like WARN_ON(!rcu_read_lock_held()) from giving false27positives when lockdep is disabled.28 29In addition, a separate kernel config parameter CONFIG_PROVE_RCU enables30checking of rcu_dereference() primitives:31 32 rcu_dereference(p):33 Check for RCU read-side critical section.34 rcu_dereference_bh(p):35 Check for RCU-bh read-side critical section.36 rcu_dereference_sched(p):37 Check for RCU-sched read-side critical section.38 srcu_dereference(p, sp):39 Check for SRCU read-side critical section.40 rcu_dereference_check(p, c):41 Use explicit check expression "c" along with42 rcu_read_lock_held(). This is useful in code that is43 invoked by both RCU readers and updaters.44 rcu_dereference_bh_check(p, c):45 Use explicit check expression "c" along with46 rcu_read_lock_bh_held(). This is useful in code that47 is invoked by both RCU-bh readers and updaters.48 rcu_dereference_sched_check(p, c):49 Use explicit check expression "c" along with50 rcu_read_lock_sched_held(). This is useful in code that51 is invoked by both RCU-sched readers and updaters.52 srcu_dereference_check(p, c):53 Use explicit check expression "c" along with54 srcu_read_lock_held(). This is useful in code that55 is invoked by both SRCU readers and updaters.56 rcu_dereference_raw(p):57 Don't check. (Use sparingly, if at all.)58 rcu_dereference_raw_check(p):59 Don't do lockdep at all. (Use sparingly, if at all.)60 rcu_dereference_protected(p, c):61 Use explicit check expression "c", and omit all barriers62 and compiler constraints. This is useful when the data63 structure cannot change, for example, in code that is64 invoked only by updaters.65 rcu_access_pointer(p):66 Return the value of the pointer and omit all barriers,67 but retain the compiler constraints that prevent duplicating68 or coalescing. This is useful when testing the69 value of the pointer itself, for example, against NULL.70 71The rcu_dereference_check() check expression can be any boolean72expression, but would normally include a lockdep expression. For a73moderately ornate example, consider the following::74 75 file = rcu_dereference_check(fdt->fd[fd],76 lockdep_is_held(&files->file_lock) ||77 atomic_read(&files->count) == 1);78 79This expression picks up the pointer "fdt->fd[fd]" in an RCU-safe manner,80and, if CONFIG_PROVE_RCU is configured, verifies that this expression81is used in:82 831. An RCU read-side critical section (implicit), or842. with files->file_lock held, or853. on an unshared files_struct.86 87In case (1), the pointer is picked up in an RCU-safe manner for vanilla88RCU read-side critical sections, in case (2) the ->file_lock prevents89any change from taking place, and finally, in case (3) the current task90is the only task accessing the file_struct, again preventing any change91from taking place. If the above statement was invoked only from updater92code, it could instead be written as follows::93 94 file = rcu_dereference_protected(fdt->fd[fd],95 lockdep_is_held(&files->file_lock) ||96 atomic_read(&files->count) == 1);97 98This would verify cases #2 and #3 above, and furthermore lockdep would99complain even if this was used in an RCU read-side critical section unless100one of these two cases held. Because rcu_dereference_protected() omits101all barriers and compiler constraints, it generates better code than do102the other flavors of rcu_dereference(). On the other hand, it is illegal103to use rcu_dereference_protected() if either the RCU-protected pointer104or the RCU-protected data that it points to can change concurrently.105 106Like rcu_dereference(), when lockdep is enabled, RCU list and hlist107traversal primitives check for being called from within an RCU read-side108critical section. However, a lockdep expression can be passed to them109as a additional optional argument. With this lockdep expression, these110traversal primitives will complain only if the lockdep expression is111false and they are called from outside any RCU read-side critical section.112 113For example, the workqueue for_each_pwq() macro is intended to be used114either within an RCU read-side critical section or with wq->mutex held.115It is thus implemented as follows::116 117 #define for_each_pwq(pwq, wq)118 list_for_each_entry_rcu((pwq), &(wq)->pwqs, pwqs_node,119 lock_is_held(&(wq->mutex).dep_map))120