From: "Paul E. McKenney" <paulmck@kernel.org>
To: rcu@vger.kernel.org
Cc: linux-kernel@vger.kernel.org, kernel-team@meta.com,
rostedt@goodmis.org, "Paul E. McKenney" <paulmck@kernel.org>,
Jan Kara <jack@suse.cz>, Amir Goldstein <amir73il@gmail.com>
Subject: [PATCH rcu 5/9] rcu: Add srcu_down_read() and srcu_up_read()
Date: Wed, 4 Jan 2023 16:28:49 -0800 [thread overview]
Message-ID: <20230105002853.1769401-5-paulmck@kernel.org> (raw)
In-Reply-To: <20230105002845.GA1769240@paulmck-ThinkPad-P17-Gen-1>
A pair of matching srcu_read_lock() and srcu_read_unlock() invocations
must take place within the same context, for example, within the same
task. Otherwise, lockdep complains, as is the right thing to do for
most use cases.
However, there are use cases involving asynchronous I/O where the
SRCU reader needs to begin on one task and end on another. This commit
therefore supplies the semaphore-like srcu_down_read() and srcu_up_read(),
which act like srcu_read_lock() and srcu_read_unlock(), but permitting
srcu_up_read() to be invoked in a different context than was the matching
srcu_down_read().
Neither srcu_down_read() nor srcu_up_read() may be invoked from an
NMI handler.
Reported-by: Jan Kara <jack@suse.cz>
Signed-off-by: Paul E. McKenney <paulmck@kernel.org>
Tested-by: Amir Goldstein <amir73il@gmail.com>
---
include/linux/srcu.h | 45 ++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 45 insertions(+)
diff --git a/include/linux/srcu.h b/include/linux/srcu.h
index 9b9d0bbf1d3cf..74796cd7e7a9d 100644
--- a/include/linux/srcu.h
+++ b/include/linux/srcu.h
@@ -214,6 +214,34 @@ srcu_read_lock_notrace(struct srcu_struct *ssp) __acquires(ssp)
return retval;
}
+/**
+ * srcu_down_read - register a new reader for an SRCU-protected structure.
+ * @ssp: srcu_struct in which to register the new reader.
+ *
+ * Enter a semaphore-like SRCU read-side critical section. Note that
+ * SRCU read-side critical sections may be nested. However, it is
+ * illegal to call anything that waits on an SRCU grace period for the
+ * same srcu_struct, whether directly or indirectly. Please note that
+ * one way to indirectly wait on an SRCU grace period is to acquire
+ * a mutex that is held elsewhere while calling synchronize_srcu() or
+ * synchronize_srcu_expedited(). But if you want lockdep to help you
+ * keep this stuff straight, you should instead use srcu_read_lock().
+ *
+ * The semaphore-like nature of srcu_down_read() means that the matching
+ * srcu_up_read() can be invoked from some other context, for example,
+ * from some other task or from an irq handler. However, neither
+ * srcu_down_read() nor srcu_up_read() may be invoked from an NMI handler.
+ *
+ * Calls to srcu_down_read() may be nested, similar to the manner in
+ * which calls to down_read() may be nested.
+ */
+static inline int srcu_down_read(struct srcu_struct *ssp) __acquires(ssp)
+{
+ WARN_ON_ONCE(in_nmi());
+ srcu_check_nmi_safety(ssp, false);
+ return __srcu_read_lock(ssp);
+}
+
/**
* srcu_read_unlock - unregister a old reader from an SRCU-protected structure.
* @ssp: srcu_struct in which to unregister the old reader.
@@ -254,6 +282,23 @@ srcu_read_unlock_notrace(struct srcu_struct *ssp, int idx) __releases(ssp)
__srcu_read_unlock(ssp, idx);
}
+/**
+ * srcu_up_read - unregister a old reader from an SRCU-protected structure.
+ * @ssp: srcu_struct in which to unregister the old reader.
+ * @idx: return value from corresponding srcu_read_lock().
+ *
+ * Exit an SRCU read-side critical section, but not necessarily from
+ * the same context as the maching srcu_down_read().
+ */
+static inline void srcu_up_read(struct srcu_struct *ssp, int idx)
+ __releases(ssp)
+{
+ WARN_ON_ONCE(idx & ~0x1);
+ WARN_ON_ONCE(in_nmi());
+ srcu_check_nmi_safety(ssp, false);
+ __srcu_read_unlock(ssp, idx);
+}
+
/**
* smp_mb__after_srcu_read_unlock - ensure full ordering after srcu_read_unlock
*
--
2.31.1.189.g2e36527f23
next prev parent reply other threads:[~2023-01-05 0:32 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-01-05 0:28 [PATCH rcu 0/9] SRCU updates for v6.3 Paul E. McKenney
2023-01-05 0:28 ` [PATCH rcu 1/9] srcu: Release early_srcu resources when no longer in use Paul E. McKenney
2023-01-05 0:28 ` [PATCH rcu 2/9] srcu: Delegate work to the boot cpu if using SRCU_SIZE_SMALL Paul E. McKenney
2023-01-05 0:28 ` [PATCH rcu 3/9] srcu: Fix a misspelling in comment Paul E. McKenney
2023-01-05 0:28 ` [PATCH rcu 4/9] srcu: Fix the comparision in srcu_invl_snp_seq() Paul E. McKenney
2023-01-05 0:28 ` Paul E. McKenney [this message]
2023-01-05 0:28 ` [PATCH rcu 6/9] rcu: Add test code for semaphore-like SRCU readers Paul E. McKenney
2023-01-05 0:28 ` [PATCH rcu 7/9] srcu: Remove needless rcu_seq_done() check while holding read lock Paul E. McKenney
2023-01-05 0:28 ` [PATCH rcu 8/9] srcu: Yet more detail for srcu_readers_active_idx_check() comments Paul E. McKenney
2023-01-05 0:28 ` [PATCH rcu 9/9] srcu: Update comment after the index flip Paul E. McKenney
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230105002853.1769401-5-paulmck@kernel.org \
--to=paulmck@kernel.org \
--cc=amir73il@gmail.com \
--cc=jack@suse.cz \
--cc=kernel-team@meta.com \
--cc=linux-kernel@vger.kernel.org \
--cc=rcu@vger.kernel.org \
--cc=rostedt@goodmis.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox