Skip to content

Commit 097e8db

Browse files
ljskernelakpm00
authored andcommitted
mm: update hugetlbfs to use VMA flags on mmap_prepare
In order to update all mmap_prepare users to utilising the new VMA flags type vma_flags_t and associated helper functions, we start by updating hugetlbfs which has a lot of additional logic that requires updating to make this change. This is laying the groundwork for eliminating the vm_flags_t from struct vm_area_desc and using vma_flags_t only, which further lays the ground for removing the deprecated vm_flags_t type altogether. No functional changes intended. Link: https://lkml.kernel.org/r/9226bec80c9aa3447cc2b83354f733841dba8a50.1769097829.git.lorenzo.stoakes@oracle.com Signed-off-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com> Reviewed-by: Liam R. Howlett <Liam.Howlett@oracle.com> Cc: Baolin Wang <baolin.wang@linux.alibaba.com> Cc: Barry Song <baohua@kernel.org> Cc: David Hildenbrand <david@kernel.org> Cc: Dev Jain <dev.jain@arm.com> Cc: Jason Gunthorpe <jgg@nvidia.com> Cc: Suren Baghdasaryan <surenb@google.com> Cc: Vlastimil Babka <vbabka@suse.cz> Cc: Zi Yan <ziy@nvidia.com> Cc: Damien Le Moal <dlemoal@kernel.org> Cc: "Darrick J. Wong" <djwong@kernel.org> Cc: Jarkko Sakkinen <jarkko@kernel.org> Cc: Yury Norov <ynorov@nvidia.com> Cc: Chris Mason <clm@fb.com> Cc: Pedro Falcato <pfalcato@suse.de> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent bae0ba7 commit 097e8db

7 files changed

Lines changed: 41 additions & 29 deletions

File tree

fs/hugetlbfs/inode.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ static int hugetlbfs_file_mmap_prepare(struct vm_area_desc *desc)
109109
loff_t len, vma_len;
110110
int ret;
111111
struct hstate *h = hstate_file(file);
112-
vm_flags_t vm_flags;
112+
vma_flags_t vma_flags;
113113

114114
/*
115115
* vma address alignment (but not the pgoff alignment) has
@@ -119,7 +119,7 @@ static int hugetlbfs_file_mmap_prepare(struct vm_area_desc *desc)
119119
* way when do_mmap unwinds (may be important on powerpc
120120
* and ia64).
121121
*/
122-
desc->vm_flags |= VM_HUGETLB | VM_DONTEXPAND;
122+
vma_desc_set_flags(desc, VMA_HUGETLB_BIT, VMA_DONTEXPAND_BIT);
123123
desc->vm_ops = &hugetlb_vm_ops;
124124

125125
/*
@@ -148,23 +148,23 @@ static int hugetlbfs_file_mmap_prepare(struct vm_area_desc *desc)
148148

149149
ret = -ENOMEM;
150150

151-
vm_flags = desc->vm_flags;
151+
vma_flags = desc->vma_flags;
152152
/*
153153
* for SHM_HUGETLB, the pages are reserved in the shmget() call so skip
154154
* reserving here. Note: only for SHM hugetlbfs file, the inode
155155
* flag S_PRIVATE is set.
156156
*/
157157
if (inode->i_flags & S_PRIVATE)
158-
vm_flags |= VM_NORESERVE;
158+
vma_flags_set(&vma_flags, VMA_NORESERVE_BIT);
159159

160160
if (hugetlb_reserve_pages(inode,
161161
desc->pgoff >> huge_page_order(h),
162162
len >> huge_page_shift(h), desc,
163-
vm_flags) < 0)
163+
vma_flags) < 0)
164164
goto out;
165165

166166
ret = 0;
167-
if ((desc->vm_flags & VM_WRITE) && inode->i_size < len)
167+
if (vma_desc_test_flags(desc, VMA_WRITE_BIT) && inode->i_size < len)
168168
i_size_write(inode, len);
169169
out:
170170
inode_unlock(inode);
@@ -1527,7 +1527,7 @@ static int get_hstate_idx(int page_size_log)
15271527
* otherwise hugetlb_reserve_pages reserves one less hugepages than intended.
15281528
*/
15291529
struct file *hugetlb_file_setup(const char *name, size_t size,
1530-
vm_flags_t acctflag, int creat_flags,
1530+
vma_flags_t acctflag, int creat_flags,
15311531
int page_size_log)
15321532
{
15331533
struct inode *inode;

include/linux/hugetlb.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ int hugetlb_mfill_atomic_pte(pte_t *dst_pte,
150150
struct folio **foliop);
151151
#endif /* CONFIG_USERFAULTFD */
152152
long hugetlb_reserve_pages(struct inode *inode, long from, long to,
153-
struct vm_area_desc *desc, vm_flags_t vm_flags);
153+
struct vm_area_desc *desc, vma_flags_t vma_flags);
154154
long hugetlb_unreserve_pages(struct inode *inode, long start, long end,
155155
long freed);
156156
bool folio_isolate_hugetlb(struct folio *folio, struct list_head *list);
@@ -529,7 +529,7 @@ static inline struct hugetlbfs_inode_info *HUGETLBFS_I(struct inode *inode)
529529
}
530530

531531
extern const struct vm_operations_struct hugetlb_vm_ops;
532-
struct file *hugetlb_file_setup(const char *name, size_t size, vm_flags_t acct,
532+
struct file *hugetlb_file_setup(const char *name, size_t size, vma_flags_t acct,
533533
int creat_flags, int page_size_log);
534534

535535
static inline bool is_file_hugepages(const struct file *file)
@@ -545,7 +545,7 @@ static inline struct hstate *hstate_inode(struct inode *i)
545545

546546
#define is_file_hugepages(file) false
547547
static inline struct file *
548-
hugetlb_file_setup(const char *name, size_t size, vm_flags_t acctflag,
548+
hugetlb_file_setup(const char *name, size_t size, vma_flags_t acctflag,
549549
int creat_flags, int page_size_log)
550550
{
551551
return ERR_PTR(-ENOSYS);

include/linux/hugetlb_inline.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,23 @@ static inline bool is_vm_hugetlb_flags(vm_flags_t vm_flags)
1111
return !!(vm_flags & VM_HUGETLB);
1212
}
1313

14+
static inline bool is_vma_hugetlb_flags(const vma_flags_t *flags)
15+
{
16+
return vma_flags_test(flags, VMA_HUGETLB_BIT);
17+
}
18+
1419
#else
1520

1621
static inline bool is_vm_hugetlb_flags(vm_flags_t vm_flags)
1722
{
1823
return false;
1924
}
2025

26+
static inline bool is_vma_hugetlb_flags(const vma_flags_t *flags)
27+
{
28+
return false;
29+
}
30+
2131
#endif
2232

2333
static inline bool is_vm_hugetlb_page(struct vm_area_struct *vma)

ipc/shm.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -707,9 +707,9 @@ static int newseg(struct ipc_namespace *ns, struct ipc_params *params)
707707
int error;
708708
struct shmid_kernel *shp;
709709
size_t numpages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
710+
const bool has_no_reserve = shmflg & SHM_NORESERVE;
710711
struct file *file;
711712
char name[13];
712-
vm_flags_t acctflag = 0;
713713

714714
if (size < SHMMIN || size > ns->shm_ctlmax)
715715
return -EINVAL;
@@ -738,6 +738,7 @@ static int newseg(struct ipc_namespace *ns, struct ipc_params *params)
738738

739739
sprintf(name, "SYSV%08x", key);
740740
if (shmflg & SHM_HUGETLB) {
741+
vma_flags_t acctflag = EMPTY_VMA_FLAGS;
741742
struct hstate *hs;
742743
size_t hugesize;
743744

@@ -749,17 +750,18 @@ static int newseg(struct ipc_namespace *ns, struct ipc_params *params)
749750
hugesize = ALIGN(size, huge_page_size(hs));
750751

751752
/* hugetlb_file_setup applies strict accounting */
752-
if (shmflg & SHM_NORESERVE)
753-
acctflag = VM_NORESERVE;
753+
if (has_no_reserve)
754+
vma_flags_set(&acctflag, VMA_NORESERVE_BIT);
754755
file = hugetlb_file_setup(name, hugesize, acctflag,
755756
HUGETLB_SHMFS_INODE, (shmflg >> SHM_HUGE_SHIFT) & SHM_HUGE_MASK);
756757
} else {
758+
vm_flags_t acctflag = 0;
759+
757760
/*
758761
* Do not allow no accounting for OVERCOMMIT_NEVER, even
759762
* if it's asked for.
760763
*/
761-
if ((shmflg & SHM_NORESERVE) &&
762-
sysctl_overcommit_memory != OVERCOMMIT_NEVER)
764+
if (has_no_reserve && sysctl_overcommit_memory != OVERCOMMIT_NEVER)
763765
acctflag = VM_NORESERVE;
764766
file = shmem_kernel_file_setup(name, size, acctflag);
765767
}

mm/hugetlb.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,16 +1193,16 @@ static void set_vma_resv_flags(struct vm_area_struct *vma, unsigned long flags)
11931193

11941194
static void set_vma_desc_resv_map(struct vm_area_desc *desc, struct resv_map *map)
11951195
{
1196-
VM_WARN_ON_ONCE(!is_vm_hugetlb_flags(desc->vm_flags));
1197-
VM_WARN_ON_ONCE(desc->vm_flags & VM_MAYSHARE);
1196+
VM_WARN_ON_ONCE(!is_vma_hugetlb_flags(&desc->vma_flags));
1197+
VM_WARN_ON_ONCE(vma_desc_test_flags(desc, VMA_MAYSHARE_BIT));
11981198

11991199
desc->private_data = map;
12001200
}
12011201

12021202
static void set_vma_desc_resv_flags(struct vm_area_desc *desc, unsigned long flags)
12031203
{
1204-
VM_WARN_ON_ONCE(!is_vm_hugetlb_flags(desc->vm_flags));
1205-
VM_WARN_ON_ONCE(desc->vm_flags & VM_MAYSHARE);
1204+
VM_WARN_ON_ONCE(!is_vma_hugetlb_flags(&desc->vma_flags));
1205+
VM_WARN_ON_ONCE(vma_desc_test_flags(desc, VMA_MAYSHARE_BIT));
12061206

12071207
desc->private_data = (void *)((unsigned long)desc->private_data | flags);
12081208
}
@@ -1216,7 +1216,7 @@ static int is_vma_resv_set(struct vm_area_struct *vma, unsigned long flag)
12161216

12171217
static bool is_vma_desc_resv_set(struct vm_area_desc *desc, unsigned long flag)
12181218
{
1219-
VM_WARN_ON_ONCE(!is_vm_hugetlb_flags(desc->vm_flags));
1219+
VM_WARN_ON_ONCE(!is_vma_hugetlb_flags(&desc->vma_flags));
12201220

12211221
return ((unsigned long)desc->private_data) & flag;
12221222
}
@@ -6571,7 +6571,7 @@ long hugetlb_change_protection(struct vm_area_struct *vma,
65716571
long hugetlb_reserve_pages(struct inode *inode,
65726572
long from, long to,
65736573
struct vm_area_desc *desc,
6574-
vm_flags_t vm_flags)
6574+
vma_flags_t vma_flags)
65756575
{
65766576
long chg = -1, add = -1, spool_resv, gbl_resv;
65776577
struct hstate *h = hstate_inode(inode);
@@ -6592,7 +6592,7 @@ long hugetlb_reserve_pages(struct inode *inode,
65926592
* attempt will be made for VM_NORESERVE to allocate a page
65936593
* without using reserves
65946594
*/
6595-
if (vm_flags & VM_NORESERVE)
6595+
if (vma_flags_test(&vma_flags, VMA_NORESERVE_BIT))
65966596
return 0;
65976597

65986598
/*
@@ -6601,7 +6601,7 @@ long hugetlb_reserve_pages(struct inode *inode,
66016601
* to reserve the full area even if read-only as mprotect() may be
66026602
* called to make the mapping read-write. Assume !desc is a shm mapping
66036603
*/
6604-
if (!desc || desc->vm_flags & VM_MAYSHARE) {
6604+
if (!desc || vma_desc_test_flags(desc, VMA_MAYSHARE_BIT)) {
66056605
/*
66066606
* resv_map can not be NULL as hugetlb_reserve_pages is only
66076607
* called for inodes for which resv_maps were created (see
@@ -6635,7 +6635,7 @@ long hugetlb_reserve_pages(struct inode *inode,
66356635
if (err < 0)
66366636
goto out_err;
66376637

6638-
if (desc && !(desc->vm_flags & VM_MAYSHARE) && h_cg) {
6638+
if (desc && !vma_desc_test_flags(desc, VMA_MAYSHARE_BIT) && h_cg) {
66396639
/* For private mappings, the hugetlb_cgroup uncharge info hangs
66406640
* of the resv_map.
66416641
*/
@@ -6672,7 +6672,7 @@ long hugetlb_reserve_pages(struct inode *inode,
66726672
* consumed reservations are stored in the map. Hence, nothing
66736673
* else has to be done for private mappings here
66746674
*/
6675-
if (!desc || desc->vm_flags & VM_MAYSHARE) {
6675+
if (!desc || vma_desc_test_flags(desc, VMA_MAYSHARE_BIT)) {
66766676
add = region_add(resv_map, from, to, regions_needed, h, h_cg);
66776677

66786678
if (unlikely(add < 0)) {
@@ -6727,7 +6727,7 @@ long hugetlb_reserve_pages(struct inode *inode,
67276727
hugetlb_cgroup_uncharge_cgroup_rsvd(hstate_index(h),
67286728
chg * pages_per_huge_page(h), h_cg);
67296729
out_err:
6730-
if (!desc || desc->vm_flags & VM_MAYSHARE)
6730+
if (!desc || vma_desc_test_flags(desc, VMA_MAYSHARE_BIT))
67316731
/* Only call region_abort if the region_chg succeeded but the
67326732
* region_add failed or didn't run.
67336733
*/

mm/memfd.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ struct folio *memfd_alloc_folio(struct file *memfd, pgoff_t idx)
8686
gfp_mask &= ~(__GFP_HIGHMEM | __GFP_MOVABLE);
8787
idx >>= huge_page_order(h);
8888

89-
nr_resv = hugetlb_reserve_pages(inode, idx, idx + 1, NULL, 0);
89+
nr_resv = hugetlb_reserve_pages(inode, idx, idx + 1, NULL, EMPTY_VMA_FLAGS);
9090
if (nr_resv < 0)
9191
return ERR_PTR(nr_resv);
9292

@@ -463,7 +463,7 @@ struct file *memfd_alloc_file(const char *name, unsigned int flags)
463463
int err = 0;
464464

465465
if (flags & MFD_HUGETLB) {
466-
file = hugetlb_file_setup(name, 0, VM_NORESERVE,
466+
file = hugetlb_file_setup(name, 0, mk_vma_flags(VMA_NORESERVE_BIT),
467467
HUGETLB_ANONHUGE_INODE,
468468
(flags >> MFD_HUGE_SHIFT) &
469469
MFD_HUGE_MASK);

mm/mmap.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ unsigned long ksys_mmap_pgoff(unsigned long addr, unsigned long len,
594594
* taken when vm_ops->mmap() is called
595595
*/
596596
file = hugetlb_file_setup(HUGETLB_ANON_FILE, len,
597-
VM_NORESERVE,
597+
mk_vma_flags(VMA_NORESERVE_BIT),
598598
HUGETLB_ANONHUGE_INODE,
599599
(flags >> MAP_HUGE_SHIFT) & MAP_HUGE_MASK);
600600
if (IS_ERR(file))

0 commit comments

Comments
 (0)