* [PATCH v2 0/2] mm/gup: introduce pin_user_pages_locked(), use it in frame_vector.c
@ 2020-05-31 23:41 John Hubbard
2020-05-31 23:41 ` [PATCH v2 1/2] mm/gup: introduce pin_user_pages_locked() John Hubbard
2020-05-31 23:41 ` [PATCH v2 2/2] mm/gup: frame_vector: convert get_user_pages() --> pin_user_pages() John Hubbard
0 siblings, 2 replies; 3+ messages in thread
From: John Hubbard @ 2020-05-31 23:41 UTC (permalink / raw)
To: Andrew Morton
Cc: David Hildenbrand, Pankaj Gupta, Souptick Joarder, LKML,
linux-mm, John Hubbard
Hi,
Changes since v1:
* added an assert-and-return to the corresponding
get_user_pages_locked() call, to keep out any externally set FOLL_PIN flag,
thanks to Souptick Joarder's review for spotting that.
* Added Acked-by and Reviewed by tags from David and Pakaj
John Hubbard (2):
mm/gup: introduce pin_user_pages_locked()
mm/gup: frame_vector: convert get_user_pages() --> pin_user_pages()
include/linux/mm.h | 2 ++
mm/frame_vector.c | 7 +++----
mm/gup.c | 36 ++++++++++++++++++++++++++++++++++++
3 files changed, 41 insertions(+), 4 deletions(-)
base-commit: bdc48fa11e46f867ea4d75fa59ee87a7f48be144
--
2.26.2
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH v2 1/2] mm/gup: introduce pin_user_pages_locked()
2020-05-31 23:41 [PATCH v2 0/2] mm/gup: introduce pin_user_pages_locked(), use it in frame_vector.c John Hubbard
@ 2020-05-31 23:41 ` John Hubbard
2020-05-31 23:41 ` [PATCH v2 2/2] mm/gup: frame_vector: convert get_user_pages() --> pin_user_pages() John Hubbard
1 sibling, 0 replies; 3+ messages in thread
From: John Hubbard @ 2020-05-31 23:41 UTC (permalink / raw)
To: Andrew Morton
Cc: David Hildenbrand, Pankaj Gupta, Souptick Joarder, LKML,
linux-mm, John Hubbard
Introduce pin_user_pages_locked(), which is nearly identical to
get_user_pages_locked() except that it sets FOLL_PIN and rejects
FOLL_GET.
As with other pairs of get_user_pages*() and pin_user_pages() API calls,
it's prudent to assert that FOLL_PIN is *not* set in the
get_user_pages*() call, so add that as part of this.
Cc: Souptick Joarder <jrdr.linux@gmail.com>
Reviewed-by: David Hildenbrand <david@redhat.com>
Acked-by: Pankaj Gupta <pankaj.gupta.linux@gmail.com>
Signed-off-by: John Hubbard <jhubbard@nvidia.com>
---
include/linux/mm.h | 2 ++
mm/gup.c | 36 ++++++++++++++++++++++++++++++++++++
2 files changed, 38 insertions(+)
diff --git a/include/linux/mm.h b/include/linux/mm.h
index b098939691ba..5e29ddcbb5fc 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -1711,6 +1711,8 @@ long pin_user_pages(unsigned long start, unsigned long nr_pages,
struct vm_area_struct **vmas);
long get_user_pages_locked(unsigned long start, unsigned long nr_pages,
unsigned int gup_flags, struct page **pages, int *locked);
+long pin_user_pages_locked(unsigned long start, unsigned long nr_pages,
+ unsigned int gup_flags, struct page **pages, int *locked);
long get_user_pages_unlocked(unsigned long start, unsigned long nr_pages,
struct page **pages, unsigned int gup_flags);
long pin_user_pages_unlocked(unsigned long start, unsigned long nr_pages,
diff --git a/mm/gup.c b/mm/gup.c
index 82ef0db1590a..06b891297a5c 100644
--- a/mm/gup.c
+++ b/mm/gup.c
@@ -1993,6 +1993,12 @@ long get_user_pages_locked(unsigned long start, unsigned long nr_pages,
*/
if (WARN_ON_ONCE(gup_flags & FOLL_LONGTERM))
return -EINVAL;
+ /*
+ * FOLL_PIN must only be set internally by the pin_user_pages*() APIs,
+ * never directly by the caller, so enforce that:
+ */
+ if (WARN_ON_ONCE(gup_flags & FOLL_PIN))
+ return -EINVAL;
return __get_user_pages_locked(current, current->mm, start, nr_pages,
pages, NULL, locked,
@@ -2998,3 +3004,33 @@ long pin_user_pages_unlocked(unsigned long start, unsigned long nr_pages,
return get_user_pages_unlocked(start, nr_pages, pages, gup_flags);
}
EXPORT_SYMBOL(pin_user_pages_unlocked);
+
+/*
+ * pin_user_pages_locked() is the FOLL_PIN variant of get_user_pages_locked().
+ * Behavior is the same, except that this one sets FOLL_PIN and rejects
+ * FOLL_GET.
+ */
+long pin_user_pages_locked(unsigned long start, unsigned long nr_pages,
+ unsigned int gup_flags, struct page **pages,
+ int *locked)
+{
+ /*
+ * FIXME: Current FOLL_LONGTERM behavior is incompatible with
+ * FAULT_FLAG_ALLOW_RETRY because of the FS DAX check requirement on
+ * vmas. As there are no users of this flag in this call we simply
+ * disallow this option for now.
+ */
+ if (WARN_ON_ONCE(gup_flags & FOLL_LONGTERM))
+ return -EINVAL;
+
+ /* FOLL_GET and FOLL_PIN are mutually exclusive. */
+ if (WARN_ON_ONCE(gup_flags & FOLL_GET))
+ return -EINVAL;
+
+ gup_flags |= FOLL_PIN;
+ return __get_user_pages_locked(current, current->mm, start, nr_pages,
+ pages, NULL, locked,
+ gup_flags | FOLL_TOUCH);
+}
+EXPORT_SYMBOL(pin_user_pages_locked);
+
--
2.26.2
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [PATCH v2 2/2] mm/gup: frame_vector: convert get_user_pages() --> pin_user_pages()
2020-05-31 23:41 [PATCH v2 0/2] mm/gup: introduce pin_user_pages_locked(), use it in frame_vector.c John Hubbard
2020-05-31 23:41 ` [PATCH v2 1/2] mm/gup: introduce pin_user_pages_locked() John Hubbard
@ 2020-05-31 23:41 ` John Hubbard
1 sibling, 0 replies; 3+ messages in thread
From: John Hubbard @ 2020-05-31 23:41 UTC (permalink / raw)
To: Andrew Morton
Cc: David Hildenbrand, Pankaj Gupta, Souptick Joarder, LKML,
linux-mm, John Hubbard
This code was using get_user_pages*(), and all of the callers so far
were in a "Case 2" scenario (DMA/RDMA), using the categorization
from [1]. That means that it's time to convert the get_user_pages*() +
put_page() calls to pin_user_pages*() + unpin_user_pages() calls.
There is some helpful background in [2]: basically, this is a small
part of fixing a long-standing disconnect between pinning pages, and
file systems' use of those pages.
[1] Documentation/core-api/pin_user_pages.rst
[2] "Explicit pinning of user-space pages":
https://lwn.net/Articles/807108/
Cc: David Hildenbrand <david@redhat.com>
Signed-off-by: John Hubbard <jhubbard@nvidia.com>
---
mm/frame_vector.c | 7 +++----
1 file changed, 3 insertions(+), 4 deletions(-)
diff --git a/mm/frame_vector.c b/mm/frame_vector.c
index c431ca81dad5..4107dbca0056 100644
--- a/mm/frame_vector.c
+++ b/mm/frame_vector.c
@@ -72,7 +72,7 @@ int get_vaddr_frames(unsigned long start, unsigned int nr_frames,
if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) {
vec->got_ref = true;
vec->is_pfns = false;
- ret = get_user_pages_locked(start, nr_frames,
+ ret = pin_user_pages_locked(start, nr_frames,
gup_flags, (struct page **)(vec->ptrs), &locked);
goto out;
}
@@ -122,7 +122,6 @@ EXPORT_SYMBOL(get_vaddr_frames);
*/
void put_vaddr_frames(struct frame_vector *vec)
{
- int i;
struct page **pages;
if (!vec->got_ref)
@@ -135,8 +134,8 @@ void put_vaddr_frames(struct frame_vector *vec)
*/
if (WARN_ON(IS_ERR(pages)))
goto out;
- for (i = 0; i < vec->nr_frames; i++)
- put_page(pages[i]);
+
+ unpin_user_pages(pages, vec->nr_frames);
vec->got_ref = false;
out:
vec->nr_frames = 0;
--
2.26.2
^ permalink raw reply related [flat|nested] 3+ messages in thread
end of thread, other threads:[~2020-05-31 23:41 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-31 23:41 [PATCH v2 0/2] mm/gup: introduce pin_user_pages_locked(), use it in frame_vector.c John Hubbard
2020-05-31 23:41 ` [PATCH v2 1/2] mm/gup: introduce pin_user_pages_locked() John Hubbard
2020-05-31 23:41 ` [PATCH v2 2/2] mm/gup: frame_vector: convert get_user_pages() --> pin_user_pages() John Hubbard
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).