History

mm: hugetlb: move mpol interpretation out of alloc_buddy_hugetlb_folio_with_mpol()

Move memory policy interpretation out of alloc_buddy_hugetlb_folio_with_mpol() and into alloc_hugetlb_folio() to separate reading and interpretation of memory policy from actual allocation. This will later allow memory policy to be interpreted outside of the process of allocating a hugetlb folio entirely. This opens doors for other callers of the HugeTLB folio allocation function, such as guest_memfd, where memory may not always be mapped and hence may not have an associated vma. Introduce struct mempolicy_interpreted to hold all the components of an interpreted memory policy. Rename alloc_buddy_hugetlb_folio_with_mpol() to alloc_buddy_hugetlb_folio() since the function no longer interprets memory policy. No functional change intended.

Ackerley Tng committed 4 months ago

Commit 66a4e9e1 parent 88819612 files · +37 −19Browse files

Unified · Split

2 files
  1. include/uapi/linux/mempolicy.h +1 −1
  2. mm/hugetlb.c +36 −18
include/uapi/linux/mempolicy.h+1−1
@@ -16,7 +16,7 @@ · 15 unmodified lines
1616 */
1717
1818 /* Policies */
19−enum {
19+enum mempolicy_mode {
2020 MPOL_DEFAULT,
2121 MPOL_PREFERRED,
2222 MPOL_BIND,
mm/hugetlb.c+36−18
@@ -1316,6 +1316,12 @@ static unsigned long available_huge_pages(struct hstate *h) · 1315 unmodified lines
13161316 return h->free_huge_pages - h->resv_huge_pages;
13171317 }
13181318
1319+struct mempolicy_interpreted {
1320+ int nid;
1321+ nodemask_t *nodemask;
1322+ enum mempolicy_mode mode;
1323+};
1324+
13191325 static struct folio *dequeue_hugetlb_folio_vma(struct hstate *h,
13201326 struct vm_area_struct *vma,
13211327 unsigned long address)
@@ -2137,32 +2143,28 @@ static struct folio *alloc_migrate_hugetlb_folio(struct hstate *h, gfp_t gfp_mas · 815 unmodified lines
21372143 return folio;
21382144 }
21392145
2140−/*
2141− * Use the VMA's mpolicy to allocate a huge page from the buddy.
2142− */
21432146 static
2144−struct folio *alloc_buddy_hugetlb_folio_with_mpol(struct hstate *h,
2145− struct vm_area_struct *vma, unsigned long addr)
2147+struct folio *alloc_buddy_hugetlb_folio(struct hstate *h,
2148+ gfp_t gfp_mask, struct mempolicy_interpreted *mpoli)
21462149 {
21472150 struct folio *folio = NULL;
2148− struct mempolicy *mpol;
2149− gfp_t gfp_mask = htlb_alloc_mask(h);
2150− int nid;
2151− nodemask_t *nodemask;
2151+ nodemask_t *nodemask = mpoli->nodemask;
21522152
2153− nid = huge_node(vma, addr, gfp_mask, &mpol, &nodemask);
2154− if (mpol_is_preferred_many(mpol)) {
2153+ if (mpoli->mode == MPOL_PREFERRED_MANY) {
21552154 gfp_t gfp = gfp_mask & ~(__GFP_DIRECT_RECLAIM | __GFP_NOFAIL);
21562155
2157− folio = alloc_surplus_hugetlb_folio(h, gfp, nid, nodemask);
2156+ folio = alloc_surplus_hugetlb_folio(h, gfp, mpoli->nid,
2157+ nodemask);
21582158
21592159 /* Fallback to all nodes if page==NULL */
21602160 nodemask = NULL;
21612161 }
21622162
2163− if (!folio)
2164− folio = alloc_surplus_hugetlb_folio(h, gfp_mask, nid, nodemask);
2165− mpol_cond_put(mpol);
2163+ if (!folio) {
2164+ folio = alloc_surplus_hugetlb_folio(h, gfp_mask, mpoli->nid,
2165+ nodemask);
2166+ }
2167+
21662168 return folio;
21672169 }
21682170
@@ -2852,7 +2854,7 @@ struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma, · 683 unmodified lines
28522854 int ret, idx;
28532855 struct hugetlb_cgroup *h_cg = NULL;
28542856 struct hugetlb_cgroup *h_cg_rsvd = NULL;
2855− gfp_t gfp = htlb_alloc_mask(h) | __GFP_RETRY_MAYFAIL;
2857+ gfp_t gfp = htlb_alloc_mask(h);
28562858
28572859 idx = hstate_index(h);
28582860
@@ -2924,8 +2926,24 @@ struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma, · 65 unmodified lines
29242926 folio = dequeue_hugetlb_folio_vma(h, vma, addr);
29252927
29262928 if (!folio) {
2929+ struct mempolicy_interpreted mpoli;
2930+ struct mempolicy *mpol;
2931+ nodemask_t *nodemask;
2932+ int nid;
2933+
29272934 spin_unlock_irq(&hugetlb_lock);
2928− folio = alloc_buddy_hugetlb_folio_with_mpol(h, vma, addr);
2935+ nid = huge_node(vma, addr, gfp, &mpol, &nodemask);
2936+ mpoli = (struct mempolicy_interpreted){
2937+ .nid = nid,
2938+#ifdef CONFIG_NUMA
2939+ .mode = mpol ? mpol->mode : MPOL_DEFAULT,
2940+#else
2941+ .mode = MPOL_DEFAULT,
2942+#endif
2943+ .nodemask = nodemask,
2944+ };
2945+ folio = alloc_buddy_hugetlb_folio(h, gfp, &mpoli);
2946+ mpol_cond_put(mpol);
29292947 if (!folio)
29302948 goto out_uncharge_cgroup;
29312949 spin_lock_irq(&hugetlb_lock);
@@ -2981,7 +2999,7 @@ struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma, · 49 unmodified lines
29812999 }
29823000 }
29833001
2984− ret = mem_cgroup_charge_hugetlb(folio, gfp);
3002+ ret = mem_cgroup_charge_hugetlb(folio, gfp | __GFP_RETRY_MAYFAIL);
29853003 /*
29863004 * Unconditionally increment NR_HUGETLB here. If it turns out that
29873005 * mem_cgroup_charge_hugetlb failed, then immediately free the page and

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t