mm/page_alloc: break on the first hit of mem range
PATCH 1/1 mm/page_alloc: break on the first hit of mem range
find_min_pfn_for_node() iterate on pfn range to find the minimum pfn for a node. The memblock_region in memblock_type are already ordered, which means the first hit in iteration is the minimum pfn.
This patch returns the fist hit instead of iterating the whole regions.
--- mm/page_alloc.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
mm/page_alloc.c +5 -5
@@ -6365,14 +6365,14 @@ unsigned long __init node_map_pfn_alignment(void)/* Find the lowest pfn for a node */ static unsigned long __init find_min_pfn_for_node (int nid) { -unsigned long min_pfn =ULONG_MAX ; -unsigned long start_pfn; +unsigned long min_pfn;int i; -for_each_mem_pfn_range (i, nid, &start_pfn,NULL ,NULL ) - min_pfn =min (min_pfn, start_pfn); +for_each_mem_pfn_range (i, nid, &min_pfn,NULL ,NULL ) { +break ; + } -if (min_pfn ==ULONG_MAX ) { +if (i ==-1 ) {pr_warn ("Could not find start_pfn for node %d\n" , nid);return 0 ; }
Signature · 1 line
2.15.1
Quoted 3 lines · Wei Yang
find_min_pfn_for_node()iterate on pfn range to find the minimum pfn for a node. Thememblock_regioninmemblock_typeare already ordered, which means the first hit in iteration is the minimum pfn.
I haven't looked at the code yet but the changelog should contain the motivation why it exists. It seems like this is an optimization. If so, what is the impact?
Quoted 34 lines
This patch returns the fist hit instead of iterating the whole regions.
Signed-off-by Wei Yang <[email protected]>--- mm/page_alloc.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)mm/page_alloc.c +5
-5@@ -6365,14 +6365,14 @@ unsigned long __init node_map_pfn_alignment(void)/* Find the lowest pfn for a node */ static unsigned long __init find_min_pfn_for_node (int nid) { -unsigned long min_pfn =ULONG_MAX ; -unsigned long start_pfn; +unsigned long min_pfn;int i; -for_each_mem_pfn_range (i, nid, &start_pfn,NULL ,NULL ) - min_pfn =min (min_pfn, start_pfn); +for_each_mem_pfn_range (i, nid, &min_pfn,NULL ,NULL ) { +break ; + } -if (min_pfn ==ULONG_MAX ) { +if (i ==-1 ) {pr_warn ("Could not find start_pfn for node %d\n" , nid);return 0 ; }Signature · 1 line
2.15.1
Signature · 2 lines
Michal Hocko SUSE Labs
Quoted 9 lines · Michal Hocko
Quoted 3 lines · Wei Yang
find_min_pfn_for_node()iterate on pfn range to find the minimum pfn for a node. Thememblock_regioninmemblock_typeare already ordered, which means the first hit in iteration is the minimum pfn.I haven't looked at the code yet but the changelog should contain the motivation why it exists. It seems like this is an optimization. If so, what is the impact?
Yep, this is a trivial optimization on searching the minimal pfn on a special node. It would be better for audience to understand if I put some words in change log.
The impact of this patch is it would accelerate the searching process when there are many memory ranges in memblock.
For example, in the case https://lkml.org/lkml/2018/3/25/291, there are around 30 memory ranges on node 0. The original code need to iterate all those ranges to find the minimal pfn, while after optimization it just need once.
The more memory ranges there are, the more impact this patch has.
Quoted 38 lines
Quoted 34 lines
This patch returns the fist hit instead of iterating the whole regions.
Signed-off-by Wei Yang <[email protected]>--- mm/page_alloc.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)mm/page_alloc.c +5
-5@@ -6365,14 +6365,14 @@ unsigned long __init node_map_pfn_alignment(void)/* Find the lowest pfn for a node */ static unsigned long __init find_min_pfn_for_node (int nid) { -unsigned long min_pfn =ULONG_MAX ; -unsigned long start_pfn; +unsigned long min_pfn;int i; -for_each_mem_pfn_range (i, nid, &start_pfn,NULL ,NULL ) - min_pfn =min (min_pfn, start_pfn); +for_each_mem_pfn_range (i, nid, &min_pfn,NULL ,NULL ) { +break ; + } -if (min_pfn ==ULONG_MAX ) { +if (i ==-1 ) {pr_warn ("Could not find start_pfn for node %d\n" , nid);return 0 ; }Signature · 1 line
2.15.1Signature · 2 lines
Michal Hocko SUSE Labs
Signature · 2 lines
Wei Yang Help you, Help me
Quoted 15 lines · Wei Yang
Quoted 9 lines · Michal Hocko
Quoted 3 lines · Wei Yang
find_min_pfn_for_node()iterate on pfn range to find the minimum pfn for a node. Thememblock_regioninmemblock_typeare already ordered, which means the first hit in iteration is the minimum pfn.I haven't looked at the code yet but the changelog should contain the motivation why it exists. It seems like this is an optimization. If so, what is the impact?
Yep, this is a trivial optimization on searching the minimal pfn on a special node. It would be better for audience to understand if I put some words in change log.
Quoted 6 lines
The impact of this patch is it would accelerate the searching process when there are many memory ranges in memblock.
For example, in the case https://lkml.org/lkml/2018/3/25/291, there are around 30 memory ranges on node 0. The original code need to iterate all those ranges to find the minimal pfn, while after optimization it just need once.
Then show us some numbers to justify the change.
Signature · 2 lines
Michal Hocko SUSE Labs
Quoted 16 lines · Michal Hocko
Quoted 15 lines · Wei Yang
Quoted 9 lines · Michal Hocko
Quoted 3 lines · Wei Yang
find_min_pfn_for_node()iterate on pfn range to find the minimum pfn for a node. Thememblock_regioninmemblock_typeare already ordered, which means the first hit in iteration is the minimum pfn.I haven't looked at the code yet but the changelog should contain the motivation why it exists. It seems like this is an optimization. If so, what is the impact?
Yep, this is a trivial optimization on searching the minimal pfn on a special node. It would be better for audience to understand if I put some words in change log.
Quoted 8 lines
Quoted 6 lines
The impact of this patch is it would accelerate the searching process when there are many memory ranges in memblock.
For example, in the case https://lkml.org/lkml/2018/3/25/291, there are around 30 memory ranges on node 0. The original code need to iterate all those ranges to find the minimal pfn, while after optimization it just need once.
Then show us some numbers to justify the change.
Oops, I don't have any data to prove this.
My test machine just has 7 memory regions and only one node. So it reduce iteration from 7 to 1, which I don't think will have some visible effect.
While we can do some calculation to estimate the effect.
Assume there are N memory regions and M nodes and each node has equal number of memory regions.
So before the change, there are
N * M iterations
After this optimization, there are
(N / 2) * M iterations
So the expected improvement of this change is half the iterations for finding the minimal pfn.
Last but not the least, as I know, usually there are less than 100 memory regions on a machine. This improvement is really limited on current systems. The more memory regions and node a system has, the more improvement it will has.
Quoted 3 lines
Signature · 2 lines
Michal Hocko SUSE Labs
Signature · 2 lines
Wei Yang Help you, Help me
Andrew Morton
maintainerQuoted 14 lines · On Tue, 27 Mar 2018 11:57:07 +0800 Wei Yang <[email protected]> wrote:
find_min_pfn_for_node()iterate on pfn range to find the minimum pfn for a node. Thememblock_regioninmemblock_typeare already ordered, which means the first hit in iteration is the minimum pfn.This patch returns the fist hit instead of iterating the whole regions.
...
mm/page_alloc.c +0
-0@@ -6365,14 +6365,14 @@ unsigned long __init node_map_pfn_alignment(void)/* Find the lowest pfn for a node */ static unsigned long __init find_min_pfn_for_node (int nid) {
mm/page_alloc.c unsigned long __init node_map_pfn_alignment(void) · line 6373
Quoted 10 lines
mm/page_alloc.c +4
-4-unsigned long min_pfn =ULONG_MAX ; -unsigned long start_pfn; +unsigned long min_pfn;int i; -for_each_mem_pfn_range (i, nid, &start_pfn,NULL ,NULL ) - min_pfn =min (min_pfn, start_pfn); +for_each_mem_pfn_range (i, nid, &min_pfn,NULL ,NULL ) { +break ; + }
That would be the weirdest-looking code snippet in mm/!
Can't we just use a single and simple call to __next_mem_pfn_range(), or something like that?
Quoted 3 lines
...
Quoted 21 lines · Andrew Morton
Quoted 19 lines · On Tue, 27 Mar 2018 11:57:07 +0800 Wei Yang <[email protected]> wrote:
find_min_pfn_for_node()iterate on pfn range to find the minimum pfn for a node. Thememblock_regioninmemblock_typeare already ordered, which means the first hit in iteration is the minimum pfn.This patch returns the fist hit instead of iterating the whole regions.
...
mm/page_alloc.c +1
-2@@ -6365,14 +6365,14 @@ unsigned long __init node_map_pfn_alignment(void)/* Find the lowest pfn for a node */ static unsigned long __init find_min_pfn_for_node (int nid) { -unsigned long min_pfn =ULONG_MAX ; -unsigned long start_pfn; +unsigned long min_pfn;int i;
Quoted 8 lines
mm/page_alloc.c unsigned long __init node_map_pfn_alignment(void) · line 6373
Quoted 5 lines
mm/page_alloc.c +3
-2-for_each_mem_pfn_range (i, nid, &start_pfn,NULL ,NULL ) - min_pfn =min (min_pfn, start_pfn); +for_each_mem_pfn_range (i, nid, &min_pfn,NULL ,NULL ) { +break ; + }That would be the weirdest-looking code snippet in
mm/!
You mean the only break in a for_each loop? Hmm..., this is really not that nice. Haven't noticed could get a "best" in this way :-)
Quoted 3 lines
Can't we just use a single and simple call to
__next_mem_pfn_range(), or something like that?
Sounds a better choice, if you like this version, I would rearrange the patch and send v2.
Have a nice day~
Quoted 3 lines
Quoted 3 lines
...
Signature · 2 lines
Wei Yang Help you, Help me
Andrew Morton
maintainerQuoted 31 lines · On Wed, 28 Mar 2018 08:51:42 +0800 Wei Yang <[email protected]> wrote:
Quoted 21 lines · Andrew Morton
Quoted 19 lines · On Tue, 27 Mar 2018 11:57:07 +0800 Wei Yang <[email protected]> wrote:
find_min_pfn_for_node()iterate on pfn range to find the minimum pfn for a node. Thememblock_regioninmemblock_typeare already ordered, which means the first hit in iteration is the minimum pfn.This patch returns the fist hit instead of iterating the whole regions.
...
mm/page_alloc.c +1
-2@@ -6365,14 +6365,14 @@ unsigned long __init node_map_pfn_alignment(void)/* Find the lowest pfn for a node */ static unsigned long __init find_min_pfn_for_node (int nid) { -unsigned long min_pfn =ULONG_MAX ; -unsigned long start_pfn; +unsigned long min_pfn;int i;Quoted 8 lines
mm/page_alloc.c unsigned long __init node_map_pfn_alignment(void) · line 6373
Quoted 5 lines
mm/page_alloc.c +3
-2-for_each_mem_pfn_range (i, nid, &start_pfn,NULL ,NULL ) - min_pfn =min (min_pfn, start_pfn); +for_each_mem_pfn_range (i, nid, &min_pfn,NULL ,NULL ) { +break ; + }That would be the weirdest-looking code snippet in
mm/!
Quoted 2 lines
You mean the only break in a
for_eachloop? Hmm..., this is really not that nice. Haven't noticed could get a "best" in this way :-)
I guess we can make it nicer by adding a comment along the lines of
/*
* Use for_each_mem_pfn_range() to locate the lowest valid pfn in the
* range. We only need to iterate a single time, as the pfn's are
* sorted in ascending order.
*/
Because adding a call to the obviously-internal __next_mem_pfn_range() isn't very nice either.
Anyway, please have a think, see what we can come up with.
Quoted 38 lines · Andrew Morton
Quoted 31 lines · On Wed, 28 Mar 2018 08:51:42 +0800 Wei Yang <[email protected]> wrote:
Quoted 21 lines · Andrew Morton
Quoted 19 lines · On Tue, 27 Mar 2018 11:57:07 +0800 Wei Yang <[email protected]> wrote:
find_min_pfn_for_node()iterate on pfn range to find the minimum pfn for a node. Thememblock_regioninmemblock_typeare already ordered, which means the first hit in iteration is the minimum pfn.This patch returns the fist hit instead of iterating the whole regions.
...
mm/page_alloc.c +1
-2@@ -6365,14 +6365,14 @@ unsigned long __init node_map_pfn_alignment(void)/* Find the lowest pfn for a node */ static unsigned long __init find_min_pfn_for_node (int nid) { -unsigned long min_pfn =ULONG_MAX ; -unsigned long start_pfn; +unsigned long min_pfn;int i;Quoted 8 lines
mm/page_alloc.c unsigned long __init node_map_pfn_alignment(void) · line 6373
Quoted 5 lines
mm/page_alloc.c +3
-2-for_each_mem_pfn_range (i, nid, &start_pfn,NULL ,NULL ) - min_pfn =min (min_pfn, start_pfn); +for_each_mem_pfn_range (i, nid, &min_pfn,NULL ,NULL ) { +break ; + }That would be the weirdest-looking code snippet in
mm/!Quoted 2 lines
You mean the only break in a
for_eachloop? Hmm..., this is really not that nice. Haven't noticed could get a "best" in this way :-)I guess we can make it nicer by adding a comment along the lines of
Quoted 8 lines
/*
* Usefor_each_mem_pfn_range()to locate the lowest valid pfn in the
* range. We only need to iterate a single time, as the pfn's are
* sorted in ascending order.
*/Because adding a call to the obviously-internal
__next_mem_pfn_range()isn't very nice either.
Yep, you are right.
Quoted 2 lines
Anyway, please have a think, see what we can come up with.
My approach is to add a macro fist_mem_pfn() as a self-explain wrapper of __next_mem_pfn_range().
Hope you would like this :-)
Signature · 2 lines
Wei Yang Help you, Help me
PATCH 1/1 mm/page_alloc: optimize find_min_pfn_for_node() by geting the minimal pfn directly
In reply to Andrew Mortonfind_min_pfn_for_node() iterates on pfn range to find the minimum pfn for a node, while this process could be optimized. The memblock_region in memblock_type are already in ascending order, so the first one is the minimal one.
For example, if there are 30 memory regions, the original version would iterate all 30 regions, while the new version just need iterate a single time.
This patch does a trivial optimization by adding first_mem_pfn() and use this to get the minimal pfn directly.
---
v2:
* add first_mem_pfn() and use it to replace for_each_mem_pfn_ragne()
* some more meaningful words in change log
--- include/linux/memblock.h | 9 +++++++++ mm/page_alloc.c | 14 ++++++++------ 2 files changed, 17 insertions(+), 6 deletions(-)
include/linux/memblock.h +9 -0
@@ -189,6 +189,15 @@ void __next_mem_pfn_range(int *idx, int nid, unsigned long *out_start_pfn, unsigned long *out_end_pfn, int *out_nid);unsigned long memblock_next_valid_pfn (unsigned long pfn,unsigned long max_pfn); +/** +* first_mem_pfn - get the first memory pfn +* @i: an integer used as an indicator +* @nid: node selector, %MAX_NUMNODES for all nodes +* @p_first: ptr to ulong for first pfn of the range, can be %NULL +*/ +#define first_mem_pfn (i, nid, p_first) \ + __next_mem_pfn_range(&i, nid, p_first, NULL, NULL) +/** * for_each_mem_pfn_range - early memory pfn range iterator * @i: an integer used as loop variable
mm/page_alloc.c +8 -6
@@ -6365,14 +6365,16 @@ unsigned long __init node_map_pfn_alignment(void)/* Find the lowest pfn for a node */ static unsigned long __init find_min_pfn_for_node (int nid) { -unsigned long min_pfn =ULONG_MAX ; -unsigned long start_pfn; -int i; +unsigned long min_pfn; +int i =-1 ; -for_each_mem_pfn_range (i, nid, &start_pfn,NULL ,NULL ) - min_pfn =min (min_pfn, start_pfn); +/* +* The first pfn on nid node is the minimal one, as the pfn's are +* stored in ascending order. +*/ +first_mem_pfn (i, nid, &min_pfn); -if (min_pfn ==ULONG_MAX ) { +if (i ==-1 ) {pr_warn ("Could not find start_pfn for node %d\n" , nid);return 0 ; }
Signature · 1 line
2.15.1
Re: [PATCH] mm/page_alloc: optimize find_min_pfn_for_node() by geting the minimal pfn directly
On Wed 28-03-18 11:47:52, Wei Yang wrote:
[...]
include/linux/memblock.h void __next_mem_pfn_range(int *idx, int nid, unsigned long *out_start_pfn, · line 200
Quoted 9 lines
include/linux/memblock.h +9
-0+/** +* first_mem_pfn - get the first memory pfn +* @i: an integer used as an indicator +* @nid: node selector, %MAX_NUMNODES for all nodes +* @p_first: ptr to ulong for first pfn of the range, can be %NULL +*/ +#define first_mem_pfn (i, nid, p_first) \ + __next_mem_pfn_range(&i, nid, p_first, NULL, NULL) +
Is this really something that we want to export to all users? And if that is the case is the documenation really telling user how to use it?
Quoted 20 lines
/**
*for_each_mem_pfn_range- early memory pfn range iterator
* @i: an integer used as loop variablemm/page_alloc.c +3
-5@@ -6365,14 +6365,16 @@ unsigned long __init node_map_pfn_alignment(void)/* Find the lowest pfn for a node */ static unsigned long __init find_min_pfn_for_node (int nid) { -unsigned long min_pfn =ULONG_MAX ; -unsigned long start_pfn; -int i; +unsigned long min_pfn; +int i =-1 ; -for_each_mem_pfn_range (i, nid, &start_pfn,NULL ,NULL ) - min_pfn =min (min_pfn, start_pfn); +/*
mm/page_alloc.c unsigned long __init node_map_pfn_alignment(void) · line 6380
Quoted 10 lines
mm/page_alloc.c +5
-1+* The first pfn on nid node is the minimal one, as the pfn's are +* stored in ascending order. +*/ +first_mem_pfn (i, nid, &min_pfn); -if (min_pfn ==ULONG_MAX ) { +if (i ==-1 ) {pr_warn ("Could not find start_pfn for node %d\n" , nid);return 0 ; }
I would just open code it. Other than that I strongly suspect this will not have any measurable impact becauser we usually only have handfull of memory ranges but why not. Just make the new implementation less ugly than it is cuurrently - e.g. opencode first_mem_pfn and you can add
Signature · 2 lines
Michal Hocko SUSE Labs
Re: [PATCH] mm/page_alloc: optimize find_min_pfn_for_node() by geting the minimal pfn directly
Quoted 15 lines · Michal Hocko
On Wed 28-03-18 11:47:52, Wei Yang wrote:
[...]include/linux/memblock.h void __next_mem_pfn_range(int *idx, int nid, unsigned long *out_start_pfn, · line 200
Quoted 9 lines
include/linux/memblock.h +9
-0+/** +* first_mem_pfn - get the first memory pfn +* @i: an integer used as an indicator +* @nid: node selector, %MAX_NUMNODES for all nodes +* @p_first: ptr to ulong for first pfn of the range, can be %NULL +*/ +#define first_mem_pfn (i, nid, p_first) \ + __next_mem_pfn_range(&i, nid, p_first, NULL, NULL) +Is this really something that we want to export to all users? And if that is the case is the documenation really telling user how to use it?
Yep, I am not good at the documentation. I really struggled a while on it, but you know it still looks not that good.
How about changing the document to the following in case this macro is still alive.
/**
* first_mem_pfn - get the first memory pfn after index i on node nid
* @i: index to memblock.memory.regions
* @nid: node selector, %MAX_NUMNODES for all nodes
* @p_first: ptr to ulong for first pfn of the range, can be %NULL */
Quoted 25 lines
Quoted 25 lines
/**
*for_each_mem_pfn_range- early memory pfn range iterator
* @i: an integer used as loop variablemm/page_alloc.c +7
-5@@ -6365,14 +6365,16 @@ unsigned long __init node_map_pfn_alignment(void)/* Find the lowest pfn for a node */ static unsigned long __init find_min_pfn_for_node (int nid) { -unsigned long min_pfn =ULONG_MAX ; -unsigned long start_pfn; -int i; +unsigned long min_pfn; +int i =-1 ; -for_each_mem_pfn_range (i, nid, &start_pfn,NULL ,NULL ) - min_pfn =min (min_pfn, start_pfn); +/* +* The first pfn on nid node is the minimal one, as the pfn's are +* stored in ascending order. +*/ +first_mem_pfn (i, nid, &min_pfn);
Quoted 10 lines
mm/page_alloc.c unsigned long __init node_map_pfn_alignment(void) · line 6380
Quoted 5 lines
mm/page_alloc.c +1
-1-if (min_pfn ==ULONG_MAX ) { +if (i ==-1 ) {pr_warn ("Could not find start_pfn for node %d\n" , nid);return 0 ; }I would just open code it. Other than that I strongly suspect this will not have any measurable impact becauser we usually only have handfull of memory ranges but why not. Just make the new implementation less ugly than it is cuurrently - e.g. opencode
first_mem_pfnand you can add
Open code here means use __next_mem_pfn_range() directly instead of using first_mem_pfn()?
Quoted 4 lines
Acked-by Michal Hocko <[email protected]>Signature · 2 lines
Michal Hocko SUSE Labs
Signature · 2 lines
Wei Yang Help you, Help me
Re: [PATCH] mm/page_alloc: optimize find_min_pfn_for_node() by geting the minimal pfn directly
Quoted 3 lines · Wei Yang
Quoted 2 lines · Michal Hocko
On Wed 28-03-18 11:47:52, Wei Yang wrote:
[...]
[...]
Quoted 29 lines
Quoted 18 lines
Quoted 18 lines
mm/page_alloc.c +7
-5@@ -6365,14 +6365,16 @@ unsigned long __init node_map_pfn_alignment(void)/* Find the lowest pfn for a node */ static unsigned long __init find_min_pfn_for_node (int nid) { -unsigned long min_pfn =ULONG_MAX ; -unsigned long start_pfn; -int i; +unsigned long min_pfn; +int i =-1 ; -for_each_mem_pfn_range (i, nid, &start_pfn,NULL ,NULL ) - min_pfn =min (min_pfn, start_pfn); +/* +* The first pfn on nid node is the minimal one, as the pfn's are +* stored in ascending order. +*/ +first_mem_pfn (i, nid, &min_pfn);Quoted 10 lines
mm/page_alloc.c unsigned long __init node_map_pfn_alignment(void) · line 6380
Quoted 5 lines
mm/page_alloc.c +1
-1-if (min_pfn ==ULONG_MAX ) { +if (i ==-1 ) {pr_warn ("Could not find start_pfn for node %d\n" , nid);return 0 ; }I would just open code it. Other than that I strongly suspect this will not have any measurable impact becauser we usually only have handfull of memory ranges but why not. Just make the new implementation less ugly than it is cuurrently - e.g. opencode
first_mem_pfnand you can add
Quoted 2 lines
Open code here means use
__next_mem_pfn_range()directly instead of usingfirst_mem_pfn()?
Yes with the comment explaining how we rely on sorted ranges the way you did.
Signature · 2 lines
Michal Hocko SUSE Labs