Skip to content

Commit 909ea96

Browse files
Christoph Lameterhtejun
authored andcommitted
core: Replace __get_cpu_var with __this_cpu_read if not used for an address.
__get_cpu_var() can be replaced with this_cpu_read and will then use a single read instruction with implied address calculation to access the correct per cpu instance. However, the address of a per cpu variable passed to __this_cpu_read() cannot be determined (since it's an implied address conversion through segment prefixes). Therefore apply this only to uses of __get_cpu_var where the address of the variable is not used. Cc: Pekka Enberg <penberg@cs.helsinki.fi> Cc: Hugh Dickins <hughd@google.com> Cc: Thomas Gleixner <tglx@linutronix.de> Acked-by: H. Peter Anvin <hpa@zytor.com> Signed-off-by: Christoph Lameter <cl@linux.com> Signed-off-by: Tejun Heo <tj@kernel.org>
1 parent 780f36d commit 909ea96

13 files changed

Lines changed: 60 additions & 66 deletions

File tree

‎include/asm-generic/irq_regs.h‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ DECLARE_PER_CPU(struct pt_regs *, __irq_regs);
2222

2323
static inline struct pt_regs *get_irq_regs(void)
2424
{
25-
return __get_cpu_var(__irq_regs);
25+
return __this_cpu_read(__irq_regs);
2626
}
2727

2828
static inline struct pt_regs *set_irq_regs(struct pt_regs *new_regs)
2929
{
30-
struct pt_regs *old_regs, **pp_regs = &__get_cpu_var(__irq_regs);
30+
struct pt_regs *old_regs;
3131

32-
old_regs = *pp_regs;
33-
*pp_regs = new_regs;
32+
old_regs = __this_cpu_read(__irq_regs);
33+
__this_cpu_write(__irq_regs, new_regs);
3434
return old_regs;
3535
}
3636

‎include/linux/elevator.h‎

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -195,15 +195,9 @@ enum {
195195
/*
196196
* io context count accounting
197197
*/
198-
#define elv_ioc_count_mod(name, __val) \
199-
do { \
200-
preempt_disable(); \
201-
__get_cpu_var(name) += (__val); \
202-
preempt_enable(); \
203-
} while (0)
204-
205-
#define elv_ioc_count_inc(name) elv_ioc_count_mod(name, 1)
206-
#define elv_ioc_count_dec(name) elv_ioc_count_mod(name, -1)
198+
#define elv_ioc_count_mod(name, __val) this_cpu_add(name, __val)
199+
#define elv_ioc_count_inc(name) this_cpu_inc(name)
200+
#define elv_ioc_count_dec(name) this_cpu_dec(name)
207201

208202
#define elv_ioc_count_read(name) \
209203
({ \

‎include/linux/kernel_stat.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ extern unsigned long long nr_context_switches(void);
4747

4848
#ifndef CONFIG_GENERIC_HARDIRQS
4949
#define kstat_irqs_this_cpu(irq) \
50-
(kstat_this_cpu.irqs[irq])
50+
(this_cpu_read(kstat.irqs[irq])
5151

5252
struct irq_desc;
5353

‎kernel/exit.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ static void __unhash_process(struct task_struct *p, bool group_dead)
6969

7070
list_del_rcu(&p->tasks);
7171
list_del_init(&p->sibling);
72-
__get_cpu_var(process_counts)--;
72+
__this_cpu_dec(process_counts);
7373
}
7474
list_del_rcu(&p->thread_group);
7575
}

‎kernel/fork.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1282,7 +1282,7 @@ static struct task_struct *copy_process(unsigned long clone_flags,
12821282
attach_pid(p, PIDTYPE_SID, task_session(current));
12831283
list_add_tail(&p->sibling, &p->real_parent->children);
12841284
list_add_tail_rcu(&p->tasks, &init_task.tasks);
1285-
__get_cpu_var(process_counts)++;
1285+
__this_cpu_inc(process_counts);
12861286
}
12871287
attach_pid(p, PIDTYPE_PID, pid);
12881288
nr_threads++;

‎kernel/hrtimer.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ static inline int hrtimer_is_hres_enabled(void)
497497
*/
498498
static inline int hrtimer_hres_active(void)
499499
{
500-
return __get_cpu_var(hrtimer_bases).hres_active;
500+
return __this_cpu_read(hrtimer_bases.hres_active);
501501
}
502502

503503
/*

‎kernel/printk.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,8 +1074,8 @@ static DEFINE_PER_CPU(int, printk_pending);
10741074

10751075
void printk_tick(void)
10761076
{
1077-
if (__get_cpu_var(printk_pending)) {
1078-
__get_cpu_var(printk_pending) = 0;
1077+
if (__this_cpu_read(printk_pending)) {
1078+
__this_cpu_write(printk_pending, 0);
10791079
wake_up_interruptible(&log_wait);
10801080
}
10811081
}

‎kernel/rcutree.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,8 +367,8 @@ void rcu_irq_exit(void)
367367
WARN_ON_ONCE(rdtp->dynticks & 0x1);
368368

369369
/* If the interrupt queued a callback, get out of dyntick mode. */
370-
if (__get_cpu_var(rcu_sched_data).nxtlist ||
371-
__get_cpu_var(rcu_bh_data).nxtlist)
370+
if (__this_cpu_read(rcu_sched_data.nxtlist) ||
371+
__this_cpu_read(rcu_bh_data.nxtlist))
372372
set_need_resched();
373373
}
374374

‎kernel/softirq.c‎

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ char *softirq_to_name[NR_SOFTIRQS] = {
7070
static void wakeup_softirqd(void)
7171
{
7272
/* Interrupts are disabled: no need to stop preemption */
73-
struct task_struct *tsk = __get_cpu_var(ksoftirqd);
73+
struct task_struct *tsk = __this_cpu_read(ksoftirqd);
7474

7575
if (tsk && tsk->state != TASK_RUNNING)
7676
wake_up_process(tsk);
@@ -388,8 +388,8 @@ void __tasklet_schedule(struct tasklet_struct *t)
388388

389389
local_irq_save(flags);
390390
t->next = NULL;
391-
*__get_cpu_var(tasklet_vec).tail = t;
392-
__get_cpu_var(tasklet_vec).tail = &(t->next);
391+
*__this_cpu_read(tasklet_vec.tail) = t;
392+
__this_cpu_write(tasklet_vec.tail, &(t->next));
393393
raise_softirq_irqoff(TASKLET_SOFTIRQ);
394394
local_irq_restore(flags);
395395
}
@@ -402,8 +402,8 @@ void __tasklet_hi_schedule(struct tasklet_struct *t)
402402

403403
local_irq_save(flags);
404404
t->next = NULL;
405-
*__get_cpu_var(tasklet_hi_vec).tail = t;
406-
__get_cpu_var(tasklet_hi_vec).tail = &(t->next);
405+
*__this_cpu_read(tasklet_hi_vec.tail) = t;
406+
__this_cpu_write(tasklet_hi_vec.tail, &(t->next));
407407
raise_softirq_irqoff(HI_SOFTIRQ);
408408
local_irq_restore(flags);
409409
}
@@ -414,8 +414,8 @@ void __tasklet_hi_schedule_first(struct tasklet_struct *t)
414414
{
415415
BUG_ON(!irqs_disabled());
416416

417-
t->next = __get_cpu_var(tasklet_hi_vec).head;
418-
__get_cpu_var(tasklet_hi_vec).head = t;
417+
t->next = __this_cpu_read(tasklet_hi_vec.head);
418+
__this_cpu_write(tasklet_hi_vec.head, t);
419419
__raise_softirq_irqoff(HI_SOFTIRQ);
420420
}
421421

@@ -426,9 +426,9 @@ static void tasklet_action(struct softirq_action *a)
426426
struct tasklet_struct *list;
427427

428428
local_irq_disable();
429-
list = __get_cpu_var(tasklet_vec).head;
430-
__get_cpu_var(tasklet_vec).head = NULL;
431-
__get_cpu_var(tasklet_vec).tail = &__get_cpu_var(tasklet_vec).head;
429+
list = __this_cpu_read(tasklet_vec.head);
430+
__this_cpu_write(tasklet_vec.head, NULL);
431+
__this_cpu_write(tasklet_vec.tail, &__get_cpu_var(tasklet_vec).head);
432432
local_irq_enable();
433433

434434
while (list) {
@@ -449,8 +449,8 @@ static void tasklet_action(struct softirq_action *a)
449449

450450
local_irq_disable();
451451
t->next = NULL;
452-
*__get_cpu_var(tasklet_vec).tail = t;
453-
__get_cpu_var(tasklet_vec).tail = &(t->next);
452+
*__this_cpu_read(tasklet_vec.tail) = t;
453+
__this_cpu_write(tasklet_vec.tail, &(t->next));
454454
__raise_softirq_irqoff(TASKLET_SOFTIRQ);
455455
local_irq_enable();
456456
}
@@ -461,9 +461,9 @@ static void tasklet_hi_action(struct softirq_action *a)
461461
struct tasklet_struct *list;
462462

463463
local_irq_disable();
464-
list = __get_cpu_var(tasklet_hi_vec).head;
465-
__get_cpu_var(tasklet_hi_vec).head = NULL;
466-
__get_cpu_var(tasklet_hi_vec).tail = &__get_cpu_var(tasklet_hi_vec).head;
464+
list = __this_cpu_read(tasklet_hi_vec.head);
465+
__this_cpu_write(tasklet_hi_vec.head, NULL);
466+
__this_cpu_write(tasklet_hi_vec.tail, &__get_cpu_var(tasklet_hi_vec).head);
467467
local_irq_enable();
468468

469469
while (list) {
@@ -484,8 +484,8 @@ static void tasklet_hi_action(struct softirq_action *a)
484484

485485
local_irq_disable();
486486
t->next = NULL;
487-
*__get_cpu_var(tasklet_hi_vec).tail = t;
488-
__get_cpu_var(tasklet_hi_vec).tail = &(t->next);
487+
*__this_cpu_read(tasklet_hi_vec.tail) = t;
488+
__this_cpu_write(tasklet_hi_vec.tail, &(t->next));
489489
__raise_softirq_irqoff(HI_SOFTIRQ);
490490
local_irq_enable();
491491
}
@@ -802,16 +802,16 @@ static void takeover_tasklets(unsigned int cpu)
802802

803803
/* Find end, append list for that CPU. */
804804
if (&per_cpu(tasklet_vec, cpu).head != per_cpu(tasklet_vec, cpu).tail) {
805-
*(__get_cpu_var(tasklet_vec).tail) = per_cpu(tasklet_vec, cpu).head;
806-
__get_cpu_var(tasklet_vec).tail = per_cpu(tasklet_vec, cpu).tail;
805+
*__this_cpu_read(tasklet_vec.tail) = per_cpu(tasklet_vec, cpu).head;
806+
this_cpu_write(tasklet_vec.tail, per_cpu(tasklet_vec, cpu).tail);
807807
per_cpu(tasklet_vec, cpu).head = NULL;
808808
per_cpu(tasklet_vec, cpu).tail = &per_cpu(tasklet_vec, cpu).head;
809809
}
810810
raise_softirq_irqoff(TASKLET_SOFTIRQ);
811811

812812
if (&per_cpu(tasklet_hi_vec, cpu).head != per_cpu(tasklet_hi_vec, cpu).tail) {
813-
*__get_cpu_var(tasklet_hi_vec).tail = per_cpu(tasklet_hi_vec, cpu).head;
814-
__get_cpu_var(tasklet_hi_vec).tail = per_cpu(tasklet_hi_vec, cpu).tail;
813+
*__this_cpu_read(tasklet_hi_vec.tail) = per_cpu(tasklet_hi_vec, cpu).head;
814+
__this_cpu_write(tasklet_hi_vec.tail, per_cpu(tasklet_hi_vec, cpu).tail);
815815
per_cpu(tasklet_hi_vec, cpu).head = NULL;
816816
per_cpu(tasklet_hi_vec, cpu).tail = &per_cpu(tasklet_hi_vec, cpu).head;
817817
}

‎kernel/time/tick-common.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ struct tick_device *tick_get_device(int cpu)
4949
*/
5050
int tick_is_oneshot_available(void)
5151
{
52-
struct clock_event_device *dev = __get_cpu_var(tick_cpu_device).evtdev;
52+
struct clock_event_device *dev = __this_cpu_read(tick_cpu_device.evtdev);
5353

5454
return dev && (dev->features & CLOCK_EVT_FEAT_ONESHOT);
5555
}

0 commit comments

Comments
 (0)