Skip to content

Commit 89c1905

Browse files
vivekkreddyakpm00
authored andcommitted
mm/gup: introduce memfd_pin_folios() for pinning memfd folios
For drivers that would like to longterm-pin the folios associated with a memfd, the memfd_pin_folios() API provides an option to not only pin the folios 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 folios 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. [akpm@linux-foundation.org: improve the CONFIG_MMU=n situation, per SeongJae] [vivek.kasireddy@intel.com: return -EINVAL if the end offset is greater than the size of memfd] Link: https://lkml.kernel.org/r/IA0PR11MB71850525CBC7D541CAB45DF1F8DB2@IA0PR11MB7185.namprd11.prod.outlook.com Link: https://lkml.kernel.org/r/20240624063952.1572359-4-vivek.kasireddy@intel.com Signed-off-by: Vivek Kasireddy <vivek.kasireddy@intel.com> Suggested-by: Jason Gunthorpe <jgg@nvidia.com> Reviewed-by: Jason Gunthorpe <jgg@nvidia.com> (v2) Reviewed-by: David Hildenbrand <david@redhat.com> (v3) Reviewed-by: Christoph Hellwig <hch@lst.de> (v6) Acked-by: Dave Airlie <airlied@redhat.com> Acked-by: Gerd Hoffmann <kraxel@redhat.com> Cc: Matthew Wilcox (Oracle) <willy@infradead.org> Cc: Daniel Vetter <daniel.vetter@ffwll.ch> Cc: Hugh Dickins <hughd@google.com> Cc: Peter Xu <peterx@redhat.com> Cc: Dongwon Kim <dongwon.kim@intel.com> Cc: Junxiao Chang <junxiao.chang@intel.com> Cc: Arnd Bergmann <arnd@arndb.de> Cc: Christoph Hellwig <hch@infradead.org> Cc: Mike Kravetz <mike.kravetz@oracle.com> Cc: Oscar Salvador <osalvador@suse.de> Cc: Shuah Khan <shuah@kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent 53ba78d commit 89c1905

File tree

4 files changed

+192
-0
lines changed

4 files changed

+192
-0
lines changed

include/linux/memfd.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,16 @@
66

77
#ifdef CONFIG_MEMFD_CREATE
88
extern long memfd_fcntl(struct file *file, unsigned int cmd, unsigned int arg);
9+
struct folio *memfd_alloc_folio(struct file *memfd, pgoff_t idx);
910
#else
1011
static inline long memfd_fcntl(struct file *f, unsigned int c, unsigned int a)
1112
{
1213
return -EINVAL;
1314
}
15+
static inline struct folio *memfd_alloc_folio(struct file *memfd, pgoff_t idx)
16+
{
17+
return ERR_PTR(-EINVAL);
18+
}
1419
#endif
1520

1621
#endif /* __LINUX_MEMFD_H */

include/linux/mm.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2500,6 +2500,9 @@ long get_user_pages_unlocked(unsigned long start, unsigned long nr_pages,
25002500
struct page **pages, unsigned int gup_flags);
25012501
long pin_user_pages_unlocked(unsigned long start, unsigned long nr_pages,
25022502
struct page **pages, unsigned int gup_flags);
2503+
long memfd_pin_folios(struct file *memfd, loff_t start, loff_t end,
2504+
struct folio **folios, unsigned int max_folios,
2505+
pgoff_t *offset);
25032506

25042507
int get_user_pages_fast(unsigned long start, int nr_pages,
25052508
unsigned int gup_flags, struct page **pages);

mm/gup.c

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <linux/spinlock.h>
66

77
#include <linux/mm.h>
8+
#include <linux/memfd.h>
89
#include <linux/memremap.h>
910
#include <linux/pagemap.h>
1011
#include <linux/rmap.h>
@@ -17,6 +18,7 @@
1718
#include <linux/hugetlb.h>
1819
#include <linux/migrate.h>
1920
#include <linux/mm_inline.h>
21+
#include <linux/pagevec.h>
2022
#include <linux/sched/mm.h>
2123
#include <linux/shmem_fs.h>
2224

@@ -3764,3 +3766,140 @@ long pin_user_pages_unlocked(unsigned long start, unsigned long nr_pages,
37643766
&locked, gup_flags);
37653767
}
37663768
EXPORT_SYMBOL(pin_user_pages_unlocked);
3769+
3770+
/**
3771+
* memfd_pin_folios() - pin folios associated with a memfd
3772+
* @memfd: the memfd whose folios are to be pinned
3773+
* @start: the first memfd offset
3774+
* @end: the last memfd offset (inclusive)
3775+
* @folios: array that receives pointers to the folios pinned
3776+
* @max_folios: maximum number of entries in @folios
3777+
* @offset: the offset into the first folio
3778+
*
3779+
* Attempt to pin folios associated with a memfd in the contiguous range
3780+
* [start, end]. Given that a memfd is either backed by shmem or hugetlb,
3781+
* the folios can either be found in the page cache or need to be allocated
3782+
* if necessary. Once the folios are located, they are all pinned via
3783+
* FOLL_PIN and @offset is populatedwith the offset into the first folio.
3784+
* And, eventually, these pinned folios must be released either using
3785+
* unpin_folios() or unpin_folio().
3786+
*
3787+
* It must be noted that the folios may be pinned for an indefinite amount
3788+
* of time. And, in most cases, the duration of time they may stay pinned
3789+
* would be controlled by the userspace. This behavior is effectively the
3790+
* same as using FOLL_LONGTERM with other GUP APIs.
3791+
*
3792+
* Returns number of folios pinned, which could be less than @max_folios
3793+
* as it depends on the folio sizes that cover the range [start, end].
3794+
* If no folios were pinned, it returns -errno.
3795+
*/
3796+
long memfd_pin_folios(struct file *memfd, loff_t start, loff_t end,
3797+
struct folio **folios, unsigned int max_folios,
3798+
pgoff_t *offset)
3799+
{
3800+
unsigned int flags, nr_folios, nr_found;
3801+
unsigned int i, pgshift = PAGE_SHIFT;
3802+
pgoff_t start_idx, end_idx, next_idx;
3803+
struct folio *folio = NULL;
3804+
struct folio_batch fbatch;
3805+
struct hstate *h;
3806+
long ret = -EINVAL;
3807+
3808+
if (start < 0 || start > end || !max_folios)
3809+
return -EINVAL;
3810+
3811+
if (!memfd)
3812+
return -EINVAL;
3813+
3814+
if (!shmem_file(memfd) && !is_file_hugepages(memfd))
3815+
return -EINVAL;
3816+
3817+
if (end >= i_size_read(file_inode(memfd)))
3818+
return -EINVAL;
3819+
3820+
if (is_file_hugepages(memfd)) {
3821+
h = hstate_file(memfd);
3822+
pgshift = huge_page_shift(h);
3823+
}
3824+
3825+
flags = memalloc_pin_save();
3826+
do {
3827+
nr_folios = 0;
3828+
start_idx = start >> pgshift;
3829+
end_idx = end >> pgshift;
3830+
if (is_file_hugepages(memfd)) {
3831+
start_idx <<= huge_page_order(h);
3832+
end_idx <<= huge_page_order(h);
3833+
}
3834+
3835+
folio_batch_init(&fbatch);
3836+
while (start_idx <= end_idx && nr_folios < max_folios) {
3837+
/*
3838+
* In most cases, we should be able to find the folios
3839+
* in the page cache. If we cannot find them for some
3840+
* reason, we try to allocate them and add them to the
3841+
* page cache.
3842+
*/
3843+
nr_found = filemap_get_folios_contig(memfd->f_mapping,
3844+
&start_idx,
3845+
end_idx,
3846+
&fbatch);
3847+
if (folio) {
3848+
folio_put(folio);
3849+
folio = NULL;
3850+
}
3851+
3852+
next_idx = 0;
3853+
for (i = 0; i < nr_found; i++) {
3854+
/*
3855+
* As there can be multiple entries for a
3856+
* given folio in the batch returned by
3857+
* filemap_get_folios_contig(), the below
3858+
* check is to ensure that we pin and return a
3859+
* unique set of folios between start and end.
3860+
*/
3861+
if (next_idx &&
3862+
next_idx != folio_index(fbatch.folios[i]))
3863+
continue;
3864+
3865+
folio = page_folio(&fbatch.folios[i]->page);
3866+
3867+
if (try_grab_folio(folio, 1, FOLL_PIN)) {
3868+
folio_batch_release(&fbatch);
3869+
ret = -EINVAL;
3870+
goto err;
3871+
}
3872+
3873+
if (nr_folios == 0)
3874+
*offset = offset_in_folio(folio, start);
3875+
3876+
folios[nr_folios] = folio;
3877+
next_idx = folio_next_index(folio);
3878+
if (++nr_folios == max_folios)
3879+
break;
3880+
}
3881+
3882+
folio = NULL;
3883+
folio_batch_release(&fbatch);
3884+
if (!nr_found) {
3885+
folio = memfd_alloc_folio(memfd, start_idx);
3886+
if (IS_ERR(folio)) {
3887+
ret = PTR_ERR(folio);
3888+
if (ret != -EEXIST)
3889+
goto err;
3890+
}
3891+
}
3892+
}
3893+
3894+
ret = check_and_migrate_movable_folios(nr_folios, folios);
3895+
} while (ret == -EAGAIN);
3896+
3897+
memalloc_pin_restore(flags);
3898+
return ret ? ret : nr_folios;
3899+
err:
3900+
memalloc_pin_restore(flags);
3901+
unpin_folios(folios, nr_folios);
3902+
3903+
return ret;
3904+
}
3905+
EXPORT_SYMBOL_GPL(memfd_pin_folios);

mm/memfd.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,51 @@ static void memfd_tag_pins(struct xa_state *xas)
5959
xas_unlock_irq(xas);
6060
}
6161

62+
/*
63+
* This is a helper function used by memfd_pin_user_pages() in GUP (gup.c).
64+
* It is mainly called to allocate a folio in a memfd when the caller
65+
* (memfd_pin_folios()) cannot find a folio in the page cache at a given
66+
* index in the mapping.
67+
*/
68+
struct folio *memfd_alloc_folio(struct file *memfd, pgoff_t idx)
69+
{
70+
#ifdef CONFIG_HUGETLB_PAGE
71+
struct folio *folio;
72+
gfp_t gfp_mask;
73+
int err;
74+
75+
if (is_file_hugepages(memfd)) {
76+
/*
77+
* The folio would most likely be accessed by a DMA driver,
78+
* therefore, we have zone memory constraints where we can
79+
* alloc from. Also, the folio will be pinned for an indefinite
80+
* amount of time, so it is not expected to be migrated away.
81+
*/
82+
gfp_mask = htlb_alloc_mask(hstate_file(memfd));
83+
gfp_mask &= ~(__GFP_HIGHMEM | __GFP_MOVABLE);
84+
85+
folio = alloc_hugetlb_folio_nodemask(hstate_file(memfd),
86+
numa_node_id(),
87+
NULL,
88+
gfp_mask,
89+
false);
90+
if (folio && folio_try_get(folio)) {
91+
err = hugetlb_add_to_page_cache(folio,
92+
memfd->f_mapping,
93+
idx);
94+
if (err) {
95+
folio_put(folio);
96+
free_huge_folio(folio);
97+
return ERR_PTR(err);
98+
}
99+
return folio;
100+
}
101+
return ERR_PTR(-ENOMEM);
102+
}
103+
#endif
104+
return shmem_read_folio(memfd->f_mapping, idx);
105+
}
106+
62107
/*
63108
* Setting SEAL_WRITE requires us to verify there's no pending writer. However,
64109
* via get_user_pages(), drivers might have some pending I/O without any active

0 commit comments

Comments
 (0)