blob: 253b03451b727650ade31e262d1351e14312079c [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 Kravetzf0539c72019-05-13 17:19:41 -0700454 hash = hugetlb_fault_mutex_hash(h, mapping, next, 0);
Mike Kravetzb5cec282015-09-08 15:01:41 -0700455 mutex_lock(&hugetlb_fault_mutex_table[hash]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Mike Kravetz4aae8d12016-01-15 16:57:40 -0800457 /*
458 * If page is mapped, it was faulted in after being
459 * unmapped in caller. Unmap (again) now after taking
460 * the fault mutex. The mutex will prevent faults
461 * until we finish removing the page.
462 *
463 * This race can only happen in the hole punch case.
464 * Getting here in a truncate operation is a bug.
465 */
466 if (unlikely(page_mapped(page))) {
Mike Kravetz18178892015-11-20 15:57:13 -0800467 BUG_ON(truncate_op);
Mike Kravetz4aae8d12016-01-15 16:57:40 -0800468
469 i_mmap_lock_write(mapping);
470 hugetlb_vmdelete_list(&mapping->i_mmap,
471 next * pages_per_huge_page(h),
472 (next + 1) * pages_per_huge_page(h));
473 i_mmap_unlock_write(mapping);
474 }
475
476 lock_page(page);
477 /*
478 * We must free the huge page and remove from page
479 * cache (remove_huge_page) BEFORE removing the
480 * region/reserve map (hugetlb_unreserve_pages). In
481 * rare out of memory conditions, removal of the
zhong jiang72e29362016-10-07 17:02:01 -0700482 * region/reserve map could fail. Correspondingly,
483 * the subpool and global reserve usage count can need
484 * to be adjusted.
Mike Kravetz4aae8d12016-01-15 16:57:40 -0800485 */
zhong jiang72e29362016-10-07 17:02:01 -0700486 VM_BUG_ON(PagePrivate(page));
Mike Kravetz4aae8d12016-01-15 16:57:40 -0800487 remove_huge_page(page);
488 freed++;
489 if (!truncate_op) {
490 if (unlikely(hugetlb_unreserve_pages(inode,
491 next, next + 1, 1)))
zhong jiang72e29362016-10-07 17:02:01 -0700492 hugetlb_fix_reserve_counts(inode);
Mike Kravetzb5cec282015-09-08 15:01:41 -0700493 }
494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 unlock_page(page);
Mike Kravetzb5cec282015-09-08 15:01:41 -0700496 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 }
Mike Kravetz18178892015-11-20 15:57:13 -0800498 ++next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 huge_pagevec_release(&pvec);
Mike Kravetz18178892015-11-20 15:57:13 -0800500 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 }
Mike Kravetzb5cec282015-09-08 15:01:41 -0700502
503 if (truncate_op)
504 (void)hugetlb_unreserve_pages(inode, start, LONG_MAX, freed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505}
506
Al Viro2bbbda32010-06-04 19:52:12 -0400507static void hugetlbfs_evict_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
Joonsoo Kim9119a412014-04-03 14:47:25 -0700509 struct resv_map *resv_map;
510
Mike Kravetzb5cec282015-09-08 15:01:41 -0700511 remove_inode_hugepages(inode, 0, LLONG_MAX);
Joonsoo Kim9119a412014-04-03 14:47:25 -0700512 resv_map = (struct resv_map *)inode->i_mapping->private_data;
513 /* root inode doesn't have the resv_map, so we should check it */
514 if (resv_map)
515 resv_map_release(&resv_map->refs);
Jan Karadbd57682012-05-03 14:48:02 +0200516 clear_inode(inode);
Christoph Hellwig149f4212005-10-29 18:16:43 -0700517}
518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519static int hugetlb_vmtruncate(struct inode *inode, loff_t offset)
520{
Hugh Dickins856fc292006-10-28 10:38:43 -0700521 pgoff_t pgoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 struct address_space *mapping = inode->i_mapping;
Andi Kleena5516432008-07-23 21:27:41 -0700523 struct hstate *h = hstate_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Andi Kleena5516432008-07-23 21:27:41 -0700525 BUG_ON(offset & ~huge_page_mask(h));
Hugh Dickins856fc292006-10-28 10:38:43 -0700526 pgoff = offset >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Ken Chen7aa91e12007-10-16 01:26:21 -0700528 i_size_write(inode, offset);
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -0800529 i_mmap_lock_write(mapping);
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700530 if (!RB_EMPTY_ROOT(&mapping->i_mmap))
Mike Kravetz1bfad992015-09-08 15:01:38 -0700531 hugetlb_vmdelete_list(&mapping->i_mmap, pgoff, 0);
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -0800532 i_mmap_unlock_write(mapping);
Mike Kravetzb5cec282015-09-08 15:01:41 -0700533 remove_inode_hugepages(inode, offset, LLONG_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 return 0;
535}
536
Mike Kravetz70c35472015-09-08 15:01:54 -0700537static long hugetlbfs_punch_hole(struct inode *inode, loff_t offset, loff_t len)
538{
539 struct hstate *h = hstate_inode(inode);
540 loff_t hpage_size = huge_page_size(h);
541 loff_t hole_start, hole_end;
542
543 /*
544 * For hole punch round up the beginning offset of the hole and
545 * round down the end.
546 */
547 hole_start = round_up(offset, hpage_size);
548 hole_end = round_down(offset + len, hpage_size);
549
550 if (hole_end > hole_start) {
551 struct address_space *mapping = inode->i_mapping;
552
Al Viro59551022016-01-22 15:40:57 -0500553 inode_lock(inode);
Mike Kravetz70c35472015-09-08 15:01:54 -0700554 i_mmap_lock_write(mapping);
555 if (!RB_EMPTY_ROOT(&mapping->i_mmap))
556 hugetlb_vmdelete_list(&mapping->i_mmap,
557 hole_start >> PAGE_SHIFT,
558 hole_end >> PAGE_SHIFT);
559 i_mmap_unlock_write(mapping);
560 remove_inode_hugepages(inode, hole_start, hole_end);
Al Viro59551022016-01-22 15:40:57 -0500561 inode_unlock(inode);
Mike Kravetz70c35472015-09-08 15:01:54 -0700562 }
563
564 return 0;
565}
566
567static long hugetlbfs_fallocate(struct file *file, int mode, loff_t offset,
568 loff_t len)
569{
570 struct inode *inode = file_inode(file);
571 struct address_space *mapping = inode->i_mapping;
572 struct hstate *h = hstate_inode(inode);
573 struct vm_area_struct pseudo_vma;
Mike Kravetz70c35472015-09-08 15:01:54 -0700574 loff_t hpage_size = huge_page_size(h);
575 unsigned long hpage_shift = huge_page_shift(h);
576 pgoff_t start, index, end;
577 int error;
578 u32 hash;
579
580 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
581 return -EOPNOTSUPP;
582
583 if (mode & FALLOC_FL_PUNCH_HOLE)
584 return hugetlbfs_punch_hole(inode, offset, len);
585
586 /*
587 * Default preallocate case.
588 * For this range, start is rounded down and end is rounded up
589 * as well as being converted to page offsets.
590 */
591 start = offset >> hpage_shift;
592 end = (offset + len + hpage_size - 1) >> hpage_shift;
593
Al Viro59551022016-01-22 15:40:57 -0500594 inode_lock(inode);
Mike Kravetz70c35472015-09-08 15:01:54 -0700595
596 /* We need to check rlimit even when FALLOC_FL_KEEP_SIZE */
597 error = inode_newsize_ok(inode, offset + len);
598 if (error)
599 goto out;
600
601 /*
602 * Initialize a pseudo vma as this is required by the huge page
603 * allocation routines. If NUMA is configured, use page index
604 * as input to create an allocation policy.
605 */
606 memset(&pseudo_vma, 0, sizeof(struct vm_area_struct));
607 pseudo_vma.vm_flags = (VM_HUGETLB | VM_MAYSHARE | VM_SHARED);
608 pseudo_vma.vm_file = file;
609
610 for (index = start; index < end; index++) {
611 /*
612 * This is supposed to be the vaddr where the page is being
613 * faulted in, but we have no vaddr here.
614 */
615 struct page *page;
616 unsigned long addr;
617 int avoid_reserve = 0;
618
619 cond_resched();
620
621 /*
622 * fallocate(2) manpage permits EINTR; we may have been
623 * interrupted because we are using up too much memory.
624 */
625 if (signal_pending(current)) {
626 error = -EINTR;
627 break;
628 }
629
630 /* Set numa allocation policy based on index */
631 hugetlb_set_vma_policy(&pseudo_vma, inode, index);
632
633 /* addr is the offset within the file (zero based) */
634 addr = index * hpage_size;
635
636 /* mutex taken here, fault path and hole punch */
Mike Kravetzf0539c72019-05-13 17:19:41 -0700637 hash = hugetlb_fault_mutex_hash(h, mapping, index, addr);
Mike Kravetz70c35472015-09-08 15:01:54 -0700638 mutex_lock(&hugetlb_fault_mutex_table[hash]);
639
640 /* See if already present in mapping to avoid alloc/free */
641 page = find_get_page(mapping, index);
642 if (page) {
643 put_page(page);
644 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
645 hugetlb_drop_vma_policy(&pseudo_vma);
646 continue;
647 }
648
649 /* Allocate page and add to page cache */
650 page = alloc_huge_page(&pseudo_vma, addr, avoid_reserve);
651 hugetlb_drop_vma_policy(&pseudo_vma);
652 if (IS_ERR(page)) {
653 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
654 error = PTR_ERR(page);
655 goto out;
656 }
657 clear_huge_page(page, addr, pages_per_huge_page(h));
658 __SetPageUptodate(page);
659 error = huge_add_to_page_cache(page, mapping, index);
660 if (unlikely(error)) {
661 put_page(page);
662 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
663 goto out;
664 }
665
666 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
667
668 /*
669 * page_put due to reference from alloc_huge_page()
670 * unlock_page because locked by add_to_page_cache()
671 */
672 put_page(page);
673 unlock_page(page);
674 }
675
676 if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size)
677 i_size_write(inode, offset + len);
Deepa Dinamani078cd822016-09-14 07:48:04 -0700678 inode->i_ctime = current_time(inode);
Mike Kravetz70c35472015-09-08 15:01:54 -0700679out:
Al Viro59551022016-01-22 15:40:57 -0500680 inode_unlock(inode);
Mike Kravetz70c35472015-09-08 15:01:54 -0700681 return error;
682}
683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684static int hugetlbfs_setattr(struct dentry *dentry, struct iattr *attr)
685{
David Howells2b0143b2015-03-17 22:25:59 +0000686 struct inode *inode = d_inode(dentry);
Andi Kleena5516432008-07-23 21:27:41 -0700687 struct hstate *h = hstate_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 int error;
689 unsigned int ia_valid = attr->ia_valid;
690
691 BUG_ON(!inode);
692
Jan Kara31051c82016-05-26 16:55:18 +0200693 error = setattr_prepare(dentry, attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 if (error)
Christoph Hellwig10257742010-06-04 11:30:02 +0200695 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697 if (ia_valid & ATTR_SIZE) {
698 error = -EINVAL;
Christoph Hellwig10257742010-06-04 11:30:02 +0200699 if (attr->ia_size & ~huge_page_mask(h))
700 return -EINVAL;
701 error = hugetlb_vmtruncate(inode, attr->ia_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 if (error)
Christoph Hellwig10257742010-06-04 11:30:02 +0200703 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 }
Christoph Hellwig10257742010-06-04 11:30:02 +0200705
706 setattr_copy(inode, attr);
707 mark_inode_dirty(inode);
708 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709}
710
Al Viro7d54fa62011-07-24 20:20:48 -0400711static struct inode *hugetlbfs_get_root(struct super_block *sb,
712 struct hugetlbfs_config *config)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
714 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
716 inode = new_inode(sb);
717 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400718 inode->i_ino = get_next_ino();
Al Viro7d54fa62011-07-24 20:20:48 -0400719 inode->i_mode = S_IFDIR | config->mode;
720 inode->i_uid = config->uid;
721 inode->i_gid = config->gid;
Deepa Dinamani078cd822016-09-14 07:48:04 -0700722 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
Al Viro7d54fa62011-07-24 20:20:48 -0400723 inode->i_op = &hugetlbfs_dir_inode_operations;
724 inode->i_fop = &simple_dir_operations;
725 /* directory inodes start off with i_nlink == 2 (for "." entry) */
726 inc_nlink(inode);
Aneesh Kumar K.V65ed7602012-04-25 16:01:50 -0700727 lockdep_annotate_inode_mutex_key(inode);
Al Viro7d54fa62011-07-24 20:20:48 -0400728 }
729 return inode;
730}
731
Michal Hockob610ded2013-08-13 16:00:55 -0700732/*
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -0800733 * Hugetlbfs is not reclaimable; therefore its i_mmap_rwsem will never
Michal Hockob610ded2013-08-13 16:00:55 -0700734 * be taken from reclaim -- unlike regular filesystems. This needs an
Kirill A. Shutemov88f306b2016-01-15 16:57:31 -0800735 * annotation because huge_pmd_share() does an allocation under hugetlb's
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -0800736 * i_mmap_rwsem.
Michal Hockob610ded2013-08-13 16:00:55 -0700737 */
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -0800738static struct lock_class_key hugetlbfs_i_mmap_rwsem_key;
Michal Hockob610ded2013-08-13 16:00:55 -0700739
Al Viro7d54fa62011-07-24 20:20:48 -0400740static struct inode *hugetlbfs_get_inode(struct super_block *sb,
741 struct inode *dir,
Al Viro18df2252011-07-24 23:17:40 -0400742 umode_t mode, dev_t dev)
Al Viro7d54fa62011-07-24 20:20:48 -0400743{
744 struct inode *inode;
Mike Kravetzd57e4ae2019-04-05 18:39:06 -0700745 struct resv_map *resv_map = NULL;
Joonsoo Kim9119a412014-04-03 14:47:25 -0700746
Mike Kravetzd57e4ae2019-04-05 18:39:06 -0700747 /*
748 * Reserve maps are only needed for inodes that can have associated
749 * page allocations.
750 */
751 if (S_ISREG(mode) || S_ISLNK(mode)) {
752 resv_map = resv_map_alloc();
753 if (!resv_map)
754 return NULL;
755 }
Al Viro7d54fa62011-07-24 20:20:48 -0400756
757 inode = new_inode(sb);
758 if (inode) {
Al Viro7d54fa62011-07-24 20:20:48 -0400759 inode->i_ino = get_next_ino();
760 inode_init_owner(inode, dir, mode);
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -0800761 lockdep_set_class(&inode->i_mapping->i_mmap_rwsem,
762 &hugetlbfs_i_mmap_rwsem_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 inode->i_mapping->a_ops = &hugetlbfs_aops;
Deepa Dinamani078cd822016-09-14 07:48:04 -0700764 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
Joonsoo Kim9119a412014-04-03 14:47:25 -0700765 inode->i_mapping->private_data = resv_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 switch (mode & S_IFMT) {
767 default:
768 init_special_inode(inode, mode, dev);
769 break;
770 case S_IFREG:
771 inode->i_op = &hugetlbfs_inode_operations;
772 inode->i_fop = &hugetlbfs_file_operations;
773 break;
774 case S_IFDIR:
775 inode->i_op = &hugetlbfs_dir_inode_operations;
776 inode->i_fop = &simple_dir_operations;
777
778 /* directory inodes start off with i_nlink == 2 (for "." entry) */
Dave Hansend8c76e62006-09-30 23:29:04 -0700779 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 break;
781 case S_IFLNK:
782 inode->i_op = &page_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -0500783 inode_nohighmem(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 break;
785 }
Josh Boyere096d0c2011-08-25 07:48:12 -0400786 lockdep_annotate_inode_mutex_key(inode);
Mike Kravetzd57e4ae2019-04-05 18:39:06 -0700787 } else {
788 if (resv_map)
789 kref_put(&resv_map->refs, resv_map_release);
790 }
Joonsoo Kim9119a412014-04-03 14:47:25 -0700791
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 return inode;
793}
794
795/*
796 * File creation. Allocate an inode, and we're done..
797 */
798static int hugetlbfs_mknod(struct inode *dir,
Al Viro1a67aaf2011-07-26 01:52:52 -0400799 struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
801 struct inode *inode;
802 int error = -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Al Viro7d54fa62011-07-24 20:20:48 -0400804 inode = hugetlbfs_get_inode(dir->i_sb, dir, mode, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 if (inode) {
Deepa Dinamani078cd822016-09-14 07:48:04 -0700806 dir->i_ctime = dir->i_mtime = current_time(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 d_instantiate(dentry, inode);
808 dget(dentry); /* Extra count - pin the dentry in core */
809 error = 0;
810 }
811 return error;
812}
813
Al Viro18bb1db2011-07-26 01:41:39 -0400814static int hugetlbfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
816 int retval = hugetlbfs_mknod(dir, dentry, mode | S_IFDIR, 0);
817 if (!retval)
Dave Hansend8c76e62006-09-30 23:29:04 -0700818 inc_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 return retval;
820}
821
Al Viroebfc3b42012-06-10 18:05:36 -0400822static int hugetlbfs_create(struct inode *dir, struct dentry *dentry, umode_t mode, bool excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
824 return hugetlbfs_mknod(dir, dentry, mode | S_IFREG, 0);
825}
826
827static int hugetlbfs_symlink(struct inode *dir,
828 struct dentry *dentry, const char *symname)
829{
830 struct inode *inode;
831 int error = -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Al Viro7d54fa62011-07-24 20:20:48 -0400833 inode = hugetlbfs_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 if (inode) {
835 int l = strlen(symname)+1;
836 error = page_symlink(inode, symname, l);
837 if (!error) {
838 d_instantiate(dentry, inode);
839 dget(dentry);
840 } else
841 iput(inode);
842 }
Deepa Dinamani078cd822016-09-14 07:48:04 -0700843 dir->i_ctime = dir->i_mtime = current_time(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
845 return error;
846}
847
848/*
Ken Chen6649a382007-02-08 14:20:27 -0800849 * mark the head page dirty
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 */
851static int hugetlbfs_set_page_dirty(struct page *page)
852{
Christoph Lameterd85f3382007-05-06 14:49:39 -0700853 struct page *head = compound_head(page);
Ken Chen6649a382007-02-08 14:20:27 -0800854
855 SetPageDirty(head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 return 0;
857}
858
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900859static int hugetlbfs_migrate_page(struct address_space *mapping,
Mel Gormanb969c4a2012-01-12 17:19:34 -0800860 struct page *newpage, struct page *page,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800861 enum migrate_mode mode)
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900862{
863 int rc;
864
865 rc = migrate_huge_page_move_mapping(mapping, newpage, page);
Rafael Aquini78bd5202012-12-11 16:02:31 -0800866 if (rc != MIGRATEPAGE_SUCCESS)
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900867 return rc;
Mike Kravetzb010e032019-02-28 16:22:02 -0800868
869 /*
870 * page_private is subpool pointer in hugetlb pages. Transfer to
871 * new page. PagePrivate is not associated with page_private for
872 * hugetlb pages and can not be set here as only page_huge_active
873 * pages can be migrated.
874 */
875 if (page_private(page)) {
876 set_page_private(newpage, page_private(page));
877 set_page_private(page, 0);
878 }
879
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900880 migrate_page_copy(newpage, page);
881
Rafael Aquini78bd5202012-12-11 16:02:31 -0800882 return MIGRATEPAGE_SUCCESS;
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900883}
884
David Howells726c3342006-06-23 02:02:58 -0700885static int hugetlbfs_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886{
David Howells726c3342006-06-23 02:02:58 -0700887 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(dentry->d_sb);
David Howells2b0143b2015-03-17 22:25:59 +0000888 struct hstate *h = hstate_inode(d_inode(dentry));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
890 buf->f_type = HUGETLBFS_MAGIC;
Andi Kleena5516432008-07-23 21:27:41 -0700891 buf->f_bsize = huge_page_size(h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 if (sbinfo) {
893 spin_lock(&sbinfo->stat_lock);
David Gibson74a8a652005-11-21 21:32:24 -0800894 /* If no limits set, just report 0 for max/free/used
895 * blocks, like simple_statfs() */
David Gibson90481622012-03-21 16:34:12 -0700896 if (sbinfo->spool) {
897 long free_pages;
898
899 spin_lock(&sbinfo->spool->lock);
900 buf->f_blocks = sbinfo->spool->max_hpages;
901 free_pages = sbinfo->spool->max_hpages
902 - sbinfo->spool->used_hpages;
903 buf->f_bavail = buf->f_bfree = free_pages;
904 spin_unlock(&sbinfo->spool->lock);
David Gibson74a8a652005-11-21 21:32:24 -0800905 buf->f_files = sbinfo->max_inodes;
906 buf->f_ffree = sbinfo->free_inodes;
907 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 spin_unlock(&sbinfo->stat_lock);
909 }
910 buf->f_namelen = NAME_MAX;
911 return 0;
912}
913
914static void hugetlbfs_put_super(struct super_block *sb)
915{
916 struct hugetlbfs_sb_info *sbi = HUGETLBFS_SB(sb);
917
918 if (sbi) {
919 sb->s_fs_info = NULL;
David Gibson90481622012-03-21 16:34:12 -0700920
921 if (sbi->spool)
922 hugepage_put_subpool(sbi->spool);
923
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 kfree(sbi);
925 }
926}
927
Christoph Hellwig96527982005-10-29 18:16:42 -0700928static inline int hugetlbfs_dec_free_inodes(struct hugetlbfs_sb_info *sbinfo)
929{
930 if (sbinfo->free_inodes >= 0) {
931 spin_lock(&sbinfo->stat_lock);
932 if (unlikely(!sbinfo->free_inodes)) {
933 spin_unlock(&sbinfo->stat_lock);
934 return 0;
935 }
936 sbinfo->free_inodes--;
937 spin_unlock(&sbinfo->stat_lock);
938 }
939
940 return 1;
941}
942
943static void hugetlbfs_inc_free_inodes(struct hugetlbfs_sb_info *sbinfo)
944{
945 if (sbinfo->free_inodes >= 0) {
946 spin_lock(&sbinfo->stat_lock);
947 sbinfo->free_inodes++;
948 spin_unlock(&sbinfo->stat_lock);
949 }
950}
951
952
Christoph Lametere18b8902006-12-06 20:33:20 -0800953static struct kmem_cache *hugetlbfs_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
955static struct inode *hugetlbfs_alloc_inode(struct super_block *sb)
956{
Christoph Hellwig96527982005-10-29 18:16:42 -0700957 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 struct hugetlbfs_inode_info *p;
959
Christoph Hellwig96527982005-10-29 18:16:42 -0700960 if (unlikely(!hugetlbfs_dec_free_inodes(sbinfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 return NULL;
Christoph Lametere94b1762006-12-06 20:33:17 -0800962 p = kmem_cache_alloc(hugetlbfs_inode_cachep, GFP_KERNEL);
Christoph Hellwig96527982005-10-29 18:16:42 -0700963 if (unlikely(!p)) {
964 hugetlbfs_inc_free_inodes(sbinfo);
965 return NULL;
966 }
Mike Kravetzdd964072017-03-31 15:12:01 -0700967
968 /*
969 * Any time after allocation, hugetlbfs_destroy_inode can be called
970 * for the inode. mpol_free_shared_policy is unconditionally called
971 * as part of hugetlbfs_destroy_inode. So, initialize policy here
972 * in case of a quick call to destroy.
973 *
974 * Note that the policy is initialized even if we are creating a
975 * private inode. This simplifies hugetlbfs_destroy_inode.
976 */
977 mpol_shared_policy_init(&p->policy, NULL);
978
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 return &p->vfs_inode;
980}
981
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100982static void hugetlbfs_i_callback(struct rcu_head *head)
983{
984 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100985 kmem_cache_free(hugetlbfs_inode_cachep, HUGETLBFS_I(inode));
986}
987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988static void hugetlbfs_destroy_inode(struct inode *inode)
989{
Christoph Hellwig96527982005-10-29 18:16:42 -0700990 hugetlbfs_inc_free_inodes(HUGETLBFS_SB(inode->i_sb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 mpol_free_shared_policy(&HUGETLBFS_I(inode)->policy);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100992 call_rcu(&inode->i_rcu, hugetlbfs_i_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993}
994
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700995static const struct address_space_operations hugetlbfs_aops = {
Nick Piggin800d15a2007-10-16 01:25:03 -0700996 .write_begin = hugetlbfs_write_begin,
997 .write_end = hugetlbfs_write_end,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 .set_page_dirty = hugetlbfs_set_page_dirty,
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900999 .migratepage = hugetlbfs_migrate_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000};
1001
Christoph Hellwig96527982005-10-29 18:16:42 -07001002
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07001003static void init_once(void *foo)
Christoph Hellwig96527982005-10-29 18:16:42 -07001004{
1005 struct hugetlbfs_inode_info *ei = (struct hugetlbfs_inode_info *)foo;
1006
Christoph Lametera35afb82007-05-16 22:10:57 -07001007 inode_init_once(&ei->vfs_inode);
Christoph Hellwig96527982005-10-29 18:16:42 -07001008}
1009
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001010const struct file_operations hugetlbfs_file_operations = {
Al Viro34d06402015-04-03 11:31:35 -04001011 .read_iter = hugetlbfs_read_iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 .mmap = hugetlbfs_file_mmap,
Christoph Hellwig1b061d92010-05-26 17:53:41 +02001013 .fsync = noop_fsync,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 .get_unmapped_area = hugetlb_get_unmapped_area,
Mike Kravetz70c35472015-09-08 15:01:54 -07001015 .llseek = default_llseek,
1016 .fallocate = hugetlbfs_fallocate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017};
1018
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001019static const struct inode_operations hugetlbfs_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 .create = hugetlbfs_create,
1021 .lookup = simple_lookup,
1022 .link = simple_link,
1023 .unlink = simple_unlink,
1024 .symlink = hugetlbfs_symlink,
1025 .mkdir = hugetlbfs_mkdir,
1026 .rmdir = simple_rmdir,
1027 .mknod = hugetlbfs_mknod,
1028 .rename = simple_rename,
1029 .setattr = hugetlbfs_setattr,
1030};
1031
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001032static const struct inode_operations hugetlbfs_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 .setattr = hugetlbfs_setattr,
1034};
1035
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -08001036static const struct super_operations hugetlbfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 .alloc_inode = hugetlbfs_alloc_inode,
1038 .destroy_inode = hugetlbfs_destroy_inode,
Al Viro2bbbda32010-06-04 19:52:12 -04001039 .evict_inode = hugetlbfs_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 .statfs = hugetlbfs_statfs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 .put_super = hugetlbfs_put_super,
Miklos Szeredi10f19a82008-02-08 04:21:45 -08001042 .show_options = generic_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043};
1044
Mike Kravetz7ca02d02015-04-15 16:13:42 -07001045enum { NO_SIZE, SIZE_STD, SIZE_PERCENT };
1046
1047/*
1048 * Convert size option passed from command line to number of huge pages
1049 * in the pool specified by hstate. Size option could be in bytes
1050 * (val_type == SIZE_STD) or percentage of the pool (val_type == SIZE_PERCENT).
1051 */
1052static long long
1053hugetlbfs_size_to_hpages(struct hstate *h, unsigned long long size_opt,
1054 int val_type)
1055{
1056 if (val_type == NO_SIZE)
1057 return -1;
1058
1059 if (val_type == SIZE_PERCENT) {
1060 size_opt <<= huge_page_shift(h);
1061 size_opt *= h->max_huge_pages;
1062 do_div(size_opt, 100);
1063 }
1064
1065 size_opt >>= huge_page_shift(h);
1066 return size_opt;
1067}
1068
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069static int
1070hugetlbfs_parse_options(char *options, struct hugetlbfs_config *pconfig)
1071{
Randy Dunlape73a75f2007-07-15 23:40:52 -07001072 char *p, *rest;
1073 substring_t args[MAX_OPT_ARGS];
1074 int option;
Mike Kravetz7ca02d02015-04-15 16:13:42 -07001075 unsigned long long max_size_opt = 0, min_size_opt = 0;
1076 int max_val_type = NO_SIZE, min_val_type = NO_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
1078 if (!options)
1079 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
Randy Dunlape73a75f2007-07-15 23:40:52 -07001081 while ((p = strsep(&options, ",")) != NULL) {
1082 int token;
Lee Schermerhornb4c07bc2007-07-15 23:40:54 -07001083 if (!*p)
1084 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
Randy Dunlape73a75f2007-07-15 23:40:52 -07001086 token = match_token(p, tokens, args);
1087 switch (token) {
1088 case Opt_uid:
1089 if (match_int(&args[0], &option))
1090 goto bad_val;
Eric W. Biedermana0eb3a02012-02-07 16:19:25 -08001091 pconfig->uid = make_kuid(current_user_ns(), option);
1092 if (!uid_valid(pconfig->uid))
1093 goto bad_val;
Randy Dunlape73a75f2007-07-15 23:40:52 -07001094 break;
1095
1096 case Opt_gid:
1097 if (match_int(&args[0], &option))
1098 goto bad_val;
Eric W. Biedermana0eb3a02012-02-07 16:19:25 -08001099 pconfig->gid = make_kgid(current_user_ns(), option);
1100 if (!gid_valid(pconfig->gid))
1101 goto bad_val;
Randy Dunlape73a75f2007-07-15 23:40:52 -07001102 break;
1103
1104 case Opt_mode:
1105 if (match_octal(&args[0], &option))
1106 goto bad_val;
Ken Chen75897d62008-02-04 22:28:36 -08001107 pconfig->mode = option & 01777U;
Randy Dunlape73a75f2007-07-15 23:40:52 -07001108 break;
1109
1110 case Opt_size: {
Randy Dunlape73a75f2007-07-15 23:40:52 -07001111 /* memparse() will accept a K/M/G without a digit */
1112 if (!isdigit(*args[0].from))
1113 goto bad_val;
Mike Kravetz7ca02d02015-04-15 16:13:42 -07001114 max_size_opt = memparse(args[0].from, &rest);
1115 max_val_type = SIZE_STD;
Andi Kleena137e1c2008-07-23 21:27:43 -07001116 if (*rest == '%')
Mike Kravetz7ca02d02015-04-15 16:13:42 -07001117 max_val_type = SIZE_PERCENT;
Randy Dunlape73a75f2007-07-15 23:40:52 -07001118 break;
1119 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
Randy Dunlape73a75f2007-07-15 23:40:52 -07001121 case Opt_nr_inodes:
1122 /* memparse() will accept a K/M/G without a digit */
1123 if (!isdigit(*args[0].from))
1124 goto bad_val;
1125 pconfig->nr_inodes = memparse(args[0].from, &rest);
1126 break;
1127
Andi Kleena137e1c2008-07-23 21:27:43 -07001128 case Opt_pagesize: {
1129 unsigned long ps;
1130 ps = memparse(args[0].from, &rest);
1131 pconfig->hstate = size_to_hstate(ps);
1132 if (!pconfig->hstate) {
Andrew Morton9b857d22014-06-04 16:07:21 -07001133 pr_err("Unsupported page size %lu MB\n",
Andi Kleena137e1c2008-07-23 21:27:43 -07001134 ps >> 20);
1135 return -EINVAL;
1136 }
1137 break;
1138 }
1139
Mike Kravetz7ca02d02015-04-15 16:13:42 -07001140 case Opt_min_size: {
1141 /* memparse() will accept a K/M/G without a digit */
1142 if (!isdigit(*args[0].from))
1143 goto bad_val;
1144 min_size_opt = memparse(args[0].from, &rest);
1145 min_val_type = SIZE_STD;
1146 if (*rest == '%')
1147 min_val_type = SIZE_PERCENT;
1148 break;
1149 }
1150
Randy Dunlape73a75f2007-07-15 23:40:52 -07001151 default:
Andrew Morton9b857d22014-06-04 16:07:21 -07001152 pr_err("Bad mount option: \"%s\"\n", p);
Lee Schermerhornb4c07bc2007-07-15 23:40:54 -07001153 return -EINVAL;
Randy Dunlape73a75f2007-07-15 23:40:52 -07001154 break;
1155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 }
Andi Kleena137e1c2008-07-23 21:27:43 -07001157
Mike Kravetz7ca02d02015-04-15 16:13:42 -07001158 /*
1159 * Use huge page pool size (in hstate) to convert the size
1160 * options to number of huge pages. If NO_SIZE, -1 is returned.
1161 */
1162 pconfig->max_hpages = hugetlbfs_size_to_hpages(pconfig->hstate,
1163 max_size_opt, max_val_type);
1164 pconfig->min_hpages = hugetlbfs_size_to_hpages(pconfig->hstate,
1165 min_size_opt, min_val_type);
1166
1167 /*
1168 * If max_size was specified, then min_size must be smaller
1169 */
1170 if (max_val_type > NO_SIZE &&
1171 pconfig->min_hpages > pconfig->max_hpages) {
1172 pr_err("minimum size can not be greater than maximum size\n");
1173 return -EINVAL;
Andi Kleena137e1c2008-07-23 21:27:43 -07001174 }
1175
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 return 0;
Randy Dunlape73a75f2007-07-15 23:40:52 -07001177
1178bad_val:
Andrew Morton9b857d22014-06-04 16:07:21 -07001179 pr_err("Bad value '%s' for mount option '%s'\n", args[0].from, p);
Akinobu Mitac12ddba2009-04-21 12:24:05 -07001180 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181}
1182
1183static int
1184hugetlbfs_fill_super(struct super_block *sb, void *data, int silent)
1185{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 int ret;
1187 struct hugetlbfs_config config;
1188 struct hugetlbfs_sb_info *sbinfo;
1189
Miklos Szeredi10f19a82008-02-08 04:21:45 -08001190 save_mount_options(sb, data);
1191
Mike Kravetz7ca02d02015-04-15 16:13:42 -07001192 config.max_hpages = -1; /* No limit on size by default */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 config.nr_inodes = -1; /* No limit on number of inodes by default */
David Howells77c70de2008-11-14 10:38:56 +11001194 config.uid = current_fsuid();
1195 config.gid = current_fsgid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 config.mode = 0755;
Andi Kleena137e1c2008-07-23 21:27:43 -07001197 config.hstate = &default_hstate;
Mike Kravetz7ca02d02015-04-15 16:13:42 -07001198 config.min_hpages = -1; /* No default minimum size */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 ret = hugetlbfs_parse_options(data, &config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 if (ret)
1201 return ret;
1202
1203 sbinfo = kmalloc(sizeof(struct hugetlbfs_sb_info), GFP_KERNEL);
1204 if (!sbinfo)
1205 return -ENOMEM;
1206 sb->s_fs_info = sbinfo;
Andi Kleena137e1c2008-07-23 21:27:43 -07001207 sbinfo->hstate = config.hstate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 spin_lock_init(&sbinfo->stat_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 sbinfo->max_inodes = config.nr_inodes;
1210 sbinfo->free_inodes = config.nr_inodes;
David Gibson90481622012-03-21 16:34:12 -07001211 sbinfo->spool = NULL;
Mike Kravetz7ca02d02015-04-15 16:13:42 -07001212 /*
1213 * Allocate and initialize subpool if maximum or minimum size is
1214 * specified. Any needed reservations (for minimim size) are taken
1215 * taken when the subpool is created.
1216 */
1217 if (config.max_hpages != -1 || config.min_hpages != -1) {
1218 sbinfo->spool = hugepage_new_subpool(config.hstate,
1219 config.max_hpages,
1220 config.min_hpages);
David Gibson90481622012-03-21 16:34:12 -07001221 if (!sbinfo->spool)
1222 goto out_free;
1223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 sb->s_maxbytes = MAX_LFS_FILESIZE;
Andi Kleena137e1c2008-07-23 21:27:43 -07001225 sb->s_blocksize = huge_page_size(config.hstate);
1226 sb->s_blocksize_bits = huge_page_shift(config.hstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 sb->s_magic = HUGETLBFS_MAGIC;
1228 sb->s_op = &hugetlbfs_ops;
1229 sb->s_time_gran = 1;
Al Viro48fde702012-01-08 22:15:13 -05001230 sb->s_root = d_make_root(hugetlbfs_get_root(sb, &config));
1231 if (!sb->s_root)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 return 0;
1234out_free:
Fabian Frederick6e6870d2014-06-04 16:10:40 -07001235 kfree(sbinfo->spool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 kfree(sbinfo);
1237 return -ENOMEM;
1238}
1239
Al Viro3c26ff62010-07-25 11:46:36 +04001240static struct dentry *hugetlbfs_mount(struct file_system_type *fs_type,
1241 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242{
Al Viro3c26ff62010-07-25 11:46:36 +04001243 return mount_nodev(fs_type, flags, data, hugetlbfs_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244}
1245
1246static struct file_system_type hugetlbfs_fs_type = {
1247 .name = "hugetlbfs",
Al Viro3c26ff62010-07-25 11:46:36 +04001248 .mount = hugetlbfs_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 .kill_sb = kill_litter_super,
1250};
1251
Andi Kleen42d73952012-12-11 16:01:34 -08001252static struct vfsmount *hugetlbfs_vfsmount[HUGE_MAX_HSTATE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
From: Mel Gormanef1ff6b2009-09-23 15:56:05 -07001254static int can_do_hugetlb_shm(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255{
Eric W. Biedermana0eb3a02012-02-07 16:19:25 -08001256 kgid_t shm_group;
1257 shm_group = make_kgid(&init_user_ns, sysctl_hugetlb_shm_group);
1258 return capable(CAP_IPC_LOCK) || in_group_p(shm_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259}
1260
Andi Kleen42d73952012-12-11 16:01:34 -08001261static int get_hstate_idx(int page_size_log)
1262{
Naoya Horiguchiaf73e4d2013-05-07 16:18:13 -07001263 struct hstate *h = hstate_sizelog(page_size_log);
Andi Kleen42d73952012-12-11 16:01:34 -08001264
Andi Kleen42d73952012-12-11 16:01:34 -08001265 if (!h)
1266 return -1;
1267 return h - hstates;
1268}
1269
Fabian Frederickbe1d2cf2014-06-04 16:10:39 -07001270static const struct dentry_operations anon_ops = {
Al Viro118b2302013-08-24 12:08:17 -04001271 .d_dname = simple_dname
Al Viro0df4d6e2013-02-14 22:39:53 -05001272};
1273
Naoya Horiguchiaf73e4d2013-05-07 16:18:13 -07001274/*
1275 * Note that size should be aligned to proper hugepage size in caller side,
1276 * otherwise hugetlb_reserve_pages reserves one less hugepages than intended.
1277 */
1278struct file *hugetlb_file_setup(const char *name, size_t size,
1279 vm_flags_t acctflag, struct user_struct **user,
Andi Kleen42d73952012-12-11 16:01:34 -08001280 int creat_flags, int page_size_log)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281{
Anatol Pomozov39b65252012-09-12 20:11:55 -07001282 struct file *file = ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 struct inode *inode;
Al Viro2c48b9c2009-08-09 00:52:35 +04001284 struct path path;
Al Viro0df4d6e2013-02-14 22:39:53 -05001285 struct super_block *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 struct qstr quick_string;
Andi Kleen42d73952012-12-11 16:01:34 -08001287 int hstate_idx;
1288
1289 hstate_idx = get_hstate_idx(page_size_log);
1290 if (hstate_idx < 0)
1291 return ERR_PTR(-ENODEV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
Hugh Dickins353d5c32009-08-24 16:30:28 +01001293 *user = NULL;
Andi Kleen42d73952012-12-11 16:01:34 -08001294 if (!hugetlbfs_vfsmount[hstate_idx])
Akinobu Mita5bc98592007-05-06 14:50:18 -07001295 return ERR_PTR(-ENOENT);
1296
From: Mel Gormanef1ff6b2009-09-23 15:56:05 -07001297 if (creat_flags == HUGETLB_SHMFS_INODE && !can_do_hugetlb_shm()) {
Hugh Dickins353d5c32009-08-24 16:30:28 +01001298 *user = current_user();
1299 if (user_shm_lock(size, *user)) {
David Rientjes21a3c272012-03-21 16:34:13 -07001300 task_lock(current);
Andrew Morton9b857d22014-06-04 16:07:21 -07001301 pr_warn_once("%s (%d): Using mlock ulimits for SHM_HUGETLB is deprecated\n",
David Rientjes21a3c272012-03-21 16:34:13 -07001302 current->comm, current->pid);
1303 task_unlock(current);
Hugh Dickins353d5c32009-08-24 16:30:28 +01001304 } else {
1305 *user = NULL;
Ravikiran G Thirumalai2584e512009-03-31 15:21:26 -07001306 return ERR_PTR(-EPERM);
Hugh Dickins353d5c32009-08-24 16:30:28 +01001307 }
Ravikiran G Thirumalai2584e512009-03-31 15:21:26 -07001308 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
Al Viro0df4d6e2013-02-14 22:39:53 -05001310 sb = hugetlbfs_vfsmount[hstate_idx]->mnt_sb;
Eric W. Biederman9d665862007-06-16 10:16:16 -07001311 quick_string.name = name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 quick_string.len = strlen(quick_string.name);
1313 quick_string.hash = 0;
Al Viro0df4d6e2013-02-14 22:39:53 -05001314 path.dentry = d_alloc_pseudo(sb, &quick_string);
Al Viro2c48b9c2009-08-09 00:52:35 +04001315 if (!path.dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 goto out_shm_unlock;
1317
Al Viro0df4d6e2013-02-14 22:39:53 -05001318 d_set_d_op(path.dentry, &anon_ops);
Andi Kleen42d73952012-12-11 16:01:34 -08001319 path.mnt = mntget(hugetlbfs_vfsmount[hstate_idx]);
Anatol Pomozov39b65252012-09-12 20:11:55 -07001320 file = ERR_PTR(-ENOSPC);
Al Viro0df4d6e2013-02-14 22:39:53 -05001321 inode = hugetlbfs_get_inode(sb, NULL, S_IFREG | S_IRWXUGO, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 if (!inode)
Dave Hansence8d2cd2007-10-16 23:31:13 -07001323 goto out_dentry;
Stephen Smalleye1832f22015-08-06 15:46:55 -07001324 if (creat_flags == HUGETLB_SHMFS_INODE)
1325 inode->i_flags |= S_PRIVATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
Anatol Pomozov39b65252012-09-12 20:11:55 -07001327 file = ERR_PTR(-ENOMEM);
Naoya Horiguchiaf73e4d2013-05-07 16:18:13 -07001328 if (hugetlb_reserve_pages(inode, 0,
1329 size >> huge_page_shift(hstate_inode(inode)), NULL,
1330 acctflag))
David Gibsonb45b5bd2006-03-22 00:08:55 -08001331 goto out_inode;
1332
Al Viro2c48b9c2009-08-09 00:52:35 +04001333 d_instantiate(path.dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 inode->i_size = size;
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +02001335 clear_nlink(inode);
Dave Hansence8d2cd2007-10-16 23:31:13 -07001336
Al Viro2c48b9c2009-08-09 00:52:35 +04001337 file = alloc_file(&path, FMODE_WRITE | FMODE_READ,
Dave Hansence8d2cd2007-10-16 23:31:13 -07001338 &hugetlbfs_file_operations);
Anatol Pomozov39b65252012-09-12 20:11:55 -07001339 if (IS_ERR(file))
Al Virob4d232e2008-02-23 05:59:19 -05001340 goto out_dentry; /* inode is already attached */
Dave Hansence8d2cd2007-10-16 23:31:13 -07001341
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 return file;
1343
David Gibsonb45b5bd2006-03-22 00:08:55 -08001344out_inode:
1345 iput(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346out_dentry:
Al Viro2c48b9c2009-08-09 00:52:35 +04001347 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348out_shm_unlock:
Hugh Dickins353d5c32009-08-24 16:30:28 +01001349 if (*user) {
1350 user_shm_unlock(size, *user);
1351 *user = NULL;
1352 }
Anatol Pomozov39b65252012-09-12 20:11:55 -07001353 return file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354}
1355
1356static int __init init_hugetlbfs_fs(void)
1357{
Andi Kleen42d73952012-12-11 16:01:34 -08001358 struct hstate *h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 int error;
Andi Kleen42d73952012-12-11 16:01:34 -08001360 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
Nishanth Aravamudan457c1b22014-05-06 12:50:00 -07001362 if (!hugepages_supported()) {
Andrew Morton9b857d22014-06-04 16:07:21 -07001363 pr_info("disabling because there are no supported hugepage sizes\n");
Nishanth Aravamudan457c1b22014-05-06 12:50:00 -07001364 return -ENOTSUPP;
1365 }
1366
Hillf Dantond1d5e052012-03-21 16:34:15 -07001367 error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 hugetlbfs_inode_cachep = kmem_cache_create("hugetlbfs_inode_cache",
1369 sizeof(struct hugetlbfs_inode_info),
Vladimir Davydov5d097052016-01-14 15:18:21 -08001370 0, SLAB_ACCOUNT, init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 if (hugetlbfs_inode_cachep == NULL)
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -07001372 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
1374 error = register_filesystem(&hugetlbfs_fs_type);
1375 if (error)
1376 goto out;
1377
Andi Kleen42d73952012-12-11 16:01:34 -08001378 i = 0;
1379 for_each_hstate(h) {
1380 char buf[50];
1381 unsigned ps_kb = 1U << (h->order + PAGE_SHIFT - 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
Andi Kleen42d73952012-12-11 16:01:34 -08001383 snprintf(buf, sizeof(buf), "pagesize=%uK", ps_kb);
1384 hugetlbfs_vfsmount[i] = kern_mount_data(&hugetlbfs_fs_type,
1385 buf);
1386
1387 if (IS_ERR(hugetlbfs_vfsmount[i])) {
Andrew Morton9b857d22014-06-04 16:07:21 -07001388 pr_err("Cannot mount internal hugetlbfs for "
Andi Kleen42d73952012-12-11 16:01:34 -08001389 "page size %uK", ps_kb);
1390 error = PTR_ERR(hugetlbfs_vfsmount[i]);
1391 hugetlbfs_vfsmount[i] = NULL;
1392 }
1393 i++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 }
Andi Kleen42d73952012-12-11 16:01:34 -08001395 /* Non default hstates are optional */
1396 if (!IS_ERR_OR_NULL(hugetlbfs_vfsmount[default_hstate_idx]))
1397 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
1399 out:
Hillf Dantond1d5e052012-03-21 16:34:15 -07001400 kmem_cache_destroy(hugetlbfs_inode_cachep);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -07001401 out2:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 return error;
1403}
Paul Gortmaker3e89e1c2016-01-14 15:21:52 -08001404fs_initcall(init_hugetlbfs_fs)