Re: [PATCH v6 3/5] mm/gup: Introduce memfd_pin_user_pages() for pinning memfd pages (v6)
On 08.12.23 08:57, Kasireddy, Vivek wrote: Hi David, On 05.12.23 06:35, Vivek Kasireddy wrote: For drivers that would like to longterm-pin the pages associated with a memfd, the pin_user_pages_fd() API provides an option to not only pin the pages via FOLL_PIN but also to check and migrate them if they reside in movable zone or CMA block. This API currently works with memfds but it should work with any files that belong to either shmemfs or hugetlbfs. Files belonging to other filesystems are rejected for now. The pages need to be located first before pinning them via FOLL_PIN. If they are found in the page cache, they can be immediately pinned. Otherwise, they need to be allocated using the filesystem specific APIs and then pinned. v2: - Drop gup_flags and improve comments and commit message (David) - Allocate a page if we cannot find in page cache for the hugetlbfs case as well (David) - Don't unpin pages if there is a migration related failure (David) - Drop the unnecessary nr_pages <= 0 check (Jason) - Have the caller of the API pass in file * instead of fd (Jason) v3: (David) - Enclose the huge page allocation code with #ifdef CONFIG_HUGETLB_PAGE (Build error reported by kernel test robot ) - Don't forget memalloc_pin_restore() on non-migration related errors - Improve the readability of the cleanup code associated with non-migration related errors - Augment the comments by describing FOLL_LONGTERM like behavior - Include the R-b tag from Jason v4: - Remove the local variable "page" and instead use 3 return statements in alloc_file_page() (David) - Add the R-b tag from David v5: (David) - For hugetlb case, ensure that we only obtain head pages from the mapping by using __filemap_get_folio() instead of find_get_page_flags() - Handle -EEXIST when two or more potential users try to simultaneously add a huge page to the mapping by forcing them to retry on failure v6: (Christoph) - Rename this API to memfd_pin_user_pages() to make it clear that it is intended for memfds - Move the memfd page allocation helper from gup.c to memfd.c - Fix indentation errors in memfd_pin_user_pages() - For contiguous ranges of folios, use a helper such as filemap_get_folios_contig() to lookup the page cache in batches Cc: David Hildenbrand Cc: Christoph Hellwig Cc: Daniel Vetter Cc: Mike Kravetz Cc: Hugh Dickins Cc: Peter Xu Cc: Gerd Hoffmann Cc: Dongwon Kim Cc: Junxiao Chang Suggested-by: Jason Gunthorpe Reviewed-by: Jason Gunthorpe (v2) Reviewed-by: David Hildenbrand (v3) Signed-off-by: Vivek Kasireddy --- include/linux/memfd.h | 5 +++ include/linux/mm.h| 2 + mm/gup.c | 102 ++ mm/memfd.c| 34 ++ 4 files changed, 143 insertions(+) diff --git a/include/linux/memfd.h b/include/linux/memfd.h index e7abf6fa4c52..6fc0d1282151 100644 --- a/include/linux/memfd.h +++ b/include/linux/memfd.h @@ -6,11 +6,16 @@ #ifdef CONFIG_MEMFD_CREATE extern long memfd_fcntl(struct file *file, unsigned int cmd, unsigned int arg); +extern struct page *memfd_alloc_page(struct file *memfd, pgoff_t idx); #else static inline long memfd_fcntl(struct file *f, unsigned int c, unsigned int a) { return -EINVAL; } +static inline struct page *memfd_alloc_page(struct file *memfd, pgoff_t idx) +{ + return ERR_PTR(-EINVAL); +} #endif #endif /* __LINUX_MEMFD_H */ diff --git a/include/linux/mm.h b/include/linux/mm.h index 418d26608ece..ac69db45509f 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2472,6 +2472,8 @@ 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, struct page **pages, unsigned int gup_flags); +long memfd_pin_user_pages(struct file *file, pgoff_t start, + unsigned long nr_pages, struct page **pages); int get_user_pages_fast(unsigned long start, int nr_pages, unsigned int gup_flags, struct page **pages); diff --git a/mm/gup.c b/mm/gup.c index 231711efa390..eb93d1ec9dc6 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -5,6 +5,7 @@ #include #include +#include #include #include #include @@ -17,6 +18,7 @@ #include #include #include +#include #include #include @@ -3410,3 +3412,103 @@ long pin_user_pages_unlocked(unsigned long start, unsigned long nr_pages, &locked, gup_flags); } EXPORT_SYMBOL(pin_user_pages_unlocked); + +/** + * memfd_pin_user_pages() - pin user pages associated with a memfd + * @memfd: the memfd whose pages are to be pinned + * @start: starting memfd offset + * @nr_pages: number of pages from start to pin + * @pages: array that receives pointers to the
RE: [PATCH v6 3/5] mm/gup: Introduce memfd_pin_user_pages() for pinning memfd pages (v6)
Hi David, > > > >> On 05.12.23 06:35, Vivek Kasireddy wrote: > >>> For drivers that would like to longterm-pin the pages associated > >>> with a memfd, the pin_user_pages_fd() API provides an option to > >>> not only pin the pages via FOLL_PIN but also to check and migrate > >>> them if they reside in movable zone or CMA block. This API > >>> currently works with memfds but it should work with any files > >>> that belong to either shmemfs or hugetlbfs. Files belonging to > >>> other filesystems are rejected for now. > >>> > >>> The pages need to be located first before pinning them via FOLL_PIN. > >>> If they are found in the page cache, they can be immediately pinned. > >>> Otherwise, they need to be allocated using the filesystem specific > >>> APIs and then pinned. > >>> > >>> v2: > >>> - Drop gup_flags and improve comments and commit message (David) > >>> - Allocate a page if we cannot find in page cache for the hugetlbfs > >>> case as well (David) > >>> - Don't unpin pages if there is a migration related failure (David) > >>> - Drop the unnecessary nr_pages <= 0 check (Jason) > >>> - Have the caller of the API pass in file * instead of fd (Jason) > >>> > >>> v3: (David) > >>> - Enclose the huge page allocation code with #ifdef > >> CONFIG_HUGETLB_PAGE > >>> (Build error reported by kernel test robot ) > >>> - Don't forget memalloc_pin_restore() on non-migration related errors > >>> - Improve the readability of the cleanup code associated with > >>> non-migration related errors > >>> - Augment the comments by describing FOLL_LONGTERM like behavior > >>> - Include the R-b tag from Jason > >>> > >>> v4: > >>> - Remove the local variable "page" and instead use 3 return statements > >>> in alloc_file_page() (David) > >>> - Add the R-b tag from David > >>> > >>> v5: (David) > >>> - For hugetlb case, ensure that we only obtain head pages from the > >>> mapping by using __filemap_get_folio() instead of > find_get_page_flags() > >>> - Handle -EEXIST when two or more potential users try to simultaneously > >>> add a huge page to the mapping by forcing them to retry on failure > >>> > >>> v6: (Christoph) > >>> - Rename this API to memfd_pin_user_pages() to make it clear that it > >>> is intended for memfds > >>> - Move the memfd page allocation helper from gup.c to memfd.c > >>> - Fix indentation errors in memfd_pin_user_pages() > >>> - For contiguous ranges of folios, use a helper such as > >>> filemap_get_folios_contig() to lookup the page cache in batches > >>> > >>> Cc: David Hildenbrand > >>> Cc: Christoph Hellwig > >>> Cc: Daniel Vetter > >>> Cc: Mike Kravetz > >>> Cc: Hugh Dickins > >>> Cc: Peter Xu > >>> Cc: Gerd Hoffmann > >>> Cc: Dongwon Kim > >>> Cc: Junxiao Chang > >>> Suggested-by: Jason Gunthorpe > >>> Reviewed-by: Jason Gunthorpe (v2) > >>> Reviewed-by: David Hildenbrand (v3) > >>> Signed-off-by: Vivek Kasireddy > >>> --- > >>>include/linux/memfd.h | 5 +++ > >>>include/linux/mm.h| 2 + > >>>mm/gup.c | 102 > ++ > >>>mm/memfd.c| 34 ++ > >>>4 files changed, 143 insertions(+) > >>> > >>> diff --git a/include/linux/memfd.h b/include/linux/memfd.h > >>> index e7abf6fa4c52..6fc0d1282151 100644 > >>> --- a/include/linux/memfd.h > >>> +++ b/include/linux/memfd.h > >>> @@ -6,11 +6,16 @@ > >>> > >>>#ifdef CONFIG_MEMFD_CREATE > >>>extern long memfd_fcntl(struct file *file, unsigned int cmd, unsigned > >>> int > >> arg); > >>> +extern struct page *memfd_alloc_page(struct file *memfd, pgoff_t idx); > >>>#else > >>>static inline long memfd_fcntl(struct file *f, unsigned int c, > >>> unsigned int > a) > >>>{ > >>> return -EINVAL; > >>>} > >>> +static inline struct page *memfd_alloc_page(struct file *memfd, pgoff_t > >> idx) > >>> +{ > >>> + return ERR_PTR(-EINVAL); > >>> +} > >>>#endif > >>> > >>>#endif /* __LINUX_MEMFD_H */ > >>> diff --git a/include/linux/mm.h b/include/linux/mm.h > >>> index 418d26608ece..ac69db45509f 100644 > >>> --- a/include/linux/mm.h > >>> +++ b/include/linux/mm.h > >>> @@ -2472,6 +2472,8 @@ 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, > >>> struct page **pages, unsigned int gup_flags); > >>> +long memfd_pin_user_pages(struct file *file, pgoff_t start, > >>> + unsigned long nr_pages, struct page **pages); > >>> > >>>int get_user_pages_fast(unsigned long start, int nr_pages, > >>> unsigned int gup_flags, struct page **pages); > >>> diff --git a/mm/gup.c b/mm/gup.c > >>> index 231711efa390..eb93d1ec9dc6 100644 > >>> --- a/mm/gup.c > >>> +++ b/mm/gup.c > >>> @@ -5,6 +5,7 @@ > >>>#include > >>> > >>>#include > >>> +#include > >>>
Re: [PATCH v6 3/5] mm/gup: Introduce memfd_pin_user_pages() for pinning memfd pages (v6)
On 07.12.23 14:05, Jason Gunthorpe wrote: On Thu, Dec 07, 2023 at 10:44:14AM +0100, David Hildenbrand wrote: If you always want to return folios, then better name it "memfd_pin_user_folios" (or just "memfd_pin_folios") and pass in a range (instead of a nr_pages parameter), and somehow indicate to the caller how many folio were in that range, and if that range was fully covered. I think it makes sense to return folios from this interface; and considering my use-case, I'd like have this API return an error if it cannot pin (or allocate) the exact number of folios the caller requested. Okay, then better use folios. Assuming a caller puts in "start = X" and gets some large folio back. How is the caller supposed to know at which offset to look into that folio (IOW< which subpage)? For "pages" it was obvious (you get the actual subpages), but as soon as we return a large folio, some information is missing for the caller. How can the caller figure that out? This can only work if the memfd is required to only have full folios at aligned locations. Under that restriction computing the first folio offset is easy enough: folio offset = (start % folio size) But is that true for the memfds here? I assume folios are always naturally aligned, like: [ 2m ][ 2m ][1m][1m][ 2m ] ^f0 ^f1 ^f2 ^f3 ^f4 If you query the range "3m -> 7m", you get back f1,f2,f3,f4 and have to start in the middle of the first folio with offset 1m. From there, it is indeed simply continuing with the full folio size -- until the last folio, where you want to only process 1m. folio offset = (1m % 2m) would be correct in that case. -- Cheers, David / dhildenb
Re: [PATCH v6 3/5] mm/gup: Introduce memfd_pin_user_pages() for pinning memfd pages (v6)
On Thu, Dec 07, 2023 at 10:44:14AM +0100, David Hildenbrand wrote: > > > If you always want to return folios, then better name it > > > "memfd_pin_user_folios" (or just "memfd_pin_folios") and pass in a range > > > (instead of a nr_pages parameter), and somehow indicate to the caller > > > how many folio were in that range, and if that range was fully covered. > > I think it makes sense to return folios from this interface; and > > considering my > > use-case, I'd like have this API return an error if it cannot pin (or > > allocate) > > the exact number of folios the caller requested. > > Okay, then better use folios. > > Assuming a caller puts in "start = X" and gets some large folio back. How is > the caller supposed to know at which offset to look into that folio (IOW< > which subpage)? For "pages" it was obvious (you get the actual subpages), > but as soon as we return a large folio, some information is missing for the > caller. > > How can the caller figure that out? This can only work if the memfd is required to only have full folios at aligned locations. Under that restriction computing the first folio offset is easy enough: folio offset = (start % folio size) But is that true for the memfds here? > > I can make the udmabuf driver use folios instead of pages too but the > > function > > check_and_migrate_movable_pages() in GUP still takes a list of pages. Do you > > think it is ok to use a local variable to collect all the head pages for > > this? > > I think you can simply pass in the head page, because only whole folios can > be converted. At some point we should convert that one to use folios as > well. It is like that because it processes the output from GUP in-place which is a page list.. Probably what we need to do is make the migration checks happen while accumulating the pages so we don't need to scan the output list.. Jason
Re: [PATCH v6 3/5] mm/gup: Introduce memfd_pin_user_pages() for pinning memfd pages (v6)
On 07.12.23 06:09, Kasireddy, Vivek wrote: Hi David, On 05.12.23 06:35, Vivek Kasireddy wrote: For drivers that would like to longterm-pin the pages associated with a memfd, the pin_user_pages_fd() API provides an option to not only pin the pages via FOLL_PIN but also to check and migrate them if they reside in movable zone or CMA block. This API currently works with memfds but it should work with any files that belong to either shmemfs or hugetlbfs. Files belonging to other filesystems are rejected for now. The pages need to be located first before pinning them via FOLL_PIN. If they are found in the page cache, they can be immediately pinned. Otherwise, they need to be allocated using the filesystem specific APIs and then pinned. v2: - Drop gup_flags and improve comments and commit message (David) - Allocate a page if we cannot find in page cache for the hugetlbfs case as well (David) - Don't unpin pages if there is a migration related failure (David) - Drop the unnecessary nr_pages <= 0 check (Jason) - Have the caller of the API pass in file * instead of fd (Jason) v3: (David) - Enclose the huge page allocation code with #ifdef CONFIG_HUGETLB_PAGE (Build error reported by kernel test robot ) - Don't forget memalloc_pin_restore() on non-migration related errors - Improve the readability of the cleanup code associated with non-migration related errors - Augment the comments by describing FOLL_LONGTERM like behavior - Include the R-b tag from Jason v4: - Remove the local variable "page" and instead use 3 return statements in alloc_file_page() (David) - Add the R-b tag from David v5: (David) - For hugetlb case, ensure that we only obtain head pages from the mapping by using __filemap_get_folio() instead of find_get_page_flags() - Handle -EEXIST when two or more potential users try to simultaneously add a huge page to the mapping by forcing them to retry on failure v6: (Christoph) - Rename this API to memfd_pin_user_pages() to make it clear that it is intended for memfds - Move the memfd page allocation helper from gup.c to memfd.c - Fix indentation errors in memfd_pin_user_pages() - For contiguous ranges of folios, use a helper such as filemap_get_folios_contig() to lookup the page cache in batches Cc: David Hildenbrand Cc: Christoph Hellwig Cc: Daniel Vetter Cc: Mike Kravetz Cc: Hugh Dickins Cc: Peter Xu Cc: Gerd Hoffmann Cc: Dongwon Kim Cc: Junxiao Chang Suggested-by: Jason Gunthorpe Reviewed-by: Jason Gunthorpe (v2) Reviewed-by: David Hildenbrand (v3) Signed-off-by: Vivek Kasireddy --- include/linux/memfd.h | 5 +++ include/linux/mm.h| 2 + mm/gup.c | 102 ++ mm/memfd.c| 34 ++ 4 files changed, 143 insertions(+) diff --git a/include/linux/memfd.h b/include/linux/memfd.h index e7abf6fa4c52..6fc0d1282151 100644 --- a/include/linux/memfd.h +++ b/include/linux/memfd.h @@ -6,11 +6,16 @@ #ifdef CONFIG_MEMFD_CREATE extern long memfd_fcntl(struct file *file, unsigned int cmd, unsigned int arg); +extern struct page *memfd_alloc_page(struct file *memfd, pgoff_t idx); #else static inline long memfd_fcntl(struct file *f, unsigned int c, unsigned int a) { return -EINVAL; } +static inline struct page *memfd_alloc_page(struct file *memfd, pgoff_t idx) +{ + return ERR_PTR(-EINVAL); +} #endif #endif /* __LINUX_MEMFD_H */ diff --git a/include/linux/mm.h b/include/linux/mm.h index 418d26608ece..ac69db45509f 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2472,6 +2472,8 @@ 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, struct page **pages, unsigned int gup_flags); +long memfd_pin_user_pages(struct file *file, pgoff_t start, + unsigned long nr_pages, struct page **pages); int get_user_pages_fast(unsigned long start, int nr_pages, unsigned int gup_flags, struct page **pages); diff --git a/mm/gup.c b/mm/gup.c index 231711efa390..eb93d1ec9dc6 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -5,6 +5,7 @@ #include #include +#include #include #include #include @@ -17,6 +18,7 @@ #include #include #include +#include #include #include @@ -3410,3 +3412,103 @@ long pin_user_pages_unlocked(unsigned long start, unsigned long nr_pages, &locked, gup_flags); } EXPORT_SYMBOL(pin_user_pages_unlocked); + +/** + * memfd_pin_user_pages() - pin user pages associated with a memfd + * @memfd: the memfd whose pages are to be pinned + * @start: starting memfd offset + * @nr_pages: number of pages from start to pin + * @pages: array that receives pointers to the pages pinned. + * Should be at
RE: [PATCH v6 3/5] mm/gup: Introduce memfd_pin_user_pages() for pinning memfd pages (v6)
Hi David, > On 05.12.23 06:35, Vivek Kasireddy wrote: > > For drivers that would like to longterm-pin the pages associated > > with a memfd, the pin_user_pages_fd() API provides an option to > > not only pin the pages via FOLL_PIN but also to check and migrate > > them if they reside in movable zone or CMA block. This API > > currently works with memfds but it should work with any files > > that belong to either shmemfs or hugetlbfs. Files belonging to > > other filesystems are rejected for now. > > > > The pages need to be located first before pinning them via FOLL_PIN. > > If they are found in the page cache, they can be immediately pinned. > > Otherwise, they need to be allocated using the filesystem specific > > APIs and then pinned. > > > > v2: > > - Drop gup_flags and improve comments and commit message (David) > > - Allocate a page if we cannot find in page cache for the hugetlbfs > >case as well (David) > > - Don't unpin pages if there is a migration related failure (David) > > - Drop the unnecessary nr_pages <= 0 check (Jason) > > - Have the caller of the API pass in file * instead of fd (Jason) > > > > v3: (David) > > - Enclose the huge page allocation code with #ifdef > CONFIG_HUGETLB_PAGE > >(Build error reported by kernel test robot ) > > - Don't forget memalloc_pin_restore() on non-migration related errors > > - Improve the readability of the cleanup code associated with > >non-migration related errors > > - Augment the comments by describing FOLL_LONGTERM like behavior > > - Include the R-b tag from Jason > > > > v4: > > - Remove the local variable "page" and instead use 3 return statements > >in alloc_file_page() (David) > > - Add the R-b tag from David > > > > v5: (David) > > - For hugetlb case, ensure that we only obtain head pages from the > >mapping by using __filemap_get_folio() instead of find_get_page_flags() > > - Handle -EEXIST when two or more potential users try to simultaneously > >add a huge page to the mapping by forcing them to retry on failure > > > > v6: (Christoph) > > - Rename this API to memfd_pin_user_pages() to make it clear that it > >is intended for memfds > > - Move the memfd page allocation helper from gup.c to memfd.c > > - Fix indentation errors in memfd_pin_user_pages() > > - For contiguous ranges of folios, use a helper such as > >filemap_get_folios_contig() to lookup the page cache in batches > > > > Cc: David Hildenbrand > > Cc: Christoph Hellwig > > Cc: Daniel Vetter > > Cc: Mike Kravetz > > Cc: Hugh Dickins > > Cc: Peter Xu > > Cc: Gerd Hoffmann > > Cc: Dongwon Kim > > Cc: Junxiao Chang > > Suggested-by: Jason Gunthorpe > > Reviewed-by: Jason Gunthorpe (v2) > > Reviewed-by: David Hildenbrand (v3) > > Signed-off-by: Vivek Kasireddy > > --- > > include/linux/memfd.h | 5 +++ > > include/linux/mm.h| 2 + > > mm/gup.c | 102 ++ > > mm/memfd.c| 34 ++ > > 4 files changed, 143 insertions(+) > > > > diff --git a/include/linux/memfd.h b/include/linux/memfd.h > > index e7abf6fa4c52..6fc0d1282151 100644 > > --- a/include/linux/memfd.h > > +++ b/include/linux/memfd.h > > @@ -6,11 +6,16 @@ > > > > #ifdef CONFIG_MEMFD_CREATE > > extern long memfd_fcntl(struct file *file, unsigned int cmd, unsigned int > arg); > > +extern struct page *memfd_alloc_page(struct file *memfd, pgoff_t idx); > > #else > > static inline long memfd_fcntl(struct file *f, unsigned int c, unsigned > > int a) > > { > > return -EINVAL; > > } > > +static inline struct page *memfd_alloc_page(struct file *memfd, pgoff_t > idx) > > +{ > > + return ERR_PTR(-EINVAL); > > +} > > #endif > > > > #endif /* __LINUX_MEMFD_H */ > > diff --git a/include/linux/mm.h b/include/linux/mm.h > > index 418d26608ece..ac69db45509f 100644 > > --- a/include/linux/mm.h > > +++ b/include/linux/mm.h > > @@ -2472,6 +2472,8 @@ 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, > > struct page **pages, unsigned int gup_flags); > > +long memfd_pin_user_pages(struct file *file, pgoff_t start, > > + unsigned long nr_pages, struct page **pages); > > > > int get_user_pages_fast(unsigned long start, int nr_pages, > > unsigned int gup_flags, struct page **pages); > > diff --git a/mm/gup.c b/mm/gup.c > > index 231711efa390..eb93d1ec9dc6 100644 > > --- a/mm/gup.c > > +++ b/mm/gup.c > > @@ -5,6 +5,7 @@ > > #include > > > > #include > > +#include > > #include > > #include > > #include > > @@ -17,6 +18,7 @@ > > #include > > #include > > #include > > +#include > > #include > > #include > > > > @@ -3410,3 +3412,103 @@ long pin_user_pages_unlocked(unsigned long > start, unsigned long nr_pages, > > &
RE: [PATCH v6 3/5] mm/gup: Introduce memfd_pin_user_pages() for pinning memfd pages (v6)
Hi, > > +struct page *memfd_alloc_page(struct file *memfd, pgoff_t idx) > > +{ > > +#ifdef CONFIG_HUGETLB_PAGE > > + struct folio *folio; > > + int err; > > + > > + if (is_file_hugepages(memfd)) { > > + folio = alloc_hugetlb_folio_nodemask(hstate_file(memfd), > > +NUMA_NO_NODE, > > +NULL, > > +GFP_USER); > > + if (folio && folio_try_get(folio)) { > > + err = hugetlb_add_to_page_cache(folio, > > If alloc_hugetlb_folio_nodemask moved out of the CONFIG_HUGETLB_PAGE > ifdef, the ifdef here could go away. Unlike alloc_hugetlb_folio_nodemask(), hugetlb_add_to_page_cache() does not get exposed without enabling CONFIG_HUGETLB_PAGE. > > Either way, this looks good: > > Reviewed-by: Christoph Hellwig Thank you for the review. Thanks, Vivek > >
Re: [PATCH v6 3/5] mm/gup: Introduce memfd_pin_user_pages() for pinning memfd pages (v6)
On 05.12.23 06:35, Vivek Kasireddy wrote: For drivers that would like to longterm-pin the pages associated with a memfd, the pin_user_pages_fd() API provides an option to not only pin the pages via FOLL_PIN but also to check and migrate them if they reside in movable zone or CMA block. This API currently works with memfds but it should work with any files that belong to either shmemfs or hugetlbfs. Files belonging to other filesystems are rejected for now. The pages need to be located first before pinning them via FOLL_PIN. If they are found in the page cache, they can be immediately pinned. Otherwise, they need to be allocated using the filesystem specific APIs and then pinned. v2: - Drop gup_flags and improve comments and commit message (David) - Allocate a page if we cannot find in page cache for the hugetlbfs case as well (David) - Don't unpin pages if there is a migration related failure (David) - Drop the unnecessary nr_pages <= 0 check (Jason) - Have the caller of the API pass in file * instead of fd (Jason) v3: (David) - Enclose the huge page allocation code with #ifdef CONFIG_HUGETLB_PAGE (Build error reported by kernel test robot ) - Don't forget memalloc_pin_restore() on non-migration related errors - Improve the readability of the cleanup code associated with non-migration related errors - Augment the comments by describing FOLL_LONGTERM like behavior - Include the R-b tag from Jason v4: - Remove the local variable "page" and instead use 3 return statements in alloc_file_page() (David) - Add the R-b tag from David v5: (David) - For hugetlb case, ensure that we only obtain head pages from the mapping by using __filemap_get_folio() instead of find_get_page_flags() - Handle -EEXIST when two or more potential users try to simultaneously add a huge page to the mapping by forcing them to retry on failure v6: (Christoph) - Rename this API to memfd_pin_user_pages() to make it clear that it is intended for memfds - Move the memfd page allocation helper from gup.c to memfd.c - Fix indentation errors in memfd_pin_user_pages() - For contiguous ranges of folios, use a helper such as filemap_get_folios_contig() to lookup the page cache in batches Cc: David Hildenbrand Cc: Christoph Hellwig Cc: Daniel Vetter Cc: Mike Kravetz Cc: Hugh Dickins Cc: Peter Xu Cc: Gerd Hoffmann Cc: Dongwon Kim Cc: Junxiao Chang Suggested-by: Jason Gunthorpe Reviewed-by: Jason Gunthorpe (v2) Reviewed-by: David Hildenbrand (v3) Signed-off-by: Vivek Kasireddy --- include/linux/memfd.h | 5 +++ include/linux/mm.h| 2 + mm/gup.c | 102 ++ mm/memfd.c| 34 ++ 4 files changed, 143 insertions(+) diff --git a/include/linux/memfd.h b/include/linux/memfd.h index e7abf6fa4c52..6fc0d1282151 100644 --- a/include/linux/memfd.h +++ b/include/linux/memfd.h @@ -6,11 +6,16 @@ #ifdef CONFIG_MEMFD_CREATE extern long memfd_fcntl(struct file *file, unsigned int cmd, unsigned int arg); +extern struct page *memfd_alloc_page(struct file *memfd, pgoff_t idx); #else static inline long memfd_fcntl(struct file *f, unsigned int c, unsigned int a) { return -EINVAL; } +static inline struct page *memfd_alloc_page(struct file *memfd, pgoff_t idx) +{ + return ERR_PTR(-EINVAL); +} #endif #endif /* __LINUX_MEMFD_H */ diff --git a/include/linux/mm.h b/include/linux/mm.h index 418d26608ece..ac69db45509f 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2472,6 +2472,8 @@ 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, struct page **pages, unsigned int gup_flags); +long memfd_pin_user_pages(struct file *file, pgoff_t start, + unsigned long nr_pages, struct page **pages); int get_user_pages_fast(unsigned long start, int nr_pages, unsigned int gup_flags, struct page **pages); diff --git a/mm/gup.c b/mm/gup.c index 231711efa390..eb93d1ec9dc6 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -5,6 +5,7 @@ #include #include +#include #include #include #include @@ -17,6 +18,7 @@ #include #include #include +#include #include #include @@ -3410,3 +3412,103 @@ long pin_user_pages_unlocked(unsigned long start, unsigned long nr_pages, &locked, gup_flags); } EXPORT_SYMBOL(pin_user_pages_unlocked); + +/** + * memfd_pin_user_pages() - pin user pages associated with a memfd + * @memfd: the memfd whose pages are to be pinned + * @start: starting memfd offset + * @nr_pages: number of pages from start to pin + * @pages: array that receives pointers to the pages pinned. + * Should be at-least nr_pages long. + * + * Attempt to pin pages associated with a memfd; given that