Skip to content

Commit 8ca6008

Browse files
Peter Zijlstraelektroschmock
Peter Zijlstra
authored andcommitted
sched: Introduce preempt_count accessor functions
Replace the single preempt_count() 'function' that's an lvalue with two proper functions: preempt_count() - returns the preempt_count value as rvalue preempt_count_set() - Allows setting the preempt-count value Also provide preempt_count_ptr() as a convenience wrapper to implement all modifying operations. Signed-off-by: Peter Zijlstra <[email protected]> Link: http://lkml.kernel.org/n/[email protected] [ Fixed build failure. ] Signed-off-by: Ingo Molnar <[email protected]> Change-Id: I7e1c2f3ccd750a7be5fb08b4568494eaed26eb5d
1 parent bc6bcad commit 8ca6008

File tree

7 files changed

+30
-18
lines changed

7 files changed

+30
-18
lines changed

include/linux/preempt.h

+19-6
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,32 @@
1010
#include <linux/linkage.h>
1111
#include <linux/list.h>
1212

13+
static __always_inline int preempt_count(void)
14+
{
15+
return current_thread_info()->preempt_count;
16+
}
17+
18+
static __always_inline int *preempt_count_ptr(void)
19+
{
20+
return &current_thread_info()->preempt_count;
21+
}
22+
23+
static __always_inline void preempt_count_set(int pc)
24+
{
25+
*preempt_count_ptr() = pc;
26+
}
27+
1328
#if defined(CONFIG_DEBUG_PREEMPT) || defined(CONFIG_PREEMPT_TRACER)
1429
extern void add_preempt_count(int val);
1530
extern void sub_preempt_count(int val);
1631
#else
17-
# define add_preempt_count(val) do { preempt_count() += (val); } while (0)
18-
# define sub_preempt_count(val) do { preempt_count() -= (val); } while (0)
32+
# define add_preempt_count(val) do { *preempt_count_ptr() += (val); } while (0)
33+
# define sub_preempt_count(val) do { *preempt_count_ptr() -= (val); } while (0)
1934
#endif
2035

2136
#define inc_preempt_count() add_preempt_count(1)
2237
#define dec_preempt_count() sub_preempt_count(1)
2338

24-
#define preempt_count() (current_thread_info()->preempt_count)
25-
2639
#ifdef CONFIG_PREEMPT
2740

2841
asmlinkage void preempt_schedule(void);
@@ -81,9 +94,9 @@ do { \
8194

8295
/* For debugging and tracer internals only! */
8396
#define add_preempt_count_notrace(val) \
84-
do { preempt_count() += (val); } while (0)
97+
do { *preempt_count_ptr() += (val); } while (0)
8598
#define sub_preempt_count_notrace(val) \
86-
do { preempt_count() -= (val); } while (0)
99+
do { *preempt_count_ptr() -= (val); } while (0)
87100
#define inc_preempt_count_notrace() add_preempt_count_notrace(1)
88101
#define dec_preempt_count_notrace() sub_preempt_count_notrace(1)
89102

init/main.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,7 @@ int __init_or_module do_one_initcall(initcall_t fn)
697697

698698
if (preempt_count() != count) {
699699
sprintf(msgbuf, "preemption imbalance ");
700-
preempt_count() = count;
700+
preempt_count_set(count);
701701
}
702702
if (irqs_disabled()) {
703703
strlcat(msgbuf, "disabled interrupts ", sizeof(msgbuf));

kernel/sched/core.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -3074,7 +3074,7 @@ void __kprobes add_preempt_count(int val)
30743074
if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0)))
30753075
return;
30763076
#endif
3077-
preempt_count() += val;
3077+
add_preempt_count_notrace(val);
30783078
#ifdef CONFIG_DEBUG_PREEMPT
30793079
/*
30803080
* Spinlock count overflowing soon?
@@ -3105,7 +3105,7 @@ void __kprobes sub_preempt_count(int val)
31053105

31063106
if (preempt_count() == val)
31073107
trace_preempt_on(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
3108-
preempt_count() -= val;
3108+
sub_preempt_count_notrace(val);
31093109
}
31103110
EXPORT_SYMBOL(sub_preempt_count);
31113111

kernel/softirq.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ static void __local_bh_disable(unsigned long ip, unsigned int cnt)
104104
* We must manually increment preempt_count here and manually
105105
* call the trace_preempt_off later.
106106
*/
107-
preempt_count() += cnt;
107+
add_preempt_count_notrace(cnt);
108108
/*
109109
* Were softirqs turned off above:
110110
*/
@@ -254,7 +254,7 @@ asmlinkage void __do_softirq(void)
254254
" exited with %08x?\n", vec_nr,
255255
softirq_to_name[vec_nr], h->action,
256256
prev_count, preempt_count());
257-
preempt_count() = prev_count;
257+
preempt_count_set(prev_count);
258258
}
259259

260260
rcu_bh_qs(cpu);

kernel/time/timer.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -1068,7 +1068,7 @@ static int cascade(struct tvec_base *base, struct tvec *tv, int index)
10681068
static void call_timer_fn(struct timer_list *timer, void (*fn)(unsigned long),
10691069
unsigned long data)
10701070
{
1071-
int preempt_count = preempt_count();
1071+
int count = preempt_count();
10721072

10731073
#ifdef CONFIG_LOCKDEP
10741074
/*
@@ -1095,16 +1095,16 @@ static void call_timer_fn(struct timer_list *timer, void (*fn)(unsigned long),
10951095

10961096
lock_map_release(&lockdep_map);
10971097

1098-
if (preempt_count != preempt_count()) {
1098+
if (count != preempt_count()) {
10991099
WARN_ONCE(1, "timer: %pF preempt leak: %08x -> %08x\n",
1100-
fn, preempt_count, preempt_count());
1100+
fn, count, preempt_count());
11011101
/*
11021102
* Restore the preempt count. That gives us a decent
11031103
* chance to survive and extract information. If the
11041104
* callback kept a lock held, bad luck, but not worse
11051105
* than the BUG() we had.
11061106
*/
1107-
preempt_count() = preempt_count;
1107+
preempt_count_set(count);
11081108
}
11091109
}
11101110

lib/locking-selftest.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -980,7 +980,7 @@ static void dotest(void (*testcase_fn)(void), int expected, int lockclass_mask)
980980
* Some tests (e.g. double-unlock) might corrupt the preemption
981981
* count, so restore it:
982982
*/
983-
preempt_count() = saved_preempt_count;
983+
preempt_count_set(saved_preempt_count);
984984
#ifdef CONFIG_TRACE_IRQFLAGS
985985
if (softirq_count())
986986
current->softirqs_enabled = 0;

lib/smp_processor_id.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@
99

1010
notrace unsigned int debug_smp_processor_id(void)
1111
{
12-
unsigned long preempt_count = preempt_count();
1312
int this_cpu = raw_smp_processor_id();
1413

15-
if (likely(preempt_count))
14+
if (likely(preempt_count()))
1615
goto out;
1716

1817
if (irqs_disabled())

0 commit comments

Comments
 (0)