Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

authored by

Vivek Kasireddy and committed by
Andrew Morton
89c1905d 53ba78de

+192
+5
include/linux/memfd.h
··· 6 6 7 7 #ifdef CONFIG_MEMFD_CREATE 8 8 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); 9 10 #else 10 11 static inline long memfd_fcntl(struct file *f, unsigned int c, unsigned int a) 11 12 { 12 13 return -EINVAL; 14 + } 15 + static inline struct folio *memfd_alloc_folio(struct file *memfd, pgoff_t idx) 16 + { 17 + return ERR_PTR(-EINVAL); 13 18 } 14 19 #endif 15 20
+3
include/linux/mm.h
··· 2500 2500 struct page **pages, unsigned int gup_flags); 2501 2501 long pin_user_pages_unlocked(unsigned long start, unsigned long nr_pages, 2502 2502 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); 2503 2506 2504 2507 int get_user_pages_fast(unsigned long start, int nr_pages, 2505 2508 unsigned int gup_flags, struct page **pages);
+139
mm/gup.c
··· 5 5 #include <linux/spinlock.h> 6 6 7 7 #include <linux/mm.h> 8 + #include <linux/memfd.h> 8 9 #include <linux/memremap.h> 9 10 #include <linux/pagemap.h> 10 11 #include <linux/rmap.h> ··· 18 17 #include <linux/hugetlb.h> 19 18 #include <linux/migrate.h> 20 19 #include <linux/mm_inline.h> 20 + #include <linux/pagevec.h> 21 21 #include <linux/sched/mm.h> 22 22 #include <linux/shmem_fs.h> 23 23 ··· 3766 3764 &locked, gup_flags); 3767 3765 } 3768 3766 EXPORT_SYMBOL(pin_user_pages_unlocked); 3767 + 3768 + /** 3769 + * memfd_pin_folios() - pin folios associated with a memfd 3770 + * @memfd: the memfd whose folios are to be pinned 3771 + * @start: the first memfd offset 3772 + * @end: the last memfd offset (inclusive) 3773 + * @folios: array that receives pointers to the folios pinned 3774 + * @max_folios: maximum number of entries in @folios 3775 + * @offset: the offset into the first folio 3776 + * 3777 + * Attempt to pin folios associated with a memfd in the contiguous range 3778 + * [start, end]. Given that a memfd is either backed by shmem or hugetlb, 3779 + * the folios can either be found in the page cache or need to be allocated 3780 + * if necessary. Once the folios are located, they are all pinned via 3781 + * FOLL_PIN and @offset is populatedwith the offset into the first folio. 3782 + * And, eventually, these pinned folios must be released either using 3783 + * unpin_folios() or unpin_folio(). 3784 + * 3785 + * It must be noted that the folios may be pinned for an indefinite amount 3786 + * of time. And, in most cases, the duration of time they may stay pinned 3787 + * would be controlled by the userspace. This behavior is effectively the 3788 + * same as using FOLL_LONGTERM with other GUP APIs. 3789 + * 3790 + * Returns number of folios pinned, which could be less than @max_folios 3791 + * as it depends on the folio sizes that cover the range [start, end]. 3792 + * If no folios were pinned, it returns -errno. 3793 + */ 3794 + long memfd_pin_folios(struct file *memfd, loff_t start, loff_t end, 3795 + struct folio **folios, unsigned int max_folios, 3796 + pgoff_t *offset) 3797 + { 3798 + unsigned int flags, nr_folios, nr_found; 3799 + unsigned int i, pgshift = PAGE_SHIFT; 3800 + pgoff_t start_idx, end_idx, next_idx; 3801 + struct folio *folio = NULL; 3802 + struct folio_batch fbatch; 3803 + struct hstate *h; 3804 + long ret = -EINVAL; 3805 + 3806 + if (start < 0 || start > end || !max_folios) 3807 + return -EINVAL; 3808 + 3809 + if (!memfd) 3810 + return -EINVAL; 3811 + 3812 + if (!shmem_file(memfd) && !is_file_hugepages(memfd)) 3813 + return -EINVAL; 3814 + 3815 + if (end >= i_size_read(file_inode(memfd))) 3816 + return -EINVAL; 3817 + 3818 + if (is_file_hugepages(memfd)) { 3819 + h = hstate_file(memfd); 3820 + pgshift = huge_page_shift(h); 3821 + } 3822 + 3823 + flags = memalloc_pin_save(); 3824 + do { 3825 + nr_folios = 0; 3826 + start_idx = start >> pgshift; 3827 + end_idx = end >> pgshift; 3828 + if (is_file_hugepages(memfd)) { 3829 + start_idx <<= huge_page_order(h); 3830 + end_idx <<= huge_page_order(h); 3831 + } 3832 + 3833 + folio_batch_init(&fbatch); 3834 + while (start_idx <= end_idx && nr_folios < max_folios) { 3835 + /* 3836 + * In most cases, we should be able to find the folios 3837 + * in the page cache. If we cannot find them for some 3838 + * reason, we try to allocate them and add them to the 3839 + * page cache. 3840 + */ 3841 + nr_found = filemap_get_folios_contig(memfd->f_mapping, 3842 + &start_idx, 3843 + end_idx, 3844 + &fbatch); 3845 + if (folio) { 3846 + folio_put(folio); 3847 + folio = NULL; 3848 + } 3849 + 3850 + next_idx = 0; 3851 + for (i = 0; i < nr_found; i++) { 3852 + /* 3853 + * As there can be multiple entries for a 3854 + * given folio in the batch returned by 3855 + * filemap_get_folios_contig(), the below 3856 + * check is to ensure that we pin and return a 3857 + * unique set of folios between start and end. 3858 + */ 3859 + if (next_idx && 3860 + next_idx != folio_index(fbatch.folios[i])) 3861 + continue; 3862 + 3863 + folio = page_folio(&fbatch.folios[i]->page); 3864 + 3865 + if (try_grab_folio(folio, 1, FOLL_PIN)) { 3866 + folio_batch_release(&fbatch); 3867 + ret = -EINVAL; 3868 + goto err; 3869 + } 3870 + 3871 + if (nr_folios == 0) 3872 + *offset = offset_in_folio(folio, start); 3873 + 3874 + folios[nr_folios] = folio; 3875 + next_idx = folio_next_index(folio); 3876 + if (++nr_folios == max_folios) 3877 + break; 3878 + } 3879 + 3880 + folio = NULL; 3881 + folio_batch_release(&fbatch); 3882 + if (!nr_found) { 3883 + folio = memfd_alloc_folio(memfd, start_idx); 3884 + if (IS_ERR(folio)) { 3885 + ret = PTR_ERR(folio); 3886 + if (ret != -EEXIST) 3887 + goto err; 3888 + } 3889 + } 3890 + } 3891 + 3892 + ret = check_and_migrate_movable_folios(nr_folios, folios); 3893 + } while (ret == -EAGAIN); 3894 + 3895 + memalloc_pin_restore(flags); 3896 + return ret ? ret : nr_folios; 3897 + err: 3898 + memalloc_pin_restore(flags); 3899 + unpin_folios(folios, nr_folios); 3900 + 3901 + return ret; 3902 + } 3903 + EXPORT_SYMBOL_GPL(memfd_pin_folios);
+45
mm/memfd.c
··· 60 60 } 61 61 62 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 + 107 + /* 63 108 * Setting SEAL_WRITE requires us to verify there's no pending writer. However, 64 109 * via get_user_pages(), drivers might have some pending I/O without any active 65 110 * user-space mappings (eg., direct-IO, AIO). Therefore, we look at all folios