Skip to content

Commit

Permalink
mm: support more pagesizes for MAP_HUGETLB/SHM_HUGETLB
Browse files Browse the repository at this point in the history
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.

[[email protected]: cleanups]
[[email protected]: fix build]
[[email protected]: checkpatch fixes]
Signed-off-by: Andi Kleen <[email protected]>
Cc: Michael Kerrisk <[email protected]>
Acked-by: Rik van Riel <[email protected]>
Acked-by: KAMEZAWA Hiroyuki <[email protected]>
Cc: Hillf Danton <[email protected]>
Signed-off-by: David Rientjes <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
Andi Kleen authored and torvalds committed Dec 12, 2012
1 parent ff604cf commit 42d7395
Show file tree
Hide file tree
Showing 12 changed files with 135 additions and 18 deletions.
11 changes: 11 additions & 0 deletions arch/alpha/include/asm/mman.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,15 @@
/* compatibility flags */
#define MAP_FILE 0

/*
* When MAP_HUGETLB is set bits [26:31] encode the log2 of the huge page size.
* This gives us 6 bits, which is enough until someone invents 128 bit address
* spaces.
*
* Assume these are all power of twos.
* When 0 use the default page size.
*/
#define MAP_HUGE_SHIFT 26
#define MAP_HUGE_MASK 0x3f

#endif /* __ALPHA_MMAN_H__ */
11 changes: 11 additions & 0 deletions arch/mips/include/uapi/asm/mman.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,15 @@
/* compatibility flags */
#define MAP_FILE 0

/*
* When MAP_HUGETLB is set bits [26:31] encode the log2 of the huge page size.
* This gives us 6 bits, which is enough until someone invents 128 bit address
* spaces.
*
* Assume these are all power of twos.
* When 0 use the default page size.
*/
#define MAP_HUGE_SHIFT 26
#define MAP_HUGE_MASK 0x3f

#endif /* _ASM_MMAN_H */
11 changes: 11 additions & 0 deletions arch/parisc/include/uapi/asm/mman.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,15 @@
#define MAP_FILE 0
#define MAP_VARIABLE 0

/*
* When MAP_HUGETLB is set bits [26:31] encode the log2 of the huge page size.
* This gives us 6 bits, which is enough until someone invents 128 bit address
* spaces.
*
* Assume these are all power of twos.
* When 0 use the default page size.
*/
#define MAP_HUGE_SHIFT 26
#define MAP_HUGE_MASK 0x3f

#endif /* __PARISC_MMAN_H__ */
3 changes: 3 additions & 0 deletions arch/x86/include/asm/mman.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

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

#define MAP_HUGE_2MB (21 << MAP_HUGE_SHIFT)
#define MAP_HUGE_1GB (30 << MAP_HUGE_SHIFT)

#include <asm-generic/mman.h>

#endif /* _ASM_X86_MMAN_H */
11 changes: 11 additions & 0 deletions arch/xtensa/include/uapi/asm/mman.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,15 @@
/* compatibility flags */
#define MAP_FILE 0

/*
* When MAP_HUGETLB is set bits [26:31] encode the log2 of the huge page size.
* This gives us 6 bits, which is enough until someone invents 128 bit address
* spaces.
*
* Assume these are all power of twos.
* When 0 use the default page size.
*/
#define MAP_HUGE_SHIFT 26
#define MAP_HUGE_MASK 0x3f

#endif /* _XTENSA_MMAN_H */
63 changes: 50 additions & 13 deletions fs/hugetlbfs/inode.c
Original file line number Diff line number Diff line change
Expand Up @@ -923,7 +923,7 @@ static struct file_system_type hugetlbfs_fs_type = {
.kill_sb = kill_litter_super,
};

static struct vfsmount *hugetlbfs_vfsmount;
static struct vfsmount *hugetlbfs_vfsmount[HUGE_MAX_HSTATE];

static int can_do_hugetlb_shm(void)
{
Expand All @@ -932,9 +932,22 @@ static int can_do_hugetlb_shm(void)
return capable(CAP_IPC_LOCK) || in_group_p(shm_group);
}

static int get_hstate_idx(int page_size_log)
{
struct hstate *h;

if (!page_size_log)
return default_hstate_idx;
h = size_to_hstate(1 << page_size_log);
if (!h)
return -1;
return h - hstates;
}

struct file *hugetlb_file_setup(const char *name, unsigned long addr,
size_t size, vm_flags_t acctflag,
struct user_struct **user, int creat_flags)
struct user_struct **user,
int creat_flags, int page_size_log)
{
int error = -ENOMEM;
struct file *file;
Expand All @@ -944,9 +957,14 @@ struct file *hugetlb_file_setup(const char *name, unsigned long addr,
struct qstr quick_string;
struct hstate *hstate;
unsigned long num_pages;
int hstate_idx;

hstate_idx = get_hstate_idx(page_size_log);
if (hstate_idx < 0)
return ERR_PTR(-ENODEV);

*user = NULL;
if (!hugetlbfs_vfsmount)
if (!hugetlbfs_vfsmount[hstate_idx])
return ERR_PTR(-ENOENT);

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

root = hugetlbfs_vfsmount->mnt_root;
root = hugetlbfs_vfsmount[hstate_idx]->mnt_root;
quick_string.name = name;
quick_string.len = strlen(quick_string.name);
quick_string.hash = 0;
path.dentry = d_alloc(root, &quick_string);
if (!path.dentry)
goto out_shm_unlock;

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

static int __init init_hugetlbfs_fs(void)
{
struct hstate *h;
int error;
struct vfsmount *vfsmount;
int i;

error = bdi_init(&hugetlbfs_backing_dev_info);
if (error)
Expand All @@ -1029,14 +1048,26 @@ static int __init init_hugetlbfs_fs(void)
if (error)
goto out;

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

if (!IS_ERR(vfsmount)) {
hugetlbfs_vfsmount = vfsmount;
return 0;
}
snprintf(buf, sizeof(buf), "pagesize=%uK", ps_kb);
hugetlbfs_vfsmount[i] = kern_mount_data(&hugetlbfs_fs_type,
buf);

error = PTR_ERR(vfsmount);
if (IS_ERR(hugetlbfs_vfsmount[i])) {
pr_err("hugetlb: Cannot mount internal hugetlbfs for "
"page size %uK", ps_kb);
error = PTR_ERR(hugetlbfs_vfsmount[i]);
hugetlbfs_vfsmount[i] = NULL;
}
i++;
}
/* Non default hstates are optional */
if (!IS_ERR_OR_NULL(hugetlbfs_vfsmount[default_hstate_idx]))
return 0;

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

static void __exit exit_hugetlbfs_fs(void)
{
struct hstate *h;
int i;


/*
* Make sure all delayed rcu free inodes are flushed before we
* destroy cache.
*/
rcu_barrier();
kmem_cache_destroy(hugetlbfs_inode_cachep);
kern_unmount(hugetlbfs_vfsmount);
i = 0;
for_each_hstate(h)
kern_unmount(hugetlbfs_vfsmount[i++]);
unregister_filesystem(&hugetlbfs_fs_type);
bdi_destroy(&hugetlbfs_backing_dev_info);
}
Expand Down
7 changes: 5 additions & 2 deletions include/linux/hugetlb.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ extern const struct file_operations hugetlbfs_file_operations;
extern const struct vm_operations_struct hugetlb_vm_ops;
struct file *hugetlb_file_setup(const char *name, unsigned long addr,
size_t size, vm_flags_t acct,
struct user_struct **user, int creat_flags);
struct user_struct **user, int creat_flags,
int page_size_log);

static inline int is_file_hugepages(struct file *file)
{
Expand All @@ -195,12 +196,14 @@ static inline int is_file_hugepages(struct file *file)
return 0;
}


#else /* !CONFIG_HUGETLBFS */

#define is_file_hugepages(file) 0
static inline struct file *
hugetlb_file_setup(const char *name, unsigned long addr, size_t size,
vm_flags_t acctflag, struct user_struct **user, int creat_flags)
vm_flags_t acctflag, struct user_struct **user, int creat_flags,
int page_size_log)
{
return ERR_PTR(-ENOSYS);
}
Expand Down
15 changes: 15 additions & 0 deletions include/linux/shm.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,21 @@ struct shmid_kernel /* private to the kernel */
#define SHM_HUGETLB 04000 /* segment will use huge TLB pages */
#define SHM_NORESERVE 010000 /* don't check for reservations */

/* Bits [26:31] are reserved */

/*
* When SHM_HUGETLB is set bits [26:31] encode the log2 of the huge page size.
* This gives us 6 bits, which is enough until someone invents 128 bit address
* spaces.
*
* Assume these are all power of twos.
* When 0 use the default page size.
*/
#define SHM_HUGE_SHIFT 26
#define SHM_HUGE_MASK 0x3f
#define SHM_HUGE_2MB (21 << SHM_HUGE_SHIFT)
#define SHM_HUGE_1GB (30 << SHM_HUGE_SHIFT)

#ifdef CONFIG_SYSVIPC
long do_shmat(int shmid, char __user *shmaddr, int shmflg, unsigned long *addr,
unsigned long shmlba);
Expand Down
11 changes: 11 additions & 0 deletions include/uapi/asm-generic/mman-common.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,15 @@
/* compatibility flags */
#define MAP_FILE 0

/*
* When MAP_HUGETLB is set bits [26:31] encode the log2 of the huge page size.
* This gives us 6 bits, which is enough until someone invents 128 bit address
* spaces.
*
* Assume these are all power of twos.
* When 0 use the default page size.
*/
#define MAP_HUGE_SHIFT 26
#define MAP_HUGE_MASK 0x3f

#endif /* __ASM_GENERIC_MMAN_COMMON_H */
2 changes: 2 additions & 0 deletions include/uapi/asm-generic/mman.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#define MAP_STACK 0x20000 /* give out an address that is best suited for process/thread stacks */
#define MAP_HUGETLB 0x40000 /* create a huge page mapping */

/* Bits [26:31] are reserved, see mman-common.h for MAP_HUGETLB usage */

#define MCL_CURRENT 1 /* lock all current mappings */
#define MCL_FUTURE 2 /* lock all future mappings */

Expand Down
3 changes: 2 additions & 1 deletion ipc/shm.c
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,8 @@ static int newseg(struct ipc_namespace *ns, struct ipc_params *params)
if (shmflg & SHM_NORESERVE)
acctflag = VM_NORESERVE;
file = hugetlb_file_setup(name, 0, size, acctflag,
&shp->mlock_user, HUGETLB_SHMFS_INODE);
&shp->mlock_user, HUGETLB_SHMFS_INODE,
(shmflg >> SHM_HUGE_SHIFT) & SHM_HUGE_MASK);
} else {
/*
* Do not allow no accounting for OVERCOMMIT_NEVER, even
Expand Down
5 changes: 3 additions & 2 deletions mm/mmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -1153,8 +1153,9 @@ SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len,
* memory so no accounting is necessary
*/
file = hugetlb_file_setup(HUGETLB_ANON_FILE, addr, len,
VM_NORESERVE, &user,
HUGETLB_ANONHUGE_INODE);
VM_NORESERVE,
&user, HUGETLB_ANONHUGE_INODE,
(flags >> MAP_HUGE_SHIFT) & MAP_HUGE_MASK);
if (IS_ERR(file))
return PTR_ERR(file);
}
Expand Down

0 comments on commit 42d7395

Please sign in to comment.