From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 670F2C433F5 for ; Fri, 8 Oct 2021 16:19:57 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 0B60C61019 for ; Fri, 8 Oct 2021 16:19:57 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 0B60C61019 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id AE0D36B0074; Fri, 8 Oct 2021 12:19:56 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9CEAE900002; Fri, 8 Oct 2021 12:19:56 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 84A0C6B0078; Fri, 8 Oct 2021 12:19:56 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 732E66B0074 for ; Fri, 8 Oct 2021 12:19:56 -0400 (EDT) Received: from smtpin27.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 21AE92CBB9 for ; Fri, 8 Oct 2021 16:19:56 +0000 (UTC) X-FDA: 78673781592.27.CA74CEC Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf29.hostedemail.com (Postfix) with ESMTP id AD0739002BFE for ; Fri, 8 Oct 2021 16:19:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1633709995; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=L06CeZVaZT/gwZhsOIEkX6vSsXsDjU7RBD3vrAJ3vYE=; b=B3y8dQAB/VzWgiryrbFljO6v3TpRtkqQZOaj4iy2b/4F1xE5UJBKGQLmDNDV4+1XY8GPdT CLLqr+lxH0ki1xlf1vAZDQR7HhADjG+/Eavw2Wv2qAhwBtxV1GiXNdXqf3EvPAgOflVYdv jOlOm6uqGAUhsYCht9kDwQ8ZbgBOPTY= Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-270-JB0kShi-MjCLWVjvxK2bVQ-1; Fri, 08 Oct 2021 12:19:38 -0400 X-MC-Unique: JB0kShi-MjCLWVjvxK2bVQ-1 Received: by mail-wr1-f71.google.com with SMTP id r25-20020adfab59000000b001609ddd5579so7707825wrc.21 for ; Fri, 08 Oct 2021 09:19:37 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=L06CeZVaZT/gwZhsOIEkX6vSsXsDjU7RBD3vrAJ3vYE=; b=Rxk05dz7HE1bRXxSM1T0ZvN1BSQ71zGe3fpQDCTnlFAio/rRMY4vVqhMnLU3qRoUca /XPH1ofhJ/sBLSsJqfA5lld+e+tJDmQIGVj7Pj4LnjWpVIXWvCnbRT6yHIgcCOIdKUAW KVLsieJsmJ/qOj7xPKDOz1gRx5u4JANwY6ze88/X1PqN7FQePARq45Cz78LSnrFnGNAl XFzgop8Y8kiddkSroNvzig/pgCaVB870dIIwhL+8by2po0L80TL/aeaXadLpCr12tvoF /c/1oiGiNgNtvu6A9fY4T4jYnp7eps098um8Q8riRSciZ4KxsYngkfqOwrCW0Tw8r6c+ Xnew== X-Gm-Message-State: AOAM531oabpQ37nkyD7Y8S7eV/1Nb7DETtYaSBrJWCxslXFx6ltR1ARP lCv4KVLhv2/hQ/zDDiHJWq8+RhXPt6r9JAV/PwiOgRzFhk/7eMALPieiq1FsvjcpIOOXdI8Qpda NQnpHxURay+s= X-Received: by 2002:adf:a347:: with SMTP id d7mr5432860wrb.139.1633709976763; Fri, 08 Oct 2021 09:19:36 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzu56ejsI05RU1p5BB9pvX/swKCZbkkrpJVl0uXpJwrS/cA6sAiV/Eyj4pPabDtwM7aHct1Yg== X-Received: by 2002:adf:a347:: with SMTP id d7mr5432815wrb.139.1633709976480; Fri, 08 Oct 2021 09:19:36 -0700 (PDT) Received: from vian.redhat.com ([2a0c:5a80:1d03:b900:c3d1:5974:ce92:3123]) by smtp.gmail.com with ESMTPSA id f184sm2901753wmf.22.2021.10.08.09.19.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Oct 2021 09:19:35 -0700 (PDT) From: Nicolas Saenz Julienne To: akpm@linux-foundation.org Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, frederic@kernel.org, tglx@linutronix.de, peterz@infradead.org, mtosatti@redhat.com, nilal@redhat.com, mgorman@suse.de, linux-rt-users@vger.kernel.org, vbabka@suse.cz, cl@linux.com, paulmck@kernel.org, ppandit@redhat.com, Nicolas Saenz Julienne Subject: [RFC 3/3] mm/page_alloc: Add remote draining support to per-cpu lists Date: Fri, 8 Oct 2021 18:19:22 +0200 Message-Id: <20211008161922.942459-4-nsaenzju@redhat.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20211008161922.942459-1-nsaenzju@redhat.com> References: <20211008161922.942459-1-nsaenzju@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="US-ASCII" X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: AD0739002BFE X-Stat-Signature: huwcwcq967ahignmyijqxbm6njfoa45e Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=B3y8dQAB; dmarc=pass (policy=none) header.from=redhat.com; spf=none (imf29.hostedemail.com: domain of nsaenzju@redhat.com has no SPF policy when checking 170.10.133.124) smtp.mailfrom=nsaenzju@redhat.com X-HE-Tag: 1633709995-588305 Content-Transfer-Encoding: quoted-printable X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: page_alloc.c's per-cpu page lists are currently protected using local locks. While performance savvy, this doesn't allow for remote access to these structures. CPUs requiring system-wide per-cpu list drains get around this by scheduling drain work on all CPUs. That said, some select setups like systems with NOHZ_FULL CPUs, aren't well suited to this, as they can't handle interruptions of any sort. To mitigate this, replace the current draining mechanism with one that allows remotely draining the lists in a lock-less manner. It leverages the fact that the per-cpu page lists are accessed through indirection, and that the pointer can be updated atomically. Upon draining we now: - Atomically switch the per-cpu lists pointers to ones pointing to empty lists. - Wait for a grace period so as for all concurrent writers holding the old per-cpu lists pointer finish updating them[1]. - Remotely flush the old lists now that we know nobody holds a reference to them. Concurrent access to the drain process is protected by a mutex. RCU guarantees atomicity both while dereferencing the per-cpu lists pointer and replacing it. It also checks for RCU critical section/locking correctness, as all writers have to hold their per-cpu pagesets local lock. Memory ordering on both pointers' data is guaranteed by synchronize_rcu() and the 'pcpu_drain_mutex'. Also, synchronize_rcu_expedited() is used to minimize hangs during low memory situations. Accesses to the pcplists like the ones in mm/vmstat.c don't require RCU supervision since they can handle outdated data, but they do use READ_ONCE() in order to avoid compiler weirdness and be explicit about the concurrent nature of the pcplists pointer. As a side effect to all this we now have to promote the spin_lock() in free_pcppages_bulk() to spin_lock_irqsave() since not all function users enter with interrupts disabled. Signed-off-by: Nicolas Saenz Julienne [1] Note that whatever concurrent writers were doing, the result was going to be flushed anyway as the old mechanism disabled preemption as the means for serialization, so per-cpu drain works were already stepping over whatever was being processed concurrently to the drain call. --- include/linux/mmzone.h | 18 ++++++- mm/page_alloc.c | 114 ++++++++++++++++++++--------------------- mm/vmstat.c | 6 +-- 3 files changed, 75 insertions(+), 63 deletions(-) diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h index fb023da9a181..c112e7831c54 100644 --- a/include/linux/mmzone.h +++ b/include/linux/mmzone.h @@ -365,13 +365,27 @@ struct per_cpu_pages { short expire; /* When 0, remote pagesets are drained */ #endif =20 - struct pcplists *lp; + /* + * Having two pcplists allows us to remotely flush them in a lock-less + * manner: we atomically switch the 'lp' and 'drain' pointers, wait a + * grace period to synchronize against concurrent users of 'lp', and + * safely free whatever is left in 'drain'. + * + * All accesses to 'lp' are protected by local locks, which also serve + * as RCU critical section delimiters. 'lp' should only be dereferenced + * *once* per critical section. + * + * See mm/page_alloc.c's __drain_all_pages() for the bulk of the remote + * drain implementation. + */ + struct pcplists __rcu *lp; + struct pcplists *drain; struct pcplists { /* Number of pages in the lists */ int count; /* Lists of pages, one per migrate type stored on the pcp-lists */ struct list_head lists[NR_PCP_LISTS]; - } __private pcplists; + } __private pcplists[2]; }; =20 struct per_cpu_zonestat { diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 842816f269da..d56d06dde66a 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -147,13 +147,7 @@ DEFINE_PER_CPU(int, _numa_mem_); /* Kernel "local m= emory" node */ EXPORT_PER_CPU_SYMBOL(_numa_mem_); #endif =20 -/* work_structs for global per-cpu drains */ -struct pcpu_drain { - struct zone *zone; - struct work_struct work; -}; static DEFINE_MUTEX(pcpu_drain_mutex); -static DEFINE_PER_CPU(struct pcpu_drain, pcpu_drain); =20 #ifdef CONFIG_GCC_PLUGIN_LATENT_ENTROPY volatile unsigned long latent_entropy __latent_entropy; @@ -1448,6 +1442,7 @@ static void free_pcppages_bulk(struct zone *zone, i= nt count, int prefetch_nr =3D READ_ONCE(pcp->batch); bool isolated_pageblocks; struct page *page, *tmp; + unsigned long flags; LIST_HEAD(head); =20 /* @@ -1511,11 +1506,7 @@ static void free_pcppages_bulk(struct zone *zone, = int count, } lp->count -=3D nr_freed; =20 - /* - * local_lock_irq held so equivalent to spin_lock_irqsave for - * both PREEMPT_RT and non-PREEMPT_RT configurations. - */ - spin_lock(&zone->lock); + spin_lock_irqsave(&zone->lock, flags); isolated_pageblocks =3D has_isolate_pageblock(zone); =20 /* @@ -1538,7 +1529,7 @@ static void free_pcppages_bulk(struct zone *zone, i= nt count, __free_one_page(page, page_to_pfn(page), zone, order, mt, FPI_NONE); trace_mm_page_pcpu_drain(page, order, mt); } - spin_unlock(&zone->lock); + spin_unlock_irqrestore(&zone->lock, flags); } =20 static void free_one_page(struct zone *zone, @@ -3076,7 +3067,7 @@ void drain_zone_pages(struct zone *zone, struct per= _cpu_pages *pcp) =20 local_lock_irqsave(&pagesets.lock, flags); batch =3D READ_ONCE(pcp->batch); - lp =3D pcp->lp; + lp =3D rcu_dereference_check(pcp->lp, lockdep_is_held(this_cpu_ptr(&pag= esets.lock))); to_drain =3D min(lp->count, batch); if (to_drain > 0) free_pcppages_bulk(zone, to_drain, pcp, lp); @@ -3100,7 +3091,7 @@ static void drain_pages_zone(unsigned int cpu, stru= ct zone *zone) local_lock_irqsave(&pagesets.lock, flags); =20 pcp =3D per_cpu_ptr(zone->per_cpu_pageset, cpu); - lp =3D pcp->lp; + lp =3D rcu_dereference_check(pcp->lp, lockdep_is_held(this_cpu_ptr(&pag= esets.lock))); if (lp->count) free_pcppages_bulk(zone, lp->count, pcp, lp); =20 @@ -3139,24 +3130,6 @@ void drain_local_pages(struct zone *zone) drain_pages(cpu); } =20 -static void drain_local_pages_wq(struct work_struct *work) -{ - struct pcpu_drain *drain; - - drain =3D container_of(work, struct pcpu_drain, work); - - /* - * drain_all_pages doesn't use proper cpu hotplug protection so - * we can race with cpu offline when the WQ can move this from - * a cpu pinned worker to an unbound one. We can operate on a different - * cpu which is alright but we also have to make sure to not move to - * a different one. - */ - preempt_disable(); - drain_local_pages(drain->zone); - preempt_enable(); -} - /* * The implementation of drain_all_pages(), exposing an extra parameter = to * drain on all cpus. @@ -3169,6 +3142,8 @@ static void drain_local_pages_wq(struct work_struct= *work) */ static void __drain_all_pages(struct zone *zone, bool force_all_cpus) { + struct per_cpu_pages *pcp; + struct zone *z; int cpu; =20 /* @@ -3177,13 +3152,6 @@ static void __drain_all_pages(struct zone *zone, b= ool force_all_cpus) */ static cpumask_t cpus_with_pcps; =20 - /* - * Make sure nobody triggers this path before mm_percpu_wq is fully - * initialized. - */ - if (WARN_ON_ONCE(!mm_percpu_wq)) - return; - /* * 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 @@ -3202,8 +3170,6 @@ static void __drain_all_pages(struct zone *zone, bo= ol force_all_cpus) * disables preemption as part of its processing */ for_each_online_cpu(cpu) { - struct per_cpu_pages *pcp; - struct zone *z; bool has_pcps =3D false; struct pcplists *lp; =20 @@ -3214,12 +3180,12 @@ static void __drain_all_pages(struct zone *zone, = bool force_all_cpus) */ has_pcps =3D true; } else if (zone) { - lp =3D per_cpu_ptr(zone->per_cpu_pageset, cpu)->lp; + lp =3D READ_ONCE(per_cpu_ptr(zone->per_cpu_pageset, cpu)->lp); if (lp->count) has_pcps =3D true; } else { for_each_populated_zone(z) { - lp =3D per_cpu_ptr(z->per_cpu_pageset, cpu)->lp; + lp =3D READ_ONCE(per_cpu_ptr(z->per_cpu_pageset, cpu)->lp); if (lp->count) { has_pcps =3D true; break; @@ -3233,16 +3199,37 @@ static void __drain_all_pages(struct zone *zone, = bool force_all_cpus) cpumask_clear_cpu(cpu, &cpus_with_pcps); } =20 + if (!force_all_cpus && cpumask_empty(&cpus_with_pcps)) + goto exit; + + for_each_cpu(cpu, &cpus_with_pcps) { + for_each_populated_zone(z) { + if (zone && zone !=3D z) + continue; + + pcp =3D per_cpu_ptr(z->per_cpu_pageset, cpu); + pcp->drain =3D rcu_replace_pointer(pcp->lp, pcp->drain, + mutex_is_locked(&pcpu_drain_mutex)); + } + } + + synchronize_rcu_expedited(); + for_each_cpu(cpu, &cpus_with_pcps) { - struct pcpu_drain *drain =3D per_cpu_ptr(&pcpu_drain, cpu); + for_each_populated_zone(z) { + int count; + + pcp =3D per_cpu_ptr(z->per_cpu_pageset, cpu); + count =3D pcp->drain->count; + if (!count) + continue; =20 - drain->zone =3D zone; - INIT_WORK(&drain->work, drain_local_pages_wq); - queue_work_on(cpu, mm_percpu_wq, &drain->work); + free_pcppages_bulk(z, count, pcp, pcp->drain); + VM_BUG_ON(pcp->drain->count); + } } - for_each_cpu(cpu, &cpus_with_pcps) - flush_work(&per_cpu_ptr(&pcpu_drain, cpu)->work); =20 +exit: mutex_unlock(&pcpu_drain_mutex); } =20 @@ -3378,7 +3365,7 @@ static void free_unref_page_commit(struct page *pag= e, unsigned long pfn, =20 __count_vm_event(PGFREE); pcp =3D this_cpu_ptr(zone->per_cpu_pageset); - lp =3D pcp->lp; + lp =3D rcu_dereference_check(pcp->lp, lockdep_is_held(this_cpu_ptr(&pag= esets.lock))); pindex =3D order_to_pindex(migratetype, order); list_add(&page->lru, &lp->lists[pindex]); lp->count +=3D 1 << order; @@ -3614,7 +3601,7 @@ struct page *__rmqueue_pcplist(struct zone *zone, u= nsigned int order, struct pcplists *lp; struct page *page; =20 - lp =3D pcp->lp; + lp =3D rcu_dereference_check(pcp->lp, lockdep_is_held(this_cpu_ptr(&pag= esets.lock))); list =3D &lp->lists[order_to_pindex(migratetype, order)]; =20 do { @@ -5886,8 +5873,12 @@ void show_free_areas(unsigned int filter, nodemask= _t *nodemask) if (show_mem_node_skip(filter, zone_to_nid(zone), nodemask)) continue; =20 - for_each_online_cpu(cpu) - free_pcp +=3D per_cpu_ptr(zone->per_cpu_pageset, cpu)->lp->count; + for_each_online_cpu(cpu) { + struct pcplists *lp; + + lp =3D READ_ONCE(per_cpu_ptr(zone->per_cpu_pageset, cpu)->lp); + free_pcp +=3D lp->count; + } } =20 printk("active_anon:%lu inactive_anon:%lu isolated_anon:%lu\n" @@ -5980,8 +5971,12 @@ void show_free_areas(unsigned int filter, nodemask= _t *nodemask) continue; =20 free_pcp =3D 0; - for_each_online_cpu(cpu) - free_pcp +=3D per_cpu_ptr(zone->per_cpu_pageset, cpu)->lp->count; + for_each_online_cpu(cpu) { + struct pcplists *lp; + + lp =3D READ_ONCE(per_cpu_ptr(zone->per_cpu_pageset, cpu)->lp); + free_pcp +=3D lp->count; + } =20 show_node(zone); printk(KERN_CONT @@ -6022,7 +6017,7 @@ void show_free_areas(unsigned int filter, nodemask_= t *nodemask) K(zone_page_state(zone, NR_MLOCK)), K(zone_page_state(zone, NR_BOUNCE)), K(free_pcp), - K(this_cpu_read(zone->per_cpu_pageset)->lp->count), + K(READ_ONCE(this_cpu_ptr(zone->per_cpu_pageset)->lp)->count), K(zone_page_state(zone, NR_FREE_CMA_PAGES))); printk("lowmem_reserve[]:"); for (i =3D 0; i < MAX_NR_ZONES; i++) @@ -6886,10 +6881,13 @@ static void per_cpu_pages_init(struct per_cpu_pag= es *pcp, struct per_cpu_zonesta memset(pcp, 0, sizeof(*pcp)); memset(pzstats, 0, sizeof(*pzstats)); =20 - pcp->lp =3D &ACCESS_PRIVATE(pcp, pcplists); + pcp->lp =3D &ACCESS_PRIVATE(pcp, pcplists[0]); + pcp->drain =3D &ACCESS_PRIVATE(pcp, pcplists[1]); =20 - for (pindex =3D 0; pindex < NR_PCP_LISTS; pindex++) + for (pindex =3D 0; pindex < NR_PCP_LISTS; pindex++) { INIT_LIST_HEAD(&pcp->lp->lists[pindex]); + INIT_LIST_HEAD(&pcp->drain->lists[pindex]); + } =20 /* * Set batch and high values safe for a boot pageset. A true percpu diff --git a/mm/vmstat.c b/mm/vmstat.c index 5279d3f34e0b..1ffa4fc64a4f 100644 --- a/mm/vmstat.c +++ b/mm/vmstat.c @@ -856,7 +856,7 @@ static int refresh_cpu_vm_stats(bool do_pagesets) * if not then there is nothing to expire. */ if (!__this_cpu_read(pcp->expire) || - !this_cpu_ptr(pcp)->lp->count) + !READ_ONCE(this_cpu_ptr(pcp)->lp)->count) continue; =20 /* @@ -870,7 +870,7 @@ static int refresh_cpu_vm_stats(bool do_pagesets) if (__this_cpu_dec_return(pcp->expire)) continue; =20 - if (this_cpu_ptr(pcp)->lp->count) { + if (READ_ONCE(this_cpu_ptr(pcp)->lp)->count) { drain_zone_pages(zone, this_cpu_ptr(pcp)); changes++; } @@ -1707,7 +1707,7 @@ static void zoneinfo_show_print(struct seq_file *m,= pg_data_t *pgdat, "\n high: %i" "\n batch: %i", i, - pcp->lp->count, + READ_ONCE(pcp->lp)->count, pcp->high, pcp->batch); #ifdef CONFIG_SMP --=20 2.31.1