hugetlb.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  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. unsigned long max_huge_pages;
  24. static struct list_head hugepage_freelists[MAX_NUMNODES];
  25. static unsigned int nr_huge_pages_node[MAX_NUMNODES];
  26. static unsigned int free_huge_pages_node[MAX_NUMNODES];
  27. /*
  28. * Protects updates to hugepage_freelists, nr_huge_pages, and free_huge_pages
  29. */
  30. static DEFINE_SPINLOCK(hugetlb_lock);
  31. static void clear_huge_page(struct page *page, unsigned long addr)
  32. {
  33. int i;
  34. might_sleep();
  35. for (i = 0; i < (HPAGE_SIZE/PAGE_SIZE); i++) {
  36. cond_resched();
  37. clear_user_highpage(page + i, addr);
  38. }
  39. }
  40. static void copy_huge_page(struct page *dst, struct page *src,
  41. unsigned long addr)
  42. {
  43. int i;
  44. might_sleep();
  45. for (i = 0; i < HPAGE_SIZE/PAGE_SIZE; i++) {
  46. cond_resched();
  47. copy_user_highpage(dst + i, src + i, addr + i*PAGE_SIZE);
  48. }
  49. }
  50. static void enqueue_huge_page(struct page *page)
  51. {
  52. int nid = page_to_nid(page);
  53. list_add(&page->lru, &hugepage_freelists[nid]);
  54. free_huge_pages++;
  55. free_huge_pages_node[nid]++;
  56. }
  57. static struct page *dequeue_huge_page(struct vm_area_struct *vma,
  58. unsigned long address)
  59. {
  60. int nid = numa_node_id();
  61. struct page *page = NULL;
  62. struct zonelist *zonelist = huge_zonelist(vma, address);
  63. struct zone **z;
  64. for (z = zonelist->zones; *z; z++) {
  65. nid = (*z)->zone_pgdat->node_id;
  66. if (cpuset_zone_allowed(*z, GFP_HIGHUSER) &&
  67. !list_empty(&hugepage_freelists[nid]))
  68. break;
  69. }
  70. if (*z) {
  71. page = list_entry(hugepage_freelists[nid].next,
  72. struct page, lru);
  73. list_del(&page->lru);
  74. free_huge_pages--;
  75. free_huge_pages_node[nid]--;
  76. }
  77. return page;
  78. }
  79. static void free_huge_page(struct page *page)
  80. {
  81. BUG_ON(page_count(page));
  82. INIT_LIST_HEAD(&page->lru);
  83. spin_lock(&hugetlb_lock);
  84. enqueue_huge_page(page);
  85. spin_unlock(&hugetlb_lock);
  86. }
  87. static int alloc_fresh_huge_page(void)
  88. {
  89. static int nid = 0;
  90. struct page *page;
  91. page = alloc_pages_node(nid, GFP_HIGHUSER|__GFP_COMP|__GFP_NOWARN,
  92. HUGETLB_PAGE_ORDER);
  93. nid = next_node(nid, node_online_map);
  94. if (nid == MAX_NUMNODES)
  95. nid = first_node(node_online_map);
  96. if (page) {
  97. page[1].lru.next = (void *)free_huge_page; /* dtor */
  98. spin_lock(&hugetlb_lock);
  99. nr_huge_pages++;
  100. nr_huge_pages_node[page_to_nid(page)]++;
  101. spin_unlock(&hugetlb_lock);
  102. put_page(page); /* free it into the hugepage allocator */
  103. return 1;
  104. }
  105. return 0;
  106. }
  107. static struct page *alloc_huge_page(struct vm_area_struct *vma,
  108. unsigned long addr)
  109. {
  110. struct page *page;
  111. spin_lock(&hugetlb_lock);
  112. if (vma->vm_flags & VM_MAYSHARE)
  113. resv_huge_pages--;
  114. else if (free_huge_pages <= resv_huge_pages)
  115. goto fail;
  116. page = dequeue_huge_page(vma, addr);
  117. if (!page)
  118. goto fail;
  119. spin_unlock(&hugetlb_lock);
  120. set_page_refcounted(page);
  121. return page;
  122. fail:
  123. spin_unlock(&hugetlb_lock);
  124. return NULL;
  125. }
  126. static int __init hugetlb_init(void)
  127. {
  128. unsigned long i;
  129. if (HPAGE_SHIFT == 0)
  130. return 0;
  131. for (i = 0; i < MAX_NUMNODES; ++i)
  132. INIT_LIST_HEAD(&hugepage_freelists[i]);
  133. for (i = 0; i < max_huge_pages; ++i) {
  134. if (!alloc_fresh_huge_page())
  135. break;
  136. }
  137. max_huge_pages = free_huge_pages = nr_huge_pages = i;
  138. printk("Total HugeTLB memory allocated, %ld\n", free_huge_pages);
  139. return 0;
  140. }
  141. module_init(hugetlb_init);
  142. static int __init hugetlb_setup(char *s)
  143. {
  144. if (sscanf(s, "%lu", &max_huge_pages) <= 0)
  145. max_huge_pages = 0;
  146. return 1;
  147. }
  148. __setup("hugepages=", hugetlb_setup);
  149. #ifdef CONFIG_SYSCTL
  150. static void update_and_free_page(struct page *page)
  151. {
  152. int i;
  153. nr_huge_pages--;
  154. nr_huge_pages_node[page_zone(page)->zone_pgdat->node_id]--;
  155. for (i = 0; i < (HPAGE_SIZE / PAGE_SIZE); i++) {
  156. page[i].flags &= ~(1 << PG_locked | 1 << PG_error | 1 << PG_referenced |
  157. 1 << PG_dirty | 1 << PG_active | 1 << PG_reserved |
  158. 1 << PG_private | 1<< PG_writeback);
  159. }
  160. page[1].lru.next = NULL;
  161. set_page_refcounted(page);
  162. __free_pages(page, HUGETLB_PAGE_ORDER);
  163. }
  164. #ifdef CONFIG_HIGHMEM
  165. static void try_to_free_low(unsigned long count)
  166. {
  167. int i, nid;
  168. for (i = 0; i < MAX_NUMNODES; ++i) {
  169. struct page *page, *next;
  170. list_for_each_entry_safe(page, next, &hugepage_freelists[i], lru) {
  171. if (PageHighMem(page))
  172. continue;
  173. list_del(&page->lru);
  174. update_and_free_page(page);
  175. nid = page_zone(page)->zone_pgdat->node_id;
  176. free_huge_pages--;
  177. free_huge_pages_node[nid]--;
  178. if (count >= nr_huge_pages)
  179. return;
  180. }
  181. }
  182. }
  183. #else
  184. static inline void try_to_free_low(unsigned long count)
  185. {
  186. }
  187. #endif
  188. static unsigned long set_max_huge_pages(unsigned long count)
  189. {
  190. while (count > nr_huge_pages) {
  191. if (!alloc_fresh_huge_page())
  192. return nr_huge_pages;
  193. }
  194. if (count >= nr_huge_pages)
  195. return nr_huge_pages;
  196. spin_lock(&hugetlb_lock);
  197. count = max(count, resv_huge_pages);
  198. try_to_free_low(count);
  199. while (count < nr_huge_pages) {
  200. struct page *page = dequeue_huge_page(NULL, 0);
  201. if (!page)
  202. break;
  203. update_and_free_page(page);
  204. }
  205. spin_unlock(&hugetlb_lock);
  206. return nr_huge_pages;
  207. }
  208. int hugetlb_sysctl_handler(struct ctl_table *table, int write,
  209. struct file *file, void __user *buffer,
  210. size_t *length, loff_t *ppos)
  211. {
  212. proc_doulongvec_minmax(table, write, file, buffer, length, ppos);
  213. max_huge_pages = set_max_huge_pages(max_huge_pages);
  214. return 0;
  215. }
  216. #endif /* CONFIG_SYSCTL */
  217. int hugetlb_report_meminfo(char *buf)
  218. {
  219. return sprintf(buf,
  220. "HugePages_Total: %5lu\n"
  221. "HugePages_Free: %5lu\n"
  222. "HugePages_Rsvd: %5lu\n"
  223. "Hugepagesize: %5lu kB\n",
  224. nr_huge_pages,
  225. free_huge_pages,
  226. resv_huge_pages,
  227. HPAGE_SIZE/1024);
  228. }
  229. int hugetlb_report_node_meminfo(int nid, char *buf)
  230. {
  231. return sprintf(buf,
  232. "Node %d HugePages_Total: %5u\n"
  233. "Node %d HugePages_Free: %5u\n",
  234. nid, nr_huge_pages_node[nid],
  235. nid, free_huge_pages_node[nid]);
  236. }
  237. /* Return the number pages of memory we physically have, in PAGE_SIZE units. */
  238. unsigned long hugetlb_total_pages(void)
  239. {
  240. return nr_huge_pages * (HPAGE_SIZE / PAGE_SIZE);
  241. }
  242. /*
  243. * We cannot handle pagefaults against hugetlb pages at all. They cause
  244. * handle_mm_fault() to try to instantiate regular-sized pages in the
  245. * hugegpage VMA. do_page_fault() is supposed to trap this, so BUG is we get
  246. * this far.
  247. */
  248. static struct page *hugetlb_nopage(struct vm_area_struct *vma,
  249. unsigned long address, int *unused)
  250. {
  251. BUG();
  252. return NULL;
  253. }
  254. struct vm_operations_struct hugetlb_vm_ops = {
  255. .nopage = hugetlb_nopage,
  256. };
  257. static pte_t make_huge_pte(struct vm_area_struct *vma, struct page *page,
  258. int writable)
  259. {
  260. pte_t entry;
  261. if (writable) {
  262. entry =
  263. pte_mkwrite(pte_mkdirty(mk_pte(page, vma->vm_page_prot)));
  264. } else {
  265. entry = pte_wrprotect(mk_pte(page, vma->vm_page_prot));
  266. }
  267. entry = pte_mkyoung(entry);
  268. entry = pte_mkhuge(entry);
  269. return entry;
  270. }
  271. static void set_huge_ptep_writable(struct vm_area_struct *vma,
  272. unsigned long address, pte_t *ptep)
  273. {
  274. pte_t entry;
  275. entry = pte_mkwrite(pte_mkdirty(*ptep));
  276. ptep_set_access_flags(vma, address, ptep, entry, 1);
  277. update_mmu_cache(vma, address, entry);
  278. lazy_mmu_prot_update(entry);
  279. }
  280. int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
  281. struct vm_area_struct *vma)
  282. {
  283. pte_t *src_pte, *dst_pte, entry;
  284. struct page *ptepage;
  285. unsigned long addr;
  286. int cow;
  287. cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
  288. for (addr = vma->vm_start; addr < vma->vm_end; addr += HPAGE_SIZE) {
  289. src_pte = huge_pte_offset(src, addr);
  290. if (!src_pte)
  291. continue;
  292. dst_pte = huge_pte_alloc(dst, addr);
  293. if (!dst_pte)
  294. goto nomem;
  295. spin_lock(&dst->page_table_lock);
  296. spin_lock(&src->page_table_lock);
  297. if (!pte_none(*src_pte)) {
  298. if (cow)
  299. ptep_set_wrprotect(src, addr, src_pte);
  300. entry = *src_pte;
  301. ptepage = pte_page(entry);
  302. get_page(ptepage);
  303. add_mm_counter(dst, file_rss, HPAGE_SIZE / PAGE_SIZE);
  304. set_huge_pte_at(dst, addr, dst_pte, entry);
  305. }
  306. spin_unlock(&src->page_table_lock);
  307. spin_unlock(&dst->page_table_lock);
  308. }
  309. return 0;
  310. nomem:
  311. return -ENOMEM;
  312. }
  313. void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
  314. unsigned long end)
  315. {
  316. struct mm_struct *mm = vma->vm_mm;
  317. unsigned long address;
  318. pte_t *ptep;
  319. pte_t pte;
  320. struct page *page;
  321. WARN_ON(!is_vm_hugetlb_page(vma));
  322. BUG_ON(start & ~HPAGE_MASK);
  323. BUG_ON(end & ~HPAGE_MASK);
  324. spin_lock(&mm->page_table_lock);
  325. /* Update high watermark before we lower rss */
  326. update_hiwater_rss(mm);
  327. for (address = start; address < end; address += HPAGE_SIZE) {
  328. ptep = huge_pte_offset(mm, address);
  329. if (!ptep)
  330. continue;
  331. pte = huge_ptep_get_and_clear(mm, address, ptep);
  332. if (pte_none(pte))
  333. continue;
  334. page = pte_page(pte);
  335. put_page(page);
  336. add_mm_counter(mm, file_rss, (int) -(HPAGE_SIZE / PAGE_SIZE));
  337. }
  338. spin_unlock(&mm->page_table_lock);
  339. flush_tlb_range(vma, start, end);
  340. }
  341. static int hugetlb_cow(struct mm_struct *mm, struct vm_area_struct *vma,
  342. unsigned long address, pte_t *ptep, pte_t pte)
  343. {
  344. struct page *old_page, *new_page;
  345. int avoidcopy;
  346. old_page = pte_page(pte);
  347. /* If no-one else is actually using this page, avoid the copy
  348. * and just make the page writable */
  349. avoidcopy = (page_count(old_page) == 1);
  350. if (avoidcopy) {
  351. set_huge_ptep_writable(vma, address, ptep);
  352. return VM_FAULT_MINOR;
  353. }
  354. page_cache_get(old_page);
  355. new_page = alloc_huge_page(vma, address);
  356. if (!new_page) {
  357. page_cache_release(old_page);
  358. return VM_FAULT_OOM;
  359. }
  360. spin_unlock(&mm->page_table_lock);
  361. copy_huge_page(new_page, old_page, address);
  362. spin_lock(&mm->page_table_lock);
  363. ptep = huge_pte_offset(mm, address & HPAGE_MASK);
  364. if (likely(pte_same(*ptep, pte))) {
  365. /* Break COW */
  366. set_huge_pte_at(mm, address, ptep,
  367. make_huge_pte(vma, new_page, 1));
  368. /* Make the old page be freed below */
  369. new_page = old_page;
  370. }
  371. page_cache_release(new_page);
  372. page_cache_release(old_page);
  373. return VM_FAULT_MINOR;
  374. }
  375. int hugetlb_no_page(struct mm_struct *mm, struct vm_area_struct *vma,
  376. unsigned long address, pte_t *ptep, int write_access)
  377. {
  378. int ret = VM_FAULT_SIGBUS;
  379. unsigned long idx;
  380. unsigned long size;
  381. struct page *page;
  382. struct address_space *mapping;
  383. pte_t new_pte;
  384. mapping = vma->vm_file->f_mapping;
  385. idx = ((address - vma->vm_start) >> HPAGE_SHIFT)
  386. + (vma->vm_pgoff >> (HPAGE_SHIFT - PAGE_SHIFT));
  387. /*
  388. * Use page lock to guard against racing truncation
  389. * before we get page_table_lock.
  390. */
  391. retry:
  392. page = find_lock_page(mapping, idx);
  393. if (!page) {
  394. if (hugetlb_get_quota(mapping))
  395. goto out;
  396. page = alloc_huge_page(vma, address);
  397. if (!page) {
  398. hugetlb_put_quota(mapping);
  399. ret = VM_FAULT_OOM;
  400. goto out;
  401. }
  402. clear_huge_page(page, address);
  403. if (vma->vm_flags & VM_SHARED) {
  404. int err;
  405. err = add_to_page_cache(page, mapping, idx, GFP_KERNEL);
  406. if (err) {
  407. put_page(page);
  408. hugetlb_put_quota(mapping);
  409. if (err == -EEXIST)
  410. goto retry;
  411. goto out;
  412. }
  413. } else
  414. lock_page(page);
  415. }
  416. spin_lock(&mm->page_table_lock);
  417. size = i_size_read(mapping->host) >> HPAGE_SHIFT;
  418. if (idx >= size)
  419. goto backout;
  420. ret = VM_FAULT_MINOR;
  421. if (!pte_none(*ptep))
  422. goto backout;
  423. add_mm_counter(mm, file_rss, HPAGE_SIZE / PAGE_SIZE);
  424. new_pte = make_huge_pte(vma, page, ((vma->vm_flags & VM_WRITE)
  425. && (vma->vm_flags & VM_SHARED)));
  426. set_huge_pte_at(mm, address, ptep, new_pte);
  427. if (write_access && !(vma->vm_flags & VM_SHARED)) {
  428. /* Optimization, do the COW without a second fault */
  429. ret = hugetlb_cow(mm, vma, address, ptep, new_pte);
  430. }
  431. spin_unlock(&mm->page_table_lock);
  432. unlock_page(page);
  433. out:
  434. return ret;
  435. backout:
  436. spin_unlock(&mm->page_table_lock);
  437. hugetlb_put_quota(mapping);
  438. unlock_page(page);
  439. put_page(page);
  440. goto out;
  441. }
  442. int hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,
  443. unsigned long address, int write_access)
  444. {
  445. pte_t *ptep;
  446. pte_t entry;
  447. int ret;
  448. static DEFINE_MUTEX(hugetlb_instantiation_mutex);
  449. ptep = huge_pte_alloc(mm, address);
  450. if (!ptep)
  451. return VM_FAULT_OOM;
  452. /*
  453. * Serialize hugepage allocation and instantiation, so that we don't
  454. * get spurious allocation failures if two CPUs race to instantiate
  455. * the same page in the page cache.
  456. */
  457. mutex_lock(&hugetlb_instantiation_mutex);
  458. entry = *ptep;
  459. if (pte_none(entry)) {
  460. ret = hugetlb_no_page(mm, vma, address, ptep, write_access);
  461. mutex_unlock(&hugetlb_instantiation_mutex);
  462. return ret;
  463. }
  464. ret = VM_FAULT_MINOR;
  465. spin_lock(&mm->page_table_lock);
  466. /* Check for a racing update before calling hugetlb_cow */
  467. if (likely(pte_same(entry, *ptep)))
  468. if (write_access && !pte_write(entry))
  469. ret = hugetlb_cow(mm, vma, address, ptep, entry);
  470. spin_unlock(&mm->page_table_lock);
  471. mutex_unlock(&hugetlb_instantiation_mutex);
  472. return ret;
  473. }
  474. int follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma,
  475. struct page **pages, struct vm_area_struct **vmas,
  476. unsigned long *position, int *length, int i)
  477. {
  478. unsigned long pfn_offset;
  479. unsigned long vaddr = *position;
  480. int remainder = *length;
  481. spin_lock(&mm->page_table_lock);
  482. while (vaddr < vma->vm_end && remainder) {
  483. pte_t *pte;
  484. struct page *page;
  485. /*
  486. * Some archs (sparc64, sh*) have multiple pte_ts to
  487. * each hugepage. We have to make * sure we get the
  488. * first, for the page indexing below to work.
  489. */
  490. pte = huge_pte_offset(mm, vaddr & HPAGE_MASK);
  491. if (!pte || pte_none(*pte)) {
  492. int ret;
  493. spin_unlock(&mm->page_table_lock);
  494. ret = hugetlb_fault(mm, vma, vaddr, 0);
  495. spin_lock(&mm->page_table_lock);
  496. if (ret == VM_FAULT_MINOR)
  497. continue;
  498. remainder = 0;
  499. if (!i)
  500. i = -EFAULT;
  501. break;
  502. }
  503. pfn_offset = (vaddr & ~HPAGE_MASK) >> PAGE_SHIFT;
  504. page = pte_page(*pte);
  505. same_page:
  506. if (pages) {
  507. get_page(page);
  508. pages[i] = page + pfn_offset;
  509. }
  510. if (vmas)
  511. vmas[i] = vma;
  512. vaddr += PAGE_SIZE;
  513. ++pfn_offset;
  514. --remainder;
  515. ++i;
  516. if (vaddr < vma->vm_end && remainder &&
  517. pfn_offset < HPAGE_SIZE/PAGE_SIZE) {
  518. /*
  519. * We use pfn_offset to avoid touching the pageframes
  520. * of this compound page.
  521. */
  522. goto same_page;
  523. }
  524. }
  525. spin_unlock(&mm->page_table_lock);
  526. *length = remainder;
  527. *position = vaddr;
  528. return i;
  529. }
  530. void hugetlb_change_protection(struct vm_area_struct *vma,
  531. unsigned long address, unsigned long end, pgprot_t newprot)
  532. {
  533. struct mm_struct *mm = vma->vm_mm;
  534. unsigned long start = address;
  535. pte_t *ptep;
  536. pte_t pte;
  537. BUG_ON(address >= end);
  538. flush_cache_range(vma, address, end);
  539. spin_lock(&mm->page_table_lock);
  540. for (; address < end; address += HPAGE_SIZE) {
  541. ptep = huge_pte_offset(mm, address);
  542. if (!ptep)
  543. continue;
  544. if (!pte_none(*ptep)) {
  545. pte = huge_ptep_get_and_clear(mm, address, ptep);
  546. pte = pte_mkhuge(pte_modify(pte, newprot));
  547. set_huge_pte_at(mm, address, ptep, pte);
  548. lazy_mmu_prot_update(pte);
  549. }
  550. }
  551. spin_unlock(&mm->page_table_lock);
  552. flush_tlb_range(vma, start, end);
  553. }
  554. struct file_region {
  555. struct list_head link;
  556. long from;
  557. long to;
  558. };
  559. static long region_add(struct list_head *head, long f, long t)
  560. {
  561. struct file_region *rg, *nrg, *trg;
  562. /* Locate the region we are either in or before. */
  563. list_for_each_entry(rg, head, link)
  564. if (f <= rg->to)
  565. break;
  566. /* Round our left edge to the current segment if it encloses us. */
  567. if (f > rg->from)
  568. f = rg->from;
  569. /* Check for and consume any regions we now overlap with. */
  570. nrg = rg;
  571. list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
  572. if (&rg->link == head)
  573. break;
  574. if (rg->from > t)
  575. break;
  576. /* If this area reaches higher then extend our area to
  577. * include it completely. If this is not the first area
  578. * which we intend to reuse, free it. */
  579. if (rg->to > t)
  580. t = rg->to;
  581. if (rg != nrg) {
  582. list_del(&rg->link);
  583. kfree(rg);
  584. }
  585. }
  586. nrg->from = f;
  587. nrg->to = t;
  588. return 0;
  589. }
  590. static long region_chg(struct list_head *head, long f, long t)
  591. {
  592. struct file_region *rg, *nrg;
  593. long chg = 0;
  594. /* Locate the region we are before or in. */
  595. list_for_each_entry(rg, head, link)
  596. if (f <= rg->to)
  597. break;
  598. /* If we are below the current region then a new region is required.
  599. * Subtle, allocate a new region at the position but make it zero
  600. * size such that we can guarentee to record the reservation. */
  601. if (&rg->link == head || t < rg->from) {
  602. nrg = kmalloc(sizeof(*nrg), GFP_KERNEL);
  603. if (nrg == 0)
  604. return -ENOMEM;
  605. nrg->from = f;
  606. nrg->to = f;
  607. INIT_LIST_HEAD(&nrg->link);
  608. list_add(&nrg->link, rg->link.prev);
  609. return t - f;
  610. }
  611. /* Round our left edge to the current segment if it encloses us. */
  612. if (f > rg->from)
  613. f = rg->from;
  614. chg = t - f;
  615. /* Check for and consume any regions we now overlap with. */
  616. list_for_each_entry(rg, rg->link.prev, link) {
  617. if (&rg->link == head)
  618. break;
  619. if (rg->from > t)
  620. return chg;
  621. /* We overlap with this area, if it extends futher than
  622. * us then we must extend ourselves. Account for its
  623. * existing reservation. */
  624. if (rg->to > t) {
  625. chg += rg->to - t;
  626. t = rg->to;
  627. }
  628. chg -= rg->to - rg->from;
  629. }
  630. return chg;
  631. }
  632. static long region_truncate(struct list_head *head, long end)
  633. {
  634. struct file_region *rg, *trg;
  635. long chg = 0;
  636. /* Locate the region we are either in or before. */
  637. list_for_each_entry(rg, head, link)
  638. if (end <= rg->to)
  639. break;
  640. if (&rg->link == head)
  641. return 0;
  642. /* If we are in the middle of a region then adjust it. */
  643. if (end > rg->from) {
  644. chg = rg->to - end;
  645. rg->to = end;
  646. rg = list_entry(rg->link.next, typeof(*rg), link);
  647. }
  648. /* Drop any remaining regions. */
  649. list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
  650. if (&rg->link == head)
  651. break;
  652. chg += rg->to - rg->from;
  653. list_del(&rg->link);
  654. kfree(rg);
  655. }
  656. return chg;
  657. }
  658. static int hugetlb_acct_memory(long delta)
  659. {
  660. int ret = -ENOMEM;
  661. spin_lock(&hugetlb_lock);
  662. if ((delta + resv_huge_pages) <= free_huge_pages) {
  663. resv_huge_pages += delta;
  664. ret = 0;
  665. }
  666. spin_unlock(&hugetlb_lock);
  667. return ret;
  668. }
  669. int hugetlb_reserve_pages(struct inode *inode, long from, long to)
  670. {
  671. long ret, chg;
  672. chg = region_chg(&inode->i_mapping->private_list, from, to);
  673. if (chg < 0)
  674. return chg;
  675. ret = hugetlb_acct_memory(chg);
  676. if (ret < 0)
  677. return ret;
  678. region_add(&inode->i_mapping->private_list, from, to);
  679. return 0;
  680. }
  681. void hugetlb_unreserve_pages(struct inode *inode, long offset, long freed)
  682. {
  683. long chg = region_truncate(&inode->i_mapping->private_list, offset);
  684. hugetlb_acct_memory(freed - chg);
  685. }