Thread (52 messages) 52 messages, 6 authors, 2021-03-12

Re: [External] Re: [PATCH v18 9/9] mm: hugetlb: optimize the code with the help of the compiler

flat view

From: Muchun Song <hidden>
Date: 2021-03-11 07:35:19
Also in: linux-fsdevel, linux-mm, lkml

On Wed, Mar 10, 2021 at 11:41 PM Michal Hocko [off-list ref] wrote:
On Mon 08-03-21 18:28:07, Muchun Song wrote:
quoted
When the "struct page size" crosses page boundaries we cannot
make use of this feature. Let free_vmemmap_pages_per_hpage()
return zero if that is the case, most of the functions can be
optimized away.
I am confused. Don't you check for this in early_hugetlb_free_vmemmap_param already?
Right.
Why do we need any runtime checks?
If the size of the struct page is not power of 2, compiler can think
is_hugetlb_free_vmemmap_enabled() always return false. So
the code snippet of this user can be optimized away.

E.g.

if (is_hugetlb_free_vmemmap_enabled())
        /* do something */

The compiler can drop "/* do something */" directly, because
it knows is_hugetlb_free_vmemmap_enabled() always returns
false.

Thanks.
quoted
Signed-off-by: Muchun Song <redacted>
Reviewed-by: Miaohe Lin <linmiaohe@huawei.com>
Reviewed-by: Oscar Salvador <osalvador@suse.de>
Tested-by: Chen Huang <redacted>
Tested-by: Bodeddula Balasubramaniam <redacted>
---
 include/linux/hugetlb.h | 3 ++-
 mm/hugetlb_vmemmap.c    | 7 +++++++
 mm/hugetlb_vmemmap.h    | 6 ++++++
 3 files changed, 15 insertions(+), 1 deletion(-)
diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h
index c70421e26189..333dd0479fc2 100644
--- a/include/linux/hugetlb.h
+++ b/include/linux/hugetlb.h
@@ -880,7 +880,8 @@ extern bool hugetlb_free_vmemmap_enabled;

 static inline bool is_hugetlb_free_vmemmap_enabled(void)
 {
-     return hugetlb_free_vmemmap_enabled;
+     return hugetlb_free_vmemmap_enabled &&
+            is_power_of_2(sizeof(struct page));
 }
 #else
 static inline bool is_hugetlb_free_vmemmap_enabled(void)
diff --git a/mm/hugetlb_vmemmap.c b/mm/hugetlb_vmemmap.c
index 33e42678abe3..1ba1ef45c48c 100644
--- a/mm/hugetlb_vmemmap.c
+++ b/mm/hugetlb_vmemmap.c
@@ -265,6 +265,13 @@ void __init hugetlb_vmemmap_init(struct hstate *h)
      BUILD_BUG_ON(__NR_USED_SUBPAGE >=
                   RESERVE_VMEMMAP_SIZE / sizeof(struct page));

+     /*
+      * The compiler can help us to optimize this function to null
+      * when the size of the struct page is not power of 2.
+      */
+     if (!is_power_of_2(sizeof(struct page)))
+             return;
+
      if (!hugetlb_free_vmemmap_enabled)
              return;
diff --git a/mm/hugetlb_vmemmap.h b/mm/hugetlb_vmemmap.h
index cb2bef8f9e73..29aaaf7b741e 100644
--- a/mm/hugetlb_vmemmap.h
+++ b/mm/hugetlb_vmemmap.h
@@ -21,6 +21,12 @@ void hugetlb_vmemmap_init(struct hstate *h);
  */
 static inline unsigned int free_vmemmap_pages_per_hpage(struct hstate *h)
 {
+     /*
+      * This check aims to let the compiler help us optimize the code as
+      * much as possible.
+      */
+     if (!is_power_of_2(sizeof(struct page)))
+             return 0;
      return h->nr_free_vmemmap_pages;
 }
 #else
--
2.11.0
--
Michal Hocko
SUSE Labs
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help