Skip to content

Commit

Permalink
s390: Replace __get_cpu_var uses
Browse files Browse the repository at this point in the history
__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: Martin Schwidefsky <schwidefsky@de.ibm.com>
CC: linux390@de.ibm.com
Acked-by: Heiko Carstens <heiko.carstens@de.ibm.com>
Signed-off-by: Christoph Lameter <cl@linux.com>
Signed-off-by: Tejun Heo <tj@kernel.org>
  • Loading branch information
Christoph Lameter authored and Tejun Heo committed Aug 26, 2014
1 parent 3589871 commit eb7e7d7
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 44 deletions.
2 changes: 1 addition & 1 deletion arch/s390/include/asm/cputime.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ cputime64_t s390_get_idle_time(int cpu);

static inline int s390_nohz_delay(int cpu)
{
return __get_cpu_var(s390_idle).nohz_delay != 0;
return __this_cpu_read(s390_idle.nohz_delay) != 0;
}

#define arch_needs_cpu(cpu) s390_nohz_delay(cpu)
Expand Down
2 changes: 1 addition & 1 deletion arch/s390/include/asm/irq.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ DECLARE_PER_CPU_SHARED_ALIGNED(struct irq_stat, irq_stat);

static __always_inline void inc_irq_stat(enum interruption_class irq)
{
__get_cpu_var(irq_stat).irqs[irq]++;
__this_cpu_inc(irq_stat.irqs[irq]);
}

struct ext_code {
Expand Down
16 changes: 8 additions & 8 deletions arch/s390/include/asm/percpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
pcp_op_T__ old__, new__, prev__; \
pcp_op_T__ *ptr__; \
preempt_disable(); \
ptr__ = __this_cpu_ptr(&(pcp)); \
ptr__ = raw_cpu_ptr(&(pcp)); \
prev__ = *ptr__; \
do { \
old__ = prev__; \
Expand Down Expand Up @@ -70,7 +70,7 @@
pcp_op_T__ val__ = (val); \
pcp_op_T__ old__, *ptr__; \
preempt_disable(); \
ptr__ = __this_cpu_ptr(&(pcp)); \
ptr__ = raw_cpu_ptr(&(pcp)); \
if (__builtin_constant_p(val__) && \
((szcast)val__ > -129) && ((szcast)val__ < 128)) { \
asm volatile( \
Expand All @@ -97,7 +97,7 @@
pcp_op_T__ val__ = (val); \
pcp_op_T__ old__, *ptr__; \
preempt_disable(); \
ptr__ = __this_cpu_ptr(&(pcp)); \
ptr__ = raw_cpu_ptr(&(pcp)); \
asm volatile( \
op " %[old__],%[val__],%[ptr__]\n" \
: [old__] "=d" (old__), [ptr__] "+Q" (*ptr__) \
Expand All @@ -116,7 +116,7 @@
pcp_op_T__ val__ = (val); \
pcp_op_T__ old__, *ptr__; \
preempt_disable(); \
ptr__ = __this_cpu_ptr(&(pcp)); \
ptr__ = raw_cpu_ptr(&(pcp)); \
asm volatile( \
op " %[old__],%[val__],%[ptr__]\n" \
: [old__] "=d" (old__), [ptr__] "+Q" (*ptr__) \
Expand All @@ -138,7 +138,7 @@
pcp_op_T__ ret__; \
pcp_op_T__ *ptr__; \
preempt_disable(); \
ptr__ = __this_cpu_ptr(&(pcp)); \
ptr__ = raw_cpu_ptr(&(pcp)); \
ret__ = cmpxchg(ptr__, oval, nval); \
preempt_enable(); \
ret__; \
Expand All @@ -154,7 +154,7 @@
typeof(pcp) *ptr__; \
typeof(pcp) ret__; \
preempt_disable(); \
ptr__ = __this_cpu_ptr(&(pcp)); \
ptr__ = raw_cpu_ptr(&(pcp)); \
ret__ = xchg(ptr__, nval); \
preempt_enable(); \
ret__; \
Expand All @@ -173,8 +173,8 @@
typeof(pcp2) *p2__; \
int ret__; \
preempt_disable(); \
p1__ = __this_cpu_ptr(&(pcp1)); \
p2__ = __this_cpu_ptr(&(pcp2)); \
p1__ = raw_cpu_ptr(&(pcp1)); \
p2__ = raw_cpu_ptr(&(pcp2)); \
ret__ = __cmpxchg_double(p1__, p2__, o1__, o2__, n1__, n2__); \
preempt_enable(); \
ret__; \
Expand Down
2 changes: 1 addition & 1 deletion arch/s390/kernel/irq.c
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ static irqreturn_t do_ext_interrupt(int irq, void *dummy)

ext_code = *(struct ext_code *) &regs->int_code;
if (ext_code.code != EXT_IRQ_CLK_COMP)
__get_cpu_var(s390_idle).nohz_delay = 1;
__this_cpu_write(s390_idle.nohz_delay, 1);

index = ext_hash(ext_code.code);
rcu_read_lock();
Expand Down
8 changes: 4 additions & 4 deletions arch/s390/kernel/kprobes.c
Original file line number Diff line number Diff line change
Expand Up @@ -366,9 +366,9 @@ static void __kprobes disable_singlestep(struct kprobe_ctlblk *kcb,
*/
static void __kprobes push_kprobe(struct kprobe_ctlblk *kcb, struct kprobe *p)
{
kcb->prev_kprobe.kp = __get_cpu_var(current_kprobe);
kcb->prev_kprobe.kp = __this_cpu_read(current_kprobe);
kcb->prev_kprobe.status = kcb->kprobe_status;
__get_cpu_var(current_kprobe) = p;
__this_cpu_write(current_kprobe, p);
}

/*
Expand All @@ -378,7 +378,7 @@ static void __kprobes push_kprobe(struct kprobe_ctlblk *kcb, struct kprobe *p)
*/
static void __kprobes pop_kprobe(struct kprobe_ctlblk *kcb)
{
__get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp;
__this_cpu_write(current_kprobe, kcb->prev_kprobe.kp);
kcb->kprobe_status = kcb->prev_kprobe.status;
}

Expand Down Expand Up @@ -459,7 +459,7 @@ static int __kprobes kprobe_handler(struct pt_regs *regs)
enable_singlestep(kcb, regs, (unsigned long) p->ainsn.insn);
return 1;
} else if (kprobe_running()) {
p = __get_cpu_var(current_kprobe);
p = __this_cpu_read(current_kprobe);
if (p->break_handler && p->break_handler(p, regs)) {
/*
* Continuation after the jprobe completed and
Expand Down
10 changes: 7 additions & 3 deletions arch/s390/kernel/nmi.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,12 @@ void s390_handle_mcck(void)
*/
local_irq_save(flags);
local_mcck_disable();
mcck = __get_cpu_var(cpu_mcck);
memset(&__get_cpu_var(cpu_mcck), 0, sizeof(struct mcck_struct));
/*
* Ummm... Does this make sense at all? Copying the percpu struct
* and then zapping it one statement later?
*/
memcpy(&mcck, this_cpu_ptr(&cpu_mcck), sizeof(mcck));
memset(&mcck, 0, sizeof(struct mcck_struct));
clear_cpu_flag(CIF_MCCK_PENDING);
local_mcck_enable();
local_irq_restore(flags);
Expand Down Expand Up @@ -253,7 +257,7 @@ void notrace s390_do_machine_check(struct pt_regs *regs)
nmi_enter();
inc_irq_stat(NMI_NMI);
mci = (struct mci *) &S390_lowcore.mcck_interruption_code;
mcck = &__get_cpu_var(cpu_mcck);
mcck = this_cpu_ptr(&cpu_mcck);
umode = user_mode(regs);

if (mci->sd) {
Expand Down
22 changes: 11 additions & 11 deletions arch/s390/kernel/perf_cpum_cf.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ static int validate_ctr_auth(const struct hw_perf_event *hwc)
*/
static void cpumf_pmu_enable(struct pmu *pmu)
{
struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
int err;

if (cpuhw->flags & PMU_F_ENABLED)
Expand All @@ -196,7 +196,7 @@ static void cpumf_pmu_enable(struct pmu *pmu)
*/
static void cpumf_pmu_disable(struct pmu *pmu)
{
struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
int err;
u64 inactive;

Expand Down Expand Up @@ -230,7 +230,7 @@ static void cpumf_measurement_alert(struct ext_code ext_code,
return;

inc_irq_stat(IRQEXT_CMC);
cpuhw = &__get_cpu_var(cpu_hw_events);
cpuhw = this_cpu_ptr(&cpu_hw_events);

/* Measurement alerts are shared and might happen when the PMU
* is not reserved. Ignore these alerts in this case. */
Expand All @@ -250,7 +250,7 @@ static void cpumf_measurement_alert(struct ext_code ext_code,
#define PMC_RELEASE 1
static void setup_pmc_cpu(void *flags)
{
struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);

switch (*((int *) flags)) {
case PMC_INIT:
Expand Down Expand Up @@ -475,7 +475,7 @@ static void cpumf_pmu_read(struct perf_event *event)

static void cpumf_pmu_start(struct perf_event *event, int flags)
{
struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
struct hw_perf_event *hwc = &event->hw;

if (WARN_ON_ONCE(!(hwc->state & PERF_HES_STOPPED)))
Expand Down Expand Up @@ -506,7 +506,7 @@ static void cpumf_pmu_start(struct perf_event *event, int flags)

static void cpumf_pmu_stop(struct perf_event *event, int flags)
{
struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
struct hw_perf_event *hwc = &event->hw;

if (!(hwc->state & PERF_HES_STOPPED)) {
Expand All @@ -527,7 +527,7 @@ static void cpumf_pmu_stop(struct perf_event *event, int flags)

static int cpumf_pmu_add(struct perf_event *event, int flags)
{
struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);

/* Check authorization for the counter set to which this
* counter belongs.
Expand All @@ -551,7 +551,7 @@ static int cpumf_pmu_add(struct perf_event *event, int flags)

static void cpumf_pmu_del(struct perf_event *event, int flags)
{
struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);

cpumf_pmu_stop(event, PERF_EF_UPDATE);

Expand All @@ -575,7 +575,7 @@ static void cpumf_pmu_del(struct perf_event *event, int flags)
*/
static void cpumf_pmu_start_txn(struct pmu *pmu)
{
struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);

perf_pmu_disable(pmu);
cpuhw->flags |= PERF_EVENT_TXN;
Expand All @@ -589,7 +589,7 @@ static void cpumf_pmu_start_txn(struct pmu *pmu)
*/
static void cpumf_pmu_cancel_txn(struct pmu *pmu)
{
struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);

WARN_ON(cpuhw->tx_state != cpuhw->state);

Expand All @@ -604,7 +604,7 @@ static void cpumf_pmu_cancel_txn(struct pmu *pmu)
*/
static int cpumf_pmu_commit_txn(struct pmu *pmu)
{
struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
u64 state;

/* check if the updated state can be scheduled */
Expand Down
16 changes: 8 additions & 8 deletions arch/s390/kernel/perf_cpum_sf.c
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ static DEFINE_MUTEX(pmc_reserve_mutex);
static void setup_pmc_cpu(void *flags)
{
int err;
struct cpu_hw_sf *cpusf = &__get_cpu_var(cpu_hw_sf);
struct cpu_hw_sf *cpusf = this_cpu_ptr(&cpu_hw_sf);

err = 0;
switch (*((int *) flags)) {
Expand Down Expand Up @@ -849,7 +849,7 @@ static int cpumsf_pmu_event_init(struct perf_event *event)

static void cpumsf_pmu_enable(struct pmu *pmu)
{
struct cpu_hw_sf *cpuhw = &__get_cpu_var(cpu_hw_sf);
struct cpu_hw_sf *cpuhw = this_cpu_ptr(&cpu_hw_sf);
struct hw_perf_event *hwc;
int err;

Expand Down Expand Up @@ -898,7 +898,7 @@ static void cpumsf_pmu_enable(struct pmu *pmu)

static void cpumsf_pmu_disable(struct pmu *pmu)
{
struct cpu_hw_sf *cpuhw = &__get_cpu_var(cpu_hw_sf);
struct cpu_hw_sf *cpuhw = this_cpu_ptr(&cpu_hw_sf);
struct hws_lsctl_request_block inactive;
struct hws_qsi_info_block si;
int err;
Expand Down Expand Up @@ -1306,7 +1306,7 @@ static void cpumsf_pmu_read(struct perf_event *event)
*/
static void cpumsf_pmu_start(struct perf_event *event, int flags)
{
struct cpu_hw_sf *cpuhw = &__get_cpu_var(cpu_hw_sf);
struct cpu_hw_sf *cpuhw = this_cpu_ptr(&cpu_hw_sf);

if (WARN_ON_ONCE(!(event->hw.state & PERF_HES_STOPPED)))
return;
Expand All @@ -1327,7 +1327,7 @@ static void cpumsf_pmu_start(struct perf_event *event, int flags)
*/
static void cpumsf_pmu_stop(struct perf_event *event, int flags)
{
struct cpu_hw_sf *cpuhw = &__get_cpu_var(cpu_hw_sf);
struct cpu_hw_sf *cpuhw = this_cpu_ptr(&cpu_hw_sf);

if (event->hw.state & PERF_HES_STOPPED)
return;
Expand All @@ -1346,7 +1346,7 @@ static void cpumsf_pmu_stop(struct perf_event *event, int flags)

static int cpumsf_pmu_add(struct perf_event *event, int flags)
{
struct cpu_hw_sf *cpuhw = &__get_cpu_var(cpu_hw_sf);
struct cpu_hw_sf *cpuhw = this_cpu_ptr(&cpu_hw_sf);
int err;

if (cpuhw->flags & PMU_F_IN_USE)
Expand Down Expand Up @@ -1397,7 +1397,7 @@ static int cpumsf_pmu_add(struct perf_event *event, int flags)

static void cpumsf_pmu_del(struct perf_event *event, int flags)
{
struct cpu_hw_sf *cpuhw = &__get_cpu_var(cpu_hw_sf);
struct cpu_hw_sf *cpuhw = this_cpu_ptr(&cpu_hw_sf);

perf_pmu_disable(event->pmu);
cpumsf_pmu_stop(event, PERF_EF_UPDATE);
Expand Down Expand Up @@ -1470,7 +1470,7 @@ static void cpumf_measurement_alert(struct ext_code ext_code,
if (!(alert & CPU_MF_INT_SF_MASK))
return;
inc_irq_stat(IRQEXT_CMS);
cpuhw = &__get_cpu_var(cpu_hw_sf);
cpuhw = this_cpu_ptr(&cpu_hw_sf);

/* Measurement alerts are shared and might happen when the PMU
* is not reserved. Ignore these alerts in this case. */
Expand Down
4 changes: 2 additions & 2 deletions arch/s390/kernel/processor.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ static DEFINE_PER_CPU(struct cpuid, cpu_id);
*/
void cpu_init(void)
{
struct s390_idle_data *idle = &__get_cpu_var(s390_idle);
struct cpuid *id = &__get_cpu_var(cpu_id);
struct s390_idle_data *idle = this_cpu_ptr(&s390_idle);
struct cpuid *id = this_cpu_ptr(&cpu_id);

get_cpu_id(id);
atomic_inc(&init_mm.mm_count);
Expand Down
6 changes: 3 additions & 3 deletions arch/s390/kernel/time.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ void clock_comparator_work(void)
struct clock_event_device *cd;

S390_lowcore.clock_comparator = -1ULL;
cd = &__get_cpu_var(comparators);
cd = this_cpu_ptr(&comparators);
cd->event_handler(cd);
}

Expand Down Expand Up @@ -360,7 +360,7 @@ EXPORT_SYMBOL(get_sync_clock);
*/
static void disable_sync_clock(void *dummy)
{
atomic_t *sw_ptr = &__get_cpu_var(clock_sync_word);
atomic_t *sw_ptr = this_cpu_ptr(&clock_sync_word);
/*
* Clear the in-sync bit 2^31. All get_sync_clock calls will
* fail until the sync bit is turned back on. In addition
Expand All @@ -377,7 +377,7 @@ static void disable_sync_clock(void *dummy)
*/
static void enable_sync_clock(void)
{
atomic_t *sw_ptr = &__get_cpu_var(clock_sync_word);
atomic_t *sw_ptr = this_cpu_ptr(&clock_sync_word);
atomic_set_mask(0x80000000, sw_ptr);
}

Expand Down
2 changes: 1 addition & 1 deletion arch/s390/kernel/vtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ EXPORT_SYMBOL_GPL(vtime_account_system);

void __kprobes vtime_stop_cpu(void)
{
struct s390_idle_data *idle = &__get_cpu_var(s390_idle);
struct s390_idle_data *idle = this_cpu_ptr(&s390_idle);
unsigned long long idle_time;
unsigned long psw_mask;

Expand Down
2 changes: 1 addition & 1 deletion arch/s390/oprofile/hwsampler.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ static int smp_ctl_qsi(int cpu)
static void hws_ext_handler(struct ext_code ext_code,
unsigned int param32, unsigned long param64)
{
struct hws_cpu_buffer *cb = &__get_cpu_var(sampler_cpu_buffer);
struct hws_cpu_buffer *cb = this_cpu_ptr(&sampler_cpu_buffer);

if (!(param32 & CPU_MF_INT_SF_MASK))
return;
Expand Down

0 comments on commit eb7e7d7

Please sign in to comment.