Skip to content

Commit

Permalink
mm, page_alloc: use static global work_struct for draining per-cpu pages
Browse files Browse the repository at this point in the history
As suggested by Vlastimil Babka and Tejun Heo, this patch uses a static
work_struct to co-ordinate the draining of per-cpu pages on the
workqueue.  Only one task can drain at a time but this is better than
the previous scheme that allowed multiple tasks to send IPIs at a time.

One consideration is whether parallel requests should synchronise
against each other.  This patch does not synchronise for a global drain
as the common case for such callers is expected to be multiple parallel
direct reclaimers competing for pages when the watermark is close to
min.  Draining the per-cpu list is unlikely to make much progress and
serialising the drain is of dubious merit.  Drains are synchonrised for
callers such as memory hotplug and CMA that care about the drain being
complete when the function returns.

Link: http://lkml.kernel.org/r/[email protected]
Signed-off-by: Mel Gorman <[email protected]>
Suggested-by: Tejun Heo <[email protected]>
Suggested-by: Vlastimil Babka <[email protected]>
Acked-by: Vlastimil Babka <[email protected]>
Acked-by: Hillf Danton <[email protected]>
Cc: Jesper Dangaard Brouer <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
gormanm authored and torvalds committed Feb 25, 2017
1 parent 5104782 commit bd233f5
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions mm/page_alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ EXPORT_PER_CPU_SYMBOL(_numa_mem_);
int _node_numa_mem_[MAX_NUMNODES];
#endif

/* work_structs for global per-cpu drains */
DEFINE_MUTEX(pcpu_drain_mutex);
DEFINE_PER_CPU(struct work_struct, pcpu_drain);

#ifdef CONFIG_GCC_PLUGIN_LATENT_ENTROPY
volatile unsigned long latent_entropy __latent_entropy;
EXPORT_SYMBOL(latent_entropy);
Expand Down Expand Up @@ -2360,7 +2364,6 @@ static void drain_local_pages_wq(struct work_struct *work)
*/
void drain_all_pages(struct zone *zone)
{
struct work_struct __percpu *works;
int cpu;

/*
Expand All @@ -2373,7 +2376,16 @@ void drain_all_pages(struct zone *zone)
if (current->flags & PF_WQ_WORKER)
return;

works = alloc_percpu_gfp(struct work_struct, GFP_ATOMIC);
/*
* Do not drain if one is already in progress unless it's specific to
* a zone. Such callers are primarily CMA and memory hotplug and need
* the drain to be complete when the call returns.
*/
if (unlikely(!mutex_trylock(&pcpu_drain_mutex))) {
if (!zone)
return;
mutex_lock(&pcpu_drain_mutex);
}

/*
* We don't care about racing with CPU hotplug event
Expand Down Expand Up @@ -2406,23 +2418,15 @@ void drain_all_pages(struct zone *zone)
cpumask_clear_cpu(cpu, &cpus_with_pcps);
}

if (works) {
for_each_cpu(cpu, &cpus_with_pcps) {
struct work_struct *work = per_cpu_ptr(works, cpu);
INIT_WORK(work, drain_local_pages_wq);
schedule_work_on(cpu, work);
}
for_each_cpu(cpu, &cpus_with_pcps)
flush_work(per_cpu_ptr(works, cpu));
} else {
for_each_cpu(cpu, &cpus_with_pcps) {
struct work_struct work;

INIT_WORK(&work, drain_local_pages_wq);
schedule_work_on(cpu, &work);
flush_work(&work);
}
for_each_cpu(cpu, &cpus_with_pcps) {
struct work_struct *work = per_cpu_ptr(&pcpu_drain, cpu);
INIT_WORK(work, drain_local_pages_wq);
schedule_work_on(cpu, work);
}
for_each_cpu(cpu, &cpus_with_pcps)
flush_work(per_cpu_ptr(&pcpu_drain, cpu));

mutex_unlock(&pcpu_drain_mutex);
}

#ifdef CONFIG_HIBERNATION
Expand Down

0 comments on commit bd233f5

Please sign in to comment.