Skip to contentSkip to conversation

mm: hugetlb_vmemmap: use bulk allocator in alloc_vmemmap_page_list()

PATCH Superseded Kefeng Wang 2 participants 5 messages Sep 5, 2023

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm: hugetlb_vmemmap: use bulk allocator in alloc_vmemmap_page_list()

It is needed 4095 pages(1G) or 7 pages(2M) to be allocated once in alloc_vmemmap_page_list(), so let's add a bulk allocator varietas alloc_pages_bulk_list_node() and switch alloc_vmemmap_page_list() to use it to accelerate page allocation.

Simple test on arm64's qemu with 1G Hugetlb, 870,842ns vs 3,845,252ns despite the fluctuations, it is still a nice improvement.

---
 include/linux/gfp.h  | 9 +++++++++
 mm/hugetlb_vmemmap.c | 7 ++++++-
 2 files changed, 15 insertions(+), 1 deletion(-)
include/linux/gfp.h +9 -0
index 665f06675c83..d6e82f15b61f 100644
@@ -195,6 +195,15 @@ alloc_pages_bulk_list(gfp_t gfp, unsigned long nr_pages, struct list_head *list)
 	return __alloc_pages_bulk(gfp, numa_mem_id(), NULL, nr_pages, list, NULL);
 }
 
+static inline unsigned long
+alloc_pages_bulk_list_node(gfp_t gfp, int nid, unsigned long nr_pages, struct list_head *list)
+{
+	if (nid == NUMA_NO_NODE)
+		nid = numa_mem_id();
+
+	return __alloc_pages_bulk(gfp, nid, NULL, nr_pages, list, NULL);
+}
+
 static inline unsigned long
 alloc_pages_bulk_array(gfp_t gfp, unsigned long nr_pages, struct page **page_array)
 {
mm/hugetlb_vmemmap.c +6 -1
index 4b9734777f69..699c4fea6b9f 100644
@@ -384,8 +384,13 @@ static int alloc_vmemmap_page_list(unsigned long start, unsigned long end,
 	unsigned long nr_pages = (end - start) >> PAGE_SHIFT;
 	int nid = page_to_nid((struct page *)start);
 	struct page *page, *next;
+	unsigned long nr_alloced;
 
-	while (nr_pages--) {
+	nr_alloced = alloc_pages_bulk_list_node(gfp_mask, nid, nr_pages, list);
+	if (!nr_alloced)
+		return -ENOMEM;
+
+	while (nr_alloced < nr_pages) {
 		page = alloc_pages_node(nid, gfp_mask, 0);
 		if (!page)
 			goto out;
Signature · 1 line
2.27.0
#1 lore raw
Quoted 40 lines · Kefeng Wang

It is needed 4095 pages(1G) or 7 pages(2M) to be allocated once in alloc_vmemmap_page_list(), so let's add a bulk allocator varietas alloc_pages_bulk_list_node() and switch alloc_vmemmap_page_list() to use it to accelerate page allocation.

Simple test on arm64's qemu with 1G Hugetlb, 870,842ns vs 3,845,252ns despite the fluctuations, it is still a nice improvement.

---
  include/linux/gfp.h  | 9 +++++++++
  mm/hugetlb_vmemmap.c | 7 ++++++-
  2 files changed, 15 insertions(+), 1 deletion(-)
include/linux/gfp.h +9 -0
index 665f06675c83..d6e82f15b61f 100644
@@ -195,6 +195,15 @@ alloc_pages_bulk_list(gfp_t gfp, unsigned long nr_pages, struct list_head *list)
  	return __alloc_pages_bulk(gfp, numa_mem_id(), NULL, nr_pages, list, NULL);
  }
 
+static inline unsigned long
+alloc_pages_bulk_list_node(gfp_t gfp, int nid, unsigned long nr_pages, struct list_head *list)
+{
+	if (nid == NUMA_NO_NODE)
+		nid = numa_mem_id();
+
+	return __alloc_pages_bulk(gfp, nid, NULL, nr_pages, list, NULL);
+}
+
  static inline unsigned long
  alloc_pages_bulk_array(gfp_t gfp, unsigned long nr_pages, struct page **page_array)
  {
mm/hugetlb_vmemmap.c +0 -0
index 4b9734777f69..699c4fea6b9f 100644

mm/hugetlb_vmemmap.c static int alloc_vmemmap_page_list(unsigned long start, unsigned long end, · line 392

Quoted 10 lines
mm/hugetlb_vmemmap.c +5 -1
@@ -384,8 +384,13 @@ static int alloc_vmemmap_page_list(unsigned long start, unsigned long end,
  	unsigned long nr_pages = (end - start) >> PAGE_SHIFT;
  	int nid = page_to_nid((struct page *)start);
  	struct page *page, *next;
+	unsigned long nr_alloced;
 
-	while (nr_pages--) {
+	nr_alloced = alloc_pages_bulk_list_node(gfp_mask, nid, nr_pages, list);
+	if (!nr_alloced)
+		return -ENOMEM;
+

eh, forget to inc nr_allocated in the fallback patch, will resend

Quoted 4 lines
mm/hugetlb_vmemmap.c +1 -0
+	while (nr_alloced < nr_pages) {
  		page = alloc_pages_node(nid, gfp_mask, 0);
  		if (!page)
  			goto out;
maintainer
#2 lore raw
Quoted 43 lines · Kefeng Wang
Quoted 39 lines · Kefeng Wang

It is needed 4095 pages(1G) or 7 pages(2M) to be allocated once in alloc_vmemmap_page_list(), so let's add a bulk allocator varietas alloc_pages_bulk_list_node() and switch alloc_vmemmap_page_list() to use it to accelerate page allocation.
Simple test on arm64's qemu with 1G Hugetlb, 870,842ns vs 3,845,252ns despite the fluctuations, it is still a nice improvement.

---
 include/linux/gfp.h  | 9 +++++++++
 mm/hugetlb_vmemmap.c | 7 ++++++-
 2 files changed, 15 insertions(+), 1 deletion(-)
include/linux/gfp.h +8 -0
index 665f06675c83..d6e82f15b61f 100644
@@ -195,6 +195,15 @@ alloc_pages_bulk_list(gfp_t gfp, unsigned long nr_pages, struct list_head *list)
  return __alloc_pages_bulk(gfp, numa_mem_id(), NULL, nr_pages, list, NULL);
 }
 +static inline unsigned long
+alloc_pages_bulk_list_node(gfp_t gfp, int nid, unsigned long nr_pages, struct list_head *list)
+{
+ if (nid == NUMA_NO_NODE)
+ nid = numa_mem_id();
+
+ return __alloc_pages_bulk(gfp, nid, NULL, nr_pages, list, NULL);
+}
+
 static inline unsigned long
 alloc_pages_bulk_array(gfp_t gfp, unsigned long nr_pages, struct page **page_array)
 {
mm/hugetlb_vmemmap.c +0 -0
index 4b9734777f69..699c4fea6b9f 100644
@@ -384,8 +384,13 @@ static int alloc_vmemmap_page_list(unsigned long start, unsigned long end,
  unsigned long nr_pages = (end - start) >> PAGE_SHIFT;
  int nid = page_to_nid((struct page *)start);
  struct page *page, *next;
Quoted 8 lines

mm/hugetlb_vmemmap.c static int alloc_vmemmap_page_list(unsigned long start, unsigned long end, · line 392

Quoted 6 lines
mm/hugetlb_vmemmap.c +5 -0
+ unsigned long nr_alloced;
 - while (nr_pages--) {
+ nr_alloced = alloc_pages_bulk_list_node(gfp_mask, nid, nr_pages, list);
+ if (!nr_alloced)
+ return -ENOMEM;
+

eh, forget to inc nr_allocated in the fallback patch, will resend

Do not change the judgement, "nr_pages -= nr_alloced;" is enough and simple.

Quoted 4 lines
Quoted 4 lines
mm/hugetlb_vmemmap.c +1 -0
+ while (nr_alloced < nr_pages) {
  page = alloc_pages_node(nid, gfp_mask, 0);
  if (!page)
  goto out;
#3 lore raw
Quoted 52 lines · Muchun Song
Quoted 43 lines · Kefeng Wang
Quoted 39 lines · Kefeng Wang

It is needed 4095 pages(1G) or 7 pages(2M) to be allocated once in alloc_vmemmap_page_list(), so let's add a bulk allocator varietas alloc_pages_bulk_list_node() and switch alloc_vmemmap_page_list() to use it to accelerate page allocation.
Simple test on arm64's qemu with 1G Hugetlb, 870,842ns vs 3,845,252ns despite the fluctuations, it is still a nice improvement.

---
  include/linux/gfp.h  | 9 +++++++++
  mm/hugetlb_vmemmap.c | 7 ++++++-
  2 files changed, 15 insertions(+), 1 deletion(-)
include/linux/gfp.h +8 -0
index 665f06675c83..d6e82f15b61f 100644
@@ -195,6 +195,15 @@ alloc_pages_bulk_list(gfp_t gfp, unsigned long nr_pages, struct list_head *list)
   return __alloc_pages_bulk(gfp, numa_mem_id(), NULL, nr_pages, list, NULL);
  }
  +static inline unsigned long
+alloc_pages_bulk_list_node(gfp_t gfp, int nid, unsigned long nr_pages, struct list_head *list)
+{
+ if (nid == NUMA_NO_NODE)
+ nid = numa_mem_id();
+
+ return __alloc_pages_bulk(gfp, nid, NULL, nr_pages, list, NULL);
+}
+
  static inline unsigned long
  alloc_pages_bulk_array(gfp_t gfp, unsigned long nr_pages, struct page **page_array)
  {
mm/hugetlb_vmemmap.c +0 -0
index 4b9734777f69..699c4fea6b9f 100644
@@ -384,8 +384,13 @@ static int alloc_vmemmap_page_list(unsigned long start, unsigned long end,
   unsigned long nr_pages = (end - start) >> PAGE_SHIFT;
   int nid = page_to_nid((struct page *)start);
   struct page *page, *next;
Quoted 8 lines

mm/hugetlb_vmemmap.c static int alloc_vmemmap_page_list(unsigned long start, unsigned long end, · line 392

Quoted 6 lines
mm/hugetlb_vmemmap.c +5 -0
+ unsigned long nr_alloced;
  - while (nr_pages--) {
+ nr_alloced = alloc_pages_bulk_list_node(gfp_mask, nid, nr_pages, list);
+ if (!nr_alloced)
+ return -ENOMEM;
+

eh, forget to inc nr_allocated in the fallback patch, will resend

Quoted 2 lines

Do not change the judgement, "nr_pages -= nr_alloced;" is enough and simple.

sure, thanks.

Quoted 7 lines
Quoted 5 lines
Quoted 5 lines
mm/hugetlb_vmemmap.c +1 -0
+ while (nr_alloced < nr_pages) {
   page = alloc_pages_node(nid, gfp_mask, 0);
   if (!page)
   goto out;
#4 lore raw
In reply to Muchun Song
Quoted 52 lines · Muchun Song
Quoted 43 lines · Kefeng Wang
Quoted 39 lines · Kefeng Wang

It is needed 4095 pages(1G) or 7 pages(2M) to be allocated once in alloc_vmemmap_page_list(), so let's add a bulk allocator varietas alloc_pages_bulk_list_node() and switch alloc_vmemmap_page_list() to use it to accelerate page allocation.
Simple test on arm64's qemu with 1G Hugetlb, 870,842ns vs 3,845,252ns despite the fluctuations, it is still a nice improvement.

---
  include/linux/gfp.h  | 9 +++++++++
  mm/hugetlb_vmemmap.c | 7 ++++++-
  2 files changed, 15 insertions(+), 1 deletion(-)
include/linux/gfp.h +8 -0
index 665f06675c83..d6e82f15b61f 100644
@@ -195,6 +195,15 @@ alloc_pages_bulk_list(gfp_t gfp, unsigned long nr_pages, struct list_head *list)
   return __alloc_pages_bulk(gfp, numa_mem_id(), NULL, nr_pages, list, NULL);
  }
  +static inline unsigned long
+alloc_pages_bulk_list_node(gfp_t gfp, int nid, unsigned long nr_pages, struct list_head *list)
+{
+ if (nid == NUMA_NO_NODE)
+ nid = numa_mem_id();
+
+ return __alloc_pages_bulk(gfp, nid, NULL, nr_pages, list, NULL);
+}
+
  static inline unsigned long
  alloc_pages_bulk_array(gfp_t gfp, unsigned long nr_pages, struct page **page_array)
  {
mm/hugetlb_vmemmap.c +0 -0
index 4b9734777f69..699c4fea6b9f 100644
@@ -384,8 +384,13 @@ static int alloc_vmemmap_page_list(unsigned long start, unsigned long end,
   unsigned long nr_pages = (end - start) >> PAGE_SHIFT;
   int nid = page_to_nid((struct page *)start);
   struct page *page, *next;
Quoted 8 lines

mm/hugetlb_vmemmap.c static int alloc_vmemmap_page_list(unsigned long start, unsigned long end, · line 392

Quoted 6 lines
mm/hugetlb_vmemmap.c +5 -0
+ unsigned long nr_alloced;
  - while (nr_pages--) {
+ nr_alloced = alloc_pages_bulk_list_node(gfp_mask, nid, nr_pages, list);
+ if (!nr_alloced)
+ return -ENOMEM;
+

eh, forget to inc nr_allocated in the fallback patch, will resend

Quoted 2 lines

Do not change the judgement, "nr_pages -= nr_alloced;" is enough and simple.

nr_pages = 7, nr_alloced = 4, new nr_pages = 3, the fallback won't execute if nr_alloced not cleared, will add nr_allocated only alloc page successfully.

        while (nr_allocated < nr_pages) {
                page = alloc_pages_node(nid, gfp_mask, 0);
                if (!page)
                        goto out;
                list_add_tail(&page->lru, list);
+               nr_allocated++;
        }
Quoted 7 lines
Quoted 5 lines
Quoted 5 lines
mm/hugetlb_vmemmap.c +1 -0
+ while (nr_alloced < nr_pages) {
   page = alloc_pages_node(nid, gfp_mask, 0);
   if (!page)
   goto out;