Skip to content

Commit 51816e9

Browse files
Waiman-Longtorvalds
authored andcommitted
locking/lock_events: Use this_cpu_add() when necessary
The kernel test robot has reported that the use of __this_cpu_add() causes bug messages like: BUG: using __this_cpu_add() in preemptible [00000000] code: ... Given the imprecise nature of the count and the possibility of resetting the count and doing the measurement again, this is not really a big problem to use the unprotected __this_cpu_*() functions. To make the preemption checking code happy, the this_cpu_*() functions will be used if CONFIG_DEBUG_PREEMPT is defined. The imprecise nature of the locking counts are also documented with the suggestion that we should run the measurement a few times with the counts reset in between to get a better picture of what is going on under the hood. Fixes: a865459 ("locking/rwsem: Enable lock event counting") Suggested-by: Linus Torvalds <[email protected]> Signed-off-by: Waiman Long <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 0a72ef8 commit 51816e9

File tree

1 file changed

+40
-2
lines changed

1 file changed

+40
-2
lines changed

kernel/locking/lock_events.h

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,59 @@ enum lock_events {
3030
*/
3131
DECLARE_PER_CPU(unsigned long, lockevents[lockevent_num]);
3232

33+
/*
34+
* The purpose of the lock event counting subsystem is to provide a low
35+
* overhead way to record the number of specific locking events by using
36+
* percpu counters. It is the percpu sum that matters, not specifically
37+
* how many of them happens in each cpu.
38+
*
39+
* It is possible that the same percpu counter may be modified in both
40+
* the process and interrupt contexts. For architectures that perform
41+
* percpu operation with multiple instructions, it is possible to lose
42+
* count if a process context percpu update is interrupted in the middle
43+
* and the same counter is updated in the interrupt context. Therefore,
44+
* the generated percpu sum may not be precise. The error, if any, should
45+
* be small and insignificant.
46+
*
47+
* For those architectures that do multi-instruction percpu operation,
48+
* preemption in the middle and moving the task to another cpu may cause
49+
* a larger error in the count. Again, this will be few and far between.
50+
* Given the imprecise nature of the count and the possibility of resetting
51+
* the count and doing the measurement again, this is not really a big
52+
* problem.
53+
*
54+
* To get a better picture of what is happening under the hood, it is
55+
* suggested that a few measurements should be taken with the counts
56+
* reset in between to stamp out outliner because of these possible
57+
* error conditions.
58+
*
59+
* To minimize overhead, we use __this_cpu_*() in all cases except when
60+
* CONFIG_DEBUG_PREEMPT is defined. In this particular case, this_cpu_*()
61+
* will be used to avoid the appearance of unwanted BUG messages.
62+
*/
63+
#ifdef CONFIG_DEBUG_PREEMPT
64+
#define lockevent_percpu_inc(x) this_cpu_inc(x)
65+
#define lockevent_percpu_add(x, v) this_cpu_add(x, v)
66+
#else
67+
#define lockevent_percpu_inc(x) __this_cpu_inc(x)
68+
#define lockevent_percpu_add(x, v) __this_cpu_add(x, v)
69+
#endif
70+
3371
/*
3472
* Increment the PV qspinlock statistical counters
3573
*/
3674
static inline void __lockevent_inc(enum lock_events event, bool cond)
3775
{
3876
if (cond)
39-
__this_cpu_inc(lockevents[event]);
77+
lockevent_percpu_inc(lockevents[event]);
4078
}
4179

4280
#define lockevent_inc(ev) __lockevent_inc(LOCKEVENT_ ##ev, true)
4381
#define lockevent_cond_inc(ev, c) __lockevent_inc(LOCKEVENT_ ##ev, c)
4482

4583
static inline void __lockevent_add(enum lock_events event, int inc)
4684
{
47-
__this_cpu_add(lockevents[event], inc);
85+
lockevent_percpu_add(lockevents[event], inc);
4886
}
4987

5088
#define lockevent_add(ev, c) __lockevent_add(LOCKEVENT_ ##ev, c)

0 commit comments

Comments
 (0)