Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp1364382pxf; Fri, 9 Apr 2021 06:44:30 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzjA+45nIGGlG/i2tcmocKQDm+xHGawrUnh2slR41wMlmv2jTdwRXmTLkal3ey6qUaqe6vZ X-Received: by 2002:a62:27c7:0:b029:204:7b11:3222 with SMTP id n190-20020a6227c70000b02902047b113222mr12201357pfn.34.1617975870748; Fri, 09 Apr 2021 06:44:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1617975870; cv=none; d=google.com; s=arc-20160816; b=dYE51p9q9Dt5WUkSLThcPh5dP430q4WVHLM2dUNdY1STkF9yIYMUtrz47oMO9yyn1C ad94pHSRLMZ3fTfH4haElGpK82eIjZ3WydZtIRSAdI70G6o2POzcfvLZ4W93ItM7f4vS o0kjnQ70uf1Nu18iIJLxcaxknkWrmYNpsCfV8rASURIp0lnnTOtZd8GUoOOe7r3kTfN7 jrJfoK5XucdS9/IOGn9VjvW4tJdoMKG00F8u0Lb20whCitblwa0NZRtay+Sr/UJoKF/e KSxBy8iDlDo7JQjDeEx4IdS2ft5tgiOurdFvlGHNoH77h1mCfQS8MdMVt7bTGoVc8xnh pFqg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date; bh=ATtC9Kd6ZQDtiqRUsIHfVwz6K3UVk5Tt2pQGle3eSG4=; b=u0gQ3O2N2NNtlhI2haP5WKf2/KgAKyNmNmz+k5uvuVWUby5eez8z2ZkMjz9RMK08JQ vqo4olNlTpUtS6FfGFYkWckz1pxLIPXzP4oROTs1NhK5QfNBtJsFWN0hHFJlg/ulUAVB 0Zi7JBNU55p5SU9hyimEqZH7SAWJrXAnPeKO1+2tbem/bXE9ZMkolyX5Me3bswXWmm3I i8IFe6kLNZYUbqdwAanT1toPdqEKzrVAC0wGQodARYALKCOBVF0J3BeSRef+eSdpl5x9 5fmFv1ZZFnB5AvefwOAoOvDdaH0jpBjyGTdix44pnTYWD2Nf7eFoGszH3LszDuU//Sy9 beQA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id ba7si3071886plb.383.2021.04.09.06.44.18; Fri, 09 Apr 2021 06:44:30 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233512AbhDINmi (ORCPT + 99 others); Fri, 9 Apr 2021 09:42:38 -0400 Received: from outbound-smtp50.blacknight.com ([46.22.136.234]:57653 "EHLO outbound-smtp50.blacknight.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233655AbhDINmg (ORCPT ); Fri, 9 Apr 2021 09:42:36 -0400 Received: from mail.blacknight.com (pemlinmail06.blacknight.ie [81.17.255.152]) by outbound-smtp50.blacknight.com (Postfix) with ESMTPS id D71E0FAD4C for ; Fri, 9 Apr 2021 14:42:22 +0100 (IST) Received: (qmail 15210 invoked from network); 9 Apr 2021 13:42:22 -0000 Received: from unknown (HELO techsingularity.net) (mgorman@techsingularity.net@[84.203.22.4]) by 81.17.254.9 with ESMTPSA (AES256-SHA encrypted, authenticated); 9 Apr 2021 13:42:22 -0000 Date: Fri, 9 Apr 2021 14:42:21 +0100 From: Mel Gorman To: Michal Hocko Cc: Andrew Morton , Linux-MM , LKML , Oscar Salvador , "Michael S. Tsirkin" , David Hildenbrand , Vlastimil Babka , Alexander Duyck , Minchan Kim Subject: Re: [PATCH] mm/memory_hotplug: Make unpopulated zones PCP structures unreachable during hot remove Message-ID: <20210409134221.GO3697@techsingularity.net> References: <20210409120957.GM3697@techsingularity.net> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-15 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.10.1 (2018-07-13) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Apr 09, 2021 at 02:48:12PM +0200, Michal Hocko wrote: > On Fri 09-04-21 14:42:58, Michal Hocko wrote: > > On Fri 09-04-21 13:09:57, Mel Gorman wrote: > > > zone_pcp_reset allegedly protects against a race with drain_pages > > > using local_irq_save but this is bogus. local_irq_save only operates > > > on the local CPU. If memory hotplug is running on CPU A and drain_pages > > > is running on CPU B, disabling IRQs on CPU A does not affect CPU B and > > > offers no protection. > > > > Yes, the synchronization aspect is bogus indeed. > > > > > This patch reorders memory hotremove such that the PCP structures > > > relevant to the zone are no longer reachable by the time the structures > > > are freed. With this reordering, no protection is required to prevent > > > a use-after-free and the IRQs can be left enabled. zone_pcp_reset is > > > renamed to zone_pcp_destroy to make it clear that the per-cpu structures > > > are deleted when the function returns. > > > > Wouldn't it be much easier to simply not destroy/reset pcp of an empty > > zone at all? The whole point of this exercise seems to be described in > > 340175b7d14d5. setup_zone_pageset can check for an already allocated pcp > > and simply reinitialize it. > > I meant this > It might be simplier but if the intention is to free as much memory as possible during hot-remove, it seems wasteful to leave the per-cpu structures behind if we do not have to. If a problem with my patch can be spotted then I'm happy to go with an alternative fix but there are two minor issues with your proposed fix. > diff --git a/mm/page_alloc.c b/mm/page_alloc.c > index e6a602e82860..b0fdda77e570 100644 > --- a/mm/page_alloc.c > +++ b/mm/page_alloc.c > @@ -6496,7 +6496,13 @@ void __meminit setup_zone_pageset(struct zone *zone) > struct per_cpu_pageset *p; > int cpu; > > - zone->pageset = alloc_percpu(struct per_cpu_pageset); > + /* > + * zone could have gone completely offline during memory hotplug > + * when the pgdat is left behind for simplicity. On a next onlining > + * we do not need to reallocate pcp state. > + */ > + if (!zone->pageset) > + zone->pageset = alloc_percpu(struct per_cpu_pageset); Should be "if (zone->pageset != &boot_pageset)" ? > for_each_possible_cpu(cpu) { > p = per_cpu_ptr(zone->pageset, cpu); > pageset_init(p); > @@ -8803,25 +8809,6 @@ void zone_pcp_enable(struct zone *zone) > mutex_unlock(&pcp_batch_high_lock); > } > > -void zone_pcp_reset(struct zone *zone) > -{ > - unsigned long flags; > - int cpu; > - struct per_cpu_pageset *pset; > - > - /* avoid races with drain_pages() */ > - local_irq_save(flags); > - if (zone->pageset != &boot_pageset) { > - for_each_online_cpu(cpu) { > - pset = per_cpu_ptr(zone->pageset, cpu); > - drain_zonestat(zone, pset); > - } > - free_percpu(zone->pageset); > - zone->pageset = &boot_pageset; > - } > - local_irq_restore(flags); > -} > - zone_pcp_reset still needs to exist to drain the remaining vmstats or it'll break 5a883813845a ("memory-hotplug: fix zone stat mismatch"). -- Mel Gorman SUSE Labs