inode.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373
  1. /*
  2. * hugetlbpage-backed filesystem. Based on ramfs.
  3. *
  4. * Nadia Yvette Chambers, 2002
  5. *
  6. * Copyright (C) 2002 Linus Torvalds.
  7. * License: GPL
  8. */
  9. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  10. #include <linux/thread_info.h>
  11. #include <asm/current.h>
  12. #include <linux/sched/signal.h> /* remove ASAP */
  13. #include <linux/falloc.h>
  14. #include <linux/fs.h>
  15. #include <linux/mount.h>
  16. #include <linux/file.h>
  17. #include <linux/kernel.h>
  18. #include <linux/writeback.h>
  19. #include <linux/pagemap.h>
  20. #include <linux/highmem.h>
  21. #include <linux/init.h>
  22. #include <linux/string.h>
  23. #include <linux/capability.h>
  24. #include <linux/ctype.h>
  25. #include <linux/backing-dev.h>
  26. #include <linux/hugetlb.h>
  27. #include <linux/pagevec.h>
  28. #include <linux/parser.h>
  29. #include <linux/mman.h>
  30. #include <linux/slab.h>
  31. #include <linux/dnotify.h>
  32. #include <linux/statfs.h>
  33. #include <linux/security.h>
  34. #include <linux/magic.h>
  35. #include <linux/migrate.h>
  36. #include <linux/uio.h>
  37. #include <linux/uaccess.h>
  38. static const struct super_operations hugetlbfs_ops;
  39. static const struct address_space_operations hugetlbfs_aops;
  40. const struct file_operations hugetlbfs_file_operations;
  41. static const struct inode_operations hugetlbfs_dir_inode_operations;
  42. static const struct inode_operations hugetlbfs_inode_operations;
  43. struct hugetlbfs_config {
  44. kuid_t uid;
  45. kgid_t gid;
  46. umode_t mode;
  47. long max_hpages;
  48. long nr_inodes;
  49. struct hstate *hstate;
  50. long min_hpages;
  51. };
  52. struct hugetlbfs_inode_info {
  53. struct shared_policy policy;
  54. struct inode vfs_inode;
  55. };
  56. static inline struct hugetlbfs_inode_info *HUGETLBFS_I(struct inode *inode)
  57. {
  58. return container_of(inode, struct hugetlbfs_inode_info, vfs_inode);
  59. }
  60. int sysctl_hugetlb_shm_group;
  61. enum {
  62. Opt_size, Opt_nr_inodes,
  63. Opt_mode, Opt_uid, Opt_gid,
  64. Opt_pagesize, Opt_min_size,
  65. Opt_err,
  66. };
  67. static const match_table_t tokens = {
  68. {Opt_size, "size=%s"},
  69. {Opt_nr_inodes, "nr_inodes=%s"},
  70. {Opt_mode, "mode=%o"},
  71. {Opt_uid, "uid=%u"},
  72. {Opt_gid, "gid=%u"},
  73. {Opt_pagesize, "pagesize=%s"},
  74. {Opt_min_size, "min_size=%s"},
  75. {Opt_err, NULL},
  76. };
  77. #ifdef CONFIG_NUMA
  78. static inline void hugetlb_set_vma_policy(struct vm_area_struct *vma,
  79. struct inode *inode, pgoff_t index)
  80. {
  81. vma->vm_policy = mpol_shared_policy_lookup(&HUGETLBFS_I(inode)->policy,
  82. index);
  83. }
  84. static inline void hugetlb_drop_vma_policy(struct vm_area_struct *vma)
  85. {
  86. mpol_cond_put(vma->vm_policy);
  87. }
  88. #else
  89. static inline void hugetlb_set_vma_policy(struct vm_area_struct *vma,
  90. struct inode *inode, pgoff_t index)
  91. {
  92. }
  93. static inline void hugetlb_drop_vma_policy(struct vm_area_struct *vma)
  94. {
  95. }
  96. #endif
  97. static void huge_pagevec_release(struct pagevec *pvec)
  98. {
  99. int i;
  100. for (i = 0; i < pagevec_count(pvec); ++i)
  101. put_page(pvec->pages[i]);
  102. pagevec_reinit(pvec);
  103. }
  104. static int hugetlbfs_file_mmap(struct file *file, struct vm_area_struct *vma)
  105. {
  106. struct inode *inode = file_inode(file);
  107. loff_t len, vma_len;
  108. int ret;
  109. struct hstate *h = hstate_file(file);
  110. /*
  111. * vma address alignment (but not the pgoff alignment) has
  112. * already been checked by prepare_hugepage_range. If you add
  113. * any error returns here, do so after setting VM_HUGETLB, so
  114. * is_vm_hugetlb_page tests below unmap_region go the right
  115. * way when do_mmap_pgoff unwinds (may be important on powerpc
  116. * and ia64).
  117. */
  118. vma->vm_flags |= VM_HUGETLB | VM_DONTEXPAND;
  119. vma->vm_ops = &hugetlb_vm_ops;
  120. /*
  121. * Offset passed to mmap (before page shift) could have been
  122. * negative when represented as a (l)off_t.
  123. */
  124. if (((loff_t)vma->vm_pgoff << PAGE_SHIFT) < 0)
  125. return -EINVAL;
  126. if (vma->vm_pgoff & (~huge_page_mask(h) >> PAGE_SHIFT))
  127. return -EINVAL;
  128. vma_len = (loff_t)(vma->vm_end - vma->vm_start);
  129. len = vma_len + ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
  130. /* check for overflow */
  131. if (len < vma_len)
  132. return -EINVAL;
  133. inode_lock(inode);
  134. file_accessed(file);
  135. ret = -ENOMEM;
  136. if (hugetlb_reserve_pages(inode,
  137. vma->vm_pgoff >> huge_page_order(h),
  138. len >> huge_page_shift(h), vma,
  139. vma->vm_flags))
  140. goto out;
  141. ret = 0;
  142. if (vma->vm_flags & VM_WRITE && inode->i_size < len)
  143. i_size_write(inode, len);
  144. out:
  145. inode_unlock(inode);
  146. return ret;
  147. }
  148. /*
  149. * Called under down_write(mmap_sem).
  150. */
  151. #ifndef HAVE_ARCH_HUGETLB_UNMAPPED_AREA
  152. static unsigned long
  153. hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
  154. unsigned long len, unsigned long pgoff, unsigned long flags)
  155. {
  156. struct mm_struct *mm = current->mm;
  157. struct vm_area_struct *vma;
  158. struct hstate *h = hstate_file(file);
  159. struct vm_unmapped_area_info info;
  160. if (len & ~huge_page_mask(h))
  161. return -EINVAL;
  162. if (len > TASK_SIZE)
  163. return -ENOMEM;
  164. if (flags & MAP_FIXED) {
  165. if (prepare_hugepage_range(file, addr, len))
  166. return -EINVAL;
  167. return addr;
  168. }
  169. if (addr) {
  170. addr = ALIGN(addr, huge_page_size(h));
  171. vma = find_vma(mm, addr);
  172. if (TASK_SIZE - len >= addr &&
  173. (!vma || addr + len <= vm_start_gap(vma)))
  174. return addr;
  175. }
  176. info.flags = 0;
  177. info.length = len;
  178. info.low_limit = TASK_UNMAPPED_BASE;
  179. info.high_limit = TASK_SIZE;
  180. info.align_mask = PAGE_MASK & ~huge_page_mask(h);
  181. info.align_offset = 0;
  182. return vm_unmapped_area(&info);
  183. }
  184. #endif
  185. static size_t
  186. hugetlbfs_read_actor(struct page *page, unsigned long offset,
  187. struct iov_iter *to, unsigned long size)
  188. {
  189. size_t copied = 0;
  190. int i, chunksize;
  191. /* Find which 4k chunk and offset with in that chunk */
  192. i = offset >> PAGE_SHIFT;
  193. offset = offset & ~PAGE_MASK;
  194. while (size) {
  195. size_t n;
  196. chunksize = PAGE_SIZE;
  197. if (offset)
  198. chunksize -= offset;
  199. if (chunksize > size)
  200. chunksize = size;
  201. n = copy_page_to_iter(&page[i], offset, chunksize, to);
  202. copied += n;
  203. if (n != chunksize)
  204. return copied;
  205. offset = 0;
  206. size -= chunksize;
  207. i++;
  208. }
  209. return copied;
  210. }
  211. /*
  212. * Support for read() - Find the page attached to f_mapping and copy out the
  213. * data. Its *very* similar to do_generic_mapping_read(), we can't use that
  214. * since it has PAGE_SIZE assumptions.
  215. */
  216. static ssize_t hugetlbfs_read_iter(struct kiocb *iocb, struct iov_iter *to)
  217. {
  218. struct file *file = iocb->ki_filp;
  219. struct hstate *h = hstate_file(file);
  220. struct address_space *mapping = file->f_mapping;
  221. struct inode *inode = mapping->host;
  222. unsigned long index = iocb->ki_pos >> huge_page_shift(h);
  223. unsigned long offset = iocb->ki_pos & ~huge_page_mask(h);
  224. unsigned long end_index;
  225. loff_t isize;
  226. ssize_t retval = 0;
  227. while (iov_iter_count(to)) {
  228. struct page *page;
  229. size_t nr, copied;
  230. /* nr is the maximum number of bytes to copy from this page */
  231. nr = huge_page_size(h);
  232. isize = i_size_read(inode);
  233. if (!isize)
  234. break;
  235. end_index = (isize - 1) >> huge_page_shift(h);
  236. if (index > end_index)
  237. break;
  238. if (index == end_index) {
  239. nr = ((isize - 1) & ~huge_page_mask(h)) + 1;
  240. if (nr <= offset)
  241. break;
  242. }
  243. nr = nr - offset;
  244. /* Find the page */
  245. page = find_lock_page(mapping, index);
  246. if (unlikely(page == NULL)) {
  247. /*
  248. * We have a HOLE, zero out the user-buffer for the
  249. * length of the hole or request.
  250. */
  251. copied = iov_iter_zero(nr, to);
  252. } else {
  253. unlock_page(page);
  254. /*
  255. * We have the page, copy it to user space buffer.
  256. */
  257. copied = hugetlbfs_read_actor(page, offset, to, nr);
  258. put_page(page);
  259. }
  260. offset += copied;
  261. retval += copied;
  262. if (copied != nr && iov_iter_count(to)) {
  263. if (!retval)
  264. retval = -EFAULT;
  265. break;
  266. }
  267. index += offset >> huge_page_shift(h);
  268. offset &= ~huge_page_mask(h);
  269. }
  270. iocb->ki_pos = ((loff_t)index << huge_page_shift(h)) + offset;
  271. return retval;
  272. }
  273. static int hugetlbfs_write_begin(struct file *file,
  274. struct address_space *mapping,
  275. loff_t pos, unsigned len, unsigned flags,
  276. struct page **pagep, void **fsdata)
  277. {
  278. return -EINVAL;
  279. }
  280. static int hugetlbfs_write_end(struct file *file, struct address_space *mapping,
  281. loff_t pos, unsigned len, unsigned copied,
  282. struct page *page, void *fsdata)
  283. {
  284. BUG();
  285. return -EINVAL;
  286. }
  287. static void remove_huge_page(struct page *page)
  288. {
  289. ClearPageDirty(page);
  290. ClearPageUptodate(page);
  291. delete_from_page_cache(page);
  292. }
  293. static void
  294. hugetlb_vmdelete_list(struct rb_root *root, pgoff_t start, pgoff_t end)
  295. {
  296. struct vm_area_struct *vma;
  297. /*
  298. * end == 0 indicates that the entire range after
  299. * start should be unmapped.
  300. */
  301. vma_interval_tree_foreach(vma, root, start, end ? end : ULONG_MAX) {
  302. unsigned long v_offset;
  303. unsigned long v_end;
  304. /*
  305. * Can the expression below overflow on 32-bit arches?
  306. * No, because the interval tree returns us only those vmas
  307. * which overlap the truncated area starting at pgoff,
  308. * and no vma on a 32-bit arch can span beyond the 4GB.
  309. */
  310. if (vma->vm_pgoff < start)
  311. v_offset = (start - vma->vm_pgoff) << PAGE_SHIFT;
  312. else
  313. v_offset = 0;
  314. if (!end)
  315. v_end = vma->vm_end;
  316. else {
  317. v_end = ((end - vma->vm_pgoff) << PAGE_SHIFT)
  318. + vma->vm_start;
  319. if (v_end > vma->vm_end)
  320. v_end = vma->vm_end;
  321. }
  322. unmap_hugepage_range(vma, vma->vm_start + v_offset, v_end,
  323. NULL);
  324. }
  325. }
  326. /*
  327. * remove_inode_hugepages handles two distinct cases: truncation and hole
  328. * punch. There are subtle differences in operation for each case.
  329. *
  330. * truncation is indicated by end of range being LLONG_MAX
  331. * In this case, we first scan the range and release found pages.
  332. * After releasing pages, hugetlb_unreserve_pages cleans up region/reserv
  333. * maps and global counts. Page faults can not race with truncation
  334. * in this routine. hugetlb_no_page() prevents page faults in the
  335. * truncated range. It checks i_size before allocation, and again after
  336. * with the page table lock for the page held. The same lock must be
  337. * acquired to unmap a page.
  338. * hole punch is indicated if end is not LLONG_MAX
  339. * In the hole punch case we scan the range and release found pages.
  340. * Only when releasing a page is the associated region/reserv map
  341. * deleted. The region/reserv map for ranges without associated
  342. * pages are not modified. Page faults can race with hole punch.
  343. * This is indicated if we find a mapped page.
  344. * Note: If the passed end of range value is beyond the end of file, but
  345. * not LLONG_MAX this routine still performs a hole punch operation.
  346. */
  347. static void remove_inode_hugepages(struct inode *inode, loff_t lstart,
  348. loff_t lend)
  349. {
  350. struct hstate *h = hstate_inode(inode);
  351. struct address_space *mapping = &inode->i_data;
  352. const pgoff_t start = lstart >> huge_page_shift(h);
  353. const pgoff_t end = lend >> huge_page_shift(h);
  354. struct vm_area_struct pseudo_vma;
  355. struct pagevec pvec;
  356. pgoff_t next;
  357. int i, freed = 0;
  358. long lookup_nr = PAGEVEC_SIZE;
  359. bool truncate_op = (lend == LLONG_MAX);
  360. memset(&pseudo_vma, 0, sizeof(struct vm_area_struct));
  361. pseudo_vma.vm_flags = (VM_HUGETLB | VM_MAYSHARE | VM_SHARED);
  362. pagevec_init(&pvec, 0);
  363. next = start;
  364. while (next < end) {
  365. /*
  366. * Don't grab more pages than the number left in the range.
  367. */
  368. if (end - next < lookup_nr)
  369. lookup_nr = end - next;
  370. /*
  371. * When no more pages are found, we are done.
  372. */
  373. if (!pagevec_lookup(&pvec, mapping, next, lookup_nr))
  374. break;
  375. for (i = 0; i < pagevec_count(&pvec); ++i) {
  376. struct page *page = pvec.pages[i];
  377. u32 hash;
  378. /*
  379. * The page (index) could be beyond end. This is
  380. * only possible in the punch hole case as end is
  381. * max page offset in the truncate case.
  382. */
  383. next = page->index;
  384. if (next >= end)
  385. break;
  386. hash = hugetlb_fault_mutex_hash(h, current->mm,
  387. &pseudo_vma,
  388. mapping, next, 0);
  389. mutex_lock(&hugetlb_fault_mutex_table[hash]);
  390. /*
  391. * If page is mapped, it was faulted in after being
  392. * unmapped in caller. Unmap (again) now after taking
  393. * the fault mutex. The mutex will prevent faults
  394. * until we finish removing the page.
  395. *
  396. * This race can only happen in the hole punch case.
  397. * Getting here in a truncate operation is a bug.
  398. */
  399. if (unlikely(page_mapped(page))) {
  400. BUG_ON(truncate_op);
  401. i_mmap_lock_write(mapping);
  402. hugetlb_vmdelete_list(&mapping->i_mmap,
  403. next * pages_per_huge_page(h),
  404. (next + 1) * pages_per_huge_page(h));
  405. i_mmap_unlock_write(mapping);
  406. }
  407. lock_page(page);
  408. /*
  409. * We must free the huge page and remove from page
  410. * cache (remove_huge_page) BEFORE removing the
  411. * region/reserve map (hugetlb_unreserve_pages). In
  412. * rare out of memory conditions, removal of the
  413. * region/reserve map could fail. Correspondingly,
  414. * the subpool and global reserve usage count can need
  415. * to be adjusted.
  416. */
  417. VM_BUG_ON(PagePrivate(page));
  418. remove_huge_page(page);
  419. freed++;
  420. if (!truncate_op) {
  421. if (unlikely(hugetlb_unreserve_pages(inode,
  422. next, next + 1, 1)))
  423. hugetlb_fix_reserve_counts(inode);
  424. }
  425. unlock_page(page);
  426. mutex_unlock(&hugetlb_fault_mutex_table[hash]);
  427. }
  428. ++next;
  429. huge_pagevec_release(&pvec);
  430. cond_resched();
  431. }
  432. if (truncate_op)
  433. (void)hugetlb_unreserve_pages(inode, start, LONG_MAX, freed);
  434. }
  435. static void hugetlbfs_evict_inode(struct inode *inode)
  436. {
  437. struct resv_map *resv_map;
  438. remove_inode_hugepages(inode, 0, LLONG_MAX);
  439. resv_map = (struct resv_map *)inode->i_mapping->private_data;
  440. /* root inode doesn't have the resv_map, so we should check it */
  441. if (resv_map)
  442. resv_map_release(&resv_map->refs);
  443. clear_inode(inode);
  444. }
  445. static int hugetlb_vmtruncate(struct inode *inode, loff_t offset)
  446. {
  447. pgoff_t pgoff;
  448. struct address_space *mapping = inode->i_mapping;
  449. struct hstate *h = hstate_inode(inode);
  450. BUG_ON(offset & ~huge_page_mask(h));
  451. pgoff = offset >> PAGE_SHIFT;
  452. i_size_write(inode, offset);
  453. i_mmap_lock_write(mapping);
  454. if (!RB_EMPTY_ROOT(&mapping->i_mmap))
  455. hugetlb_vmdelete_list(&mapping->i_mmap, pgoff, 0);
  456. i_mmap_unlock_write(mapping);
  457. remove_inode_hugepages(inode, offset, LLONG_MAX);
  458. return 0;
  459. }
  460. static long hugetlbfs_punch_hole(struct inode *inode, loff_t offset, loff_t len)
  461. {
  462. struct hstate *h = hstate_inode(inode);
  463. loff_t hpage_size = huge_page_size(h);
  464. loff_t hole_start, hole_end;
  465. /*
  466. * For hole punch round up the beginning offset of the hole and
  467. * round down the end.
  468. */
  469. hole_start = round_up(offset, hpage_size);
  470. hole_end = round_down(offset + len, hpage_size);
  471. if (hole_end > hole_start) {
  472. struct address_space *mapping = inode->i_mapping;
  473. inode_lock(inode);
  474. i_mmap_lock_write(mapping);
  475. if (!RB_EMPTY_ROOT(&mapping->i_mmap))
  476. hugetlb_vmdelete_list(&mapping->i_mmap,
  477. hole_start >> PAGE_SHIFT,
  478. hole_end >> PAGE_SHIFT);
  479. i_mmap_unlock_write(mapping);
  480. remove_inode_hugepages(inode, hole_start, hole_end);
  481. inode_unlock(inode);
  482. }
  483. return 0;
  484. }
  485. static long hugetlbfs_fallocate(struct file *file, int mode, loff_t offset,
  486. loff_t len)
  487. {
  488. struct inode *inode = file_inode(file);
  489. struct address_space *mapping = inode->i_mapping;
  490. struct hstate *h = hstate_inode(inode);
  491. struct vm_area_struct pseudo_vma;
  492. struct mm_struct *mm = current->mm;
  493. loff_t hpage_size = huge_page_size(h);
  494. unsigned long hpage_shift = huge_page_shift(h);
  495. pgoff_t start, index, end;
  496. int error;
  497. u32 hash;
  498. if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
  499. return -EOPNOTSUPP;
  500. if (mode & FALLOC_FL_PUNCH_HOLE)
  501. return hugetlbfs_punch_hole(inode, offset, len);
  502. /*
  503. * Default preallocate case.
  504. * For this range, start is rounded down and end is rounded up
  505. * as well as being converted to page offsets.
  506. */
  507. start = offset >> hpage_shift;
  508. end = (offset + len + hpage_size - 1) >> hpage_shift;
  509. inode_lock(inode);
  510. /* We need to check rlimit even when FALLOC_FL_KEEP_SIZE */
  511. error = inode_newsize_ok(inode, offset + len);
  512. if (error)
  513. goto out;
  514. /*
  515. * Initialize a pseudo vma as this is required by the huge page
  516. * allocation routines. If NUMA is configured, use page index
  517. * as input to create an allocation policy.
  518. */
  519. memset(&pseudo_vma, 0, sizeof(struct vm_area_struct));
  520. pseudo_vma.vm_flags = (VM_HUGETLB | VM_MAYSHARE | VM_SHARED);
  521. pseudo_vma.vm_file = file;
  522. for (index = start; index < end; index++) {
  523. /*
  524. * This is supposed to be the vaddr where the page is being
  525. * faulted in, but we have no vaddr here.
  526. */
  527. struct page *page;
  528. unsigned long addr;
  529. int avoid_reserve = 0;
  530. cond_resched();
  531. /*
  532. * fallocate(2) manpage permits EINTR; we may have been
  533. * interrupted because we are using up too much memory.
  534. */
  535. if (signal_pending(current)) {
  536. error = -EINTR;
  537. break;
  538. }
  539. /* Set numa allocation policy based on index */
  540. hugetlb_set_vma_policy(&pseudo_vma, inode, index);
  541. /* addr is the offset within the file (zero based) */
  542. addr = index * hpage_size;
  543. /* mutex taken here, fault path and hole punch */
  544. hash = hugetlb_fault_mutex_hash(h, mm, &pseudo_vma, mapping,
  545. index, addr);
  546. mutex_lock(&hugetlb_fault_mutex_table[hash]);
  547. /* See if already present in mapping to avoid alloc/free */
  548. page = find_get_page(mapping, index);
  549. if (page) {
  550. put_page(page);
  551. mutex_unlock(&hugetlb_fault_mutex_table[hash]);
  552. hugetlb_drop_vma_policy(&pseudo_vma);
  553. continue;
  554. }
  555. /* Allocate page and add to page cache */
  556. page = alloc_huge_page(&pseudo_vma, addr, avoid_reserve);
  557. hugetlb_drop_vma_policy(&pseudo_vma);
  558. if (IS_ERR(page)) {
  559. mutex_unlock(&hugetlb_fault_mutex_table[hash]);
  560. error = PTR_ERR(page);
  561. goto out;
  562. }
  563. clear_huge_page(page, addr, pages_per_huge_page(h));
  564. __SetPageUptodate(page);
  565. error = huge_add_to_page_cache(page, mapping, index);
  566. if (unlikely(error)) {
  567. put_page(page);
  568. mutex_unlock(&hugetlb_fault_mutex_table[hash]);
  569. goto out;
  570. }
  571. mutex_unlock(&hugetlb_fault_mutex_table[hash]);
  572. /*
  573. * page_put due to reference from alloc_huge_page()
  574. * unlock_page because locked by add_to_page_cache()
  575. */
  576. put_page(page);
  577. unlock_page(page);
  578. }
  579. if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size)
  580. i_size_write(inode, offset + len);
  581. inode->i_ctime = current_time(inode);
  582. out:
  583. inode_unlock(inode);
  584. return error;
  585. }
  586. static int hugetlbfs_setattr(struct dentry *dentry, struct iattr *attr)
  587. {
  588. struct inode *inode = d_inode(dentry);
  589. struct hstate *h = hstate_inode(inode);
  590. int error;
  591. unsigned int ia_valid = attr->ia_valid;
  592. BUG_ON(!inode);
  593. error = setattr_prepare(dentry, attr);
  594. if (error)
  595. return error;
  596. if (ia_valid & ATTR_SIZE) {
  597. error = -EINVAL;
  598. if (attr->ia_size & ~huge_page_mask(h))
  599. return -EINVAL;
  600. error = hugetlb_vmtruncate(inode, attr->ia_size);
  601. if (error)
  602. return error;
  603. }
  604. setattr_copy(inode, attr);
  605. mark_inode_dirty(inode);
  606. return 0;
  607. }
  608. static struct inode *hugetlbfs_get_root(struct super_block *sb,
  609. struct hugetlbfs_config *config)
  610. {
  611. struct inode *inode;
  612. inode = new_inode(sb);
  613. if (inode) {
  614. inode->i_ino = get_next_ino();
  615. inode->i_mode = S_IFDIR | config->mode;
  616. inode->i_uid = config->uid;
  617. inode->i_gid = config->gid;
  618. inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
  619. inode->i_op = &hugetlbfs_dir_inode_operations;
  620. inode->i_fop = &simple_dir_operations;
  621. /* directory inodes start off with i_nlink == 2 (for "." entry) */
  622. inc_nlink(inode);
  623. lockdep_annotate_inode_mutex_key(inode);
  624. }
  625. return inode;
  626. }
  627. /*
  628. * Hugetlbfs is not reclaimable; therefore its i_mmap_rwsem will never
  629. * be taken from reclaim -- unlike regular filesystems. This needs an
  630. * annotation because huge_pmd_share() does an allocation under hugetlb's
  631. * i_mmap_rwsem.
  632. */
  633. static struct lock_class_key hugetlbfs_i_mmap_rwsem_key;
  634. static struct inode *hugetlbfs_get_inode(struct super_block *sb,
  635. struct inode *dir,
  636. umode_t mode, dev_t dev)
  637. {
  638. struct inode *inode;
  639. struct resv_map *resv_map;
  640. resv_map = resv_map_alloc();
  641. if (!resv_map)
  642. return NULL;
  643. inode = new_inode(sb);
  644. if (inode) {
  645. inode->i_ino = get_next_ino();
  646. inode_init_owner(inode, dir, mode);
  647. lockdep_set_class(&inode->i_mapping->i_mmap_rwsem,
  648. &hugetlbfs_i_mmap_rwsem_key);
  649. inode->i_mapping->a_ops = &hugetlbfs_aops;
  650. inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
  651. inode->i_mapping->private_data = resv_map;
  652. switch (mode & S_IFMT) {
  653. default:
  654. init_special_inode(inode, mode, dev);
  655. break;
  656. case S_IFREG:
  657. inode->i_op = &hugetlbfs_inode_operations;
  658. inode->i_fop = &hugetlbfs_file_operations;
  659. break;
  660. case S_IFDIR:
  661. inode->i_op = &hugetlbfs_dir_inode_operations;
  662. inode->i_fop = &simple_dir_operations;
  663. /* directory inodes start off with i_nlink == 2 (for "." entry) */
  664. inc_nlink(inode);
  665. break;
  666. case S_IFLNK:
  667. inode->i_op = &page_symlink_inode_operations;
  668. inode_nohighmem(inode);
  669. break;
  670. }
  671. lockdep_annotate_inode_mutex_key(inode);
  672. } else
  673. kref_put(&resv_map->refs, resv_map_release);
  674. return inode;
  675. }
  676. /*
  677. * File creation. Allocate an inode, and we're done..
  678. */
  679. static int hugetlbfs_mknod(struct inode *dir,
  680. struct dentry *dentry, umode_t mode, dev_t dev)
  681. {
  682. struct inode *inode;
  683. int error = -ENOSPC;
  684. inode = hugetlbfs_get_inode(dir->i_sb, dir, mode, dev);
  685. if (inode) {
  686. dir->i_ctime = dir->i_mtime = current_time(dir);
  687. d_instantiate(dentry, inode);
  688. dget(dentry); /* Extra count - pin the dentry in core */
  689. error = 0;
  690. }
  691. return error;
  692. }
  693. static int hugetlbfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  694. {
  695. int retval = hugetlbfs_mknod(dir, dentry, mode | S_IFDIR, 0);
  696. if (!retval)
  697. inc_nlink(dir);
  698. return retval;
  699. }
  700. static int hugetlbfs_create(struct inode *dir, struct dentry *dentry, umode_t mode, bool excl)
  701. {
  702. return hugetlbfs_mknod(dir, dentry, mode | S_IFREG, 0);
  703. }
  704. static int hugetlbfs_symlink(struct inode *dir,
  705. struct dentry *dentry, const char *symname)
  706. {
  707. struct inode *inode;
  708. int error = -ENOSPC;
  709. inode = hugetlbfs_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0);
  710. if (inode) {
  711. int l = strlen(symname)+1;
  712. error = page_symlink(inode, symname, l);
  713. if (!error) {
  714. d_instantiate(dentry, inode);
  715. dget(dentry);
  716. } else
  717. iput(inode);
  718. }
  719. dir->i_ctime = dir->i_mtime = current_time(dir);
  720. return error;
  721. }
  722. /*
  723. * mark the head page dirty
  724. */
  725. static int hugetlbfs_set_page_dirty(struct page *page)
  726. {
  727. struct page *head = compound_head(page);
  728. SetPageDirty(head);
  729. return 0;
  730. }
  731. static int hugetlbfs_migrate_page(struct address_space *mapping,
  732. struct page *newpage, struct page *page,
  733. enum migrate_mode mode)
  734. {
  735. int rc;
  736. rc = migrate_huge_page_move_mapping(mapping, newpage, page);
  737. if (rc != MIGRATEPAGE_SUCCESS)
  738. return rc;
  739. migrate_page_copy(newpage, page);
  740. return MIGRATEPAGE_SUCCESS;
  741. }
  742. static int hugetlbfs_statfs(struct dentry *dentry, struct kstatfs *buf)
  743. {
  744. struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(dentry->d_sb);
  745. struct hstate *h = hstate_inode(d_inode(dentry));
  746. buf->f_type = HUGETLBFS_MAGIC;
  747. buf->f_bsize = huge_page_size(h);
  748. if (sbinfo) {
  749. spin_lock(&sbinfo->stat_lock);
  750. /* If no limits set, just report 0 for max/free/used
  751. * blocks, like simple_statfs() */
  752. if (sbinfo->spool) {
  753. long free_pages;
  754. spin_lock(&sbinfo->spool->lock);
  755. buf->f_blocks = sbinfo->spool->max_hpages;
  756. free_pages = sbinfo->spool->max_hpages
  757. - sbinfo->spool->used_hpages;
  758. buf->f_bavail = buf->f_bfree = free_pages;
  759. spin_unlock(&sbinfo->spool->lock);
  760. buf->f_files = sbinfo->max_inodes;
  761. buf->f_ffree = sbinfo->free_inodes;
  762. }
  763. spin_unlock(&sbinfo->stat_lock);
  764. }
  765. buf->f_namelen = NAME_MAX;
  766. return 0;
  767. }
  768. static void hugetlbfs_put_super(struct super_block *sb)
  769. {
  770. struct hugetlbfs_sb_info *sbi = HUGETLBFS_SB(sb);
  771. if (sbi) {
  772. sb->s_fs_info = NULL;
  773. if (sbi->spool)
  774. hugepage_put_subpool(sbi->spool);
  775. kfree(sbi);
  776. }
  777. }
  778. static inline int hugetlbfs_dec_free_inodes(struct hugetlbfs_sb_info *sbinfo)
  779. {
  780. if (sbinfo->free_inodes >= 0) {
  781. spin_lock(&sbinfo->stat_lock);
  782. if (unlikely(!sbinfo->free_inodes)) {
  783. spin_unlock(&sbinfo->stat_lock);
  784. return 0;
  785. }
  786. sbinfo->free_inodes--;
  787. spin_unlock(&sbinfo->stat_lock);
  788. }
  789. return 1;
  790. }
  791. static void hugetlbfs_inc_free_inodes(struct hugetlbfs_sb_info *sbinfo)
  792. {
  793. if (sbinfo->free_inodes >= 0) {
  794. spin_lock(&sbinfo->stat_lock);
  795. sbinfo->free_inodes++;
  796. spin_unlock(&sbinfo->stat_lock);
  797. }
  798. }
  799. static struct kmem_cache *hugetlbfs_inode_cachep;
  800. static struct inode *hugetlbfs_alloc_inode(struct super_block *sb)
  801. {
  802. struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(sb);
  803. struct hugetlbfs_inode_info *p;
  804. if (unlikely(!hugetlbfs_dec_free_inodes(sbinfo)))
  805. return NULL;
  806. p = kmem_cache_alloc(hugetlbfs_inode_cachep, GFP_KERNEL);
  807. if (unlikely(!p)) {
  808. hugetlbfs_inc_free_inodes(sbinfo);
  809. return NULL;
  810. }
  811. /*
  812. * Any time after allocation, hugetlbfs_destroy_inode can be called
  813. * for the inode. mpol_free_shared_policy is unconditionally called
  814. * as part of hugetlbfs_destroy_inode. So, initialize policy here
  815. * in case of a quick call to destroy.
  816. *
  817. * Note that the policy is initialized even if we are creating a
  818. * private inode. This simplifies hugetlbfs_destroy_inode.
  819. */
  820. mpol_shared_policy_init(&p->policy, NULL);
  821. return &p->vfs_inode;
  822. }
  823. static void hugetlbfs_i_callback(struct rcu_head *head)
  824. {
  825. struct inode *inode = container_of(head, struct inode, i_rcu);
  826. kmem_cache_free(hugetlbfs_inode_cachep, HUGETLBFS_I(inode));
  827. }
  828. static void hugetlbfs_destroy_inode(struct inode *inode)
  829. {
  830. hugetlbfs_inc_free_inodes(HUGETLBFS_SB(inode->i_sb));
  831. mpol_free_shared_policy(&HUGETLBFS_I(inode)->policy);
  832. call_rcu(&inode->i_rcu, hugetlbfs_i_callback);
  833. }
  834. static const struct address_space_operations hugetlbfs_aops = {
  835. .write_begin = hugetlbfs_write_begin,
  836. .write_end = hugetlbfs_write_end,
  837. .set_page_dirty = hugetlbfs_set_page_dirty,
  838. .migratepage = hugetlbfs_migrate_page,
  839. };
  840. static void init_once(void *foo)
  841. {
  842. struct hugetlbfs_inode_info *ei = (struct hugetlbfs_inode_info *)foo;
  843. inode_init_once(&ei->vfs_inode);
  844. }
  845. const struct file_operations hugetlbfs_file_operations = {
  846. .read_iter = hugetlbfs_read_iter,
  847. .mmap = hugetlbfs_file_mmap,
  848. .fsync = noop_fsync,
  849. .get_unmapped_area = hugetlb_get_unmapped_area,
  850. .llseek = default_llseek,
  851. .fallocate = hugetlbfs_fallocate,
  852. };
  853. static const struct inode_operations hugetlbfs_dir_inode_operations = {
  854. .create = hugetlbfs_create,
  855. .lookup = simple_lookup,
  856. .link = simple_link,
  857. .unlink = simple_unlink,
  858. .symlink = hugetlbfs_symlink,
  859. .mkdir = hugetlbfs_mkdir,
  860. .rmdir = simple_rmdir,
  861. .mknod = hugetlbfs_mknod,
  862. .rename = simple_rename,
  863. .setattr = hugetlbfs_setattr,
  864. };
  865. static const struct inode_operations hugetlbfs_inode_operations = {
  866. .setattr = hugetlbfs_setattr,
  867. };
  868. static const struct super_operations hugetlbfs_ops = {
  869. .alloc_inode = hugetlbfs_alloc_inode,
  870. .destroy_inode = hugetlbfs_destroy_inode,
  871. .evict_inode = hugetlbfs_evict_inode,
  872. .statfs = hugetlbfs_statfs,
  873. .put_super = hugetlbfs_put_super,
  874. .show_options = generic_show_options,
  875. };
  876. enum { NO_SIZE, SIZE_STD, SIZE_PERCENT };
  877. /*
  878. * Convert size option passed from command line to number of huge pages
  879. * in the pool specified by hstate. Size option could be in bytes
  880. * (val_type == SIZE_STD) or percentage of the pool (val_type == SIZE_PERCENT).
  881. */
  882. static long long
  883. hugetlbfs_size_to_hpages(struct hstate *h, unsigned long long size_opt,
  884. int val_type)
  885. {
  886. if (val_type == NO_SIZE)
  887. return -1;
  888. if (val_type == SIZE_PERCENT) {
  889. size_opt <<= huge_page_shift(h);
  890. size_opt *= h->max_huge_pages;
  891. do_div(size_opt, 100);
  892. }
  893. size_opt >>= huge_page_shift(h);
  894. return size_opt;
  895. }
  896. static int
  897. hugetlbfs_parse_options(char *options, struct hugetlbfs_config *pconfig)
  898. {
  899. char *p, *rest;
  900. substring_t args[MAX_OPT_ARGS];
  901. int option;
  902. unsigned long long max_size_opt = 0, min_size_opt = 0;
  903. int max_val_type = NO_SIZE, min_val_type = NO_SIZE;
  904. if (!options)
  905. return 0;
  906. while ((p = strsep(&options, ",")) != NULL) {
  907. int token;
  908. if (!*p)
  909. continue;
  910. token = match_token(p, tokens, args);
  911. switch (token) {
  912. case Opt_uid:
  913. if (match_int(&args[0], &option))
  914. goto bad_val;
  915. pconfig->uid = make_kuid(current_user_ns(), option);
  916. if (!uid_valid(pconfig->uid))
  917. goto bad_val;
  918. break;
  919. case Opt_gid:
  920. if (match_int(&args[0], &option))
  921. goto bad_val;
  922. pconfig->gid = make_kgid(current_user_ns(), option);
  923. if (!gid_valid(pconfig->gid))
  924. goto bad_val;
  925. break;
  926. case Opt_mode:
  927. if (match_octal(&args[0], &option))
  928. goto bad_val;
  929. pconfig->mode = option & 01777U;
  930. break;
  931. case Opt_size: {
  932. /* memparse() will accept a K/M/G without a digit */
  933. if (!isdigit(*args[0].from))
  934. goto bad_val;
  935. max_size_opt = memparse(args[0].from, &rest);
  936. max_val_type = SIZE_STD;
  937. if (*rest == '%')
  938. max_val_type = SIZE_PERCENT;
  939. break;
  940. }
  941. case Opt_nr_inodes:
  942. /* memparse() will accept a K/M/G without a digit */
  943. if (!isdigit(*args[0].from))
  944. goto bad_val;
  945. pconfig->nr_inodes = memparse(args[0].from, &rest);
  946. break;
  947. case Opt_pagesize: {
  948. unsigned long ps;
  949. ps = memparse(args[0].from, &rest);
  950. pconfig->hstate = size_to_hstate(ps);
  951. if (!pconfig->hstate) {
  952. pr_err("Unsupported page size %lu MB\n",
  953. ps >> 20);
  954. return -EINVAL;
  955. }
  956. break;
  957. }
  958. case Opt_min_size: {
  959. /* memparse() will accept a K/M/G without a digit */
  960. if (!isdigit(*args[0].from))
  961. goto bad_val;
  962. min_size_opt = memparse(args[0].from, &rest);
  963. min_val_type = SIZE_STD;
  964. if (*rest == '%')
  965. min_val_type = SIZE_PERCENT;
  966. break;
  967. }
  968. default:
  969. pr_err("Bad mount option: \"%s\"\n", p);
  970. return -EINVAL;
  971. break;
  972. }
  973. }
  974. /*
  975. * Use huge page pool size (in hstate) to convert the size
  976. * options to number of huge pages. If NO_SIZE, -1 is returned.
  977. */
  978. pconfig->max_hpages = hugetlbfs_size_to_hpages(pconfig->hstate,
  979. max_size_opt, max_val_type);
  980. pconfig->min_hpages = hugetlbfs_size_to_hpages(pconfig->hstate,
  981. min_size_opt, min_val_type);
  982. /*
  983. * If max_size was specified, then min_size must be smaller
  984. */
  985. if (max_val_type > NO_SIZE &&
  986. pconfig->min_hpages > pconfig->max_hpages) {
  987. pr_err("minimum size can not be greater than maximum size\n");
  988. return -EINVAL;
  989. }
  990. return 0;
  991. bad_val:
  992. pr_err("Bad value '%s' for mount option '%s'\n", args[0].from, p);
  993. return -EINVAL;
  994. }
  995. static int
  996. hugetlbfs_fill_super(struct super_block *sb, void *data, int silent)
  997. {
  998. int ret;
  999. struct hugetlbfs_config config;
  1000. struct hugetlbfs_sb_info *sbinfo;
  1001. save_mount_options(sb, data);
  1002. config.max_hpages = -1; /* No limit on size by default */
  1003. config.nr_inodes = -1; /* No limit on number of inodes by default */
  1004. config.uid = current_fsuid();
  1005. config.gid = current_fsgid();
  1006. config.mode = 0755;
  1007. config.hstate = &default_hstate;
  1008. config.min_hpages = -1; /* No default minimum size */
  1009. ret = hugetlbfs_parse_options(data, &config);
  1010. if (ret)
  1011. return ret;
  1012. sbinfo = kmalloc(sizeof(struct hugetlbfs_sb_info), GFP_KERNEL);
  1013. if (!sbinfo)
  1014. return -ENOMEM;
  1015. sb->s_fs_info = sbinfo;
  1016. sbinfo->hstate = config.hstate;
  1017. spin_lock_init(&sbinfo->stat_lock);
  1018. sbinfo->max_inodes = config.nr_inodes;
  1019. sbinfo->free_inodes = config.nr_inodes;
  1020. sbinfo->spool = NULL;
  1021. /*
  1022. * Allocate and initialize subpool if maximum or minimum size is
  1023. * specified. Any needed reservations (for minimim size) are taken
  1024. * taken when the subpool is created.
  1025. */
  1026. if (config.max_hpages != -1 || config.min_hpages != -1) {
  1027. sbinfo->spool = hugepage_new_subpool(config.hstate,
  1028. config.max_hpages,
  1029. config.min_hpages);
  1030. if (!sbinfo->spool)
  1031. goto out_free;
  1032. }
  1033. sb->s_maxbytes = MAX_LFS_FILESIZE;
  1034. sb->s_blocksize = huge_page_size(config.hstate);
  1035. sb->s_blocksize_bits = huge_page_shift(config.hstate);
  1036. sb->s_magic = HUGETLBFS_MAGIC;
  1037. sb->s_op = &hugetlbfs_ops;
  1038. sb->s_time_gran = 1;
  1039. sb->s_root = d_make_root(hugetlbfs_get_root(sb, &config));
  1040. if (!sb->s_root)
  1041. goto out_free;
  1042. return 0;
  1043. out_free:
  1044. kfree(sbinfo->spool);
  1045. kfree(sbinfo);
  1046. return -ENOMEM;
  1047. }
  1048. static struct dentry *hugetlbfs_mount(struct file_system_type *fs_type,
  1049. int flags, const char *dev_name, void *data)
  1050. {
  1051. return mount_nodev(fs_type, flags, data, hugetlbfs_fill_super);
  1052. }
  1053. static struct file_system_type hugetlbfs_fs_type = {
  1054. .name = "hugetlbfs",
  1055. .mount = hugetlbfs_mount,
  1056. .kill_sb = kill_litter_super,
  1057. };
  1058. static struct vfsmount *hugetlbfs_vfsmount[HUGE_MAX_HSTATE];
  1059. static int can_do_hugetlb_shm(void)
  1060. {
  1061. kgid_t shm_group;
  1062. shm_group = make_kgid(&init_user_ns, sysctl_hugetlb_shm_group);
  1063. return capable(CAP_IPC_LOCK) || in_group_p(shm_group);
  1064. }
  1065. static int get_hstate_idx(int page_size_log)
  1066. {
  1067. struct hstate *h = hstate_sizelog(page_size_log);
  1068. if (!h)
  1069. return -1;
  1070. return h - hstates;
  1071. }
  1072. static const struct dentry_operations anon_ops = {
  1073. .d_dname = simple_dname
  1074. };
  1075. /*
  1076. * Note that size should be aligned to proper hugepage size in caller side,
  1077. * otherwise hugetlb_reserve_pages reserves one less hugepages than intended.
  1078. */
  1079. struct file *hugetlb_file_setup(const char *name, size_t size,
  1080. vm_flags_t acctflag, struct user_struct **user,
  1081. int creat_flags, int page_size_log)
  1082. {
  1083. struct file *file = ERR_PTR(-ENOMEM);
  1084. struct inode *inode;
  1085. struct path path;
  1086. struct super_block *sb;
  1087. struct qstr quick_string;
  1088. int hstate_idx;
  1089. hstate_idx = get_hstate_idx(page_size_log);
  1090. if (hstate_idx < 0)
  1091. return ERR_PTR(-ENODEV);
  1092. *user = NULL;
  1093. if (!hugetlbfs_vfsmount[hstate_idx])
  1094. return ERR_PTR(-ENOENT);
  1095. if (creat_flags == HUGETLB_SHMFS_INODE && !can_do_hugetlb_shm()) {
  1096. *user = current_user();
  1097. if (user_shm_lock(size, *user)) {
  1098. task_lock(current);
  1099. pr_warn_once("%s (%d): Using mlock ulimits for SHM_HUGETLB is deprecated\n",
  1100. current->comm, current->pid);
  1101. task_unlock(current);
  1102. } else {
  1103. *user = NULL;
  1104. return ERR_PTR(-EPERM);
  1105. }
  1106. }
  1107. sb = hugetlbfs_vfsmount[hstate_idx]->mnt_sb;
  1108. quick_string.name = name;
  1109. quick_string.len = strlen(quick_string.name);
  1110. quick_string.hash = 0;
  1111. path.dentry = d_alloc_pseudo(sb, &quick_string);
  1112. if (!path.dentry)
  1113. goto out_shm_unlock;
  1114. d_set_d_op(path.dentry, &anon_ops);
  1115. path.mnt = mntget(hugetlbfs_vfsmount[hstate_idx]);
  1116. file = ERR_PTR(-ENOSPC);
  1117. inode = hugetlbfs_get_inode(sb, NULL, S_IFREG | S_IRWXUGO, 0);
  1118. if (!inode)
  1119. goto out_dentry;
  1120. if (creat_flags == HUGETLB_SHMFS_INODE)
  1121. inode->i_flags |= S_PRIVATE;
  1122. file = ERR_PTR(-ENOMEM);
  1123. if (hugetlb_reserve_pages(inode, 0,
  1124. size >> huge_page_shift(hstate_inode(inode)), NULL,
  1125. acctflag))
  1126. goto out_inode;
  1127. d_instantiate(path.dentry, inode);
  1128. inode->i_size = size;
  1129. clear_nlink(inode);
  1130. file = alloc_file(&path, FMODE_WRITE | FMODE_READ,
  1131. &hugetlbfs_file_operations);
  1132. if (IS_ERR(file))
  1133. goto out_dentry; /* inode is already attached */
  1134. return file;
  1135. out_inode:
  1136. iput(inode);
  1137. out_dentry:
  1138. path_put(&path);
  1139. out_shm_unlock:
  1140. if (*user) {
  1141. user_shm_unlock(size, *user);
  1142. *user = NULL;
  1143. }
  1144. return file;
  1145. }
  1146. static int __init init_hugetlbfs_fs(void)
  1147. {
  1148. struct hstate *h;
  1149. int error;
  1150. int i;
  1151. if (!hugepages_supported()) {
  1152. pr_info("disabling because there are no supported hugepage sizes\n");
  1153. return -ENOTSUPP;
  1154. }
  1155. error = -ENOMEM;
  1156. hugetlbfs_inode_cachep = kmem_cache_create("hugetlbfs_inode_cache",
  1157. sizeof(struct hugetlbfs_inode_info),
  1158. 0, SLAB_ACCOUNT, init_once);
  1159. if (hugetlbfs_inode_cachep == NULL)
  1160. goto out2;
  1161. error = register_filesystem(&hugetlbfs_fs_type);
  1162. if (error)
  1163. goto out;
  1164. i = 0;
  1165. for_each_hstate(h) {
  1166. char buf[50];
  1167. unsigned ps_kb = 1U << (h->order + PAGE_SHIFT - 10);
  1168. snprintf(buf, sizeof(buf), "pagesize=%uK", ps_kb);
  1169. hugetlbfs_vfsmount[i] = kern_mount_data(&hugetlbfs_fs_type,
  1170. buf);
  1171. if (IS_ERR(hugetlbfs_vfsmount[i])) {
  1172. pr_err("Cannot mount internal hugetlbfs for "
  1173. "page size %uK", ps_kb);
  1174. error = PTR_ERR(hugetlbfs_vfsmount[i]);
  1175. hugetlbfs_vfsmount[i] = NULL;
  1176. }
  1177. i++;
  1178. }
  1179. /* Non default hstates are optional */
  1180. if (!IS_ERR_OR_NULL(hugetlbfs_vfsmount[default_hstate_idx]))
  1181. return 0;
  1182. out:
  1183. kmem_cache_destroy(hugetlbfs_inode_cachep);
  1184. out2:
  1185. return error;
  1186. }
  1187. fs_initcall(init_hugetlbfs_fs)