inode.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721
  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/falloc.h>
  13. #include <linux/fs.h>
  14. #include <linux/mount.h>
  15. #include <linux/file.h>
  16. #include <linux/kernel.h>
  17. #include <linux/writeback.h>
  18. #include <linux/pagemap.h>
  19. #include <linux/highmem.h>
  20. #include <linux/init.h>
  21. #include <linux/string.h>
  22. #include <linux/capability.h>
  23. #include <linux/ctype.h>
  24. #include <linux/backing-dev.h>
  25. #include <linux/hugetlb.h>
  26. #include <linux/pagevec.h>
  27. #include <linux/fs_parser.h>
  28. #include <linux/mman.h>
  29. #include <linux/slab.h>
  30. #include <linux/dnotify.h>
  31. #include <linux/statfs.h>
  32. #include <linux/security.h>
  33. #include <linux/magic.h>
  34. #include <linux/migrate.h>
  35. #include <linux/uio.h>
  36. #include <linux/uaccess.h>
  37. #include <linux/sched/mm.h>
  38. static const struct address_space_operations hugetlbfs_aops;
  39. static const struct file_operations hugetlbfs_file_operations;
  40. static const struct inode_operations hugetlbfs_dir_inode_operations;
  41. static const struct inode_operations hugetlbfs_inode_operations;
  42. enum hugetlbfs_size_type { NO_SIZE, SIZE_STD, SIZE_PERCENT };
  43. struct hugetlbfs_fs_context {
  44. struct hstate *hstate;
  45. unsigned long long max_size_opt;
  46. unsigned long long min_size_opt;
  47. long max_hpages;
  48. long nr_inodes;
  49. long min_hpages;
  50. enum hugetlbfs_size_type max_val_type;
  51. enum hugetlbfs_size_type min_val_type;
  52. kuid_t uid;
  53. kgid_t gid;
  54. umode_t mode;
  55. };
  56. int sysctl_hugetlb_shm_group;
  57. enum hugetlb_param {
  58. Opt_gid,
  59. Opt_min_size,
  60. Opt_mode,
  61. Opt_nr_inodes,
  62. Opt_pagesize,
  63. Opt_size,
  64. Opt_uid,
  65. };
  66. static const struct fs_parameter_spec hugetlb_fs_parameters[] = {
  67. fsparam_gid ("gid", Opt_gid),
  68. fsparam_string("min_size", Opt_min_size),
  69. fsparam_u32oct("mode", Opt_mode),
  70. fsparam_string("nr_inodes", Opt_nr_inodes),
  71. fsparam_string("pagesize", Opt_pagesize),
  72. fsparam_string("size", Opt_size),
  73. fsparam_uid ("uid", Opt_uid),
  74. {}
  75. };
  76. /*
  77. * Mask used when checking the page offset value passed in via system
  78. * calls. This value will be converted to a loff_t which is signed.
  79. * Therefore, we want to check the upper PAGE_SHIFT + 1 bits of the
  80. * value. The extra bit (- 1 in the shift value) is to take the sign
  81. * bit into account.
  82. */
  83. #define PGOFF_LOFFT_MAX \
  84. (((1UL << (PAGE_SHIFT + 1)) - 1) << (BITS_PER_LONG - (PAGE_SHIFT + 1)))
  85. static int hugetlbfs_file_mmap(struct file *file, struct vm_area_struct *vma)
  86. {
  87. struct inode *inode = file_inode(file);
  88. struct hugetlbfs_inode_info *info = HUGETLBFS_I(inode);
  89. loff_t len, vma_len;
  90. int ret;
  91. struct hstate *h = hstate_file(file);
  92. vm_flags_t vm_flags;
  93. /*
  94. * vma address alignment (but not the pgoff alignment) has
  95. * already been checked by prepare_hugepage_range. If you add
  96. * any error returns here, do so after setting VM_HUGETLB, so
  97. * is_vm_hugetlb_page tests below unmap_region go the right
  98. * way when do_mmap unwinds (may be important on powerpc
  99. * and ia64).
  100. */
  101. vm_flags_set(vma, VM_HUGETLB | VM_DONTEXPAND);
  102. vma->vm_ops = &hugetlb_vm_ops;
  103. ret = seal_check_write(info->seals, vma);
  104. if (ret)
  105. return ret;
  106. /*
  107. * page based offset in vm_pgoff could be sufficiently large to
  108. * overflow a loff_t when converted to byte offset. This can
  109. * only happen on architectures where sizeof(loff_t) ==
  110. * sizeof(unsigned long). So, only check in those instances.
  111. */
  112. if (sizeof(unsigned long) == sizeof(loff_t)) {
  113. if (vma->vm_pgoff & PGOFF_LOFFT_MAX)
  114. return -EINVAL;
  115. }
  116. /* must be huge page aligned */
  117. if (vma->vm_pgoff & (~huge_page_mask(h) >> PAGE_SHIFT))
  118. return -EINVAL;
  119. vma_len = (loff_t)(vma->vm_end - vma->vm_start);
  120. len = vma_len + ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
  121. /* check for overflow */
  122. if (len < vma_len)
  123. return -EINVAL;
  124. inode_lock(inode);
  125. file_accessed(file);
  126. ret = -ENOMEM;
  127. vm_flags = vma->vm_flags;
  128. /*
  129. * for SHM_HUGETLB, the pages are reserved in the shmget() call so skip
  130. * reserving here. Note: only for SHM hugetlbfs file, the inode
  131. * flag S_PRIVATE is set.
  132. */
  133. if (inode->i_flags & S_PRIVATE)
  134. vm_flags |= VM_NORESERVE;
  135. if (!hugetlb_reserve_pages(inode,
  136. vma->vm_pgoff >> huge_page_order(h),
  137. len >> huge_page_shift(h), vma,
  138. vm_flags))
  139. goto out;
  140. ret = 0;
  141. if (vma->vm_flags & VM_WRITE && inode->i_size < len)
  142. i_size_write(inode, len);
  143. out:
  144. inode_unlock(inode);
  145. return ret;
  146. }
  147. /*
  148. * Called under mmap_write_lock(mm).
  149. */
  150. static unsigned long
  151. hugetlb_get_unmapped_area_bottomup(struct file *file, unsigned long addr,
  152. unsigned long len, unsigned long pgoff, unsigned long flags)
  153. {
  154. struct hstate *h = hstate_file(file);
  155. struct vm_unmapped_area_info info = {};
  156. info.length = len;
  157. info.low_limit = current->mm->mmap_base;
  158. info.high_limit = arch_get_mmap_end(addr, len, flags);
  159. info.align_mask = PAGE_MASK & ~huge_page_mask(h);
  160. return vm_unmapped_area(&info);
  161. }
  162. static unsigned long
  163. hugetlb_get_unmapped_area_topdown(struct file *file, unsigned long addr,
  164. unsigned long len, unsigned long pgoff, unsigned long flags)
  165. {
  166. struct hstate *h = hstate_file(file);
  167. struct vm_unmapped_area_info info = {};
  168. info.flags = VM_UNMAPPED_AREA_TOPDOWN;
  169. info.length = len;
  170. info.low_limit = PAGE_SIZE;
  171. info.high_limit = arch_get_mmap_base(addr, current->mm->mmap_base);
  172. info.align_mask = PAGE_MASK & ~huge_page_mask(h);
  173. addr = vm_unmapped_area(&info);
  174. /*
  175. * A failed mmap() very likely causes application failure,
  176. * so fall back to the bottom-up function here. This scenario
  177. * can happen with large stack limits and large mmap()
  178. * allocations.
  179. */
  180. if (unlikely(offset_in_page(addr))) {
  181. VM_BUG_ON(addr != -ENOMEM);
  182. info.flags = 0;
  183. info.low_limit = current->mm->mmap_base;
  184. info.high_limit = arch_get_mmap_end(addr, len, flags);
  185. addr = vm_unmapped_area(&info);
  186. }
  187. return addr;
  188. }
  189. unsigned long
  190. generic_hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
  191. unsigned long len, unsigned long pgoff,
  192. unsigned long flags)
  193. {
  194. struct mm_struct *mm = current->mm;
  195. struct vm_area_struct *vma, *prev;
  196. struct hstate *h = hstate_file(file);
  197. const unsigned long mmap_end = arch_get_mmap_end(addr, len, flags);
  198. if (len & ~huge_page_mask(h))
  199. return -EINVAL;
  200. if (len > mmap_end - mmap_min_addr)
  201. return -ENOMEM;
  202. if (flags & MAP_FIXED) {
  203. if (prepare_hugepage_range(file, addr, len))
  204. return -EINVAL;
  205. return addr;
  206. }
  207. if (addr) {
  208. addr = ALIGN(addr, huge_page_size(h));
  209. vma = find_vma_prev(mm, addr, &prev);
  210. if (mmap_end - len >= addr && addr >= mmap_min_addr &&
  211. (!vma || addr + len <= vm_start_gap(vma)) &&
  212. (!prev || addr >= vm_end_gap(prev)))
  213. return addr;
  214. }
  215. /*
  216. * Use MMF_TOPDOWN flag as a hint to use topdown routine.
  217. * If architectures have special needs, they should define their own
  218. * version of hugetlb_get_unmapped_area.
  219. */
  220. if (test_bit(MMF_TOPDOWN, &mm->flags))
  221. return hugetlb_get_unmapped_area_topdown(file, addr, len,
  222. pgoff, flags);
  223. return hugetlb_get_unmapped_area_bottomup(file, addr, len,
  224. pgoff, flags);
  225. }
  226. #ifndef HAVE_ARCH_HUGETLB_UNMAPPED_AREA
  227. static unsigned long
  228. hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
  229. unsigned long len, unsigned long pgoff,
  230. unsigned long flags)
  231. {
  232. return generic_hugetlb_get_unmapped_area(file, addr, len, pgoff, flags);
  233. }
  234. #endif
  235. /*
  236. * Someone wants to read @bytes from a HWPOISON hugetlb @page from @offset.
  237. * Returns the maximum number of bytes one can read without touching the 1st raw
  238. * HWPOISON subpage.
  239. *
  240. * The implementation borrows the iteration logic from copy_page_to_iter*.
  241. */
  242. static size_t adjust_range_hwpoison(struct page *page, size_t offset, size_t bytes)
  243. {
  244. size_t n = 0;
  245. size_t res = 0;
  246. /* First subpage to start the loop. */
  247. page = nth_page(page, offset / PAGE_SIZE);
  248. offset %= PAGE_SIZE;
  249. while (1) {
  250. if (is_raw_hwpoison_page_in_hugepage(page))
  251. break;
  252. /* Safe to read n bytes without touching HWPOISON subpage. */
  253. n = min(bytes, (size_t)PAGE_SIZE - offset);
  254. res += n;
  255. bytes -= n;
  256. if (!bytes || !n)
  257. break;
  258. offset += n;
  259. if (offset == PAGE_SIZE) {
  260. page = nth_page(page, 1);
  261. offset = 0;
  262. }
  263. }
  264. return res;
  265. }
  266. /*
  267. * Support for read() - Find the page attached to f_mapping and copy out the
  268. * data. This provides functionality similar to filemap_read().
  269. */
  270. static ssize_t hugetlbfs_read_iter(struct kiocb *iocb, struct iov_iter *to)
  271. {
  272. struct file *file = iocb->ki_filp;
  273. struct hstate *h = hstate_file(file);
  274. struct address_space *mapping = file->f_mapping;
  275. struct inode *inode = mapping->host;
  276. unsigned long index = iocb->ki_pos >> huge_page_shift(h);
  277. unsigned long offset = iocb->ki_pos & ~huge_page_mask(h);
  278. unsigned long end_index;
  279. loff_t isize;
  280. ssize_t retval = 0;
  281. while (iov_iter_count(to)) {
  282. struct folio *folio;
  283. size_t nr, copied, want;
  284. /* nr is the maximum number of bytes to copy from this page */
  285. nr = huge_page_size(h);
  286. isize = i_size_read(inode);
  287. if (!isize)
  288. break;
  289. end_index = (isize - 1) >> huge_page_shift(h);
  290. if (index > end_index)
  291. break;
  292. if (index == end_index) {
  293. nr = ((isize - 1) & ~huge_page_mask(h)) + 1;
  294. if (nr <= offset)
  295. break;
  296. }
  297. nr = nr - offset;
  298. /* Find the folio */
  299. folio = filemap_lock_hugetlb_folio(h, mapping, index);
  300. if (IS_ERR(folio)) {
  301. /*
  302. * We have a HOLE, zero out the user-buffer for the
  303. * length of the hole or request.
  304. */
  305. copied = iov_iter_zero(nr, to);
  306. } else {
  307. folio_unlock(folio);
  308. if (!folio_test_hwpoison(folio))
  309. want = nr;
  310. else {
  311. /*
  312. * Adjust how many bytes safe to read without
  313. * touching the 1st raw HWPOISON subpage after
  314. * offset.
  315. */
  316. want = adjust_range_hwpoison(&folio->page, offset, nr);
  317. if (want == 0) {
  318. folio_put(folio);
  319. retval = -EIO;
  320. break;
  321. }
  322. }
  323. /*
  324. * We have the folio, copy it to user space buffer.
  325. */
  326. copied = copy_folio_to_iter(folio, offset, want, to);
  327. folio_put(folio);
  328. }
  329. offset += copied;
  330. retval += copied;
  331. if (copied != nr && iov_iter_count(to)) {
  332. if (!retval)
  333. retval = -EFAULT;
  334. break;
  335. }
  336. index += offset >> huge_page_shift(h);
  337. offset &= ~huge_page_mask(h);
  338. }
  339. iocb->ki_pos = ((loff_t)index << huge_page_shift(h)) + offset;
  340. return retval;
  341. }
  342. static int hugetlbfs_write_begin(struct file *file,
  343. struct address_space *mapping,
  344. loff_t pos, unsigned len,
  345. struct folio **foliop, void **fsdata)
  346. {
  347. return -EINVAL;
  348. }
  349. static int hugetlbfs_write_end(struct file *file, struct address_space *mapping,
  350. loff_t pos, unsigned len, unsigned copied,
  351. struct folio *folio, void *fsdata)
  352. {
  353. BUG();
  354. return -EINVAL;
  355. }
  356. static void hugetlb_delete_from_page_cache(struct folio *folio)
  357. {
  358. folio_clear_dirty(folio);
  359. folio_clear_uptodate(folio);
  360. filemap_remove_folio(folio);
  361. }
  362. /*
  363. * Called with i_mmap_rwsem held for inode based vma maps. This makes
  364. * sure vma (and vm_mm) will not go away. We also hold the hugetlb fault
  365. * mutex for the page in the mapping. So, we can not race with page being
  366. * faulted into the vma.
  367. */
  368. static bool hugetlb_vma_maps_page(struct vm_area_struct *vma,
  369. unsigned long addr, struct page *page)
  370. {
  371. pte_t *ptep, pte;
  372. ptep = hugetlb_walk(vma, addr, huge_page_size(hstate_vma(vma)));
  373. if (!ptep)
  374. return false;
  375. pte = huge_ptep_get(vma->vm_mm, addr, ptep);
  376. if (huge_pte_none(pte) || !pte_present(pte))
  377. return false;
  378. if (pte_page(pte) == page)
  379. return true;
  380. return false;
  381. }
  382. /*
  383. * Can vma_offset_start/vma_offset_end overflow on 32-bit arches?
  384. * No, because the interval tree returns us only those vmas
  385. * which overlap the truncated area starting at pgoff,
  386. * and no vma on a 32-bit arch can span beyond the 4GB.
  387. */
  388. static unsigned long vma_offset_start(struct vm_area_struct *vma, pgoff_t start)
  389. {
  390. unsigned long offset = 0;
  391. if (vma->vm_pgoff < start)
  392. offset = (start - vma->vm_pgoff) << PAGE_SHIFT;
  393. return vma->vm_start + offset;
  394. }
  395. static unsigned long vma_offset_end(struct vm_area_struct *vma, pgoff_t end)
  396. {
  397. unsigned long t_end;
  398. if (!end)
  399. return vma->vm_end;
  400. t_end = ((end - vma->vm_pgoff) << PAGE_SHIFT) + vma->vm_start;
  401. if (t_end > vma->vm_end)
  402. t_end = vma->vm_end;
  403. return t_end;
  404. }
  405. /*
  406. * Called with hugetlb fault mutex held. Therefore, no more mappings to
  407. * this folio can be created while executing the routine.
  408. */
  409. static void hugetlb_unmap_file_folio(struct hstate *h,
  410. struct address_space *mapping,
  411. struct folio *folio, pgoff_t index)
  412. {
  413. struct rb_root_cached *root = &mapping->i_mmap;
  414. struct hugetlb_vma_lock *vma_lock;
  415. struct page *page = &folio->page;
  416. struct vm_area_struct *vma;
  417. unsigned long v_start;
  418. unsigned long v_end;
  419. pgoff_t start, end;
  420. start = index * pages_per_huge_page(h);
  421. end = (index + 1) * pages_per_huge_page(h);
  422. i_mmap_lock_write(mapping);
  423. retry:
  424. vma_lock = NULL;
  425. vma_interval_tree_foreach(vma, root, start, end - 1) {
  426. v_start = vma_offset_start(vma, start);
  427. v_end = vma_offset_end(vma, end);
  428. if (!hugetlb_vma_maps_page(vma, v_start, page))
  429. continue;
  430. if (!hugetlb_vma_trylock_write(vma)) {
  431. vma_lock = vma->vm_private_data;
  432. /*
  433. * If we can not get vma lock, we need to drop
  434. * immap_sema and take locks in order. First,
  435. * take a ref on the vma_lock structure so that
  436. * we can be guaranteed it will not go away when
  437. * dropping immap_sema.
  438. */
  439. kref_get(&vma_lock->refs);
  440. break;
  441. }
  442. unmap_hugepage_range(vma, v_start, v_end, NULL,
  443. ZAP_FLAG_DROP_MARKER);
  444. hugetlb_vma_unlock_write(vma);
  445. }
  446. i_mmap_unlock_write(mapping);
  447. if (vma_lock) {
  448. /*
  449. * Wait on vma_lock. We know it is still valid as we have
  450. * a reference. We must 'open code' vma locking as we do
  451. * not know if vma_lock is still attached to vma.
  452. */
  453. down_write(&vma_lock->rw_sema);
  454. i_mmap_lock_write(mapping);
  455. vma = vma_lock->vma;
  456. if (!vma) {
  457. /*
  458. * If lock is no longer attached to vma, then just
  459. * unlock, drop our reference and retry looking for
  460. * other vmas.
  461. */
  462. up_write(&vma_lock->rw_sema);
  463. kref_put(&vma_lock->refs, hugetlb_vma_lock_release);
  464. goto retry;
  465. }
  466. /*
  467. * vma_lock is still attached to vma. Check to see if vma
  468. * still maps page and if so, unmap.
  469. */
  470. v_start = vma_offset_start(vma, start);
  471. v_end = vma_offset_end(vma, end);
  472. if (hugetlb_vma_maps_page(vma, v_start, page))
  473. unmap_hugepage_range(vma, v_start, v_end, NULL,
  474. ZAP_FLAG_DROP_MARKER);
  475. kref_put(&vma_lock->refs, hugetlb_vma_lock_release);
  476. hugetlb_vma_unlock_write(vma);
  477. goto retry;
  478. }
  479. }
  480. static void
  481. hugetlb_vmdelete_list(struct rb_root_cached *root, pgoff_t start, pgoff_t end,
  482. zap_flags_t zap_flags)
  483. {
  484. struct vm_area_struct *vma;
  485. /*
  486. * end == 0 indicates that the entire range after start should be
  487. * unmapped. Note, end is exclusive, whereas the interval tree takes
  488. * an inclusive "last".
  489. */
  490. vma_interval_tree_foreach(vma, root, start, end ? end - 1 : ULONG_MAX) {
  491. unsigned long v_start;
  492. unsigned long v_end;
  493. if (!hugetlb_vma_trylock_write(vma))
  494. continue;
  495. v_start = vma_offset_start(vma, start);
  496. v_end = vma_offset_end(vma, end);
  497. unmap_hugepage_range(vma, v_start, v_end, NULL, zap_flags);
  498. /*
  499. * Note that vma lock only exists for shared/non-private
  500. * vmas. Therefore, lock is not held when calling
  501. * unmap_hugepage_range for private vmas.
  502. */
  503. hugetlb_vma_unlock_write(vma);
  504. }
  505. }
  506. /*
  507. * Called with hugetlb fault mutex held.
  508. * Returns true if page was actually removed, false otherwise.
  509. */
  510. static bool remove_inode_single_folio(struct hstate *h, struct inode *inode,
  511. struct address_space *mapping,
  512. struct folio *folio, pgoff_t index,
  513. bool truncate_op)
  514. {
  515. bool ret = false;
  516. /*
  517. * If folio is mapped, it was faulted in after being
  518. * unmapped in caller or hugetlb_vmdelete_list() skips
  519. * unmapping it due to fail to grab lock. Unmap (again)
  520. * while holding the fault mutex. The mutex will prevent
  521. * faults until we finish removing the folio. Hold folio
  522. * lock to guarantee no concurrent migration.
  523. */
  524. folio_lock(folio);
  525. if (unlikely(folio_mapped(folio)))
  526. hugetlb_unmap_file_folio(h, mapping, folio, index);
  527. /*
  528. * We must remove the folio from page cache before removing
  529. * the region/ reserve map (hugetlb_unreserve_pages). In
  530. * rare out of memory conditions, removal of the region/reserve
  531. * map could fail. Correspondingly, the subpool and global
  532. * reserve usage count can need to be adjusted.
  533. */
  534. VM_BUG_ON_FOLIO(folio_test_hugetlb_restore_reserve(folio), folio);
  535. hugetlb_delete_from_page_cache(folio);
  536. ret = true;
  537. if (!truncate_op) {
  538. if (unlikely(hugetlb_unreserve_pages(inode, index,
  539. index + 1, 1)))
  540. hugetlb_fix_reserve_counts(inode);
  541. }
  542. folio_unlock(folio);
  543. return ret;
  544. }
  545. /*
  546. * remove_inode_hugepages handles two distinct cases: truncation and hole
  547. * punch. There are subtle differences in operation for each case.
  548. *
  549. * truncation is indicated by end of range being LLONG_MAX
  550. * In this case, we first scan the range and release found pages.
  551. * After releasing pages, hugetlb_unreserve_pages cleans up region/reserve
  552. * maps and global counts. Page faults can race with truncation.
  553. * During faults, hugetlb_no_page() checks i_size before page allocation,
  554. * and again after obtaining page table lock. It will 'back out'
  555. * allocations in the truncated range.
  556. * hole punch is indicated if end is not LLONG_MAX
  557. * In the hole punch case we scan the range and release found pages.
  558. * Only when releasing a page is the associated region/reserve map
  559. * deleted. The region/reserve map for ranges without associated
  560. * pages are not modified. Page faults can race with hole punch.
  561. * This is indicated if we find a mapped page.
  562. * Note: If the passed end of range value is beyond the end of file, but
  563. * not LLONG_MAX this routine still performs a hole punch operation.
  564. */
  565. static void remove_inode_hugepages(struct inode *inode, loff_t lstart,
  566. loff_t lend)
  567. {
  568. struct hstate *h = hstate_inode(inode);
  569. struct address_space *mapping = &inode->i_data;
  570. const pgoff_t end = lend >> PAGE_SHIFT;
  571. struct folio_batch fbatch;
  572. pgoff_t next, index;
  573. int i, freed = 0;
  574. bool truncate_op = (lend == LLONG_MAX);
  575. folio_batch_init(&fbatch);
  576. next = lstart >> PAGE_SHIFT;
  577. while (filemap_get_folios(mapping, &next, end - 1, &fbatch)) {
  578. for (i = 0; i < folio_batch_count(&fbatch); ++i) {
  579. struct folio *folio = fbatch.folios[i];
  580. u32 hash = 0;
  581. index = folio->index >> huge_page_order(h);
  582. hash = hugetlb_fault_mutex_hash(mapping, index);
  583. mutex_lock(&hugetlb_fault_mutex_table[hash]);
  584. /*
  585. * Remove folio that was part of folio_batch.
  586. */
  587. if (remove_inode_single_folio(h, inode, mapping, folio,
  588. index, truncate_op))
  589. freed++;
  590. mutex_unlock(&hugetlb_fault_mutex_table[hash]);
  591. }
  592. folio_batch_release(&fbatch);
  593. cond_resched();
  594. }
  595. if (truncate_op)
  596. (void)hugetlb_unreserve_pages(inode,
  597. lstart >> huge_page_shift(h),
  598. LONG_MAX, freed);
  599. }
  600. static void hugetlbfs_evict_inode(struct inode *inode)
  601. {
  602. struct resv_map *resv_map;
  603. remove_inode_hugepages(inode, 0, LLONG_MAX);
  604. /*
  605. * Get the resv_map from the address space embedded in the inode.
  606. * This is the address space which points to any resv_map allocated
  607. * at inode creation time. If this is a device special inode,
  608. * i_mapping may not point to the original address space.
  609. */
  610. resv_map = (struct resv_map *)(&inode->i_data)->i_private_data;
  611. /* Only regular and link inodes have associated reserve maps */
  612. if (resv_map)
  613. resv_map_release(&resv_map->refs);
  614. clear_inode(inode);
  615. }
  616. static void hugetlb_vmtruncate(struct inode *inode, loff_t offset)
  617. {
  618. pgoff_t pgoff;
  619. struct address_space *mapping = inode->i_mapping;
  620. struct hstate *h = hstate_inode(inode);
  621. BUG_ON(offset & ~huge_page_mask(h));
  622. pgoff = offset >> PAGE_SHIFT;
  623. i_size_write(inode, offset);
  624. i_mmap_lock_write(mapping);
  625. if (!RB_EMPTY_ROOT(&mapping->i_mmap.rb_root))
  626. hugetlb_vmdelete_list(&mapping->i_mmap, pgoff, 0,
  627. ZAP_FLAG_DROP_MARKER);
  628. i_mmap_unlock_write(mapping);
  629. remove_inode_hugepages(inode, offset, LLONG_MAX);
  630. }
  631. static void hugetlbfs_zero_partial_page(struct hstate *h,
  632. struct address_space *mapping,
  633. loff_t start,
  634. loff_t end)
  635. {
  636. pgoff_t idx = start >> huge_page_shift(h);
  637. struct folio *folio;
  638. folio = filemap_lock_hugetlb_folio(h, mapping, idx);
  639. if (IS_ERR(folio))
  640. return;
  641. start = start & ~huge_page_mask(h);
  642. end = end & ~huge_page_mask(h);
  643. if (!end)
  644. end = huge_page_size(h);
  645. folio_zero_segment(folio, (size_t)start, (size_t)end);
  646. folio_unlock(folio);
  647. folio_put(folio);
  648. }
  649. static long hugetlbfs_punch_hole(struct inode *inode, loff_t offset, loff_t len)
  650. {
  651. struct hugetlbfs_inode_info *info = HUGETLBFS_I(inode);
  652. struct address_space *mapping = inode->i_mapping;
  653. struct hstate *h = hstate_inode(inode);
  654. loff_t hpage_size = huge_page_size(h);
  655. loff_t hole_start, hole_end;
  656. /*
  657. * hole_start and hole_end indicate the full pages within the hole.
  658. */
  659. hole_start = round_up(offset, hpage_size);
  660. hole_end = round_down(offset + len, hpage_size);
  661. inode_lock(inode);
  662. /* protected by i_rwsem */
  663. if (info->seals & (F_SEAL_WRITE | F_SEAL_FUTURE_WRITE)) {
  664. inode_unlock(inode);
  665. return -EPERM;
  666. }
  667. i_mmap_lock_write(mapping);
  668. /* If range starts before first full page, zero partial page. */
  669. if (offset < hole_start)
  670. hugetlbfs_zero_partial_page(h, mapping,
  671. offset, min(offset + len, hole_start));
  672. /* Unmap users of full pages in the hole. */
  673. if (hole_end > hole_start) {
  674. if (!RB_EMPTY_ROOT(&mapping->i_mmap.rb_root))
  675. hugetlb_vmdelete_list(&mapping->i_mmap,
  676. hole_start >> PAGE_SHIFT,
  677. hole_end >> PAGE_SHIFT, 0);
  678. }
  679. /* If range extends beyond last full page, zero partial page. */
  680. if ((offset + len) > hole_end && (offset + len) > hole_start)
  681. hugetlbfs_zero_partial_page(h, mapping,
  682. hole_end, offset + len);
  683. i_mmap_unlock_write(mapping);
  684. /* Remove full pages from the file. */
  685. if (hole_end > hole_start)
  686. remove_inode_hugepages(inode, hole_start, hole_end);
  687. inode_unlock(inode);
  688. return 0;
  689. }
  690. static long hugetlbfs_fallocate(struct file *file, int mode, loff_t offset,
  691. loff_t len)
  692. {
  693. struct inode *inode = file_inode(file);
  694. struct hugetlbfs_inode_info *info = HUGETLBFS_I(inode);
  695. struct address_space *mapping = inode->i_mapping;
  696. struct hstate *h = hstate_inode(inode);
  697. struct vm_area_struct pseudo_vma;
  698. struct mm_struct *mm = current->mm;
  699. loff_t hpage_size = huge_page_size(h);
  700. unsigned long hpage_shift = huge_page_shift(h);
  701. pgoff_t start, index, end;
  702. int error;
  703. u32 hash;
  704. if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
  705. return -EOPNOTSUPP;
  706. if (mode & FALLOC_FL_PUNCH_HOLE)
  707. return hugetlbfs_punch_hole(inode, offset, len);
  708. /*
  709. * Default preallocate case.
  710. * For this range, start is rounded down and end is rounded up
  711. * as well as being converted to page offsets.
  712. */
  713. start = offset >> hpage_shift;
  714. end = (offset + len + hpage_size - 1) >> hpage_shift;
  715. inode_lock(inode);
  716. /* We need to check rlimit even when FALLOC_FL_KEEP_SIZE */
  717. error = inode_newsize_ok(inode, offset + len);
  718. if (error)
  719. goto out;
  720. if ((info->seals & F_SEAL_GROW) && offset + len > inode->i_size) {
  721. error = -EPERM;
  722. goto out;
  723. }
  724. /*
  725. * Initialize a pseudo vma as this is required by the huge page
  726. * allocation routines.
  727. */
  728. vma_init(&pseudo_vma, mm);
  729. vm_flags_init(&pseudo_vma, VM_HUGETLB | VM_MAYSHARE | VM_SHARED);
  730. pseudo_vma.vm_file = file;
  731. for (index = start; index < end; index++) {
  732. /*
  733. * This is supposed to be the vaddr where the page is being
  734. * faulted in, but we have no vaddr here.
  735. */
  736. struct folio *folio;
  737. unsigned long addr;
  738. cond_resched();
  739. /*
  740. * fallocate(2) manpage permits EINTR; we may have been
  741. * interrupted because we are using up too much memory.
  742. */
  743. if (signal_pending(current)) {
  744. error = -EINTR;
  745. break;
  746. }
  747. /* addr is the offset within the file (zero based) */
  748. addr = index * hpage_size;
  749. /* mutex taken here, fault path and hole punch */
  750. hash = hugetlb_fault_mutex_hash(mapping, index);
  751. mutex_lock(&hugetlb_fault_mutex_table[hash]);
  752. /* See if already present in mapping to avoid alloc/free */
  753. folio = filemap_get_folio(mapping, index << huge_page_order(h));
  754. if (!IS_ERR(folio)) {
  755. folio_put(folio);
  756. mutex_unlock(&hugetlb_fault_mutex_table[hash]);
  757. continue;
  758. }
  759. /*
  760. * Allocate folio without setting the avoid_reserve argument.
  761. * There certainly are no reserves associated with the
  762. * pseudo_vma. However, there could be shared mappings with
  763. * reserves for the file at the inode level. If we fallocate
  764. * folios in these areas, we need to consume the reserves
  765. * to keep reservation accounting consistent.
  766. */
  767. folio = alloc_hugetlb_folio(&pseudo_vma, addr, 0);
  768. if (IS_ERR(folio)) {
  769. mutex_unlock(&hugetlb_fault_mutex_table[hash]);
  770. error = PTR_ERR(folio);
  771. goto out;
  772. }
  773. folio_zero_user(folio, addr);
  774. __folio_mark_uptodate(folio);
  775. error = hugetlb_add_to_page_cache(folio, mapping, index);
  776. if (unlikely(error)) {
  777. restore_reserve_on_error(h, &pseudo_vma, addr, folio);
  778. folio_put(folio);
  779. mutex_unlock(&hugetlb_fault_mutex_table[hash]);
  780. goto out;
  781. }
  782. mutex_unlock(&hugetlb_fault_mutex_table[hash]);
  783. folio_set_hugetlb_migratable(folio);
  784. /*
  785. * folio_unlock because locked by hugetlb_add_to_page_cache()
  786. * folio_put() due to reference from alloc_hugetlb_folio()
  787. */
  788. folio_unlock(folio);
  789. folio_put(folio);
  790. }
  791. if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size)
  792. i_size_write(inode, offset + len);
  793. inode_set_ctime_current(inode);
  794. out:
  795. inode_unlock(inode);
  796. return error;
  797. }
  798. static int hugetlbfs_setattr(struct mnt_idmap *idmap,
  799. struct dentry *dentry, struct iattr *attr)
  800. {
  801. struct inode *inode = d_inode(dentry);
  802. struct hstate *h = hstate_inode(inode);
  803. int error;
  804. unsigned int ia_valid = attr->ia_valid;
  805. struct hugetlbfs_inode_info *info = HUGETLBFS_I(inode);
  806. error = setattr_prepare(idmap, dentry, attr);
  807. if (error)
  808. return error;
  809. if (ia_valid & ATTR_SIZE) {
  810. loff_t oldsize = inode->i_size;
  811. loff_t newsize = attr->ia_size;
  812. if (newsize & ~huge_page_mask(h))
  813. return -EINVAL;
  814. /* protected by i_rwsem */
  815. if ((newsize < oldsize && (info->seals & F_SEAL_SHRINK)) ||
  816. (newsize > oldsize && (info->seals & F_SEAL_GROW)))
  817. return -EPERM;
  818. hugetlb_vmtruncate(inode, newsize);
  819. }
  820. setattr_copy(idmap, inode, attr);
  821. mark_inode_dirty(inode);
  822. return 0;
  823. }
  824. static struct inode *hugetlbfs_get_root(struct super_block *sb,
  825. struct hugetlbfs_fs_context *ctx)
  826. {
  827. struct inode *inode;
  828. inode = new_inode(sb);
  829. if (inode) {
  830. inode->i_ino = get_next_ino();
  831. inode->i_mode = S_IFDIR | ctx->mode;
  832. inode->i_uid = ctx->uid;
  833. inode->i_gid = ctx->gid;
  834. simple_inode_init_ts(inode);
  835. inode->i_op = &hugetlbfs_dir_inode_operations;
  836. inode->i_fop = &simple_dir_operations;
  837. /* directory inodes start off with i_nlink == 2 (for "." entry) */
  838. inc_nlink(inode);
  839. lockdep_annotate_inode_mutex_key(inode);
  840. }
  841. return inode;
  842. }
  843. /*
  844. * Hugetlbfs is not reclaimable; therefore its i_mmap_rwsem will never
  845. * be taken from reclaim -- unlike regular filesystems. This needs an
  846. * annotation because huge_pmd_share() does an allocation under hugetlb's
  847. * i_mmap_rwsem.
  848. */
  849. static struct lock_class_key hugetlbfs_i_mmap_rwsem_key;
  850. static struct inode *hugetlbfs_get_inode(struct super_block *sb,
  851. struct mnt_idmap *idmap,
  852. struct inode *dir,
  853. umode_t mode, dev_t dev)
  854. {
  855. struct inode *inode;
  856. struct resv_map *resv_map = NULL;
  857. /*
  858. * Reserve maps are only needed for inodes that can have associated
  859. * page allocations.
  860. */
  861. if (S_ISREG(mode) || S_ISLNK(mode)) {
  862. resv_map = resv_map_alloc();
  863. if (!resv_map)
  864. return NULL;
  865. }
  866. inode = new_inode(sb);
  867. if (inode) {
  868. struct hugetlbfs_inode_info *info = HUGETLBFS_I(inode);
  869. inode->i_ino = get_next_ino();
  870. inode_init_owner(idmap, inode, dir, mode);
  871. lockdep_set_class(&inode->i_mapping->i_mmap_rwsem,
  872. &hugetlbfs_i_mmap_rwsem_key);
  873. inode->i_mapping->a_ops = &hugetlbfs_aops;
  874. simple_inode_init_ts(inode);
  875. inode->i_mapping->i_private_data = resv_map;
  876. info->seals = F_SEAL_SEAL;
  877. switch (mode & S_IFMT) {
  878. default:
  879. init_special_inode(inode, mode, dev);
  880. break;
  881. case S_IFREG:
  882. inode->i_op = &hugetlbfs_inode_operations;
  883. inode->i_fop = &hugetlbfs_file_operations;
  884. break;
  885. case S_IFDIR:
  886. inode->i_op = &hugetlbfs_dir_inode_operations;
  887. inode->i_fop = &simple_dir_operations;
  888. /* directory inodes start off with i_nlink == 2 (for "." entry) */
  889. inc_nlink(inode);
  890. break;
  891. case S_IFLNK:
  892. inode->i_op = &page_symlink_inode_operations;
  893. inode_nohighmem(inode);
  894. break;
  895. }
  896. lockdep_annotate_inode_mutex_key(inode);
  897. } else {
  898. if (resv_map)
  899. kref_put(&resv_map->refs, resv_map_release);
  900. }
  901. return inode;
  902. }
  903. /*
  904. * File creation. Allocate an inode, and we're done..
  905. */
  906. static int hugetlbfs_mknod(struct mnt_idmap *idmap, struct inode *dir,
  907. struct dentry *dentry, umode_t mode, dev_t dev)
  908. {
  909. struct inode *inode;
  910. inode = hugetlbfs_get_inode(dir->i_sb, idmap, dir, mode, dev);
  911. if (!inode)
  912. return -ENOSPC;
  913. inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
  914. d_instantiate(dentry, inode);
  915. dget(dentry);/* Extra count - pin the dentry in core */
  916. return 0;
  917. }
  918. static int hugetlbfs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
  919. struct dentry *dentry, umode_t mode)
  920. {
  921. int retval = hugetlbfs_mknod(idmap, dir, dentry,
  922. mode | S_IFDIR, 0);
  923. if (!retval)
  924. inc_nlink(dir);
  925. return retval;
  926. }
  927. static int hugetlbfs_create(struct mnt_idmap *idmap,
  928. struct inode *dir, struct dentry *dentry,
  929. umode_t mode, bool excl)
  930. {
  931. return hugetlbfs_mknod(idmap, dir, dentry, mode | S_IFREG, 0);
  932. }
  933. static int hugetlbfs_tmpfile(struct mnt_idmap *idmap,
  934. struct inode *dir, struct file *file,
  935. umode_t mode)
  936. {
  937. struct inode *inode;
  938. inode = hugetlbfs_get_inode(dir->i_sb, idmap, dir, mode | S_IFREG, 0);
  939. if (!inode)
  940. return -ENOSPC;
  941. inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
  942. d_tmpfile(file, inode);
  943. return finish_open_simple(file, 0);
  944. }
  945. static int hugetlbfs_symlink(struct mnt_idmap *idmap,
  946. struct inode *dir, struct dentry *dentry,
  947. const char *symname)
  948. {
  949. const umode_t mode = S_IFLNK|S_IRWXUGO;
  950. struct inode *inode;
  951. int error = -ENOSPC;
  952. inode = hugetlbfs_get_inode(dir->i_sb, idmap, dir, mode, 0);
  953. if (inode) {
  954. int l = strlen(symname)+1;
  955. error = page_symlink(inode, symname, l);
  956. if (!error) {
  957. d_instantiate(dentry, inode);
  958. dget(dentry);
  959. } else
  960. iput(inode);
  961. }
  962. inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
  963. return error;
  964. }
  965. #ifdef CONFIG_MIGRATION
  966. static int hugetlbfs_migrate_folio(struct address_space *mapping,
  967. struct folio *dst, struct folio *src,
  968. enum migrate_mode mode)
  969. {
  970. int rc;
  971. rc = migrate_huge_page_move_mapping(mapping, dst, src);
  972. if (rc != MIGRATEPAGE_SUCCESS)
  973. return rc;
  974. if (hugetlb_folio_subpool(src)) {
  975. hugetlb_set_folio_subpool(dst,
  976. hugetlb_folio_subpool(src));
  977. hugetlb_set_folio_subpool(src, NULL);
  978. }
  979. folio_migrate_flags(dst, src);
  980. return MIGRATEPAGE_SUCCESS;
  981. }
  982. #else
  983. #define hugetlbfs_migrate_folio NULL
  984. #endif
  985. static int hugetlbfs_error_remove_folio(struct address_space *mapping,
  986. struct folio *folio)
  987. {
  988. return 0;
  989. }
  990. /*
  991. * Display the mount options in /proc/mounts.
  992. */
  993. static int hugetlbfs_show_options(struct seq_file *m, struct dentry *root)
  994. {
  995. struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(root->d_sb);
  996. struct hugepage_subpool *spool = sbinfo->spool;
  997. unsigned long hpage_size = huge_page_size(sbinfo->hstate);
  998. unsigned hpage_shift = huge_page_shift(sbinfo->hstate);
  999. char mod;
  1000. if (!uid_eq(sbinfo->uid, GLOBAL_ROOT_UID))
  1001. seq_printf(m, ",uid=%u",
  1002. from_kuid_munged(&init_user_ns, sbinfo->uid));
  1003. if (!gid_eq(sbinfo->gid, GLOBAL_ROOT_GID))
  1004. seq_printf(m, ",gid=%u",
  1005. from_kgid_munged(&init_user_ns, sbinfo->gid));
  1006. if (sbinfo->mode != 0755)
  1007. seq_printf(m, ",mode=%o", sbinfo->mode);
  1008. if (sbinfo->max_inodes != -1)
  1009. seq_printf(m, ",nr_inodes=%lu", sbinfo->max_inodes);
  1010. hpage_size /= 1024;
  1011. mod = 'K';
  1012. if (hpage_size >= 1024) {
  1013. hpage_size /= 1024;
  1014. mod = 'M';
  1015. }
  1016. seq_printf(m, ",pagesize=%lu%c", hpage_size, mod);
  1017. if (spool) {
  1018. if (spool->max_hpages != -1)
  1019. seq_printf(m, ",size=%llu",
  1020. (unsigned long long)spool->max_hpages << hpage_shift);
  1021. if (spool->min_hpages != -1)
  1022. seq_printf(m, ",min_size=%llu",
  1023. (unsigned long long)spool->min_hpages << hpage_shift);
  1024. }
  1025. return 0;
  1026. }
  1027. static int hugetlbfs_statfs(struct dentry *dentry, struct kstatfs *buf)
  1028. {
  1029. struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(dentry->d_sb);
  1030. struct hstate *h = hstate_inode(d_inode(dentry));
  1031. u64 id = huge_encode_dev(dentry->d_sb->s_dev);
  1032. buf->f_fsid = u64_to_fsid(id);
  1033. buf->f_type = HUGETLBFS_MAGIC;
  1034. buf->f_bsize = huge_page_size(h);
  1035. if (sbinfo) {
  1036. spin_lock(&sbinfo->stat_lock);
  1037. /* If no limits set, just report 0 or -1 for max/free/used
  1038. * blocks, like simple_statfs() */
  1039. if (sbinfo->spool) {
  1040. long free_pages;
  1041. spin_lock_irq(&sbinfo->spool->lock);
  1042. buf->f_blocks = sbinfo->spool->max_hpages;
  1043. free_pages = sbinfo->spool->max_hpages
  1044. - sbinfo->spool->used_hpages;
  1045. buf->f_bavail = buf->f_bfree = free_pages;
  1046. spin_unlock_irq(&sbinfo->spool->lock);
  1047. buf->f_files = sbinfo->max_inodes;
  1048. buf->f_ffree = sbinfo->free_inodes;
  1049. }
  1050. spin_unlock(&sbinfo->stat_lock);
  1051. }
  1052. buf->f_namelen = NAME_MAX;
  1053. return 0;
  1054. }
  1055. static void hugetlbfs_put_super(struct super_block *sb)
  1056. {
  1057. struct hugetlbfs_sb_info *sbi = HUGETLBFS_SB(sb);
  1058. if (sbi) {
  1059. sb->s_fs_info = NULL;
  1060. if (sbi->spool)
  1061. hugepage_put_subpool(sbi->spool);
  1062. kfree(sbi);
  1063. }
  1064. }
  1065. static inline int hugetlbfs_dec_free_inodes(struct hugetlbfs_sb_info *sbinfo)
  1066. {
  1067. if (sbinfo->free_inodes >= 0) {
  1068. spin_lock(&sbinfo->stat_lock);
  1069. if (unlikely(!sbinfo->free_inodes)) {
  1070. spin_unlock(&sbinfo->stat_lock);
  1071. return 0;
  1072. }
  1073. sbinfo->free_inodes--;
  1074. spin_unlock(&sbinfo->stat_lock);
  1075. }
  1076. return 1;
  1077. }
  1078. static void hugetlbfs_inc_free_inodes(struct hugetlbfs_sb_info *sbinfo)
  1079. {
  1080. if (sbinfo->free_inodes >= 0) {
  1081. spin_lock(&sbinfo->stat_lock);
  1082. sbinfo->free_inodes++;
  1083. spin_unlock(&sbinfo->stat_lock);
  1084. }
  1085. }
  1086. static struct kmem_cache *hugetlbfs_inode_cachep;
  1087. static struct inode *hugetlbfs_alloc_inode(struct super_block *sb)
  1088. {
  1089. struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(sb);
  1090. struct hugetlbfs_inode_info *p;
  1091. if (unlikely(!hugetlbfs_dec_free_inodes(sbinfo)))
  1092. return NULL;
  1093. p = alloc_inode_sb(sb, hugetlbfs_inode_cachep, GFP_KERNEL);
  1094. if (unlikely(!p)) {
  1095. hugetlbfs_inc_free_inodes(sbinfo);
  1096. return NULL;
  1097. }
  1098. return &p->vfs_inode;
  1099. }
  1100. static void hugetlbfs_free_inode(struct inode *inode)
  1101. {
  1102. kmem_cache_free(hugetlbfs_inode_cachep, HUGETLBFS_I(inode));
  1103. }
  1104. static void hugetlbfs_destroy_inode(struct inode *inode)
  1105. {
  1106. hugetlbfs_inc_free_inodes(HUGETLBFS_SB(inode->i_sb));
  1107. }
  1108. static const struct address_space_operations hugetlbfs_aops = {
  1109. .write_begin = hugetlbfs_write_begin,
  1110. .write_end = hugetlbfs_write_end,
  1111. .dirty_folio = noop_dirty_folio,
  1112. .migrate_folio = hugetlbfs_migrate_folio,
  1113. .error_remove_folio = hugetlbfs_error_remove_folio,
  1114. };
  1115. static void init_once(void *foo)
  1116. {
  1117. struct hugetlbfs_inode_info *ei = foo;
  1118. inode_init_once(&ei->vfs_inode);
  1119. }
  1120. static const struct file_operations hugetlbfs_file_operations = {
  1121. .read_iter = hugetlbfs_read_iter,
  1122. .mmap = hugetlbfs_file_mmap,
  1123. .fsync = noop_fsync,
  1124. .get_unmapped_area = hugetlb_get_unmapped_area,
  1125. .llseek = default_llseek,
  1126. .fallocate = hugetlbfs_fallocate,
  1127. .fop_flags = FOP_HUGE_PAGES,
  1128. };
  1129. static const struct inode_operations hugetlbfs_dir_inode_operations = {
  1130. .create = hugetlbfs_create,
  1131. .lookup = simple_lookup,
  1132. .link = simple_link,
  1133. .unlink = simple_unlink,
  1134. .symlink = hugetlbfs_symlink,
  1135. .mkdir = hugetlbfs_mkdir,
  1136. .rmdir = simple_rmdir,
  1137. .mknod = hugetlbfs_mknod,
  1138. .rename = simple_rename,
  1139. .setattr = hugetlbfs_setattr,
  1140. .tmpfile = hugetlbfs_tmpfile,
  1141. };
  1142. static const struct inode_operations hugetlbfs_inode_operations = {
  1143. .setattr = hugetlbfs_setattr,
  1144. };
  1145. static const struct super_operations hugetlbfs_ops = {
  1146. .alloc_inode = hugetlbfs_alloc_inode,
  1147. .free_inode = hugetlbfs_free_inode,
  1148. .destroy_inode = hugetlbfs_destroy_inode,
  1149. .evict_inode = hugetlbfs_evict_inode,
  1150. .statfs = hugetlbfs_statfs,
  1151. .put_super = hugetlbfs_put_super,
  1152. .show_options = hugetlbfs_show_options,
  1153. };
  1154. /*
  1155. * Convert size option passed from command line to number of huge pages
  1156. * in the pool specified by hstate. Size option could be in bytes
  1157. * (val_type == SIZE_STD) or percentage of the pool (val_type == SIZE_PERCENT).
  1158. */
  1159. static long
  1160. hugetlbfs_size_to_hpages(struct hstate *h, unsigned long long size_opt,
  1161. enum hugetlbfs_size_type val_type)
  1162. {
  1163. if (val_type == NO_SIZE)
  1164. return -1;
  1165. if (val_type == SIZE_PERCENT) {
  1166. size_opt <<= huge_page_shift(h);
  1167. size_opt *= h->max_huge_pages;
  1168. do_div(size_opt, 100);
  1169. }
  1170. size_opt >>= huge_page_shift(h);
  1171. return size_opt;
  1172. }
  1173. /*
  1174. * Parse one mount parameter.
  1175. */
  1176. static int hugetlbfs_parse_param(struct fs_context *fc, struct fs_parameter *param)
  1177. {
  1178. struct hugetlbfs_fs_context *ctx = fc->fs_private;
  1179. struct fs_parse_result result;
  1180. struct hstate *h;
  1181. char *rest;
  1182. unsigned long ps;
  1183. int opt;
  1184. opt = fs_parse(fc, hugetlb_fs_parameters, param, &result);
  1185. if (opt < 0)
  1186. return opt;
  1187. switch (opt) {
  1188. case Opt_uid:
  1189. ctx->uid = result.uid;
  1190. return 0;
  1191. case Opt_gid:
  1192. ctx->gid = result.gid;
  1193. return 0;
  1194. case Opt_mode:
  1195. ctx->mode = result.uint_32 & 01777U;
  1196. return 0;
  1197. case Opt_size:
  1198. /* memparse() will accept a K/M/G without a digit */
  1199. if (!param->string || !isdigit(param->string[0]))
  1200. goto bad_val;
  1201. ctx->max_size_opt = memparse(param->string, &rest);
  1202. ctx->max_val_type = SIZE_STD;
  1203. if (*rest == '%')
  1204. ctx->max_val_type = SIZE_PERCENT;
  1205. return 0;
  1206. case Opt_nr_inodes:
  1207. /* memparse() will accept a K/M/G without a digit */
  1208. if (!param->string || !isdigit(param->string[0]))
  1209. goto bad_val;
  1210. ctx->nr_inodes = memparse(param->string, &rest);
  1211. return 0;
  1212. case Opt_pagesize:
  1213. ps = memparse(param->string, &rest);
  1214. h = size_to_hstate(ps);
  1215. if (!h) {
  1216. pr_err("Unsupported page size %lu MB\n", ps / SZ_1M);
  1217. return -EINVAL;
  1218. }
  1219. ctx->hstate = h;
  1220. return 0;
  1221. case Opt_min_size:
  1222. /* memparse() will accept a K/M/G without a digit */
  1223. if (!param->string || !isdigit(param->string[0]))
  1224. goto bad_val;
  1225. ctx->min_size_opt = memparse(param->string, &rest);
  1226. ctx->min_val_type = SIZE_STD;
  1227. if (*rest == '%')
  1228. ctx->min_val_type = SIZE_PERCENT;
  1229. return 0;
  1230. default:
  1231. return -EINVAL;
  1232. }
  1233. bad_val:
  1234. return invalfc(fc, "Bad value '%s' for mount option '%s'\n",
  1235. param->string, param->key);
  1236. }
  1237. /*
  1238. * Validate the parsed options.
  1239. */
  1240. static int hugetlbfs_validate(struct fs_context *fc)
  1241. {
  1242. struct hugetlbfs_fs_context *ctx = fc->fs_private;
  1243. /*
  1244. * Use huge page pool size (in hstate) to convert the size
  1245. * options to number of huge pages. If NO_SIZE, -1 is returned.
  1246. */
  1247. ctx->max_hpages = hugetlbfs_size_to_hpages(ctx->hstate,
  1248. ctx->max_size_opt,
  1249. ctx->max_val_type);
  1250. ctx->min_hpages = hugetlbfs_size_to_hpages(ctx->hstate,
  1251. ctx->min_size_opt,
  1252. ctx->min_val_type);
  1253. /*
  1254. * If max_size was specified, then min_size must be smaller
  1255. */
  1256. if (ctx->max_val_type > NO_SIZE &&
  1257. ctx->min_hpages > ctx->max_hpages) {
  1258. pr_err("Minimum size can not be greater than maximum size\n");
  1259. return -EINVAL;
  1260. }
  1261. return 0;
  1262. }
  1263. static int
  1264. hugetlbfs_fill_super(struct super_block *sb, struct fs_context *fc)
  1265. {
  1266. struct hugetlbfs_fs_context *ctx = fc->fs_private;
  1267. struct hugetlbfs_sb_info *sbinfo;
  1268. sbinfo = kmalloc(sizeof(struct hugetlbfs_sb_info), GFP_KERNEL);
  1269. if (!sbinfo)
  1270. return -ENOMEM;
  1271. sb->s_fs_info = sbinfo;
  1272. spin_lock_init(&sbinfo->stat_lock);
  1273. sbinfo->hstate = ctx->hstate;
  1274. sbinfo->max_inodes = ctx->nr_inodes;
  1275. sbinfo->free_inodes = ctx->nr_inodes;
  1276. sbinfo->spool = NULL;
  1277. sbinfo->uid = ctx->uid;
  1278. sbinfo->gid = ctx->gid;
  1279. sbinfo->mode = ctx->mode;
  1280. /*
  1281. * Allocate and initialize subpool if maximum or minimum size is
  1282. * specified. Any needed reservations (for minimum size) are taken
  1283. * when the subpool is created.
  1284. */
  1285. if (ctx->max_hpages != -1 || ctx->min_hpages != -1) {
  1286. sbinfo->spool = hugepage_new_subpool(ctx->hstate,
  1287. ctx->max_hpages,
  1288. ctx->min_hpages);
  1289. if (!sbinfo->spool)
  1290. goto out_free;
  1291. }
  1292. sb->s_maxbytes = MAX_LFS_FILESIZE;
  1293. sb->s_blocksize = huge_page_size(ctx->hstate);
  1294. sb->s_blocksize_bits = huge_page_shift(ctx->hstate);
  1295. sb->s_magic = HUGETLBFS_MAGIC;
  1296. sb->s_op = &hugetlbfs_ops;
  1297. sb->s_time_gran = 1;
  1298. /*
  1299. * Due to the special and limited functionality of hugetlbfs, it does
  1300. * not work well as a stacking filesystem.
  1301. */
  1302. sb->s_stack_depth = FILESYSTEM_MAX_STACK_DEPTH;
  1303. sb->s_root = d_make_root(hugetlbfs_get_root(sb, ctx));
  1304. if (!sb->s_root)
  1305. goto out_free;
  1306. return 0;
  1307. out_free:
  1308. kfree(sbinfo->spool);
  1309. kfree(sbinfo);
  1310. return -ENOMEM;
  1311. }
  1312. static int hugetlbfs_get_tree(struct fs_context *fc)
  1313. {
  1314. int err = hugetlbfs_validate(fc);
  1315. if (err)
  1316. return err;
  1317. return get_tree_nodev(fc, hugetlbfs_fill_super);
  1318. }
  1319. static void hugetlbfs_fs_context_free(struct fs_context *fc)
  1320. {
  1321. kfree(fc->fs_private);
  1322. }
  1323. static const struct fs_context_operations hugetlbfs_fs_context_ops = {
  1324. .free = hugetlbfs_fs_context_free,
  1325. .parse_param = hugetlbfs_parse_param,
  1326. .get_tree = hugetlbfs_get_tree,
  1327. };
  1328. static int hugetlbfs_init_fs_context(struct fs_context *fc)
  1329. {
  1330. struct hugetlbfs_fs_context *ctx;
  1331. ctx = kzalloc(sizeof(struct hugetlbfs_fs_context), GFP_KERNEL);
  1332. if (!ctx)
  1333. return -ENOMEM;
  1334. ctx->max_hpages = -1; /* No limit on size by default */
  1335. ctx->nr_inodes = -1; /* No limit on number of inodes by default */
  1336. ctx->uid = current_fsuid();
  1337. ctx->gid = current_fsgid();
  1338. ctx->mode = 0755;
  1339. ctx->hstate = &default_hstate;
  1340. ctx->min_hpages = -1; /* No default minimum size */
  1341. ctx->max_val_type = NO_SIZE;
  1342. ctx->min_val_type = NO_SIZE;
  1343. fc->fs_private = ctx;
  1344. fc->ops = &hugetlbfs_fs_context_ops;
  1345. return 0;
  1346. }
  1347. static struct file_system_type hugetlbfs_fs_type = {
  1348. .name = "hugetlbfs",
  1349. .init_fs_context = hugetlbfs_init_fs_context,
  1350. .parameters = hugetlb_fs_parameters,
  1351. .kill_sb = kill_litter_super,
  1352. .fs_flags = FS_ALLOW_IDMAP,
  1353. };
  1354. static struct vfsmount *hugetlbfs_vfsmount[HUGE_MAX_HSTATE];
  1355. static int can_do_hugetlb_shm(void)
  1356. {
  1357. kgid_t shm_group;
  1358. shm_group = make_kgid(&init_user_ns, sysctl_hugetlb_shm_group);
  1359. return capable(CAP_IPC_LOCK) || in_group_p(shm_group);
  1360. }
  1361. static int get_hstate_idx(int page_size_log)
  1362. {
  1363. struct hstate *h = hstate_sizelog(page_size_log);
  1364. if (!h)
  1365. return -1;
  1366. return hstate_index(h);
  1367. }
  1368. /*
  1369. * Note that size should be aligned to proper hugepage size in caller side,
  1370. * otherwise hugetlb_reserve_pages reserves one less hugepages than intended.
  1371. */
  1372. struct file *hugetlb_file_setup(const char *name, size_t size,
  1373. vm_flags_t acctflag, int creat_flags,
  1374. int page_size_log)
  1375. {
  1376. struct inode *inode;
  1377. struct vfsmount *mnt;
  1378. int hstate_idx;
  1379. struct file *file;
  1380. hstate_idx = get_hstate_idx(page_size_log);
  1381. if (hstate_idx < 0)
  1382. return ERR_PTR(-ENODEV);
  1383. mnt = hugetlbfs_vfsmount[hstate_idx];
  1384. if (!mnt)
  1385. return ERR_PTR(-ENOENT);
  1386. if (creat_flags == HUGETLB_SHMFS_INODE && !can_do_hugetlb_shm()) {
  1387. struct ucounts *ucounts = current_ucounts();
  1388. if (user_shm_lock(size, ucounts)) {
  1389. pr_warn_once("%s (%d): Using mlock ulimits for SHM_HUGETLB is obsolete\n",
  1390. current->comm, current->pid);
  1391. user_shm_unlock(size, ucounts);
  1392. }
  1393. return ERR_PTR(-EPERM);
  1394. }
  1395. file = ERR_PTR(-ENOSPC);
  1396. /* hugetlbfs_vfsmount[] mounts do not use idmapped mounts. */
  1397. inode = hugetlbfs_get_inode(mnt->mnt_sb, &nop_mnt_idmap, NULL,
  1398. S_IFREG | S_IRWXUGO, 0);
  1399. if (!inode)
  1400. goto out;
  1401. if (creat_flags == HUGETLB_SHMFS_INODE)
  1402. inode->i_flags |= S_PRIVATE;
  1403. inode->i_size = size;
  1404. clear_nlink(inode);
  1405. if (!hugetlb_reserve_pages(inode, 0,
  1406. size >> huge_page_shift(hstate_inode(inode)), NULL,
  1407. acctflag))
  1408. file = ERR_PTR(-ENOMEM);
  1409. else
  1410. file = alloc_file_pseudo(inode, mnt, name, O_RDWR,
  1411. &hugetlbfs_file_operations);
  1412. if (!IS_ERR(file))
  1413. return file;
  1414. iput(inode);
  1415. out:
  1416. return file;
  1417. }
  1418. static struct vfsmount *__init mount_one_hugetlbfs(struct hstate *h)
  1419. {
  1420. struct fs_context *fc;
  1421. struct vfsmount *mnt;
  1422. fc = fs_context_for_mount(&hugetlbfs_fs_type, SB_KERNMOUNT);
  1423. if (IS_ERR(fc)) {
  1424. mnt = ERR_CAST(fc);
  1425. } else {
  1426. struct hugetlbfs_fs_context *ctx = fc->fs_private;
  1427. ctx->hstate = h;
  1428. mnt = fc_mount(fc);
  1429. put_fs_context(fc);
  1430. }
  1431. if (IS_ERR(mnt))
  1432. pr_err("Cannot mount internal hugetlbfs for page size %luK",
  1433. huge_page_size(h) / SZ_1K);
  1434. return mnt;
  1435. }
  1436. static int __init init_hugetlbfs_fs(void)
  1437. {
  1438. struct vfsmount *mnt;
  1439. struct hstate *h;
  1440. int error;
  1441. int i;
  1442. if (!hugepages_supported()) {
  1443. pr_info("disabling because there are no supported hugepage sizes\n");
  1444. return -ENOTSUPP;
  1445. }
  1446. error = -ENOMEM;
  1447. hugetlbfs_inode_cachep = kmem_cache_create("hugetlbfs_inode_cache",
  1448. sizeof(struct hugetlbfs_inode_info),
  1449. 0, SLAB_ACCOUNT, init_once);
  1450. if (hugetlbfs_inode_cachep == NULL)
  1451. goto out;
  1452. error = register_filesystem(&hugetlbfs_fs_type);
  1453. if (error)
  1454. goto out_free;
  1455. /* default hstate mount is required */
  1456. mnt = mount_one_hugetlbfs(&default_hstate);
  1457. if (IS_ERR(mnt)) {
  1458. error = PTR_ERR(mnt);
  1459. goto out_unreg;
  1460. }
  1461. hugetlbfs_vfsmount[default_hstate_idx] = mnt;
  1462. /* other hstates are optional */
  1463. i = 0;
  1464. for_each_hstate(h) {
  1465. if (i == default_hstate_idx) {
  1466. i++;
  1467. continue;
  1468. }
  1469. mnt = mount_one_hugetlbfs(h);
  1470. if (IS_ERR(mnt))
  1471. hugetlbfs_vfsmount[i] = NULL;
  1472. else
  1473. hugetlbfs_vfsmount[i] = mnt;
  1474. i++;
  1475. }
  1476. return 0;
  1477. out_unreg:
  1478. (void)unregister_filesystem(&hugetlbfs_fs_type);
  1479. out_free:
  1480. kmem_cache_destroy(hugetlbfs_inode_cachep);
  1481. out:
  1482. return error;
  1483. }
  1484. fs_initcall(init_hugetlbfs_fs)