Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

sparc: Replace __get_cpu_var uses

__get_cpu_var() is used for multiple purposes in the kernel source. One of
them is address calculation via the form &__get_cpu_var(x). This calculates
the address for the instance of the percpu variable of the current processor
based on an offset.

Other use cases are for storing and retrieving data from the current
processors percpu area. __get_cpu_var() can be used as an lvalue when
writing data or on the right side of an assignment.

__get_cpu_var() is defined as :

#define __get_cpu_var(var) (*this_cpu_ptr(&(var)))

__get_cpu_var() always only does an address determination. However, store
and retrieve operations could use a segment prefix (or global register on
other platforms) to avoid the address calculation.

this_cpu_write() and this_cpu_read() can directly take an offset into a
percpu area and use optimized assembly code to read and write per cpu
variables.

This patch converts __get_cpu_var into either an explicit address
calculation using this_cpu_ptr() or into a use of this_cpu operations that
use the offset. Thereby address calculations are avoided and less registers
are used when code is generated.

At the end of the patch set all uses of __get_cpu_var have been removed so
the macro is removed too.

The patch set includes passes over all arches as well. Once these operations
are used throughout then specialized macros can be defined in non -x86
arches as well in order to optimize per cpu access by f.e. using a global
register that may be set to the per cpu base.

Transformations done to __get_cpu_var()

1. Determine the address of the percpu instance of the current processor.

DEFINE_PER_CPU(int, y);
int *x = &__get_cpu_var(y);

Converts to

int *x = this_cpu_ptr(&y);

2. Same as #1 but this time an array structure is involved.

DEFINE_PER_CPU(int, y[20]);
int *x = __get_cpu_var(y);

Converts to

int *x = this_cpu_ptr(y);

3. Retrieve the content of the current processors instance of a per cpu
variable.

DEFINE_PER_CPU(int, y);
int x = __get_cpu_var(y)

Converts to

int x = __this_cpu_read(y);

4. Retrieve the content of a percpu struct

DEFINE_PER_CPU(struct mystruct, y);
struct mystruct x = __get_cpu_var(y);

Converts to

memcpy(&x, this_cpu_ptr(&y), sizeof(x));

5. Assignment to a per cpu variable

DEFINE_PER_CPU(int, y)
__get_cpu_var(y) = x;

Converts to

__this_cpu_write(y, x);

6. Increment/Decrement etc of a per cpu variable

DEFINE_PER_CPU(int, y);
__get_cpu_var(y)++

Converts to

__this_cpu_inc(y)

Cc: sparclinux@vger.kernel.org
Acked-by: David S. Miller <davem@davemloft.net>
Signed-off-by: Christoph Lameter <cl@linux.com>
Signed-off-by: Tejun Heo <tj@kernel.org>

authored by

Christoph Lameter and committed by
Tejun Heo
494fc421 8c23af61

+35 -35
+1 -1
arch/sparc/include/asm/cpudata_32.h
··· 26 26 27 27 DECLARE_PER_CPU(cpuinfo_sparc, __cpu_data); 28 28 #define cpu_data(__cpu) per_cpu(__cpu_data, (__cpu)) 29 - #define local_cpu_data() __get_cpu_var(__cpu_data) 29 + #define local_cpu_data() (*this_cpu_ptr(&__cpu_data)) 30 30 31 31 #endif /* _SPARC_CPUDATA_H */
+1 -1
arch/sparc/include/asm/cpudata_64.h
··· 30 30 31 31 DECLARE_PER_CPU(cpuinfo_sparc, __cpu_data); 32 32 #define cpu_data(__cpu) per_cpu(__cpu_data, (__cpu)) 33 - #define local_cpu_data() __get_cpu_var(__cpu_data) 33 + #define local_cpu_data() (*this_cpu_ptr(&__cpu_data)) 34 34 35 35 #endif /* !(__ASSEMBLY__) */ 36 36
+3 -3
arch/sparc/kernel/kprobes.c
··· 83 83 84 84 static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb) 85 85 { 86 - __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp; 86 + __this_cpu_write(current_kprobe, kcb->prev_kprobe.kp); 87 87 kcb->kprobe_status = kcb->prev_kprobe.status; 88 88 kcb->kprobe_orig_tnpc = kcb->prev_kprobe.orig_tnpc; 89 89 kcb->kprobe_orig_tstate_pil = kcb->prev_kprobe.orig_tstate_pil; ··· 92 92 static void __kprobes set_current_kprobe(struct kprobe *p, struct pt_regs *regs, 93 93 struct kprobe_ctlblk *kcb) 94 94 { 95 - __get_cpu_var(current_kprobe) = p; 95 + __this_cpu_write(current_kprobe, p); 96 96 kcb->kprobe_orig_tnpc = regs->tnpc; 97 97 kcb->kprobe_orig_tstate_pil = (regs->tstate & TSTATE_PIL); 98 98 } ··· 155 155 ret = 1; 156 156 goto no_kprobe; 157 157 } 158 - p = __get_cpu_var(current_kprobe); 158 + p = __this_cpu_read(current_kprobe); 159 159 if (p->break_handler && p->break_handler(p, regs)) 160 160 goto ss_probe; 161 161 }
+1 -1
arch/sparc/kernel/leon_smp.c
··· 343 343 344 344 void leonsmp_ipi_interrupt(void) 345 345 { 346 - struct leon_ipi_work *work = &__get_cpu_var(leon_ipi_work); 346 + struct leon_ipi_work *work = this_cpu_ptr(&leon_ipi_work); 347 347 348 348 if (work->single) { 349 349 work->single = 0;
+8 -8
arch/sparc/kernel/nmi.c
··· 100 100 pcr_ops->write_pcr(0, pcr_ops->pcr_nmi_disable); 101 101 102 102 sum = local_cpu_data().irq0_irqs; 103 - if (__get_cpu_var(nmi_touch)) { 104 - __get_cpu_var(nmi_touch) = 0; 103 + if (__this_cpu_read(nmi_touch)) { 104 + __this_cpu_write(nmi_touch, 0); 105 105 touched = 1; 106 106 } 107 - if (!touched && __get_cpu_var(last_irq_sum) == sum) { 107 + if (!touched && __this_cpu_read(last_irq_sum) == sum) { 108 108 __this_cpu_inc(alert_counter); 109 109 if (__this_cpu_read(alert_counter) == 30 * nmi_hz) 110 110 die_nmi("BUG: NMI Watchdog detected LOCKUP", 111 111 regs, panic_on_timeout); 112 112 } else { 113 - __get_cpu_var(last_irq_sum) = sum; 113 + __this_cpu_write(last_irq_sum, sum); 114 114 __this_cpu_write(alert_counter, 0); 115 115 } 116 - if (__get_cpu_var(wd_enabled)) { 116 + if (__this_cpu_read(wd_enabled)) { 117 117 pcr_ops->write_pic(0, pcr_ops->nmi_picl_value(nmi_hz)); 118 118 pcr_ops->write_pcr(0, pcr_ops->pcr_nmi_enable); 119 119 } ··· 154 154 void stop_nmi_watchdog(void *unused) 155 155 { 156 156 pcr_ops->write_pcr(0, pcr_ops->pcr_nmi_disable); 157 - __get_cpu_var(wd_enabled) = 0; 157 + __this_cpu_write(wd_enabled, 0); 158 158 atomic_dec(&nmi_active); 159 159 } 160 160 ··· 207 207 208 208 void start_nmi_watchdog(void *unused) 209 209 { 210 - __get_cpu_var(wd_enabled) = 1; 210 + __this_cpu_write(wd_enabled, 1); 211 211 atomic_inc(&nmi_active); 212 212 213 213 pcr_ops->write_pcr(0, pcr_ops->pcr_nmi_disable); ··· 218 218 219 219 static void nmi_adjust_hz_one(void *unused) 220 220 { 221 - if (!__get_cpu_var(wd_enabled)) 221 + if (!__this_cpu_read(wd_enabled)) 222 222 return; 223 223 224 224 pcr_ops->write_pcr(0, pcr_ops->pcr_nmi_disable);
+4 -4
arch/sparc/kernel/pci_sun4v.c
··· 48 48 /* Interrupts must be disabled. */ 49 49 static inline void iommu_batch_start(struct device *dev, unsigned long prot, unsigned long entry) 50 50 { 51 - struct iommu_batch *p = &__get_cpu_var(iommu_batch); 51 + struct iommu_batch *p = this_cpu_ptr(&iommu_batch); 52 52 53 53 p->dev = dev; 54 54 p->prot = prot; ··· 94 94 95 95 static inline void iommu_batch_new_entry(unsigned long entry) 96 96 { 97 - struct iommu_batch *p = &__get_cpu_var(iommu_batch); 97 + struct iommu_batch *p = this_cpu_ptr(&iommu_batch); 98 98 99 99 if (p->entry + p->npages == entry) 100 100 return; ··· 106 106 /* Interrupts must be disabled. */ 107 107 static inline long iommu_batch_add(u64 phys_page) 108 108 { 109 - struct iommu_batch *p = &__get_cpu_var(iommu_batch); 109 + struct iommu_batch *p = this_cpu_ptr(&iommu_batch); 110 110 111 111 BUG_ON(p->npages >= PGLIST_NENTS); 112 112 ··· 120 120 /* Interrupts must be disabled. */ 121 121 static inline long iommu_batch_end(void) 122 122 { 123 - struct iommu_batch *p = &__get_cpu_var(iommu_batch); 123 + struct iommu_batch *p = this_cpu_ptr(&iommu_batch); 124 124 125 125 BUG_ON(p->npages >= PGLIST_NENTS); 126 126
+13 -13
arch/sparc/kernel/perf_event.c
··· 1013 1013 1014 1014 static void sparc_pmu_enable(struct pmu *pmu) 1015 1015 { 1016 - struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); 1016 + struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 1017 1017 int i; 1018 1018 1019 1019 if (cpuc->enabled) ··· 1031 1031 1032 1032 static void sparc_pmu_disable(struct pmu *pmu) 1033 1033 { 1034 - struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); 1034 + struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 1035 1035 int i; 1036 1036 1037 1037 if (!cpuc->enabled) ··· 1065 1065 1066 1066 static void sparc_pmu_start(struct perf_event *event, int flags) 1067 1067 { 1068 - struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); 1068 + struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 1069 1069 int idx = active_event_index(cpuc, event); 1070 1070 1071 1071 if (flags & PERF_EF_RELOAD) { ··· 1080 1080 1081 1081 static void sparc_pmu_stop(struct perf_event *event, int flags) 1082 1082 { 1083 - struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); 1083 + struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 1084 1084 int idx = active_event_index(cpuc, event); 1085 1085 1086 1086 if (!(event->hw.state & PERF_HES_STOPPED)) { ··· 1096 1096 1097 1097 static void sparc_pmu_del(struct perf_event *event, int _flags) 1098 1098 { 1099 - struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); 1099 + struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 1100 1100 unsigned long flags; 1101 1101 int i; 1102 1102 ··· 1133 1133 1134 1134 static void sparc_pmu_read(struct perf_event *event) 1135 1135 { 1136 - struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); 1136 + struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 1137 1137 int idx = active_event_index(cpuc, event); 1138 1138 struct hw_perf_event *hwc = &event->hw; 1139 1139 ··· 1145 1145 1146 1146 static void perf_stop_nmi_watchdog(void *unused) 1147 1147 { 1148 - struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); 1148 + struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 1149 1149 int i; 1150 1150 1151 1151 stop_nmi_watchdog(NULL); ··· 1356 1356 1357 1357 static int sparc_pmu_add(struct perf_event *event, int ef_flags) 1358 1358 { 1359 - struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); 1359 + struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 1360 1360 int n0, ret = -EAGAIN; 1361 1361 unsigned long flags; 1362 1362 ··· 1498 1498 */ 1499 1499 static void sparc_pmu_start_txn(struct pmu *pmu) 1500 1500 { 1501 - struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events); 1501 + struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events); 1502 1502 1503 1503 perf_pmu_disable(pmu); 1504 1504 cpuhw->group_flag |= PERF_EVENT_TXN; ··· 1511 1511 */ 1512 1512 static void sparc_pmu_cancel_txn(struct pmu *pmu) 1513 1513 { 1514 - struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events); 1514 + struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events); 1515 1515 1516 1516 cpuhw->group_flag &= ~PERF_EVENT_TXN; 1517 1517 perf_pmu_enable(pmu); ··· 1524 1524 */ 1525 1525 static int sparc_pmu_commit_txn(struct pmu *pmu) 1526 1526 { 1527 - struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); 1527 + struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 1528 1528 int n; 1529 1529 1530 1530 if (!sparc_pmu) 1531 1531 return -EINVAL; 1532 1532 1533 - cpuc = &__get_cpu_var(cpu_hw_events); 1533 + cpuc = this_cpu_ptr(&cpu_hw_events); 1534 1534 n = cpuc->n_events; 1535 1535 if (check_excludes(cpuc->event, 0, n)) 1536 1536 return -EINVAL; ··· 1601 1601 1602 1602 regs = args->regs; 1603 1603 1604 - cpuc = &__get_cpu_var(cpu_hw_events); 1604 + cpuc = this_cpu_ptr(&cpu_hw_events); 1605 1605 1606 1606 /* If the PMU has the TOE IRQ enable bits, we need to do a 1607 1607 * dummy write to the %pcr to clear the overflow bits and thus
+1 -1
arch/sparc/kernel/sun4d_smp.c
··· 204 204 205 205 void sun4d_ipi_interrupt(void) 206 206 { 207 - struct sun4d_ipi_work *work = &__get_cpu_var(sun4d_ipi_work); 207 + struct sun4d_ipi_work *work = this_cpu_ptr(&sun4d_ipi_work); 208 208 209 209 if (work->single) { 210 210 work->single = 0;
+1 -1
arch/sparc/kernel/time_64.c
··· 765 765 : /* no outputs */ 766 766 : "r" (pstate)); 767 767 768 - sevt = &__get_cpu_var(sparc64_events); 768 + sevt = this_cpu_ptr(&sparc64_events); 769 769 770 770 memcpy(sevt, &sparc64_clockevent, sizeof(*sevt)); 771 771 sevt->cpumask = cpumask_of(smp_processor_id());
+2 -2
arch/sparc/mm/tlb.c
··· 52 52 53 53 void arch_enter_lazy_mmu_mode(void) 54 54 { 55 - struct tlb_batch *tb = &__get_cpu_var(tlb_batch); 55 + struct tlb_batch *tb = this_cpu_ptr(&tlb_batch); 56 56 57 57 tb->active = 1; 58 58 } 59 59 60 60 void arch_leave_lazy_mmu_mode(void) 61 61 { 62 - struct tlb_batch *tb = &__get_cpu_var(tlb_batch); 62 + struct tlb_batch *tb = this_cpu_ptr(&tlb_batch); 63 63 64 64 if (tb->tlb_nr) 65 65 flush_tlb_pending();