Skip to content

Commit

Permalink
locking/atomics: COCCINELLE/treewide: Convert trivial ACCESS_ONCE() p…
Browse files Browse the repository at this point in the history
…atterns to READ_ONCE()/WRITE_ONCE()

Please do not apply this to mainline directly, instead please re-run the
coccinelle script shown below and apply its output.

For several reasons, it is desirable to use {READ,WRITE}_ONCE() in
preference to ACCESS_ONCE(), and new code is expected to use one of the
former. So far, there's been no reason to change most existing uses of
ACCESS_ONCE(), as these aren't harmful, and changing them results in
churn.

However, for some features, the read/write distinction is critical to
correct operation. To distinguish these cases, separate read/write
accessors must be used. This patch migrates (most) remaining
ACCESS_ONCE() instances to {READ,WRITE}_ONCE(), using the following
coccinelle script:

----
// Convert trivial ACCESS_ONCE() uses to equivalent READ_ONCE() and
// WRITE_ONCE()

// $ make coccicheck COCCI=/home/mark/once.cocci SPFLAGS="--include-headers" MODE=patch

virtual patch

@ depends on patch @
expression E1, E2;
@@

- ACCESS_ONCE(E1) = E2
+ WRITE_ONCE(E1, E2)

@ depends on patch @
expression E;
@@

- ACCESS_ONCE(E)
+ READ_ONCE(E)
----

Signed-off-by: Mark Rutland <mark.rutland@arm.com>
Signed-off-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: davem@davemloft.net
Cc: linux-arch@vger.kernel.org
Cc: mpe@ellerman.id.au
Cc: shuah@kernel.org
Cc: snitzer@redhat.com
Cc: thor.thayer@linux.intel.com
Cc: tj@kernel.org
Cc: viro@zeniv.linux.org.uk
Cc: will.deacon@arm.com
Link: http://lkml.kernel.org/r/1508792849-3115-19-git-send-email-paulmck@linux.vnet.ibm.com
Signed-off-by: Ingo Molnar <mingo@kernel.org>
  • Loading branch information
Mark Rutland authored and Ingo Molnar committed Oct 25, 2017
1 parent b03a0fe commit 6aa7de0
Show file tree
Hide file tree
Showing 180 changed files with 383 additions and 385 deletions.
2 changes: 1 addition & 1 deletion arch/arc/kernel/smp.c
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ static void ipi_send_msg_one(int cpu, enum ipi_msg_type msg)
* and read back old value
*/
do {
new = old = ACCESS_ONCE(*ipi_data_ptr);
new = old = READ_ONCE(*ipi_data_ptr);
new |= 1U << msg;
} while (cmpxchg(ipi_data_ptr, old, new) != old);

Expand Down
2 changes: 1 addition & 1 deletion arch/arm/include/asm/spinlock.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ static inline void arch_spin_lock(arch_spinlock_t *lock)

while (lockval.tickets.next != lockval.tickets.owner) {
wfe();
lockval.tickets.owner = ACCESS_ONCE(lock->tickets.owner);
lockval.tickets.owner = READ_ONCE(lock->tickets.owner);
}

smp_mb();
Expand Down
2 changes: 1 addition & 1 deletion arch/arm/mach-tegra/cpuidle-tegra20.c
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ static int tegra20_idle_lp2_coupled(struct cpuidle_device *dev,
bool entered_lp2 = false;

if (tegra_pending_sgi())
ACCESS_ONCE(abort_flag) = true;
WRITE_ONCE(abort_flag, true);

cpuidle_coupled_parallel_barrier(dev, &abort_barrier);

Expand Down
2 changes: 1 addition & 1 deletion arch/arm/vdso/vgettimeofday.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ static notrace u32 __vdso_read_begin(const struct vdso_data *vdata)
{
u32 seq;
repeat:
seq = ACCESS_ONCE(vdata->seq_count);
seq = READ_ONCE(vdata->seq_count);
if (seq & 1) {
cpu_relax();
goto repeat;
Expand Down
8 changes: 4 additions & 4 deletions arch/ia64/include/asm/spinlock.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ static __always_inline void __ticket_spin_lock(arch_spinlock_t *lock)

static __always_inline int __ticket_spin_trylock(arch_spinlock_t *lock)
{
int tmp = ACCESS_ONCE(lock->lock);
int tmp = READ_ONCE(lock->lock);

if (!(((tmp >> TICKET_SHIFT) ^ tmp) & TICKET_MASK))
return ia64_cmpxchg(acq, &lock->lock, tmp, tmp + 1, sizeof (tmp)) == tmp;
Expand All @@ -73,19 +73,19 @@ static __always_inline void __ticket_spin_unlock(arch_spinlock_t *lock)
unsigned short *p = (unsigned short *)&lock->lock + 1, tmp;

asm volatile ("ld2.bias %0=[%1]" : "=r"(tmp) : "r"(p));
ACCESS_ONCE(*p) = (tmp + 2) & ~1;
WRITE_ONCE(*p, (tmp + 2) & ~1);
}

static inline int __ticket_spin_is_locked(arch_spinlock_t *lock)
{
long tmp = ACCESS_ONCE(lock->lock);
long tmp = READ_ONCE(lock->lock);

return !!(((tmp >> TICKET_SHIFT) ^ tmp) & TICKET_MASK);
}

static inline int __ticket_spin_is_contended(arch_spinlock_t *lock)
{
long tmp = ACCESS_ONCE(lock->lock);
long tmp = READ_ONCE(lock->lock);

return ((tmp - (tmp >> TICKET_SHIFT)) & TICKET_MASK) > 1;
}
Expand Down
2 changes: 1 addition & 1 deletion arch/mips/include/asm/vdso.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ static inline u32 vdso_data_read_begin(const union mips_vdso_data *data)
u32 seq;

while (true) {
seq = ACCESS_ONCE(data->seq_count);
seq = READ_ONCE(data->seq_count);
if (likely(!(seq & 1))) {
/* Paired with smp_wmb() in vdso_data_write_*(). */
smp_rmb();
Expand Down
2 changes: 1 addition & 1 deletion arch/mips/kernel/pm-cps.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ int cps_pm_enter_state(enum cps_pm_state state)
nc_core_ready_count = nc_addr;

/* Ensure ready_count is zero-initialised before the assembly runs */
ACCESS_ONCE(*nc_core_ready_count) = 0;
WRITE_ONCE(*nc_core_ready_count, 0);
coupled_barrier(&per_cpu(pm_barrier, core), online);

/* Run the generated entry code */
Expand Down
4 changes: 2 additions & 2 deletions arch/mn10300/kernel/mn10300-serial.c
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ static void mn10300_serial_receive_interrupt(struct mn10300_serial_port *port)

try_again:
/* pull chars out of the hat */
ix = ACCESS_ONCE(port->rx_outp);
ix = READ_ONCE(port->rx_outp);
if (CIRC_CNT(port->rx_inp, ix, MNSC_BUFFER_SIZE) == 0) {
if (push && !tport->low_latency)
tty_flip_buffer_push(tport);
Expand Down Expand Up @@ -1724,7 +1724,7 @@ static int mn10300_serial_poll_get_char(struct uart_port *_port)
if (mn10300_serial_int_tbl[port->rx_irq].port != NULL) {
do {
/* pull chars out of the hat */
ix = ACCESS_ONCE(port->rx_outp);
ix = READ_ONCE(port->rx_outp);
if (CIRC_CNT(port->rx_inp, ix, MNSC_BUFFER_SIZE) == 0)
return NO_POLL_CHAR;

Expand Down
2 changes: 1 addition & 1 deletion arch/parisc/include/asm/atomic.h
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ atomic64_set(atomic64_t *v, s64 i)
static __inline__ s64
atomic64_read(const atomic64_t *v)
{
return ACCESS_ONCE((v)->counter);
return READ_ONCE((v)->counter);
}

#define atomic64_inc(v) (atomic64_add( 1,(v)))
Expand Down
2 changes: 1 addition & 1 deletion arch/powerpc/platforms/powernv/opal-msglog.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ ssize_t opal_msglog_copy(char *to, loff_t pos, size_t count)
if (!opal_memcons)
return -ENODEV;

out_pos = be32_to_cpu(ACCESS_ONCE(opal_memcons->out_pos));
out_pos = be32_to_cpu(READ_ONCE(opal_memcons->out_pos));

/* Now we've read out_pos, put a barrier in before reading the new
* data it points to in conbuf. */
Expand Down
6 changes: 3 additions & 3 deletions arch/s390/include/asm/spinlock.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,14 @@ extern int _raw_write_trylock_retry(arch_rwlock_t *lp);

static inline int arch_read_trylock_once(arch_rwlock_t *rw)
{
int old = ACCESS_ONCE(rw->lock);
int old = READ_ONCE(rw->lock);
return likely(old >= 0 &&
__atomic_cmpxchg_bool(&rw->lock, old, old + 1));
}

static inline int arch_write_trylock_once(arch_rwlock_t *rw)
{
int old = ACCESS_ONCE(rw->lock);
int old = READ_ONCE(rw->lock);
return likely(old == 0 &&
__atomic_cmpxchg_bool(&rw->lock, 0, 0x80000000));
}
Expand Down Expand Up @@ -211,7 +211,7 @@ static inline void arch_read_unlock(arch_rwlock_t *rw)
int old;

do {
old = ACCESS_ONCE(rw->lock);
old = READ_ONCE(rw->lock);
} while (!__atomic_cmpxchg_bool(&rw->lock, old, old - 1));
}

Expand Down
16 changes: 8 additions & 8 deletions arch/s390/lib/spinlock.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ void _raw_read_lock_wait(arch_rwlock_t *rw)
smp_yield_cpu(~owner);
count = spin_retry;
}
old = ACCESS_ONCE(rw->lock);
owner = ACCESS_ONCE(rw->owner);
old = READ_ONCE(rw->lock);
owner = READ_ONCE(rw->owner);
if (old < 0)
continue;
if (__atomic_cmpxchg_bool(&rw->lock, old, old + 1))
Expand All @@ -178,7 +178,7 @@ int _raw_read_trylock_retry(arch_rwlock_t *rw)
int old;

while (count-- > 0) {
old = ACCESS_ONCE(rw->lock);
old = READ_ONCE(rw->lock);
if (old < 0)
continue;
if (__atomic_cmpxchg_bool(&rw->lock, old, old + 1))
Expand All @@ -202,8 +202,8 @@ void _raw_write_lock_wait(arch_rwlock_t *rw, int prev)
smp_yield_cpu(~owner);
count = spin_retry;
}
old = ACCESS_ONCE(rw->lock);
owner = ACCESS_ONCE(rw->owner);
old = READ_ONCE(rw->lock);
owner = READ_ONCE(rw->owner);
smp_mb();
if (old >= 0) {
prev = __RAW_LOCK(&rw->lock, 0x80000000, __RAW_OP_OR);
Expand All @@ -230,8 +230,8 @@ void _raw_write_lock_wait(arch_rwlock_t *rw)
smp_yield_cpu(~owner);
count = spin_retry;
}
old = ACCESS_ONCE(rw->lock);
owner = ACCESS_ONCE(rw->owner);
old = READ_ONCE(rw->lock);
owner = READ_ONCE(rw->owner);
if (old >= 0 &&
__atomic_cmpxchg_bool(&rw->lock, old, old | 0x80000000))
prev = old;
Expand All @@ -251,7 +251,7 @@ int _raw_write_trylock_retry(arch_rwlock_t *rw)
int old;

while (count-- > 0) {
old = ACCESS_ONCE(rw->lock);
old = READ_ONCE(rw->lock);
if (old)
continue;
if (__atomic_cmpxchg_bool(&rw->lock, 0, 0x80000000))
Expand Down
2 changes: 1 addition & 1 deletion arch/sparc/include/asm/atomic_32.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void atomic_set(atomic_t *, int);

#define atomic_set_release(v, i) atomic_set((v), (i))

#define atomic_read(v) ACCESS_ONCE((v)->counter)
#define atomic_read(v) READ_ONCE((v)->counter)

#define atomic_add(i, v) ((void)atomic_add_return( (int)(i), (v)))
#define atomic_sub(i, v) ((void)atomic_add_return(-(int)(i), (v)))
Expand Down
4 changes: 2 additions & 2 deletions arch/tile/gxio/dma_queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,14 @@ int __gxio_dma_queue_is_complete(__gxio_dma_queue_t *dma_queue,
int64_t completion_slot, int update)
{
if (update) {
if (ACCESS_ONCE(dma_queue->hw_complete_count) >
if (READ_ONCE(dma_queue->hw_complete_count) >
completion_slot)
return 1;

__gxio_dma_queue_update_credits(dma_queue);
}

return ACCESS_ONCE(dma_queue->hw_complete_count) > completion_slot;
return READ_ONCE(dma_queue->hw_complete_count) > completion_slot;
}

EXPORT_SYMBOL_GPL(__gxio_dma_queue_is_complete);
2 changes: 1 addition & 1 deletion arch/tile/include/gxio/dma_queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ static inline int64_t __gxio_dma_queue_reserve(__gxio_dma_queue_t *dma_queue,
* if the result is LESS than "hw_complete_count".
*/
uint64_t complete;
complete = ACCESS_ONCE(dma_queue->hw_complete_count);
complete = READ_ONCE(dma_queue->hw_complete_count);
slot |= (complete & 0xffffffffff000000);
if (slot < complete)
slot += 0x1000000;
Expand Down
2 changes: 1 addition & 1 deletion arch/tile/kernel/ptrace.c
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ long compat_arch_ptrace(struct task_struct *child, compat_long_t request,

int do_syscall_trace_enter(struct pt_regs *regs)
{
u32 work = ACCESS_ONCE(current_thread_info()->flags);
u32 work = READ_ONCE(current_thread_info()->flags);

if ((work & _TIF_SYSCALL_TRACE) &&
tracehook_report_syscall_entry(regs)) {
Expand Down
2 changes: 1 addition & 1 deletion arch/x86/entry/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ static long syscall_trace_enter(struct pt_regs *regs)
if (IS_ENABLED(CONFIG_DEBUG_ENTRY))
BUG_ON(regs != task_pt_regs(current));

work = ACCESS_ONCE(ti->flags) & _TIF_WORK_SYSCALL_ENTRY;
work = READ_ONCE(ti->flags) & _TIF_WORK_SYSCALL_ENTRY;

if (unlikely(work & _TIF_SYSCALL_EMU))
emulated = true;
Expand Down
2 changes: 1 addition & 1 deletion arch/x86/entry/vdso/vclock_gettime.c
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ int gettimeofday(struct timeval *, struct timezone *)
notrace time_t __vdso_time(time_t *t)
{
/* This is atomic on x86 so we don't need any locks. */
time_t result = ACCESS_ONCE(gtod->wall_time_sec);
time_t result = READ_ONCE(gtod->wall_time_sec);

if (t)
*t = result;
Expand Down
2 changes: 1 addition & 1 deletion arch/x86/events/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -2118,7 +2118,7 @@ static int x86_pmu_event_init(struct perf_event *event)
event->destroy(event);
}

if (ACCESS_ONCE(x86_pmu.attr_rdpmc))
if (READ_ONCE(x86_pmu.attr_rdpmc))
event->hw.flags |= PERF_X86_EVENT_RDPMC_ALLOWED;

return err;
Expand Down
2 changes: 1 addition & 1 deletion arch/x86/include/asm/vgtod.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ static inline unsigned gtod_read_begin(const struct vsyscall_gtod_data *s)
unsigned ret;

repeat:
ret = ACCESS_ONCE(s->seq);
ret = READ_ONCE(s->seq);
if (unlikely(ret & 1)) {
cpu_relax();
goto repeat;
Expand Down
6 changes: 3 additions & 3 deletions arch/x86/kernel/espfix_64.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,14 @@ void init_espfix_ap(int cpu)
page = cpu/ESPFIX_STACKS_PER_PAGE;

/* Did another CPU already set this up? */
stack_page = ACCESS_ONCE(espfix_pages[page]);
stack_page = READ_ONCE(espfix_pages[page]);
if (likely(stack_page))
goto done;

mutex_lock(&espfix_init_mutex);

/* Did we race on the lock? */
stack_page = ACCESS_ONCE(espfix_pages[page]);
stack_page = READ_ONCE(espfix_pages[page]);
if (stack_page)
goto unlock_done;

Expand Down Expand Up @@ -200,7 +200,7 @@ void init_espfix_ap(int cpu)
set_pte(&pte_p[n*PTE_STRIDE], pte);

/* Job is done for this CPU and any CPU which shares this page */
ACCESS_ONCE(espfix_pages[page]) = stack_page;
WRITE_ONCE(espfix_pages[page], stack_page);

unlock_done:
mutex_unlock(&espfix_init_mutex);
Expand Down
2 changes: 1 addition & 1 deletion arch/x86/kernel/nmi.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ static void nmi_max_handler(struct irq_work *w)
{
struct nmiaction *a = container_of(w, struct nmiaction, irq_work);
int remainder_ns, decimal_msecs;
u64 whole_msecs = ACCESS_ONCE(a->max_duration);
u64 whole_msecs = READ_ONCE(a->max_duration);

remainder_ns = do_div(whole_msecs, (1000 * 1000));
decimal_msecs = remainder_ns / 1000;
Expand Down
4 changes: 2 additions & 2 deletions arch/x86/kvm/mmu.c
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ static u64 __update_clear_spte_slow(u64 *sptep, u64 spte)

static u64 __get_spte_lockless(u64 *sptep)
{
return ACCESS_ONCE(*sptep);
return READ_ONCE(*sptep);
}
#else
union split_spte {
Expand Down Expand Up @@ -4819,7 +4819,7 @@ static void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
* If we don't have indirect shadow pages, it means no page is
* write-protected, so we can exit simply.
*/
if (!ACCESS_ONCE(vcpu->kvm->arch.indirect_shadow_pages))
if (!READ_ONCE(vcpu->kvm->arch.indirect_shadow_pages))
return;

remote_flush = local_flush = false;
Expand Down
2 changes: 1 addition & 1 deletion arch/x86/kvm/page_track.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ bool kvm_page_track_is_active(struct kvm_vcpu *vcpu, gfn_t gfn,
return false;

index = gfn_to_index(gfn, slot->base_gfn, PT_PAGE_TABLE_LEVEL);
return !!ACCESS_ONCE(slot->arch.gfn_track[mode][index]);
return !!READ_ONCE(slot->arch.gfn_track[mode][index]);
}

void kvm_page_track_cleanup(struct kvm *kvm)
Expand Down
2 changes: 1 addition & 1 deletion arch/x86/xen/p2m.c
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ int xen_alloc_p2m_entry(unsigned long pfn)
if (p2m_top_mfn && pfn < MAX_P2M_PFN) {
topidx = p2m_top_index(pfn);
top_mfn_p = &p2m_top_mfn[topidx];
mid_mfn = ACCESS_ONCE(p2m_top_mfn_p[topidx]);
mid_mfn = READ_ONCE(p2m_top_mfn_p[topidx]);

BUG_ON(virt_to_mfn(mid_mfn) != *top_mfn_p);

Expand Down
14 changes: 7 additions & 7 deletions arch/xtensa/platforms/xtfpga/lcd.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,23 @@
static void lcd_put_byte(u8 *addr, u8 data)
{
#ifdef CONFIG_XTFPGA_LCD_8BIT_ACCESS
ACCESS_ONCE(*addr) = data;
WRITE_ONCE(*addr, data);
#else
ACCESS_ONCE(*addr) = data & 0xf0;
ACCESS_ONCE(*addr) = (data << 4) & 0xf0;
WRITE_ONCE(*addr, data & 0xf0);
WRITE_ONCE(*addr, (data << 4) & 0xf0);
#endif
}

static int __init lcd_init(void)
{
ACCESS_ONCE(*LCD_INSTR_ADDR) = LCD_DISPLAY_MODE8BIT;
WRITE_ONCE(*LCD_INSTR_ADDR, LCD_DISPLAY_MODE8BIT);
mdelay(5);
ACCESS_ONCE(*LCD_INSTR_ADDR) = LCD_DISPLAY_MODE8BIT;
WRITE_ONCE(*LCD_INSTR_ADDR, LCD_DISPLAY_MODE8BIT);
udelay(200);
ACCESS_ONCE(*LCD_INSTR_ADDR) = LCD_DISPLAY_MODE8BIT;
WRITE_ONCE(*LCD_INSTR_ADDR, LCD_DISPLAY_MODE8BIT);
udelay(50);
#ifndef CONFIG_XTFPGA_LCD_8BIT_ACCESS
ACCESS_ONCE(*LCD_INSTR_ADDR) = LCD_DISPLAY_MODE4BIT;
WRITE_ONCE(*LCD_INSTR_ADDR, LCD_DISPLAY_MODE4BIT);
udelay(50);
lcd_put_byte(LCD_INSTR_ADDR, LCD_DISPLAY_MODE4BIT);
udelay(50);
Expand Down
2 changes: 1 addition & 1 deletion block/blk-wbt.c
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ static inline bool stat_sample_valid(struct blk_rq_stat *stat)

static u64 rwb_sync_issue_lat(struct rq_wb *rwb)
{
u64 now, issue = ACCESS_ONCE(rwb->sync_issue);
u64 now, issue = READ_ONCE(rwb->sync_issue);

if (!issue || !rwb->sync_cookie)
return 0;
Expand Down
2 changes: 1 addition & 1 deletion drivers/base/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,7 @@ const char *dev_driver_string(const struct device *dev)
* so be careful about accessing it. dev->bus and dev->class should
* never change once they are set, so they don't need special care.
*/
drv = ACCESS_ONCE(dev->driver);
drv = READ_ONCE(dev->driver);
return drv ? drv->name :
(dev->bus ? dev->bus->name :
(dev->class ? dev->class->name : ""));
Expand Down
Loading

0 comments on commit 6aa7de0

Please sign in to comment.