Skip to contentSkip to conversation

A few cleanup patches for hugetlb

PATCH Applied Miaohe Lin 1 participant 11 messages Sep 1, 2022

Subsystems (3)
lore
Share & tools
lore

Miaohe Lin

author
#0 lore raw

v2 cover A few cleanup patches for hugetlb

Hi everyone,
This series contains a few cleanup patches to use helper functions to simplify the codes, remove unneeded nid parameter and so on. More details can be found in the respective changelogs.
Thanks!

---
v2:
collect Reviewed-by tags per Muchun. Thanks!
tweak the commit log in 3/10
change to add a comment for SetHPageVmemmapOptimized() in 8/10 make hugetlb depends on SYSFS || SYSCTL in 10/10
---
Miaohe Lin (10):
hugetlb: make hugetlb_cma_check() static
hugetlb: Use helper macro SZ_1K
hugetlb: Use LIST_HEAD() to define a list head
hugetlb: Use sizeof() to get the array size
hugetlb: Use helper {huge_pte|pmd}_lock()
hugetlb: pass NULL to kobj_to_hstate() if nid is unused
hugetlb: kill hugetlbfs_pagecache_page()
hugetlb: add comment for subtle SetHPageVmemmapOptimized()
hugetlb: remove meaningless BUG_ON(huge_pte_none())
hugetlb: make hugetlb depends on SYSFS or SYSCTL

 fs/Kconfig              |  1 +
 include/linux/hugetlb.h |  4 ---
 mm/hugetlb.c            | 55 +++++++++++++++++------------------------
 3 files changed, 24 insertions(+), 36 deletions(-)
Signature · 1 line
2.23.0
author
#1 lore raw

PATCH v2 1/10 hugetlb: make hugetlb_cma_check() static

Make hugetlb_cma_check() static as it's only used inside mm/hugetlb.c.

---
 include/linux/hugetlb.h |  4 ----
 mm/hugetlb.c            | 10 +++++++++-
 2 files changed, 9 insertions(+), 5 deletions(-)
include/linux/hugetlb.h +0 -4
index 852f911d676e..c2abbd7950de 100644
@@ -1155,14 +1155,10 @@ static inline spinlock_t *huge_pte_lock(struct hstate *h,
 
 #if defined(CONFIG_HUGETLB_PAGE) && defined(CONFIG_CMA)
 extern void __init hugetlb_cma_reserve(int order);
-extern void __init hugetlb_cma_check(void);
 #else
 static inline __init void hugetlb_cma_reserve(int order)
 {
 }
-static inline __init void hugetlb_cma_check(void)
-{
-}
 #endif
 
 bool want_pmd_share(struct vm_area_struct *vma, unsigned long addr);
mm/hugetlb.c +9 -1
index d0617d64d718..2f5008c67624 100644
@@ -4058,6 +4058,14 @@ static void hugetlb_register_all_nodes(void) { }
 
 #endif
 
+#ifdef CONFIG_CMA
+static void __init hugetlb_cma_check(void);
+#else
+static inline __init void hugetlb_cma_check(void)
+{
+}
+#endif
+
 static int __init hugetlb_init(void)
 {
 	int i;
@@ -7546,7 +7554,7 @@ void __init hugetlb_cma_reserve(int order)
 		hugetlb_cma_size = 0;
 }
 
-void __init hugetlb_cma_check(void)
+static void __init hugetlb_cma_check(void)
 {
 	if (!hugetlb_cma_size || cma_reserve_called)
 		return;
Signature · 1 line
2.23.0
author
#2 lore raw

PATCH v2 2/10 hugetlb: Use helper macro SZ_1K

In reply to Miaohe Lin

Use helper macro SZ_1K to do the size conversion to make code more consistent in this file. Minor readability improvement.

---
 mm/hugetlb.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/hugetlb.c +1 -1
index 2f5008c67624..0d63a7fda1d9 100644
@@ -4165,7 +4165,7 @@ void __init hugetlb_add_hstate(unsigned int order)
 	h->next_nid_to_alloc = first_memory_node;
 	h->next_nid_to_free = first_memory_node;
 	snprintf(h->name, HSTATE_NAME_LEN, "hugepages-%lukB",
-					huge_page_size(h)/1024);
+					huge_page_size(h)/SZ_1K);
 
 	parsed_hstate = h;
 }
Signature · 1 line
2.23.0
author
#3 lore raw

PATCH v2 3/10 hugetlb: Use LIST_HEAD() to define a list head

In reply to Miaohe Lin

Use LIST_HEAD() directly to define a list head to simplify the code. No functional change intended.

---
 mm/hugetlb.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
mm/hugetlb.c +2 -5
index 0d63a7fda1d9..94cb81e9b4df 100644
@@ -459,14 +459,12 @@ static int allocate_file_region_entries(struct resv_map *resv,
 					int regions_needed)
 	__must_hold(&resv->lock)
 {
-	struct list_head allocated_regions;
+	LIST_HEAD(allocated_regions);
 	int to_allocate = 0, i = 0;
 	struct file_region *trg = NULL, *rg = NULL;
 
 	VM_BUG_ON(regions_needed < 0);
 
-	INIT_LIST_HEAD(&allocated_regions);
-
 	/*
 	 * Check for sufficient descriptors in the cache to accommodate
 	 * the number of in progress add operations plus regions_needed.
@@ -2352,7 +2350,7 @@ struct page *alloc_huge_page_vma(struct hstate *h, struct vm_area_struct *vma,
 static int gather_surplus_pages(struct hstate *h, long delta)
 	__must_hold(&hugetlb_lock)
 {
-	struct list_head surplus_list;
+	LIST_HEAD(surplus_list);
 	struct page *page, *tmp;
 	int ret;
 	long i;
@@ -2367,7 +2365,6 @@ static int gather_surplus_pages(struct hstate *h, long delta)
 	}
 
 	allocated = 0;
-	INIT_LIST_HEAD(&surplus_list);
 
 	ret = -ENOMEM;
 retry:
Signature · 1 line
2.23.0
author
#4 lore raw

PATCH v2 4/10 hugetlb: Use sizeof() to get the array size

In reply to Miaohe Lin

It's better to use sizeof() to get the array size instead of manual calculation. Minor readability improvement.

---
 mm/hugetlb.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
mm/hugetlb.c +2 -2
index 94cb81e9b4df..c29b444a5515 100644
@@ -4177,11 +4177,11 @@ static void __init hugepages_clear_pages_in_node(void)
 	if (!hugetlb_max_hstate) {
 		default_hstate_max_huge_pages = 0;
 		memset(default_hugepages_in_node, 0,
-			MAX_NUMNODES * sizeof(unsigned int));
+			sizeof(default_hugepages_in_node));
 	} else {
 		parsed_hstate->max_huge_pages = 0;
 		memset(parsed_hstate->max_huge_pages_node, 0,
-			MAX_NUMNODES * sizeof(unsigned int));
+			sizeof(parsed_hstate->max_huge_pages_node));
 	}
 }
 
Signature · 1 line
2.23.0
author
#5 lore raw

PATCH v2 5/10 hugetlb: Use helper {huge_pte|pmd}_lock()

In reply to Miaohe Lin

Use helper huge_pte_lock and pmd_lock to simplify the code. No functional change intended.

---
 mm/hugetlb.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
mm/hugetlb.c +2 -4
index c29b444a5515..d35381de90c3 100644
@@ -6094,8 +6094,7 @@ int hugetlb_mcopy_atomic_pte(struct mm_struct *dst_mm,
 		page_in_pagecache = true;
 	}
 
-	ptl = huge_pte_lockptr(h, dst_mm, dst_pte);
-	spin_lock(ptl);
+	ptl = huge_pte_lock(h, dst_mm, dst_pte);
 
 	/*
 	 * We allow to overwrite a pte marker: consider when both MISSING|WP
@@ -7176,8 +7175,7 @@ follow_huge_pmd(struct mm_struct *mm, unsigned long address,
 		return NULL;
 
 retry:
-	ptl = pmd_lockptr(mm, pmd);
-	spin_lock(ptl);
+	ptl = pmd_lock(mm, pmd);
 	/*
 	 * make sure that the address range covered by this pmd is not
 	 * unmapped from other threads.
Signature · 1 line
2.23.0
author
#6 lore raw

PATCH v2 6/10 hugetlb: pass NULL to kobj_to_hstate() if nid is unused

In reply to Miaohe Lin

We can pass NULL to kobj_to_hstate() directly when nid is unused to simplify the code. No functional change intended.

---
 mm/hugetlb.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
mm/hugetlb.c +2 -4
index d35381de90c3..bb65b7fdca25 100644
@@ -3779,8 +3779,7 @@ HSTATE_ATTR_WO(demote);
 static ssize_t demote_size_show(struct kobject *kobj,
 					struct kobj_attribute *attr, char *buf)
 {
-	int nid;
-	struct hstate *h = kobj_to_hstate(kobj, &nid);
+	struct hstate *h = kobj_to_hstate(kobj, NULL);
 	unsigned long demote_size = (PAGE_SIZE << h->demote_order) / SZ_1K;
 
 	return sysfs_emit(buf, "%lukB\n", demote_size);
@@ -3793,7 +3792,6 @@ static ssize_t demote_size_store(struct kobject *kobj,
 	struct hstate *h, *demote_hstate;
 	unsigned long demote_size;
 	unsigned int demote_order;
-	int nid;
 
 	demote_size = (unsigned long)memparse(buf, NULL);
 
@@ -3805,7 +3803,7 @@ static ssize_t demote_size_store(struct kobject *kobj,
 		return -EINVAL;
 
 	/* demote order must be smaller than hstate order */
-	h = kobj_to_hstate(kobj, &nid);
+	h = kobj_to_hstate(kobj, NULL);
 	if (demote_order >= h->order)
 		return -EINVAL;
 
Signature · 1 line
2.23.0
author
#7 lore raw

PATCH v2 7/10 hugetlb: kill hugetlbfs_pagecache_page()

In reply to Miaohe Lin

Fold hugetlbfs_pagecache_page() into its sole caller to remove some duplicated code. No functional change intended.

---
 mm/hugetlb.c | 15 +--------------
 1 file changed, 1 insertion(+), 14 deletions(-)
mm/hugetlb.c +1 -14
index bb65b7fdca25..26bcc85715eb 100644
@@ -5467,19 +5467,6 @@ static vm_fault_t hugetlb_wp(struct mm_struct *mm, struct vm_area_struct *vma,
 	return ret;
 }
 
-/* Return the pagecache page at a given address within a VMA */
-static struct page *hugetlbfs_pagecache_page(struct hstate *h,
-			struct vm_area_struct *vma, unsigned long address)
-{
-	struct address_space *mapping;
-	pgoff_t idx;
-
-	mapping = vma->vm_file->f_mapping;
-	idx = vma_hugecache_offset(h, vma, address);
-
-	return find_lock_page(mapping, idx);
-}
-
 /*
  * Return whether there is a pagecache page to back given address within VMA.
  * Caller follow_hugetlb_page() holds page_table_lock so we cannot lock_page.
@@ -5885,7 +5872,7 @@ vm_fault_t hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,
 		/* Just decrements count, does not deallocate */
 		vma_end_reservation(h, vma, haddr);
 
-		pagecache_page = hugetlbfs_pagecache_page(h, vma, haddr);
+		pagecache_page = find_lock_page(mapping, idx);
 	}
 
 	ptl = huge_pte_lock(h, mm, ptep);
Signature · 1 line
2.23.0
author
#8 lore raw

PATCH v2 8/10 hugetlb: add comment for subtle SetHPageVmemmapOptimized()

In reply to Miaohe Lin

The SetHPageVmemmapOptimized() called here seems unnecessary as it's assumed to be set when calling this function. But it's indeed cleared by above set_page_private(page, 0). Add a comment to avoid possible future confusion.

---
 mm/hugetlb.c | 4 ++++
 1 file changed, 4 insertions(+)
mm/hugetlb.c +4 -0
index 26bcc85715eb..2ba45addcdeb 100644
@@ -1520,6 +1520,10 @@ static void add_hugetlb_page(struct hstate *h, struct page *page,
 
 	set_compound_page_dtor(page, HUGETLB_PAGE_DTOR);
 	set_page_private(page, 0);
+	/*
+	 * We have to set HPageVmemmapOptimized again as above
+	 * set_page_private(page, 0) cleared it.
+	 */
 	SetHPageVmemmapOptimized(page);
 
 	/*
Signature · 1 line
2.23.0
author
#9 lore raw

PATCH v2 9/10 hugetlb: remove meaningless BUG_ON(huge_pte_none())

In reply to Miaohe Lin

When code reaches here, invalid page would have been accessed if huge pte is none. So this BUG_ON(huge_pte_none()) is meaningless. Remove it.

---
 mm/hugetlb.c | 1 -
 1 file changed, 1 deletion(-)
mm/hugetlb.c +0 -1
index 2ba45addcdeb..9178ab521c74 100644
@@ -5379,7 +5379,6 @@ static vm_fault_t hugetlb_wp(struct mm_struct *mm, struct vm_area_struct *vma,
 			u32 hash;
 
 			put_page(old_page);
-			BUG_ON(huge_pte_none(pte));
 			/*
 			 * Drop hugetlb_fault_mutex and vma_lock before
 			 * unmapping.  unmapping needs to hold vma_lock
Signature · 1 line
2.23.0
author
#10 lore raw

PATCH v2 10/10 hugetlb: make hugetlb depends on SYSFS or SYSCTL

In reply to Miaohe Lin

If CONFIG_SYSFS and CONFIG_SYSCTL are both undefined, hugetlb doesn't work now as there's no way to set max huge pages. Make sure at least one of the above configs is defined to make hugetlb works as expected.

---
 fs/Kconfig | 1 +
 1 file changed, 1 insertion(+)
fs/Kconfig +1 -0
index a547307c1ae8..2685a4d0d353 100644
@@ -235,6 +235,7 @@ config ARCH_SUPPORTS_HUGETLBFS
 config HUGETLBFS
 	bool "HugeTLB file system support"
 	depends on X86 || IA64 || SPARC64 || ARCH_SUPPORTS_HUGETLBFS || BROKEN
+	depends on (SYSFS || SYSCTL)
 	help
 	  hugetlbfs is a filesystem backing for HugeTLB pages, based on
 	  ramfs. For architectures that support it, say Y here and read
Signature · 1 line
2.23.0