disable pcplists during memory offline
Subsystems (5)
cover disable pcplists during memory offline
As per the discussions [1] [2] this is an attempt to implement David's suggestion that page isolation should disable pcplists to avoid races with page freeing in progress. This is done without extra checks in fast paths, as explained in Patch 9. The repeated draining done by [2] is then no longer needed. Previous version (RFC) is at [3].
The RFC tried to hide pcplists disabling/enabling into page isolation, but it wasn't completely possible, as memory offline does not unisolation. Michal suggested an explicit API in [4] so that's the current implementation and it seems indeed nicer.
Once we accept that page isolation users need to do explicit actions around it depending on the needed guarantees, we can also IMHO accept that the current pcplist draining can be also done by the callers, which is more effective. After all, there are only two users of page isolation. So patch 7 does effectively the same thing as Pavel proposed in [5], and patches 8-9 implement stronger guarantees only for memory offline. If CMA decides to opt-in to the stronger guarantee, it's easy to do so.
Patches 1-6 are preparatory cleanups for pcplist disabling.
Patchset was briefly tested in QEMU so that memory online/offline works, but I haven't done a stress test that would prove the race fixed by [2] is eliminated.
Note that patch 9 could be avoided if we instead adjusted page freeing in shown in [6], but I believe the current implementation of disabling pcplists is not too much complex, so I would prefer this instead of adding new checks and longer irq-disabled section into page freeing hotpaths.
[1] https://lore.kernel.org/linux-mm/[email protected]/
[2] https://lore.kernel.org/linux-mm/[email protected]/
[3] https://lore.kernel.org/linux-mm/[email protected]/
[4] https://lore.kernel.org/linux-mm/[email protected]/
[5] https://lore.kernel.org/linux-mm/[email protected]/
[6] https://lore.kernel.org/linux-mm/[email protected]/
Vlastimil Babka (9):
mm, page_alloc: clean up pageset high and batch update mm, page_alloc: calculate pageset high and batch once per zone mm, page_alloc: remove setup_pageset()
mm, page_alloc: simplify pageset_update()
mm, page_alloc: make per_cpu_pageset accessible only after init mm, page_alloc: cache pageset high and batch in struct zone mm, page_alloc: move draining pcplists to page isolation users mm, page_alloc: drain all pcplists during memory offline mm, page_alloc: optionally disable pcplists during page isolation
include/linux/gfp.h | 1 + include/linux/mmzone.h | 8 ++ include/linux/page-isolation.h | 2 + mm/internal.h | 4 + mm/memory_hotplug.c | 27 +++-- mm/page_alloc.c | 190 ++++++++++++++++++--------------- mm/page_isolation.c | 26 ++++- 7 files changed, 152 insertions(+), 106 deletions(-)
Signature · 1 line
2.28.0
PATCH 1/9 mm, page_alloc: clean up pageset high and batch update
The updates to pcplists' high and batch valued are handled by multiple functions that make the calculations hard to follow. Consolidate everything to pageset_set_high_and_batch() and remove pageset_set_batch() and pageset_set_high() wrappers.
The only special case using one of the removed wrappers was:build_all_zonelists_init()
setup_pageset()
pageset_set_batch()
which was hardcoding batch as 0, so we can just open-code a call to pageset_update() with constant parameters instead.
No functional change.
--- mm/page_alloc.c | 49 ++++++++++++++++++++----------------------------- 1 file changed, 20 insertions(+), 29 deletions(-)
mm/page_alloc.c +20 -29
@@ -5823,7 +5823,7 @@ static void build_zonelists(pg_data_t *pgdat)* not check if the processor is online before following the pageset pointer. * Other parts of the kernel may not check if the zone is available. */ -static void setup_pageset (struct per_cpu_pageset *p,unsigned long batch); +static void setup_pageset (struct per_cpu_pageset *p); static DEFINE_PER_CPU(struct per_cpu_pageset, boot_pageset); static DEFINE_PER_CPU(struct per_cpu_nodestat, boot_nodestats);
@@ -5891,7 +5891,7 @@ build_all_zonelists_init(void)* (a chicken-egg dilemma). */ for_each_possible_cpu (cpu) -setup_pageset (&per_cpu (boot_pageset, cpu),0 ); +setup_pageset (&per_cpu (boot_pageset, cpu));mminit_verify_zonelist ();cpuset_init_current_mems_allowed ();
@@ -6200,12 +6200,6 @@ static void pageset_update(struct per_cpu_pages *pcp, unsigned long high, pcp->batch = batch; } -/* a companion to pageset_set_high() */ -static void pageset_set_batch (struct per_cpu_pageset *p,unsigned long batch) -{ -pageset_update (&p->pcp,6 * batch,max (1UL ,1 * batch)); -} -static void pageset_init (struct per_cpu_pageset *p) {struct per_cpu_pages *pcp;
@@ -6218,35 +6212,32 @@ static void pageset_init(struct per_cpu_pageset *p)INIT_LIST_HEAD (&pcp->lists[migratetype]); } -static void setup_pageset (struct per_cpu_pageset *p,unsigned long batch) +static void setup_pageset (struct per_cpu_pageset *p) {pageset_init (p); -pageset_set_batch (p, batch); +pageset_update (&p->pcp,0 ,1 ); }/* -* pageset_set_high() sets the high water mark for hot per_cpu_pagelist -* to the value high for the pageset p. +* Calculate and set new high and batch values for given per-cpu pageset of a +* zone, based on the zone's size and the percpu_pagelist_fraction sysctl. */ -static void pageset_set_high (struct per_cpu_pageset *p, -unsigned long high) -{ -unsigned long batch =max (1UL , high /4 ); -if ((high /4 ) > (PAGE_SHIFT *8 )) - batch =PAGE_SHIFT *8 ; - -pageset_update (&p->pcp, high, batch); -} -static void pageset_set_high_and_batch (struct zone *zone, -struct per_cpu_pageset *pcp) +struct per_cpu_pageset *p) { -if (percpu_pagelist_fraction) -pageset_set_high (pcp, - (zone_managed_pages (zone) / - percpu_pagelist_fraction)); -else -pageset_set_batch (pcp,zone_batchsize (zone)); +unsigned long new_high, new_batch; + +if (percpu_pagelist_fraction) { + new_high =zone_managed_pages (zone) / percpu_pagelist_fraction; + new_batch =max (1UL , new_high /4 ); +if ((new_high /4 ) > (PAGE_SHIFT *8 )) + new_batch =PAGE_SHIFT *8 ; + }else { + new_batch =zone_batchsize (zone); + new_high =6 * new_batch; + new_batch =max (1UL ,1 * new_batch); + } +pageset_update (&p->pcp, new_high, new_batch); }static void __meminit zone_pageset_init (struct zone *zone,int cpu)
Signature · 1 line
2.28.0
Re: [PATCH 1/9] mm, page_alloc: clean up pageset high and batch update
Quoted 100 lines · Vlastimil Babka
The updates to pcplists' high and batch valued are handled by multiple functions that make the calculations hard to follow. Consolidate everything to
pageset_set_high_and_batch()and removepageset_set_batch()andpageset_set_high()wrappers.The only special case using one of the removed wrappers was:
build_all_zonelists_init()
setup_pageset()
pageset_set_batch()
which was hardcoding batch as 0, so we can just open-code a call topageset_update()with constant parameters instead.No functional change.
Signed-off-by Vlastimil Babka <[email protected]>Reviewed-by Oscar Salvador <[email protected]>--- mm/page_alloc.c | 49 ++++++++++++++++++++----------------------------- 1 file changed, 20 insertions(+), 29 deletions(-)mm/page_alloc.c +13
-29@@ -5823,7 +5823,7 @@ static void build_zonelists(pg_data_t *pgdat)* not check if the processor is online before following the pageset pointer. * Other parts of the kernel may not check if the zone is available. */ -static void setup_pageset (struct per_cpu_pageset *p,unsigned long batch); +static void setup_pageset (struct per_cpu_pageset *p); static DEFINE_PER_CPU(struct per_cpu_pageset, boot_pageset); static DEFINE_PER_CPU(struct per_cpu_nodestat, boot_nodestats);@@ -5891,7 +5891,7 @@ build_all_zonelists_init(void)* (a chicken-egg dilemma). */ for_each_possible_cpu (cpu) -setup_pageset (&per_cpu (boot_pageset, cpu),0 ); +setup_pageset (&per_cpu (boot_pageset, cpu));mminit_verify_zonelist ();cpuset_init_current_mems_allowed ();@@ -6200,12 +6200,6 @@ static void pageset_update(struct per_cpu_pages *pcp, unsigned long high, pcp->batch = batch; } -/* a companion to pageset_set_high() */ -static void pageset_set_batch (struct per_cpu_pageset *p,unsigned long batch) -{ -pageset_update (&p->pcp,6 * batch,max (1UL ,1 * batch)); -} -static void pageset_init (struct per_cpu_pageset *p) {struct per_cpu_pages *pcp;@@ -6218,35 +6212,32 @@ static void pageset_init(struct per_cpu_pageset *p)INIT_LIST_HEAD (&pcp->lists[migratetype]); } -static void setup_pageset (struct per_cpu_pageset *p,unsigned long batch) +static void setup_pageset (struct per_cpu_pageset *p) {pageset_init (p); -pageset_set_batch (p, batch); +pageset_update (&p->pcp,0 ,1 ); }/* -* pageset_set_high() sets the high water mark for hot per_cpu_pagelist -* to the value high for the pageset p. +* Calculate and set new high and batch values for given per-cpu pageset of a +* zone, based on the zone's size and the percpu_pagelist_fraction sysctl. */ -static void pageset_set_high (struct per_cpu_pageset *p, -unsigned long high) -{ -unsigned long batch =max (1UL , high /4 ); -if ((high /4 ) > (PAGE_SHIFT *8 )) - batch =PAGE_SHIFT *8 ; - -pageset_update (&p->pcp, high, batch); -} -static void pageset_set_high_and_batch (struct zone *zone, -struct per_cpu_pageset *pcp) +struct per_cpu_pageset *p) { -if (percpu_pagelist_fraction) -pageset_set_high (pcp, - (zone_managed_pages (zone) / - percpu_pagelist_fraction)); -else -pageset_set_batch (pcp,zone_batchsize (zone)); +unsigned long new_high, new_batch; + +if (percpu_pagelist_fraction) { + new_high =zone_managed_pages (zone) / percpu_pagelist_fraction; + new_batch =max (1UL , new_high /4 ); +if ((new_high /4 ) > (PAGE_SHIFT *8 ))
mm/page_alloc.c static void pageset_init(struct per_cpu_pageset *p) · line 6243
Quoted 10 lines
mm/page_alloc.c +7
-0+ new_batch =PAGE_SHIFT *8 ; + }else { + new_batch =zone_batchsize (zone); + new_high =6 * new_batch; + new_batch =max (1UL ,1 * new_batch); + } +pageset_update (&p->pcp, new_high, new_batch); }static void __meminit zone_pageset_init (struct zone *zone,int cpu)
Signature · 3 lines
Thanks, David / dhildenb
Re: [PATCH 1/9] mm, page_alloc: clean up pageset high and batch update
In reply to Vlastimil BabkaQuoted 12 lines · Vlastimil Babka
The updates to pcplists' high and batch valued are handled by multiple functions that make the calculations hard to follow. Consolidate everything to
pageset_set_high_and_batch()and removepageset_set_batch()andpageset_set_high()wrappers.The only special case using one of the removed wrappers was:
build_all_zonelists_init()
setup_pageset()
pageset_set_batch()
which was hardcoding batch as 0, so we can just open-code a call topageset_update()with constant parameters instead.
Quoted 4 lines
No functional change.
Signed-off-by Vlastimil Babka <[email protected]>Reviewed-by Oscar Salvador <[email protected]>
yes this looks better, the original code was really hard to follow.
Quoted 95 lines
--- mm/page_alloc.c | 49 ++++++++++++++++++++----------------------------- 1 file changed, 20 insertions(+), 29 deletions(-)mm/page_alloc.c +20
-29@@ -5823,7 +5823,7 @@ static void build_zonelists(pg_data_t *pgdat)* not check if the processor is online before following the pageset pointer. * Other parts of the kernel may not check if the zone is available. */ -static void setup_pageset (struct per_cpu_pageset *p,unsigned long batch); +static void setup_pageset (struct per_cpu_pageset *p); static DEFINE_PER_CPU(struct per_cpu_pageset, boot_pageset); static DEFINE_PER_CPU(struct per_cpu_nodestat, boot_nodestats);@@ -5891,7 +5891,7 @@ build_all_zonelists_init(void)* (a chicken-egg dilemma). */ for_each_possible_cpu (cpu) -setup_pageset (&per_cpu (boot_pageset, cpu),0 ); +setup_pageset (&per_cpu (boot_pageset, cpu));mminit_verify_zonelist ();cpuset_init_current_mems_allowed ();@@ -6200,12 +6200,6 @@ static void pageset_update(struct per_cpu_pages *pcp, unsigned long high, pcp->batch = batch; } -/* a companion to pageset_set_high() */ -static void pageset_set_batch (struct per_cpu_pageset *p,unsigned long batch) -{ -pageset_update (&p->pcp,6 * batch,max (1UL ,1 * batch)); -} -static void pageset_init (struct per_cpu_pageset *p) {struct per_cpu_pages *pcp;@@ -6218,35 +6212,32 @@ static void pageset_init(struct per_cpu_pageset *p)INIT_LIST_HEAD (&pcp->lists[migratetype]); } -static void setup_pageset (struct per_cpu_pageset *p,unsigned long batch) +static void setup_pageset (struct per_cpu_pageset *p) {pageset_init (p); -pageset_set_batch (p, batch); +pageset_update (&p->pcp,0 ,1 ); }/* -* pageset_set_high() sets the high water mark for hot per_cpu_pagelist -* to the value high for the pageset p. +* Calculate and set new high and batch values for given per-cpu pageset of a +* zone, based on the zone's size and the percpu_pagelist_fraction sysctl. */ -static void pageset_set_high (struct per_cpu_pageset *p, -unsigned long high) -{ -unsigned long batch =max (1UL , high /4 ); -if ((high /4 ) > (PAGE_SHIFT *8 )) - batch =PAGE_SHIFT *8 ; - -pageset_update (&p->pcp, high, batch); -} -static void pageset_set_high_and_batch (struct zone *zone, -struct per_cpu_pageset *pcp) +struct per_cpu_pageset *p) { -if (percpu_pagelist_fraction) -pageset_set_high (pcp, - (zone_managed_pages (zone) / - percpu_pagelist_fraction)); -else -pageset_set_batch (pcp,zone_batchsize (zone)); +unsigned long new_high, new_batch; + +if (percpu_pagelist_fraction) { + new_high =zone_managed_pages (zone) / percpu_pagelist_fraction; + new_batch =max (1UL , new_high /4 ); +if ((new_high /4 ) > (PAGE_SHIFT *8 )) + new_batch =PAGE_SHIFT *8 ; + }else { + new_batch =zone_batchsize (zone); + new_high =6 * new_batch; + new_batch =max (1UL ,1 * new_batch); + } +pageset_update (&p->pcp, new_high, new_batch); }static void __meminit zone_pageset_init (struct zone *zone,int cpu)Signature · 1 line
2.28.0
Signature · 2 lines
Michal Hocko SUSE Labs
PATCH 2/9 mm, page_alloc: calculate pageset high and batch once per zone
In reply to Vlastimil BabkaWe currently call pageset_set_high_and_batch() for each possible cpu, which repeats the same calculations of high and batch values.
Instead call the function just once per zone, and make it apply the calculated values to all per-cpu pagesets of the zone.
This also allows removing the zone_pageset_init() and __zone_pcp_update() wrappers.
No functional change.
--- mm/page_alloc.c | 42 ++++++++++++++++++------------------------ 1 file changed, 18 insertions(+), 24 deletions(-)
mm/page_alloc.c +18 -24
@@ -6219,13 +6219,14 @@ static void setup_pageset(struct per_cpu_pageset *p) }/* -* Calculate and set new high and batch values for given per-cpu pageset of a +* Calculate and set new high and batch values for all per-cpu pagesets of a * zone, based on the zone's size and the percpu_pagelist_fraction sysctl. */ -static void pageset_set_high_and_batch (struct zone *zone, -struct per_cpu_pageset *p) +static void zone_set_pageset_high_and_batch (struct zone *zone) {unsigned long new_high, new_batch; +struct per_cpu_pageset *p; +int cpu;if (percpu_pagelist_fraction) { new_high =zone_managed_pages (zone) / percpu_pagelist_fraction;
@@ -6237,23 +6238,25 @@ static void pageset_set_high_and_batch(struct zone *zone, new_high =6 * new_batch; new_batch =max (1UL ,1 * new_batch); } -pageset_update (&p->pcp, new_high, new_batch); -} - -static void __meminit zone_pageset_init (struct zone *zone,int cpu) -{ -struct per_cpu_pageset *pcp =per_cpu_ptr (zone->pageset, cpu); -pageset_init (pcp); -pageset_set_high_and_batch (zone, pcp); +for_each_possible_cpu (cpu) { + p =per_cpu_ptr (zone->pageset, cpu); +pageset_update (&p->pcp, new_high, new_batch); + } }void __meminit setup_zone_pageset (struct zone *zone) { +struct per_cpu_pageset *p;int cpu; + zone->pageset = alloc_percpu(struct per_cpu_pageset); -for_each_possible_cpu (cpu) -zone_pageset_init (zone, cpu); +for_each_possible_cpu (cpu) { + p =per_cpu_ptr (zone->pageset, cpu); +pageset_init (p); + } + +zone_set_pageset_high_and_batch (zone); }/*
@@ -7985,15 +7988,6 @@ int lowmem_reserve_ratio_sysctl_handler(struct ctl_table *table, int write,return 0 ; } -static void __zone_pcp_update (struct zone *zone) -{ -unsigned int cpu; - -for_each_possible_cpu (cpu) -pageset_set_high_and_batch (zone, -per_cpu_ptr (zone->pageset, cpu)); -} -/* * percpu_pagelist_fraction - changes the pcp->high for each zone on each * cpu. It is the fraction of total pages in each zone that a hot per cpu
@@ -8026,7 +8020,7 @@ int percpu_pagelist_fraction_sysctl_handler(struct ctl_table *table, int write, goto out;for_each_populated_zone (zone) -__zone_pcp_update (zone); +zone_set_pageset_high_and_batch (zone); out:mutex_unlock (&pcp_batch_high_lock);return ret;
@@ -8633,7 +8627,7 @@ EXPORT_SYMBOL(free_contig_range);void __meminit zone_pcp_update (struct zone *zone) {mutex_lock (&pcp_batch_high_lock); -__zone_pcp_update (zone); +zone_set_pageset_high_and_batch (zone);mutex_unlock (&pcp_batch_high_lock); }
Signature · 1 line
2.28.0
Re: [PATCH 2/9] mm, page_alloc: calculate pageset high and batch once per zone
Quoted 6 lines · Vlastimil Babka
We currently call
pageset_set_high_and_batch()for each possible cpu, which repeats the same calculations of high and batch values.Instead call the function just once per zone, and make it apply the calculated values to all per-cpu pagesets of the zone.
Quoted 8 lines
This also allows removing the
zone_pageset_init()and__zone_pcp_update()wrappers.No functional change.
Signed-off-by Vlastimil Babka <[email protected]>Reviewed-by Oscar Salvador <[email protected]>Reviewed-by David Hildenbrand <[email protected]>
I like this. One question below
Quoted 49 lines
--- mm/page_alloc.c | 42 ++++++++++++++++++------------------------ 1 file changed, 18 insertions(+), 24 deletions(-)mm/page_alloc.c +9
-11@@ -6219,13 +6219,14 @@ static void setup_pageset(struct per_cpu_pageset *p) }/* -* Calculate and set new high and batch values for given per-cpu pageset of a +* Calculate and set new high and batch values for all per-cpu pagesets of a * zone, based on the zone's size and the percpu_pagelist_fraction sysctl. */ -static void pageset_set_high_and_batch (struct zone *zone, -struct per_cpu_pageset *p) +static void zone_set_pageset_high_and_batch (struct zone *zone) {unsigned long new_high, new_batch; +struct per_cpu_pageset *p; +int cpu;if (percpu_pagelist_fraction) { new_high =zone_managed_pages (zone) / percpu_pagelist_fraction;@@ -6237,23 +6238,25 @@ static void pageset_set_high_and_batch(struct zone *zone, new_high =6 * new_batch; new_batch =max (1UL ,1 * new_batch); } -pageset_update (&p->pcp, new_high, new_batch); -} - -static void __meminit zone_pageset_init (struct zone *zone,int cpu) -{ -struct per_cpu_pageset *pcp =per_cpu_ptr (zone->pageset, cpu); -pageset_init (pcp); -pageset_set_high_and_batch (zone, pcp); +for_each_possible_cpu (cpu) { + p =per_cpu_ptr (zone->pageset, cpu); +pageset_update (&p->pcp, new_high, new_batch); + } }void __meminit setup_zone_pageset (struct zone *zone) { +struct per_cpu_pageset *p;int cpu;
mm/page_alloc.c static void pageset_set_high_and_batch(struct zone *zone, · line 6259
Quoted 10 lines
mm/page_alloc.c +7
-2+ zone->pageset = alloc_percpu(struct per_cpu_pageset); -for_each_possible_cpu (cpu) -zone_pageset_init (zone, cpu); +for_each_possible_cpu (cpu) { + p =per_cpu_ptr (zone->pageset, cpu); +pageset_init (p); + } + +zone_set_pageset_high_and_batch (zone);
I hope I am not misreading the diff but it seems that setup_zone_pageset is calling pageset_init which is then done again by zone_set_pageset_high_and_batch as a part of pageset_update
Signature · 2 lines
Michal Hocko SUSE Labs
Re: [PATCH 2/9] mm, page_alloc: calculate pageset high and batch once per zone
Quoted 10 lines · Michal Hocko
Quoted 9 lines · Vlastimil Babka
We currently call
pageset_set_high_and_batch()for each possible cpu, which repeats the same calculations of high and batch values.Instead call the function just once per zone, and make it apply the calculated values to all per-cpu pagesets of the zone.
This also allows removing the
zone_pageset_init()and__zone_pcp_update()wrappers.
Quoted 8 lines
Quoted 5 lines
No functional change.
Signed-off-by Vlastimil Babka <[email protected]>Reviewed-by Oscar Salvador <[email protected]>Reviewed-by David Hildenbrand <[email protected]>I like this. One question below
Acked-by Michal Hocko <[email protected]>
Thanks.
Quoted 3 lines
I hope I am not misreading the diff but it seems that
setup_zone_pagesetis callingpageset_initwhich is then done again byzone_set_pageset_high_and_batchas a part ofpageset_update
No, pageset_init() is not called again from there, so must be insufficient diff context giving that impression.
PATCH 3/9 mm, page_alloc: remove setup_pageset()
In reply to Vlastimil BabkaWe initialize boot-time pagesets with setup_pageset(), which sets high and batch values that effectively disable pcplists.
We can remove this wrapper if we just set these values for all pagesets in pageset_init(). Non-boot pagesets then subsequently update them to the proper values.
No functional change.
--- mm/page_alloc.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-)
mm/page_alloc.c +10 -7
@@ -5823,7 +5823,7 @@ static void build_zonelists(pg_data_t *pgdat)* not check if the processor is online before following the pageset pointer. * Other parts of the kernel may not check if the zone is available. */ -static void setup_pageset (struct per_cpu_pageset *p); +static void pageset_init (struct per_cpu_pageset *p); static DEFINE_PER_CPU(struct per_cpu_pageset, boot_pageset); static DEFINE_PER_CPU(struct per_cpu_nodestat, boot_nodestats);
@@ -5891,7 +5891,7 @@ build_all_zonelists_init(void)* (a chicken-egg dilemma). */ for_each_possible_cpu (cpu) -setup_pageset (&per_cpu (boot_pageset, cpu)); +pageset_init (&per_cpu (boot_pageset, cpu));mminit_verify_zonelist ();cpuset_init_current_mems_allowed ();
@@ -6210,12 +6210,15 @@ static void pageset_init(struct per_cpu_pageset *p) pcp = &p->pcp;for (migratetype =0 ; migratetype <MIGRATE_PCPTYPES ; migratetype++)INIT_LIST_HEAD (&pcp->lists[migratetype]); -} -static void setup_pageset (struct per_cpu_pageset *p) -{ -pageset_init (p); -pageset_update (&p->pcp,0 ,1 ); +/* +* Set batch and high values safe for a boot pageset. A true percpu +* pageset's initialization will update them subsequently. Here we don't +* need to be as careful as pageset_update() as nobody can access the +* pageset yet. +*/ + pcp->high =0 ; + pcp->batch =1 ; }/*
Signature · 1 line
2.28.0
Re: [PATCH 3/9] mm, page_alloc: remove setup_pageset()
Quoted 48 lines · Vlastimil Babka
We initialize boot-time pagesets with
setup_pageset(), which sets high and batch values that effectively disable pcplists.We can remove this wrapper if we just set these values for all pagesets in
pageset_init(). Non-boot pagesets then subsequently update them to the proper values.No functional change.
Signed-off-by Vlastimil Babka <[email protected]>--- mm/page_alloc.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-)mm/page_alloc.c +3
-7@@ -5823,7 +5823,7 @@ static void build_zonelists(pg_data_t *pgdat)* not check if the processor is online before following the pageset pointer. * Other parts of the kernel may not check if the zone is available. */ -static void setup_pageset (struct per_cpu_pageset *p); +static void pageset_init (struct per_cpu_pageset *p); static DEFINE_PER_CPU(struct per_cpu_pageset, boot_pageset); static DEFINE_PER_CPU(struct per_cpu_nodestat, boot_nodestats);@@ -5891,7 +5891,7 @@ build_all_zonelists_init(void)* (a chicken-egg dilemma). */ for_each_possible_cpu (cpu) -setup_pageset (&per_cpu (boot_pageset, cpu)); +pageset_init (&per_cpu (boot_pageset, cpu));mminit_verify_zonelist ();cpuset_init_current_mems_allowed ();@@ -6210,12 +6210,15 @@ static void pageset_init(struct per_cpu_pageset *p) pcp = &p->pcp;for (migratetype =0 ; migratetype <MIGRATE_PCPTYPES ; migratetype++)INIT_LIST_HEAD (&pcp->lists[migratetype]); -} -static void setup_pageset (struct per_cpu_pageset *p) -{ -pageset_init (p); -pageset_update (&p->pcp,0 ,1 ); +/*
mm/page_alloc.c static void pageset_init(struct per_cpu_pageset *p) · line 6224
Quoted 10 lines
mm/page_alloc.c +7
-0+* Set batch and high values safe for a boot pageset. A true percpu +* pageset's initialization will update them subsequently. Here we don't +* need to be as careful as pageset_update() as nobody can access the +* pageset yet. +*/ + pcp->high =0 ; + pcp->batch =1 ; }/*
Signature · 3 lines
Thanks, David / dhildenb
Michal Hocko
reviewerRe: [PATCH 3/9] mm, page_alloc: remove setup_pageset()
In reply to Vlastimil BabkaQuoted 41 lines · Vlastimil Babka
We initialize boot-time pagesets with
setup_pageset(), which sets high and batch values that effectively disable pcplists.We can remove this wrapper if we just set these values for all pagesets in
pageset_init(). Non-boot pagesets then subsequently update them to the proper values.No functional change.
Signed-off-by Vlastimil Babka <[email protected]>--- mm/page_alloc.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-)mm/page_alloc.c +2
-3@@ -5823,7 +5823,7 @@ static void build_zonelists(pg_data_t *pgdat)* not check if the processor is online before following the pageset pointer. * Other parts of the kernel may not check if the zone is available. */ -static void setup_pageset (struct per_cpu_pageset *p); +static void pageset_init (struct per_cpu_pageset *p); static DEFINE_PER_CPU(struct per_cpu_pageset, boot_pageset); static DEFINE_PER_CPU(struct per_cpu_nodestat, boot_nodestats);@@ -5891,7 +5891,7 @@ build_all_zonelists_init(void)* (a chicken-egg dilemma). */ for_each_possible_cpu (cpu) -setup_pageset (&per_cpu (boot_pageset, cpu)); +pageset_init (&per_cpu (boot_pageset, cpu));mminit_verify_zonelist ();cpuset_init_current_mems_allowed ();@@ -6210,12 +6210,15 @@ static void pageset_init(struct per_cpu_pageset *p) pcp = &p->pcp;for (migratetype =0 ; migratetype <MIGRATE_PCPTYPES ; migratetype++)INIT_LIST_HEAD (&pcp->lists[migratetype]); -}
mm/page_alloc.c static void pageset_init(struct per_cpu_pageset *p) · line 6218
Quoted 10 lines
mm/page_alloc.c +5
-4-static void setup_pageset (struct per_cpu_pageset *p) -{ -pageset_init (p); -pageset_update (&p->pcp,0 ,1 ); +/* +* Set batch and high values safe for a boot pageset. A true percpu +* pageset's initialization will update them subsequently. Here we don't +* need to be as careful as pageset_update() as nobody can access the +* pageset yet.
Isn't this slightly misleading? pageset_init is called from setup_zone_pageset which is called from the memory hotplug as well. Isn't this more about early zone initialization rather than boot pagesets? Or am I misreading the patch?
Quoted 8 lines
mm/page_alloc.c +3
-0+*/ + pcp->high =0 ; + pcp->batch =1 ; }/* Signature · 1 line
2.28.0
Signature · 2 lines
Michal Hocko SUSE Labs
Re: [PATCH 3/9] mm, page_alloc: remove setup_pageset()
Quoted 43 lines · Michal Hocko
Quoted 42 lines · Vlastimil Babka
We initialize boot-time pagesets with
setup_pageset(), which sets high and batch values that effectively disable pcplists.We can remove this wrapper if we just set these values for all pagesets in
pageset_init(). Non-boot pagesets then subsequently update them to the proper values.No functional change.
Signed-off-by Vlastimil Babka <[email protected]>--- mm/page_alloc.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-)mm/page_alloc.c +2
-3@@ -5823,7 +5823,7 @@ static void build_zonelists(pg_data_t *pgdat)* not check if the processor is online before following the pageset pointer. * Other parts of the kernel may not check if the zone is available. */ -static void setup_pageset (struct per_cpu_pageset *p); +static void pageset_init (struct per_cpu_pageset *p); static DEFINE_PER_CPU(struct per_cpu_pageset, boot_pageset); static DEFINE_PER_CPU(struct per_cpu_nodestat, boot_nodestats);@@ -5891,7 +5891,7 @@ build_all_zonelists_init(void)* (a chicken-egg dilemma). */ for_each_possible_cpu (cpu) -setup_pageset (&per_cpu (boot_pageset, cpu)); +pageset_init (&per_cpu (boot_pageset, cpu));mminit_verify_zonelist ();cpuset_init_current_mems_allowed ();@@ -6210,12 +6210,15 @@ static void pageset_init(struct per_cpu_pageset *p) pcp = &p->pcp;for (migratetype =0 ; migratetype <MIGRATE_PCPTYPES ; migratetype++)INIT_LIST_HEAD (&pcp->lists[migratetype]); -}
Quoted 12 lines
mm/page_alloc.c static void pageset_init(struct per_cpu_pageset *p) · line 6218
Quoted 9 lines
mm/page_alloc.c +5
-4-static void setup_pageset (struct per_cpu_pageset *p) -{ -pageset_init (p); -pageset_update (&p->pcp,0 ,1 ); +/* +* Set batch and high values safe for a boot pageset. A true percpu +* pageset's initialization will update them subsequently. Here we don't +* need to be as careful as pageset_update() as nobody can access the +* pageset yet. Isn't this slightly misleading?
pageset_initis called fromsetup_zone_pagesetwhich is called from the memory hotplug as well. Isn't this more about
It is.
Quoted 2 lines
early zone initialization rather than boot pagesets? Or am I misreading the patch?
It's for boot pagesets (call from build_all_zonelists_init()) that we need these particular values (well, just high = 0). For early zone initialization during hotplug the values don't matter as the caller is setup_zone_pageset() who immediately replaces the values by zone_set_pageset_high_and_batch(). Which is what the comment tries to say.
Quoted 9 lines
Quoted 8 lines
mm/page_alloc.c +3
-0+*/ + pcp->high =0 ; + pcp->batch =1 ; }/* Signature · 1 line
2.28.0
PATCH 4/9 mm, page_alloc: simplify pageset_update()
In reply to Vlastimil Babkapageset_update() attempts to update pcplist's high and batch values in a way that readers don't observe batch > high. It uses smp_wmb() to order the updates in a way to achieve this. However, without proper pairing read barriers in readers this guarantee doesn't hold, and there are no such barriers in e.g. free_unref_page_commit().
Commit 88e8ac11d2ea ("mm, page_alloc: fix core hung in free_pcppages_bulk()") already showed this is problematic, and solved this by ultimately only trusing pcp->count of the current cpu with interrupts disabled.
The update dance with unpaired write barriers thus makes no sense. Replace them with plain WRITE_ONCE to prevent store tearing, and document that the values can change asynchronously and should not be trusted for correctness.
All current readers appear to be OK after 88e8ac11d2ea. Convert them to READ_ONCE to prevent unnecessary read tearing, but mainly to alert anybody making future changes to the code that special care is needed.
--- mm/page_alloc.c | 40 ++++++++++++++++++---------------------- 1 file changed, 18 insertions(+), 22 deletions(-)
mm/page_alloc.c +18 -22
@@ -1297,7 +1297,7 @@ static void free_pcppages_bulk(struct zone *zone, int count, {int migratetype =0 ;int batch_free =0 ; -int prefetch_nr =0 ; +int prefetch_nr =READ_ONCE (pcp->batch);bool isolated_pageblocks;struct page *page, *tmp;LIST_HEAD (head);
@@ -1348,8 +1348,10 @@ static void free_pcppages_bulk(struct zone *zone, int count,* avoid excessive prefetching due to large count, only * prefetch buddy for the first pcp->batch nr of pages. */ -if (prefetch_nr++ < pcp->batch) +if (prefetch_nr) {prefetch_buddy (page); + prefetch_nr--; + } }while (--count && --batch_free && !list_empty (list)); }
@@ -3131,10 +3133,8 @@ static void free_unref_page_commit(struct page *page, unsigned long pfn) pcp = &this_cpu_ptr (zone->pageset)->pcp;list_add (&page->lru, &pcp->lists[migratetype]); pcp->count++; -if (pcp->count >= pcp->high) { -unsigned long batch =READ_ONCE (pcp->batch); -free_pcppages_bulk (zone, batch, pcp); - } +if (pcp->count >=READ_ONCE (pcp->high)) +free_pcppages_bulk (zone,READ_ONCE (pcp->batch), pcp); }/*
@@ -3318,7 +3318,7 @@ static struct page *__rmqueue_pcplist(struct zone *zone, int migratetype, do { if (list_empty(list)) { pcp->count += rmqueue_bulk(zone, 0, - pcp->batch, list, + READ_ONCE(pcp->batch), list, migratetype, alloc_flags); if (unlikely(list_empty(list))) return NULL;
@@ -6174,13 +6174,16 @@ static int zone_batchsize(struct zone *zone) }/* -* pcp->high and pcp->batch values are related and dependent on one another: -* ->batch must never be higher then ->high. -* The following function updates them in a safe manner without read side -* locking. +* pcp->high and pcp->batch values are related and generally batch is lower +* than high. They are also related to pcp->count such that count is lower +* than high, and as soon as it reaches high, the pcplist is flushed. * -* Any new users of pcp->batch and pcp->high should ensure they can cope with -* those fields changing asynchronously (acording to the above rule). +* However, guaranteeing these relations at all times would require e.g. write +* barriers here but also careful usage of read barriers at the read side, and +* thus be prone to error and bad for performance. Thus the update only prevents +* store tearing. Any new users of pcp->batch and pcp->high should ensure they +* can cope with those fields changing asynchronously, and fully trust only the +* pcp->count field on the local CPU with interrupts disabled. * * mutex_is_locked(&pcp_batch_high_lock) required when calling this function * outside of boot time (or some other assurance that no concurrent updaters
@@ -6189,15 +6192,8 @@ static int zone_batchsize(struct zone *zone)static void pageset_update (struct per_cpu_pages *pcp,unsigned long high,unsigned long batch) { -/* start with a fail safe value for batch */ - pcp->batch =1 ; -smp_wmb (); - -/* Update high, then batch, in order */ - pcp->high = high; -smp_wmb (); - - pcp->batch = batch; +WRITE_ONCE (pcp->batch, batch); +WRITE_ONCE (pcp->high, high); }static void pageset_init (struct per_cpu_pageset *p)
Signature · 1 line
2.28.0
Re: [PATCH 4/9] mm, page_alloc: simplify pageset_update()
Quoted 102 lines · Vlastimil Babka
pageset_update()attempts to update pcplist's high and batch values in a way that readers don't observe batch > high. It usessmp_wmb()to order the updates in a way to achieve this. However, without proper pairing read barriers in readers this guarantee doesn't hold, and there are no such barriers in e.g.free_unref_page_commit().Commit
88e8ac11d2ea("mm,page_alloc: fix core hung infree_pcppages_bulk()") already showed this is problematic, and solved this by ultimately only trusingpcp->countof the current cpu with interrupts disabled.The update dance with unpaired write barriers thus makes no sense. Replace them with plain
WRITE_ONCEto prevent store tearing, and document that the values can change asynchronously and should not be trusted for correctness.All current readers appear to be OK after
88e8ac11d2ea. Convert them toREAD_ONCEto prevent unnecessary read tearing, but mainly to alert anybody making future changes to the code that special care is needed.Signed-off-by Vlastimil Babka <[email protected]>--- mm/page_alloc.c | 40 ++++++++++++++++++---------------------- 1 file changed, 18 insertions(+), 22 deletions(-)mm/page_alloc.c +16
-17@@ -1297,7 +1297,7 @@ static void free_pcppages_bulk(struct zone *zone, int count, {int migratetype =0 ;int batch_free =0 ; -int prefetch_nr =0 ; +int prefetch_nr =READ_ONCE (pcp->batch);bool isolated_pageblocks;struct page *page, *tmp;LIST_HEAD (head);@@ -1348,8 +1348,10 @@ static void free_pcppages_bulk(struct zone *zone, int count,* avoid excessive prefetching due to large count, only * prefetch buddy for the first pcp->batch nr of pages. */ -if (prefetch_nr++ < pcp->batch) +if (prefetch_nr) {prefetch_buddy (page); + prefetch_nr--; + } }while (--count && --batch_free && !list_empty (list)); }@@ -3131,10 +3133,8 @@ static void free_unref_page_commit(struct page *page, unsigned long pfn) pcp = &this_cpu_ptr (zone->pageset)->pcp;list_add (&page->lru, &pcp->lists[migratetype]); pcp->count++; -if (pcp->count >= pcp->high) { -unsigned long batch =READ_ONCE (pcp->batch); -free_pcppages_bulk (zone, batch, pcp); - } +if (pcp->count >=READ_ONCE (pcp->high)) +free_pcppages_bulk (zone,READ_ONCE (pcp->batch), pcp); }/* @@ -3318,7 +3318,7 @@ static struct page *__rmqueue_pcplist(struct zone *zone, int migratetype, do { if (list_empty(list)) { pcp->count += rmqueue_bulk(zone, 0, - pcp->batch, list, + READ_ONCE(pcp->batch), list, migratetype, alloc_flags); if (unlikely(list_empty(list))) return NULL;@@ -6174,13 +6174,16 @@ static int zone_batchsize(struct zone *zone) }/* -* pcp->high and pcp->batch values are related and dependent on one another: -* ->batch must never be higher then ->high. -* The following function updates them in a safe manner without read side -* locking. +* pcp->high and pcp->batch values are related and generally batch is lower +* than high. They are also related to pcp->count such that count is lower +* than high, and as soon as it reaches high, the pcplist is flushed. * -* Any new users of pcp->batch and pcp->high should ensure they can cope with -* those fields changing asynchronously (acording to the above rule). +* However, guaranteeing these relations at all times would require e.g. write +* barriers here but also careful usage of read barriers at the read side, and +* thus be prone to error and bad for performance. Thus the update only prevents +* store tearing. Any new users of pcp->batch and pcp->high should ensure they +* can cope with those fields changing asynchronously, and fully trust only the +* pcp->count field on the local CPU with interrupts disabled. * * mutex_is_locked(&pcp_batch_high_lock) required when calling this function * outside of boot time (or some other assurance that no concurrent updaters @@ -6189,15 +6192,8 @@ static int zone_batchsize(struct zone *zone)static void pageset_update (struct per_cpu_pages *pcp,unsigned long high,unsigned long batch) { -/* start with a fail safe value for batch */ - pcp->batch =1 ; -smp_wmb (); -
mm/page_alloc.c static int zone_batchsize(struct zone *zone) · line 6199
Quoted 10 lines
mm/page_alloc.c +2
-5-/* Update high, then batch, in order */ - pcp->high = high; -smp_wmb (); - - pcp->batch = batch; +WRITE_ONCE (pcp->batch, batch); +WRITE_ONCE (pcp->high, high); }static void pageset_init (struct per_cpu_pageset *p)
I *think* this is okay and obviously simplifies things. But to be 100% sure, I'll have to rely on your judgment :)
Signature · 3 lines
Thanks, David / dhildenb
Re: [PATCH 4/9] mm, page_alloc: simplify pageset_update()
In reply to Vlastimil BabkaQuoted 14 lines · Vlastimil Babka
pageset_update()attempts to update pcplist's high and batch values in a way that readers don't observe batch > high. It usessmp_wmb()to order the updates in a way to achieve this. However, without proper pairing read barriers in readers this guarantee doesn't hold, and there are no such barriers in e.g.free_unref_page_commit().Commit
88e8ac11d2ea("mm,page_alloc: fix core hung infree_pcppages_bulk()") already showed this is problematic, and solved this by ultimately only trusingpcp->countof the current cpu with interrupts disabled.The update dance with unpaired write barriers thus makes no sense. Replace them with plain
WRITE_ONCEto prevent store tearing, and document that the values can change asynchronously and should not be trusted for correctness.
Quoted 5 lines
All current readers appear to be OK after
88e8ac11d2ea. Convert them toREAD_ONCEto prevent unnecessary read tearing, but mainly to alert anybody making future changes to the code that special care is needed.Signed-off-by Vlastimil Babka <[email protected]>
Yes, this should be safe AFAICS. I believe the original intention was well minded but didn't go all the way to do the thing properly.
I have to admit I have stumbled over this weirdness few times and never found enough motivation to think that through.
Quoted 94 lines
--- mm/page_alloc.c | 40 ++++++++++++++++++---------------------- 1 file changed, 18 insertions(+), 22 deletions(-)mm/page_alloc.c +18
-22@@ -1297,7 +1297,7 @@ static void free_pcppages_bulk(struct zone *zone, int count, {int migratetype =0 ;int batch_free =0 ; -int prefetch_nr =0 ; +int prefetch_nr =READ_ONCE (pcp->batch);bool isolated_pageblocks;struct page *page, *tmp;LIST_HEAD (head);@@ -1348,8 +1348,10 @@ static void free_pcppages_bulk(struct zone *zone, int count,* avoid excessive prefetching due to large count, only * prefetch buddy for the first pcp->batch nr of pages. */ -if (prefetch_nr++ < pcp->batch) +if (prefetch_nr) {prefetch_buddy (page); + prefetch_nr--; + } }while (--count && --batch_free && !list_empty (list)); }@@ -3131,10 +3133,8 @@ static void free_unref_page_commit(struct page *page, unsigned long pfn) pcp = &this_cpu_ptr (zone->pageset)->pcp;list_add (&page->lru, &pcp->lists[migratetype]); pcp->count++; -if (pcp->count >= pcp->high) { -unsigned long batch =READ_ONCE (pcp->batch); -free_pcppages_bulk (zone, batch, pcp); - } +if (pcp->count >=READ_ONCE (pcp->high)) +free_pcppages_bulk (zone,READ_ONCE (pcp->batch), pcp); }/* @@ -3318,7 +3318,7 @@ static struct page *__rmqueue_pcplist(struct zone *zone, int migratetype, do { if (list_empty(list)) { pcp->count += rmqueue_bulk(zone, 0, - pcp->batch, list, + READ_ONCE(pcp->batch), list, migratetype, alloc_flags); if (unlikely(list_empty(list))) return NULL;@@ -6174,13 +6174,16 @@ static int zone_batchsize(struct zone *zone) }/* -* pcp->high and pcp->batch values are related and dependent on one another: -* ->batch must never be higher then ->high. -* The following function updates them in a safe manner without read side -* locking. +* pcp->high and pcp->batch values are related and generally batch is lower +* than high. They are also related to pcp->count such that count is lower +* than high, and as soon as it reaches high, the pcplist is flushed. * -* Any new users of pcp->batch and pcp->high should ensure they can cope with -* those fields changing asynchronously (acording to the above rule). +* However, guaranteeing these relations at all times would require e.g. write +* barriers here but also careful usage of read barriers at the read side, and +* thus be prone to error and bad for performance. Thus the update only prevents +* store tearing. Any new users of pcp->batch and pcp->high should ensure they +* can cope with those fields changing asynchronously, and fully trust only the +* pcp->count field on the local CPU with interrupts disabled. * * mutex_is_locked(&pcp_batch_high_lock) required when calling this function * outside of boot time (or some other assurance that no concurrent updaters @@ -6189,15 +6192,8 @@ static int zone_batchsize(struct zone *zone)static void pageset_update (struct per_cpu_pages *pcp,unsigned long high,unsigned long batch) { -/* start with a fail safe value for batch */ - pcp->batch =1 ; -smp_wmb (); - -/* Update high, then batch, in order */ - pcp->high = high; -smp_wmb (); - - pcp->batch = batch; +WRITE_ONCE (pcp->batch, batch); +WRITE_ONCE (pcp->high, high); }static void pageset_init (struct per_cpu_pageset *p)Signature · 1 line
2.28.0
Signature · 2 lines
Michal Hocko SUSE Labs
PATCH 5/9 mm, page_alloc: make per_cpu_pageset accessible only after init
In reply to Vlastimil Babkasetup_zone_pageset() replaces the boot_pageset by allocating and initializing a proper percpu one. Currently it assigns zone->pageset with the newly allocated one before initializing it. That's currently not an issue, because the zone should not be in any zonelist, thus not visible to allocators at this point.
Memory ordering between the pcplist contents and its visibility is also not guaranteed here, but that also shouldn't be an issue because online_pages() does a spin_unlock(pgdat->node_size_lock) before building the zonelists.
However it's best that we don't silently rely on operations that can be changed in the future. Make sure only properly initialized pcplists are visible, using smp_store_release(). The read side has a data dependency via the zone->pageset pointer instead of an explicit read barrier.
--- mm/page_alloc.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
mm/page_alloc.c +4 -2
@@ -6246,15 +6246,17 @@ static void zone_set_pageset_high_and_batch(struct zone *zone)void __meminit setup_zone_pageset (struct zone *zone) { +struct per_cpu_pageset __percpu * new_pageset;struct per_cpu_pageset *p;int cpu; - zone->pageset = alloc_percpu(struct per_cpu_pageset); + new_pageset = alloc_percpu(struct per_cpu_pageset);for_each_possible_cpu (cpu) { - p =per_cpu_ptr (zone->pageset, cpu); + p =per_cpu_ptr (new_pageset, cpu);pageset_init (p); } +smp_store_release (&zone->pageset, new_pageset);zone_set_pageset_high_and_batch (zone); }
Signature · 1 line
2.28.0
Re: [PATCH 5/9] mm, page_alloc: make per_cpu_pageset accessible only after init
Quoted 34 lines · Vlastimil Babka
setup_zone_pageset()replaces theboot_pagesetby allocating and initializing a proper percpu one. Currently it assignszone->pagesetwith the newly allocated one before initializing it. That's currently not an issue, because the zone should not be in any zonelist, thus not visible to allocators at this point.Memory ordering between the pcplist contents and its visibility is also not guaranteed here, but that also shouldn't be an issue because
online_pages()does aspin_unlock(pgdat->node_size_lock)before building the zonelists.However it's best that we don't silently rely on operations that can be changed in the future. Make sure only properly initialized pcplists are visible, using
smp_store_release(). The read side has a data dependency via thezone->pagesetpointer instead of an explicit read barrier.Signed-off-by Vlastimil Babka <[email protected]>--- mm/page_alloc.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)mm/page_alloc.c +2
-1@@ -6246,15 +6246,17 @@ static void zone_set_pageset_high_and_batch(struct zone *zone)void __meminit setup_zone_pageset (struct zone *zone) { +struct per_cpu_pageset __percpu * new_pageset;struct per_cpu_pageset *p;int cpu; - zone->pageset = alloc_percpu(struct per_cpu_pageset); + new_pageset = alloc_percpu(struct per_cpu_pageset);
mm/page_alloc.c static void zone_set_pageset_high_and_batch(struct zone *zone) · line 6262
Quoted 10 lines
mm/page_alloc.c +2
-1for_each_possible_cpu (cpu) { - p =per_cpu_ptr (zone->pageset, cpu); + p =per_cpu_ptr (new_pageset, cpu);pageset_init (p); } +smp_store_release (&zone->pageset, new_pageset);zone_set_pageset_high_and_batch (zone); }
Signature · 3 lines
Thanks, David / dhildenb
Michal Hocko
reviewerRe: [PATCH 5/9] mm, page_alloc: make per_cpu_pageset accessible only after init
In reply to Vlastimil BabkaQuoted 5 lines · Vlastimil Babka
setup_zone_pageset()replaces theboot_pagesetby allocating and initializing a proper percpu one. Currently it assignszone->pagesetwith the newly allocated one before initializing it. That's currently not an issue, because the zone should not be in any zonelist, thus not visible to allocators at this point.
Quoted 8 lines
Memory ordering between the pcplist contents and its visibility is also not guaranteed here, but that also shouldn't be an issue because
online_pages()does aspin_unlock(pgdat->node_size_lock)before building the zonelists.However it's best that we don't silently rely on operations that can be changed in the future. Make sure only properly initialized pcplists are visible, using
smp_store_release(). The read side has a data dependency via thezone->pagesetpointer instead of an explicit read barrier.
Heh, this looks like inveting a similar trap the previous patch was removing. But more seriously considering that we need a locking for the whole setup, wouldn't it be better to simply document the locking requirements rather than adding scary looking barriers future ourselves or somebody else will need to scratch heads about. I am pretty sure we don't do anything like that when initializating numa node or other data structures that might be allocated during the memory hotadd.
Quoted 31 lines
Signed-off-by Vlastimil Babka <[email protected]>--- mm/page_alloc.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)mm/page_alloc.c +4
-2@@ -6246,15 +6246,17 @@ static void zone_set_pageset_high_and_batch(struct zone *zone)void __meminit setup_zone_pageset (struct zone *zone) { +struct per_cpu_pageset __percpu * new_pageset;struct per_cpu_pageset *p;int cpu; - zone->pageset = alloc_percpu(struct per_cpu_pageset); + new_pageset = alloc_percpu(struct per_cpu_pageset);for_each_possible_cpu (cpu) { - p =per_cpu_ptr (zone->pageset, cpu); + p =per_cpu_ptr (new_pageset, cpu);pageset_init (p); } +smp_store_release (&zone->pageset, new_pageset);zone_set_pageset_high_and_batch (zone); }Signature · 1 line
2.28.0
Signature · 2 lines
Michal Hocko SUSE Labs
Re: [PATCH 5/9] mm, page_alloc: make per_cpu_pageset accessible only after init
Quoted 15 lines · Michal Hocko
Quoted 5 lines · Vlastimil Babka
setup_zone_pageset()replaces theboot_pagesetby allocating and initializing a proper percpu one. Currently it assignszone->pagesetwith the newly allocated one before initializing it. That's currently not an issue, because the zone should not be in any zonelist, thus not visible to allocators at this point.Quoted 8 lines
Memory ordering between the pcplist contents and its visibility is also not guaranteed here, but that also shouldn't be an issue because
online_pages()does aspin_unlock(pgdat->node_size_lock)before building the zonelists.However it's best that we don't silently rely on operations that can be changed in the future. Make sure only properly initialized pcplists are visible, using
smp_store_release(). The read side has a data dependency via thezone->pagesetpointer instead of an explicit read barrier.
Quoted 7 lines
Heh, this looks like inveting a similar trap the previous patch was removing. But more seriously considering that we need a locking for the whole setup, wouldn't it be better to simply document the locking requirements rather than adding scary looking barriers future ourselves or somebody else will need to scratch heads about. I am pretty sure we don't do anything like that when initializating numa node or other data structures that might be allocated during the memory hotadd.
Yeah it will be best to drop this for now. I just looked closed at build_zonelist() implementation and got scared how it just modifies the list that others might be reading at the same time. zoneref_set_zone() sets zoneref->zone and zoneref->zone_idx without any sync, what if somebody, e.g. for_next_zone_zonelist_nodemask() makes decision based on zone_idx and then picks wrong zone pointer? etc.
Quoted 32 lines
Quoted 31 lines
Signed-off-by Vlastimil Babka <[email protected]>--- mm/page_alloc.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)mm/page_alloc.c +4
-2@@ -6246,15 +6246,17 @@ static void zone_set_pageset_high_and_batch(struct zone *zone)void __meminit setup_zone_pageset (struct zone *zone) { +struct per_cpu_pageset __percpu * new_pageset;struct per_cpu_pageset *p;int cpu; - zone->pageset = alloc_percpu(struct per_cpu_pageset); + new_pageset = alloc_percpu(struct per_cpu_pageset);for_each_possible_cpu (cpu) { - p =per_cpu_ptr (zone->pageset, cpu); + p =per_cpu_ptr (new_pageset, cpu);pageset_init (p); } +smp_store_release (&zone->pageset, new_pageset);zone_set_pageset_high_and_batch (zone); }Signature · 1 line
2.28.0
PATCH 6/9 mm, page_alloc: cache pageset high and batch in struct zone
In reply to Vlastimil BabkaAll per-cpu pagesets for a zone use the same high and batch values, that are duplicated there just for performance (locality) reasons. This patch adds the same variables also to struct zone as a shared copy.
This will be useful later for making possible to disable pcplists temporarily by setting high value to 0, while remembering the values for restoring them later. But we can also immediately benefit from not updating pagesets of all possible cpus in case the newly recalculated values (after sysctl change or memory online/offline) are actually unchanged from the previous ones.
--- include/linux/mmzone.h | 6 ++++++ mm/page_alloc.c | 16 ++++++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-)
include/linux/mmzone.h +6 -0
@@ -470,6 +470,12 @@ struct zone {#endif struct pglist_data *zone_pgdat;struct per_cpu_pageset __percpu *pageset; +/* +* the high and batch values are copied to individual pagesets for +* faster access +*/ +int pageset_high; +int pageset_batch;#ifndef CONFIG_SPARSEMEM /*
mm/page_alloc.c +14 -2
@@ -5824,6 +5824,8 @@ static void build_zonelists(pg_data_t *pgdat)* Other parts of the kernel may not check if the zone is available. */ static void pageset_init (struct per_cpu_pageset *p); +#define BOOT_PAGESET_HIGH 0 +#define BOOT_PAGESET_BATCH 1 static DEFINE_PER_CPU(struct per_cpu_pageset, boot_pageset); static DEFINE_PER_CPU(struct per_cpu_nodestat, boot_nodestats);
@@ -6213,8 +6215,8 @@ static void pageset_init(struct per_cpu_pageset *p)* need to be as careful as pageset_update() as nobody can access the * pageset yet. */ - pcp->high =0 ; - pcp->batch =1 ; + pcp->high =BOOT_PAGESET_HIGH ; + pcp->batch =BOOT_PAGESET_BATCH ; }/*
@@ -6238,6 +6240,14 @@ static void zone_set_pageset_high_and_batch(struct zone *zone) new_batch =max (1UL ,1 * new_batch); } +if (zone->pageset_high != new_high || + zone->pageset_batch != new_batch) { + zone->pageset_high = new_high; + zone->pageset_batch = new_batch; + }else { +return ; + } +for_each_possible_cpu (cpu) { p =per_cpu_ptr (zone->pageset, cpu);pageset_update (&p->pcp, new_high, new_batch);
@@ -6300,6 +6310,8 @@ static __meminit void zone_pcp_init(struct zone *zone)* offset of a (static) per cpu variable into the per cpu area. */ zone->pageset = &boot_pageset; + zone->pageset_high =BOOT_PAGESET_HIGH ; + zone->pageset_batch =BOOT_PAGESET_BATCH ;if (populated_zone (zone)) printk(KERN_DEBUG " %s zone: %lu pages, LIFO batch:%u\n",
Signature · 1 line
2.28.0
Re: [PATCH 6/9] mm, page_alloc: cache pageset high and batch in struct zone
Quoted 38 lines · Vlastimil Babka
All per-cpu pagesets for a zone use the same high and batch values, that are duplicated there just for performance (locality) reasons. This patch adds the same variables also to struct zone as a shared copy.
This will be useful later for making possible to disable pcplists temporarily by setting high value to 0, while remembering the values for restoring them later. But we can also immediately benefit from not updating pagesets of all possible cpus in case the newly recalculated values (after sysctl change or memory online/offline) are actually unchanged from the previous ones.
Signed-off-by Vlastimil Babka <[email protected]>--- include/linux/mmzone.h | 6 ++++++ mm/page_alloc.c | 16 ++++++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-)include/linux/mmzone.h +6
-0@@ -470,6 +470,12 @@ struct zone {#endif struct pglist_data *zone_pgdat;struct per_cpu_pageset __percpu *pageset; +/* +* the high and batch values are copied to individual pagesets for +* faster access +*/ +int pageset_high; +int pageset_batch;#ifndef CONFIG_SPARSEMEM /* mm/page_alloc.c +0
-0
mm/page_alloc.c static void build_zonelists(pg_data_t *pgdat) · line 5828
Quoted 5 lines
mm/page_alloc.c +2
-0@@ -5824,6 +5824,8 @@ static void build_zonelists(pg_data_t *pgdat)* Other parts of the kernel may not check if the zone is available. */ static void pageset_init (struct per_cpu_pageset *p); +#define BOOT_PAGESET_HIGH 0 +#define BOOT_PAGESET_BATCH 1
Much better. A comment would have been nice ("this disables the pcp via the boot pageset completely.") :) (I'm pretty sure I'd forget at one point what these values mean)
Quoted 30 lines
mm/page_alloc.c +10
-2static DEFINE_PER_CPU(struct per_cpu_pageset, boot_pageset); static DEFINE_PER_CPU(struct per_cpu_nodestat, boot_nodestats);@@ -6213,8 +6215,8 @@ static void pageset_init(struct per_cpu_pageset *p)* need to be as careful as pageset_update() as nobody can access the * pageset yet. */ - pcp->high =0 ; - pcp->batch =1 ; + pcp->high =BOOT_PAGESET_HIGH ; + pcp->batch =BOOT_PAGESET_BATCH ; }/* @@ -6238,6 +6240,14 @@ static void zone_set_pageset_high_and_batch(struct zone *zone) new_batch =max (1UL ,1 * new_batch); } +if (zone->pageset_high != new_high || + zone->pageset_batch != new_batch) { + zone->pageset_high = new_high; + zone->pageset_batch = new_batch; + }else { +return ; + } +for_each_possible_cpu (cpu) { p =per_cpu_ptr (zone->pageset, cpu);pageset_update (&p->pcp, new_high, new_batch);
mm/page_alloc.c static __meminit void zone_pcp_init(struct zone *zone) · line 6314
Quoted 5 lines
mm/page_alloc.c +2
-0@@ -6300,6 +6310,8 @@ static __meminit void zone_pcp_init(struct zone *zone)* offset of a (static) per cpu variable into the per cpu area. */ zone->pageset = &boot_pageset; + zone->pageset_high =BOOT_PAGESET_HIGH ; + zone->pageset_batch =BOOT_PAGESET_BATCH ;
I do wonder if copying from any cpuvar inside boot_pageset is cleaner.
zone->pageset_high = &this_cpu_ptr(zone->pageset)->pcp.high;
...
Signature · 3 lines
Thanks, David / dhildenb
Re: [PATCH 6/9] mm, page_alloc: cache pageset high and batch in struct zone
Quoted 11 lines · David Hildenbrand
mm/page_alloc.c static __meminit void zone_pcp_init(struct zone *zone) · line 6314
Quoted 6 lines · Vlastimil Babka
mm/page_alloc.c +2
-0@@ -6300,6 +6310,8 @@ static __meminit void zone_pcp_init(struct zone *zone)* offset of a (static) per cpu variable into the per cpu area. */ zone->pageset = &boot_pageset; + zone->pageset_high =BOOT_PAGESET_HIGH ; + zone->pageset_batch =BOOT_PAGESET_BATCH ;I do wonder if copying from any cpuvar inside
boot_pagesetis cleaner.
zone->pageset_high= &this_cpu_ptr(zone->pageset)->pcp.high;
Uh I don't know. That would be like admitting they can be different than what was initialized. But then they could be also different depending on what cpu we happen to run it on. It's why I added the #define BOOT_PAGESET_* in the first place - to ensure same value used in two places. Makes sense?
Michal Hocko
reviewerRe: [PATCH 6/9] mm, page_alloc: cache pageset high and batch in struct zone
In reply to Vlastimil BabkaQuoted 9 lines · Vlastimil Babka
All per-cpu pagesets for a zone use the same high and batch values, that are duplicated there just for performance (locality) reasons. This patch adds the same variables also to struct zone as a shared copy.
This will be useful later for making possible to disable pcplists temporarily by setting high value to 0, while remembering the values for restoring them later. But we can also immediately benefit from not updating pagesets of all possible cpus in case the newly recalculated values (after sysctl change or memory online/offline) are actually unchanged from the previous ones.
Advantage of this patch is not really clear from it in isolation. Maybe merge it with the patch which uses the duplicated state.
Quoted 74 lines
Signed-off-by Vlastimil Babka <[email protected]>--- include/linux/mmzone.h | 6 ++++++ mm/page_alloc.c | 16 ++++++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-)include/linux/mmzone.h +6
-0@@ -470,6 +470,12 @@ struct zone {#endif struct pglist_data *zone_pgdat;struct per_cpu_pageset __percpu *pageset; +/* +* the high and batch values are copied to individual pagesets for +* faster access +*/ +int pageset_high; +int pageset_batch;#ifndef CONFIG_SPARSEMEM /* mm/page_alloc.c +14
-2@@ -5824,6 +5824,8 @@ static void build_zonelists(pg_data_t *pgdat)* Other parts of the kernel may not check if the zone is available. */ static void pageset_init (struct per_cpu_pageset *p); +#define BOOT_PAGESET_HIGH 0 +#define BOOT_PAGESET_BATCH 1 static DEFINE_PER_CPU(struct per_cpu_pageset, boot_pageset); static DEFINE_PER_CPU(struct per_cpu_nodestat, boot_nodestats);@@ -6213,8 +6215,8 @@ static void pageset_init(struct per_cpu_pageset *p)* need to be as careful as pageset_update() as nobody can access the * pageset yet. */ - pcp->high =0 ; - pcp->batch =1 ; + pcp->high =BOOT_PAGESET_HIGH ; + pcp->batch =BOOT_PAGESET_BATCH ; }/* @@ -6238,6 +6240,14 @@ static void zone_set_pageset_high_and_batch(struct zone *zone) new_batch =max (1UL ,1 * new_batch); } +if (zone->pageset_high != new_high || + zone->pageset_batch != new_batch) { + zone->pageset_high = new_high; + zone->pageset_batch = new_batch; + }else { +return ; + } +for_each_possible_cpu (cpu) { p =per_cpu_ptr (zone->pageset, cpu);pageset_update (&p->pcp, new_high, new_batch);@@ -6300,6 +6310,8 @@ static __meminit void zone_pcp_init(struct zone *zone)* offset of a (static) per cpu variable into the per cpu area. */ zone->pageset = &boot_pageset; + zone->pageset_high =BOOT_PAGESET_HIGH ; + zone->pageset_batch =BOOT_PAGESET_BATCH ;if (populated_zone (zone)) printk(KERN_DEBUG " %s zone: %lu pages, LIFO batch:%u\n",Signature · 1 line
2.28.0
Signature · 2 lines
Michal Hocko SUSE Labs
Re: [PATCH 6/9] mm, page_alloc: cache pageset high and batch in struct zone
Quoted 5 lines · Michal Hocko
Quoted 4 lines · Vlastimil Babka
All per-cpu pagesets for a zone use the same high and batch values, that are duplicated there just for performance (locality) reasons. This patch adds the same variables also to struct zone as a shared copy.
Quoted 8 lines
Quoted 5 lines
This will be useful later for making possible to disable pcplists temporarily by setting high value to 0, while remembering the values for restoring them later. But we can also immediately benefit from not updating pagesets of all possible cpus in case the newly recalculated values (after sysctl change or memory online/offline) are actually unchanged from the previous ones.
Advantage of this patch is not really clear from it in isolation. Maybe merge it with the patch which uses the duplicated state.
I'm not sure that would help its reviewability? As the patch that uses it is the last, largest one. And there is already a small advantage right away as changelog explains.
PATCH 7/9 mm, page_alloc: move draining pcplists to page isolation users
In reply to Vlastimil BabkaCurrently, pcplists are drained during set_migratetype_isolate() which means once per pageblock processed start_isolate_page_range(). This is somewhat wasteful. Moreover, the callers might need different guarantees, and the draining is currently prone to races and does not guarantee that no page from isolated pageblock will end up on the pcplist after the drain.
Better guarantees are added by later patches and require explicit actions by page isolation users that need them. Thus it makes sense to move the current imperfect draining to the callers also as a preparation step.
--- mm/memory_hotplug.c | 11 ++++++----- mm/page_alloc.c | 2 ++ mm/page_isolation.c | 10 +++++----- 3 files changed, 13 insertions(+), 10 deletions(-)
mm/memory_hotplug.c +6 -5
@@ -1524,6 +1524,8 @@ int __ref offline_pages(unsigned long start_pfn, unsigned long nr_pages) goto failed_removal; } +drain_all_pages (zone); + arg.start_pfn = start_pfn; arg.nr_pages = nr_pages;node_states_check_changes_offline (nr_pages, zone, &arg);
@@ -1574,11 +1576,10 @@ int __ref offline_pages(unsigned long start_pfn, unsigned long nr_pages) }/* -* per-cpu pages are drained in start_isolate_page_range, but if -* there are still pages that are not free, make sure that we -* drain again, because when we isolated range we might -* have raced with another thread that was adding pages to pcp -* list. +* per-cpu pages are drained after start_isolate_page_range, but +* if there are still pages that are not free, make sure that we +* drain again, because when we isolated range we might have +* raced with another thread that was adding pages to pcp list. * * Forward progress should be still guaranteed because * pages on the pcp list can only belong to MOVABLE_ZONE
mm/page_alloc.c +2 -0
@@ -8432,6 +8432,8 @@ int alloc_contig_range(unsigned long start, unsigned long end,if (ret)return ret; +drain_all_pages (cc.zone); +/* * In case of -EBUSY, we'd like to know which page causes problem. * So, just fall through. test_pages_isolated() has a tracepoint
mm/page_isolation.c +5 -5
@@ -49,7 +49,6 @@ static int set_migratetype_isolate(struct page *page, int migratetype, int isol___mod_zone_freepage_state (zone, -nr_pages, mt);spin_unlock_irqrestore (&zone->lock, flags); -drain_all_pages (zone);return 0 ; }
@@ -167,11 +166,12 @@ __first_valid_page(unsigned long pfn, unsigned long nr_pages)* * Please note that there is no strong synchronization with the page allocator * either. Pages might be freed while their page blocks are marked ISOLATED. -* In some cases pages might still end up on pcp lists and that would allow +* A call to drain_all_pages() after isolation can flush most of them. However +* in some cases pages might still end up on pcp lists and that would allow * for their allocation even when they are in fact isolated already. Depending -* on how strong of a guarantee the caller needs drain_all_pages might be needed -* (e.g. __offline_pages will need to call it after check for isolated range for -* a next retry). +* on how strong of a guarantee the caller needs, further drain_all_pages() +* might be needed (e.g. __offline_pages will need to call it after check for +* isolated range for a next retry). * * Return: 0 on success and -EBUSY if any part of range cannot be isolated. */
Signature · 1 line
2.28.0
Re: [PATCH 7/9] mm, page_alloc: move draining pcplists to page isolation users
Quoted 11 lines · Vlastimil Babka
Currently, pcplists are drained during
set_migratetype_isolate()which means once per pageblock processedstart_isolate_page_range(). This is somewhat wasteful. Moreover, the callers might need different guarantees, and the draining is currently prone to races and does not guarantee that no page from isolated pageblock will end up on the pcplist after the drain.Better guarantees are added by later patches and require explicit actions by page isolation users that need them. Thus it makes sense to move the current imperfect draining to the callers also as a preparation step.
Suggested-by Pavel Tatashin <[email protected]>
I would have guessed I suggested that one first as well :)
https://lkml.kernel.org/r/[email protected]
(But honestly, I don't care :), just stumbled over it)
Quoted 69 lines
Signed-off-by Vlastimil Babka <[email protected]>--- mm/memory_hotplug.c | 11 ++++++----- mm/page_alloc.c | 2 ++ mm/page_isolation.c | 10 +++++----- 3 files changed, 13 insertions(+), 10 deletions(-)mm/memory_hotplug.c +6
-5@@ -1524,6 +1524,8 @@ int __ref offline_pages(unsigned long start_pfn, unsigned long nr_pages) goto failed_removal; } +drain_all_pages (zone); + arg.start_pfn = start_pfn; arg.nr_pages = nr_pages;node_states_check_changes_offline (nr_pages, zone, &arg);@@ -1574,11 +1576,10 @@ int __ref offline_pages(unsigned long start_pfn, unsigned long nr_pages) }/* -* per-cpu pages are drained in start_isolate_page_range, but if -* there are still pages that are not free, make sure that we -* drain again, because when we isolated range we might -* have raced with another thread that was adding pages to pcp -* list. +* per-cpu pages are drained after start_isolate_page_range, but +* if there are still pages that are not free, make sure that we +* drain again, because when we isolated range we might have +* raced with another thread that was adding pages to pcp list. * * Forward progress should be still guaranteed because * pages on the pcp list can only belong to MOVABLE_ZONE mm/page_alloc.c +2
-0@@ -8432,6 +8432,8 @@ int alloc_contig_range(unsigned long start, unsigned long end,if (ret)return ret; +drain_all_pages (cc.zone); +/* * In case of -EBUSY, we'd like to know which page causes problem. * So, just fall through. test_pages_isolated() has a tracepoint mm/page_isolation.c +2
-2@@ -49,7 +49,6 @@ static int set_migratetype_isolate(struct page *page, int migratetype, int isol___mod_zone_freepage_state (zone, -nr_pages, mt);spin_unlock_irqrestore (&zone->lock, flags); -drain_all_pages (zone);return 0 ; }@@ -167,11 +166,12 @@ __first_valid_page(unsigned long pfn, unsigned long nr_pages)* * Please note that there is no strong synchronization with the page allocator * either. Pages might be freed while their page blocks are marked ISOLATED. -* In some cases pages might still end up on pcp lists and that would allow +* A call to drain_all_pages() after isolation can flush most of them. However +* in some cases pages might still end up on pcp lists and that would allow
mm/page_isolation.c __first_valid_page(unsigned long pfn, unsigned long nr_pages) · line 177
Quoted 10 lines
mm/page_isolation.c +3
-3* for their allocation even when they are in fact isolated already. Depending -* on how strong of a guarantee the caller needs drain_all_pages might be needed -* (e.g. __offline_pages will need to call it after check for isolated range for -* a next retry). +* on how strong of a guarantee the caller needs, further drain_all_pages() +* might be needed (e.g. __offline_pages will need to call it after check for +* isolated range for a next retry). * * Return: 0 on success and -EBUSY if any part of range cannot be isolated. */
Signature · 3 lines
Thanks, David / dhildenb
Re: [PATCH 7/9] mm, page_alloc: move draining pcplists to page isolation users
In reply to Vlastimil BabkaQuoted 6 lines · Vlastimil Babka
Currently, pcplists are drained during
set_migratetype_isolate()which means once per pageblock processedstart_isolate_page_range(). This is somewhat wasteful. Moreover, the callers might need different guarantees, and the draining is currently prone to races and does not guarantee that no page from isolated pageblock will end up on the pcplist after the drain.
Quoted 6 lines
Better guarantees are added by later patches and require explicit actions by page isolation users that need them. Thus it makes sense to move the current imperfect draining to the callers also as a preparation step.
Suggested-by Pavel Tatashin <[email protected]>Signed-off-by Vlastimil Babka <[email protected]>
Quoted 79 lines
--- mm/memory_hotplug.c | 11 ++++++----- mm/page_alloc.c | 2 ++ mm/page_isolation.c | 10 +++++----- 3 files changed, 13 insertions(+), 10 deletions(-)mm/memory_hotplug.c +6
-5@@ -1524,6 +1524,8 @@ int __ref offline_pages(unsigned long start_pfn, unsigned long nr_pages) goto failed_removal; } +drain_all_pages (zone); + arg.start_pfn = start_pfn; arg.nr_pages = nr_pages;node_states_check_changes_offline (nr_pages, zone, &arg);@@ -1574,11 +1576,10 @@ int __ref offline_pages(unsigned long start_pfn, unsigned long nr_pages) }/* -* per-cpu pages are drained in start_isolate_page_range, but if -* there are still pages that are not free, make sure that we -* drain again, because when we isolated range we might -* have raced with another thread that was adding pages to pcp -* list. +* per-cpu pages are drained after start_isolate_page_range, but +* if there are still pages that are not free, make sure that we +* drain again, because when we isolated range we might have +* raced with another thread that was adding pages to pcp list. * * Forward progress should be still guaranteed because * pages on the pcp list can only belong to MOVABLE_ZONE mm/page_alloc.c +2
-0@@ -8432,6 +8432,8 @@ int alloc_contig_range(unsigned long start, unsigned long end,if (ret)return ret; +drain_all_pages (cc.zone); +/* * In case of -EBUSY, we'd like to know which page causes problem. * So, just fall through. test_pages_isolated() has a tracepoint mm/page_isolation.c +5
-5@@ -49,7 +49,6 @@ static int set_migratetype_isolate(struct page *page, int migratetype, int isol___mod_zone_freepage_state (zone, -nr_pages, mt);spin_unlock_irqrestore (&zone->lock, flags); -drain_all_pages (zone);return 0 ; }@@ -167,11 +166,12 @@ __first_valid_page(unsigned long pfn, unsigned long nr_pages)* * Please note that there is no strong synchronization with the page allocator * either. Pages might be freed while their page blocks are marked ISOLATED. -* In some cases pages might still end up on pcp lists and that would allow +* A call to drain_all_pages() after isolation can flush most of them. However +* in some cases pages might still end up on pcp lists and that would allow * for their allocation even when they are in fact isolated already. Depending -* on how strong of a guarantee the caller needs drain_all_pages might be needed -* (e.g. __offline_pages will need to call it after check for isolated range for -* a next retry). +* on how strong of a guarantee the caller needs, further drain_all_pages() +* might be needed (e.g. __offline_pages will need to call it after check for +* isolated range for a next retry). * * Return: 0 on success and -EBUSY if any part of range cannot be isolated. */ Signature · 1 line
2.28.0
Signature · 2 lines
Michal Hocko SUSE Labs
PATCH 8/9 mm, page_alloc: drain all pcplists during memory offline
In reply to Vlastimil Babkadrain_all_pages() is optimized to only execute on cpus where pcplists are not empty. The check can however race with a free to pcplist that has not yet increased the pcp->count from 0 to 1. Make the drain optionally skip the racy check and drain on all cpus, and use it in memory offline context, where we want to make sure no isolated pages are left behind on pcplists.
--- include/linux/gfp.h | 1 + mm/memory_hotplug.c | 4 ++-- mm/page_alloc.c | 29 ++++++++++++++++++++--------- 3 files changed, 23 insertions(+), 11 deletions(-)
include/linux/gfp.h +1 -0
@@ -592,6 +592,7 @@ extern void page_frag_free(void *addr);void page_alloc_init (void );void drain_zone_pages (struct zone *zone,struct per_cpu_pages *pcp); +void __drain_all_pages (struct zone *zone,bool page_isolation);void drain_all_pages (struct zone *zone);void drain_local_pages (struct zone *zone);
mm/memory_hotplug.c +2 -2
@@ -1524,7 +1524,7 @@ int __ref offline_pages(unsigned long start_pfn, unsigned long nr_pages) goto failed_removal; } -drain_all_pages (zone); +__drain_all_pages (zone, true); arg.start_pfn = start_pfn; arg.nr_pages = nr_pages;
@@ -1588,7 +1588,7 @@ int __ref offline_pages(unsigned long start_pfn, unsigned long nr_pages)*/ ret =test_pages_isolated (start_pfn, end_pfn,MEMORY_OFFLINE );if (ret) -drain_all_pages (zone); +__drain_all_pages (zone, true); }while (ret);/* Mark all sections offline and remove free pages from the buddy. */
mm/page_alloc.c +20 -9
@@ -2960,14 +2960,7 @@ static void drain_local_pages_wq(struct work_struct *work)preempt_enable (); } -/* -* Spill all the per-cpu pages from all CPUs back into the buddy allocator. -* -* When zone parameter is non-NULL, spill just the single zone's pages. -* -* Note that this can be extremely slow as the draining happens in a workqueue. -*/ -void drain_all_pages (struct zone *zone) +void __drain_all_pages (struct zone *zone,bool force_all_cpus) {int cpu;
@@ -3006,7 +2999,13 @@ void drain_all_pages(struct zone *zone)struct zone *z;bool has_pcps = false; -if (zone) { +if (force_all_cpus) { +/* +* The pcp.count check is racy, some callers need a +* guarantee that no cpu is missed. +*/ + has_pcps = true; + }else if (zone) { pcp =per_cpu_ptr (zone->pageset, cpu);if (pcp->pcp.count) has_pcps = true;
@@ -3039,6 +3038,18 @@ void drain_all_pages(struct zone *zone)mutex_unlock (&pcpu_drain_mutex); } +/* +* Spill all the per-cpu pages from all CPUs back into the buddy allocator. +* +* When zone parameter is non-NULL, spill just the single zone's pages. +* +* Note that this can be extremely slow as the draining happens in a workqueue. +*/ +void drain_all_pages (struct zone *zone) +{ +__drain_all_pages (zone, false); +} +#ifdef CONFIG_HIBERNATION /*
Signature · 1 line
2.28.0
Re: [PATCH 8/9] mm, page_alloc: drain all pcplists during memory offline
Quoted 92 lines · Vlastimil Babka
drain_all_pages()is optimized to only execute on cpus where pcplists are not empty. The check can however race with a free to pcplist that has not yet increased thepcp->countfrom 0 to 1. Make the drain optionally skip the racy check and drain on all cpus, and use it in memory offline context, where we want to make sure no isolated pages are left behind on pcplists.Signed-off-by Vlastimil Babka <[email protected]>--- include/linux/gfp.h | 1 + mm/memory_hotplug.c | 4 ++-- mm/page_alloc.c | 29 ++++++++++++++++++++--------- 3 files changed, 23 insertions(+), 11 deletions(-)include/linux/gfp.h +1
-0@@ -592,6 +592,7 @@ extern void page_frag_free(void *addr);void page_alloc_init (void );void drain_zone_pages (struct zone *zone,struct per_cpu_pages *pcp); +void __drain_all_pages (struct zone *zone,bool page_isolation);void drain_all_pages (struct zone *zone);void drain_local_pages (struct zone *zone);mm/memory_hotplug.c +2
-2@@ -1524,7 +1524,7 @@ int __ref offline_pages(unsigned long start_pfn, unsigned long nr_pages) goto failed_removal; } -drain_all_pages (zone); +__drain_all_pages (zone, true); arg.start_pfn = start_pfn; arg.nr_pages = nr_pages;@@ -1588,7 +1588,7 @@ int __ref offline_pages(unsigned long start_pfn, unsigned long nr_pages)*/ ret =test_pages_isolated (start_pfn, end_pfn,MEMORY_OFFLINE );if (ret) -drain_all_pages (zone); +__drain_all_pages (zone, true); }while (ret);/* Mark all sections offline and remove free pages from the buddy. */ mm/page_alloc.c +13
-9@@ -2960,14 +2960,7 @@ static void drain_local_pages_wq(struct work_struct *work)preempt_enable (); } -/* -* Spill all the per-cpu pages from all CPUs back into the buddy allocator. -* -* When zone parameter is non-NULL, spill just the single zone's pages. -* -* Note that this can be extremely slow as the draining happens in a workqueue. -*/ -void drain_all_pages (struct zone *zone) +void __drain_all_pages (struct zone *zone,bool force_all_cpus) {int cpu;@@ -3006,7 +2999,13 @@ void drain_all_pages(struct zone *zone)struct zone *z;bool has_pcps = false; -if (zone) { +if (force_all_cpus) { +/* +* The pcp.count check is racy, some callers need a +* guarantee that no cpu is missed. +*/ + has_pcps = true; + }else if (zone) { pcp =per_cpu_ptr (zone->pageset, cpu);if (pcp->pcp.count) has_pcps = true;@@ -3039,6 +3038,18 @@ void drain_all_pages(struct zone *zone)mutex_unlock (&pcpu_drain_mutex); } +/* +* Spill all the per-cpu pages from all CPUs back into the buddy allocator. +* +* When zone parameter is non-NULL, spill just the single zone's pages. +*
mm/page_alloc.c void drain_all_pages(struct zone *zone) · line 3055
Quoted 10 lines
mm/page_alloc.c +7
-0+* Note that this can be extremely slow as the draining happens in a workqueue. +*/ +void drain_all_pages (struct zone *zone) +{ +__drain_all_pages (zone, false); +} +#ifdef CONFIG_HIBERNATION /*
Interesting race. Instead of this ugly __drain_all_pages() with a boolean parameter, can we have two properly named functions to be used in !page_alloc.c code without scratching your head what the difference is?
(yeah, coming up with a proper name is difficult. the one gives more guarantees than the other, that cannot really be deducted from "force_all_cpus" - maybe we can encode the actual semantics in the name)
Signature · 3 lines
Thanks, David / dhildenb
Michal Hocko
reviewerRe: [PATCH 8/9] mm, page_alloc: drain all pcplists during memory offline
Quoted 1 line · David Hildenbrand
On 22.09.20 16:37, Vlastimil Babka wrote:
[...]
Quoted 14 lines
Quoted 14 lines
mm/page_alloc.c +12
-0+/* +* Spill all the per-cpu pages from all CPUs back into the buddy allocator. +* +* When zone parameter is non-NULL, spill just the single zone's pages. +* +* Note that this can be extremely slow as the draining happens in a workqueue. +*/ +void drain_all_pages (struct zone *zone) +{ +__drain_all_pages (zone, false); +} +#ifdef CONFIG_HIBERNATION
Quoted 6 lines
Quoted 2 lines
/*
Interesting race. Instead of this ugly
__drain_all_pages()with a boolean parameter, can we have two properly named functions to be used in !page_alloc.c code without scratching your head what the difference is?
I tend to agree here. I would even fold this into the next patch because disable/enable interface is much more manageable.
Signature · 2 lines
Michal Hocko SUSE Labs
PATCH 9/9 mm, page_alloc: optionally disable pcplists during page isolation
In reply to Vlastimil BabkaPage isolation can race with process freeing pages to pcplists in a way that a page from isolated pageblock can end up on pcplist. This can be fixed by repeated draining of pcplists, as done by patch "mm/memory_hotplug: drain per-cpu pages again during memory offline" in [1].
David and Michal would prefer that this race was closed in a way that callers of page isolation who need stronger guarantees don't need to repeatedly drain. David suggested disabling pcplists usage completely during page isolation, instead of repeatedly draining them.
To achieve this without adding special cases in alloc/free fastpath, we can use the same approach as boot pagesets - when pcp->high is 0, any pcplist addition will be immediately flushed.
The race can thus be closed by setting pcp->high to 0 and draining pcplists once, before calling start_isolate_page_range(). The draining will serialize after processes that already disabled interrupts and read the old value of pcp->high in free_unref_page_commit(), and processes that have not yet disabled interrupts, will observe pcp->high == 0 when they are rescheduled, and skip pcplists. This guarantees no stray pages on pcplists in zones where isolation happens.
This patch thus adds zone_pcplist_disable() and zone_pcplist_enable() functions that page isolation users can call before start_isolate_page_range() and after unisolating (or offlining) the isolated pages. A new zone->pcplist_disabled atomic variable makes sure we disable only pcplists once and don't enable them prematurely in case there are multiple users in parallel.
We however have to avoid external updates to high and batch by taking pcp_batch_high_lock. To allow multiple isolations in parallel, change this lock from mutex to rwsem.
Currently the only user of this functionality is offline_pages().
[1] https://lore.kernel.org/linux-mm/[email protected]/
--- include/linux/mmzone.h | 2 ++ include/linux/page-isolation.h | 2 ++ mm/internal.h | 4 ++++ mm/memory_hotplug.c | 28 ++++++++++++---------------- mm/page_alloc.c | 29 ++++++++++++++++++----------- mm/page_isolation.c | 22 +++++++++++++++++++--- 6 files changed, 57 insertions(+), 30 deletions(-)
include/linux/mmzone.h +2 -0
@@ -536,6 +536,8 @@ struct zone {* of pageblock. Protected by zone->lock. */ unsigned long nr_isolate_pageblock; +/* Track requests for disabling pcplists */ +atomic_t pcplist_disabled;#endif #ifdef CONFIG_MEMORY_HOTPLUG
include/linux/page-isolation.h +2 -0
@@ -38,6 +38,8 @@ struct page *has_unmovable_pages(struct zone *zone, struct page *page,void set_pageblock_migratetype (struct page *page,int migratetype);int move_freepages_block (struct zone *zone,struct page *page,int migratetype,int *num_movable); +void zone_pcplist_disable (struct zone *zone); +void zone_pcplist_enable (struct zone *zone);/* * Changes migrate type in [start_pfn, end_pfn) to be MIGRATE_ISOLATE.
mm/internal.h +4 -0
@@ -7,6 +7,7 @@#ifndef __MM_INTERNAL_H#define __MM_INTERNAL_H +#include <linux/rwsem.h> #include <linux/fs.h> #include <linux/mm.h> #include <linux/pagemap.h>
@@ -199,8 +200,11 @@ extern void post_alloc_hook(struct page *page, unsigned int order, gfp_t gfp_flags);extern int user_min_free_kbytes; +extern struct rw_semaphore pcp_batch_high_lock;extern void zone_pcp_update (struct zone *zone);extern void zone_pcp_reset (struct zone *zone); +extern void zone_update_pageset_high_and_batch (struct zone *zone, +unsigned long high,unsigned long batch);#if definedCONFIG_COMPACTION || definedCONFIG_CMA
mm/memory_hotplug.c +12 -16
@@ -1515,17 +1515,21 @@ int __ref offline_pages(unsigned long start_pfn, unsigned long nr_pages) } node =zone_to_nid (zone); +/* +* Disable pcplists so that page isolation cannot race with freeing +* in a way that pages from isolated pageblock are left on pcplists. +*/ +zone_pcplist_disable (zone); +/* set above range as isolated */ ret =start_isolate_page_range (start_pfn, end_pfn,MIGRATE_MOVABLE ,MEMORY_OFFLINE |REPORT_FAILURE );if (ret) { reason ="failure to isolate range" ; - goto failed_removal; + goto failed_removal_pcplists_disabled; } -__drain_all_pages (zone, true); - arg.start_pfn = start_pfn; arg.nr_pages = nr_pages;node_states_check_changes_offline (nr_pages, zone, &arg);
@@ -1575,20 +1579,8 @@ int __ref offline_pages(unsigned long start_pfn, unsigned long nr_pages) goto failed_removal_isolated; } -/* -* per-cpu pages are drained after start_isolate_page_range, but -* if there are still pages that are not free, make sure that we -* drain again, because when we isolated range we might have -* raced with another thread that was adding pages to pcp list. -* -* Forward progress should be still guaranteed because -* pages on the pcp list can only belong to MOVABLE_ZONE -* because has_unmovable_pages explicitly checks for -* PageBuddy on freed pages on other zones. -*/ ret =test_pages_isolated (start_pfn, end_pfn,MEMORY_OFFLINE ); -if (ret) -__drain_all_pages (zone, true); + }while (ret);/* Mark all sections offline and remove free pages from the buddy. */
@@ -1604,6 +1596,8 @@ int __ref offline_pages(unsigned long start_pfn, unsigned long nr_pages) zone->nr_isolate_pageblock -= nr_pages / pageblock_nr_pages;spin_unlock_irqrestore (&zone->lock, flags); +zone_pcplist_enable (zone); +/* removal success */ adjust_managed_page_count (pfn_to_page (start_pfn), -nr_pages); zone->present_pages -= nr_pages;
@@ -1637,6 +1631,8 @@ int __ref offline_pages(unsigned long start_pfn, unsigned long nr_pages) failed_removal_isolated:undo_isolate_page_range (start_pfn, end_pfn,MIGRATE_MOVABLE );memory_notify (MEM_CANCEL_OFFLINE , &arg); +failed_removal_pcplists_disabled: +zone_pcplist_enable (zone); failed_removal: pr_debug("memory offlining [mem %#010llx-%#010llx] failed due to %s\n", (unsigned long long) start_pfn << PAGE_SHIFT,
mm/page_alloc.c +18 -11
@@ -78,7 +78,7 @@#include "page_reporting.h" /* prevent >1 _updater_ of zone percpu pageset ->high and ->batch fields */ -static DEFINE_MUTEX (pcp_batch_high_lock); +DECLARE_RWSEM (pcp_batch_high_lock);#define MIN_PERCPU_PAGELIST_FRACTION (8)#ifdef CONFIG_USE_PERCPU_NUMA_NODE_ID
@@ -6230,6 +6230,18 @@ static void pageset_init(struct per_cpu_pageset *p) pcp->batch =BOOT_PAGESET_BATCH ; } +void zone_update_pageset_high_and_batch (struct zone *zone,unsigned long high, +unsigned long batch) +{ +struct per_cpu_pageset *p; +int cpu; + +for_each_possible_cpu (cpu) { + p =per_cpu_ptr (zone->pageset, cpu); +pageset_update (&p->pcp, high, batch); + } +} +/* * Calculate and set new high and batch values for all per-cpu pagesets of a * zone, based on the zone's size and the percpu_pagelist_fraction sysctl.
@@ -6237,8 +6249,6 @@ static void pageset_init(struct per_cpu_pageset *p)static void zone_set_pageset_high_and_batch (struct zone *zone) {unsigned long new_high, new_batch; -struct per_cpu_pageset *p; -int cpu;if (percpu_pagelist_fraction) { new_high =zone_managed_pages (zone) / percpu_pagelist_fraction;
@@ -6259,10 +6269,7 @@ static void zone_set_pageset_high_and_batch(struct zone *zone)return ; } -for_each_possible_cpu (cpu) { - p =per_cpu_ptr (zone->pageset, cpu); -pageset_update (&p->pcp, new_high, new_batch); - } +zone_update_pageset_high_and_batch (zone, new_high, new_batch); }void __meminit setup_zone_pageset (struct zone *zone)
@@ -8024,7 +8031,7 @@ int percpu_pagelist_fraction_sysctl_handler(struct ctl_table *table, int write,int old_percpu_pagelist_fraction;int ret; -mutex_lock (&pcp_batch_high_lock); +down_write (&pcp_batch_high_lock); old_percpu_pagelist_fraction = percpu_pagelist_fraction; ret =proc_dointvec_minmax (table, write, buffer, length, ppos);
@@ -8046,7 +8053,7 @@ int percpu_pagelist_fraction_sysctl_handler(struct ctl_table *table, int write,for_each_populated_zone (zone)zone_set_pageset_high_and_batch (zone); out: -mutex_unlock (&pcp_batch_high_lock); +up_write (&pcp_batch_high_lock);return ret; }
@@ -8652,9 +8659,9 @@ EXPORT_SYMBOL(free_contig_range);*/ void __meminit zone_pcp_update (struct zone *zone) { -mutex_lock (&pcp_batch_high_lock); +down_write (&pcp_batch_high_lock);zone_set_pageset_high_and_batch (zone); -mutex_unlock (&pcp_batch_high_lock); +up_write (&pcp_batch_high_lock); }void zone_pcp_reset (struct zone *zone)
mm/page_isolation.c +19 -3
@@ -15,6 +15,22 @@#define CREATE_TRACE_POINTS #include <trace/events/page_isolation.h> +void zone_pcplist_disable (struct zone *zone) +{ +down_read (&pcp_batch_high_lock); +if (atomic_inc_return (&zone->pcplist_disabled) ==1 ) { +zone_update_pageset_high_and_batch (zone,0 ,1 ); +__drain_all_pages (zone, true); + } +} + +void zone_pcplist_enable (struct zone *zone) +{ +if (atomic_dec_return (&zone->pcplist_disabled) ==0 ) +zone_update_pageset_high_and_batch (zone, zone->pageset_high, zone->pageset_batch); +up_read (&pcp_batch_high_lock); +} +static int set_migratetype_isolate (struct page *page,int migratetype,int isol_flags) {struct zone *zone =page_zone (page);
@@ -169,9 +185,9 @@ __first_valid_page(unsigned long pfn, unsigned long nr_pages)* A call to drain_all_pages() after isolation can flush most of them. However * in some cases pages might still end up on pcp lists and that would allow * for their allocation even when they are in fact isolated already. Depending -* on how strong of a guarantee the caller needs, further drain_all_pages() -* might be needed (e.g. __offline_pages will need to call it after check for -* isolated range for a next retry). +* on how strong of a guarantee the caller needs, zone_pcplist_disable/enable() +* might be used to flush and disable pcplist before isolation and after +* unisolation. * * Return: 0 on success and -EBUSY if any part of range cannot be isolated. */
Signature · 1 line
2.28.0
Re: [PATCH 9/9] mm, page_alloc: optionally disable pcplists during page isolation
Quoted 255 lines · Vlastimil Babka
Page isolation can race with process freeing pages to pcplists in a way that a page from isolated pageblock can end up on pcplist. This can be fixed by repeated draining of pcplists, as done by patch "
mm/memory_hotplug: drain per-cpu pages again during memory offline" in [1].David and Michal would prefer that this race was closed in a way that callers of page isolation who need stronger guarantees don't need to repeatedly drain. David suggested disabling pcplists usage completely during page isolation, instead of repeatedly draining them.
To achieve this without adding special cases in alloc/free fastpath, we can use the same approach as boot pagesets - when
pcp->highis 0, any pcplist addition will be immediately flushed.The race can thus be closed by setting
pcp->highto 0 and draining pcplists once, before callingstart_isolate_page_range(). The draining will serialize after processes that already disabled interrupts and read the old value ofpcp->highinfree_unref_page_commit(), and processes that have not yet disabled interrupts, will observepcp->high== 0 when they are rescheduled, and skip pcplists. This guarantees no stray pages on pcplists in zones where isolation happens.This patch thus adds
zone_pcplist_disable()andzone_pcplist_enable()functions that page isolation users can call beforestart_isolate_page_range()and after unisolating (or offlining) the isolated pages. A newzone->pcplist_disabledatomic variable makes sure we disable only pcplists once and don't enable them prematurely in case there are multiple users in parallel.We however have to avoid external updates to high and batch by taking
pcp_batch_high_lock. To allow multiple isolations in parallel, change this lock from mutex to rwsem.Currently the only user of this functionality is
offline_pages().[1] https://lore.kernel.org/linux-mm/[email protected]/
Suggested-by David Hildenbrand <[email protected]>Suggested-by Michal Hocko <[email protected]>Signed-off-by Vlastimil Babka <[email protected]>--- include/linux/mmzone.h | 2 ++ include/linux/page-isolation.h | 2 ++ mm/internal.h | 4 ++++ mm/memory_hotplug.c | 28 ++++++++++++---------------- mm/page_alloc.c | 29 ++++++++++++++++++----------- mm/page_isolation.c | 22 +++++++++++++++++++--- 6 files changed, 57 insertions(+), 30 deletions(-)include/linux/mmzone.h +2
-0@@ -536,6 +536,8 @@ struct zone {* of pageblock. Protected by zone->lock. */ unsigned long nr_isolate_pageblock; +/* Track requests for disabling pcplists */ +atomic_t pcplist_disabled;#endif #ifdef CONFIG_MEMORY_HOTPLUG include/linux/page-isolation.h +2
-0@@ -38,6 +38,8 @@ struct page *has_unmovable_pages(struct zone *zone, struct page *page,void set_pageblock_migratetype (struct page *page,int migratetype);int move_freepages_block (struct zone *zone,struct page *page,int migratetype,int *num_movable); +void zone_pcplist_disable (struct zone *zone); +void zone_pcplist_enable (struct zone *zone);/* * Changes migrate type in [start_pfn, end_pfn) to be MIGRATE_ISOLATE. mm/internal.h +4
-0@@ -7,6 +7,7 @@#ifndef __MM_INTERNAL_H#define __MM_INTERNAL_H +#include <linux/rwsem.h> #include <linux/fs.h> #include <linux/mm.h> #include <linux/pagemap.h> @@ -199,8 +200,11 @@ extern void post_alloc_hook(struct page *page, unsigned int order, gfp_t gfp_flags);extern int user_min_free_kbytes; +extern struct rw_semaphore pcp_batch_high_lock;extern void zone_pcp_update (struct zone *zone);extern void zone_pcp_reset (struct zone *zone); +extern void zone_update_pageset_high_and_batch (struct zone *zone, +unsigned long high,unsigned long batch);#if definedCONFIG_COMPACTION || definedCONFIG_CMA mm/memory_hotplug.c +12
-16@@ -1515,17 +1515,21 @@ int __ref offline_pages(unsigned long start_pfn, unsigned long nr_pages) } node =zone_to_nid (zone); +/* +* Disable pcplists so that page isolation cannot race with freeing +* in a way that pages from isolated pageblock are left on pcplists. +*/ +zone_pcplist_disable (zone); +/* set above range as isolated */ ret =start_isolate_page_range (start_pfn, end_pfn,MIGRATE_MOVABLE ,MEMORY_OFFLINE |REPORT_FAILURE );if (ret) { reason ="failure to isolate range" ; - goto failed_removal; + goto failed_removal_pcplists_disabled; } -__drain_all_pages (zone, true); - arg.start_pfn = start_pfn; arg.nr_pages = nr_pages;node_states_check_changes_offline (nr_pages, zone, &arg);@@ -1575,20 +1579,8 @@ int __ref offline_pages(unsigned long start_pfn, unsigned long nr_pages) goto failed_removal_isolated; } -/* -* per-cpu pages are drained after start_isolate_page_range, but -* if there are still pages that are not free, make sure that we -* drain again, because when we isolated range we might have -* raced with another thread that was adding pages to pcp list. -* -* Forward progress should be still guaranteed because -* pages on the pcp list can only belong to MOVABLE_ZONE -* because has_unmovable_pages explicitly checks for -* PageBuddy on freed pages on other zones. -*/ ret =test_pages_isolated (start_pfn, end_pfn,MEMORY_OFFLINE ); -if (ret) -__drain_all_pages (zone, true); + }while (ret);/* Mark all sections offline and remove free pages from the buddy. */ @@ -1604,6 +1596,8 @@ int __ref offline_pages(unsigned long start_pfn, unsigned long nr_pages) zone->nr_isolate_pageblock -= nr_pages / pageblock_nr_pages;spin_unlock_irqrestore (&zone->lock, flags); +zone_pcplist_enable (zone); +/* removal success */ adjust_managed_page_count (pfn_to_page (start_pfn), -nr_pages); zone->present_pages -= nr_pages;@@ -1637,6 +1631,8 @@ int __ref offline_pages(unsigned long start_pfn, unsigned long nr_pages) failed_removal_isolated:undo_isolate_page_range (start_pfn, end_pfn,MIGRATE_MOVABLE );memory_notify (MEM_CANCEL_OFFLINE , &arg); +failed_removal_pcplists_disabled: +zone_pcplist_enable (zone); failed_removal: pr_debug("memory offlining [mem %#010llx-%#010llx] failed due to %s\n", (unsigned long long) start_pfn << PAGE_SHIFT,mm/page_alloc.c +18
-11@@ -78,7 +78,7 @@#include "page_reporting.h" /* prevent >1 _updater_ of zone percpu pageset ->high and ->batch fields */ -static DEFINE_MUTEX (pcp_batch_high_lock); +DECLARE_RWSEM (pcp_batch_high_lock);#define MIN_PERCPU_PAGELIST_FRACTION (8)#ifdef CONFIG_USE_PERCPU_NUMA_NODE_ID @@ -6230,6 +6230,18 @@ static void pageset_init(struct per_cpu_pageset *p) pcp->batch =BOOT_PAGESET_BATCH ; } +void zone_update_pageset_high_and_batch (struct zone *zone,unsigned long high, +unsigned long batch) +{ +struct per_cpu_pageset *p; +int cpu; + +for_each_possible_cpu (cpu) { + p =per_cpu_ptr (zone->pageset, cpu); +pageset_update (&p->pcp, high, batch); + } +} +/* * Calculate and set new high and batch values for all per-cpu pagesets of a * zone, based on the zone's size and the percpu_pagelist_fraction sysctl. @@ -6237,8 +6249,6 @@ static void pageset_init(struct per_cpu_pageset *p)static void zone_set_pageset_high_and_batch (struct zone *zone) {unsigned long new_high, new_batch; -struct per_cpu_pageset *p; -int cpu;if (percpu_pagelist_fraction) { new_high =zone_managed_pages (zone) / percpu_pagelist_fraction;@@ -6259,10 +6269,7 @@ static void zone_set_pageset_high_and_batch(struct zone *zone)return ; } -for_each_possible_cpu (cpu) { - p =per_cpu_ptr (zone->pageset, cpu); -pageset_update (&p->pcp, new_high, new_batch); - } +zone_update_pageset_high_and_batch (zone, new_high, new_batch); }void __meminit setup_zone_pageset (struct zone *zone)@@ -8024,7 +8031,7 @@ int percpu_pagelist_fraction_sysctl_handler(struct ctl_table *table, int write,int old_percpu_pagelist_fraction;int ret; -mutex_lock (&pcp_batch_high_lock); +down_write (&pcp_batch_high_lock); old_percpu_pagelist_fraction = percpu_pagelist_fraction; ret =proc_dointvec_minmax (table, write, buffer, length, ppos);@@ -8046,7 +8053,7 @@ int percpu_pagelist_fraction_sysctl_handler(struct ctl_table *table, int write,for_each_populated_zone (zone)zone_set_pageset_high_and_batch (zone); out: -mutex_unlock (&pcp_batch_high_lock); +up_write (&pcp_batch_high_lock);return ret; }@@ -8652,9 +8659,9 @@ EXPORT_SYMBOL(free_contig_range);*/ void __meminit zone_pcp_update (struct zone *zone) { -mutex_lock (&pcp_batch_high_lock); +down_write (&pcp_batch_high_lock);zone_set_pageset_high_and_batch (zone); -mutex_unlock (&pcp_batch_high_lock); +up_write (&pcp_batch_high_lock); }void zone_pcp_reset (struct zone *zone)mm/page_isolation.c +0
-0
mm/page_isolation.c line 24
Quoted 10 lines
mm/page_isolation.c +7
-0@@ -15,6 +15,22 @@#define CREATE_TRACE_POINTS #include <trace/events/page_isolation.h> +void zone_pcplist_disable (struct zone *zone) +{ +down_read (&pcp_batch_high_lock); +if (atomic_inc_return (&zone->pcplist_disabled) ==1 ) { +zone_update_pageset_high_and_batch (zone,0 ,1 ); +__drain_all_pages (zone, true); + }
Hm, if one CPU is still inside the if-clause, the other one would continue, however pcp wpould not be disabled and zones not drained when returning.
(while we only allow a single Offline_pages() call, it will be different when we use the function in other context - especially, alloc_contig_range() for some users)
Can't we use down_write() here? So it's serialized and everybody has to properly wait. (and we would not have to rely on an atomic_t)
mm/page_isolation.c line 36
Quoted 12 lines
mm/page_isolation.c +9
-0+} + +void zone_pcplist_enable (struct zone *zone) +{ +if (atomic_dec_return (&zone->pcplist_disabled) ==0 ) +zone_update_pageset_high_and_batch (zone, zone->pageset_high, zone->pageset_batch); +up_read (&pcp_batch_high_lock); +} +static int set_migratetype_isolate (struct page *page,int migratetype,int isol_flags) {struct zone *zone =page_zone (page);
In general, I like that approach. But I do wonder if we can tweak the rwsem + atomic_t handling.
Signature · 3 lines
Thanks, David / dhildenb
Re: [PATCH 9/9] mm, page_alloc: optionally disable pcplists during page isolation
Quoted 271 lines · David Hildenbrand
Quoted 255 lines · Vlastimil Babka
Page isolation can race with process freeing pages to pcplists in a way that a page from isolated pageblock can end up on pcplist. This can be fixed by repeated draining of pcplists, as done by patch "
mm/memory_hotplug: drain per-cpu pages again during memory offline" in [1].David and Michal would prefer that this race was closed in a way that callers of page isolation who need stronger guarantees don't need to repeatedly drain. David suggested disabling pcplists usage completely during page isolation, instead of repeatedly draining them.
To achieve this without adding special cases in alloc/free fastpath, we can use the same approach as boot pagesets - when
pcp->highis 0, any pcplist addition will be immediately flushed.The race can thus be closed by setting
pcp->highto 0 and draining pcplists once, before callingstart_isolate_page_range(). The draining will serialize after processes that already disabled interrupts and read the old value ofpcp->highinfree_unref_page_commit(), and processes that have not yet disabled interrupts, will observepcp->high== 0 when they are rescheduled, and skip pcplists. This guarantees no stray pages on pcplists in zones where isolation happens.This patch thus adds
zone_pcplist_disable()andzone_pcplist_enable()functions that page isolation users can call beforestart_isolate_page_range()and after unisolating (or offlining) the isolated pages. A newzone->pcplist_disabledatomic variable makes sure we disable only pcplists once and don't enable them prematurely in case there are multiple users in parallel.We however have to avoid external updates to high and batch by taking
pcp_batch_high_lock. To allow multiple isolations in parallel, change this lock from mutex to rwsem.Currently the only user of this functionality is
offline_pages().[1] https://lore.kernel.org/linux-mm/[email protected]/
Suggested-by David Hildenbrand <[email protected]>Suggested-by Michal Hocko <[email protected]>Signed-off-by Vlastimil Babka <[email protected]>--- include/linux/mmzone.h | 2 ++ include/linux/page-isolation.h | 2 ++ mm/internal.h | 4 ++++ mm/memory_hotplug.c | 28 ++++++++++++---------------- mm/page_alloc.c | 29 ++++++++++++++++++----------- mm/page_isolation.c | 22 +++++++++++++++++++--- 6 files changed, 57 insertions(+), 30 deletions(-)include/linux/mmzone.h +2
-0@@ -536,6 +536,8 @@ struct zone {* of pageblock. Protected by zone->lock. */ unsigned long nr_isolate_pageblock; +/* Track requests for disabling pcplists */ +atomic_t pcplist_disabled;#endif #ifdef CONFIG_MEMORY_HOTPLUG include/linux/page-isolation.h +2
-0@@ -38,6 +38,8 @@ struct page *has_unmovable_pages(struct zone *zone, struct page *page,void set_pageblock_migratetype (struct page *page,int migratetype);int move_freepages_block (struct zone *zone,struct page *page,int migratetype,int *num_movable); +void zone_pcplist_disable (struct zone *zone); +void zone_pcplist_enable (struct zone *zone);/* * Changes migrate type in [start_pfn, end_pfn) to be MIGRATE_ISOLATE. mm/internal.h +4
-0@@ -7,6 +7,7 @@#ifndef __MM_INTERNAL_H#define __MM_INTERNAL_H +#include <linux/rwsem.h> #include <linux/fs.h> #include <linux/mm.h> #include <linux/pagemap.h> @@ -199,8 +200,11 @@ extern void post_alloc_hook(struct page *page, unsigned int order, gfp_t gfp_flags);extern int user_min_free_kbytes; +extern struct rw_semaphore pcp_batch_high_lock;extern void zone_pcp_update (struct zone *zone);extern void zone_pcp_reset (struct zone *zone); +extern void zone_update_pageset_high_and_batch (struct zone *zone, +unsigned long high,unsigned long batch);#if definedCONFIG_COMPACTION || definedCONFIG_CMA mm/memory_hotplug.c +12
-16@@ -1515,17 +1515,21 @@ int __ref offline_pages(unsigned long start_pfn, unsigned long nr_pages) } node =zone_to_nid (zone); +/* +* Disable pcplists so that page isolation cannot race with freeing +* in a way that pages from isolated pageblock are left on pcplists. +*/ +zone_pcplist_disable (zone); +/* set above range as isolated */ ret =start_isolate_page_range (start_pfn, end_pfn,MIGRATE_MOVABLE ,MEMORY_OFFLINE |REPORT_FAILURE );if (ret) { reason ="failure to isolate range" ; - goto failed_removal; + goto failed_removal_pcplists_disabled; } -__drain_all_pages (zone, true); - arg.start_pfn = start_pfn; arg.nr_pages = nr_pages;node_states_check_changes_offline (nr_pages, zone, &arg);@@ -1575,20 +1579,8 @@ int __ref offline_pages(unsigned long start_pfn, unsigned long nr_pages) goto failed_removal_isolated; } -/* -* per-cpu pages are drained after start_isolate_page_range, but -* if there are still pages that are not free, make sure that we -* drain again, because when we isolated range we might have -* raced with another thread that was adding pages to pcp list. -* -* Forward progress should be still guaranteed because -* pages on the pcp list can only belong to MOVABLE_ZONE -* because has_unmovable_pages explicitly checks for -* PageBuddy on freed pages on other zones. -*/ ret =test_pages_isolated (start_pfn, end_pfn,MEMORY_OFFLINE ); -if (ret) -__drain_all_pages (zone, true); + }while (ret);/* Mark all sections offline and remove free pages from the buddy. */ @@ -1604,6 +1596,8 @@ int __ref offline_pages(unsigned long start_pfn, unsigned long nr_pages) zone->nr_isolate_pageblock -= nr_pages / pageblock_nr_pages;spin_unlock_irqrestore (&zone->lock, flags); +zone_pcplist_enable (zone); +/* removal success */ adjust_managed_page_count (pfn_to_page (start_pfn), -nr_pages); zone->present_pages -= nr_pages;@@ -1637,6 +1631,8 @@ int __ref offline_pages(unsigned long start_pfn, unsigned long nr_pages) failed_removal_isolated:undo_isolate_page_range (start_pfn, end_pfn,MIGRATE_MOVABLE );memory_notify (MEM_CANCEL_OFFLINE , &arg); +failed_removal_pcplists_disabled: +zone_pcplist_enable (zone); failed_removal: pr_debug("memory offlining [mem %#010llx-%#010llx] failed due to %s\n", (unsigned long long) start_pfn << PAGE_SHIFT,mm/page_alloc.c +18
-11@@ -78,7 +78,7 @@#include "page_reporting.h" /* prevent >1 _updater_ of zone percpu pageset ->high and ->batch fields */ -static DEFINE_MUTEX (pcp_batch_high_lock); +DECLARE_RWSEM (pcp_batch_high_lock);#define MIN_PERCPU_PAGELIST_FRACTION (8)#ifdef CONFIG_USE_PERCPU_NUMA_NODE_ID @@ -6230,6 +6230,18 @@ static void pageset_init(struct per_cpu_pageset *p) pcp->batch =BOOT_PAGESET_BATCH ; } +void zone_update_pageset_high_and_batch (struct zone *zone,unsigned long high, +unsigned long batch) +{ +struct per_cpu_pageset *p; +int cpu; + +for_each_possible_cpu (cpu) { + p =per_cpu_ptr (zone->pageset, cpu); +pageset_update (&p->pcp, high, batch); + } +} +/* * Calculate and set new high and batch values for all per-cpu pagesets of a * zone, based on the zone's size and the percpu_pagelist_fraction sysctl. @@ -6237,8 +6249,6 @@ static void pageset_init(struct per_cpu_pageset *p)static void zone_set_pageset_high_and_batch (struct zone *zone) {unsigned long new_high, new_batch; -struct per_cpu_pageset *p; -int cpu;if (percpu_pagelist_fraction) { new_high =zone_managed_pages (zone) / percpu_pagelist_fraction;@@ -6259,10 +6269,7 @@ static void zone_set_pageset_high_and_batch(struct zone *zone)return ; } -for_each_possible_cpu (cpu) { - p =per_cpu_ptr (zone->pageset, cpu); -pageset_update (&p->pcp, new_high, new_batch); - } +zone_update_pageset_high_and_batch (zone, new_high, new_batch); }void __meminit setup_zone_pageset (struct zone *zone)@@ -8024,7 +8031,7 @@ int percpu_pagelist_fraction_sysctl_handler(struct ctl_table *table, int write,int old_percpu_pagelist_fraction;int ret; -mutex_lock (&pcp_batch_high_lock); +down_write (&pcp_batch_high_lock); old_percpu_pagelist_fraction = percpu_pagelist_fraction; ret =proc_dointvec_minmax (table, write, buffer, length, ppos);@@ -8046,7 +8053,7 @@ int percpu_pagelist_fraction_sysctl_handler(struct ctl_table *table, int write,for_each_populated_zone (zone)zone_set_pageset_high_and_batch (zone); out: -mutex_unlock (&pcp_batch_high_lock); +up_write (&pcp_batch_high_lock);return ret; }@@ -8652,9 +8659,9 @@ EXPORT_SYMBOL(free_contig_range);*/ void __meminit zone_pcp_update (struct zone *zone) { -mutex_lock (&pcp_batch_high_lock); +down_write (&pcp_batch_high_lock);zone_set_pageset_high_and_batch (zone); -mutex_unlock (&pcp_batch_high_lock); +up_write (&pcp_batch_high_lock); }void zone_pcp_reset (struct zone *zone)mm/page_isolation.c +0
-0mm/page_isolation.c line 24
Quoted 10 lines
mm/page_isolation.c +7
-0@@ -15,6 +15,22 @@#define CREATE_TRACE_POINTS #include <trace/events/page_isolation.h> +void zone_pcplist_disable (struct zone *zone) +{ +down_read (&pcp_batch_high_lock); +if (atomic_inc_return (&zone->pcplist_disabled) ==1 ) { +zone_update_pageset_high_and_batch (zone,0 ,1 ); +__drain_all_pages (zone, true); + }Hm, if one CPU is still inside the if-clause, the other one would continue, however pcp wpould not be disabled and zones not drained when returning.
Quoted 6 lines
(while we only allow a single
Offline_pages()call, it will be different when we use the function in other context - especially,alloc_contig_range()for some users)Can't we use
down_write()here? So it's serialized and everybody has to properly wait. (and we would not have to rely on anatomic_t)
Sorry, I meant down_write only temporarily in this code path. Not keeping it locked in write when returning (I remember there is a way to downgrade).
Signature · 3 lines
Thanks, David / dhildenb
Re: [PATCH 9/9] mm, page_alloc: optionally disable pcplists during page isolation
Quoted 6 lines · David Hildenbrand
Quoted 6 lines
Quoted 6 lines
mm/page_isolation.c +0
-0@@ -15,6 +15,22 @@#define CREATE_TRACE_POINTS #include <trace/events/page_isolation.h>
Quoted 10 lines
Quoted 10 lines
mm/page_isolation.c line 24
Quoted 7 lines
mm/page_isolation.c +7
-0+void zone_pcplist_disable (struct zone *zone) +{ +down_read (&pcp_batch_high_lock); +if (atomic_inc_return (&zone->pcplist_disabled) ==1 ) { +zone_update_pageset_high_and_batch (zone,0 ,1 ); +__drain_all_pages (zone, true); + }Hm, if one CPU is still inside the if-clause, the other one would continue, however pcp wpould not be disabled and zones not drained when returning.
Ah, well spotted, thanks!
Quoted 9 lines
Quoted 6 lines
(while we only allow a single
Offline_pages()call, it will be different when we use the function in other context - especially,alloc_contig_range()for some users)Can't we use
down_write()here? So it's serialized and everybody has to properly wait. (and we would not have to rely on anatomic_t)Sorry, I meant
down_writeonly temporarily in this code path. Not keeping it locked in write when returning (I remember there is a way to downgrade).
Hmm that temporary write lock would still block new callers until previous finish with the downgraded-to-read lock.
But I guess something like this would work:
retry:
if (atomic_read(...) == 0) {
// zone_update... + drain
atomic_inc(...);
else if (atomic_inc_return == 1)
// atomic_cmpxchg from 0 to 1; if that fails, goto retryTricky, but races could only read to unnecessary duplicated updates + flushing but nothing worse?
Or add another spinlock to cover this part instead of the temp write lock...
Re: [PATCH 9/9] mm, page_alloc: optionally disable pcplists during page isolation
Quoted 42 lines · Vlastimil Babka
Quoted 6 lines · David Hildenbrand
Quoted 6 lines
Quoted 6 lines
mm/page_isolation.c +0
-0@@ -15,6 +15,22 @@#define CREATE_TRACE_POINTS #include <trace/events/page_isolation.h> Quoted 10 lines
Quoted 10 lines
mm/page_isolation.c line 24
Quoted 7 lines
mm/page_isolation.c +7
-0+void zone_pcplist_disable (struct zone *zone) +{ +down_read (&pcp_batch_high_lock); +if (atomic_inc_return (&zone->pcplist_disabled) ==1 ) { +zone_update_pageset_high_and_batch (zone,0 ,1 ); +__drain_all_pages (zone, true); + }Hm, if one CPU is still inside the if-clause, the other one would continue, however pcp wpould not be disabled and zones not drained when returning.
Ah, well spotted, thanks!
Quoted 9 lines
Quoted 6 lines
(while we only allow a single
Offline_pages()call, it will be different when we use the function in other context - especially,alloc_contig_range()for some users)Can't we use
down_write()here? So it's serialized and everybody has to properly wait. (and we would not have to rely on anatomic_t)Sorry, I meant
down_writeonly temporarily in this code path. Not keeping it locked in write when returning (I remember there is a way to downgrade).Hmm that temporary write lock would still block new callers until previous finish with the downgraded-to-read lock.
But I guess something like this would work:
retry:
if (atomic_read(...) == 0) { // zone_update... + drain atomic_inc(...); else if (atomic_inc_return == 1) // atomic_cmpxchg from 0 to 1; if that fails, goto retry
Quoted 4 lines
Tricky, but races could only read to unnecessary duplicated updates + flushing but nothing worse?
Or add another spinlock to cover this part instead of the temp write lock...
My gut feeling is, that that would be the cleanest approach.
Signature · 3 lines
Thanks, David / dhildenb
Michal Hocko
reviewerRe: [PATCH 9/9] mm, page_alloc: optionally disable pcplists during page isolation
In reply to Vlastimil BabkaQuoted 42 lines · Vlastimil Babka
Quoted 6 lines · David Hildenbrand
Quoted 6 lines
Quoted 6 lines
mm/page_isolation.c +0
-0@@ -15,6 +15,22 @@#define CREATE_TRACE_POINTS #include <trace/events/page_isolation.h> Quoted 10 lines
Quoted 10 lines
mm/page_isolation.c line 24
Quoted 7 lines
mm/page_isolation.c +7
-0+void zone_pcplist_disable (struct zone *zone) +{ +down_read (&pcp_batch_high_lock); +if (atomic_inc_return (&zone->pcplist_disabled) ==1 ) { +zone_update_pageset_high_and_batch (zone,0 ,1 ); +__drain_all_pages (zone, true); + }Hm, if one CPU is still inside the if-clause, the other one would continue, however pcp wpould not be disabled and zones not drained when returning.
Ah, well spotted, thanks!
Quoted 9 lines
Quoted 6 lines
(while we only allow a single
Offline_pages()call, it will be different when we use the function in other context - especially,alloc_contig_range()for some users)Can't we use
down_write()here? So it's serialized and everybody has to properly wait. (and we would not have to rely on anatomic_t)Sorry, I meant
down_writeonly temporarily in this code path. Not keeping it locked in write when returning (I remember there is a way to downgrade).Hmm that temporary write lock would still block new callers until previous finish with the downgraded-to-read lock.
But I guess something like this would work:
retry:
if (atomic_read(...) == 0) { // zone_update... + drain atomic_inc(...); else if (atomic_inc_return == 1) // atomic_cmpxchg from 0 to 1; if that fails, goto retry
Quoted 4 lines
Tricky, but races could only read to unnecessary duplicated updates + flushing but nothing worse?
Or add another spinlock to cover this part instead of the temp write lock...
Do you plan to post a new version or should I review this one?
Signature · 2 lines
Michal Hocko SUSE Labs
Re: [PATCH 9/9] mm, page_alloc: optionally disable pcplists during page isolation
Quoted 15 lines · Michal Hocko
Quoted 14 lines · Vlastimil Babka
On 9/25/20 12:54 PM, David Hildenbrand wrote:
Hmm that temporary write lock would still block new callers until previous finish with the downgraded-to-read lock.
But I guess something like this would work:
retry:
if (atomic_read(...) == 0) { // zone_update... + drain atomic_inc(...); else if (atomic_inc_return == 1) // atomic_cmpxchg from 0 to 1; if that fails, goto retry
Quoted 6 lines
Quoted 4 lines
Tricky, but races could only read to unnecessary duplicated updates + flushing but nothing worse?
Or add another spinlock to cover this part instead of the temp write lock...
Do you plan to post a new version or should I review this one?
I will, hopefully this week, but you could comment on other details and overall approach meanwhile? I don't think it will change significantly.
Michal Hocko
reviewerRe: [PATCH 9/9] mm, page_alloc: optionally disable pcplists during page isolation
Quoted 19 lines · Vlastimil Babka
Quoted 18 lines · Michal Hocko
Quoted 17 lines · Vlastimil Babka
On 9/25/20 12:54 PM, David Hildenbrand wrote:
Hmm that temporary write lock would still block new callers until previous finish with the downgraded-to-read lock.
But I guess something like this would work:
retry:
if (atomic_read(...) == 0) { // zone_update... + drain atomic_inc(...); else if (atomic_inc_return == 1) // atomic_cmpxchg from 0 to 1; if that fails, goto retryTricky, but races could only read to unnecessary duplicated updates + flushing but nothing worse?
Quoted 6 lines
Quoted 3 lines
Quoted 1 line
Or add another spinlock to cover this part instead of the temp write lock...
Do you plan to post a new version or should I review this one?
I will, hopefully this week, but you could comment on other details and overall approach meanwhile? I don't think it will change significantly.
OK. I will have a look tomorrow.
Signature · 2 lines
Michal Hocko SUSE Labs
Michal Hocko
reviewerRe: [PATCH 9/9] mm, page_alloc: optionally disable pcplists during page isolation
In reply to Vlastimil BabkaQuoted 28 lines · Vlastimil Babka
Page isolation can race with process freeing pages to pcplists in a way that a page from isolated pageblock can end up on pcplist. This can be fixed by repeated draining of pcplists, as done by patch "
mm/memory_hotplug: drain per-cpu pages again during memory offline" in [1].David and Michal would prefer that this race was closed in a way that callers of page isolation who need stronger guarantees don't need to repeatedly drain. David suggested disabling pcplists usage completely during page isolation, instead of repeatedly draining them.
To achieve this without adding special cases in alloc/free fastpath, we can use the same approach as boot pagesets - when
pcp->highis 0, any pcplist addition will be immediately flushed.The race can thus be closed by setting
pcp->highto 0 and draining pcplists once, before callingstart_isolate_page_range(). The draining will serialize after processes that already disabled interrupts and read the old value ofpcp->highinfree_unref_page_commit(), and processes that have not yet disabled interrupts, will observepcp->high== 0 when they are rescheduled, and skip pcplists. This guarantees no stray pages on pcplists in zones where isolation happens.This patch thus adds
zone_pcplist_disable()andzone_pcplist_enable()functions that page isolation users can call beforestart_isolate_page_range()and after unisolating (or offlining) the isolated pages. A newzone->pcplist_disabledatomic variable makes sure we disable only pcplists once and don't enable them prematurely in case there are multiple users in parallel.
Quoted 3 lines
We however have to avoid external updates to high and batch by taking
pcp_batch_high_lock. To allow multiple isolations in parallel, change this lock from mutex to rwsem.
The overall idea makes sense. I just suspect you are over overcomplicating the implementation a bit. Is there any reason that we cannot start with a really dumb implementation first. The only user of this functionality is the memory offlining and that is already strongly synchronized (mem_hotplug_begin) so a lot of trickery can be dropped here. Should we find a new user later on we can make the implementation finer grained but now it will not serve any purpose. So can we simply update pcp->high and drain all pcp in the given zone and wait for all remote pcp draining in zone_pcplist_enable and updte revert all that in zone_pcplist_enable. We can stick to the existing pcp_batch_high_lock.
What do you think?
Btw. zone_pcplist_{enable,disable} would benefit from a documentation explaining the purpose and guarantees. And side effect on the performance. I would even stick this interface to internal.h
Quoted 260 lines
Currently the only user of this functionality is
offline_pages().[1] https://lore.kernel.org/linux-mm/[email protected]/
Suggested-by David Hildenbrand <[email protected]>Suggested-by Michal Hocko <[email protected]>Signed-off-by Vlastimil Babka <[email protected]>--- include/linux/mmzone.h | 2 ++ include/linux/page-isolation.h | 2 ++ mm/internal.h | 4 ++++ mm/memory_hotplug.c | 28 ++++++++++++---------------- mm/page_alloc.c | 29 ++++++++++++++++++----------- mm/page_isolation.c | 22 +++++++++++++++++++--- 6 files changed, 57 insertions(+), 30 deletions(-)include/linux/mmzone.h +2
-0@@ -536,6 +536,8 @@ struct zone {* of pageblock. Protected by zone->lock. */ unsigned long nr_isolate_pageblock; +/* Track requests for disabling pcplists */ +atomic_t pcplist_disabled;#endif #ifdef CONFIG_MEMORY_HOTPLUG include/linux/page-isolation.h +2
-0@@ -38,6 +38,8 @@ struct page *has_unmovable_pages(struct zone *zone, struct page *page,void set_pageblock_migratetype (struct page *page,int migratetype);int move_freepages_block (struct zone *zone,struct page *page,int migratetype,int *num_movable); +void zone_pcplist_disable (struct zone *zone); +void zone_pcplist_enable (struct zone *zone);/* * Changes migrate type in [start_pfn, end_pfn) to be MIGRATE_ISOLATE. mm/internal.h +4
-0@@ -7,6 +7,7 @@#ifndef __MM_INTERNAL_H#define __MM_INTERNAL_H +#include <linux/rwsem.h> #include <linux/fs.h> #include <linux/mm.h> #include <linux/pagemap.h> @@ -199,8 +200,11 @@ extern void post_alloc_hook(struct page *page, unsigned int order, gfp_t gfp_flags);extern int user_min_free_kbytes; +extern struct rw_semaphore pcp_batch_high_lock;extern void zone_pcp_update (struct zone *zone);extern void zone_pcp_reset (struct zone *zone); +extern void zone_update_pageset_high_and_batch (struct zone *zone, +unsigned long high,unsigned long batch);#if definedCONFIG_COMPACTION || definedCONFIG_CMA mm/memory_hotplug.c +12
-16@@ -1515,17 +1515,21 @@ int __ref offline_pages(unsigned long start_pfn, unsigned long nr_pages) } node =zone_to_nid (zone); +/* +* Disable pcplists so that page isolation cannot race with freeing +* in a way that pages from isolated pageblock are left on pcplists. +*/ +zone_pcplist_disable (zone); +/* set above range as isolated */ ret =start_isolate_page_range (start_pfn, end_pfn,MIGRATE_MOVABLE ,MEMORY_OFFLINE |REPORT_FAILURE );if (ret) { reason ="failure to isolate range" ; - goto failed_removal; + goto failed_removal_pcplists_disabled; } -__drain_all_pages (zone, true); - arg.start_pfn = start_pfn; arg.nr_pages = nr_pages;node_states_check_changes_offline (nr_pages, zone, &arg);@@ -1575,20 +1579,8 @@ int __ref offline_pages(unsigned long start_pfn, unsigned long nr_pages) goto failed_removal_isolated; } -/* -* per-cpu pages are drained after start_isolate_page_range, but -* if there are still pages that are not free, make sure that we -* drain again, because when we isolated range we might have -* raced with another thread that was adding pages to pcp list. -* -* Forward progress should be still guaranteed because -* pages on the pcp list can only belong to MOVABLE_ZONE -* because has_unmovable_pages explicitly checks for -* PageBuddy on freed pages on other zones. -*/ ret =test_pages_isolated (start_pfn, end_pfn,MEMORY_OFFLINE ); -if (ret) -__drain_all_pages (zone, true); + }while (ret);/* Mark all sections offline and remove free pages from the buddy. */ @@ -1604,6 +1596,8 @@ int __ref offline_pages(unsigned long start_pfn, unsigned long nr_pages) zone->nr_isolate_pageblock -= nr_pages / pageblock_nr_pages;spin_unlock_irqrestore (&zone->lock, flags); +zone_pcplist_enable (zone); +/* removal success */ adjust_managed_page_count (pfn_to_page (start_pfn), -nr_pages); zone->present_pages -= nr_pages;@@ -1637,6 +1631,8 @@ int __ref offline_pages(unsigned long start_pfn, unsigned long nr_pages) failed_removal_isolated:undo_isolate_page_range (start_pfn, end_pfn,MIGRATE_MOVABLE );memory_notify (MEM_CANCEL_OFFLINE , &arg); +failed_removal_pcplists_disabled: +zone_pcplist_enable (zone); failed_removal: pr_debug("memory offlining [mem %#010llx-%#010llx] failed due to %s\n", (unsigned long long) start_pfn << PAGE_SHIFT,mm/page_alloc.c +18
-11@@ -78,7 +78,7 @@#include "page_reporting.h" /* prevent >1 _updater_ of zone percpu pageset ->high and ->batch fields */ -static DEFINE_MUTEX (pcp_batch_high_lock); +DECLARE_RWSEM (pcp_batch_high_lock);#define MIN_PERCPU_PAGELIST_FRACTION (8)#ifdef CONFIG_USE_PERCPU_NUMA_NODE_ID @@ -6230,6 +6230,18 @@ static void pageset_init(struct per_cpu_pageset *p) pcp->batch =BOOT_PAGESET_BATCH ; } +void zone_update_pageset_high_and_batch (struct zone *zone,unsigned long high, +unsigned long batch) +{ +struct per_cpu_pageset *p; +int cpu; + +for_each_possible_cpu (cpu) { + p =per_cpu_ptr (zone->pageset, cpu); +pageset_update (&p->pcp, high, batch); + } +} +/* * Calculate and set new high and batch values for all per-cpu pagesets of a * zone, based on the zone's size and the percpu_pagelist_fraction sysctl. @@ -6237,8 +6249,6 @@ static void pageset_init(struct per_cpu_pageset *p)static void zone_set_pageset_high_and_batch (struct zone *zone) {unsigned long new_high, new_batch; -struct per_cpu_pageset *p; -int cpu;if (percpu_pagelist_fraction) { new_high =zone_managed_pages (zone) / percpu_pagelist_fraction;@@ -6259,10 +6269,7 @@ static void zone_set_pageset_high_and_batch(struct zone *zone)return ; } -for_each_possible_cpu (cpu) { - p =per_cpu_ptr (zone->pageset, cpu); -pageset_update (&p->pcp, new_high, new_batch); - } +zone_update_pageset_high_and_batch (zone, new_high, new_batch); }void __meminit setup_zone_pageset (struct zone *zone)@@ -8024,7 +8031,7 @@ int percpu_pagelist_fraction_sysctl_handler(struct ctl_table *table, int write,int old_percpu_pagelist_fraction;int ret; -mutex_lock (&pcp_batch_high_lock); +down_write (&pcp_batch_high_lock); old_percpu_pagelist_fraction = percpu_pagelist_fraction; ret =proc_dointvec_minmax (table, write, buffer, length, ppos);@@ -8046,7 +8053,7 @@ int percpu_pagelist_fraction_sysctl_handler(struct ctl_table *table, int write,for_each_populated_zone (zone)zone_set_pageset_high_and_batch (zone); out: -mutex_unlock (&pcp_batch_high_lock); +up_write (&pcp_batch_high_lock);return ret; }@@ -8652,9 +8659,9 @@ EXPORT_SYMBOL(free_contig_range);*/ void __meminit zone_pcp_update (struct zone *zone) { -mutex_lock (&pcp_batch_high_lock); +down_write (&pcp_batch_high_lock);zone_set_pageset_high_and_batch (zone); -mutex_unlock (&pcp_batch_high_lock); +up_write (&pcp_batch_high_lock); }void zone_pcp_reset (struct zone *zone)mm/page_isolation.c +19
-3@@ -15,6 +15,22 @@#define CREATE_TRACE_POINTS #include <trace/events/page_isolation.h> +void zone_pcplist_disable (struct zone *zone) +{ +down_read (&pcp_batch_high_lock); +if (atomic_inc_return (&zone->pcplist_disabled) ==1 ) { +zone_update_pageset_high_and_batch (zone,0 ,1 ); +__drain_all_pages (zone, true); + } +} + +void zone_pcplist_enable (struct zone *zone) +{ +if (atomic_dec_return (&zone->pcplist_disabled) ==0 ) +zone_update_pageset_high_and_batch (zone, zone->pageset_high, zone->pageset_batch); +up_read (&pcp_batch_high_lock); +} +static int set_migratetype_isolate (struct page *page,int migratetype,int isol_flags) {struct zone *zone =page_zone (page);@@ -169,9 +185,9 @@ __first_valid_page(unsigned long pfn, unsigned long nr_pages)* A call to drain_all_pages() after isolation can flush most of them. However * in some cases pages might still end up on pcp lists and that would allow * for their allocation even when they are in fact isolated already. Depending -* on how strong of a guarantee the caller needs, further drain_all_pages() -* might be needed (e.g. __offline_pages will need to call it after check for -* isolated range for a next retry). +* on how strong of a guarantee the caller needs, zone_pcplist_disable/enable() +* might be used to flush and disable pcplist before isolation and after +* unisolation. * * Return: 0 on success and -EBUSY if any part of range cannot be isolated. */ Signature · 1 line
2.28.0
Signature · 2 lines
Michal Hocko SUSE Labs
Re: [PATCH 9/9] mm, page_alloc: optionally disable pcplists during page isolation
Quoted 44 lines · Michal Hocko
Quoted 28 lines · Vlastimil Babka
Page isolation can race with process freeing pages to pcplists in a way that a page from isolated pageblock can end up on pcplist. This can be fixed by repeated draining of pcplists, as done by patch "
mm/memory_hotplug: drain per-cpu pages again during memory offline" in [1].David and Michal would prefer that this race was closed in a way that callers of page isolation who need stronger guarantees don't need to repeatedly drain. David suggested disabling pcplists usage completely during page isolation, instead of repeatedly draining them.
To achieve this without adding special cases in alloc/free fastpath, we can use the same approach as boot pagesets - when
pcp->highis 0, any pcplist addition will be immediately flushed.The race can thus be closed by setting
pcp->highto 0 and draining pcplists once, before callingstart_isolate_page_range(). The draining will serialize after processes that already disabled interrupts and read the old value ofpcp->highinfree_unref_page_commit(), and processes that have not yet disabled interrupts, will observepcp->high== 0 when they are rescheduled, and skip pcplists. This guarantees no stray pages on pcplists in zones where isolation happens.This patch thus adds
zone_pcplist_disable()andzone_pcplist_enable()functions that page isolation users can call beforestart_isolate_page_range()and after unisolating (or offlining) the isolated pages. A newzone->pcplist_disabledatomic variable makes sure we disable only pcplists once and don't enable them prematurely in case there are multiple users in parallel.Quoted 3 lines
We however have to avoid external updates to high and batch by taking
pcp_batch_high_lock. To allow multiple isolations in parallel, change this lock from mutex to rwsem.The overall idea makes sense. I just suspect you are over overcomplicating the implementation a bit. Is there any reason that we cannot start with a really dumb implementation first. The only user of this functionality is the memory offlining and that is already strongly synchronized (
mem_hotplug_begin) so a lot of trickery can be dropped here. Should we find a new user later on we can make the implementation finer grained but now it will not serve any purpose. So can we simply updatepcp->highand drain all pcp in the given zone and wait for all remote pcp draining inzone_pcplist_enableand updte revert all that inzone_pcplist_enable. We can stick to the existingpcp_batch_high_lock.
Quoted 2 lines
What do you think?
My two cents, we might want to make use of this in some cases of alloc_contig_range() soon ("try hard mode"). So I'd love to see a synchronized mechanism. However, that can be factored out into a separate patch, so this patch gets significantly simpler.
Signature · 3 lines
Thanks, David / dhildenb
Michal Hocko
reviewerRe: [PATCH 9/9] mm, page_alloc: optionally disable pcplists during page isolation
Quoted 45 lines · David Hildenbrand
Quoted 44 lines · Michal Hocko
Quoted 28 lines · Vlastimil Babka
Page isolation can race with process freeing pages to pcplists in a way that a page from isolated pageblock can end up on pcplist. This can be fixed by repeated draining of pcplists, as done by patch "
mm/memory_hotplug: drain per-cpu pages again during memory offline" in [1].David and Michal would prefer that this race was closed in a way that callers of page isolation who need stronger guarantees don't need to repeatedly drain. David suggested disabling pcplists usage completely during page isolation, instead of repeatedly draining them.
To achieve this without adding special cases in alloc/free fastpath, we can use the same approach as boot pagesets - when
pcp->highis 0, any pcplist addition will be immediately flushed.The race can thus be closed by setting
pcp->highto 0 and draining pcplists once, before callingstart_isolate_page_range(). The draining will serialize after processes that already disabled interrupts and read the old value ofpcp->highinfree_unref_page_commit(), and processes that have not yet disabled interrupts, will observepcp->high== 0 when they are rescheduled, and skip pcplists. This guarantees no stray pages on pcplists in zones where isolation happens.This patch thus adds
zone_pcplist_disable()andzone_pcplist_enable()functions that page isolation users can call beforestart_isolate_page_range()and after unisolating (or offlining) the isolated pages. A newzone->pcplist_disabledatomic variable makes sure we disable only pcplists once and don't enable them prematurely in case there are multiple users in parallel.Quoted 3 lines
We however have to avoid external updates to high and batch by taking
pcp_batch_high_lock. To allow multiple isolations in parallel, change this lock from mutex to rwsem.The overall idea makes sense. I just suspect you are over overcomplicating the implementation a bit. Is there any reason that we cannot start with a really dumb implementation first. The only user of this functionality is the memory offlining and that is already strongly synchronized (
mem_hotplug_begin) so a lot of trickery can be dropped here. Should we find a new user later on we can make the implementation finer grained but now it will not serve any purpose. So can we simply updatepcp->highand drain all pcp in the given zone and wait for all remote pcp draining inzone_pcplist_enableand updte revert all that inzone_pcplist_enable. We can stick to the existingpcp_batch_high_lock.
Quoted 7 lines
Quoted 2 lines
What do you think?
My two cents, we might want to make use of this in some cases of
alloc_contig_range()soon ("try hard mode"). So I'd love to see a synchronized mechanism. However, that can be factored out into a separate patch, so this patch gets significantly simpler.
Exactly. And the incremental patch can be added along with the a-c-r try harder mode.
Signature · 2 lines
Michal Hocko SUSE Labs
Quoted 21 lines · Vlastimil Babka
As per the discussions [1] [2] this is an attempt to implement David's suggestion that page isolation should disable pcplists to avoid races with page freeing in progress. This is done without extra checks in fast paths, as explained in Patch 9. The repeated draining done by [2] is then no longer needed. Previous version (RFC) is at [3].
The RFC tried to hide pcplists disabling/enabling into page isolation, but it wasn't completely possible, as memory offline does not unisolation. Michal suggested an explicit API in [4] so that's the current implementation and it seems indeed nicer.
Once we accept that page isolation users need to do explicit actions around it depending on the needed guarantees, we can also IMHO accept that the current pcplist draining can be also done by the callers, which is more effective. After all, there are only two users of page isolation. So patch 7 does effectively the same thing as Pavel proposed in [5], and patches 8-9 implement stronger guarantees only for memory offline. If CMA decides to opt-in to the stronger guarantee, it's easy to do so.
Patches 1-6 are preparatory cleanups for pcplist disabling.
Quoted 8 lines
Patchset was briefly tested in QEMU so that memory online/offline works, but I haven't done a stress test that would prove the race fixed by [2] is eliminated.
Note that patch 9 could be avoided if we instead adjusted page freeing in shown in [6], but I believe the current implementation of disabling pcplists is not too much complex, so I would prefer this instead of adding new checks and longer irq-disabled section into page freeing hotpaths.
Haven't looked into the details (yet), but I assume we can add some flag to alloc_contig_range(), to also disable+flush+enable. (or let the caller do it, for example on a bunch of bulk allocations - TBD).
Result of patch #9 looks quite clean.
Signature · 3 lines
Thanks, David / dhildenb