From: Suren Baghdasaryan <surenb@google.com>
To: akpm@linux-foundation.org
Cc: viro@zeniv.linux.org.uk, brauner@kernel.org, jack@suse.cz,
dchinner@redhat.com, casey@schaufler-ca.com,
ben.wolsieffer@hefring.com, paulmck@kernel.org,
david@redhat.com, avagin@google.com, usama.anjum@collabora.com,
peterx@redhat.com, hughd@google.com, ryan.roberts@arm.com,
wangkefeng.wang@huawei.com, Liam.Howlett@Oracle.com,
yuzhao@google.com, axelrasmussen@google.com, lstoakes@gmail.com,
talumbau@google.com, willy@infradead.org, vbabka@suse.cz,
mgorman@techsingularity.net, jhubbard@nvidia.com,
vishal.moola@gmail.com, mathieu.desnoyers@efficios.com,
dhowells@redhat.com, jgg@ziepe.ca, sidhartha.kumar@oracle.com,
andriy.shevchenko@linux.intel.com, yangxingui@huawei.com,
keescook@chromium.org, linux-kernel@vger.kernel.org,
linux-fsdevel@vger.kernel.org, linux-mm@kvack.org,
kernel-team@android.com, surenb@google.com
Subject: [PATCH 2/3] mm: add mm_struct sequence number to detect write locks
Date: Sun, 21 Jan 2024 23:13:23 -0800 [thread overview]
Message-ID: <20240122071324.2099712-2-surenb@google.com> (raw)
In-Reply-To: <20240122071324.2099712-1-surenb@google.com>
Provide a way for lockless mm_struct users to detect whether mm might have
been changed since some specific point in time. The API provided allows
the user to record a counter when it starts using the mm and later use
that counter to check if anyone write-locked mmap_lock since the counter
was recorded. Recording the counter value should be done while holding
mmap_lock at least for reading to prevent the counter from concurrent
changes. Every time mmap_lock is write-locked mm_struct updates its
mm_wr_seq counter so that checks against counters recorded before that
would fail, indicating a possibility of mm being modified.
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
---
include/linux/mm_types.h | 2 ++
include/linux/mmap_lock.h | 22 ++++++++++++++++++++++
2 files changed, 24 insertions(+)
diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
index bbe1223cd992..e749f7f09314 100644
--- a/include/linux/mm_types.h
+++ b/include/linux/mm_types.h
@@ -846,6 +846,8 @@ struct mm_struct {
*/
int mm_lock_seq;
#endif
+ /* Counter incremented each time mm gets write-locked */
+ unsigned long mm_wr_seq;
unsigned long hiwater_rss; /* High-watermark of RSS usage */
diff --git a/include/linux/mmap_lock.h b/include/linux/mmap_lock.h
index 8d38dcb6d044..0197079cb6fe 100644
--- a/include/linux/mmap_lock.h
+++ b/include/linux/mmap_lock.h
@@ -106,6 +106,8 @@ static inline void mmap_write_lock(struct mm_struct *mm)
{
__mmap_lock_trace_start_locking(mm, true);
down_write(&mm->mmap_lock);
+ /* Pairs with ACQUIRE semantics in mmap_write_seq_read */
+ smp_store_release(&mm->mm_wr_seq, mm->mm_wr_seq + 1);
__mmap_lock_trace_acquire_returned(mm, true, true);
}
@@ -113,6 +115,8 @@ static inline void mmap_write_lock_nested(struct mm_struct *mm, int subclass)
{
__mmap_lock_trace_start_locking(mm, true);
down_write_nested(&mm->mmap_lock, subclass);
+ /* Pairs with ACQUIRE semantics in mmap_write_seq_read */
+ smp_store_release(&mm->mm_wr_seq, mm->mm_wr_seq + 1);
__mmap_lock_trace_acquire_returned(mm, true, true);
}
@@ -122,6 +126,10 @@ static inline int mmap_write_lock_killable(struct mm_struct *mm)
__mmap_lock_trace_start_locking(mm, true);
ret = down_write_killable(&mm->mmap_lock);
+ if (!ret) {
+ /* Pairs with ACQUIRE semantics in mmap_write_seq_read */
+ smp_store_release(&mm->mm_wr_seq, mm->mm_wr_seq + 1);
+ }
__mmap_lock_trace_acquire_returned(mm, true, ret == 0);
return ret;
}
@@ -140,6 +148,20 @@ static inline void mmap_write_downgrade(struct mm_struct *mm)
downgrade_write(&mm->mmap_lock);
}
+static inline unsigned long mmap_write_seq_read(struct mm_struct *mm)
+{
+ /* Pairs with RELEASE semantics in mmap_write_lock */
+ return smp_load_acquire(&mm->mm_wr_seq);
+}
+
+static inline void mmap_write_seq_record(struct mm_struct *mm,
+ unsigned long *mm_wr_seq)
+{
+ mmap_assert_locked(mm);
+ /* Nobody can concurrently modify since we hold the mmap_lock */
+ *mm_wr_seq = mm->mm_wr_seq;
+}
+
static inline void mmap_read_lock(struct mm_struct *mm)
{
__mmap_lock_trace_start_locking(mm, false);
--
2.43.0.429.g432eaa2c6b-goog
next prev parent reply other threads:[~2024-01-22 7:13 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-01-22 7:13 [PATCH 1/3] mm: make vm_area_struct anon_name field RCU-safe Suren Baghdasaryan
2024-01-22 7:13 ` Suren Baghdasaryan [this message]
2024-01-22 7:13 ` [PATCH 3/3] mm/maps: read proc/pid/maps under RCU Suren Baghdasaryan
2024-01-23 5:36 ` SeongJae Park
2024-01-23 6:07 ` Suren Baghdasaryan
2024-01-23 23:12 ` Suren Baghdasaryan
2024-01-23 23:48 ` SeongJae Park
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=20240122071324.2099712-2-surenb@google.com \
--to=surenb@google.com \
--cc=Liam.Howlett@Oracle.com \
--cc=akpm@linux-foundation.org \
--cc=andriy.shevchenko@linux.intel.com \
--cc=avagin@google.com \
--cc=axelrasmussen@google.com \
--cc=ben.wolsieffer@hefring.com \
--cc=brauner@kernel.org \
--cc=casey@schaufler-ca.com \
--cc=david@redhat.com \
--cc=dchinner@redhat.com \
--cc=dhowells@redhat.com \
--cc=hughd@google.com \
--cc=jack@suse.cz \
--cc=jgg@ziepe.ca \
--cc=jhubbard@nvidia.com \
--cc=keescook@chromium.org \
--cc=kernel-team@android.com \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=lstoakes@gmail.com \
--cc=mathieu.desnoyers@efficios.com \
--cc=mgorman@techsingularity.net \
--cc=paulmck@kernel.org \
--cc=peterx@redhat.com \
--cc=ryan.roberts@arm.com \
--cc=sidhartha.kumar@oracle.com \
--cc=talumbau@google.com \
--cc=usama.anjum@collabora.com \
--cc=vbabka@suse.cz \
--cc=viro@zeniv.linux.org.uk \
--cc=vishal.moola@gmail.com \
--cc=wangkefeng.wang@huawei.com \
--cc=willy@infradead.org \
--cc=yangxingui@huawei.com \
--cc=yuzhao@google.com \
/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