mm: Micro-optimisation: Save two branches on hot - page allocation path
Subsystems (3)
RFC PATCH 1/1 mm: Micro-optimisation: Save two branches on hot - page allocation path
From: Mateusz Nosek <[email protected]>
This patch makes ALLOC_KSWAPD
equal to __GFP_KSWAPD_RACLAIM (cast to 'int').
Thanks to that code like:
```if (gfp_mask & __GFP_KSWAPD_RECLAIM) alloc_flags |= ALLOC_KSWAPD;``` can be changed to: ```alloc_flags |= (__force int) (gfp_mask &__GFP_KSWAPD_RECLAIM);```
Thanks to this one branch less is generated in the assembly.
In case of ALLOC_KSWAPD flag two branches are saved,
first one in code that always executes in the beggining of page allocation and the second one in loop in page allocator slowpath.
--- mm/internal.h | 2 +- mm/page_alloc.c | 23 +++++++++++++++-------- 2 files changed, 16 insertions(+), 9 deletions(-)
mm/internal.h +1 -1
@@ -535,7 +535,7 @@ unsigned long reclaim_clean_pages_from_list(struct zone *zone,#else #define ALLOC_NOFRAGMENT 0x0#endif -#define ALLOC_KSWAPD 0x200/* allow waking of kswapd */ +#define ALLOC_KSWAPD 0x800/* allow waking of kswapd */ enum ttu_flags ;struct tlbflush_unmap_batch ;
mm/page_alloc.c +15 -8
@@ -3609,10 +3609,14 @@ static bool zone_allows_reclaim(struct zone *local_zone, struct zone *zone)static inline unsigned int alloc_flags_nofragment (struct zone *zone,gfp_t gfp_mask) { -unsigned int alloc_flags =0 ; +unsigned int alloc_flags; -if (gfp_mask & __GFP_KSWAPD_RECLAIM) - alloc_flags |=ALLOC_KSWAPD ; +/* +* __GFP_KSWAPD_RECLAIM is assumed to be the same as ALLOC_KSWAPD +* to save a branch. +*/ +BUILD_BUG_ON (__GFP_KSWAPD_RECLAIM != (__force gfp_t )ALLOC_KSWAPD ); + alloc_flags = (__force int ) (gfp_mask & __GFP_KSWAPD_RECLAIM);#ifdef CONFIG_ZONE_DMA32 if (!zone)
@@ -4248,8 +4252,13 @@ gfp_to_alloc_flags(gfp_t gfp_mask) {unsigned int alloc_flags =ALLOC_WMARK_MIN |ALLOC_CPUSET ; -/* __GFP_HIGH is assumed to be the same as ALLOC_HIGH to save a branch. */ +/* +* __GFP_HIGH is assumed to be the same as ALLOC_HIGH +* and __GFP_KSWAPD_RECLAIM is assumed to be the same as ALLOC_KSWAPD +* to save two branches. +*/ BUILD_BUG_ON (__GFP_HIGH != (__force gfp_t )ALLOC_HIGH ); +BUILD_BUG_ON (__GFP_KSWAPD_RECLAIM != (__force gfp_t )ALLOC_KSWAPD );/* * The caller may dip into page reserves a bit more if the caller
@@ -4257,7 +4266,8 @@ gfp_to_alloc_flags(gfp_t gfp_mask)* policy or is asking for __GFP_HIGH memory. GFP_ATOMIC requests will * set both ALLOC_HARDER (__GFP_ATOMIC) and ALLOC_HIGH (__GFP_HIGH). */ - alloc_flags |= (__force int ) (gfp_mask & __GFP_HIGH); + alloc_flags |= (__force int ) + (gfp_mask & (__GFP_HIGH | __GFP_KSWAPD_RECLAIM));if (gfp_mask & __GFP_ATOMIC) {/*
@@ -4274,9 +4284,6 @@ gfp_to_alloc_flags(gfp_t gfp_mask) }else if (unlikely (rt_task (current)) && !in_interrupt ()) alloc_flags |=ALLOC_HARDER ; -if (gfp_mask & __GFP_KSWAPD_RECLAIM) - alloc_flags |=ALLOC_KSWAPD ; -#ifdef CONFIG_CMA if (gfpflags_to_migratetype (gfp_mask) ==MIGRATE_MOVABLE ) alloc_flags |=ALLOC_CMA ;
Signature · 1 line
2.17.1
Let's CC Mel.
Quoted 10 lines · On 3/4/20 3:22 PM, [email protected] wrote:
From: Mateusz Nosek <[email protected]>
This patch makes
ALLOC_KSWAPD
equal to__GFP_KSWAPD_RACLAIM(cast to 'int').Thanks to that code like:
```if (gfp_mask & __GFP_KSWAPD_RECLAIM) alloc_flags |= ALLOC_KSWAPD;``` can be changed to: ```alloc_flags |= (__force int) (gfp_mask &__GFP_KSWAPD_RECLAIM);```
Quoted 7 lines
Thanks to this one branch less is generated in the assembly.
In case of
ALLOC_KSWAPDflag two branches are saved,
first one in code that always executes in the beggining of page allocation and the second one in loop in page allocator slowpath.Signed-off-by Mateusz Nosek <[email protected]>
I think it's fine and in line with similar optimisations done by Mel in the past.
Some nits below.
Quoted 10 lines
--- mm/internal.h | 2 +- mm/page_alloc.c | 23 +++++++++++++++-------- 2 files changed, 16 insertions(+), 9 deletions(-)mm/internal.h +0
-0
mm/internal.h unsigned long reclaim_clean_pages_from_list(struct zone *zone, · line 538
Quoted 5 lines
mm/internal.h +1
-1@@ -535,7 +535,7 @@ unsigned long reclaim_clean_pages_from_list(struct zone *zone,#else #define ALLOC_NOFRAGMENT 0x0#endif -#define ALLOC_KSWAPD 0x200/* allow waking of kswapd */ +#define ALLOC_KSWAPD 0x800/* allow waking of kswapd */
Add a comment that the value has to match the GFP flag?
Quoted 11 lines
mm/internal.h +0
-0enum ttu_flags ;struct tlbflush_unmap_batch ;mm/page_alloc.c +0
-0@@ -3609,10 +3609,14 @@ static bool zone_allows_reclaim(struct zone *local_zone, struct zone *zone)static inline unsigned int alloc_flags_nofragment (struct zone *zone,gfp_t gfp_mask) {
mm/page_alloc.c static bool zone_allows_reclaim(struct zone *local_zone, struct zone *zone) · line 3618
Quoted 10 lines
mm/page_alloc.c +6
-3-unsigned int alloc_flags =0 ; +unsigned int alloc_flags; -if (gfp_mask & __GFP_KSWAPD_RECLAIM) - alloc_flags |=ALLOC_KSWAPD ; +/* +* __GFP_KSWAPD_RECLAIM is assumed to be the same as ALLOC_KSWAPD +* to save a branch. +*/ +BUILD_BUG_ON (__GFP_KSWAPD_RECLAIM != (__force gfp_t )ALLOC_KSWAPD );
I think one BUILD_BUG_ON is enough...
Quoted 6 lines
mm/page_alloc.c +1
-0+ alloc_flags = (__force int ) (gfp_mask & __GFP_KSWAPD_RECLAIM);#ifdef CONFIG_ZONE_DMA32 if (!zone)@@ -4248,8 +4252,13 @@ gfp_to_alloc_flags(gfp_t gfp_mask) {
mm/page_alloc.c gfp_to_alloc_flags(gfp_t gfp_mask) · line 4261
Quoted 10 lines
mm/page_alloc.c +6
-1unsigned int alloc_flags =ALLOC_WMARK_MIN |ALLOC_CPUSET ; -/* __GFP_HIGH is assumed to be the same as ALLOC_HIGH to save a branch. */ +/* +* __GFP_HIGH is assumed to be the same as ALLOC_HIGH +* and __GFP_KSWAPD_RECLAIM is assumed to be the same as ALLOC_KSWAPD +* to save two branches. +*/ BUILD_BUG_ON (__GFP_HIGH != (__force gfp_t )ALLOC_HIGH ); +BUILD_BUG_ON (__GFP_KSWAPD_RECLAIM != (__force gfp_t )ALLOC_KSWAPD );
... and this would be the better one of the two.
Thanks.
Quoted 24 lines
mm/page_alloc.c +2
-4/* * The caller may dip into page reserves a bit more if the caller @@ -4257,7 +4266,8 @@ gfp_to_alloc_flags(gfp_t gfp_mask)* policy or is asking for __GFP_HIGH memory. GFP_ATOMIC requests will * set both ALLOC_HARDER (__GFP_ATOMIC) and ALLOC_HIGH (__GFP_HIGH). */ - alloc_flags |= (__force int ) (gfp_mask & __GFP_HIGH); + alloc_flags |= (__force int ) + (gfp_mask & (__GFP_HIGH | __GFP_KSWAPD_RECLAIM));if (gfp_mask & __GFP_ATOMIC) {/* @@ -4274,9 +4284,6 @@ gfp_to_alloc_flags(gfp_t gfp_mask) }else if (unlikely (rt_task (current)) && !in_interrupt ()) alloc_flags |=ALLOC_HARDER ; -if (gfp_mask & __GFP_KSWAPD_RECLAIM) - alloc_flags |=ALLOC_KSWAPD ; -#ifdef CONFIG_CMA if (gfpflags_to_migratetype (gfp_mask) ==MIGRATE_MOVABLE ) alloc_flags |=ALLOC_CMA ;
Quoted 5 lines · On Wed, Mar 04, 2020 at 03:22:30PM +0100, [email protected] wrote:
From: Mateusz Nosek <[email protected]>
This patch makes
ALLOC_KSWAPD
equal to__GFP_KSWAPD_RACLAIM(cast to 'int').
s/RACLAIM/RECLAIM/
Very strange word wrapping
Quoted 5 lines
Thanks to that code like:
```if (gfp_mask & __GFP_KSWAPD_RECLAIM) alloc_flags |= ALLOC_KSWAPD;``` can be changed to: ```alloc_flags |= (__force int) (gfp_mask &__GFP_KSWAPD_RECLAIM);```
Not sure what the multiple ``` are about. It does not appear to be an encoding error but I think you can drop them. Maybe some mail readers render this differently but it looks weird in plain text.
Quoted 4 lines
Thanks to this one branch less is generated in the assembly.
In case of
ALLOC_KSWAPDflag two branches are saved,
first one in code that always executes in the beggining of page allocation
s/beggining/beginning/
Quoted 1 line
and the second one in loop in page allocator slowpath.
Also strange word wrapping.
Quoted 32 lines
Signed-off-by Mateusz Nosek <[email protected]>--- mm/internal.h | 2 +- mm/page_alloc.c | 23 +++++++++++++++-------- 2 files changed, 16 insertions(+), 9 deletions(-)mm/internal.h +1
-1@@ -535,7 +535,7 @@ unsigned long reclaim_clean_pages_from_list(struct zone *zone,#else #define ALLOC_NOFRAGMENT 0x0#endif -#define ALLOC_KSWAPD 0x200/* allow waking of kswapd */ +#define ALLOC_KSWAPD 0x800/* allow waking of kswapd */ enum ttu_flags ;struct tlbflush_unmap_batch ;mm/page_alloc.c +1
-2@@ -3609,10 +3609,14 @@ static bool zone_allows_reclaim(struct zone *local_zone, struct zone *zone)static inline unsigned int alloc_flags_nofragment (struct zone *zone,gfp_t gfp_mask) { -unsigned int alloc_flags =0 ; +unsigned int alloc_flags; -if (gfp_mask & __GFP_KSWAPD_RECLAIM)
mm/page_alloc.c static bool zone_allows_reclaim(struct zone *local_zone, struct zone *zone) · line 3622
Quoted 10 lines
mm/page_alloc.c +6
-1- alloc_flags |=ALLOC_KSWAPD ; +/* +* __GFP_KSWAPD_RECLAIM is assumed to be the same as ALLOC_KSWAPD +* to save a branch. +*/ +BUILD_BUG_ON (__GFP_KSWAPD_RECLAIM != (__force gfp_t )ALLOC_KSWAPD ); + alloc_flags = (__force int ) (gfp_mask & __GFP_KSWAPD_RECLAIM);#ifdef CONFIG_ZONE_DMA32 if (!zone)
Ok, that looks reasonable.
Quoted 5 lines
mm/page_alloc.c +0
-1@@ -4248,8 +4252,13 @@ gfp_to_alloc_flags(gfp_t gfp_mask) {unsigned int alloc_flags =ALLOC_WMARK_MIN |ALLOC_CPUSET ; -/* __GFP_HIGH is assumed to be the same as ALLOC_HIGH to save a branch. */
mm/page_alloc.c gfp_to_alloc_flags(gfp_t gfp_mask) · line 4264
Quoted 10 lines
mm/page_alloc.c +6
-0+/* +* __GFP_HIGH is assumed to be the same as ALLOC_HIGH +* and __GFP_KSWAPD_RECLAIM is assumed to be the same as ALLOC_KSWAPD +* to save two branches. +*/ BUILD_BUG_ON (__GFP_HIGH != (__force gfp_t )ALLOC_HIGH ); +BUILD_BUG_ON (__GFP_KSWAPD_RECLAIM != (__force gfp_t )ALLOC_KSWAPD );/* * The caller may dip into page reserves a bit more if the caller
As Vlastimil pointed out, you do not need to make two compiler-based checks. This seems like a reasonable location given that gfp_to_alloc_flags is the most obvious place to document tricks with the flags.
mm/page_alloc.c gfp_to_alloc_flags(gfp_t gfp_mask) · line 4273
Quoted 10 lines
mm/page_alloc.c +2
-1@@ -4257,7 +4266,8 @@ gfp_to_alloc_flags(gfp_t gfp_mask)* policy or is asking for __GFP_HIGH memory. GFP_ATOMIC requests will * set both ALLOC_HARDER (__GFP_ATOMIC) and ALLOC_HIGH (__GFP_HIGH). */ - alloc_flags |= (__force int ) (gfp_mask & __GFP_HIGH); + alloc_flags |= (__force int ) + (gfp_mask & (__GFP_HIGH | __GFP_KSWAPD_RECLAIM));if (gfp_mask & __GFP_ATOMIC) {/*
Slightly harder to read but it does potentially generate better code.
If you correct the typos in the changelog, the slightly strange formatting of the changelog and drop one of the build checks then you can add
Signature · 2 lines
Mel Gorman SUSE Labs