linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [RFC] WRITE_ONCE_INC() and friends
@ 2020-04-19  9:44 Petko Manolov
  2020-04-19 18:02 ` David Laight
  0 siblings, 1 reply; 12+ messages in thread
From: Petko Manolov @ 2020-04-19  9:44 UTC (permalink / raw)
  To: Paul E . McKenney; +Cc: LKML

	Hi Paul,

Recently I started reading up on KCSAN and at some point I ran into stuff like:

WRITE_ONCE(ssp->srcu_lock_nesting[idx], ssp->srcu_lock_nesting[idx] + 1);
WRITE_ONCE(p->mm->numa_scan_seq, READ_ONCE(p->mm->numa_scan_seq) + 1);

Some of these are a bit eye-watering for me and could easily be converted to:

WRITE_ONCE_INC(ssp->srcu_lock_nesting[idx]);
WRITE_ONCE_INC(p->mm->numa_scan_seq);

where the above macro could be either: 

#define	WRITE_ONCE_INC(x)	WRITE_ONCE(x, READ_ONCE(x) + 1)

or the more relaxed version:

#define	WRITE_ONCE_INC(x)	WRITE_ONCE(x, x + 1)

I personally like the stronger version better as a) it doesn't seem to increase 
code size (relative to the relaxed one), and b) should be less prone to load 
tearing, etc.  Given the growing popularity of KCSAN I expect a lot of 
concurrent code soon will get the READ_ONCE/WRITE_ONCE conversion.

If you think the above makes sense we could also do this:

#define	WRITE_ONCE_DEC(x)	WRITE_ONCE(x, READ_ONCE(x) - 1)
#define	WRITE_ONCE_ADD(x, v)	WRITE_ONCE(x, READ_ONCE(x) + v)
#define	WRITE_ONCE_SUB(x, v)	WRITE_ONCE(x, READ_ONCE(x) - v)


cheers,
Petko

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

end of thread, other threads:[~2020-04-21 13:19 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-04-19  9:44 [RFC] WRITE_ONCE_INC() and friends Petko Manolov
2020-04-19 18:02 ` David Laight
2020-04-19 18:29   ` Petko Manolov
2020-04-19 21:37     ` David Laight
2020-04-20 15:05       ` Paul E. McKenney
2020-04-20 16:32         ` Petko Manolov
2020-04-21  8:00           ` David Laight
2020-04-21  9:30             ` Petko Manolov
2020-04-20 22:57         ` Marco Elver
2020-04-20 23:12           ` Paul E. McKenney
2020-04-21  9:33             ` Marco Elver
2020-04-21 13:19               ` Paul E. McKenney

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).