svn commit: r329974 - stable/11/sys/compat/linuxkpi/common/include/asm

Hans Petter Selasky hselasky at FreeBSD.org
Sun Feb 25 10:38:42 UTC 2018


Author: hselasky
Date: Sun Feb 25 10:38:42 2018
New Revision: 329974
URL: https://svnweb.freebsd.org/changeset/base/329974

Log:
  MFC r329523 and r329524:
  Fix implementation of xchg() function macro in the LinuxKPI.
  The exchange operation must be atomic.
  
  Sponsored by:	Mellanox Technologies

Modified:
  stable/11/sys/compat/linuxkpi/common/include/asm/atomic.h
Directory Properties:
  stable/11/   (props changed)

Modified: stable/11/sys/compat/linuxkpi/common/include/asm/atomic.h
==============================================================================
--- stable/11/sys/compat/linuxkpi/common/include/asm/atomic.h	Sun Feb 25 10:37:07 2018	(r329973)
+++ stable/11/sys/compat/linuxkpi/common/include/asm/atomic.h	Sun Feb 25 10:38:42 2018	(r329974)
@@ -198,12 +198,41 @@ atomic_cmpxchg(atomic_t *v, int old, int new)
 
 #define	cmpxchg_relaxed(...)	cmpxchg(__VA_ARGS__)
 
-#define	xchg(ptr, v) ({						\
-	__typeof(*(ptr)) __ret;					\
-								\
-	__ret = *(ptr);						\
-	*(ptr) = v;						\
-	__ret;							\
+#define	xchg(ptr, new) ({						\
+	union {								\
+		__typeof(*(ptr)) val;					\
+		u8 u8[0];						\
+		u16 u16[0];						\
+		u32 u32[0];						\
+		u64 u64[0];						\
+	} __ret, __new = { .val = (new) };				\
+									\
+	CTASSERT(sizeof(__ret.val) == 1 || sizeof(__ret.val) == 2 ||	\
+	    sizeof(__ret.val) == 4 || sizeof(__ret.val) == 8);		\
+									\
+	switch (sizeof(__ret.val)) {					\
+	case 1:								\
+		__ret.val = READ_ONCE(*ptr);				\
+		while (!atomic_fcmpset_8((volatile u8 *)(ptr),		\
+	            __ret.u8, __new.u8[0]))				\
+			;						\
+		break;							\
+	case 2:								\
+		__ret.val = READ_ONCE(*ptr);				\
+		while (!atomic_fcmpset_16((volatile u16 *)(ptr),	\
+	            __ret.u16, __new.u16[0]))				\
+			;						\
+		break;							\
+	case 4:								\
+		__ret.u32[0] = atomic_swap_32((volatile u32 *)(ptr),	\
+		    __new.u32[0]);					\
+		break;							\
+	case 8:								\
+		__ret.u64[0] = atomic_swap_64((volatile u64 *)(ptr),	\
+		    __new.u64[0]);					\
+		break;							\
+	}								\
+	__ret.val;							\
 })
 
 #define	LINUX_ATOMIC_OP(op, c_op)				\


More information about the svn-src-all mailing list