linux-doc.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH rcu 2/7] docs: Improve discussion of this_cpu_ptr(), add raw_cpu_ptr()
       [not found] <a2c2793d-771a-436e-8c42-4cb38def0e2f@paulmck-laptop>
@ 2025-01-16 20:02 ` Paul E. McKenney
  2025-01-16 21:02   ` Matthew Wilcox
  0 siblings, 1 reply; 4+ messages in thread
From: Paul E. McKenney @ 2025-01-16 20:02 UTC (permalink / raw)
  To: rcu
  Cc: linux-kernel, kernel-team, rostedt, Paul E. McKenney,
	Jonathan Corbet, Peter Zijlstra, Mark Rutland, Boqun Feng,
	linux-doc

Most of the this_cpu_*() operations may be used in preemptible code,
but not this_cpu_ptr(), and for good reasons.  Therefore, better explain
the reasons and call out raw_cpu_ptr() as an alternative in certain very
special cases.

Signed-off-by: Paul E. McKenney <paulmck@kernel.org>
Cc: Jonathan Corbet <corbet@lwn.net>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Boqun Feng <boqun.feng@gmail.com>
Cc: <linux-doc@vger.kernel.org>
---
 Documentation/core-api/this_cpu_ops.rst | 22 ++++++++++++++++------
 1 file changed, 16 insertions(+), 6 deletions(-)

diff --git a/Documentation/core-api/this_cpu_ops.rst b/Documentation/core-api/this_cpu_ops.rst
index 91acbcf30e9bd..11e3e48731553 100644
--- a/Documentation/core-api/this_cpu_ops.rst
+++ b/Documentation/core-api/this_cpu_ops.rst
@@ -138,12 +138,22 @@ get_cpu/put_cpu sequence requires. No processor number is
 available. Instead, the offset of the local per cpu area is simply
 added to the per cpu offset.
 
-Note that this operation is usually used in a code segment when
-preemption has been disabled. The pointer is then used to
-access local per cpu data in a critical section. When preemption
-is re-enabled this pointer is usually no longer useful since it may
-no longer point to per cpu data of the current processor.
-
+Note that this operation can only be used in code segments where
+smp_processor_id() may be used, for example, where preemption has been
+disabled. The pointer is then used to access local per cpu data in a
+critical section. When preemption is re-enabled this pointer is usually
+no longer useful since it may no longer point to per cpu data of the
+current processor.
+
+The special cases where it makes sense do obtain a per-CPU pointer in
+preemptible code are addressed by raw_cpu_ptr(), but please note that such
+use cases need to handle cases where two different CPUs are accessing
+the same per cpu variable, which might well be that of a third CPU.
+These use cases are typically performance optimizations.  For example,
+SRCU implements a pair of counters as a pair of per-CPU variables,
+and rcu_read_lock_nmisafe() uses raw_cpu_ptr() to get a pointer to some
+CPU's counter, and uses atomic_inc_long() to handle migration between
+the raw_cpu_ptr() and the atomic_inc_long().
 
 Per cpu variables and offsets
 -----------------------------
-- 
2.40.1


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

* Re: [PATCH rcu 2/7] docs: Improve discussion of this_cpu_ptr(), add raw_cpu_ptr()
  2025-01-16 20:02 ` [PATCH rcu 2/7] docs: Improve discussion of this_cpu_ptr(), add raw_cpu_ptr() Paul E. McKenney
@ 2025-01-16 21:02   ` Matthew Wilcox
  2025-01-16 22:57     ` Paul E. McKenney
  0 siblings, 1 reply; 4+ messages in thread
From: Matthew Wilcox @ 2025-01-16 21:02 UTC (permalink / raw)
  To: Paul E. McKenney
  Cc: rcu, linux-kernel, kernel-team, rostedt, Jonathan Corbet,
	Peter Zijlstra, Mark Rutland, Boqun Feng, linux-doc

On Thu, Jan 16, 2025 at 12:02:34PM -0800, Paul E. McKenney wrote:
> +The special cases where it makes sense do obtain a per-CPU pointer in

s/do/to/

> +preemptible code are addressed by raw_cpu_ptr(), but please note that such

s/please note that //

> +use cases need to handle cases where two different CPUs are accessing
> +the same per cpu variable, which might well be that of a third CPU.
> +These use cases are typically performance optimizations.  For example,
> +SRCU implements a pair of counters as a pair of per-CPU variables,
> +and rcu_read_lock_nmisafe() uses raw_cpu_ptr() to get a pointer to some
> +CPU's counter, and uses atomic_inc_long() to handle migration between
> +the raw_cpu_ptr() and the atomic_inc_long().

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

* Re: [PATCH rcu 2/7] docs: Improve discussion of this_cpu_ptr(), add raw_cpu_ptr()
  2025-01-16 21:02   ` Matthew Wilcox
@ 2025-01-16 22:57     ` Paul E. McKenney
  0 siblings, 0 replies; 4+ messages in thread
From: Paul E. McKenney @ 2025-01-16 22:57 UTC (permalink / raw)
  To: Matthew Wilcox
  Cc: rcu, linux-kernel, kernel-team, rostedt, Jonathan Corbet,
	Peter Zijlstra, Mark Rutland, Boqun Feng, linux-doc

On Thu, Jan 16, 2025 at 09:02:16PM +0000, Matthew Wilcox wrote:
> On Thu, Jan 16, 2025 at 12:02:34PM -0800, Paul E. McKenney wrote:
> > +The special cases where it makes sense do obtain a per-CPU pointer in
> 
> s/do/to/
> 
> > +preemptible code are addressed by raw_cpu_ptr(), but please note that such
> 
> s/please note that //

Good eyes, thank you!  I am a bit sorry to see the "please note that"
go, but I suppose fewer words is a good thing.

I will fix these on my next rebase and repost.

							Thanx, Paul

> > +use cases need to handle cases where two different CPUs are accessing
> > +the same per cpu variable, which might well be that of a third CPU.
> > +These use cases are typically performance optimizations.  For example,
> > +SRCU implements a pair of counters as a pair of per-CPU variables,
> > +and rcu_read_lock_nmisafe() uses raw_cpu_ptr() to get a pointer to some
> > +CPU's counter, and uses atomic_inc_long() to handle migration between
> > +the raw_cpu_ptr() and the atomic_inc_long().

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

* [PATCH rcu 2/7] docs: Improve discussion of this_cpu_ptr(), add raw_cpu_ptr()
  2025-02-18  5:45 [PATCH rcu 0/7] RCU documentation changes for v6.15 Boqun Feng
@ 2025-02-18  5:45 ` Boqun Feng
  0 siblings, 0 replies; 4+ messages in thread
From: Boqun Feng @ 2025-02-18  5:45 UTC (permalink / raw)
  To: rcu
  Cc: Paul E. McKenney, Frederic Weisbecker, Neeraj Upadhyay,
	Joel Fernandes, Josh Triplett, Boqun Feng, Uladzislau Rezki,
	Steven Rostedt, Mathieu Desnoyers, Lai Jiangshan, Zqiang,
	Jonathan Corbet, linux-doc, linux-kernel, Peter Zijlstra,
	Mark Rutland

From: "Paul E. McKenney" <paulmck@kernel.org>

Most of the this_cpu_*() operations may be used in preemptible code,
but not this_cpu_ptr(), and for good reasons.  Therefore, better explain
the reasons and call out raw_cpu_ptr() as an alternative in certain very
special cases.

Signed-off-by: Paul E. McKenney <paulmck@kernel.org>
Cc: Jonathan Corbet <corbet@lwn.net>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Boqun Feng <boqun.feng@gmail.com>
Cc: <linux-doc@vger.kernel.org>
Signed-off-by: Boqun Feng <boqun.feng@gmail.com>
---
 Documentation/core-api/this_cpu_ops.rst | 22 ++++++++++++++++------
 1 file changed, 16 insertions(+), 6 deletions(-)

diff --git a/Documentation/core-api/this_cpu_ops.rst b/Documentation/core-api/this_cpu_ops.rst
index 91acbcf30e9b..533ac5dd5750 100644
--- a/Documentation/core-api/this_cpu_ops.rst
+++ b/Documentation/core-api/this_cpu_ops.rst
@@ -138,12 +138,22 @@ get_cpu/put_cpu sequence requires. No processor number is
 available. Instead, the offset of the local per cpu area is simply
 added to the per cpu offset.
 
-Note that this operation is usually used in a code segment when
-preemption has been disabled. The pointer is then used to
-access local per cpu data in a critical section. When preemption
-is re-enabled this pointer is usually no longer useful since it may
-no longer point to per cpu data of the current processor.
-
+Note that this operation can only be used in code segments where
+smp_processor_id() may be used, for example, where preemption has been
+disabled. The pointer is then used to access local per cpu data in a
+critical section. When preemption is re-enabled this pointer is usually
+no longer useful since it may no longer point to per cpu data of the
+current processor.
+
+The special cases where it makes sense to obtain a per-CPU pointer in
+preemptible code are addressed by raw_cpu_ptr(), but such use cases need
+to handle cases where two different CPUs are accessing the same per cpu
+variable, which might well be that of a third CPU.  These use cases are
+typically performance optimizations.  For example, SRCU implements a pair
+of counters as a pair of per-CPU variables, and rcu_read_lock_nmisafe()
+uses raw_cpu_ptr() to get a pointer to some CPU's counter, and uses
+atomic_inc_long() to handle migration between the raw_cpu_ptr() and
+the atomic_inc_long().
 
 Per cpu variables and offsets
 -----------------------------
-- 
2.39.5 (Apple Git-154)


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

end of thread, other threads:[~2025-02-18  5:45 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <a2c2793d-771a-436e-8c42-4cb38def0e2f@paulmck-laptop>
2025-01-16 20:02 ` [PATCH rcu 2/7] docs: Improve discussion of this_cpu_ptr(), add raw_cpu_ptr() Paul E. McKenney
2025-01-16 21:02   ` Matthew Wilcox
2025-01-16 22:57     ` Paul E. McKenney
2025-02-18  5:45 [PATCH rcu 0/7] RCU documentation changes for v6.15 Boqun Feng
2025-02-18  5:45 ` [PATCH rcu 2/7] docs: Improve discussion of this_cpu_ptr(), add raw_cpu_ptr() Boqun Feng

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).