From: Suren Baghdasaryan <surenb@google.com>
To: akpm@linux-foundation.org
Cc: willy@infradead.org, hannes@cmpxchg.org, mhocko@suse.com,
josef@toxicpanda.com, jack@suse.cz, ldufour@linux.ibm.com,
laurent.dufour@fr.ibm.com, michel@lespinasse.org,
liam.howlett@oracle.com, jglisse@google.com, vbabka@suse.cz,
minchan@google.com, dave@stgolabs.net,
punit.agrawal@bytedance.com, lstoakes@gmail.com,
hdanton@sina.com, apopple@nvidia.com, surenb@google.com,
linux-mm@kvack.org, linux-fsdevel@vger.kernel.org,
linux-kernel@vger.kernel.org, kernel-team@android.com
Subject: [PATCH 3/3] mm: implement folio wait under VMA lock
Date: Mon, 1 May 2023 10:50:25 -0700 [thread overview]
Message-ID: <20230501175025.36233-3-surenb@google.com> (raw)
In-Reply-To: <20230501175025.36233-1-surenb@google.com>
Follow the same pattern as mmap_lock when waiting for folio by dropping
VMA lock before the wait and retrying once folio is available.
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
---
include/linux/pagemap.h | 14 ++++++++++----
mm/filemap.c | 43 ++++++++++++++++++++++-------------------
mm/memory.c | 13 +++++++++----
3 files changed, 42 insertions(+), 28 deletions(-)
diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h
index a56308a9d1a4..6c9493314c21 100644
--- a/include/linux/pagemap.h
+++ b/include/linux/pagemap.h
@@ -896,8 +896,8 @@ static inline bool wake_page_match(struct wait_page_queue *wait_page,
void __folio_lock(struct folio *folio);
int __folio_lock_killable(struct folio *folio);
-bool __folio_lock_or_retry(struct folio *folio, struct mm_struct *mm,
- unsigned int flags);
+bool __folio_lock_or_retry(struct folio *folio, struct vm_area_struct *vma,
+ unsigned int flags, bool *lock_dropped);
void unlock_page(struct page *page);
void folio_unlock(struct folio *folio);
@@ -1002,10 +1002,16 @@ static inline int folio_lock_killable(struct folio *folio)
* __folio_lock_or_retry().
*/
static inline bool folio_lock_or_retry(struct folio *folio,
- struct mm_struct *mm, unsigned int flags)
+ struct vm_area_struct *vma, unsigned int flags,
+ bool *lock_dropped)
{
might_sleep();
- return folio_trylock(folio) || __folio_lock_or_retry(folio, mm, flags);
+ if (folio_trylock(folio)) {
+ *lock_dropped = false;
+ return true;
+ }
+
+ return __folio_lock_or_retry(folio, vma, flags, lock_dropped);
}
/*
diff --git a/mm/filemap.c b/mm/filemap.c
index 84f39114d4de..9c0fa8578b2f 100644
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -1701,37 +1701,35 @@ static int __folio_lock_async(struct folio *folio, struct wait_page_queue *wait)
/*
* Return values:
- * true - folio is locked; mmap_lock is still held.
+ * true - folio is locked.
* false - folio is not locked.
- * mmap_lock has been released (mmap_read_unlock(), unless flags had both
- * FAULT_FLAG_ALLOW_RETRY and FAULT_FLAG_RETRY_NOWAIT set, in
- * which case mmap_lock is still held.
- * If flags had FAULT_FLAG_VMA_LOCK set, meaning the operation is performed
- * with VMA lock only, the VMA lock is still held.
+ *
+ * lock_dropped indicates whether mmap_lock/VMA lock got dropped.
+ * mmap_lock/VMA lock is dropped when function fails to lock the folio,
+ * unless flags had both FAULT_FLAG_ALLOW_RETRY and FAULT_FLAG_RETRY_NOWAIT
+ * set, in which case mmap_lock/VMA lock is still held.
*
* If neither ALLOW_RETRY nor KILLABLE are set, will always return true
- * with the folio locked and the mmap_lock unperturbed.
+ * with the folio locked and the mmap_lock/VMA lock unperturbed.
*/
-bool __folio_lock_or_retry(struct folio *folio, struct mm_struct *mm,
- unsigned int flags)
+bool __folio_lock_or_retry(struct folio *folio, struct vm_area_struct *vma,
+ unsigned int flags, bool *lock_dropped)
{
- /* Can't do this if not holding mmap_lock */
- if (flags & FAULT_FLAG_VMA_LOCK)
- return false;
-
if (fault_flag_allow_retry_first(flags)) {
- /*
- * CAUTION! In this case, mmap_lock is not released
- * even though return 0.
- */
- if (flags & FAULT_FLAG_RETRY_NOWAIT)
+ if (flags & FAULT_FLAG_RETRY_NOWAIT) {
+ *lock_dropped = false;
return false;
+ }
- mmap_read_unlock(mm);
+ if (flags & FAULT_FLAG_VMA_LOCK)
+ vma_end_read(vma);
+ else
+ mmap_read_unlock(vma->vm_mm);
if (flags & FAULT_FLAG_KILLABLE)
folio_wait_locked_killable(folio);
else
folio_wait_locked(folio);
+ *lock_dropped = true;
return false;
}
if (flags & FAULT_FLAG_KILLABLE) {
@@ -1739,13 +1737,18 @@ bool __folio_lock_or_retry(struct folio *folio, struct mm_struct *mm,
ret = __folio_lock_killable(folio);
if (ret) {
- mmap_read_unlock(mm);
+ if (flags & FAULT_FLAG_VMA_LOCK)
+ vma_end_read(vma);
+ else
+ mmap_read_unlock(vma->vm_mm);
+ *lock_dropped = true;
return false;
}
} else {
__folio_lock(folio);
}
+ *lock_dropped = false;
return true;
}
diff --git a/mm/memory.c b/mm/memory.c
index 8222acf74fd3..e1cd39f00756 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -3568,6 +3568,7 @@ static vm_fault_t remove_device_exclusive_entry(struct vm_fault *vmf)
struct folio *folio = page_folio(vmf->page);
struct vm_area_struct *vma = vmf->vma;
struct mmu_notifier_range range;
+ bool lock_dropped;
/*
* We need a reference to lock the folio because we don't hold
@@ -3580,8 +3581,10 @@ static vm_fault_t remove_device_exclusive_entry(struct vm_fault *vmf)
if (!folio_try_get(folio))
return 0;
- if (!folio_lock_or_retry(folio, vma->vm_mm, vmf->flags)) {
+ if (!folio_lock_or_retry(folio, vma, vmf->flags, &lock_dropped)) {
folio_put(folio);
+ if (lock_dropped && vmf->flags & FAULT_FLAG_VMA_LOCK)
+ return VM_FAULT_VMA_UNLOCKED | VM_FAULT_RETRY;
return VM_FAULT_RETRY;
}
mmu_notifier_range_init_owner(&range, MMU_NOTIFY_EXCLUSIVE, 0,
@@ -3704,7 +3707,8 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)
bool exclusive = false;
swp_entry_t entry;
pte_t pte;
- int locked;
+ bool locked;
+ bool lock_dropped;
vm_fault_t ret = 0;
void *shadow = NULL;
@@ -3837,9 +3841,10 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)
goto out_release;
}
- locked = folio_lock_or_retry(folio, vma->vm_mm, vmf->flags);
-
+ locked = folio_lock_or_retry(folio, vma, vmf->flags, &lock_dropped);
if (!locked) {
+ if (lock_dropped && vmf->flags & FAULT_FLAG_VMA_LOCK)
+ ret |= VM_FAULT_VMA_UNLOCKED;
ret |= VM_FAULT_RETRY;
goto out_release;
}
--
2.40.1.495.gc816e09b53d-goog
next prev parent reply other threads:[~2023-05-01 17:50 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-05-01 17:50 [PATCH 1/3] mm: handle swap page faults under VMA lock if page is uncontended Suren Baghdasaryan
2023-05-01 17:50 ` [PATCH 2/3] mm: drop VMA lock before waiting for migration Suren Baghdasaryan
2023-05-02 13:21 ` Alistair Popple
2023-05-02 16:39 ` Suren Baghdasaryan
2023-05-03 13:03 ` Alistair Popple
2023-05-03 19:42 ` Suren Baghdasaryan
2023-05-02 14:28 ` Matthew Wilcox
2023-05-02 16:41 ` Suren Baghdasaryan
2023-05-01 17:50 ` Suren Baghdasaryan [this message]
2023-05-02 2:02 ` [PATCH 1/3] mm: handle swap page faults under VMA lock if page is uncontended Matthew Wilcox
[not found] ` <CAJuCfpHfAFx9rjv0gHK77LbP-8gd-kFnWw=aqfQTP6pH=zvMNg@mail.gmail.com>
2023-05-02 3:22 ` Matthew Wilcox
2023-05-02 5:04 ` Suren Baghdasaryan
2023-05-02 15:03 ` Matthew Wilcox
2023-05-02 16:36 ` Suren Baghdasaryan
2023-05-02 22:31 ` Matthew Wilcox
2023-05-02 23:04 ` Suren Baghdasaryan
2023-05-02 23:40 ` Matthew Wilcox
2023-05-03 1:05 ` Suren Baghdasaryan
2023-05-03 8:34 ` Yosry Ahmed
2023-05-03 19:57 ` Suren Baghdasaryan
2023-05-03 20:57 ` Yosry Ahmed
2023-05-05 5:02 ` Huang, Ying
2023-05-05 22:30 ` Suren Baghdasaryan
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=20230501175025.36233-3-surenb@google.com \
--to=surenb@google.com \
--cc=akpm@linux-foundation.org \
--cc=apopple@nvidia.com \
--cc=dave@stgolabs.net \
--cc=hannes@cmpxchg.org \
--cc=hdanton@sina.com \
--cc=jack@suse.cz \
--cc=jglisse@google.com \
--cc=josef@toxicpanda.com \
--cc=kernel-team@android.com \
--cc=laurent.dufour@fr.ibm.com \
--cc=ldufour@linux.ibm.com \
--cc=liam.howlett@oracle.com \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=lstoakes@gmail.com \
--cc=mhocko@suse.com \
--cc=michel@lespinasse.org \
--cc=minchan@google.com \
--cc=punit.agrawal@bytedance.com \
--cc=vbabka@suse.cz \
--cc=willy@infradead.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