inode.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415
  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. struct hstate *hstate;
  45. long max_hpages;
  46. long nr_inodes;
  47. long min_hpages;
  48. kuid_t uid;
  49. kgid_t gid;
  50. umode_t mode;
  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_cached *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, index;
  357. int i, freed = 0;
  358. bool truncate_op = (lend == LLONG_MAX);
  359. memset(&pseudo_vma, 0, sizeof(struct vm_area_struct));
  360. pseudo_vma.vm_flags = (VM_HUGETLB | VM_MAYSHARE | VM_SHARED);
  361. pagevec_init(&pvec);
  362. next = start;
  363. while (next < end) {
  364. /*
  365. * When no more pages are found, we are done.
  366. */
  367. if (!pagevec_lookup_range(&pvec, mapping, &next, end - 1))
  368. break;
  369. for (i = 0; i < pagevec_count(&pvec); ++i) {
  370. struct page *page = pvec.pages[i];
  371. u32 hash;
  372. index = page->index;
  373. hash = hugetlb_fault_mutex_hash(h, current->mm,
  374. &pseudo_vma,
  375. mapping, index, 0);
  376. mutex_lock(&hugetlb_fault_mutex_table[hash]);
  377. /*
  378. * If page is mapped, it was faulted in after being
  379. * unmapped in caller. Unmap (again) now after taking
  380. * the fault mutex. The mutex will prevent faults
  381. * until we finish removing the page.
  382. *
  383. * This race can only happen in the hole punch case.
  384. * Getting here in a truncate operation is a bug.
  385. */
  386. if (unlikely(page_mapped(page))) {
  387. BUG_ON(truncate_op);
  388. i_mmap_lock_write(mapping);
  389. hugetlb_vmdelete_list(&mapping->i_mmap,
  390. index * pages_per_huge_page(h),
  391. (index + 1) * pages_per_huge_page(h));
  392. i_mmap_unlock_write(mapping);
  393. }
  394. lock_page(page);
  395. /*
  396. * We must free the huge page and remove from page
  397. * cache (remove_huge_page) BEFORE removing the
  398. * region/reserve map (hugetlb_unreserve_pages). In
  399. * rare out of memory conditions, removal of the
  400. * region/reserve map could fail. Correspondingly,
  401. * the subpool and global reserve usage count can need
  402. * to be adjusted.
  403. */
  404. VM_BUG_ON(PagePrivate(page));
  405. remove_huge_page(page);
  406. freed++;
  407. if (!truncate_op) {
  408. if (unlikely(hugetlb_unreserve_pages(inode,
  409. index, index + 1, 1)))
  410. hugetlb_fix_reserve_counts(inode);
  411. }
  412. unlock_page(page);
  413. mutex_unlock(&hugetlb_fault_mutex_table[hash]);
  414. }
  415. huge_pagevec_release(&pvec);
  416. cond_resched();
  417. }
  418. if (truncate_op)
  419. (void)hugetlb_unreserve_pages(inode, start, LONG_MAX, freed);
  420. }
  421. static void hugetlbfs_evict_inode(struct inode *inode)
  422. {
  423. struct resv_map *resv_map;
  424. remove_inode_hugepages(inode, 0, LLONG_MAX);
  425. resv_map = (struct resv_map *)inode->i_mapping->private_data;
  426. /* root inode doesn't have the resv_map, so we should check it */
  427. if (resv_map)
  428. resv_map_release(&resv_map->refs);
  429. clear_inode(inode);
  430. }
  431. static int hugetlb_vmtruncate(struct inode *inode, loff_t offset)
  432. {
  433. pgoff_t pgoff;
  434. struct address_space *mapping = inode->i_mapping;
  435. struct hstate *h = hstate_inode(inode);
  436. BUG_ON(offset & ~huge_page_mask(h));
  437. pgoff = offset >> PAGE_SHIFT;
  438. i_size_write(inode, offset);
  439. i_mmap_lock_write(mapping);
  440. if (!RB_EMPTY_ROOT(&mapping->i_mmap.rb_root))
  441. hugetlb_vmdelete_list(&mapping->i_mmap, pgoff, 0);
  442. i_mmap_unlock_write(mapping);
  443. remove_inode_hugepages(inode, offset, LLONG_MAX);
  444. return 0;
  445. }
  446. static long hugetlbfs_punch_hole(struct inode *inode, loff_t offset, loff_t len)
  447. {
  448. struct hstate *h = hstate_inode(inode);
  449. loff_t hpage_size = huge_page_size(h);
  450. loff_t hole_start, hole_end;
  451. /*
  452. * For hole punch round up the beginning offset of the hole and
  453. * round down the end.
  454. */
  455. hole_start = round_up(offset, hpage_size);
  456. hole_end = round_down(offset + len, hpage_size);
  457. if (hole_end > hole_start) {
  458. struct address_space *mapping = inode->i_mapping;
  459. inode_lock(inode);
  460. i_mmap_lock_write(mapping);
  461. if (!RB_EMPTY_ROOT(&mapping->i_mmap.rb_root))
  462. hugetlb_vmdelete_list(&mapping->i_mmap,
  463. hole_start >> PAGE_SHIFT,
  464. hole_end >> PAGE_SHIFT);
  465. i_mmap_unlock_write(mapping);
  466. remove_inode_hugepages(inode, hole_start, hole_end);
  467. inode_unlock(inode);
  468. }
  469. return 0;
  470. }
  471. static long hugetlbfs_fallocate(struct file *file, int mode, loff_t offset,
  472. loff_t len)
  473. {
  474. struct inode *inode = file_inode(file);
  475. struct address_space *mapping = inode->i_mapping;
  476. struct hstate *h = hstate_inode(inode);
  477. struct vm_area_struct pseudo_vma;
  478. struct mm_struct *mm = current->mm;
  479. loff_t hpage_size = huge_page_size(h);
  480. unsigned long hpage_shift = huge_page_shift(h);
  481. pgoff_t start, index, end;
  482. int error;
  483. u32 hash;
  484. if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
  485. return -EOPNOTSUPP;
  486. if (mode & FALLOC_FL_PUNCH_HOLE)
  487. return hugetlbfs_punch_hole(inode, offset, len);
  488. /*
  489. * Default preallocate case.
  490. * For this range, start is rounded down and end is rounded up
  491. * as well as being converted to page offsets.
  492. */
  493. start = offset >> hpage_shift;
  494. end = (offset + len + hpage_size - 1) >> hpage_shift;
  495. inode_lock(inode);
  496. /* We need to check rlimit even when FALLOC_FL_KEEP_SIZE */
  497. error = inode_newsize_ok(inode, offset + len);
  498. if (error)
  499. goto out;
  500. /*
  501. * Initialize a pseudo vma as this is required by the huge page
  502. * allocation routines. If NUMA is configured, use page index
  503. * as input to create an allocation policy.
  504. */
  505. memset(&pseudo_vma, 0, sizeof(struct vm_area_struct));
  506. pseudo_vma.vm_flags = (VM_HUGETLB | VM_MAYSHARE | VM_SHARED);
  507. pseudo_vma.vm_file = file;
  508. for (index = start; index < end; index++) {
  509. /*
  510. * This is supposed to be the vaddr where the page is being
  511. * faulted in, but we have no vaddr here.
  512. */
  513. struct page *page;
  514. unsigned long addr;
  515. int avoid_reserve = 0;
  516. cond_resched();
  517. /*
  518. * fallocate(2) manpage permits EINTR; we may have been
  519. * interrupted because we are using up too much memory.
  520. */
  521. if (signal_pending(current)) {
  522. error = -EINTR;
  523. break;
  524. }
  525. /* Set numa allocation policy based on index */
  526. hugetlb_set_vma_policy(&pseudo_vma, inode, index);
  527. /* addr is the offset within the file (zero based) */
  528. addr = index * hpage_size;
  529. /* mutex taken here, fault path and hole punch */
  530. hash = hugetlb_fault_mutex_hash(h, mm, &pseudo_vma, mapping,
  531. index, addr);
  532. mutex_lock(&hugetlb_fault_mutex_table[hash]);
  533. /* See if already present in mapping to avoid alloc/free */
  534. page = find_get_page(mapping, index);
  535. if (page) {
  536. put_page(page);
  537. mutex_unlock(&hugetlb_fault_mutex_table[hash]);
  538. hugetlb_drop_vma_policy(&pseudo_vma);
  539. continue;
  540. }
  541. /* Allocate page and add to page cache */
  542. page = alloc_huge_page(&pseudo_vma, addr, avoid_reserve);
  543. hugetlb_drop_vma_policy(&pseudo_vma);
  544. if (IS_ERR(page)) {
  545. mutex_unlock(&hugetlb_fault_mutex_table[hash]);
  546. error = PTR_ERR(page);
  547. goto out;
  548. }
  549. clear_huge_page(page, addr, pages_per_huge_page(h));
  550. __SetPageUptodate(page);
  551. error = huge_add_to_page_cache(page, mapping, index);
  552. if (unlikely(error)) {
  553. put_page(page);
  554. mutex_unlock(&hugetlb_fault_mutex_table[hash]);
  555. goto out;
  556. }
  557. mutex_unlock(&hugetlb_fault_mutex_table[hash]);
  558. /*
  559. * page_put due to reference from alloc_huge_page()
  560. * unlock_page because locked by add_to_page_cache()
  561. */
  562. put_page(page);
  563. unlock_page(page);
  564. }
  565. if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size)
  566. i_size_write(inode, offset + len);
  567. inode->i_ctime = current_time(inode);
  568. out:
  569. inode_unlock(inode);
  570. return error;
  571. }
  572. static int hugetlbfs_setattr(struct dentry *dentry, struct iattr *attr)
  573. {
  574. struct inode *inode = d_inode(dentry);
  575. struct hstate *h = hstate_inode(inode);
  576. int error;
  577. unsigned int ia_valid = attr->ia_valid;
  578. BUG_ON(!inode);
  579. error = setattr_prepare(dentry, attr);
  580. if (error)
  581. return error;
  582. if (ia_valid & ATTR_SIZE) {
  583. if (attr->ia_size & ~huge_page_mask(h))
  584. return -EINVAL;
  585. error = hugetlb_vmtruncate(inode, attr->ia_size);
  586. if (error)
  587. return error;
  588. }
  589. setattr_copy(inode, attr);
  590. mark_inode_dirty(inode);
  591. return 0;
  592. }
  593. static struct inode *hugetlbfs_get_root(struct super_block *sb,
  594. struct hugetlbfs_config *config)
  595. {
  596. struct inode *inode;
  597. inode = new_inode(sb);
  598. if (inode) {
  599. inode->i_ino = get_next_ino();
  600. inode->i_mode = S_IFDIR | config->mode;
  601. inode->i_uid = config->uid;
  602. inode->i_gid = config->gid;
  603. inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
  604. inode->i_op = &hugetlbfs_dir_inode_operations;
  605. inode->i_fop = &simple_dir_operations;
  606. /* directory inodes start off with i_nlink == 2 (for "." entry) */
  607. inc_nlink(inode);
  608. lockdep_annotate_inode_mutex_key(inode);
  609. }
  610. return inode;
  611. }
  612. /*
  613. * Hugetlbfs is not reclaimable; therefore its i_mmap_rwsem will never
  614. * be taken from reclaim -- unlike regular filesystems. This needs an
  615. * annotation because huge_pmd_share() does an allocation under hugetlb's
  616. * i_mmap_rwsem.
  617. */
  618. static struct lock_class_key hugetlbfs_i_mmap_rwsem_key;
  619. static struct inode *hugetlbfs_get_inode(struct super_block *sb,
  620. struct inode *dir,
  621. umode_t mode, dev_t dev)
  622. {
  623. struct inode *inode;
  624. struct resv_map *resv_map;
  625. resv_map = resv_map_alloc();
  626. if (!resv_map)
  627. return NULL;
  628. inode = new_inode(sb);
  629. if (inode) {
  630. inode->i_ino = get_next_ino();
  631. inode_init_owner(inode, dir, mode);
  632. lockdep_set_class(&inode->i_mapping->i_mmap_rwsem,
  633. &hugetlbfs_i_mmap_rwsem_key);
  634. inode->i_mapping->a_ops = &hugetlbfs_aops;
  635. inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
  636. inode->i_mapping->private_data = resv_map;
  637. switch (mode & S_IFMT) {
  638. default:
  639. init_special_inode(inode, mode, dev);
  640. break;
  641. case S_IFREG:
  642. inode->i_op = &hugetlbfs_inode_operations;
  643. inode->i_fop = &hugetlbfs_file_operations;
  644. break;
  645. case S_IFDIR:
  646. inode->i_op = &hugetlbfs_dir_inode_operations;
  647. inode->i_fop = &simple_dir_operations;
  648. /* directory inodes start off with i_nlink == 2 (for "." entry) */
  649. inc_nlink(inode);
  650. break;
  651. case S_IFLNK:
  652. inode->i_op = &page_symlink_inode_operations;
  653. inode_nohighmem(inode);
  654. break;
  655. }
  656. lockdep_annotate_inode_mutex_key(inode);
  657. } else
  658. kref_put(&resv_map->refs, resv_map_release);
  659. return inode;
  660. }
  661. /*
  662. * File creation. Allocate an inode, and we're done..
  663. */
  664. static int hugetlbfs_mknod(struct inode *dir,
  665. struct dentry *dentry, umode_t mode, dev_t dev)
  666. {
  667. struct inode *inode;
  668. int error = -ENOSPC;
  669. inode = hugetlbfs_get_inode(dir->i_sb, dir, mode, dev);
  670. if (inode) {
  671. dir->i_ctime = dir->i_mtime = current_time(dir);
  672. d_instantiate(dentry, inode);
  673. dget(dentry); /* Extra count - pin the dentry in core */
  674. error = 0;
  675. }
  676. return error;
  677. }
  678. static int hugetlbfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  679. {
  680. int retval = hugetlbfs_mknod(dir, dentry, mode | S_IFDIR, 0);
  681. if (!retval)
  682. inc_nlink(dir);
  683. return retval;
  684. }
  685. static int hugetlbfs_create(struct inode *dir, struct dentry *dentry, umode_t mode, bool excl)
  686. {
  687. return hugetlbfs_mknod(dir, dentry, mode | S_IFREG, 0);
  688. }
  689. static int hugetlbfs_symlink(struct inode *dir,
  690. struct dentry *dentry, const char *symname)
  691. {
  692. struct inode *inode;
  693. int error = -ENOSPC;
  694. inode = hugetlbfs_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0);
  695. if (inode) {
  696. int l = strlen(symname)+1;
  697. error = page_symlink(inode, symname, l);
  698. if (!error) {
  699. d_instantiate(dentry, inode);
  700. dget(dentry);
  701. } else
  702. iput(inode);
  703. }
  704. dir->i_ctime = dir->i_mtime = current_time(dir);
  705. return error;
  706. }
  707. /*
  708. * mark the head page dirty
  709. */
  710. static int hugetlbfs_set_page_dirty(struct page *page)
  711. {
  712. struct page *head = compound_head(page);
  713. SetPageDirty(head);
  714. return 0;
  715. }
  716. static int hugetlbfs_migrate_page(struct address_space *mapping,
  717. struct page *newpage, struct page *page,
  718. enum migrate_mode mode)
  719. {
  720. int rc;
  721. rc = migrate_huge_page_move_mapping(mapping, newpage, page);
  722. if (rc != MIGRATEPAGE_SUCCESS)
  723. return rc;
  724. if (mode != MIGRATE_SYNC_NO_COPY)
  725. migrate_page_copy(newpage, page);
  726. else
  727. migrate_page_states(newpage, page);
  728. return MIGRATEPAGE_SUCCESS;
  729. }
  730. static int hugetlbfs_error_remove_page(struct address_space *mapping,
  731. struct page *page)
  732. {
  733. struct inode *inode = mapping->host;
  734. pgoff_t index = page->index;
  735. remove_huge_page(page);
  736. if (unlikely(hugetlb_unreserve_pages(inode, index, index + 1, 1)))
  737. hugetlb_fix_reserve_counts(inode);
  738. return 0;
  739. }
  740. /*
  741. * Display the mount options in /proc/mounts.
  742. */
  743. static int hugetlbfs_show_options(struct seq_file *m, struct dentry *root)
  744. {
  745. struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(root->d_sb);
  746. struct hugepage_subpool *spool = sbinfo->spool;
  747. unsigned long hpage_size = huge_page_size(sbinfo->hstate);
  748. unsigned hpage_shift = huge_page_shift(sbinfo->hstate);
  749. char mod;
  750. if (!uid_eq(sbinfo->uid, GLOBAL_ROOT_UID))
  751. seq_printf(m, ",uid=%u",
  752. from_kuid_munged(&init_user_ns, sbinfo->uid));
  753. if (!gid_eq(sbinfo->gid, GLOBAL_ROOT_GID))
  754. seq_printf(m, ",gid=%u",
  755. from_kgid_munged(&init_user_ns, sbinfo->gid));
  756. if (sbinfo->mode != 0755)
  757. seq_printf(m, ",mode=%o", sbinfo->mode);
  758. if (sbinfo->max_inodes != -1)
  759. seq_printf(m, ",nr_inodes=%lu", sbinfo->max_inodes);
  760. hpage_size /= 1024;
  761. mod = 'K';
  762. if (hpage_size >= 1024) {
  763. hpage_size /= 1024;
  764. mod = 'M';
  765. }
  766. seq_printf(m, ",pagesize=%lu%c", hpage_size, mod);
  767. if (spool) {
  768. if (spool->max_hpages != -1)
  769. seq_printf(m, ",size=%llu",
  770. (unsigned long long)spool->max_hpages << hpage_shift);
  771. if (spool->min_hpages != -1)
  772. seq_printf(m, ",min_size=%llu",
  773. (unsigned long long)spool->min_hpages << hpage_shift);
  774. }
  775. return 0;
  776. }
  777. static int hugetlbfs_statfs(struct dentry *dentry, struct kstatfs *buf)
  778. {
  779. struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(dentry->d_sb);
  780. struct hstate *h = hstate_inode(d_inode(dentry));
  781. buf->f_type = HUGETLBFS_MAGIC;
  782. buf->f_bsize = huge_page_size(h);
  783. if (sbinfo) {
  784. spin_lock(&sbinfo->stat_lock);
  785. /* If no limits set, just report 0 for max/free/used
  786. * blocks, like simple_statfs() */
  787. if (sbinfo->spool) {
  788. long free_pages;
  789. spin_lock(&sbinfo->spool->lock);
  790. buf->f_blocks = sbinfo->spool->max_hpages;
  791. free_pages = sbinfo->spool->max_hpages
  792. - sbinfo->spool->used_hpages;
  793. buf->f_bavail = buf->f_bfree = free_pages;
  794. spin_unlock(&sbinfo->spool->lock);
  795. buf->f_files = sbinfo->max_inodes;
  796. buf->f_ffree = sbinfo->free_inodes;
  797. }
  798. spin_unlock(&sbinfo->stat_lock);
  799. }
  800. buf->f_namelen = NAME_MAX;
  801. return 0;
  802. }
  803. static void hugetlbfs_put_super(struct super_block *sb)
  804. {
  805. struct hugetlbfs_sb_info *sbi = HUGETLBFS_SB(sb);
  806. if (sbi) {
  807. sb->s_fs_info = NULL;
  808. if (sbi->spool)
  809. hugepage_put_subpool(sbi->spool);
  810. kfree(sbi);
  811. }
  812. }
  813. static inline int hugetlbfs_dec_free_inodes(struct hugetlbfs_sb_info *sbinfo)
  814. {
  815. if (sbinfo->free_inodes >= 0) {
  816. spin_lock(&sbinfo->stat_lock);
  817. if (unlikely(!sbinfo->free_inodes)) {
  818. spin_unlock(&sbinfo->stat_lock);
  819. return 0;
  820. }
  821. sbinfo->free_inodes--;
  822. spin_unlock(&sbinfo->stat_lock);
  823. }
  824. return 1;
  825. }
  826. static void hugetlbfs_inc_free_inodes(struct hugetlbfs_sb_info *sbinfo)
  827. {
  828. if (sbinfo->free_inodes >= 0) {
  829. spin_lock(&sbinfo->stat_lock);
  830. sbinfo->free_inodes++;
  831. spin_unlock(&sbinfo->stat_lock);
  832. }
  833. }
  834. static struct kmem_cache *hugetlbfs_inode_cachep;
  835. static struct inode *hugetlbfs_alloc_inode(struct super_block *sb)
  836. {
  837. struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(sb);
  838. struct hugetlbfs_inode_info *p;
  839. if (unlikely(!hugetlbfs_dec_free_inodes(sbinfo)))
  840. return NULL;
  841. p = kmem_cache_alloc(hugetlbfs_inode_cachep, GFP_KERNEL);
  842. if (unlikely(!p)) {
  843. hugetlbfs_inc_free_inodes(sbinfo);
  844. return NULL;
  845. }
  846. /*
  847. * Any time after allocation, hugetlbfs_destroy_inode can be called
  848. * for the inode. mpol_free_shared_policy is unconditionally called
  849. * as part of hugetlbfs_destroy_inode. So, initialize policy here
  850. * in case of a quick call to destroy.
  851. *
  852. * Note that the policy is initialized even if we are creating a
  853. * private inode. This simplifies hugetlbfs_destroy_inode.
  854. */
  855. mpol_shared_policy_init(&p->policy, NULL);
  856. return &p->vfs_inode;
  857. }
  858. static void hugetlbfs_i_callback(struct rcu_head *head)
  859. {
  860. struct inode *inode = container_of(head, struct inode, i_rcu);
  861. kmem_cache_free(hugetlbfs_inode_cachep, HUGETLBFS_I(inode));
  862. }
  863. static void hugetlbfs_destroy_inode(struct inode *inode)
  864. {
  865. hugetlbfs_inc_free_inodes(HUGETLBFS_SB(inode->i_sb));
  866. mpol_free_shared_policy(&HUGETLBFS_I(inode)->policy);
  867. call_rcu(&inode->i_rcu, hugetlbfs_i_callback);
  868. }
  869. static const struct address_space_operations hugetlbfs_aops = {
  870. .write_begin = hugetlbfs_write_begin,
  871. .write_end = hugetlbfs_write_end,
  872. .set_page_dirty = hugetlbfs_set_page_dirty,
  873. .migratepage = hugetlbfs_migrate_page,
  874. .error_remove_page = hugetlbfs_error_remove_page,
  875. };
  876. static void init_once(void *foo)
  877. {
  878. struct hugetlbfs_inode_info *ei = (struct hugetlbfs_inode_info *)foo;
  879. inode_init_once(&ei->vfs_inode);
  880. }
  881. const struct file_operations hugetlbfs_file_operations = {
  882. .read_iter = hugetlbfs_read_iter,
  883. .mmap = hugetlbfs_file_mmap,
  884. .fsync = noop_fsync,
  885. .get_unmapped_area = hugetlb_get_unmapped_area,
  886. .llseek = default_llseek,
  887. .fallocate = hugetlbfs_fallocate,
  888. };
  889. static const struct inode_operations hugetlbfs_dir_inode_operations = {
  890. .create = hugetlbfs_create,
  891. .lookup = simple_lookup,
  892. .link = simple_link,
  893. .unlink = simple_unlink,
  894. .symlink = hugetlbfs_symlink,
  895. .mkdir = hugetlbfs_mkdir,
  896. .rmdir = simple_rmdir,
  897. .mknod = hugetlbfs_mknod,
  898. .rename = simple_rename,
  899. .setattr = hugetlbfs_setattr,
  900. };
  901. static const struct inode_operations hugetlbfs_inode_operations = {
  902. .setattr = hugetlbfs_setattr,
  903. };
  904. static const struct super_operations hugetlbfs_ops = {
  905. .alloc_inode = hugetlbfs_alloc_inode,
  906. .destroy_inode = hugetlbfs_destroy_inode,
  907. .evict_inode = hugetlbfs_evict_inode,
  908. .statfs = hugetlbfs_statfs,
  909. .put_super = hugetlbfs_put_super,
  910. .show_options = hugetlbfs_show_options,
  911. };
  912. enum hugetlbfs_size_type { NO_SIZE, SIZE_STD, SIZE_PERCENT };
  913. /*
  914. * Convert size option passed from command line to number of huge pages
  915. * in the pool specified by hstate. Size option could be in bytes
  916. * (val_type == SIZE_STD) or percentage of the pool (val_type == SIZE_PERCENT).
  917. */
  918. static long
  919. hugetlbfs_size_to_hpages(struct hstate *h, unsigned long long size_opt,
  920. enum hugetlbfs_size_type val_type)
  921. {
  922. if (val_type == NO_SIZE)
  923. return -1;
  924. if (val_type == SIZE_PERCENT) {
  925. size_opt <<= huge_page_shift(h);
  926. size_opt *= h->max_huge_pages;
  927. do_div(size_opt, 100);
  928. }
  929. size_opt >>= huge_page_shift(h);
  930. return size_opt;
  931. }
  932. static int
  933. hugetlbfs_parse_options(char *options, struct hugetlbfs_config *pconfig)
  934. {
  935. char *p, *rest;
  936. substring_t args[MAX_OPT_ARGS];
  937. int option;
  938. unsigned long long max_size_opt = 0, min_size_opt = 0;
  939. enum hugetlbfs_size_type max_val_type = NO_SIZE, min_val_type = NO_SIZE;
  940. if (!options)
  941. return 0;
  942. while ((p = strsep(&options, ",")) != NULL) {
  943. int token;
  944. if (!*p)
  945. continue;
  946. token = match_token(p, tokens, args);
  947. switch (token) {
  948. case Opt_uid:
  949. if (match_int(&args[0], &option))
  950. goto bad_val;
  951. pconfig->uid = make_kuid(current_user_ns(), option);
  952. if (!uid_valid(pconfig->uid))
  953. goto bad_val;
  954. break;
  955. case Opt_gid:
  956. if (match_int(&args[0], &option))
  957. goto bad_val;
  958. pconfig->gid = make_kgid(current_user_ns(), option);
  959. if (!gid_valid(pconfig->gid))
  960. goto bad_val;
  961. break;
  962. case Opt_mode:
  963. if (match_octal(&args[0], &option))
  964. goto bad_val;
  965. pconfig->mode = option & 01777U;
  966. break;
  967. case Opt_size: {
  968. /* memparse() will accept a K/M/G without a digit */
  969. if (!isdigit(*args[0].from))
  970. goto bad_val;
  971. max_size_opt = memparse(args[0].from, &rest);
  972. max_val_type = SIZE_STD;
  973. if (*rest == '%')
  974. max_val_type = SIZE_PERCENT;
  975. break;
  976. }
  977. case Opt_nr_inodes:
  978. /* memparse() will accept a K/M/G without a digit */
  979. if (!isdigit(*args[0].from))
  980. goto bad_val;
  981. pconfig->nr_inodes = memparse(args[0].from, &rest);
  982. break;
  983. case Opt_pagesize: {
  984. unsigned long ps;
  985. ps = memparse(args[0].from, &rest);
  986. pconfig->hstate = size_to_hstate(ps);
  987. if (!pconfig->hstate) {
  988. pr_err("Unsupported page size %lu MB\n",
  989. ps >> 20);
  990. return -EINVAL;
  991. }
  992. break;
  993. }
  994. case Opt_min_size: {
  995. /* memparse() will accept a K/M/G without a digit */
  996. if (!isdigit(*args[0].from))
  997. goto bad_val;
  998. min_size_opt = memparse(args[0].from, &rest);
  999. min_val_type = SIZE_STD;
  1000. if (*rest == '%')
  1001. min_val_type = SIZE_PERCENT;
  1002. break;
  1003. }
  1004. default:
  1005. pr_err("Bad mount option: \"%s\"\n", p);
  1006. return -EINVAL;
  1007. break;
  1008. }
  1009. }
  1010. /*
  1011. * Use huge page pool size (in hstate) to convert the size
  1012. * options to number of huge pages. If NO_SIZE, -1 is returned.
  1013. */
  1014. pconfig->max_hpages = hugetlbfs_size_to_hpages(pconfig->hstate,
  1015. max_size_opt, max_val_type);
  1016. pconfig->min_hpages = hugetlbfs_size_to_hpages(pconfig->hstate,
  1017. min_size_opt, min_val_type);
  1018. /*
  1019. * If max_size was specified, then min_size must be smaller
  1020. */
  1021. if (max_val_type > NO_SIZE &&
  1022. pconfig->min_hpages > pconfig->max_hpages) {
  1023. pr_err("minimum size can not be greater than maximum size\n");
  1024. return -EINVAL;
  1025. }
  1026. return 0;
  1027. bad_val:
  1028. pr_err("Bad value '%s' for mount option '%s'\n", args[0].from, p);
  1029. return -EINVAL;
  1030. }
  1031. static int
  1032. hugetlbfs_fill_super(struct super_block *sb, void *data, int silent)
  1033. {
  1034. int ret;
  1035. struct hugetlbfs_config config;
  1036. struct hugetlbfs_sb_info *sbinfo;
  1037. config.max_hpages = -1; /* No limit on size by default */
  1038. config.nr_inodes = -1; /* No limit on number of inodes by default */
  1039. config.uid = current_fsuid();
  1040. config.gid = current_fsgid();
  1041. config.mode = 0755;
  1042. config.hstate = &default_hstate;
  1043. config.min_hpages = -1; /* No default minimum size */
  1044. ret = hugetlbfs_parse_options(data, &config);
  1045. if (ret)
  1046. return ret;
  1047. sbinfo = kmalloc(sizeof(struct hugetlbfs_sb_info), GFP_KERNEL);
  1048. if (!sbinfo)
  1049. return -ENOMEM;
  1050. sb->s_fs_info = sbinfo;
  1051. sbinfo->hstate = config.hstate;
  1052. spin_lock_init(&sbinfo->stat_lock);
  1053. sbinfo->max_inodes = config.nr_inodes;
  1054. sbinfo->free_inodes = config.nr_inodes;
  1055. sbinfo->spool = NULL;
  1056. sbinfo->uid = config.uid;
  1057. sbinfo->gid = config.gid;
  1058. sbinfo->mode = config.mode;
  1059. /*
  1060. * Allocate and initialize subpool if maximum or minimum size is
  1061. * specified. Any needed reservations (for minimim size) are taken
  1062. * taken when the subpool is created.
  1063. */
  1064. if (config.max_hpages != -1 || config.min_hpages != -1) {
  1065. sbinfo->spool = hugepage_new_subpool(config.hstate,
  1066. config.max_hpages,
  1067. config.min_hpages);
  1068. if (!sbinfo->spool)
  1069. goto out_free;
  1070. }
  1071. sb->s_maxbytes = MAX_LFS_FILESIZE;
  1072. sb->s_blocksize = huge_page_size(config.hstate);
  1073. sb->s_blocksize_bits = huge_page_shift(config.hstate);
  1074. sb->s_magic = HUGETLBFS_MAGIC;
  1075. sb->s_op = &hugetlbfs_ops;
  1076. sb->s_time_gran = 1;
  1077. sb->s_root = d_make_root(hugetlbfs_get_root(sb, &config));
  1078. if (!sb->s_root)
  1079. goto out_free;
  1080. return 0;
  1081. out_free:
  1082. kfree(sbinfo->spool);
  1083. kfree(sbinfo);
  1084. return -ENOMEM;
  1085. }
  1086. static struct dentry *hugetlbfs_mount(struct file_system_type *fs_type,
  1087. int flags, const char *dev_name, void *data)
  1088. {
  1089. return mount_nodev(fs_type, flags, data, hugetlbfs_fill_super);
  1090. }
  1091. static struct file_system_type hugetlbfs_fs_type = {
  1092. .name = "hugetlbfs",
  1093. .mount = hugetlbfs_mount,
  1094. .kill_sb = kill_litter_super,
  1095. };
  1096. static struct vfsmount *hugetlbfs_vfsmount[HUGE_MAX_HSTATE];
  1097. static int can_do_hugetlb_shm(void)
  1098. {
  1099. kgid_t shm_group;
  1100. shm_group = make_kgid(&init_user_ns, sysctl_hugetlb_shm_group);
  1101. return capable(CAP_IPC_LOCK) || in_group_p(shm_group);
  1102. }
  1103. static int get_hstate_idx(int page_size_log)
  1104. {
  1105. struct hstate *h = hstate_sizelog(page_size_log);
  1106. if (!h)
  1107. return -1;
  1108. return h - hstates;
  1109. }
  1110. static const struct dentry_operations anon_ops = {
  1111. .d_dname = simple_dname
  1112. };
  1113. /*
  1114. * Note that size should be aligned to proper hugepage size in caller side,
  1115. * otherwise hugetlb_reserve_pages reserves one less hugepages than intended.
  1116. */
  1117. struct file *hugetlb_file_setup(const char *name, size_t size,
  1118. vm_flags_t acctflag, struct user_struct **user,
  1119. int creat_flags, int page_size_log)
  1120. {
  1121. struct file *file = ERR_PTR(-ENOMEM);
  1122. struct inode *inode;
  1123. struct path path;
  1124. struct super_block *sb;
  1125. struct qstr quick_string;
  1126. int hstate_idx;
  1127. hstate_idx = get_hstate_idx(page_size_log);
  1128. if (hstate_idx < 0)
  1129. return ERR_PTR(-ENODEV);
  1130. *user = NULL;
  1131. if (!hugetlbfs_vfsmount[hstate_idx])
  1132. return ERR_PTR(-ENOENT);
  1133. if (creat_flags == HUGETLB_SHMFS_INODE && !can_do_hugetlb_shm()) {
  1134. *user = current_user();
  1135. if (user_shm_lock(size, *user)) {
  1136. task_lock(current);
  1137. pr_warn_once("%s (%d): Using mlock ulimits for SHM_HUGETLB is deprecated\n",
  1138. current->comm, current->pid);
  1139. task_unlock(current);
  1140. } else {
  1141. *user = NULL;
  1142. return ERR_PTR(-EPERM);
  1143. }
  1144. }
  1145. sb = hugetlbfs_vfsmount[hstate_idx]->mnt_sb;
  1146. quick_string.name = name;
  1147. quick_string.len = strlen(quick_string.name);
  1148. quick_string.hash = 0;
  1149. path.dentry = d_alloc_pseudo(sb, &quick_string);
  1150. if (!path.dentry)
  1151. goto out_shm_unlock;
  1152. d_set_d_op(path.dentry, &anon_ops);
  1153. path.mnt = mntget(hugetlbfs_vfsmount[hstate_idx]);
  1154. file = ERR_PTR(-ENOSPC);
  1155. inode = hugetlbfs_get_inode(sb, NULL, S_IFREG | S_IRWXUGO, 0);
  1156. if (!inode)
  1157. goto out_dentry;
  1158. if (creat_flags == HUGETLB_SHMFS_INODE)
  1159. inode->i_flags |= S_PRIVATE;
  1160. file = ERR_PTR(-ENOMEM);
  1161. if (hugetlb_reserve_pages(inode, 0,
  1162. size >> huge_page_shift(hstate_inode(inode)), NULL,
  1163. acctflag))
  1164. goto out_inode;
  1165. d_instantiate(path.dentry, inode);
  1166. inode->i_size = size;
  1167. clear_nlink(inode);
  1168. file = alloc_file(&path, FMODE_WRITE | FMODE_READ,
  1169. &hugetlbfs_file_operations);
  1170. if (IS_ERR(file))
  1171. goto out_dentry; /* inode is already attached */
  1172. return file;
  1173. out_inode:
  1174. iput(inode);
  1175. out_dentry:
  1176. path_put(&path);
  1177. out_shm_unlock:
  1178. if (*user) {
  1179. user_shm_unlock(size, *user);
  1180. *user = NULL;
  1181. }
  1182. return file;
  1183. }
  1184. static int __init init_hugetlbfs_fs(void)
  1185. {
  1186. struct hstate *h;
  1187. int error;
  1188. int i;
  1189. if (!hugepages_supported()) {
  1190. pr_info("disabling because there are no supported hugepage sizes\n");
  1191. return -ENOTSUPP;
  1192. }
  1193. error = -ENOMEM;
  1194. hugetlbfs_inode_cachep = kmem_cache_create("hugetlbfs_inode_cache",
  1195. sizeof(struct hugetlbfs_inode_info),
  1196. 0, SLAB_ACCOUNT, init_once);
  1197. if (hugetlbfs_inode_cachep == NULL)
  1198. goto out2;
  1199. error = register_filesystem(&hugetlbfs_fs_type);
  1200. if (error)
  1201. goto out;
  1202. i = 0;
  1203. for_each_hstate(h) {
  1204. char buf[50];
  1205. unsigned ps_kb = 1U << (h->order + PAGE_SHIFT - 10);
  1206. snprintf(buf, sizeof(buf), "pagesize=%uK", ps_kb);
  1207. hugetlbfs_vfsmount[i] = kern_mount_data(&hugetlbfs_fs_type,
  1208. buf);
  1209. if (IS_ERR(hugetlbfs_vfsmount[i])) {
  1210. pr_err("Cannot mount internal hugetlbfs for "
  1211. "page size %uK", ps_kb);
  1212. error = PTR_ERR(hugetlbfs_vfsmount[i]);
  1213. hugetlbfs_vfsmount[i] = NULL;
  1214. }
  1215. i++;
  1216. }
  1217. /* Non default hstates are optional */
  1218. if (!IS_ERR_OR_NULL(hugetlbfs_vfsmount[default_hstate_idx]))
  1219. return 0;
  1220. out:
  1221. kmem_cache_destroy(hugetlbfs_inode_cachep);
  1222. out2:
  1223. return error;
  1224. }
  1225. fs_initcall(init_hugetlbfs_fs)