hugetlb.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320
  1. /*
  2. * Generic hugetlb support.
  3. * (C) William Irwin, April 2004
  4. */
  5. #include <linux/gfp.h>
  6. #include <linux/list.h>
  7. #include <linux/init.h>
  8. #include <linux/module.h>
  9. #include <linux/mm.h>
  10. #include <linux/sysctl.h>
  11. #include <linux/highmem.h>
  12. #include <linux/nodemask.h>
  13. #include <linux/pagemap.h>
  14. #include <linux/mempolicy.h>
  15. #include <linux/cpuset.h>
  16. #include <linux/mutex.h>
  17. #include <asm/page.h>
  18. #include <asm/pgtable.h>
  19. #include <linux/hugetlb.h>
  20. #include "internal.h"
  21. const unsigned long hugetlb_zero = 0, hugetlb_infinity = ~0UL;
  22. static unsigned long nr_huge_pages, free_huge_pages, resv_huge_pages;
  23. static unsigned long surplus_huge_pages;
  24. static unsigned long nr_overcommit_huge_pages;
  25. unsigned long max_huge_pages;
  26. unsigned long sysctl_overcommit_huge_pages;
  27. static struct list_head hugepage_freelists[MAX_NUMNODES];
  28. static unsigned int nr_huge_pages_node[MAX_NUMNODES];
  29. static unsigned int free_huge_pages_node[MAX_NUMNODES];
  30. static unsigned int surplus_huge_pages_node[MAX_NUMNODES];
  31. static gfp_t htlb_alloc_mask = GFP_HIGHUSER;
  32. unsigned long hugepages_treat_as_movable;
  33. static int hugetlb_next_nid;
  34. /*
  35. * Protects updates to hugepage_freelists, nr_huge_pages, and free_huge_pages
  36. */
  37. static DEFINE_SPINLOCK(hugetlb_lock);
  38. static void clear_huge_page(struct page *page, unsigned long addr)
  39. {
  40. int i;
  41. might_sleep();
  42. for (i = 0; i < (HPAGE_SIZE/PAGE_SIZE); i++) {
  43. cond_resched();
  44. clear_user_highpage(page + i, addr + i * PAGE_SIZE);
  45. }
  46. }
  47. static void copy_huge_page(struct page *dst, struct page *src,
  48. unsigned long addr, struct vm_area_struct *vma)
  49. {
  50. int i;
  51. might_sleep();
  52. for (i = 0; i < HPAGE_SIZE/PAGE_SIZE; i++) {
  53. cond_resched();
  54. copy_user_highpage(dst + i, src + i, addr + i*PAGE_SIZE, vma);
  55. }
  56. }
  57. static void enqueue_huge_page(struct page *page)
  58. {
  59. int nid = page_to_nid(page);
  60. list_add(&page->lru, &hugepage_freelists[nid]);
  61. free_huge_pages++;
  62. free_huge_pages_node[nid]++;
  63. }
  64. static struct page *dequeue_huge_page(void)
  65. {
  66. int nid;
  67. struct page *page = NULL;
  68. for (nid = 0; nid < MAX_NUMNODES; ++nid) {
  69. if (!list_empty(&hugepage_freelists[nid])) {
  70. page = list_entry(hugepage_freelists[nid].next,
  71. struct page, lru);
  72. list_del(&page->lru);
  73. free_huge_pages--;
  74. free_huge_pages_node[nid]--;
  75. break;
  76. }
  77. }
  78. return page;
  79. }
  80. static struct page *dequeue_huge_page_vma(struct vm_area_struct *vma,
  81. unsigned long address)
  82. {
  83. int nid;
  84. struct page *page = NULL;
  85. struct mempolicy *mpol;
  86. nodemask_t *nodemask;
  87. struct zonelist *zonelist = huge_zonelist(vma, address,
  88. htlb_alloc_mask, &mpol, &nodemask);
  89. struct zone *zone;
  90. struct zoneref *z;
  91. for_each_zone_zonelist_nodemask(zone, z, zonelist,
  92. MAX_NR_ZONES - 1, nodemask) {
  93. nid = zone_to_nid(zone);
  94. if (cpuset_zone_allowed_softwall(zone, htlb_alloc_mask) &&
  95. !list_empty(&hugepage_freelists[nid])) {
  96. page = list_entry(hugepage_freelists[nid].next,
  97. struct page, lru);
  98. list_del(&page->lru);
  99. free_huge_pages--;
  100. free_huge_pages_node[nid]--;
  101. if (vma && vma->vm_flags & VM_MAYSHARE)
  102. resv_huge_pages--;
  103. break;
  104. }
  105. }
  106. mpol_cond_put(mpol);
  107. return page;
  108. }
  109. static void update_and_free_page(struct page *page)
  110. {
  111. int i;
  112. nr_huge_pages--;
  113. nr_huge_pages_node[page_to_nid(page)]--;
  114. for (i = 0; i < (HPAGE_SIZE / PAGE_SIZE); i++) {
  115. page[i].flags &= ~(1 << PG_locked | 1 << PG_error | 1 << PG_referenced |
  116. 1 << PG_dirty | 1 << PG_active | 1 << PG_reserved |
  117. 1 << PG_private | 1<< PG_writeback);
  118. }
  119. set_compound_page_dtor(page, NULL);
  120. set_page_refcounted(page);
  121. arch_release_hugepage(page);
  122. __free_pages(page, HUGETLB_PAGE_ORDER);
  123. }
  124. static void free_huge_page(struct page *page)
  125. {
  126. int nid = page_to_nid(page);
  127. struct address_space *mapping;
  128. mapping = (struct address_space *) page_private(page);
  129. set_page_private(page, 0);
  130. BUG_ON(page_count(page));
  131. INIT_LIST_HEAD(&page->lru);
  132. spin_lock(&hugetlb_lock);
  133. if (surplus_huge_pages_node[nid]) {
  134. update_and_free_page(page);
  135. surplus_huge_pages--;
  136. surplus_huge_pages_node[nid]--;
  137. } else {
  138. enqueue_huge_page(page);
  139. }
  140. spin_unlock(&hugetlb_lock);
  141. if (mapping)
  142. hugetlb_put_quota(mapping, 1);
  143. }
  144. /*
  145. * Increment or decrement surplus_huge_pages. Keep node-specific counters
  146. * balanced by operating on them in a round-robin fashion.
  147. * Returns 1 if an adjustment was made.
  148. */
  149. static int adjust_pool_surplus(int delta)
  150. {
  151. static int prev_nid;
  152. int nid = prev_nid;
  153. int ret = 0;
  154. VM_BUG_ON(delta != -1 && delta != 1);
  155. do {
  156. nid = next_node(nid, node_online_map);
  157. if (nid == MAX_NUMNODES)
  158. nid = first_node(node_online_map);
  159. /* To shrink on this node, there must be a surplus page */
  160. if (delta < 0 && !surplus_huge_pages_node[nid])
  161. continue;
  162. /* Surplus cannot exceed the total number of pages */
  163. if (delta > 0 && surplus_huge_pages_node[nid] >=
  164. nr_huge_pages_node[nid])
  165. continue;
  166. surplus_huge_pages += delta;
  167. surplus_huge_pages_node[nid] += delta;
  168. ret = 1;
  169. break;
  170. } while (nid != prev_nid);
  171. prev_nid = nid;
  172. return ret;
  173. }
  174. static struct page *alloc_fresh_huge_page_node(int nid)
  175. {
  176. struct page *page;
  177. page = alloc_pages_node(nid,
  178. htlb_alloc_mask|__GFP_COMP|__GFP_THISNODE|__GFP_NOWARN,
  179. HUGETLB_PAGE_ORDER);
  180. if (page) {
  181. if (arch_prepare_hugepage(page)) {
  182. __free_pages(page, HUGETLB_PAGE_ORDER);
  183. return NULL;
  184. }
  185. set_compound_page_dtor(page, free_huge_page);
  186. spin_lock(&hugetlb_lock);
  187. nr_huge_pages++;
  188. nr_huge_pages_node[nid]++;
  189. spin_unlock(&hugetlb_lock);
  190. put_page(page); /* free it into the hugepage allocator */
  191. }
  192. return page;
  193. }
  194. static int alloc_fresh_huge_page(void)
  195. {
  196. struct page *page;
  197. int start_nid;
  198. int next_nid;
  199. int ret = 0;
  200. start_nid = hugetlb_next_nid;
  201. do {
  202. page = alloc_fresh_huge_page_node(hugetlb_next_nid);
  203. if (page)
  204. ret = 1;
  205. /*
  206. * Use a helper variable to find the next node and then
  207. * copy it back to hugetlb_next_nid afterwards:
  208. * otherwise there's a window in which a racer might
  209. * pass invalid nid MAX_NUMNODES to alloc_pages_node.
  210. * But we don't need to use a spin_lock here: it really
  211. * doesn't matter if occasionally a racer chooses the
  212. * same nid as we do. Move nid forward in the mask even
  213. * if we just successfully allocated a hugepage so that
  214. * the next caller gets hugepages on the next node.
  215. */
  216. next_nid = next_node(hugetlb_next_nid, node_online_map);
  217. if (next_nid == MAX_NUMNODES)
  218. next_nid = first_node(node_online_map);
  219. hugetlb_next_nid = next_nid;
  220. } while (!page && hugetlb_next_nid != start_nid);
  221. if (ret)
  222. count_vm_event(HTLB_BUDDY_PGALLOC);
  223. else
  224. count_vm_event(HTLB_BUDDY_PGALLOC_FAIL);
  225. return ret;
  226. }
  227. static struct page *alloc_buddy_huge_page(struct vm_area_struct *vma,
  228. unsigned long address)
  229. {
  230. struct page *page;
  231. unsigned int nid;
  232. /*
  233. * Assume we will successfully allocate the surplus page to
  234. * prevent racing processes from causing the surplus to exceed
  235. * overcommit
  236. *
  237. * This however introduces a different race, where a process B
  238. * tries to grow the static hugepage pool while alloc_pages() is
  239. * called by process A. B will only examine the per-node
  240. * counters in determining if surplus huge pages can be
  241. * converted to normal huge pages in adjust_pool_surplus(). A
  242. * won't be able to increment the per-node counter, until the
  243. * lock is dropped by B, but B doesn't drop hugetlb_lock until
  244. * no more huge pages can be converted from surplus to normal
  245. * state (and doesn't try to convert again). Thus, we have a
  246. * case where a surplus huge page exists, the pool is grown, and
  247. * the surplus huge page still exists after, even though it
  248. * should just have been converted to a normal huge page. This
  249. * does not leak memory, though, as the hugepage will be freed
  250. * once it is out of use. It also does not allow the counters to
  251. * go out of whack in adjust_pool_surplus() as we don't modify
  252. * the node values until we've gotten the hugepage and only the
  253. * per-node value is checked there.
  254. */
  255. spin_lock(&hugetlb_lock);
  256. if (surplus_huge_pages >= nr_overcommit_huge_pages) {
  257. spin_unlock(&hugetlb_lock);
  258. return NULL;
  259. } else {
  260. nr_huge_pages++;
  261. surplus_huge_pages++;
  262. }
  263. spin_unlock(&hugetlb_lock);
  264. page = alloc_pages(htlb_alloc_mask|__GFP_COMP|__GFP_NOWARN,
  265. HUGETLB_PAGE_ORDER);
  266. spin_lock(&hugetlb_lock);
  267. if (page) {
  268. /*
  269. * This page is now managed by the hugetlb allocator and has
  270. * no users -- drop the buddy allocator's reference.
  271. */
  272. put_page_testzero(page);
  273. VM_BUG_ON(page_count(page));
  274. nid = page_to_nid(page);
  275. set_compound_page_dtor(page, free_huge_page);
  276. /*
  277. * We incremented the global counters already
  278. */
  279. nr_huge_pages_node[nid]++;
  280. surplus_huge_pages_node[nid]++;
  281. __count_vm_event(HTLB_BUDDY_PGALLOC);
  282. } else {
  283. nr_huge_pages--;
  284. surplus_huge_pages--;
  285. __count_vm_event(HTLB_BUDDY_PGALLOC_FAIL);
  286. }
  287. spin_unlock(&hugetlb_lock);
  288. return page;
  289. }
  290. /*
  291. * Increase the hugetlb pool such that it can accomodate a reservation
  292. * of size 'delta'.
  293. */
  294. static int gather_surplus_pages(int delta)
  295. {
  296. struct list_head surplus_list;
  297. struct page *page, *tmp;
  298. int ret, i;
  299. int needed, allocated;
  300. needed = (resv_huge_pages + delta) - free_huge_pages;
  301. if (needed <= 0) {
  302. resv_huge_pages += delta;
  303. return 0;
  304. }
  305. allocated = 0;
  306. INIT_LIST_HEAD(&surplus_list);
  307. ret = -ENOMEM;
  308. retry:
  309. spin_unlock(&hugetlb_lock);
  310. for (i = 0; i < needed; i++) {
  311. page = alloc_buddy_huge_page(NULL, 0);
  312. if (!page) {
  313. /*
  314. * We were not able to allocate enough pages to
  315. * satisfy the entire reservation so we free what
  316. * we've allocated so far.
  317. */
  318. spin_lock(&hugetlb_lock);
  319. needed = 0;
  320. goto free;
  321. }
  322. list_add(&page->lru, &surplus_list);
  323. }
  324. allocated += needed;
  325. /*
  326. * After retaking hugetlb_lock, we need to recalculate 'needed'
  327. * because either resv_huge_pages or free_huge_pages may have changed.
  328. */
  329. spin_lock(&hugetlb_lock);
  330. needed = (resv_huge_pages + delta) - (free_huge_pages + allocated);
  331. if (needed > 0)
  332. goto retry;
  333. /*
  334. * The surplus_list now contains _at_least_ the number of extra pages
  335. * needed to accomodate the reservation. Add the appropriate number
  336. * of pages to the hugetlb pool and free the extras back to the buddy
  337. * allocator. Commit the entire reservation here to prevent another
  338. * process from stealing the pages as they are added to the pool but
  339. * before they are reserved.
  340. */
  341. needed += allocated;
  342. resv_huge_pages += delta;
  343. ret = 0;
  344. free:
  345. /* Free the needed pages to the hugetlb pool */
  346. list_for_each_entry_safe(page, tmp, &surplus_list, lru) {
  347. if ((--needed) < 0)
  348. break;
  349. list_del(&page->lru);
  350. enqueue_huge_page(page);
  351. }
  352. /* Free unnecessary surplus pages to the buddy allocator */
  353. if (!list_empty(&surplus_list)) {
  354. spin_unlock(&hugetlb_lock);
  355. list_for_each_entry_safe(page, tmp, &surplus_list, lru) {
  356. list_del(&page->lru);
  357. /*
  358. * The page has a reference count of zero already, so
  359. * call free_huge_page directly instead of using
  360. * put_page. This must be done with hugetlb_lock
  361. * unlocked which is safe because free_huge_page takes
  362. * hugetlb_lock before deciding how to free the page.
  363. */
  364. free_huge_page(page);
  365. }
  366. spin_lock(&hugetlb_lock);
  367. }
  368. return ret;
  369. }
  370. /*
  371. * When releasing a hugetlb pool reservation, any surplus pages that were
  372. * allocated to satisfy the reservation must be explicitly freed if they were
  373. * never used.
  374. */
  375. static void return_unused_surplus_pages(unsigned long unused_resv_pages)
  376. {
  377. static int nid = -1;
  378. struct page *page;
  379. unsigned long nr_pages;
  380. /*
  381. * We want to release as many surplus pages as possible, spread
  382. * evenly across all nodes. Iterate across all nodes until we
  383. * can no longer free unreserved surplus pages. This occurs when
  384. * the nodes with surplus pages have no free pages.
  385. */
  386. unsigned long remaining_iterations = num_online_nodes();
  387. /* Uncommit the reservation */
  388. resv_huge_pages -= unused_resv_pages;
  389. nr_pages = min(unused_resv_pages, surplus_huge_pages);
  390. while (remaining_iterations-- && nr_pages) {
  391. nid = next_node(nid, node_online_map);
  392. if (nid == MAX_NUMNODES)
  393. nid = first_node(node_online_map);
  394. if (!surplus_huge_pages_node[nid])
  395. continue;
  396. if (!list_empty(&hugepage_freelists[nid])) {
  397. page = list_entry(hugepage_freelists[nid].next,
  398. struct page, lru);
  399. list_del(&page->lru);
  400. update_and_free_page(page);
  401. free_huge_pages--;
  402. free_huge_pages_node[nid]--;
  403. surplus_huge_pages--;
  404. surplus_huge_pages_node[nid]--;
  405. nr_pages--;
  406. remaining_iterations = num_online_nodes();
  407. }
  408. }
  409. }
  410. static struct page *alloc_huge_page_shared(struct vm_area_struct *vma,
  411. unsigned long addr)
  412. {
  413. struct page *page;
  414. spin_lock(&hugetlb_lock);
  415. page = dequeue_huge_page_vma(vma, addr);
  416. spin_unlock(&hugetlb_lock);
  417. return page ? page : ERR_PTR(-VM_FAULT_OOM);
  418. }
  419. static struct page *alloc_huge_page_private(struct vm_area_struct *vma,
  420. unsigned long addr)
  421. {
  422. struct page *page = NULL;
  423. if (hugetlb_get_quota(vma->vm_file->f_mapping, 1))
  424. return ERR_PTR(-VM_FAULT_SIGBUS);
  425. spin_lock(&hugetlb_lock);
  426. if (free_huge_pages > resv_huge_pages)
  427. page = dequeue_huge_page_vma(vma, addr);
  428. spin_unlock(&hugetlb_lock);
  429. if (!page) {
  430. page = alloc_buddy_huge_page(vma, addr);
  431. if (!page) {
  432. hugetlb_put_quota(vma->vm_file->f_mapping, 1);
  433. return ERR_PTR(-VM_FAULT_OOM);
  434. }
  435. }
  436. return page;
  437. }
  438. static struct page *alloc_huge_page(struct vm_area_struct *vma,
  439. unsigned long addr)
  440. {
  441. struct page *page;
  442. struct address_space *mapping = vma->vm_file->f_mapping;
  443. if (vma->vm_flags & VM_MAYSHARE)
  444. page = alloc_huge_page_shared(vma, addr);
  445. else
  446. page = alloc_huge_page_private(vma, addr);
  447. if (!IS_ERR(page)) {
  448. set_page_refcounted(page);
  449. set_page_private(page, (unsigned long) mapping);
  450. }
  451. return page;
  452. }
  453. static int __init hugetlb_init(void)
  454. {
  455. unsigned long i;
  456. if (HPAGE_SHIFT == 0)
  457. return 0;
  458. for (i = 0; i < MAX_NUMNODES; ++i)
  459. INIT_LIST_HEAD(&hugepage_freelists[i]);
  460. hugetlb_next_nid = first_node(node_online_map);
  461. for (i = 0; i < max_huge_pages; ++i) {
  462. if (!alloc_fresh_huge_page())
  463. break;
  464. }
  465. max_huge_pages = free_huge_pages = nr_huge_pages = i;
  466. printk("Total HugeTLB memory allocated, %ld\n", free_huge_pages);
  467. return 0;
  468. }
  469. module_init(hugetlb_init);
  470. static int __init hugetlb_setup(char *s)
  471. {
  472. if (sscanf(s, "%lu", &max_huge_pages) <= 0)
  473. max_huge_pages = 0;
  474. return 1;
  475. }
  476. __setup("hugepages=", hugetlb_setup);
  477. static unsigned int cpuset_mems_nr(unsigned int *array)
  478. {
  479. int node;
  480. unsigned int nr = 0;
  481. for_each_node_mask(node, cpuset_current_mems_allowed)
  482. nr += array[node];
  483. return nr;
  484. }
  485. #ifdef CONFIG_SYSCTL
  486. #ifdef CONFIG_HIGHMEM
  487. static void try_to_free_low(unsigned long count)
  488. {
  489. int i;
  490. for (i = 0; i < MAX_NUMNODES; ++i) {
  491. struct page *page, *next;
  492. list_for_each_entry_safe(page, next, &hugepage_freelists[i], lru) {
  493. if (count >= nr_huge_pages)
  494. return;
  495. if (PageHighMem(page))
  496. continue;
  497. list_del(&page->lru);
  498. update_and_free_page(page);
  499. free_huge_pages--;
  500. free_huge_pages_node[page_to_nid(page)]--;
  501. }
  502. }
  503. }
  504. #else
  505. static inline void try_to_free_low(unsigned long count)
  506. {
  507. }
  508. #endif
  509. #define persistent_huge_pages (nr_huge_pages - surplus_huge_pages)
  510. static unsigned long set_max_huge_pages(unsigned long count)
  511. {
  512. unsigned long min_count, ret;
  513. /*
  514. * Increase the pool size
  515. * First take pages out of surplus state. Then make up the
  516. * remaining difference by allocating fresh huge pages.
  517. *
  518. * We might race with alloc_buddy_huge_page() here and be unable
  519. * to convert a surplus huge page to a normal huge page. That is
  520. * not critical, though, it just means the overall size of the
  521. * pool might be one hugepage larger than it needs to be, but
  522. * within all the constraints specified by the sysctls.
  523. */
  524. spin_lock(&hugetlb_lock);
  525. while (surplus_huge_pages && count > persistent_huge_pages) {
  526. if (!adjust_pool_surplus(-1))
  527. break;
  528. }
  529. while (count > persistent_huge_pages) {
  530. int ret;
  531. /*
  532. * If this allocation races such that we no longer need the
  533. * page, free_huge_page will handle it by freeing the page
  534. * and reducing the surplus.
  535. */
  536. spin_unlock(&hugetlb_lock);
  537. ret = alloc_fresh_huge_page();
  538. spin_lock(&hugetlb_lock);
  539. if (!ret)
  540. goto out;
  541. }
  542. /*
  543. * Decrease the pool size
  544. * First return free pages to the buddy allocator (being careful
  545. * to keep enough around to satisfy reservations). Then place
  546. * pages into surplus state as needed so the pool will shrink
  547. * to the desired size as pages become free.
  548. *
  549. * By placing pages into the surplus state independent of the
  550. * overcommit value, we are allowing the surplus pool size to
  551. * exceed overcommit. There are few sane options here. Since
  552. * alloc_buddy_huge_page() is checking the global counter,
  553. * though, we'll note that we're not allowed to exceed surplus
  554. * and won't grow the pool anywhere else. Not until one of the
  555. * sysctls are changed, or the surplus pages go out of use.
  556. */
  557. min_count = resv_huge_pages + nr_huge_pages - free_huge_pages;
  558. min_count = max(count, min_count);
  559. try_to_free_low(min_count);
  560. while (min_count < persistent_huge_pages) {
  561. struct page *page = dequeue_huge_page();
  562. if (!page)
  563. break;
  564. update_and_free_page(page);
  565. }
  566. while (count < persistent_huge_pages) {
  567. if (!adjust_pool_surplus(1))
  568. break;
  569. }
  570. out:
  571. ret = persistent_huge_pages;
  572. spin_unlock(&hugetlb_lock);
  573. return ret;
  574. }
  575. int hugetlb_sysctl_handler(struct ctl_table *table, int write,
  576. struct file *file, void __user *buffer,
  577. size_t *length, loff_t *ppos)
  578. {
  579. proc_doulongvec_minmax(table, write, file, buffer, length, ppos);
  580. max_huge_pages = set_max_huge_pages(max_huge_pages);
  581. return 0;
  582. }
  583. int hugetlb_treat_movable_handler(struct ctl_table *table, int write,
  584. struct file *file, void __user *buffer,
  585. size_t *length, loff_t *ppos)
  586. {
  587. proc_dointvec(table, write, file, buffer, length, ppos);
  588. if (hugepages_treat_as_movable)
  589. htlb_alloc_mask = GFP_HIGHUSER_MOVABLE;
  590. else
  591. htlb_alloc_mask = GFP_HIGHUSER;
  592. return 0;
  593. }
  594. int hugetlb_overcommit_handler(struct ctl_table *table, int write,
  595. struct file *file, void __user *buffer,
  596. size_t *length, loff_t *ppos)
  597. {
  598. proc_doulongvec_minmax(table, write, file, buffer, length, ppos);
  599. spin_lock(&hugetlb_lock);
  600. nr_overcommit_huge_pages = sysctl_overcommit_huge_pages;
  601. spin_unlock(&hugetlb_lock);
  602. return 0;
  603. }
  604. #endif /* CONFIG_SYSCTL */
  605. int hugetlb_report_meminfo(char *buf)
  606. {
  607. return sprintf(buf,
  608. "HugePages_Total: %5lu\n"
  609. "HugePages_Free: %5lu\n"
  610. "HugePages_Rsvd: %5lu\n"
  611. "HugePages_Surp: %5lu\n"
  612. "Hugepagesize: %5lu kB\n",
  613. nr_huge_pages,
  614. free_huge_pages,
  615. resv_huge_pages,
  616. surplus_huge_pages,
  617. HPAGE_SIZE/1024);
  618. }
  619. int hugetlb_report_node_meminfo(int nid, char *buf)
  620. {
  621. return sprintf(buf,
  622. "Node %d HugePages_Total: %5u\n"
  623. "Node %d HugePages_Free: %5u\n"
  624. "Node %d HugePages_Surp: %5u\n",
  625. nid, nr_huge_pages_node[nid],
  626. nid, free_huge_pages_node[nid],
  627. nid, surplus_huge_pages_node[nid]);
  628. }
  629. /* Return the number pages of memory we physically have, in PAGE_SIZE units. */
  630. unsigned long hugetlb_total_pages(void)
  631. {
  632. return nr_huge_pages * (HPAGE_SIZE / PAGE_SIZE);
  633. }
  634. /*
  635. * We cannot handle pagefaults against hugetlb pages at all. They cause
  636. * handle_mm_fault() to try to instantiate regular-sized pages in the
  637. * hugegpage VMA. do_page_fault() is supposed to trap this, so BUG is we get
  638. * this far.
  639. */
  640. static int hugetlb_vm_op_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  641. {
  642. BUG();
  643. return 0;
  644. }
  645. struct vm_operations_struct hugetlb_vm_ops = {
  646. .fault = hugetlb_vm_op_fault,
  647. };
  648. static pte_t make_huge_pte(struct vm_area_struct *vma, struct page *page,
  649. int writable)
  650. {
  651. pte_t entry;
  652. if (writable) {
  653. entry =
  654. pte_mkwrite(pte_mkdirty(mk_pte(page, vma->vm_page_prot)));
  655. } else {
  656. entry = huge_pte_wrprotect(mk_pte(page, vma->vm_page_prot));
  657. }
  658. entry = pte_mkyoung(entry);
  659. entry = pte_mkhuge(entry);
  660. return entry;
  661. }
  662. static void set_huge_ptep_writable(struct vm_area_struct *vma,
  663. unsigned long address, pte_t *ptep)
  664. {
  665. pte_t entry;
  666. entry = pte_mkwrite(pte_mkdirty(huge_ptep_get(ptep)));
  667. if (huge_ptep_set_access_flags(vma, address, ptep, entry, 1)) {
  668. update_mmu_cache(vma, address, entry);
  669. }
  670. }
  671. int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
  672. struct vm_area_struct *vma)
  673. {
  674. pte_t *src_pte, *dst_pte, entry;
  675. struct page *ptepage;
  676. unsigned long addr;
  677. int cow;
  678. cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
  679. for (addr = vma->vm_start; addr < vma->vm_end; addr += HPAGE_SIZE) {
  680. src_pte = huge_pte_offset(src, addr);
  681. if (!src_pte)
  682. continue;
  683. dst_pte = huge_pte_alloc(dst, addr);
  684. if (!dst_pte)
  685. goto nomem;
  686. /* If the pagetables are shared don't copy or take references */
  687. if (dst_pte == src_pte)
  688. continue;
  689. spin_lock(&dst->page_table_lock);
  690. spin_lock(&src->page_table_lock);
  691. if (!huge_pte_none(huge_ptep_get(src_pte))) {
  692. if (cow)
  693. huge_ptep_set_wrprotect(src, addr, src_pte);
  694. entry = huge_ptep_get(src_pte);
  695. ptepage = pte_page(entry);
  696. get_page(ptepage);
  697. set_huge_pte_at(dst, addr, dst_pte, entry);
  698. }
  699. spin_unlock(&src->page_table_lock);
  700. spin_unlock(&dst->page_table_lock);
  701. }
  702. return 0;
  703. nomem:
  704. return -ENOMEM;
  705. }
  706. void __unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
  707. unsigned long end)
  708. {
  709. struct mm_struct *mm = vma->vm_mm;
  710. unsigned long address;
  711. pte_t *ptep;
  712. pte_t pte;
  713. struct page *page;
  714. struct page *tmp;
  715. /*
  716. * A page gathering list, protected by per file i_mmap_lock. The
  717. * lock is used to avoid list corruption from multiple unmapping
  718. * of the same page since we are using page->lru.
  719. */
  720. LIST_HEAD(page_list);
  721. WARN_ON(!is_vm_hugetlb_page(vma));
  722. BUG_ON(start & ~HPAGE_MASK);
  723. BUG_ON(end & ~HPAGE_MASK);
  724. spin_lock(&mm->page_table_lock);
  725. for (address = start; address < end; address += HPAGE_SIZE) {
  726. ptep = huge_pte_offset(mm, address);
  727. if (!ptep)
  728. continue;
  729. if (huge_pmd_unshare(mm, &address, ptep))
  730. continue;
  731. pte = huge_ptep_get_and_clear(mm, address, ptep);
  732. if (huge_pte_none(pte))
  733. continue;
  734. page = pte_page(pte);
  735. if (pte_dirty(pte))
  736. set_page_dirty(page);
  737. list_add(&page->lru, &page_list);
  738. }
  739. spin_unlock(&mm->page_table_lock);
  740. flush_tlb_range(vma, start, end);
  741. list_for_each_entry_safe(page, tmp, &page_list, lru) {
  742. list_del(&page->lru);
  743. put_page(page);
  744. }
  745. }
  746. void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
  747. unsigned long end)
  748. {
  749. /*
  750. * It is undesirable to test vma->vm_file as it should be non-null
  751. * for valid hugetlb area. However, vm_file will be NULL in the error
  752. * cleanup path of do_mmap_pgoff. When hugetlbfs ->mmap method fails,
  753. * do_mmap_pgoff() nullifies vma->vm_file before calling this function
  754. * to clean up. Since no pte has actually been setup, it is safe to
  755. * do nothing in this case.
  756. */
  757. if (vma->vm_file) {
  758. spin_lock(&vma->vm_file->f_mapping->i_mmap_lock);
  759. __unmap_hugepage_range(vma, start, end);
  760. spin_unlock(&vma->vm_file->f_mapping->i_mmap_lock);
  761. }
  762. }
  763. static int hugetlb_cow(struct mm_struct *mm, struct vm_area_struct *vma,
  764. unsigned long address, pte_t *ptep, pte_t pte)
  765. {
  766. struct page *old_page, *new_page;
  767. int avoidcopy;
  768. old_page = pte_page(pte);
  769. /* If no-one else is actually using this page, avoid the copy
  770. * and just make the page writable */
  771. avoidcopy = (page_count(old_page) == 1);
  772. if (avoidcopy) {
  773. set_huge_ptep_writable(vma, address, ptep);
  774. return 0;
  775. }
  776. page_cache_get(old_page);
  777. new_page = alloc_huge_page(vma, address);
  778. if (IS_ERR(new_page)) {
  779. page_cache_release(old_page);
  780. return -PTR_ERR(new_page);
  781. }
  782. spin_unlock(&mm->page_table_lock);
  783. copy_huge_page(new_page, old_page, address, vma);
  784. __SetPageUptodate(new_page);
  785. spin_lock(&mm->page_table_lock);
  786. ptep = huge_pte_offset(mm, address & HPAGE_MASK);
  787. if (likely(pte_same(huge_ptep_get(ptep), pte))) {
  788. /* Break COW */
  789. huge_ptep_clear_flush(vma, address, ptep);
  790. set_huge_pte_at(mm, address, ptep,
  791. make_huge_pte(vma, new_page, 1));
  792. /* Make the old page be freed below */
  793. new_page = old_page;
  794. }
  795. page_cache_release(new_page);
  796. page_cache_release(old_page);
  797. return 0;
  798. }
  799. static int hugetlb_no_page(struct mm_struct *mm, struct vm_area_struct *vma,
  800. unsigned long address, pte_t *ptep, int write_access)
  801. {
  802. int ret = VM_FAULT_SIGBUS;
  803. unsigned long idx;
  804. unsigned long size;
  805. struct page *page;
  806. struct address_space *mapping;
  807. pte_t new_pte;
  808. mapping = vma->vm_file->f_mapping;
  809. idx = ((address - vma->vm_start) >> HPAGE_SHIFT)
  810. + (vma->vm_pgoff >> (HPAGE_SHIFT - PAGE_SHIFT));
  811. /*
  812. * Use page lock to guard against racing truncation
  813. * before we get page_table_lock.
  814. */
  815. retry:
  816. page = find_lock_page(mapping, idx);
  817. if (!page) {
  818. size = i_size_read(mapping->host) >> HPAGE_SHIFT;
  819. if (idx >= size)
  820. goto out;
  821. page = alloc_huge_page(vma, address);
  822. if (IS_ERR(page)) {
  823. ret = -PTR_ERR(page);
  824. goto out;
  825. }
  826. clear_huge_page(page, address);
  827. __SetPageUptodate(page);
  828. if (vma->vm_flags & VM_SHARED) {
  829. int err;
  830. struct inode *inode = mapping->host;
  831. err = add_to_page_cache(page, mapping, idx, GFP_KERNEL);
  832. if (err) {
  833. put_page(page);
  834. if (err == -EEXIST)
  835. goto retry;
  836. goto out;
  837. }
  838. spin_lock(&inode->i_lock);
  839. inode->i_blocks += BLOCKS_PER_HUGEPAGE;
  840. spin_unlock(&inode->i_lock);
  841. } else
  842. lock_page(page);
  843. }
  844. spin_lock(&mm->page_table_lock);
  845. size = i_size_read(mapping->host) >> HPAGE_SHIFT;
  846. if (idx >= size)
  847. goto backout;
  848. ret = 0;
  849. if (!huge_pte_none(huge_ptep_get(ptep)))
  850. goto backout;
  851. new_pte = make_huge_pte(vma, page, ((vma->vm_flags & VM_WRITE)
  852. && (vma->vm_flags & VM_SHARED)));
  853. set_huge_pte_at(mm, address, ptep, new_pte);
  854. if (write_access && !(vma->vm_flags & VM_SHARED)) {
  855. /* Optimization, do the COW without a second fault */
  856. ret = hugetlb_cow(mm, vma, address, ptep, new_pte);
  857. }
  858. spin_unlock(&mm->page_table_lock);
  859. unlock_page(page);
  860. out:
  861. return ret;
  862. backout:
  863. spin_unlock(&mm->page_table_lock);
  864. unlock_page(page);
  865. put_page(page);
  866. goto out;
  867. }
  868. int hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,
  869. unsigned long address, int write_access)
  870. {
  871. pte_t *ptep;
  872. pte_t entry;
  873. int ret;
  874. static DEFINE_MUTEX(hugetlb_instantiation_mutex);
  875. ptep = huge_pte_alloc(mm, address);
  876. if (!ptep)
  877. return VM_FAULT_OOM;
  878. /*
  879. * Serialize hugepage allocation and instantiation, so that we don't
  880. * get spurious allocation failures if two CPUs race to instantiate
  881. * the same page in the page cache.
  882. */
  883. mutex_lock(&hugetlb_instantiation_mutex);
  884. entry = huge_ptep_get(ptep);
  885. if (huge_pte_none(entry)) {
  886. ret = hugetlb_no_page(mm, vma, address, ptep, write_access);
  887. mutex_unlock(&hugetlb_instantiation_mutex);
  888. return ret;
  889. }
  890. ret = 0;
  891. spin_lock(&mm->page_table_lock);
  892. /* Check for a racing update before calling hugetlb_cow */
  893. if (likely(pte_same(entry, huge_ptep_get(ptep))))
  894. if (write_access && !pte_write(entry))
  895. ret = hugetlb_cow(mm, vma, address, ptep, entry);
  896. spin_unlock(&mm->page_table_lock);
  897. mutex_unlock(&hugetlb_instantiation_mutex);
  898. return ret;
  899. }
  900. int follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma,
  901. struct page **pages, struct vm_area_struct **vmas,
  902. unsigned long *position, int *length, int i,
  903. int write)
  904. {
  905. unsigned long pfn_offset;
  906. unsigned long vaddr = *position;
  907. int remainder = *length;
  908. spin_lock(&mm->page_table_lock);
  909. while (vaddr < vma->vm_end && remainder) {
  910. pte_t *pte;
  911. struct page *page;
  912. /*
  913. * Some archs (sparc64, sh*) have multiple pte_ts to
  914. * each hugepage. We have to make * sure we get the
  915. * first, for the page indexing below to work.
  916. */
  917. pte = huge_pte_offset(mm, vaddr & HPAGE_MASK);
  918. if (!pte || huge_pte_none(huge_ptep_get(pte)) ||
  919. (write && !pte_write(huge_ptep_get(pte)))) {
  920. int ret;
  921. spin_unlock(&mm->page_table_lock);
  922. ret = hugetlb_fault(mm, vma, vaddr, write);
  923. spin_lock(&mm->page_table_lock);
  924. if (!(ret & VM_FAULT_ERROR))
  925. continue;
  926. remainder = 0;
  927. if (!i)
  928. i = -EFAULT;
  929. break;
  930. }
  931. pfn_offset = (vaddr & ~HPAGE_MASK) >> PAGE_SHIFT;
  932. page = pte_page(huge_ptep_get(pte));
  933. same_page:
  934. if (pages) {
  935. get_page(page);
  936. pages[i] = page + pfn_offset;
  937. }
  938. if (vmas)
  939. vmas[i] = vma;
  940. vaddr += PAGE_SIZE;
  941. ++pfn_offset;
  942. --remainder;
  943. ++i;
  944. if (vaddr < vma->vm_end && remainder &&
  945. pfn_offset < HPAGE_SIZE/PAGE_SIZE) {
  946. /*
  947. * We use pfn_offset to avoid touching the pageframes
  948. * of this compound page.
  949. */
  950. goto same_page;
  951. }
  952. }
  953. spin_unlock(&mm->page_table_lock);
  954. *length = remainder;
  955. *position = vaddr;
  956. return i;
  957. }
  958. void hugetlb_change_protection(struct vm_area_struct *vma,
  959. unsigned long address, unsigned long end, pgprot_t newprot)
  960. {
  961. struct mm_struct *mm = vma->vm_mm;
  962. unsigned long start = address;
  963. pte_t *ptep;
  964. pte_t pte;
  965. BUG_ON(address >= end);
  966. flush_cache_range(vma, address, end);
  967. spin_lock(&vma->vm_file->f_mapping->i_mmap_lock);
  968. spin_lock(&mm->page_table_lock);
  969. for (; address < end; address += HPAGE_SIZE) {
  970. ptep = huge_pte_offset(mm, address);
  971. if (!ptep)
  972. continue;
  973. if (huge_pmd_unshare(mm, &address, ptep))
  974. continue;
  975. if (!huge_pte_none(huge_ptep_get(ptep))) {
  976. pte = huge_ptep_get_and_clear(mm, address, ptep);
  977. pte = pte_mkhuge(pte_modify(pte, newprot));
  978. set_huge_pte_at(mm, address, ptep, pte);
  979. }
  980. }
  981. spin_unlock(&mm->page_table_lock);
  982. spin_unlock(&vma->vm_file->f_mapping->i_mmap_lock);
  983. flush_tlb_range(vma, start, end);
  984. }
  985. struct file_region {
  986. struct list_head link;
  987. long from;
  988. long to;
  989. };
  990. static long region_add(struct list_head *head, long f, long t)
  991. {
  992. struct file_region *rg, *nrg, *trg;
  993. /* Locate the region we are either in or before. */
  994. list_for_each_entry(rg, head, link)
  995. if (f <= rg->to)
  996. break;
  997. /* Round our left edge to the current segment if it encloses us. */
  998. if (f > rg->from)
  999. f = rg->from;
  1000. /* Check for and consume any regions we now overlap with. */
  1001. nrg = rg;
  1002. list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
  1003. if (&rg->link == head)
  1004. break;
  1005. if (rg->from > t)
  1006. break;
  1007. /* If this area reaches higher then extend our area to
  1008. * include it completely. If this is not the first area
  1009. * which we intend to reuse, free it. */
  1010. if (rg->to > t)
  1011. t = rg->to;
  1012. if (rg != nrg) {
  1013. list_del(&rg->link);
  1014. kfree(rg);
  1015. }
  1016. }
  1017. nrg->from = f;
  1018. nrg->to = t;
  1019. return 0;
  1020. }
  1021. static long region_chg(struct list_head *head, long f, long t)
  1022. {
  1023. struct file_region *rg, *nrg;
  1024. long chg = 0;
  1025. /* Locate the region we are before or in. */
  1026. list_for_each_entry(rg, head, link)
  1027. if (f <= rg->to)
  1028. break;
  1029. /* If we are below the current region then a new region is required.
  1030. * Subtle, allocate a new region at the position but make it zero
  1031. * size such that we can guarantee to record the reservation. */
  1032. if (&rg->link == head || t < rg->from) {
  1033. nrg = kmalloc(sizeof(*nrg), GFP_KERNEL);
  1034. if (!nrg)
  1035. return -ENOMEM;
  1036. nrg->from = f;
  1037. nrg->to = f;
  1038. INIT_LIST_HEAD(&nrg->link);
  1039. list_add(&nrg->link, rg->link.prev);
  1040. return t - f;
  1041. }
  1042. /* Round our left edge to the current segment if it encloses us. */
  1043. if (f > rg->from)
  1044. f = rg->from;
  1045. chg = t - f;
  1046. /* Check for and consume any regions we now overlap with. */
  1047. list_for_each_entry(rg, rg->link.prev, link) {
  1048. if (&rg->link == head)
  1049. break;
  1050. if (rg->from > t)
  1051. return chg;
  1052. /* We overlap with this area, if it extends futher than
  1053. * us then we must extend ourselves. Account for its
  1054. * existing reservation. */
  1055. if (rg->to > t) {
  1056. chg += rg->to - t;
  1057. t = rg->to;
  1058. }
  1059. chg -= rg->to - rg->from;
  1060. }
  1061. return chg;
  1062. }
  1063. static long region_truncate(struct list_head *head, long end)
  1064. {
  1065. struct file_region *rg, *trg;
  1066. long chg = 0;
  1067. /* Locate the region we are either in or before. */
  1068. list_for_each_entry(rg, head, link)
  1069. if (end <= rg->to)
  1070. break;
  1071. if (&rg->link == head)
  1072. return 0;
  1073. /* If we are in the middle of a region then adjust it. */
  1074. if (end > rg->from) {
  1075. chg = rg->to - end;
  1076. rg->to = end;
  1077. rg = list_entry(rg->link.next, typeof(*rg), link);
  1078. }
  1079. /* Drop any remaining regions. */
  1080. list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
  1081. if (&rg->link == head)
  1082. break;
  1083. chg += rg->to - rg->from;
  1084. list_del(&rg->link);
  1085. kfree(rg);
  1086. }
  1087. return chg;
  1088. }
  1089. static int hugetlb_acct_memory(long delta)
  1090. {
  1091. int ret = -ENOMEM;
  1092. spin_lock(&hugetlb_lock);
  1093. /*
  1094. * When cpuset is configured, it breaks the strict hugetlb page
  1095. * reservation as the accounting is done on a global variable. Such
  1096. * reservation is completely rubbish in the presence of cpuset because
  1097. * the reservation is not checked against page availability for the
  1098. * current cpuset. Application can still potentially OOM'ed by kernel
  1099. * with lack of free htlb page in cpuset that the task is in.
  1100. * Attempt to enforce strict accounting with cpuset is almost
  1101. * impossible (or too ugly) because cpuset is too fluid that
  1102. * task or memory node can be dynamically moved between cpusets.
  1103. *
  1104. * The change of semantics for shared hugetlb mapping with cpuset is
  1105. * undesirable. However, in order to preserve some of the semantics,
  1106. * we fall back to check against current free page availability as
  1107. * a best attempt and hopefully to minimize the impact of changing
  1108. * semantics that cpuset has.
  1109. */
  1110. if (delta > 0) {
  1111. if (gather_surplus_pages(delta) < 0)
  1112. goto out;
  1113. if (delta > cpuset_mems_nr(free_huge_pages_node)) {
  1114. return_unused_surplus_pages(delta);
  1115. goto out;
  1116. }
  1117. }
  1118. ret = 0;
  1119. if (delta < 0)
  1120. return_unused_surplus_pages((unsigned long) -delta);
  1121. out:
  1122. spin_unlock(&hugetlb_lock);
  1123. return ret;
  1124. }
  1125. int hugetlb_reserve_pages(struct inode *inode, long from, long to)
  1126. {
  1127. long ret, chg;
  1128. chg = region_chg(&inode->i_mapping->private_list, from, to);
  1129. if (chg < 0)
  1130. return chg;
  1131. if (hugetlb_get_quota(inode->i_mapping, chg))
  1132. return -ENOSPC;
  1133. ret = hugetlb_acct_memory(chg);
  1134. if (ret < 0) {
  1135. hugetlb_put_quota(inode->i_mapping, chg);
  1136. return ret;
  1137. }
  1138. region_add(&inode->i_mapping->private_list, from, to);
  1139. return 0;
  1140. }
  1141. void hugetlb_unreserve_pages(struct inode *inode, long offset, long freed)
  1142. {
  1143. long chg = region_truncate(&inode->i_mapping->private_list, offset);
  1144. spin_lock(&inode->i_lock);
  1145. inode->i_blocks -= BLOCKS_PER_HUGEPAGE * freed;
  1146. spin_unlock(&inode->i_lock);
  1147. hugetlb_put_quota(inode->i_mapping, (chg - freed));
  1148. hugetlb_acct_memory(-(chg - freed));
  1149. }