public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/5] linux/minmax.h: add non-atomic version of xchg
@ 2022-12-09 15:48 Andrzej Hajda
  2022-12-09 15:48 ` [PATCH 2/5] drm/i915/display: kill fetch_and_zero usage Andrzej Hajda
                   ` (6 more replies)
  0 siblings, 7 replies; 23+ messages in thread
From: Andrzej Hajda @ 2022-12-09 15:48 UTC (permalink / raw)
  To: linux-kernel, intel-gfx, dri-devel
  Cc: Andrzej Hajda, Jani Nikula, Joonas Lahtinen, Rodrigo Vivi,
	Tvrtko Ursulin, Andi Shyti, Andrew Morton, Andy Shevchenko,
	Arnd Bergmann

The pattern of setting variable with new value and returning old
one is very common in kernel. Usually atomicity of the operation
is not required, so xchg seems to be suboptimal and confusing in
such cases. Since name xchg is already in use and __xchg is used
in architecture code, proposition is to name the macro exchange.

Signed-off-by: Andrzej Hajda <andrzej.hajda@intel.com>
---
Hi,

I hope there will be place for such tiny helper in kernel.
Quick cocci analyze shows there is probably few thousands places
where it could be used, of course I do not intend to do it :).

I was not sure where to put this macro, I hope near swap definition
is the most suitable place.

Moreover sorry if to/cc is not correct - get_maintainers.pl was
more confused than me, to who address this patch.

Regards
Andrzej
---
 include/linux/minmax.h | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/include/linux/minmax.h b/include/linux/minmax.h
index 5433c08fcc6858..17d48769203bd5 100644
--- a/include/linux/minmax.h
+++ b/include/linux/minmax.h
@@ -144,4 +144,18 @@
 #define swap(a, b) \
 	do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
 
+/**
+ * exchange - set variable pointed by @ptr to @val, return old value
+ * @ptr: pointer to affected variable
+ * @val: value to be written
+ *
+ * This is non-atomic variant of xchg.
+ */
+#define exchange(ptr, val) ({		\
+	typeof(ptr) __ptr = ptr;	\
+	typeof(*__ptr) __t = *__ptr;	\
+	*(__ptr) = (val);		\
+	__t;				\
+})
+
 #endif	/* _LINUX_MINMAX_H */
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 23+ messages in thread

end of thread, other threads:[~2023-01-05 16:17 UTC | newest]

Thread overview: 23+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-12-09 15:48 [PATCH 1/5] linux/minmax.h: add non-atomic version of xchg Andrzej Hajda
2022-12-09 15:48 ` [PATCH 2/5] drm/i915/display: kill fetch_and_zero usage Andrzej Hajda
2022-12-09 15:48 ` [PATCH 3/5] drm/i915/gt: " Andrzej Hajda
2022-12-12  9:14   ` [Intel-gfx] " Upadhyay, Tejas
2022-12-12  9:23     ` Andrzej Hajda
2022-12-09 15:48 ` [PATCH 4/5] drm/i915/gvt: " Andrzej Hajda
2022-12-09 15:48 ` [PATCH 5/5] drm/i915: kill fetch_and_zero Andrzej Hajda
2022-12-09 17:16 ` [PATCH 1/5] linux/minmax.h: add non-atomic version of xchg Arnd Bergmann
2022-12-13  9:28   ` [Intel-gfx] " Andrzej Hajda
2022-12-13  9:35     ` Arnd Bergmann
2022-12-09 18:56 ` Andy Shevchenko
2022-12-09 18:58   ` Andy Shevchenko
2022-12-13 10:09   ` [Intel-gfx] " Andrzej Hajda
2022-12-13 10:27     ` Andy Shevchenko
2022-12-12  9:38 ` David Laight
2023-01-05 13:04   ` Daniel Vetter
2023-01-05 13:28     ` [Intel-gfx] " Jani Nikula
2023-01-05 13:34       ` David Laight
2023-01-05 14:13         ` Daniel Vetter
2023-01-05 14:41           ` David Laight
2023-01-05 14:57             ` Daniel Vetter
2023-01-05 16:16               ` Mark Rutland
2023-01-05 14:13         ` Tvrtko Ursulin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox