Skip to content

Commit 42d7395

Browse files
Andi Kleentorvalds
authored andcommitted
mm: support more pagesizes for MAP_HUGETLB/SHM_HUGETLB
There was some desire in large applications using MAP_HUGETLB or SHM_HUGETLB to use 1GB huge pages on some mappings, and stay with 2MB on others. This is useful together with NUMA policy: use 2MB interleaving on some mappings, but 1GB on local mappings. This patch extends the IPC/SHM syscall interfaces slightly to allow specifying the page size. It borrows some upper bits in the existing flag arguments and allows encoding the log of the desired page size in addition to the *_HUGETLB flag. When 0 is specified the default size is used, this makes the change fully compatible. Extending the internal hugetlb code to handle this is straight forward. Instead of a single mount it just keeps an array of them and selects the right mount based on the specified page size. When no page size is specified it uses the mount of the default page size. The change is not visible in /proc/mounts because internal mounts don't appear there. It also has very little overhead: the additional mounts just consume a super block, but not more memory when not used. I also exported the new flags to the user headers (they were previously under __KERNEL__). Right now only symbols for x86 and some other architecture for 1GB and 2MB are defined. The interface should already work for all other architectures though. Only architectures that define multiple hugetlb sizes actually need it (that is currently x86, tile, powerpc). However tile and powerpc have user configurable hugetlb sizes, so it's not easy to add defines. A program on those architectures would need to query sysfs and use the appropiate log2. [akpm@linux-foundation.org: cleanups] [rientjes@google.com: fix build] [akpm@linux-foundation.org: checkpatch fixes] Signed-off-by: Andi Kleen <ak@linux.intel.com> Cc: Michael Kerrisk <mtk.manpages@gmail.com> Acked-by: Rik van Riel <riel@redhat.com> Acked-by: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com> Cc: Hillf Danton <dhillf@gmail.com> Signed-off-by: David Rientjes <rientjes@google.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent ff604cf commit 42d7395

12 files changed

Lines changed: 135 additions & 18 deletions

File tree

‎arch/alpha/include/asm/mman.h‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,15 @@
6363
/* compatibility flags */
6464
#define MAP_FILE 0
6565

66+
/*
67+
* When MAP_HUGETLB is set bits [26:31] encode the log2 of the huge page size.
68+
* This gives us 6 bits, which is enough until someone invents 128 bit address
69+
* spaces.
70+
*
71+
* Assume these are all power of twos.
72+
* When 0 use the default page size.
73+
*/
74+
#define MAP_HUGE_SHIFT 26
75+
#define MAP_HUGE_MASK 0x3f
76+
6677
#endif /* __ALPHA_MMAN_H__ */

‎arch/mips/include/uapi/asm/mman.h‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,15 @@
8787
/* compatibility flags */
8888
#define MAP_FILE 0
8989

90+
/*
91+
* When MAP_HUGETLB is set bits [26:31] encode the log2 of the huge page size.
92+
* This gives us 6 bits, which is enough until someone invents 128 bit address
93+
* spaces.
94+
*
95+
* Assume these are all power of twos.
96+
* When 0 use the default page size.
97+
*/
98+
#define MAP_HUGE_SHIFT 26
99+
#define MAP_HUGE_MASK 0x3f
100+
90101
#endif /* _ASM_MMAN_H */

‎arch/parisc/include/uapi/asm/mman.h‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,15 @@
7070
#define MAP_FILE 0
7171
#define MAP_VARIABLE 0
7272

73+
/*
74+
* When MAP_HUGETLB is set bits [26:31] encode the log2 of the huge page size.
75+
* This gives us 6 bits, which is enough until someone invents 128 bit address
76+
* spaces.
77+
*
78+
* Assume these are all power of twos.
79+
* When 0 use the default page size.
80+
*/
81+
#define MAP_HUGE_SHIFT 26
82+
#define MAP_HUGE_MASK 0x3f
83+
7384
#endif /* __PARISC_MMAN_H__ */

‎arch/x86/include/asm/mman.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
#define MAP_32BIT 0x40 /* only give out 32bit addresses */
55

6+
#define MAP_HUGE_2MB (21 << MAP_HUGE_SHIFT)
7+
#define MAP_HUGE_1GB (30 << MAP_HUGE_SHIFT)
8+
69
#include <asm-generic/mman.h>
710

811
#endif /* _ASM_X86_MMAN_H */

‎arch/xtensa/include/uapi/asm/mman.h‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,15 @@
9393
/* compatibility flags */
9494
#define MAP_FILE 0
9595

96+
/*
97+
* When MAP_HUGETLB is set bits [26:31] encode the log2 of the huge page size.
98+
* This gives us 6 bits, which is enough until someone invents 128 bit address
99+
* spaces.
100+
*
101+
* Assume these are all power of twos.
102+
* When 0 use the default page size.
103+
*/
104+
#define MAP_HUGE_SHIFT 26
105+
#define MAP_HUGE_MASK 0x3f
106+
96107
#endif /* _XTENSA_MMAN_H */

‎fs/hugetlbfs/inode.c‎

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -923,7 +923,7 @@ static struct file_system_type hugetlbfs_fs_type = {
923923
.kill_sb = kill_litter_super,
924924
};
925925

926-
static struct vfsmount *hugetlbfs_vfsmount;
926+
static struct vfsmount *hugetlbfs_vfsmount[HUGE_MAX_HSTATE];
927927

928928
static int can_do_hugetlb_shm(void)
929929
{
@@ -932,9 +932,22 @@ static int can_do_hugetlb_shm(void)
932932
return capable(CAP_IPC_LOCK) || in_group_p(shm_group);
933933
}
934934

935+
static int get_hstate_idx(int page_size_log)
936+
{
937+
struct hstate *h;
938+
939+
if (!page_size_log)
940+
return default_hstate_idx;
941+
h = size_to_hstate(1 << page_size_log);
942+
if (!h)
943+
return -1;
944+
return h - hstates;
945+
}
946+
935947
struct file *hugetlb_file_setup(const char *name, unsigned long addr,
936948
size_t size, vm_flags_t acctflag,
937-
struct user_struct **user, int creat_flags)
949+
struct user_struct **user,
950+
int creat_flags, int page_size_log)
938951
{
939952
int error = -ENOMEM;
940953
struct file *file;
@@ -944,9 +957,14 @@ struct file *hugetlb_file_setup(const char *name, unsigned long addr,
944957
struct qstr quick_string;
945958
struct hstate *hstate;
946959
unsigned long num_pages;
960+
int hstate_idx;
961+
962+
hstate_idx = get_hstate_idx(page_size_log);
963+
if (hstate_idx < 0)
964+
return ERR_PTR(-ENODEV);
947965

948966
*user = NULL;
949-
if (!hugetlbfs_vfsmount)
967+
if (!hugetlbfs_vfsmount[hstate_idx])
950968
return ERR_PTR(-ENOENT);
951969

952970
if (creat_flags == HUGETLB_SHMFS_INODE && !can_do_hugetlb_shm()) {
@@ -963,15 +981,15 @@ struct file *hugetlb_file_setup(const char *name, unsigned long addr,
963981
}
964982
}
965983

966-
root = hugetlbfs_vfsmount->mnt_root;
984+
root = hugetlbfs_vfsmount[hstate_idx]->mnt_root;
967985
quick_string.name = name;
968986
quick_string.len = strlen(quick_string.name);
969987
quick_string.hash = 0;
970988
path.dentry = d_alloc(root, &quick_string);
971989
if (!path.dentry)
972990
goto out_shm_unlock;
973991

974-
path.mnt = mntget(hugetlbfs_vfsmount);
992+
path.mnt = mntget(hugetlbfs_vfsmount[hstate_idx]);
975993
error = -ENOSPC;
976994
inode = hugetlbfs_get_inode(root->d_sb, NULL, S_IFREG | S_IRWXUGO, 0);
977995
if (!inode)
@@ -1011,8 +1029,9 @@ struct file *hugetlb_file_setup(const char *name, unsigned long addr,
10111029

10121030
static int __init init_hugetlbfs_fs(void)
10131031
{
1032+
struct hstate *h;
10141033
int error;
1015-
struct vfsmount *vfsmount;
1034+
int i;
10161035

10171036
error = bdi_init(&hugetlbfs_backing_dev_info);
10181037
if (error)
@@ -1029,14 +1048,26 @@ static int __init init_hugetlbfs_fs(void)
10291048
if (error)
10301049
goto out;
10311050

1032-
vfsmount = kern_mount(&hugetlbfs_fs_type);
1051+
i = 0;
1052+
for_each_hstate(h) {
1053+
char buf[50];
1054+
unsigned ps_kb = 1U << (h->order + PAGE_SHIFT - 10);
10331055

1034-
if (!IS_ERR(vfsmount)) {
1035-
hugetlbfs_vfsmount = vfsmount;
1036-
return 0;
1037-
}
1056+
snprintf(buf, sizeof(buf), "pagesize=%uK", ps_kb);
1057+
hugetlbfs_vfsmount[i] = kern_mount_data(&hugetlbfs_fs_type,
1058+
buf);
10381059

1039-
error = PTR_ERR(vfsmount);
1060+
if (IS_ERR(hugetlbfs_vfsmount[i])) {
1061+
pr_err("hugetlb: Cannot mount internal hugetlbfs for "
1062+
"page size %uK", ps_kb);
1063+
error = PTR_ERR(hugetlbfs_vfsmount[i]);
1064+
hugetlbfs_vfsmount[i] = NULL;
1065+
}
1066+
i++;
1067+
}
1068+
/* Non default hstates are optional */
1069+
if (!IS_ERR_OR_NULL(hugetlbfs_vfsmount[default_hstate_idx]))
1070+
return 0;
10401071

10411072
out:
10421073
kmem_cache_destroy(hugetlbfs_inode_cachep);
@@ -1047,13 +1078,19 @@ static int __init init_hugetlbfs_fs(void)
10471078

10481079
static void __exit exit_hugetlbfs_fs(void)
10491080
{
1081+
struct hstate *h;
1082+
int i;
1083+
1084+
10501085
/*
10511086
* Make sure all delayed rcu free inodes are flushed before we
10521087
* destroy cache.
10531088
*/
10541089
rcu_barrier();
10551090
kmem_cache_destroy(hugetlbfs_inode_cachep);
1056-
kern_unmount(hugetlbfs_vfsmount);
1091+
i = 0;
1092+
for_each_hstate(h)
1093+
kern_unmount(hugetlbfs_vfsmount[i++]);
10571094
unregister_filesystem(&hugetlbfs_fs_type);
10581095
bdi_destroy(&hugetlbfs_backing_dev_info);
10591096
}

‎include/linux/hugetlb.h‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,8 @@ extern const struct file_operations hugetlbfs_file_operations;
183183
extern const struct vm_operations_struct hugetlb_vm_ops;
184184
struct file *hugetlb_file_setup(const char *name, unsigned long addr,
185185
size_t size, vm_flags_t acct,
186-
struct user_struct **user, int creat_flags);
186+
struct user_struct **user, int creat_flags,
187+
int page_size_log);
187188

188189
static inline int is_file_hugepages(struct file *file)
189190
{
@@ -195,12 +196,14 @@ static inline int is_file_hugepages(struct file *file)
195196
return 0;
196197
}
197198

199+
198200
#else /* !CONFIG_HUGETLBFS */
199201

200202
#define is_file_hugepages(file) 0
201203
static inline struct file *
202204
hugetlb_file_setup(const char *name, unsigned long addr, size_t size,
203-
vm_flags_t acctflag, struct user_struct **user, int creat_flags)
205+
vm_flags_t acctflag, struct user_struct **user, int creat_flags,
206+
int page_size_log)
204207
{
205208
return ERR_PTR(-ENOSYS);
206209
}

‎include/linux/shm.h‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,21 @@ struct shmid_kernel /* private to the kernel */
2929
#define SHM_HUGETLB 04000 /* segment will use huge TLB pages */
3030
#define SHM_NORESERVE 010000 /* don't check for reservations */
3131

32+
/* Bits [26:31] are reserved */
33+
34+
/*
35+
* When SHM_HUGETLB is set bits [26:31] encode the log2 of the huge page size.
36+
* This gives us 6 bits, which is enough until someone invents 128 bit address
37+
* spaces.
38+
*
39+
* Assume these are all power of twos.
40+
* When 0 use the default page size.
41+
*/
42+
#define SHM_HUGE_SHIFT 26
43+
#define SHM_HUGE_MASK 0x3f
44+
#define SHM_HUGE_2MB (21 << SHM_HUGE_SHIFT)
45+
#define SHM_HUGE_1GB (30 << SHM_HUGE_SHIFT)
46+
3247
#ifdef CONFIG_SYSVIPC
3348
long do_shmat(int shmid, char __user *shmaddr, int shmflg, unsigned long *addr,
3449
unsigned long shmlba);

‎include/uapi/asm-generic/mman-common.h‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,15 @@
5555
/* compatibility flags */
5656
#define MAP_FILE 0
5757

58+
/*
59+
* When MAP_HUGETLB is set bits [26:31] encode the log2 of the huge page size.
60+
* This gives us 6 bits, which is enough until someone invents 128 bit address
61+
* spaces.
62+
*
63+
* Assume these are all power of twos.
64+
* When 0 use the default page size.
65+
*/
66+
#define MAP_HUGE_SHIFT 26
67+
#define MAP_HUGE_MASK 0x3f
68+
5869
#endif /* __ASM_GENERIC_MMAN_COMMON_H */

‎include/uapi/asm-generic/mman.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#define MAP_STACK 0x20000 /* give out an address that is best suited for process/thread stacks */
1414
#define MAP_HUGETLB 0x40000 /* create a huge page mapping */
1515

16+
/* Bits [26:31] are reserved, see mman-common.h for MAP_HUGETLB usage */
17+
1618
#define MCL_CURRENT 1 /* lock all current mappings */
1719
#define MCL_FUTURE 2 /* lock all future mappings */
1820

0 commit comments

Comments
 (0)