blob: 4acc677ac8fbcd71733a8a2e120d89401be336cb [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * hugetlbpage-backed filesystem. Based on ramfs.
3 *
Nadia Yvette Chambers6d49e352012-12-06 10:39:54 +01004 * Nadia Yvette Chambers, 2002
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * Copyright (C) 2002 Linus Torvalds.
Paul Gortmaker3e89e1c2016-01-14 15:21:52 -08007 * License: GPL
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
Andrew Morton9b857d22014-06-04 16:07:21 -070010#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/thread_info.h>
13#include <asm/current.h>
14#include <linux/sched.h> /* remove ASAP */
Mike Kravetz70c35472015-09-08 15:01:54 -070015#include <linux/falloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/fs.h>
17#include <linux/mount.h>
18#include <linux/file.h>
Randy Dunlape73a75f2007-07-15 23:40:52 -070019#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/writeback.h>
21#include <linux/pagemap.h>
22#include <linux/highmem.h>
23#include <linux/init.h>
24#include <linux/string.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080025#include <linux/capability.h>
Randy Dunlape73a75f2007-07-15 23:40:52 -070026#include <linux/ctype.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/backing-dev.h>
28#include <linux/hugetlb.h>
29#include <linux/pagevec.h>
Randy Dunlape73a75f2007-07-15 23:40:52 -070030#include <linux/parser.h>
Benjamin Herrenschmidt036e0852007-05-06 14:50:12 -070031#include <linux/mman.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/slab.h>
33#include <linux/dnotify.h>
34#include <linux/statfs.h>
35#include <linux/security.h>
Nick Black1fd7317d2009-09-22 16:43:33 -070036#include <linux/magic.h>
Naoya Horiguchi290408d2010-09-08 10:19:35 +090037#include <linux/migrate.h>
Al Viro34d06402015-04-03 11:31:35 -040038#include <linux/uio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40#include <asm/uaccess.h>
41
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -080042static const struct super_operations hugetlbfs_ops;
Christoph Hellwigf5e54d62006-06-28 04:26:44 -070043static const struct address_space_operations hugetlbfs_aops;
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080044const struct file_operations hugetlbfs_file_operations;
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -080045static const struct inode_operations hugetlbfs_dir_inode_operations;
46static const struct inode_operations hugetlbfs_inode_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
David Gibsona1d776e2012-03-21 16:34:12 -070048struct hugetlbfs_config {
Eric W. Biedermana0eb3a02012-02-07 16:19:25 -080049 kuid_t uid;
50 kgid_t gid;
David Gibsona1d776e2012-03-21 16:34:12 -070051 umode_t mode;
Mike Kravetz7ca02d02015-04-15 16:13:42 -070052 long max_hpages;
David Gibsona1d776e2012-03-21 16:34:12 -070053 long nr_inodes;
54 struct hstate *hstate;
Mike Kravetz7ca02d02015-04-15 16:13:42 -070055 long min_hpages;
David Gibsona1d776e2012-03-21 16:34:12 -070056};
57
58struct hugetlbfs_inode_info {
59 struct shared_policy policy;
60 struct inode vfs_inode;
61};
62
63static inline struct hugetlbfs_inode_info *HUGETLBFS_I(struct inode *inode)
64{
65 return container_of(inode, struct hugetlbfs_inode_info, vfs_inode);
66}
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068int sysctl_hugetlb_shm_group;
69
Randy Dunlape73a75f2007-07-15 23:40:52 -070070enum {
71 Opt_size, Opt_nr_inodes,
72 Opt_mode, Opt_uid, Opt_gid,
Mike Kravetz7ca02d02015-04-15 16:13:42 -070073 Opt_pagesize, Opt_min_size,
Randy Dunlape73a75f2007-07-15 23:40:52 -070074 Opt_err,
75};
76
Steven Whitehousea447c092008-10-13 10:46:57 +010077static const match_table_t tokens = {
Randy Dunlape73a75f2007-07-15 23:40:52 -070078 {Opt_size, "size=%s"},
79 {Opt_nr_inodes, "nr_inodes=%s"},
80 {Opt_mode, "mode=%o"},
81 {Opt_uid, "uid=%u"},
82 {Opt_gid, "gid=%u"},
Andi Kleena137e1c2008-07-23 21:27:43 -070083 {Opt_pagesize, "pagesize=%s"},
Mike Kravetz7ca02d02015-04-15 16:13:42 -070084 {Opt_min_size, "min_size=%s"},
Randy Dunlape73a75f2007-07-15 23:40:52 -070085 {Opt_err, NULL},
86};
87
Mike Kravetz70c35472015-09-08 15:01:54 -070088#ifdef CONFIG_NUMA
89static inline void hugetlb_set_vma_policy(struct vm_area_struct *vma,
90 struct inode *inode, pgoff_t index)
91{
92 vma->vm_policy = mpol_shared_policy_lookup(&HUGETLBFS_I(inode)->policy,
93 index);
94}
95
96static inline void hugetlb_drop_vma_policy(struct vm_area_struct *vma)
97{
98 mpol_cond_put(vma->vm_policy);
99}
100#else
101static inline void hugetlb_set_vma_policy(struct vm_area_struct *vma,
102 struct inode *inode, pgoff_t index)
103{
104}
105
106static inline void hugetlb_drop_vma_policy(struct vm_area_struct *vma)
107{
108}
109#endif
110
Adam Litke2e9b3672005-10-29 18:16:47 -0700111static void huge_pagevec_release(struct pagevec *pvec)
112{
113 int i;
114
115 for (i = 0; i < pagevec_count(pvec); ++i)
116 put_page(pvec->pages[i]);
117
118 pagevec_reinit(pvec);
119}
120
Mike Kravetz447effd2018-03-22 16:17:13 -0700121/*
122 * Mask used when checking the page offset value passed in via system
123 * calls. This value will be converted to a loff_t which is signed.
124 * Therefore, we want to check the upper PAGE_SHIFT + 1 bits of the
125 * value. The extra bit (- 1 in the shift value) is to take the sign
126 * bit into account.
127 */
128#define PGOFF_LOFFT_MAX \
129 (((1UL << (PAGE_SHIFT + 1)) - 1) << (BITS_PER_LONG - (PAGE_SHIFT + 1)))
130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131static int hugetlbfs_file_mmap(struct file *file, struct vm_area_struct *vma)
132{
Al Viro496ad9a2013-01-23 17:07:38 -0500133 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 loff_t len, vma_len;
135 int ret;
Andi Kleena5516432008-07-23 21:27:41 -0700136 struct hstate *h = hstate_file(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Hugh Dickins68589bc2006-11-14 02:03:32 -0800138 /*
David Gibsondec4ad82007-08-30 23:56:40 -0700139 * vma address alignment (but not the pgoff alignment) has
140 * already been checked by prepare_hugepage_range. If you add
141 * any error returns here, do so after setting VM_HUGETLB, so
142 * is_vm_hugetlb_page tests below unmap_region go the right
143 * way when do_mmap_pgoff unwinds (may be important on powerpc
144 * and ia64).
Hugh Dickins68589bc2006-11-14 02:03:32 -0800145 */
Naoya Horiguchia2fce912013-04-17 15:58:27 -0700146 vma->vm_flags |= VM_HUGETLB | VM_DONTEXPAND;
Hugh Dickins68589bc2006-11-14 02:03:32 -0800147 vma->vm_ops = &hugetlb_vm_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Mike Kravetz3d101f32017-04-13 14:56:32 -0700149 /*
Mike Kravetz447effd2018-03-22 16:17:13 -0700150 * page based offset in vm_pgoff could be sufficiently large to
Mike Kravetz0c82bca2018-04-05 16:18:21 -0700151 * overflow a loff_t when converted to byte offset. This can
152 * only happen on architectures where sizeof(loff_t) ==
153 * sizeof(unsigned long). So, only check in those instances.
Mike Kravetz3d101f32017-04-13 14:56:32 -0700154 */
Mike Kravetz0c82bca2018-04-05 16:18:21 -0700155 if (sizeof(unsigned long) == sizeof(loff_t)) {
156 if (vma->vm_pgoff & PGOFF_LOFFT_MAX)
157 return -EINVAL;
158 }
Mike Kravetz3d101f32017-04-13 14:56:32 -0700159
Mike Kravetz447effd2018-03-22 16:17:13 -0700160 /* must be huge page aligned */
Becky Bruce2b37c352011-07-25 17:11:49 -0700161 if (vma->vm_pgoff & (~huge_page_mask(h) >> PAGE_SHIFT))
David Gibsondec4ad82007-08-30 23:56:40 -0700162 return -EINVAL;
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 vma_len = (loff_t)(vma->vm_end - vma->vm_start);
Mike Kravetz3d101f32017-04-13 14:56:32 -0700165 len = vma_len + ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
166 /* check for overflow */
167 if (len < vma_len)
168 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Al Viro59551022016-01-22 15:40:57 -0500170 inode_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 file_accessed(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173 ret = -ENOMEM;
Mel Gormana1e78772008-07-23 21:27:23 -0700174 if (hugetlb_reserve_pages(inode,
Andi Kleena5516432008-07-23 21:27:41 -0700175 vma->vm_pgoff >> huge_page_order(h),
Mel Gorman5a6fe122009-02-10 14:02:27 +0000176 len >> huge_page_shift(h), vma,
177 vma->vm_flags))
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -0700178 goto out;
David Gibsonb45b5bd2006-03-22 00:08:55 -0800179
Adam Litke4c887262005-10-29 18:16:46 -0700180 ret = 0;
Zhang, Yanminb6174df2006-07-10 04:44:49 -0700181 if (vma->vm_flags & VM_WRITE && inode->i_size < len)
Mike Kravetz3d101f32017-04-13 14:56:32 -0700182 i_size_write(inode, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183out:
Al Viro59551022016-01-22 15:40:57 -0500184 inode_unlock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186 return ret;
187}
188
189/*
Hugh Dickins508034a2005-10-29 18:16:30 -0700190 * Called under down_write(mmap_sem).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 */
192
Adrian Bunkd2ba27e82007-05-06 14:49:00 -0700193#ifndef HAVE_ARCH_HUGETLB_UNMAPPED_AREA
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194static unsigned long
195hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
196 unsigned long len, unsigned long pgoff, unsigned long flags)
197{
198 struct mm_struct *mm = current->mm;
199 struct vm_area_struct *vma;
Andi Kleena5516432008-07-23 21:27:41 -0700200 struct hstate *h = hstate_file(file);
Michel Lespinasse08659352012-12-11 16:02:00 -0800201 struct vm_unmapped_area_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Andi Kleena5516432008-07-23 21:27:41 -0700203 if (len & ~huge_page_mask(h))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 return -EINVAL;
205 if (len > TASK_SIZE)
206 return -ENOMEM;
207
Benjamin Herrenschmidt036e0852007-05-06 14:50:12 -0700208 if (flags & MAP_FIXED) {
Andi Kleena5516432008-07-23 21:27:41 -0700209 if (prepare_hugepage_range(file, addr, len))
Benjamin Herrenschmidt036e0852007-05-06 14:50:12 -0700210 return -EINVAL;
211 return addr;
212 }
213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 if (addr) {
Andi Kleena5516432008-07-23 21:27:41 -0700215 addr = ALIGN(addr, huge_page_size(h));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 vma = find_vma(mm, addr);
217 if (TASK_SIZE - len >= addr &&
Hugh Dickinscfc0eb402017-06-19 04:03:24 -0700218 (!vma || addr + len <= vm_start_gap(vma)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 return addr;
220 }
221
Michel Lespinasse08659352012-12-11 16:02:00 -0800222 info.flags = 0;
223 info.length = len;
224 info.low_limit = TASK_UNMAPPED_BASE;
225 info.high_limit = TASK_SIZE;
226 info.align_mask = PAGE_MASK & ~huge_page_mask(h);
227 info.align_offset = 0;
228 return vm_unmapped_area(&info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229}
230#endif
231
Al Viro34d06402015-04-03 11:31:35 -0400232static size_t
Badari Pulavartye63e1e52007-10-16 01:26:22 -0700233hugetlbfs_read_actor(struct page *page, unsigned long offset,
Al Viro34d06402015-04-03 11:31:35 -0400234 struct iov_iter *to, unsigned long size)
Badari Pulavartye63e1e52007-10-16 01:26:22 -0700235{
Al Viro34d06402015-04-03 11:31:35 -0400236 size_t copied = 0;
Badari Pulavartye63e1e52007-10-16 01:26:22 -0700237 int i, chunksize;
238
Badari Pulavartye63e1e52007-10-16 01:26:22 -0700239 /* Find which 4k chunk and offset with in that chunk */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300240 i = offset >> PAGE_SHIFT;
241 offset = offset & ~PAGE_MASK;
Badari Pulavartye63e1e52007-10-16 01:26:22 -0700242
243 while (size) {
Al Viro34d06402015-04-03 11:31:35 -0400244 size_t n;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300245 chunksize = PAGE_SIZE;
Badari Pulavartye63e1e52007-10-16 01:26:22 -0700246 if (offset)
247 chunksize -= offset;
248 if (chunksize > size)
249 chunksize = size;
Al Viro34d06402015-04-03 11:31:35 -0400250 n = copy_page_to_iter(&page[i], offset, chunksize, to);
251 copied += n;
252 if (n != chunksize)
253 return copied;
Badari Pulavartye63e1e52007-10-16 01:26:22 -0700254 offset = 0;
255 size -= chunksize;
Badari Pulavartye63e1e52007-10-16 01:26:22 -0700256 i++;
257 }
Al Viro34d06402015-04-03 11:31:35 -0400258 return copied;
Badari Pulavartye63e1e52007-10-16 01:26:22 -0700259}
260
261/*
262 * Support for read() - Find the page attached to f_mapping and copy out the
263 * data. Its *very* similar to do_generic_mapping_read(), we can't use that
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +0300264 * since it has PAGE_SIZE assumptions.
Badari Pulavartye63e1e52007-10-16 01:26:22 -0700265 */
Al Viro34d06402015-04-03 11:31:35 -0400266static ssize_t hugetlbfs_read_iter(struct kiocb *iocb, struct iov_iter *to)
Badari Pulavartye63e1e52007-10-16 01:26:22 -0700267{
Al Viro34d06402015-04-03 11:31:35 -0400268 struct file *file = iocb->ki_filp;
269 struct hstate *h = hstate_file(file);
270 struct address_space *mapping = file->f_mapping;
Badari Pulavartye63e1e52007-10-16 01:26:22 -0700271 struct inode *inode = mapping->host;
Al Viro34d06402015-04-03 11:31:35 -0400272 unsigned long index = iocb->ki_pos >> huge_page_shift(h);
273 unsigned long offset = iocb->ki_pos & ~huge_page_mask(h);
Badari Pulavartye63e1e52007-10-16 01:26:22 -0700274 unsigned long end_index;
275 loff_t isize;
276 ssize_t retval = 0;
277
Al Viro34d06402015-04-03 11:31:35 -0400278 while (iov_iter_count(to)) {
Badari Pulavartye63e1e52007-10-16 01:26:22 -0700279 struct page *page;
Al Viro34d06402015-04-03 11:31:35 -0400280 size_t nr, copied;
Badari Pulavartye63e1e52007-10-16 01:26:22 -0700281
282 /* nr is the maximum number of bytes to copy from this page */
Andi Kleena5516432008-07-23 21:27:41 -0700283 nr = huge_page_size(h);
Aneesh Kumar K.Va05b0852012-03-21 16:34:08 -0700284 isize = i_size_read(inode);
285 if (!isize)
Al Viro34d06402015-04-03 11:31:35 -0400286 break;
Aneesh Kumar K.Va05b0852012-03-21 16:34:08 -0700287 end_index = (isize - 1) >> huge_page_shift(h);
Al Viro34d06402015-04-03 11:31:35 -0400288 if (index > end_index)
289 break;
290 if (index == end_index) {
Andi Kleena5516432008-07-23 21:27:41 -0700291 nr = ((isize - 1) & ~huge_page_mask(h)) + 1;
Aneesh Kumar K.Va05b0852012-03-21 16:34:08 -0700292 if (nr <= offset)
Al Viro34d06402015-04-03 11:31:35 -0400293 break;
Badari Pulavartye63e1e52007-10-16 01:26:22 -0700294 }
295 nr = nr - offset;
296
297 /* Find the page */
Aneesh Kumar K.Va05b0852012-03-21 16:34:08 -0700298 page = find_lock_page(mapping, index);
Badari Pulavartye63e1e52007-10-16 01:26:22 -0700299 if (unlikely(page == NULL)) {
300 /*
301 * We have a HOLE, zero out the user-buffer for the
302 * length of the hole or request.
303 */
Al Viro34d06402015-04-03 11:31:35 -0400304 copied = iov_iter_zero(nr, to);
Badari Pulavartye63e1e52007-10-16 01:26:22 -0700305 } else {
Aneesh Kumar K.Va05b0852012-03-21 16:34:08 -0700306 unlock_page(page);
307
Badari Pulavartye63e1e52007-10-16 01:26:22 -0700308 /*
309 * We have the page, copy it to user space buffer.
310 */
Al Viro34d06402015-04-03 11:31:35 -0400311 copied = hugetlbfs_read_actor(page, offset, to, nr);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300312 put_page(page);
Badari Pulavartye63e1e52007-10-16 01:26:22 -0700313 }
Al Viro34d06402015-04-03 11:31:35 -0400314 offset += copied;
315 retval += copied;
316 if (copied != nr && iov_iter_count(to)) {
317 if (!retval)
318 retval = -EFAULT;
319 break;
Badari Pulavartye63e1e52007-10-16 01:26:22 -0700320 }
Andi Kleena5516432008-07-23 21:27:41 -0700321 index += offset >> huge_page_shift(h);
322 offset &= ~huge_page_mask(h);
Badari Pulavartye63e1e52007-10-16 01:26:22 -0700323 }
Al Viro34d06402015-04-03 11:31:35 -0400324 iocb->ki_pos = ((loff_t)index << huge_page_shift(h)) + offset;
Badari Pulavartye63e1e52007-10-16 01:26:22 -0700325 return retval;
326}
327
Nick Piggin800d15a2007-10-16 01:25:03 -0700328static int hugetlbfs_write_begin(struct file *file,
329 struct address_space *mapping,
330 loff_t pos, unsigned len, unsigned flags,
331 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
333 return -EINVAL;
334}
335
Nick Piggin800d15a2007-10-16 01:25:03 -0700336static int hugetlbfs_write_end(struct file *file, struct address_space *mapping,
337 loff_t pos, unsigned len, unsigned copied,
338 struct page *page, void *fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
Nick Piggin800d15a2007-10-16 01:25:03 -0700340 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 return -EINVAL;
342}
343
Mike Kravetzb5cec282015-09-08 15:01:41 -0700344static void remove_huge_page(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
Konstantin Khlebnikovb9ea2512015-04-14 15:45:27 -0700346 ClearPageDirty(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 ClearPageUptodate(page);
Minchan Kimbd65cb82011-03-22 16:30:54 -0700348 delete_from_page_cache(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349}
350
Mike Kravetz4aae8d12016-01-15 16:57:40 -0800351static void
352hugetlb_vmdelete_list(struct rb_root *root, pgoff_t start, pgoff_t end)
353{
354 struct vm_area_struct *vma;
355
356 /*
357 * end == 0 indicates that the entire range after
358 * start should be unmapped.
359 */
360 vma_interval_tree_foreach(vma, root, start, end ? end : ULONG_MAX) {
361 unsigned long v_offset;
362 unsigned long v_end;
363
364 /*
365 * Can the expression below overflow on 32-bit arches?
366 * No, because the interval tree returns us only those vmas
367 * which overlap the truncated area starting at pgoff,
368 * and no vma on a 32-bit arch can span beyond the 4GB.
369 */
370 if (vma->vm_pgoff < start)
371 v_offset = (start - vma->vm_pgoff) << PAGE_SHIFT;
372 else
373 v_offset = 0;
374
375 if (!end)
376 v_end = vma->vm_end;
377 else {
378 v_end = ((end - vma->vm_pgoff) << PAGE_SHIFT)
379 + vma->vm_start;
380 if (v_end > vma->vm_end)
381 v_end = vma->vm_end;
382 }
383
384 unmap_hugepage_range(vma, vma->vm_start + v_offset, v_end,
385 NULL);
386 }
387}
Mike Kravetzb5cec282015-09-08 15:01:41 -0700388
389/*
390 * remove_inode_hugepages handles two distinct cases: truncation and hole
391 * punch. There are subtle differences in operation for each case.
Mike Kravetz4aae8d12016-01-15 16:57:40 -0800392 *
Mike Kravetzb5cec282015-09-08 15:01:41 -0700393 * truncation is indicated by end of range being LLONG_MAX
394 * In this case, we first scan the range and release found pages.
395 * After releasing pages, hugetlb_unreserve_pages cleans up region/reserv
Mike Kravetz18178892015-11-20 15:57:13 -0800396 * maps and global counts. Page faults can not race with truncation
397 * in this routine. hugetlb_no_page() prevents page faults in the
398 * truncated range. It checks i_size before allocation, and again after
399 * with the page table lock for the page held. The same lock must be
400 * acquired to unmap a page.
Mike Kravetzb5cec282015-09-08 15:01:41 -0700401 * hole punch is indicated if end is not LLONG_MAX
402 * In the hole punch case we scan the range and release found pages.
403 * Only when releasing a page is the associated region/reserv map
404 * deleted. The region/reserv map for ranges without associated
Mike Kravetz18178892015-11-20 15:57:13 -0800405 * pages are not modified. Page faults can race with hole punch.
406 * This is indicated if we find a mapped page.
Mike Kravetzb5cec282015-09-08 15:01:41 -0700407 * Note: If the passed end of range value is beyond the end of file, but
408 * not LLONG_MAX this routine still performs a hole punch operation.
409 */
410static void remove_inode_hugepages(struct inode *inode, loff_t lstart,
411 loff_t lend)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
Andi Kleena5516432008-07-23 21:27:41 -0700413 struct hstate *h = hstate_inode(inode);
David Gibsonb45b5bd2006-03-22 00:08:55 -0800414 struct address_space *mapping = &inode->i_data;
Andi Kleena5516432008-07-23 21:27:41 -0700415 const pgoff_t start = lstart >> huge_page_shift(h);
Mike Kravetzb5cec282015-09-08 15:01:41 -0700416 const pgoff_t end = lend >> huge_page_shift(h);
417 struct vm_area_struct pseudo_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 struct pagevec pvec;
419 pgoff_t next;
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -0700420 int i, freed = 0;
Mike Kravetzb5cec282015-09-08 15:01:41 -0700421 long lookup_nr = PAGEVEC_SIZE;
422 bool truncate_op = (lend == LLONG_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Mike Kravetzb5cec282015-09-08 15:01:41 -0700424 memset(&pseudo_vma, 0, sizeof(struct vm_area_struct));
425 pseudo_vma.vm_flags = (VM_HUGETLB | VM_MAYSHARE | VM_SHARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 pagevec_init(&pvec, 0);
427 next = start;
Mike Kravetzb5cec282015-09-08 15:01:41 -0700428 while (next < end) {
429 /*
Mike Kravetz18178892015-11-20 15:57:13 -0800430 * Don't grab more pages than the number left in the range.
Mike Kravetzb5cec282015-09-08 15:01:41 -0700431 */
432 if (end - next < lookup_nr)
433 lookup_nr = end - next;
434
435 /*
Mike Kravetz18178892015-11-20 15:57:13 -0800436 * When no more pages are found, we are done.
Mike Kravetzb5cec282015-09-08 15:01:41 -0700437 */
Mike Kravetz18178892015-11-20 15:57:13 -0800438 if (!pagevec_lookup(&pvec, mapping, next, lookup_nr))
439 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441 for (i = 0; i < pagevec_count(&pvec); ++i) {
442 struct page *page = pvec.pages[i];
Mike Kravetzb5cec282015-09-08 15:01:41 -0700443 u32 hash;
444
Mike Kravetz18178892015-11-20 15:57:13 -0800445 /*
446 * The page (index) could be beyond end. This is
447 * only possible in the punch hole case as end is
448 * max page offset in the truncate case.
449 */
450 next = page->index;
451 if (next >= end)
452 break;
453
Mike Kravetzb5cec282015-09-08 15:01:41 -0700454 hash = hugetlb_fault_mutex_hash(h, current->mm,
455 &pseudo_vma,
456 mapping, next, 0);
457 mutex_lock(&hugetlb_fault_mutex_table[hash]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Mike Kravetz4aae8d12016-01-15 16:57:40 -0800459 /*
460 * If page is mapped, it was faulted in after being
461 * unmapped in caller. Unmap (again) now after taking
462 * the fault mutex. The mutex will prevent faults
463 * until we finish removing the page.
464 *
465 * This race can only happen in the hole punch case.
466 * Getting here in a truncate operation is a bug.
467 */
468 if (unlikely(page_mapped(page))) {
Mike Kravetz18178892015-11-20 15:57:13 -0800469 BUG_ON(truncate_op);
Mike Kravetz4aae8d12016-01-15 16:57:40 -0800470
471 i_mmap_lock_write(mapping);
472 hugetlb_vmdelete_list(&mapping->i_mmap,
473 next * pages_per_huge_page(h),
474 (next + 1) * pages_per_huge_page(h));
475 i_mmap_unlock_write(mapping);
476 }
477
478 lock_page(page);
479 /*
480 * We must free the huge page and remove from page
481 * cache (remove_huge_page) BEFORE removing the
482 * region/reserve map (hugetlb_unreserve_pages). In
483 * rare out of memory conditions, removal of the
zhong jiang72e29362016-10-07 17:02:01 -0700484 * region/reserve map could fail. Correspondingly,
485 * the subpool and global reserve usage count can need
486 * to be adjusted.
Mike Kravetz4aae8d12016-01-15 16:57:40 -0800487 */
zhong jiang72e29362016-10-07 17:02:01 -0700488 VM_BUG_ON(PagePrivate(page));
Mike Kravetz4aae8d12016-01-15 16:57:40 -0800489 remove_huge_page(page);
490 freed++;
491 if (!truncate_op) {
492 if (unlikely(hugetlb_unreserve_pages(inode,
493 next, next + 1, 1)))
zhong jiang72e29362016-10-07 17:02:01 -0700494 hugetlb_fix_reserve_counts(inode);
Mike Kravetzb5cec282015-09-08 15:01:41 -0700495 }
496
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 unlock_page(page);
Mike Kravetzb5cec282015-09-08 15:01:41 -0700498 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 }
Mike Kravetz18178892015-11-20 15:57:13 -0800500 ++next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 huge_pagevec_release(&pvec);
Mike Kravetz18178892015-11-20 15:57:13 -0800502 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 }
Mike Kravetzb5cec282015-09-08 15:01:41 -0700504
505 if (truncate_op)
506 (void)hugetlb_unreserve_pages(inode, start, LONG_MAX, freed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507}
508
Al Viro2bbbda32010-06-04 19:52:12 -0400509static void hugetlbfs_evict_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510{
Joonsoo Kim9119a412014-04-03 14:47:25 -0700511 struct resv_map *resv_map;
512
Mike Kravetzb5cec282015-09-08 15:01:41 -0700513 remove_inode_hugepages(inode, 0, LLONG_MAX);
Joonsoo Kim9119a412014-04-03 14:47:25 -0700514 resv_map = (struct resv_map *)inode->i_mapping->private_data;
515 /* root inode doesn't have the resv_map, so we should check it */
516 if (resv_map)
517 resv_map_release(&resv_map->refs);
Jan Karadbd57682012-05-03 14:48:02 +0200518 clear_inode(inode);
Christoph Hellwig149f4212005-10-29 18:16:43 -0700519}
520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521static int hugetlb_vmtruncate(struct inode *inode, loff_t offset)
522{
Hugh Dickins856fc292006-10-28 10:38:43 -0700523 pgoff_t pgoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 struct address_space *mapping = inode->i_mapping;
Andi Kleena5516432008-07-23 21:27:41 -0700525 struct hstate *h = hstate_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Andi Kleena5516432008-07-23 21:27:41 -0700527 BUG_ON(offset & ~huge_page_mask(h));
Hugh Dickins856fc292006-10-28 10:38:43 -0700528 pgoff = offset >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Ken Chen7aa91e12007-10-16 01:26:21 -0700530 i_size_write(inode, offset);
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -0800531 i_mmap_lock_write(mapping);
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700532 if (!RB_EMPTY_ROOT(&mapping->i_mmap))
Mike Kravetz1bfad992015-09-08 15:01:38 -0700533 hugetlb_vmdelete_list(&mapping->i_mmap, pgoff, 0);
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -0800534 i_mmap_unlock_write(mapping);
Mike Kravetzb5cec282015-09-08 15:01:41 -0700535 remove_inode_hugepages(inode, offset, LLONG_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 return 0;
537}
538
Mike Kravetz70c35472015-09-08 15:01:54 -0700539static long hugetlbfs_punch_hole(struct inode *inode, loff_t offset, loff_t len)
540{
541 struct hstate *h = hstate_inode(inode);
542 loff_t hpage_size = huge_page_size(h);
543 loff_t hole_start, hole_end;
544
545 /*
546 * For hole punch round up the beginning offset of the hole and
547 * round down the end.
548 */
549 hole_start = round_up(offset, hpage_size);
550 hole_end = round_down(offset + len, hpage_size);
551
552 if (hole_end > hole_start) {
553 struct address_space *mapping = inode->i_mapping;
554
Al Viro59551022016-01-22 15:40:57 -0500555 inode_lock(inode);
Mike Kravetz70c35472015-09-08 15:01:54 -0700556 i_mmap_lock_write(mapping);
557 if (!RB_EMPTY_ROOT(&mapping->i_mmap))
558 hugetlb_vmdelete_list(&mapping->i_mmap,
559 hole_start >> PAGE_SHIFT,
560 hole_end >> PAGE_SHIFT);
561 i_mmap_unlock_write(mapping);
562 remove_inode_hugepages(inode, hole_start, hole_end);
Al Viro59551022016-01-22 15:40:57 -0500563 inode_unlock(inode);
Mike Kravetz70c35472015-09-08 15:01:54 -0700564 }
565
566 return 0;
567}
568
569static long hugetlbfs_fallocate(struct file *file, int mode, loff_t offset,
570 loff_t len)
571{
572 struct inode *inode = file_inode(file);
573 struct address_space *mapping = inode->i_mapping;
574 struct hstate *h = hstate_inode(inode);
575 struct vm_area_struct pseudo_vma;
576 struct mm_struct *mm = current->mm;
577 loff_t hpage_size = huge_page_size(h);
578 unsigned long hpage_shift = huge_page_shift(h);
579 pgoff_t start, index, end;
580 int error;
581 u32 hash;
582
583 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
584 return -EOPNOTSUPP;
585
586 if (mode & FALLOC_FL_PUNCH_HOLE)
587 return hugetlbfs_punch_hole(inode, offset, len);
588
589 /*
590 * Default preallocate case.
591 * For this range, start is rounded down and end is rounded up
592 * as well as being converted to page offsets.
593 */
594 start = offset >> hpage_shift;
595 end = (offset + len + hpage_size - 1) >> hpage_shift;
596
Al Viro59551022016-01-22 15:40:57 -0500597 inode_lock(inode);
Mike Kravetz70c35472015-09-08 15:01:54 -0700598
599 /* We need to check rlimit even when FALLOC_FL_KEEP_SIZE */
600 error = inode_newsize_ok(inode, offset + len);
601 if (error)
602 goto out;
603
604 /*
605 * Initialize a pseudo vma as this is required by the huge page
606 * allocation routines. If NUMA is configured, use page index
607 * as input to create an allocation policy.
608 */
609 memset(&pseudo_vma, 0, sizeof(struct vm_area_struct));
610 pseudo_vma.vm_flags = (VM_HUGETLB | VM_MAYSHARE | VM_SHARED);
611 pseudo_vma.vm_file = file;
612
613 for (index = start; index < end; index++) {
614 /*
615 * This is supposed to be the vaddr where the page is being
616 * faulted in, but we have no vaddr here.
617 */
618 struct page *page;
619 unsigned long addr;
620 int avoid_reserve = 0;
621
622 cond_resched();
623
624 /*
625 * fallocate(2) manpage permits EINTR; we may have been
626 * interrupted because we are using up too much memory.
627 */
628 if (signal_pending(current)) {
629 error = -EINTR;
630 break;
631 }
632
633 /* Set numa allocation policy based on index */
634 hugetlb_set_vma_policy(&pseudo_vma, inode, index);
635
636 /* addr is the offset within the file (zero based) */
637 addr = index * hpage_size;
638
639 /* mutex taken here, fault path and hole punch */
640 hash = hugetlb_fault_mutex_hash(h, mm, &pseudo_vma, mapping,
641 index, addr);
642 mutex_lock(&hugetlb_fault_mutex_table[hash]);
643
644 /* See if already present in mapping to avoid alloc/free */
645 page = find_get_page(mapping, index);
646 if (page) {
647 put_page(page);
648 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
649 hugetlb_drop_vma_policy(&pseudo_vma);
650 continue;
651 }
652
653 /* Allocate page and add to page cache */
654 page = alloc_huge_page(&pseudo_vma, addr, avoid_reserve);
655 hugetlb_drop_vma_policy(&pseudo_vma);
656 if (IS_ERR(page)) {
657 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
658 error = PTR_ERR(page);
659 goto out;
660 }
661 clear_huge_page(page, addr, pages_per_huge_page(h));
662 __SetPageUptodate(page);
663 error = huge_add_to_page_cache(page, mapping, index);
664 if (unlikely(error)) {
665 put_page(page);
666 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
667 goto out;
668 }
669
670 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
671
672 /*
673 * page_put due to reference from alloc_huge_page()
674 * unlock_page because locked by add_to_page_cache()
675 */
676 put_page(page);
677 unlock_page(page);
678 }
679
680 if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size)
681 i_size_write(inode, offset + len);
Deepa Dinamani078cd822016-09-14 07:48:04 -0700682 inode->i_ctime = current_time(inode);
Mike Kravetz70c35472015-09-08 15:01:54 -0700683out:
Al Viro59551022016-01-22 15:40:57 -0500684 inode_unlock(inode);
Mike Kravetz70c35472015-09-08 15:01:54 -0700685 return error;
686}
687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688static int hugetlbfs_setattr(struct dentry *dentry, struct iattr *attr)
689{
David Howells2b0143b2015-03-17 22:25:59 +0000690 struct inode *inode = d_inode(dentry);
Andi Kleena5516432008-07-23 21:27:41 -0700691 struct hstate *h = hstate_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 int error;
693 unsigned int ia_valid = attr->ia_valid;
694
695 BUG_ON(!inode);
696
Jan Kara31051c82016-05-26 16:55:18 +0200697 error = setattr_prepare(dentry, attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 if (error)
Christoph Hellwig10257742010-06-04 11:30:02 +0200699 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
701 if (ia_valid & ATTR_SIZE) {
702 error = -EINVAL;
Christoph Hellwig10257742010-06-04 11:30:02 +0200703 if (attr->ia_size & ~huge_page_mask(h))
704 return -EINVAL;
705 error = hugetlb_vmtruncate(inode, attr->ia_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 if (error)
Christoph Hellwig10257742010-06-04 11:30:02 +0200707 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 }
Christoph Hellwig10257742010-06-04 11:30:02 +0200709
710 setattr_copy(inode, attr);
711 mark_inode_dirty(inode);
712 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713}
714
Al Viro7d54fa62011-07-24 20:20:48 -0400715static struct inode *hugetlbfs_get_root(struct super_block *sb,
716 struct hugetlbfs_config *config)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
718 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720 inode = new_inode(sb);
721 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400722 inode->i_ino = get_next_ino();
Al Viro7d54fa62011-07-24 20:20:48 -0400723 inode->i_mode = S_IFDIR | config->mode;
724 inode->i_uid = config->uid;
725 inode->i_gid = config->gid;
Deepa Dinamani078cd822016-09-14 07:48:04 -0700726 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
Al Viro7d54fa62011-07-24 20:20:48 -0400727 inode->i_op = &hugetlbfs_dir_inode_operations;
728 inode->i_fop = &simple_dir_operations;
729 /* directory inodes start off with i_nlink == 2 (for "." entry) */
730 inc_nlink(inode);
Aneesh Kumar K.V65ed7602012-04-25 16:01:50 -0700731 lockdep_annotate_inode_mutex_key(inode);
Al Viro7d54fa62011-07-24 20:20:48 -0400732 }
733 return inode;
734}
735
Michal Hockob610ded2013-08-13 16:00:55 -0700736/*
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -0800737 * Hugetlbfs is not reclaimable; therefore its i_mmap_rwsem will never
Michal Hockob610ded2013-08-13 16:00:55 -0700738 * be taken from reclaim -- unlike regular filesystems. This needs an
Kirill A. Shutemov88f306b2016-01-15 16:57:31 -0800739 * annotation because huge_pmd_share() does an allocation under hugetlb's
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -0800740 * i_mmap_rwsem.
Michal Hockob610ded2013-08-13 16:00:55 -0700741 */
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -0800742static struct lock_class_key hugetlbfs_i_mmap_rwsem_key;
Michal Hockob610ded2013-08-13 16:00:55 -0700743
Al Viro7d54fa62011-07-24 20:20:48 -0400744static struct inode *hugetlbfs_get_inode(struct super_block *sb,
745 struct inode *dir,
Al Viro18df2252011-07-24 23:17:40 -0400746 umode_t mode, dev_t dev)
Al Viro7d54fa62011-07-24 20:20:48 -0400747{
748 struct inode *inode;
Mike Kravetzd57e4ae2019-04-05 18:39:06 -0700749 struct resv_map *resv_map = NULL;
Joonsoo Kim9119a412014-04-03 14:47:25 -0700750
Mike Kravetzd57e4ae2019-04-05 18:39:06 -0700751 /*
752 * Reserve maps are only needed for inodes that can have associated
753 * page allocations.
754 */
755 if (S_ISREG(mode) || S_ISLNK(mode)) {
756 resv_map = resv_map_alloc();
757 if (!resv_map)
758 return NULL;
759 }
Al Viro7d54fa62011-07-24 20:20:48 -0400760
761 inode = new_inode(sb);
762 if (inode) {
Al Viro7d54fa62011-07-24 20:20:48 -0400763 inode->i_ino = get_next_ino();
764 inode_init_owner(inode, dir, mode);
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -0800765 lockdep_set_class(&inode->i_mapping->i_mmap_rwsem,
766 &hugetlbfs_i_mmap_rwsem_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 inode->i_mapping->a_ops = &hugetlbfs_aops;
Deepa Dinamani078cd822016-09-14 07:48:04 -0700768 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
Joonsoo Kim9119a412014-04-03 14:47:25 -0700769 inode->i_mapping->private_data = resv_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 switch (mode & S_IFMT) {
771 default:
772 init_special_inode(inode, mode, dev);
773 break;
774 case S_IFREG:
775 inode->i_op = &hugetlbfs_inode_operations;
776 inode->i_fop = &hugetlbfs_file_operations;
777 break;
778 case S_IFDIR:
779 inode->i_op = &hugetlbfs_dir_inode_operations;
780 inode->i_fop = &simple_dir_operations;
781
782 /* directory inodes start off with i_nlink == 2 (for "." entry) */
Dave Hansend8c76e62006-09-30 23:29:04 -0700783 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 break;
785 case S_IFLNK:
786 inode->i_op = &page_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -0500787 inode_nohighmem(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 break;
789 }
Josh Boyere096d0c2011-08-25 07:48:12 -0400790 lockdep_annotate_inode_mutex_key(inode);
Mike Kravetzd57e4ae2019-04-05 18:39:06 -0700791 } else {
792 if (resv_map)
793 kref_put(&resv_map->refs, resv_map_release);
794 }
Joonsoo Kim9119a412014-04-03 14:47:25 -0700795
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 return inode;
797}
798
799/*
800 * File creation. Allocate an inode, and we're done..
801 */
802static int hugetlbfs_mknod(struct inode *dir,
Al Viro1a67aaf2011-07-26 01:52:52 -0400803 struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804{
805 struct inode *inode;
806 int error = -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Al Viro7d54fa62011-07-24 20:20:48 -0400808 inode = hugetlbfs_get_inode(dir->i_sb, dir, mode, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 if (inode) {
Deepa Dinamani078cd822016-09-14 07:48:04 -0700810 dir->i_ctime = dir->i_mtime = current_time(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 d_instantiate(dentry, inode);
812 dget(dentry); /* Extra count - pin the dentry in core */
813 error = 0;
814 }
815 return error;
816}
817
Al Viro18bb1db2011-07-26 01:41:39 -0400818static int hugetlbfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819{
820 int retval = hugetlbfs_mknod(dir, dentry, mode | S_IFDIR, 0);
821 if (!retval)
Dave Hansend8c76e62006-09-30 23:29:04 -0700822 inc_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 return retval;
824}
825
Al Viroebfc3b42012-06-10 18:05:36 -0400826static int hugetlbfs_create(struct inode *dir, struct dentry *dentry, umode_t mode, bool excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
828 return hugetlbfs_mknod(dir, dentry, mode | S_IFREG, 0);
829}
830
831static int hugetlbfs_symlink(struct inode *dir,
832 struct dentry *dentry, const char *symname)
833{
834 struct inode *inode;
835 int error = -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Al Viro7d54fa62011-07-24 20:20:48 -0400837 inode = hugetlbfs_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 if (inode) {
839 int l = strlen(symname)+1;
840 error = page_symlink(inode, symname, l);
841 if (!error) {
842 d_instantiate(dentry, inode);
843 dget(dentry);
844 } else
845 iput(inode);
846 }
Deepa Dinamani078cd822016-09-14 07:48:04 -0700847 dir->i_ctime = dir->i_mtime = current_time(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849 return error;
850}
851
852/*
Ken Chen6649a382007-02-08 14:20:27 -0800853 * mark the head page dirty
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 */
855static int hugetlbfs_set_page_dirty(struct page *page)
856{
Christoph Lameterd85f3382007-05-06 14:49:39 -0700857 struct page *head = compound_head(page);
Ken Chen6649a382007-02-08 14:20:27 -0800858
859 SetPageDirty(head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 return 0;
861}
862
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900863static int hugetlbfs_migrate_page(struct address_space *mapping,
Mel Gormanb969c4a2012-01-12 17:19:34 -0800864 struct page *newpage, struct page *page,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800865 enum migrate_mode mode)
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900866{
867 int rc;
868
869 rc = migrate_huge_page_move_mapping(mapping, newpage, page);
Rafael Aquini78bd5202012-12-11 16:02:31 -0800870 if (rc != MIGRATEPAGE_SUCCESS)
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900871 return rc;
Mike Kravetzb010e032019-02-28 16:22:02 -0800872
873 /*
874 * page_private is subpool pointer in hugetlb pages. Transfer to
875 * new page. PagePrivate is not associated with page_private for
876 * hugetlb pages and can not be set here as only page_huge_active
877 * pages can be migrated.
878 */
879 if (page_private(page)) {
880 set_page_private(newpage, page_private(page));
881 set_page_private(page, 0);
882 }
883
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900884 migrate_page_copy(newpage, page);
885
Rafael Aquini78bd5202012-12-11 16:02:31 -0800886 return MIGRATEPAGE_SUCCESS;
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900887}
888
David Howells726c3342006-06-23 02:02:58 -0700889static int hugetlbfs_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890{
David Howells726c3342006-06-23 02:02:58 -0700891 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(dentry->d_sb);
David Howells2b0143b2015-03-17 22:25:59 +0000892 struct hstate *h = hstate_inode(d_inode(dentry));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
894 buf->f_type = HUGETLBFS_MAGIC;
Andi Kleena5516432008-07-23 21:27:41 -0700895 buf->f_bsize = huge_page_size(h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 if (sbinfo) {
897 spin_lock(&sbinfo->stat_lock);
David Gibson74a8a652005-11-21 21:32:24 -0800898 /* If no limits set, just report 0 for max/free/used
899 * blocks, like simple_statfs() */
David Gibson90481622012-03-21 16:34:12 -0700900 if (sbinfo->spool) {
901 long free_pages;
902
903 spin_lock(&sbinfo->spool->lock);
904 buf->f_blocks = sbinfo->spool->max_hpages;
905 free_pages = sbinfo->spool->max_hpages
906 - sbinfo->spool->used_hpages;
907 buf->f_bavail = buf->f_bfree = free_pages;
908 spin_unlock(&sbinfo->spool->lock);
David Gibson74a8a652005-11-21 21:32:24 -0800909 buf->f_files = sbinfo->max_inodes;
910 buf->f_ffree = sbinfo->free_inodes;
911 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 spin_unlock(&sbinfo->stat_lock);
913 }
914 buf->f_namelen = NAME_MAX;
915 return 0;
916}
917
918static void hugetlbfs_put_super(struct super_block *sb)
919{
920 struct hugetlbfs_sb_info *sbi = HUGETLBFS_SB(sb);
921
922 if (sbi) {
923 sb->s_fs_info = NULL;
David Gibson90481622012-03-21 16:34:12 -0700924
925 if (sbi->spool)
926 hugepage_put_subpool(sbi->spool);
927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 kfree(sbi);
929 }
930}
931
Christoph Hellwig96527982005-10-29 18:16:42 -0700932static inline int hugetlbfs_dec_free_inodes(struct hugetlbfs_sb_info *sbinfo)
933{
934 if (sbinfo->free_inodes >= 0) {
935 spin_lock(&sbinfo->stat_lock);
936 if (unlikely(!sbinfo->free_inodes)) {
937 spin_unlock(&sbinfo->stat_lock);
938 return 0;
939 }
940 sbinfo->free_inodes--;
941 spin_unlock(&sbinfo->stat_lock);
942 }
943
944 return 1;
945}
946
947static void hugetlbfs_inc_free_inodes(struct hugetlbfs_sb_info *sbinfo)
948{
949 if (sbinfo->free_inodes >= 0) {
950 spin_lock(&sbinfo->stat_lock);
951 sbinfo->free_inodes++;
952 spin_unlock(&sbinfo->stat_lock);
953 }
954}
955
956
Christoph Lametere18b8902006-12-06 20:33:20 -0800957static struct kmem_cache *hugetlbfs_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959static struct inode *hugetlbfs_alloc_inode(struct super_block *sb)
960{
Christoph Hellwig96527982005-10-29 18:16:42 -0700961 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 struct hugetlbfs_inode_info *p;
963
Christoph Hellwig96527982005-10-29 18:16:42 -0700964 if (unlikely(!hugetlbfs_dec_free_inodes(sbinfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 return NULL;
Christoph Lametere94b1762006-12-06 20:33:17 -0800966 p = kmem_cache_alloc(hugetlbfs_inode_cachep, GFP_KERNEL);
Christoph Hellwig96527982005-10-29 18:16:42 -0700967 if (unlikely(!p)) {
968 hugetlbfs_inc_free_inodes(sbinfo);
969 return NULL;
970 }
Mike Kravetzdd964072017-03-31 15:12:01 -0700971
972 /*
973 * Any time after allocation, hugetlbfs_destroy_inode can be called
974 * for the inode. mpol_free_shared_policy is unconditionally called
975 * as part of hugetlbfs_destroy_inode. So, initialize policy here
976 * in case of a quick call to destroy.
977 *
978 * Note that the policy is initialized even if we are creating a
979 * private inode. This simplifies hugetlbfs_destroy_inode.
980 */
981 mpol_shared_policy_init(&p->policy, NULL);
982
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 return &p->vfs_inode;
984}
985
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100986static void hugetlbfs_i_callback(struct rcu_head *head)
987{
988 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100989 kmem_cache_free(hugetlbfs_inode_cachep, HUGETLBFS_I(inode));
990}
991
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992static void hugetlbfs_destroy_inode(struct inode *inode)
993{
Christoph Hellwig96527982005-10-29 18:16:42 -0700994 hugetlbfs_inc_free_inodes(HUGETLBFS_SB(inode->i_sb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 mpol_free_shared_policy(&HUGETLBFS_I(inode)->policy);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100996 call_rcu(&inode->i_rcu, hugetlbfs_i_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997}
998
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700999static const struct address_space_operations hugetlbfs_aops = {
Nick Piggin800d15a2007-10-16 01:25:03 -07001000 .write_begin = hugetlbfs_write_begin,
1001 .write_end = hugetlbfs_write_end,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 .set_page_dirty = hugetlbfs_set_page_dirty,
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001003 .migratepage = hugetlbfs_migrate_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004};
1005
Christoph Hellwig96527982005-10-29 18:16:42 -07001006
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07001007static void init_once(void *foo)
Christoph Hellwig96527982005-10-29 18:16:42 -07001008{
1009 struct hugetlbfs_inode_info *ei = (struct hugetlbfs_inode_info *)foo;
1010
Christoph Lametera35afb82007-05-16 22:10:57 -07001011 inode_init_once(&ei->vfs_inode);
Christoph Hellwig96527982005-10-29 18:16:42 -07001012}
1013
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001014const struct file_operations hugetlbfs_file_operations = {
Al Viro34d06402015-04-03 11:31:35 -04001015 .read_iter = hugetlbfs_read_iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 .mmap = hugetlbfs_file_mmap,
Christoph Hellwig1b061d92010-05-26 17:53:41 +02001017 .fsync = noop_fsync,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 .get_unmapped_area = hugetlb_get_unmapped_area,
Mike Kravetz70c35472015-09-08 15:01:54 -07001019 .llseek = default_llseek,
1020 .fallocate = hugetlbfs_fallocate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021};
1022
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001023static const struct inode_operations hugetlbfs_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 .create = hugetlbfs_create,
1025 .lookup = simple_lookup,
1026 .link = simple_link,
1027 .unlink = simple_unlink,
1028 .symlink = hugetlbfs_symlink,
1029 .mkdir = hugetlbfs_mkdir,
1030 .rmdir = simple_rmdir,
1031 .mknod = hugetlbfs_mknod,
1032 .rename = simple_rename,
1033 .setattr = hugetlbfs_setattr,
1034};
1035
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001036static const struct inode_operations hugetlbfs_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 .setattr = hugetlbfs_setattr,
1038};
1039
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -08001040static const struct super_operations hugetlbfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 .alloc_inode = hugetlbfs_alloc_inode,
1042 .destroy_inode = hugetlbfs_destroy_inode,
Al Viro2bbbda32010-06-04 19:52:12 -04001043 .evict_inode = hugetlbfs_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 .statfs = hugetlbfs_statfs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 .put_super = hugetlbfs_put_super,
Miklos Szeredi10f19a82008-02-08 04:21:45 -08001046 .show_options = generic_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047};
1048
Mike Kravetz7ca02d02015-04-15 16:13:42 -07001049enum { NO_SIZE, SIZE_STD, SIZE_PERCENT };
1050
1051/*
1052 * Convert size option passed from command line to number of huge pages
1053 * in the pool specified by hstate. Size option could be in bytes
1054 * (val_type == SIZE_STD) or percentage of the pool (val_type == SIZE_PERCENT).
1055 */
1056static long long
1057hugetlbfs_size_to_hpages(struct hstate *h, unsigned long long size_opt,
1058 int val_type)
1059{
1060 if (val_type == NO_SIZE)
1061 return -1;
1062
1063 if (val_type == SIZE_PERCENT) {
1064 size_opt <<= huge_page_shift(h);
1065 size_opt *= h->max_huge_pages;
1066 do_div(size_opt, 100);
1067 }
1068
1069 size_opt >>= huge_page_shift(h);
1070 return size_opt;
1071}
1072
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073static int
1074hugetlbfs_parse_options(char *options, struct hugetlbfs_config *pconfig)
1075{
Randy Dunlape73a75f2007-07-15 23:40:52 -07001076 char *p, *rest;
1077 substring_t args[MAX_OPT_ARGS];
1078 int option;
Mike Kravetz7ca02d02015-04-15 16:13:42 -07001079 unsigned long long max_size_opt = 0, min_size_opt = 0;
1080 int max_val_type = NO_SIZE, min_val_type = NO_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
1082 if (!options)
1083 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
Randy Dunlape73a75f2007-07-15 23:40:52 -07001085 while ((p = strsep(&options, ",")) != NULL) {
1086 int token;
Lee Schermerhornb4c07bc2007-07-15 23:40:54 -07001087 if (!*p)
1088 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
Randy Dunlape73a75f2007-07-15 23:40:52 -07001090 token = match_token(p, tokens, args);
1091 switch (token) {
1092 case Opt_uid:
1093 if (match_int(&args[0], &option))
1094 goto bad_val;
Eric W. Biedermana0eb3a02012-02-07 16:19:25 -08001095 pconfig->uid = make_kuid(current_user_ns(), option);
1096 if (!uid_valid(pconfig->uid))
1097 goto bad_val;
Randy Dunlape73a75f2007-07-15 23:40:52 -07001098 break;
1099
1100 case Opt_gid:
1101 if (match_int(&args[0], &option))
1102 goto bad_val;
Eric W. Biedermana0eb3a02012-02-07 16:19:25 -08001103 pconfig->gid = make_kgid(current_user_ns(), option);
1104 if (!gid_valid(pconfig->gid))
1105 goto bad_val;
Randy Dunlape73a75f2007-07-15 23:40:52 -07001106 break;
1107
1108 case Opt_mode:
1109 if (match_octal(&args[0], &option))
1110 goto bad_val;
Ken Chen75897d62008-02-04 22:28:36 -08001111 pconfig->mode = option & 01777U;
Randy Dunlape73a75f2007-07-15 23:40:52 -07001112 break;
1113
1114 case Opt_size: {
Randy Dunlape73a75f2007-07-15 23:40:52 -07001115 /* memparse() will accept a K/M/G without a digit */
1116 if (!isdigit(*args[0].from))
1117 goto bad_val;
Mike Kravetz7ca02d02015-04-15 16:13:42 -07001118 max_size_opt = memparse(args[0].from, &rest);
1119 max_val_type = SIZE_STD;
Andi Kleena137e1c2008-07-23 21:27:43 -07001120 if (*rest == '%')
Mike Kravetz7ca02d02015-04-15 16:13:42 -07001121 max_val_type = SIZE_PERCENT;
Randy Dunlape73a75f2007-07-15 23:40:52 -07001122 break;
1123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
Randy Dunlape73a75f2007-07-15 23:40:52 -07001125 case Opt_nr_inodes:
1126 /* memparse() will accept a K/M/G without a digit */
1127 if (!isdigit(*args[0].from))
1128 goto bad_val;
1129 pconfig->nr_inodes = memparse(args[0].from, &rest);
1130 break;
1131
Andi Kleena137e1c2008-07-23 21:27:43 -07001132 case Opt_pagesize: {
1133 unsigned long ps;
1134 ps = memparse(args[0].from, &rest);
1135 pconfig->hstate = size_to_hstate(ps);
1136 if (!pconfig->hstate) {
Andrew Morton9b857d22014-06-04 16:07:21 -07001137 pr_err("Unsupported page size %lu MB\n",
Andi Kleena137e1c2008-07-23 21:27:43 -07001138 ps >> 20);
1139 return -EINVAL;
1140 }
1141 break;
1142 }
1143
Mike Kravetz7ca02d02015-04-15 16:13:42 -07001144 case Opt_min_size: {
1145 /* memparse() will accept a K/M/G without a digit */
1146 if (!isdigit(*args[0].from))
1147 goto bad_val;
1148 min_size_opt = memparse(args[0].from, &rest);
1149 min_val_type = SIZE_STD;
1150 if (*rest == '%')
1151 min_val_type = SIZE_PERCENT;
1152 break;
1153 }
1154
Randy Dunlape73a75f2007-07-15 23:40:52 -07001155 default:
Andrew Morton9b857d22014-06-04 16:07:21 -07001156 pr_err("Bad mount option: \"%s\"\n", p);
Lee Schermerhornb4c07bc2007-07-15 23:40:54 -07001157 return -EINVAL;
Randy Dunlape73a75f2007-07-15 23:40:52 -07001158 break;
1159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 }
Andi Kleena137e1c2008-07-23 21:27:43 -07001161
Mike Kravetz7ca02d02015-04-15 16:13:42 -07001162 /*
1163 * Use huge page pool size (in hstate) to convert the size
1164 * options to number of huge pages. If NO_SIZE, -1 is returned.
1165 */
1166 pconfig->max_hpages = hugetlbfs_size_to_hpages(pconfig->hstate,
1167 max_size_opt, max_val_type);
1168 pconfig->min_hpages = hugetlbfs_size_to_hpages(pconfig->hstate,
1169 min_size_opt, min_val_type);
1170
1171 /*
1172 * If max_size was specified, then min_size must be smaller
1173 */
1174 if (max_val_type > NO_SIZE &&
1175 pconfig->min_hpages > pconfig->max_hpages) {
1176 pr_err("minimum size can not be greater than maximum size\n");
1177 return -EINVAL;
Andi Kleena137e1c2008-07-23 21:27:43 -07001178 }
1179
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 return 0;
Randy Dunlape73a75f2007-07-15 23:40:52 -07001181
1182bad_val:
Andrew Morton9b857d22014-06-04 16:07:21 -07001183 pr_err("Bad value '%s' for mount option '%s'\n", args[0].from, p);
Akinobu Mitac12ddba2009-04-21 12:24:05 -07001184 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185}
1186
1187static int
1188hugetlbfs_fill_super(struct super_block *sb, void *data, int silent)
1189{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 int ret;
1191 struct hugetlbfs_config config;
1192 struct hugetlbfs_sb_info *sbinfo;
1193
Miklos Szeredi10f19a82008-02-08 04:21:45 -08001194 save_mount_options(sb, data);
1195
Mike Kravetz7ca02d02015-04-15 16:13:42 -07001196 config.max_hpages = -1; /* No limit on size by default */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 config.nr_inodes = -1; /* No limit on number of inodes by default */
David Howells77c70de2008-11-14 10:38:56 +11001198 config.uid = current_fsuid();
1199 config.gid = current_fsgid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 config.mode = 0755;
Andi Kleena137e1c2008-07-23 21:27:43 -07001201 config.hstate = &default_hstate;
Mike Kravetz7ca02d02015-04-15 16:13:42 -07001202 config.min_hpages = -1; /* No default minimum size */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 ret = hugetlbfs_parse_options(data, &config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 if (ret)
1205 return ret;
1206
1207 sbinfo = kmalloc(sizeof(struct hugetlbfs_sb_info), GFP_KERNEL);
1208 if (!sbinfo)
1209 return -ENOMEM;
1210 sb->s_fs_info = sbinfo;
Andi Kleena137e1c2008-07-23 21:27:43 -07001211 sbinfo->hstate = config.hstate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 spin_lock_init(&sbinfo->stat_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 sbinfo->max_inodes = config.nr_inodes;
1214 sbinfo->free_inodes = config.nr_inodes;
David Gibson90481622012-03-21 16:34:12 -07001215 sbinfo->spool = NULL;
Mike Kravetz7ca02d02015-04-15 16:13:42 -07001216 /*
1217 * Allocate and initialize subpool if maximum or minimum size is
1218 * specified. Any needed reservations (for minimim size) are taken
1219 * taken when the subpool is created.
1220 */
1221 if (config.max_hpages != -1 || config.min_hpages != -1) {
1222 sbinfo->spool = hugepage_new_subpool(config.hstate,
1223 config.max_hpages,
1224 config.min_hpages);
David Gibson90481622012-03-21 16:34:12 -07001225 if (!sbinfo->spool)
1226 goto out_free;
1227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 sb->s_maxbytes = MAX_LFS_FILESIZE;
Andi Kleena137e1c2008-07-23 21:27:43 -07001229 sb->s_blocksize = huge_page_size(config.hstate);
1230 sb->s_blocksize_bits = huge_page_shift(config.hstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 sb->s_magic = HUGETLBFS_MAGIC;
1232 sb->s_op = &hugetlbfs_ops;
1233 sb->s_time_gran = 1;
Al Viro48fde702012-01-08 22:15:13 -05001234 sb->s_root = d_make_root(hugetlbfs_get_root(sb, &config));
1235 if (!sb->s_root)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 return 0;
1238out_free:
Fabian Frederick6e6870d2014-06-04 16:10:40 -07001239 kfree(sbinfo->spool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 kfree(sbinfo);
1241 return -ENOMEM;
1242}
1243
Al Viro3c26ff62010-07-25 11:46:36 +04001244static struct dentry *hugetlbfs_mount(struct file_system_type *fs_type,
1245 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246{
Al Viro3c26ff62010-07-25 11:46:36 +04001247 return mount_nodev(fs_type, flags, data, hugetlbfs_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248}
1249
1250static struct file_system_type hugetlbfs_fs_type = {
1251 .name = "hugetlbfs",
Al Viro3c26ff62010-07-25 11:46:36 +04001252 .mount = hugetlbfs_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 .kill_sb = kill_litter_super,
1254};
1255
Andi Kleen42d73952012-12-11 16:01:34 -08001256static struct vfsmount *hugetlbfs_vfsmount[HUGE_MAX_HSTATE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
From: Mel Gormanef1ff6b2009-09-23 15:56:05 -07001258static int can_do_hugetlb_shm(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259{
Eric W. Biedermana0eb3a02012-02-07 16:19:25 -08001260 kgid_t shm_group;
1261 shm_group = make_kgid(&init_user_ns, sysctl_hugetlb_shm_group);
1262 return capable(CAP_IPC_LOCK) || in_group_p(shm_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263}
1264
Andi Kleen42d73952012-12-11 16:01:34 -08001265static int get_hstate_idx(int page_size_log)
1266{
Naoya Horiguchiaf73e4d2013-05-07 16:18:13 -07001267 struct hstate *h = hstate_sizelog(page_size_log);
Andi Kleen42d73952012-12-11 16:01:34 -08001268
Andi Kleen42d73952012-12-11 16:01:34 -08001269 if (!h)
1270 return -1;
1271 return h - hstates;
1272}
1273
Fabian Frederickbe1d2cf2014-06-04 16:10:39 -07001274static const struct dentry_operations anon_ops = {
Al Viro118b2302013-08-24 12:08:17 -04001275 .d_dname = simple_dname
Al Viro0df4d6e2013-02-14 22:39:53 -05001276};
1277
Naoya Horiguchiaf73e4d2013-05-07 16:18:13 -07001278/*
1279 * Note that size should be aligned to proper hugepage size in caller side,
1280 * otherwise hugetlb_reserve_pages reserves one less hugepages than intended.
1281 */
1282struct file *hugetlb_file_setup(const char *name, size_t size,
1283 vm_flags_t acctflag, struct user_struct **user,
Andi Kleen42d73952012-12-11 16:01:34 -08001284 int creat_flags, int page_size_log)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285{
Anatol Pomozov39b65252012-09-12 20:11:55 -07001286 struct file *file = ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 struct inode *inode;
Al Viro2c48b9c2009-08-09 00:52:35 +04001288 struct path path;
Al Viro0df4d6e2013-02-14 22:39:53 -05001289 struct super_block *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 struct qstr quick_string;
Andi Kleen42d73952012-12-11 16:01:34 -08001291 int hstate_idx;
1292
1293 hstate_idx = get_hstate_idx(page_size_log);
1294 if (hstate_idx < 0)
1295 return ERR_PTR(-ENODEV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Hugh Dickins353d5c32009-08-24 16:30:28 +01001297 *user = NULL;
Andi Kleen42d73952012-12-11 16:01:34 -08001298 if (!hugetlbfs_vfsmount[hstate_idx])
Akinobu Mita5bc98592007-05-06 14:50:18 -07001299 return ERR_PTR(-ENOENT);
1300
From: Mel Gormanef1ff6b2009-09-23 15:56:05 -07001301 if (creat_flags == HUGETLB_SHMFS_INODE && !can_do_hugetlb_shm()) {
Hugh Dickins353d5c32009-08-24 16:30:28 +01001302 *user = current_user();
1303 if (user_shm_lock(size, *user)) {
David Rientjes21a3c272012-03-21 16:34:13 -07001304 task_lock(current);
Andrew Morton9b857d22014-06-04 16:07:21 -07001305 pr_warn_once("%s (%d): Using mlock ulimits for SHM_HUGETLB is deprecated\n",
David Rientjes21a3c272012-03-21 16:34:13 -07001306 current->comm, current->pid);
1307 task_unlock(current);
Hugh Dickins353d5c32009-08-24 16:30:28 +01001308 } else {
1309 *user = NULL;
Ravikiran G Thirumalai2584e512009-03-31 15:21:26 -07001310 return ERR_PTR(-EPERM);
Hugh Dickins353d5c32009-08-24 16:30:28 +01001311 }
Ravikiran G Thirumalai2584e512009-03-31 15:21:26 -07001312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
Al Viro0df4d6e2013-02-14 22:39:53 -05001314 sb = hugetlbfs_vfsmount[hstate_idx]->mnt_sb;
Eric W. Biederman9d665862007-06-16 10:16:16 -07001315 quick_string.name = name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 quick_string.len = strlen(quick_string.name);
1317 quick_string.hash = 0;
Al Viro0df4d6e2013-02-14 22:39:53 -05001318 path.dentry = d_alloc_pseudo(sb, &quick_string);
Al Viro2c48b9c2009-08-09 00:52:35 +04001319 if (!path.dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 goto out_shm_unlock;
1321
Al Viro0df4d6e2013-02-14 22:39:53 -05001322 d_set_d_op(path.dentry, &anon_ops);
Andi Kleen42d73952012-12-11 16:01:34 -08001323 path.mnt = mntget(hugetlbfs_vfsmount[hstate_idx]);
Anatol Pomozov39b65252012-09-12 20:11:55 -07001324 file = ERR_PTR(-ENOSPC);
Al Viro0df4d6e2013-02-14 22:39:53 -05001325 inode = hugetlbfs_get_inode(sb, NULL, S_IFREG | S_IRWXUGO, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 if (!inode)
Dave Hansence8d2cd2007-10-16 23:31:13 -07001327 goto out_dentry;
Stephen Smalleye1832f22015-08-06 15:46:55 -07001328 if (creat_flags == HUGETLB_SHMFS_INODE)
1329 inode->i_flags |= S_PRIVATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
Anatol Pomozov39b65252012-09-12 20:11:55 -07001331 file = ERR_PTR(-ENOMEM);
Naoya Horiguchiaf73e4d2013-05-07 16:18:13 -07001332 if (hugetlb_reserve_pages(inode, 0,
1333 size >> huge_page_shift(hstate_inode(inode)), NULL,
1334 acctflag))
David Gibsonb45b5bd2006-03-22 00:08:55 -08001335 goto out_inode;
1336
Al Viro2c48b9c2009-08-09 00:52:35 +04001337 d_instantiate(path.dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 inode->i_size = size;
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +02001339 clear_nlink(inode);
Dave Hansence8d2cd2007-10-16 23:31:13 -07001340
Al Viro2c48b9c2009-08-09 00:52:35 +04001341 file = alloc_file(&path, FMODE_WRITE | FMODE_READ,
Dave Hansence8d2cd2007-10-16 23:31:13 -07001342 &hugetlbfs_file_operations);
Anatol Pomozov39b65252012-09-12 20:11:55 -07001343 if (IS_ERR(file))
Al Virob4d232e2008-02-23 05:59:19 -05001344 goto out_dentry; /* inode is already attached */
Dave Hansence8d2cd2007-10-16 23:31:13 -07001345
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 return file;
1347
David Gibsonb45b5bd2006-03-22 00:08:55 -08001348out_inode:
1349 iput(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350out_dentry:
Al Viro2c48b9c2009-08-09 00:52:35 +04001351 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352out_shm_unlock:
Hugh Dickins353d5c32009-08-24 16:30:28 +01001353 if (*user) {
1354 user_shm_unlock(size, *user);
1355 *user = NULL;
1356 }
Anatol Pomozov39b65252012-09-12 20:11:55 -07001357 return file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358}
1359
1360static int __init init_hugetlbfs_fs(void)
1361{
Andi Kleen42d73952012-12-11 16:01:34 -08001362 struct hstate *h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 int error;
Andi Kleen42d73952012-12-11 16:01:34 -08001364 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
Nishanth Aravamudan457c1b22014-05-06 12:50:00 -07001366 if (!hugepages_supported()) {
Andrew Morton9b857d22014-06-04 16:07:21 -07001367 pr_info("disabling because there are no supported hugepage sizes\n");
Nishanth Aravamudan457c1b22014-05-06 12:50:00 -07001368 return -ENOTSUPP;
1369 }
1370
Hillf Dantond1d5e052012-03-21 16:34:15 -07001371 error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 hugetlbfs_inode_cachep = kmem_cache_create("hugetlbfs_inode_cache",
1373 sizeof(struct hugetlbfs_inode_info),
Vladimir Davydov5d097052016-01-14 15:18:21 -08001374 0, SLAB_ACCOUNT, init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 if (hugetlbfs_inode_cachep == NULL)
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -07001376 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
1378 error = register_filesystem(&hugetlbfs_fs_type);
1379 if (error)
1380 goto out;
1381
Andi Kleen42d73952012-12-11 16:01:34 -08001382 i = 0;
1383 for_each_hstate(h) {
1384 char buf[50];
1385 unsigned ps_kb = 1U << (h->order + PAGE_SHIFT - 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
Andi Kleen42d73952012-12-11 16:01:34 -08001387 snprintf(buf, sizeof(buf), "pagesize=%uK", ps_kb);
1388 hugetlbfs_vfsmount[i] = kern_mount_data(&hugetlbfs_fs_type,
1389 buf);
1390
1391 if (IS_ERR(hugetlbfs_vfsmount[i])) {
Andrew Morton9b857d22014-06-04 16:07:21 -07001392 pr_err("Cannot mount internal hugetlbfs for "
Andi Kleen42d73952012-12-11 16:01:34 -08001393 "page size %uK", ps_kb);
1394 error = PTR_ERR(hugetlbfs_vfsmount[i]);
1395 hugetlbfs_vfsmount[i] = NULL;
1396 }
1397 i++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 }
Andi Kleen42d73952012-12-11 16:01:34 -08001399 /* Non default hstates are optional */
1400 if (!IS_ERR_OR_NULL(hugetlbfs_vfsmount[default_hstate_idx]))
1401 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
1403 out:
Hillf Dantond1d5e052012-03-21 16:34:15 -07001404 kmem_cache_destroy(hugetlbfs_inode_cachep);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -07001405 out2:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 return error;
1407}
Paul Gortmaker3e89e1c2016-01-14 15:21:52 -08001408fs_initcall(init_hugetlbfs_fs)