Skip to content

Commit b4f5019

Browse files
Christoph Lameterhtejun
authored andcommitted
tile: 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 raspberrypi#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) Acked-by: Chris Metcalf <cmetcalf@tilera.com> Signed-off-by: Christoph Lameter <cl@linux.com> Signed-off-by: Tejun Heo <tj@kernel.org>
1 parent 5828f66 commit b4f5019

12 files changed

Lines changed: 30 additions & 29 deletions

File tree

‎arch/tile/include/asm/irqflags.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,12 @@ extern unsigned int debug_smp_processor_id(void);
140140

141141
/*
142142
* Read the set of maskable interrupts.
143-
* We avoid the preemption warning here via __this_cpu_ptr since even
143+
* We avoid the preemption warning here via raw_cpu_ptr since even
144144
* if irqs are already enabled, it's harmless to read the wrong cpu's
145145
* enabled mask.
146146
*/
147147
#define arch_local_irqs_enabled() \
148-
(*__this_cpu_ptr(&interrupts_enabled_mask))
148+
(*raw_cpu_ptr(&interrupts_enabled_mask))
149149

150150
/* Re-enable all maskable interrupts. */
151151
#define arch_local_irq_enable() \

‎arch/tile/include/asm/mmu_context.h‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ static inline void enter_lazy_tlb(struct mm_struct *mm, struct task_struct *t)
8484
* clear any pending DMA interrupts.
8585
*/
8686
if (current->thread.tile_dma_state.enabled)
87-
install_page_table(mm->pgd, __get_cpu_var(current_asid));
87+
install_page_table(mm->pgd, __this_cpu_read(current_asid));
8888
#endif
8989
}
9090

@@ -96,12 +96,12 @@ static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next,
9696
int cpu = smp_processor_id();
9797

9898
/* Pick new ASID. */
99-
int asid = __get_cpu_var(current_asid) + 1;
99+
int asid = __this_cpu_read(current_asid) + 1;
100100
if (asid > max_asid) {
101101
asid = min_asid;
102102
local_flush_tlb();
103103
}
104-
__get_cpu_var(current_asid) = asid;
104+
__this_cpu_write(current_asid, asid);
105105

106106
/* Clear cpu from the old mm, and set it in the new one. */
107107
cpumask_clear_cpu(cpu, mm_cpumask(prev));

‎arch/tile/kernel/irq.c‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ static DEFINE_PER_CPU(int, irq_depth);
7373
*/
7474
void tile_dev_intr(struct pt_regs *regs, int intnum)
7575
{
76-
int depth = __get_cpu_var(irq_depth)++;
76+
int depth = __this_cpu_inc_return(irq_depth);
7777
unsigned long original_irqs;
7878
unsigned long remaining_irqs;
7979
struct pt_regs *old_regs;
@@ -120,7 +120,7 @@ void tile_dev_intr(struct pt_regs *regs, int intnum)
120120

121121
/* Count device irqs; Linux IPIs are counted elsewhere. */
122122
if (irq != IRQ_RESCHEDULE)
123-
__get_cpu_var(irq_stat).irq_dev_intr_count++;
123+
__this_cpu_inc(irq_stat.irq_dev_intr_count);
124124

125125
generic_handle_irq(irq);
126126
}
@@ -130,10 +130,10 @@ void tile_dev_intr(struct pt_regs *regs, int intnum)
130130
* including any that were reenabled during interrupt
131131
* handling.
132132
*/
133-
if (depth == 0)
134-
unmask_irqs(~__get_cpu_var(irq_disable_mask));
133+
if (depth == 1)
134+
unmask_irqs(~__this_cpu_read(irq_disable_mask));
135135

136-
__get_cpu_var(irq_depth)--;
136+
__this_cpu_dec(irq_depth);
137137

138138
/*
139139
* Track time spent against the current process again and
@@ -151,7 +151,7 @@ void tile_dev_intr(struct pt_regs *regs, int intnum)
151151
static void tile_irq_chip_enable(struct irq_data *d)
152152
{
153153
get_cpu_var(irq_disable_mask) &= ~(1UL << d->irq);
154-
if (__get_cpu_var(irq_depth) == 0)
154+
if (__this_cpu_read(irq_depth) == 0)
155155
unmask_irqs(1UL << d->irq);
156156
put_cpu_var(irq_disable_mask);
157157
}
@@ -197,7 +197,7 @@ static void tile_irq_chip_ack(struct irq_data *d)
197197
*/
198198
static void tile_irq_chip_eoi(struct irq_data *d)
199199
{
200-
if (!(__get_cpu_var(irq_disable_mask) & (1UL << d->irq)))
200+
if (!(__this_cpu_read(irq_disable_mask) & (1UL << d->irq)))
201201
unmask_irqs(1UL << d->irq);
202202
}
203203

‎arch/tile/kernel/messaging.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ static DEFINE_PER_CPU(HV_MsgState, msg_state);
2828
void init_messaging(void)
2929
{
3030
/* Allocate storage for messages in kernel space */
31-
HV_MsgState *state = &__get_cpu_var(msg_state);
31+
HV_MsgState *state = this_cpu_ptr(&msg_state);
3232
int rc = hv_register_message_state(state);
3333
if (rc != HV_OK)
3434
panic("hv_register_message_state: error %d", rc);
@@ -96,7 +96,7 @@ void hv_message_intr(struct pt_regs *regs, int intnum)
9696
struct hv_driver_cb *cb =
9797
(struct hv_driver_cb *)him->intarg;
9898
cb->callback(cb, him->intdata);
99-
__get_cpu_var(irq_stat).irq_hv_msg_count++;
99+
__this_cpu_inc(irq_stat.irq_hv_msg_count);
100100
}
101101
}
102102

‎arch/tile/kernel/process.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ early_param("idle", idle_setup);
6464

6565
void arch_cpu_idle(void)
6666
{
67-
__get_cpu_var(irq_stat).idle_timestamp = jiffies;
67+
__this_cpu_write(irq_stat.idle_timestamp, jiffies);
6868
_cpu_idle();
6969
}
7070

‎arch/tile/kernel/setup.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1218,7 +1218,8 @@ static void __init validate_hv(void)
12181218
* various asid variables to their appropriate initial states.
12191219
*/
12201220
asid_range = hv_inquire_asid(0);
1221-
__get_cpu_var(current_asid) = min_asid = asid_range.start;
1221+
min_asid = asid_range.start;
1222+
__this_cpu_write(current_asid, min_asid);
12221223
max_asid = asid_range.start + asid_range.size - 1;
12231224

12241225
if (hv_confstr(HV_CONFSTR_CHIP_MODEL, (HV_VirtAddr)chip_model,

‎arch/tile/kernel/single_step.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,7 @@ static DEFINE_PER_CPU(unsigned long, ss_saved_pc);
740740

741741
void gx_singlestep_handle(struct pt_regs *regs, int fault_num)
742742
{
743-
unsigned long *ss_pc = &__get_cpu_var(ss_saved_pc);
743+
unsigned long *ss_pc = this_cpu_ptr(&ss_saved_pc);
744744
struct thread_info *info = (void *)current_thread_info();
745745
int is_single_step = test_ti_thread_flag(info, TIF_SINGLESTEP);
746746
unsigned long control = __insn_mfspr(SPR_SINGLE_STEP_CONTROL_K);
@@ -766,7 +766,7 @@ void gx_singlestep_handle(struct pt_regs *regs, int fault_num)
766766

767767
void single_step_once(struct pt_regs *regs)
768768
{
769-
unsigned long *ss_pc = &__get_cpu_var(ss_saved_pc);
769+
unsigned long *ss_pc = this_cpu_ptr(&ss_saved_pc);
770770
unsigned long control = __insn_mfspr(SPR_SINGLE_STEP_CONTROL_K);
771771

772772
*ss_pc = regs->pc;

‎arch/tile/kernel/smp.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ void flush_icache_range(unsigned long start, unsigned long end)
188188
/* Called when smp_send_reschedule() triggers IRQ_RESCHEDULE. */
189189
static irqreturn_t handle_reschedule_ipi(int irq, void *token)
190190
{
191-
__get_cpu_var(irq_stat).irq_resched_count++;
191+
__this_cpu_inc(irq_stat.irq_resched_count);
192192
scheduler_ipi();
193193

194194
return IRQ_HANDLED;

‎arch/tile/kernel/smpboot.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ void __init smp_prepare_boot_cpu(void)
4141
int cpu = smp_processor_id();
4242
set_cpu_online(cpu, 1);
4343
set_cpu_present(cpu, 1);
44-
__get_cpu_var(cpu_state) = CPU_ONLINE;
44+
__this_cpu_write(cpu_state, CPU_ONLINE);
4545

4646
init_messaging();
4747
}
@@ -158,7 +158,7 @@ static void start_secondary(void)
158158
/* printk(KERN_DEBUG "Initializing CPU#%d\n", cpuid); */
159159

160160
/* Initialize the current asid for our first page table. */
161-
__get_cpu_var(current_asid) = min_asid;
161+
__this_cpu_write(current_asid, min_asid);
162162

163163
/* Set up this thread as another owner of the init_mm */
164164
atomic_inc(&init_mm.mm_count);
@@ -201,7 +201,7 @@ void online_secondary(void)
201201
notify_cpu_starting(smp_processor_id());
202202

203203
set_cpu_online(smp_processor_id(), 1);
204-
__get_cpu_var(cpu_state) = CPU_ONLINE;
204+
__this_cpu_write(cpu_state, CPU_ONLINE);
205205

206206
/* Set up tile-specific state for this cpu. */
207207
setup_cpu(0);

‎arch/tile/kernel/time.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ static DEFINE_PER_CPU(struct clock_event_device, tile_timer) = {
162162

163163
void setup_tile_timer(void)
164164
{
165-
struct clock_event_device *evt = &__get_cpu_var(tile_timer);
165+
struct clock_event_device *evt = this_cpu_ptr(&tile_timer);
166166

167167
/* Fill in fields that are speed-specific. */
168168
clockevents_calc_mult_shift(evt, cycles_per_sec, TILE_MINSEC);
@@ -182,7 +182,7 @@ void setup_tile_timer(void)
182182
void do_timer_interrupt(struct pt_regs *regs, int fault_num)
183183
{
184184
struct pt_regs *old_regs = set_irq_regs(regs);
185-
struct clock_event_device *evt = &__get_cpu_var(tile_timer);
185+
struct clock_event_device *evt = this_cpu_ptr(&tile_timer);
186186

187187
/*
188188
* Mask the timer interrupt here, since we are a oneshot timer
@@ -194,7 +194,7 @@ void do_timer_interrupt(struct pt_regs *regs, int fault_num)
194194
irq_enter();
195195

196196
/* Track interrupt count. */
197-
__get_cpu_var(irq_stat).irq_timer_count++;
197+
__this_cpu_inc(irq_stat.irq_timer_count);
198198

199199
/* Call the generic timer handler */
200200
evt->event_handler(evt);
@@ -235,7 +235,7 @@ cycles_t ns2cycles(unsigned long nsecs)
235235
* We do not have to disable preemption here as each core has the same
236236
* clock frequency.
237237
*/
238-
struct clock_event_device *dev = &__raw_get_cpu_var(tile_timer);
238+
struct clock_event_device *dev = raw_cpu_ptr(&tile_timer);
239239

240240
/*
241241
* as in clocksource.h and x86's timer.h, we split the calculation

0 commit comments

Comments
 (0)