Skip to content

Commit f3eab71

Browse files
stevecapperlinaroctmarinas
authored andcommitted
arm64: percpu: Make this_cpu accessors pre-empt safe
this_cpu operations were implemented for arm64 in: 5284e1b arm64: xchg: Implement cmpxchg_double f97fc81 arm64: percpu: Implement this_cpu operations Unfortunately, it is possible for pre-emption to take place between address generation and data access. This can lead to cases where data is being manipulated by this_cpu for a different CPU than it was called on. Which effectively breaks the spec. This patch disables pre-emption for the this_cpu operations guaranteeing that address generation and data manipulation take place without a pre-emption in-between. Fixes: 5284e1b ("arm64: xchg: Implement cmpxchg_double") Fixes: f97fc81 ("arm64: percpu: Implement this_cpu operations") Reported-by: Mark Rutland <mark.rutland@arm.com> Acked-by: Will Deacon <will.deacon@arm.com> Signed-off-by: Steve Capper <steve.capper@linaro.org> [catalin.marinas@arm.com: remove space after type cast] Signed-off-by: Catalin Marinas <catalin.marinas@arm.com>
1 parent e53f21b commit f3eab71

File tree

2 files changed

+57
-19
lines changed

2 files changed

+57
-19
lines changed

arch/arm64/include/asm/cmpxchg.h

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -246,14 +246,30 @@ static inline unsigned long __cmpxchg_mb(volatile void *ptr, unsigned long old,
246246
__ret; \
247247
})
248248

249-
#define this_cpu_cmpxchg_1(ptr, o, n) cmpxchg_local(raw_cpu_ptr(&(ptr)), o, n)
250-
#define this_cpu_cmpxchg_2(ptr, o, n) cmpxchg_local(raw_cpu_ptr(&(ptr)), o, n)
251-
#define this_cpu_cmpxchg_4(ptr, o, n) cmpxchg_local(raw_cpu_ptr(&(ptr)), o, n)
252-
#define this_cpu_cmpxchg_8(ptr, o, n) cmpxchg_local(raw_cpu_ptr(&(ptr)), o, n)
253-
254-
#define this_cpu_cmpxchg_double_8(ptr1, ptr2, o1, o2, n1, n2) \
255-
cmpxchg_double_local(raw_cpu_ptr(&(ptr1)), raw_cpu_ptr(&(ptr2)), \
256-
o1, o2, n1, n2)
249+
#define _protect_cmpxchg_local(pcp, o, n) \
250+
({ \
251+
typeof(*raw_cpu_ptr(&(pcp))) __ret; \
252+
preempt_disable(); \
253+
__ret = cmpxchg_local(raw_cpu_ptr(&(pcp)), o, n); \
254+
preempt_enable(); \
255+
__ret; \
256+
})
257+
258+
#define this_cpu_cmpxchg_1(ptr, o, n) _protect_cmpxchg_local(ptr, o, n)
259+
#define this_cpu_cmpxchg_2(ptr, o, n) _protect_cmpxchg_local(ptr, o, n)
260+
#define this_cpu_cmpxchg_4(ptr, o, n) _protect_cmpxchg_local(ptr, o, n)
261+
#define this_cpu_cmpxchg_8(ptr, o, n) _protect_cmpxchg_local(ptr, o, n)
262+
263+
#define this_cpu_cmpxchg_double_8(ptr1, ptr2, o1, o2, n1, n2) \
264+
({ \
265+
int __ret; \
266+
preempt_disable(); \
267+
__ret = cmpxchg_double_local( raw_cpu_ptr(&(ptr1)), \
268+
raw_cpu_ptr(&(ptr2)), \
269+
o1, o2, n1, n2); \
270+
preempt_enable(); \
271+
__ret; \
272+
})
257273

258274
#define cmpxchg64(ptr,o,n) cmpxchg((ptr),(o),(n))
259275
#define cmpxchg64_local(ptr,o,n) cmpxchg_local((ptr),(o),(n))

arch/arm64/include/asm/percpu.h

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -204,25 +204,47 @@ static inline unsigned long __percpu_xchg(void *ptr, unsigned long val,
204204
return ret;
205205
}
206206

207+
#define _percpu_read(pcp) \
208+
({ \
209+
typeof(pcp) __retval; \
210+
preempt_disable(); \
211+
__retval = (typeof(pcp))__percpu_read(raw_cpu_ptr(&(pcp)), \
212+
sizeof(pcp)); \
213+
preempt_enable(); \
214+
__retval; \
215+
})
216+
217+
#define _percpu_write(pcp, val) \
218+
do { \
219+
preempt_disable(); \
220+
__percpu_write(raw_cpu_ptr(&(pcp)), (unsigned long)(val), \
221+
sizeof(pcp)); \
222+
preempt_enable(); \
223+
} while(0) \
224+
225+
#define _pcp_protect(operation, pcp, val) \
226+
({ \
227+
typeof(pcp) __retval; \
228+
preempt_disable(); \
229+
__retval = (typeof(pcp))operation(raw_cpu_ptr(&(pcp)), \
230+
(val), sizeof(pcp)); \
231+
preempt_enable(); \
232+
__retval; \
233+
})
234+
207235
#define _percpu_add(pcp, val) \
208-
__percpu_add(raw_cpu_ptr(&(pcp)), val, sizeof(pcp))
236+
_pcp_protect(__percpu_add, pcp, val)
209237

210-
#define _percpu_add_return(pcp, val) (typeof(pcp)) (_percpu_add(pcp, val))
238+
#define _percpu_add_return(pcp, val) _percpu_add(pcp, val)
211239

212240
#define _percpu_and(pcp, val) \
213-
__percpu_and(raw_cpu_ptr(&(pcp)), val, sizeof(pcp))
241+
_pcp_protect(__percpu_and, pcp, val)
214242

215243
#define _percpu_or(pcp, val) \
216-
__percpu_or(raw_cpu_ptr(&(pcp)), val, sizeof(pcp))
217-
218-
#define _percpu_read(pcp) (typeof(pcp)) \
219-
(__percpu_read(raw_cpu_ptr(&(pcp)), sizeof(pcp)))
220-
221-
#define _percpu_write(pcp, val) \
222-
__percpu_write(raw_cpu_ptr(&(pcp)), (unsigned long)(val), sizeof(pcp))
244+
_pcp_protect(__percpu_or, pcp, val)
223245

224246
#define _percpu_xchg(pcp, val) (typeof(pcp)) \
225-
(__percpu_xchg(raw_cpu_ptr(&(pcp)), (unsigned long)(val), sizeof(pcp)))
247+
_pcp_protect(__percpu_xchg, pcp, (unsigned long)(val))
226248

227249
#define this_cpu_add_1(pcp, val) _percpu_add(pcp, val)
228250
#define this_cpu_add_2(pcp, val) _percpu_add(pcp, val)

0 commit comments

Comments
 (0)