Skip to contentSkip to conversation

trivial code refine for sparsemem

PATCH In review Wei Yang 4 participants 23 messages Aug 23, 2018 – Sep 11, 2018

Subsystems (3)
lore
Share & tools
lore

Wei Yang

author
#0 lore raw

cover trivial code refine for sparsemem

Here is three trivial refine patches for sparsemem.

Wei Yang (3):
mm/sparse: add likely to mem_section[root] check in
sparse_index_init()
mm/sparse: expand the CONFIG_SPARSEMEM_EXTREME range in

    __nr_to_section()

mm/sparse: use __highest_present_section_nr as the boundary for pfn
check

 include/linux/mmzone.h | 6 +++---
 mm/sparse.c            | 3 ++-
 2 files changed, 5 insertions(+), 4 deletions(-)
Signature · 1 line
2.15.1
author
#1 lore raw

PATCH 1/3 mm/sparse: add likely to mem_section[root] check in sparse_index_init()

Each time SECTIONS_PER_ROOT number of mem_section is allocated when mem_section[root] is null. This means only (1 / SECTIONS_PER_ROOT) chance of the mem_section[root] check is false.

This patch adds likely to the if check to optimize this a little.

---
 mm/sparse.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/sparse.c +1 -1
index 10b07eea9a6e..90bab7f03757 100644
@@ -78,7 +78,7 @@ static int __meminit sparse_index_init(unsigned long section_nr, int nid)
 	unsigned long root = SECTION_NR_TO_ROOT(section_nr);
 	struct mem_section *section;
 
-	if (mem_section[root])
+	if (likely(mem_section[root]))
 		return -EEXIST;
 
 	section = sparse_index_alloc(nid);
Signature · 1 line
2.15.1
author
#11 lore raw

PATCH 2/3 mm/sparse: expand the CONFIG_SPARSEMEM_EXTREME range in __nr_to_section()

In reply to Wei Yang

When CONFIG_SPARSEMEM_EXTREME is not defined, mem_section is a static two dimension array. This means !mem_section[SECTION_NR_TO_ROOT(nr)] is always true.

This patch expand the CONFIG_SPARSEMEM_EXTREME range to return a proper mem_section when CONFIG_SPARSEMEM_EXTREME is not defined.

---
 include/linux/mmzone.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/mmzone.h +1 -1
index 32699b2dc52a..33086f86d1a7 100644
@@ -1155,9 +1155,9 @@ static inline struct mem_section *__nr_to_section(unsigned long nr)
 #ifdef CONFIG_SPARSEMEM_EXTREME
 	if (!mem_section)
 		return NULL;
-#endif
 	if (!mem_section[SECTION_NR_TO_ROOT(nr)])
 		return NULL;
+#endif
 	return &mem_section[SECTION_NR_TO_ROOT(nr)][nr & SECTION_ROOT_MASK];
 }
 extern int __section_nr(struct mem_section* ms);
Signature · 1 line
2.15.1
author
#16 lore raw

PATCH 3/3 mm/sparse: use __highest_present_section_nr as the boundary for pfn check

In reply to Wei Yang

And it is known, __highest_present_section_nr is a more strict boundary than NR_MEM_SECTIONS.

This patch uses a __highest_present_section_nr to check a valid pfn.

---
 include/linux/mmzone.h | 4 ++--
 mm/sparse.c            | 1 +
 2 files changed, 3 insertions(+), 2 deletions(-)
include/linux/mmzone.h +2 -2
index 33086f86d1a7..5138efde11ae 100644
@@ -1237,7 +1237,7 @@ extern int __highest_present_section_nr;
 #ifndef CONFIG_HAVE_ARCH_PFN_VALID
 static inline int pfn_valid(unsigned long pfn)
 {
-	if (pfn_to_section_nr(pfn) >= NR_MEM_SECTIONS)
+	if (pfn_to_section_nr(pfn) > __highest_present_section_nr)
 		return 0;
 	return valid_section(__nr_to_section(pfn_to_section_nr(pfn)));
 }
@@ -1245,7 +1245,7 @@ static inline int pfn_valid(unsigned long pfn)
 
 static inline int pfn_present(unsigned long pfn)
 {
-	if (pfn_to_section_nr(pfn) >= NR_MEM_SECTIONS)
+	if (pfn_to_section_nr(pfn) > __highest_present_section_nr)
 		return 0;
 	return present_section(__nr_to_section(pfn_to_section_nr(pfn)));
 }
mm/sparse.c +1 -0
index 90bab7f03757..a9c55c8da11f 100644
@@ -174,6 +174,7 @@ void __meminit mminit_validate_memmodel_limits(unsigned long *start_pfn,
  * those loops early.
  */
 int __highest_present_section_nr;
+EXPORT_SYMBOL(__highest_present_section_nr);
 static void section_mark_present(struct mem_section *ms)
 {
 	int section_nr = __section_nr(ms);
Signature · 1 line
2.15.1
reviewer
#2 lore raw

Re: [PATCH 1/3] mm/sparse: add likely to mem_section[root] check in sparse_index_init()

In reply to Wei Yang
Quoted 5 lines · Wei Yang

Each time SECTIONS_PER_ROOT number of mem_section is allocated when mem_section[root] is null. This means only (1 / SECTIONS_PER_ROOT) chance of the mem_section[root] check is false.

This patch adds likely to the if check to optimize this a little.

Could you evaluate how much does this help if any? Does this have any impact on the initialization path at all?

Quoted 21 lines
---
 mm/sparse.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/sparse.c +1 -1
index 10b07eea9a6e..90bab7f03757 100644
@@ -78,7 +78,7 @@ static int __meminit sparse_index_init(unsigned long section_nr, int nid)
 	unsigned long root = SECTION_NR_TO_ROOT(section_nr);
 	struct mem_section *section;
 
-	if (mem_section[root])
+	if (likely(mem_section[root]))
 		return -EEXIST;
 
 	section = sparse_index_alloc(nid);
Signature · 1 line
2.15.1
Signature · 2 lines
Michal Hocko
SUSE Labs
reviewer
#12 lore raw

Re: [PATCH 2/3] mm/sparse: expand the CONFIG_SPARSEMEM_EXTREME range in __nr_to_section()

In reply to Wei Yang

[Cc Dave]

Quoted 6 lines · Wei Yang

When CONFIG_SPARSEMEM_EXTREME is not defined, mem_section is a static two dimension array. This means !mem_section[SECTION_NR_TO_ROOT(nr)] is always true.

This patch expand the CONFIG_SPARSEMEM_EXTREME range to return a proper mem_section when CONFIG_SPARSEMEM_EXTREME is not defined.

As long as all callers provide a valid section number then yes. I am not really sure this is the case though.

Quoted 23 lines
---
 include/linux/mmzone.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/mmzone.h +1 -1
index 32699b2dc52a..33086f86d1a7 100644
@@ -1155,9 +1155,9 @@ static inline struct mem_section *__nr_to_section(unsigned long nr)
 #ifdef CONFIG_SPARSEMEM_EXTREME
 	if (!mem_section)
 		return NULL;
-#endif
 	if (!mem_section[SECTION_NR_TO_ROOT(nr)])
 		return NULL;
+#endif
 	return &mem_section[SECTION_NR_TO_ROOT(nr)][nr & SECTION_ROOT_MASK];
 }
 extern int __section_nr(struct mem_section* ms);
Signature · 1 line
2.15.1
Signature · 2 lines
Michal Hocko
SUSE Labs
reviewer
#17 lore raw

Re: [PATCH 3/3] mm/sparse: use __highest_present_section_nr as the boundary for pfn check

In reply to Wei Yang
Quoted 4 lines · Wei Yang

And it is known, __highest_present_section_nr is a more strict boundary than NR_MEM_SECTIONS.

This patch uses a __highest_present_section_nr to check a valid pfn.

But why is this an improvement? Sure when you loop over all sections than __highest_present_section_nr makes a lot of sense. But all the updated function perform a trivial comparision.

Quoted 43 lines
---
 include/linux/mmzone.h | 4 ++--
 mm/sparse.c            | 1 +
 2 files changed, 3 insertions(+), 2 deletions(-)
include/linux/mmzone.h +2 -2
index 33086f86d1a7..5138efde11ae 100644
@@ -1237,7 +1237,7 @@ extern int __highest_present_section_nr;
 #ifndef CONFIG_HAVE_ARCH_PFN_VALID
 static inline int pfn_valid(unsigned long pfn)
 {
-	if (pfn_to_section_nr(pfn) >= NR_MEM_SECTIONS)
+	if (pfn_to_section_nr(pfn) > __highest_present_section_nr)
 		return 0;
 	return valid_section(__nr_to_section(pfn_to_section_nr(pfn)));
 }
@@ -1245,7 +1245,7 @@ static inline int pfn_valid(unsigned long pfn)
 
 static inline int pfn_present(unsigned long pfn)
 {
-	if (pfn_to_section_nr(pfn) >= NR_MEM_SECTIONS)
+	if (pfn_to_section_nr(pfn) > __highest_present_section_nr)
 		return 0;
 	return present_section(__nr_to_section(pfn_to_section_nr(pfn)));
 }
mm/sparse.c +1 -0
index 90bab7f03757..a9c55c8da11f 100644
@@ -174,6 +174,7 @@ void __meminit mminit_validate_memmodel_limits(unsigned long *start_pfn,
  * those loops early.
  */
 int __highest_present_section_nr;
+EXPORT_SYMBOL(__highest_present_section_nr);
 static void section_mark_present(struct mem_section *ms)
 {
 	int section_nr = __section_nr(ms);
Signature · 1 line
2.15.1
Signature · 2 lines
Michal Hocko
SUSE Labs
#18 lore raw

Re: [PATCH 3/3] mm/sparse: use __highest_present_section_nr as the boundary for pfn check

Quoted 9 lines · Michal Hocko
Quoted 4 lines · Wei Yang

And it is known, __highest_present_section_nr is a more strict boundary than NR_MEM_SECTIONS.

This patch uses a __highest_present_section_nr to check a valid pfn.

But why is this an improvement? Sure when you loop over all sections than __highest_present_section_nr makes a lot of sense. But all the updated function perform a trivial comparision.

I think it makes some sense.
NR_MEM_SECTIONS can be a big number, but we might not be using all sections, so __highest_present_section_nr ends up being a much lower value.

I think that we want to compare the pfn's section_nr with our current limit of present sections.
Sections over that do not really exist for us, so it is no use to look for them in __nr_to_section/valid_section.

It might not be a big improvement, but I think that given the nature of pfn_valid/pfn_present, comparing to __highest_present_section_nr suits better.

Signature · 2 lines
Oscar Salvador
SUSE L3
reviewer
#19 lore raw

Re: [PATCH 3/3] mm/sparse: use __highest_present_section_nr as the boundary for pfn check

Quoted 7 lines · Oscar Salvador
Quoted 6 lines · Michal Hocko
Quoted 4 lines · Wei Yang

And it is known, __highest_present_section_nr is a more strict boundary than NR_MEM_SECTIONS.

This patch uses a __highest_present_section_nr to check a valid pfn.

Quoted 8 lines
Quoted 3 lines

But why is this an improvement? Sure when you loop over all sections than __highest_present_section_nr makes a lot of sense. But all the updated function perform a trivial comparision.

I think it makes some sense.
NR_MEM_SECTIONS can be a big number, but we might not be using all sections, so __highest_present_section_nr ends up being a much lower value.

And how exactly does it help to check for the smaller vs. a larger number? Both are O(1) operations AFAICS. __highest_present_section_nr makes perfect sense when we iterate over all sections or similar operations where it smaller number of iterations really makes sense.

I am not saying the patch is wrong but I just do not see this being an improvement. You have to export an internal symbol to achieve something that is hardly an optimization.

Signature · 2 lines
Michal Hocko
SUSE Labs
#20 lore raw

Re: [PATCH 3/3] mm/sparse: use __highest_present_section_nr as the boundary for pfn check

Quoted 4 lines · Michal Hocko

And how exactly does it help to check for the smaller vs. a larger number? Both are O(1) operations AFAICS. __highest_present_section_nr makes perfect sense when we iterate over all sections or similar operations where it smaller number of iterations really makes sense.

Sure, improvement/optimization was not really my point, a comparasion is a comparasion.
The gain, if any, would be because we would catch
non present sections sooner before calling to present_section(). In the case that __highest_present_section_nr differs from NR_MEM_SECTIONS, of course.

I thought it would make more sense given the nature of the function itself.

The only thing I did not like much was that we need to export the symbol, though. So, as you said, the price might be too hight for what we get.

Signature · 2 lines
Oscar Salvador
SUSE L3
author
#3 lore raw

Re: [PATCH 1/3] mm/sparse: add likely to mem_section[root] check in sparse_index_init()

In reply to Michal Hocko
Quoted 9 lines · Michal Hocko
Quoted 5 lines · Wei Yang

Each time SECTIONS_PER_ROOT number of mem_section is allocated when mem_section[root] is null. This means only (1 / SECTIONS_PER_ROOT) chance of the mem_section[root] check is false.

This patch adds likely to the if check to optimize this a little.

Could you evaluate how much does this help if any? Does this have any impact on the initialization path at all?

Let me test on my 4G machine with this patch :-)

Quoted 26 lines
Quoted 22 lines
---
 mm/sparse.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/sparse.c +1 -1
index 10b07eea9a6e..90bab7f03757 100644
@@ -78,7 +78,7 @@ static int __meminit sparse_index_init(unsigned long section_nr, int nid)
 	unsigned long root = SECTION_NR_TO_ROOT(section_nr);
 	struct mem_section *section;
 
-	if (mem_section[root])
+	if (likely(mem_section[root]))
 		return -EEXIST;
 
 	section = sparse_index_alloc(nid);
Signature · 1 line
2.15.1
Signature · 2 lines
Michal Hocko
SUSE Labs
Signature · 2 lines
Wei Yang
Help you, Help me
author
#13 lore raw

Re: [PATCH 2/3] mm/sparse: expand the CONFIG_SPARSEMEM_EXTREME range in __nr_to_section()

In reply to Michal Hocko
Quoted 7 lines · Michal Hocko

[Cc Dave]

Quoted 4 lines · Wei Yang

When CONFIG_SPARSEMEM_EXTREME is not defined, mem_section is a static two dimension array. This means !mem_section[SECTION_NR_TO_ROOT(nr)] is always true.

Quoted 6 lines
Quoted 2 lines

This patch expand the CONFIG_SPARSEMEM_EXTREME range to return a proper mem_section when CONFIG_SPARSEMEM_EXTREME is not defined.

As long as all callers provide a valid section number then yes. I am not really sure this is the case though.

I don't get your point.

When CONFIG_SPARSEMEM_EXTREME is not defined, each section number is a valid one in this context. Because for eavry section number in [0, NR_MEM_SECTIONS - 1], we have a mem_sectioin structure there.

This patch helps to reduce a meaningless check when CONFIG_SPARSEMEM_EXTREME=n.

Quoted 27 lines
Quoted 23 lines
---
 include/linux/mmzone.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/mmzone.h +1 -1
index 32699b2dc52a..33086f86d1a7 100644
@@ -1155,9 +1155,9 @@ static inline struct mem_section *__nr_to_section(unsigned long nr)
 #ifdef CONFIG_SPARSEMEM_EXTREME
 	if (!mem_section)
 		return NULL;
-#endif
 	if (!mem_section[SECTION_NR_TO_ROOT(nr)])
 		return NULL;
+#endif
 	return &mem_section[SECTION_NR_TO_ROOT(nr)][nr & SECTION_ROOT_MASK];
 }
 extern int __section_nr(struct mem_section* ms);
Signature · 1 line
2.15.1
Signature · 2 lines
Michal Hocko
SUSE Labs
Signature · 2 lines
Wei Yang
Help you, Help me
#14 lore raw

Re: [PATCH 2/3] mm/sparse: expand the CONFIG_SPARSEMEM_EXTREME range in __nr_to_section()

In reply to Michal Hocko

include/linux/mmzone.h static inline struct mem_section *__nr_to_section(unsigned long nr) · line 1162

Quoted 12 lines · Michal Hocko
include/linux/mmzone.h +1 -1
@@ -1155,9 +1155,9 @@ static inline struct mem_section *__nr_to_section(unsigned long nr)
 #ifdef CONFIG_SPARSEMEM_EXTREME
 	if (!mem_section)
 		return NULL;
-#endif
 	if (!mem_section[SECTION_NR_TO_ROOT(nr)])
 		return NULL;
+#endif
 	return &mem_section[SECTION_NR_TO_ROOT(nr)][nr & SECTION_ROOT_MASK];
 }

This patch has no practical effect and only adds unnecessary churn.

#ifdef CONFIG_SPARSEMEM_EXTREME
...
#else
struct mem_section mem_section[NR_SECTION_ROOTS][SECTIONS_PER_ROOT]; #endif

The compiler knows that NR_SECTION_ROOTS==1 and that !mem_section[SECTION_NR_TO_ROOT(nr) is always false. It doesn't need our help.

My goal with the sparsemem code, and code in general is t avoid #ifdefs whenever possible and limit their scope to the smallest possible area whenever possible.

#5 lore raw

Re: [PATCH 1/3] mm/sparse: add likely to mem_section[root] check in sparse_index_init()

In reply to Wei Yang

mm/sparse.c static int __meminit sparse_index_init(unsigned long section_nr, int nid) · line 82

Quoted 9 lines · Wei Yang
mm/sparse.c +1 -1
@@ -78,7 +78,7 @@ static int __meminit sparse_index_init(unsigned long section_nr, int nid)
 	unsigned long root = SECTION_NR_TO_ROOT(section_nr);
 	struct mem_section *section;
 
-	if (mem_section[root])
+	if (likely(mem_section[root]))
 		return -EEXIST;

We could add likely()/unlikely() to approximately a billion if()s around the kernel if we felt like it. We don't because it's messy and it actually takes away choices from the compiler.

Please don't send patches like this unless you have some *actual* analysis that shows the benefit of the patch. Performance numbers are best.

#21 lore raw

Re: [PATCH 3/3] mm/sparse: use __highest_present_section_nr as the boundary for pfn check

In reply to Wei Yang
Quoted 2 lines · Wei Yang

And it is known, __highest_present_section_nr is a more strict boundary than NR_MEM_SECTIONS.

What is the benefit of this patch?

You're adding a more "strict" boundary, but you're also adding a potential cacheline miss and removing optimizations that the compiler can make with a constant vs. a variable. Providing absolute bounds limits that the compiler knows about can actually be pretty handy for it to optimize things

Do you have *any* analysis to show that this has a benefit? What does it do to text size, for instance?

reviewer
#4 lore raw

Re: [PATCH 1/3] mm/sparse: add likely to mem_section[root] check in sparse_index_init()

In reply to Wei Yang
Quoted 6 lines · Wei Yang
Quoted 5 lines · Michal Hocko
Quoted 4 lines · Wei Yang

Each time SECTIONS_PER_ROOT number of mem_section is allocated when mem_section[root] is null. This means only (1 / SECTIONS_PER_ROOT) chance of the mem_section[root] check is false.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

This patch adds likely to the if check to optimize this a little.

Could you evaluate how much does this help if any? Does this have any impact on the initialization path at all?

Let me test on my 4G machine with this patch :-)

Well, this should have been done before posting the patch. In general, though, you should have some convincing numbers in order to add new likely/unlikely annotations. They are rarely helpful and they tend to rot over time. Steven Rostedt has made some test few years back and found out that a vast majority of those annotation were simply wrong.

Signature · 2 lines
Michal Hocko
SUSE Labs
author
#6 lore raw

Re: [PATCH 1/3] mm/sparse: add likely to mem_section[root] check in sparse_index_init()

In reply to Dave Hansen
Quoted 15 lines · Dave Hansen

mm/sparse.c static int __meminit sparse_index_init(unsigned long section_nr, int nid) · line 82

Quoted 9 lines · Wei Yang
mm/sparse.c +1 -1
@@ -78,7 +78,7 @@ static int __meminit sparse_index_init(unsigned long section_nr, int nid)
 	unsigned long root = SECTION_NR_TO_ROOT(section_nr);
 	struct mem_section *section;
 
-	if (mem_section[root])
+	if (likely(mem_section[root]))
 		return -EEXIST;

We could add likely()/unlikely() to approximately a billion if()s around the kernel if we felt like it. We don't because it's messy and it actually takes away choices from the compiler.

Quoted 2 lines

Please don't send patches like this unless you have some *actual* analysis that shows the benefit of the patch. Performance numbers are best.

Thanks all for your comments, Michal, Dave and Oscar.

Well, maybe I took it for granted, so let me put more words on this. To be honest, my analysis maybe partially effective, so if the cost is higher than the gain, please let me know.

Below is my analysis and test result for this patch.
------------------------------------------------------

During bootup, the call flow looks like this.

sparse_memory_present_with_active_regions()
memory_present()
sparse_index_init()

sparse_memory_present_with_active_regions() iterates on pfn continuously for the whole system RAM, which leads to sparse_index_init() will iterate section_nr continuously. Usually, we don't expect many large holes, right?

Each time when mem_section[root] is null, SECTIONS_PER_ROOT number of mem_section will be allocated. This means, for SECTIONS_PER_ROOT number of check, only the first check is false. So the possibility to be false is (1 / SECTIONS_PER_ROOT).

SECTIONS_PER_ROOT is defined as (PAGE_SIZE / sizeof (struct mem_section)).

On my x86_64 machine, PAGE_SIZE is 4KB and mem_section is 16B.

    SECTIONS_PER_ROOT = 4K / 16 = 256.

So the check for mem_section[root] is (1 / 256) chance to be invalid and (255 / 256) valid. In theory, this value seems to be a "likely" to me.

In practice, when the system RAM is multiple times of
((1 << SECTION_SIZE_BITS) * SECTIONS_PER_ROOT), the "likely" chance is (255 / 256), otherwise the chance would be less.

On my x86_64 machine, SECTION_SIZE_BITS is defined to 27.

    ((1 << SECTION_SIZE_BITS) * SECTIONS_PER_ROOT) = 32GB

          System RAM size       32G         16G        8G         4G
      Possibility          (255 / 256) (127 / 128) (63 / 64)  (31 / 32)

Generally, in my mind, if we iterate pfn continuously and there is no large holes, the check on mem_section[root] is likely to be true.

At last, here is the test result on my 4G virtual machine. I added printk before and after sparse_memory_present_with_active_regions() and tested three times with/without "likely".

                without      with
     Elapsed   0.000252     0.000250   -0.8%

The benefit seems to be too small on a 4G virtual machine or even this is not stable. Not sure we can see some visible effect on a 32G machine.

Well, above is all my analysis and test result. I did the optimization based on my own experience and understanding. If this is not qualified, I am very glad to hear from your statement, so that I would learn more from your experience.

Thanks all for your comments again :-)

Signature · 2 lines
Wei Yang
Help you, Help me
author
#15 lore raw

Re: [PATCH 2/3] mm/sparse: expand the CONFIG_SPARSEMEM_EXTREME range in __nr_to_section()

In reply to Dave Hansen
Quoted 22 lines · Dave Hansen

include/linux/mmzone.h static inline struct mem_section *__nr_to_section(unsigned long nr) · line 1162

Quoted 12 lines · Michal Hocko
include/linux/mmzone.h +1 -1
@@ -1155,9 +1155,9 @@ static inline struct mem_section *__nr_to_section(unsigned long nr)
 #ifdef CONFIG_SPARSEMEM_EXTREME
 	if (!mem_section)
 		return NULL;
-#endif
 	if (!mem_section[SECTION_NR_TO_ROOT(nr)])
 		return NULL;
+#endif
 	return &mem_section[SECTION_NR_TO_ROOT(nr)][nr & SECTION_ROOT_MASK];
 }

This patch has no practical effect and only adds unnecessary churn.

#ifdef CONFIG_SPARSEMEM_EXTREME
...
#else
struct mem_section mem_section[NR_SECTION_ROOTS][SECTIONS_PER_ROOT]; #endif

Quoted 4 lines

The compiler knows that NR_SECTION_ROOTS==1 and that !mem_section[SECTION_NR_TO_ROOT(nr) is always false. It doesn't need our help.

I didn't know the compile would optimize the code when this is a one dimension array. Just wrote a code and their assembly looks the same.

Thanks for pointing out.

Quoted 3 lines

My goal with the sparsemem code, and code in general is t avoid #ifdefs whenever possible and limit their scope to the smallest possible area whenever possible.

Signature · 2 lines
Wei Yang
Help you, Help me
author
#22 lore raw

Re: [PATCH 3/3] mm/sparse: use __highest_present_section_nr as the boundary for pfn check

In reply to Dave Hansen
Quoted 6 lines · Dave Hansen
Quoted 2 lines · Wei Yang

And it is known, __highest_present_section_nr is a more strict boundary than NR_MEM_SECTIONS.

What is the benefit of this patch?

The original idea is simple: with a more strict boundary, it will has less computation.

Quoted 5 lines

You're adding a more "strict" boundary, but you're also adding a potential cacheline miss and removing optimizations that the compiler can make with a constant vs. a variable. Providing absolute bounds limits that the compiler knows about can actually be pretty handy for it to optimize things

You are right.

I haven't thought about the compiler optimization case.

Quoted 3 lines

Do you have *any* analysis to show that this has a benefit? What does it do to text size, for instance?

I don't have more analysis about this. Originally, I thought a strict boundary will have a benefit. But as you mentioned, it loose the compiler optimization.

BTW, I did some tests and found during a normal boot, all the section number are within NR_MEM_SECTIONS. I am wondering in which case, this check will be valid and return 0?

Signature · 2 lines
Wei Yang
Help you, Help me
author
#7 lore raw

Re: [PATCH 1/3] mm/sparse: add likely to mem_section[root] check in sparse_index_init()

In reply to Wei Yang
Quoted 16 lines · Wei Yang
Quoted 15 lines · Dave Hansen

mm/sparse.c static int __meminit sparse_index_init(unsigned long section_nr, int nid) · line 82

Quoted 9 lines · Wei Yang
mm/sparse.c +1 -1
@@ -78,7 +78,7 @@ static int __meminit sparse_index_init(unsigned long section_nr, int nid)
 	unsigned long root = SECTION_NR_TO_ROOT(section_nr);
 	struct mem_section *section;
 
-	if (mem_section[root])
+	if (likely(mem_section[root]))
 		return -EEXIST;

We could add likely()/unlikely() to approximately a billion if()s around the kernel if we felt like it. We don't because it's messy and it actually takes away choices from the compiler.

Quoted 3 lines
Quoted 2 lines

Please don't send patches like this unless you have some *actual* analysis that shows the benefit of the patch. Performance numbers are best.

Hi,

Is my analysis reasonable? Or which part is not valid?

Quoted 69 lines

Thanks all for your comments, Michal, Dave and Oscar.

Well, maybe I took it for granted, so let me put more words on this. To be honest, my analysis maybe partially effective, so if the cost is higher than the gain, please let me know.

Below is my analysis and test result for this patch.
------------------------------------------------------

During bootup, the call flow looks like this.

sparse_memory_present_with_active_regions()
memory_present()
sparse_index_init()

sparse_memory_present_with_active_regions() iterates on pfn continuously for the whole system RAM, which leads to sparse_index_init() will iterate section_nr continuously. Usually, we don't expect many large holes, right?

Each time when mem_section[root] is null, SECTIONS_PER_ROOT number of mem_section will be allocated. This means, for SECTIONS_PER_ROOT number of check, only the first check is false. So the possibility to be false is (1 / SECTIONS_PER_ROOT).

SECTIONS_PER_ROOT is defined as (PAGE_SIZE / sizeof (struct mem_section)).

On my x86_64 machine, PAGE_SIZE is 4KB and mem_section is 16B.

   SECTIONS_PER_ROOT = 4K / 16 = 256.

So the check for mem_section[root] is (1 / 256) chance to be invalid and (255 / 256) valid. In theory, this value seems to be a "likely" to me.

In practice, when the system RAM is multiple times of
((1 << SECTION_SIZE_BITS) * SECTIONS_PER_ROOT), the "likely" chance is (255 / 256), otherwise the chance would be less.

On my x86_64 machine, SECTION_SIZE_BITS is defined to 27.

   ((1 << SECTION_SIZE_BITS) * SECTIONS_PER_ROOT) = 32GB

         System RAM size       32G         16G        8G         4G
     Possibility          (255 / 256) (127 / 128) (63 / 64)  (31 / 32)

Generally, in my mind, if we iterate pfn continuously and there is no large holes, the check on mem_section[root] is likely to be true.

At last, here is the test result on my 4G virtual machine. I added printk before and after sparse_memory_present_with_active_regions() and tested three times with/without "likely".

               without      with
    Elapsed   0.000252     0.000250   -0.8%

The benefit seems to be too small on a 4G virtual machine or even this is not stable. Not sure we can see some visible effect on a 32G machine.

Well, above is all my analysis and test result. I did the optimization based on my own experience and understanding. If this is not qualified, I am very glad to hear from your statement, so that I would learn more from your experience.

Thanks all for your comments again :-)

Signature · 2 lines
Wei Yang
Help you, Help me
Signature · 2 lines
Wei Yang
Help you, Help me
author
#8 lore raw

Re: [PATCH 1/3] mm/sparse: add likely to mem_section[root] check in sparse_index_init()

Quoted 17 lines · Wei Yang
Quoted 16 lines · Wei Yang
Quoted 15 lines · Dave Hansen

mm/sparse.c static int __meminit sparse_index_init(unsigned long section_nr, int nid) · line 82

Quoted 9 lines · Wei Yang
mm/sparse.c +1 -1
@@ -78,7 +78,7 @@ static int __meminit sparse_index_init(unsigned long section_nr, int nid)
 	unsigned long root = SECTION_NR_TO_ROOT(section_nr);
 	struct mem_section *section;
 
-	if (mem_section[root])
+	if (likely(mem_section[root]))
 		return -EEXIST;

We could add likely()/unlikely() to approximately a billion if()s around the kernel if we felt like it. We don't because it's messy and it actually takes away choices from the compiler.

Quoted 8 lines
Quoted 3 lines
Quoted 2 lines

Please don't send patches like this unless you have some *actual* analysis that shows the benefit of the patch. Performance numbers are best.

Hi,

Is my analysis reasonable? Or which part is not valid?

Would someone share some idea on my analysis?

Quoted 73 lines
Quoted 69 lines

Thanks all for your comments, Michal, Dave and Oscar.

Well, maybe I took it for granted, so let me put more words on this. To be honest, my analysis maybe partially effective, so if the cost is higher than the gain, please let me know.

Below is my analysis and test result for this patch.
------------------------------------------------------

During bootup, the call flow looks like this.

sparse_memory_present_with_active_regions()
memory_present()
sparse_index_init()

sparse_memory_present_with_active_regions() iterates on pfn continuously for the whole system RAM, which leads to sparse_index_init() will iterate section_nr continuously. Usually, we don't expect many large holes, right?

Each time when mem_section[root] is null, SECTIONS_PER_ROOT number of mem_section will be allocated. This means, for SECTIONS_PER_ROOT number of check, only the first check is false. So the possibility to be false is (1 / SECTIONS_PER_ROOT).

SECTIONS_PER_ROOT is defined as (PAGE_SIZE / sizeof (struct mem_section)).

On my x86_64 machine, PAGE_SIZE is 4KB and mem_section is 16B.

   SECTIONS_PER_ROOT = 4K / 16 = 256.

So the check for mem_section[root] is (1 / 256) chance to be invalid and (255 / 256) valid. In theory, this value seems to be a "likely" to me.

In practice, when the system RAM is multiple times of
((1 << SECTION_SIZE_BITS) * SECTIONS_PER_ROOT), the "likely" chance is (255 / 256), otherwise the chance would be less.

On my x86_64 machine, SECTION_SIZE_BITS is defined to 27.

   ((1 << SECTION_SIZE_BITS) * SECTIONS_PER_ROOT) = 32GB

         System RAM size       32G         16G        8G         4G
     Possibility          (255 / 256) (127 / 128) (63 / 64)  (31 / 32)

Generally, in my mind, if we iterate pfn continuously and there is no large holes, the check on mem_section[root] is likely to be true.

At last, here is the test result on my 4G virtual machine. I added printk before and after sparse_memory_present_with_active_regions() and tested three times with/without "likely".

               without      with
    Elapsed   0.000252     0.000250   -0.8%

The benefit seems to be too small on a 4G virtual machine or even this is not stable. Not sure we can see some visible effect on a 32G machine.

Well, above is all my analysis and test result. I did the optimization based on my own experience and understanding. If this is not qualified, I am very glad to hear from your statement, so that I would learn more from your experience.

Thanks all for your comments again :-)

Signature · 2 lines
Wei Yang
Help you, Help me
Signature · 2 lines
Wei Yang
Help you, Help me
Signature · 2 lines
Wei Yang
Help you, Help me
#9 lore raw

Re: [PATCH 1/3] mm/sparse: add likely to mem_section[root] check in sparse_index_init()

Quoted 10 lines · On 09/08/2018 06:38 PM, [email protected] wrote:

At last, here is the test result on my 4G virtual machine. I added printk before and after sparse_memory_present_with_active_regions() and tested three times with/without "likely".

               without      with
    Elapsed   0.000252     0.000250   -0.8%

The benefit seems to be too small on a 4G virtual machine or even this is not stable. Not sure we can see some visible effect on a 32G machine.

I think it's highly unlikely you have found something significant here. It's one system, in a VM and it's not being measured using a mechanism that is suitable for benchmarking (the kernel dmesg timestamps).

Plus, if this is a really tight loop, the cpu's branch predictors will be good at it.

author
#10 lore raw

Re: [PATCH 1/3] mm/sparse: add likely to mem_section[root] check in sparse_index_init()

Quoted 12 lines · Dave Hansen
Quoted 10 lines · On 09/08/2018 06:38 PM, [email protected] wrote:

At last, here is the test result on my 4G virtual machine. I added printk before and after sparse_memory_present_with_active_regions() and tested three times with/without "likely".

               without      with
    Elapsed   0.000252     0.000250   -0.8%

The benefit seems to be too small on a 4G virtual machine or even this is not stable. Not sure we can see some visible effect on a 32G machine.

Quoted 6 lines

I think it's highly unlikely you have found something significant here. It's one system, in a VM and it's not being measured using a mechanism that is suitable for benchmarking (the kernel dmesg timestamps).

Plus, if this is a really tight loop, the cpu's branch predictors will be good at it.

Hi, Dave

Thanks for your reply.

I think you are right. This part is not significant and cpu may do its job well.

Hmm... I am still willing to hear your opinion on my analysis of this situation. In which case we would use likely/unlikely.

For example, in this case the possibility is (255/ 256) if the system has 32G RAM. Do we have a threshold of the possibility to use likely/unlikely. Or we'd prefer not to use this any more? Let compiler and cpu do their job.

Look forward your insights.

Signature · 2 lines
Wei Yang
Help you, Help me