Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp4541948pxf; Tue, 23 Mar 2021 13:12:28 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyduw5MDlWAcdMYbFL4//O7Jrdpv9hr+e8wh0D8ymbCcIW7dxVduqdD7ISVYE0KqsGMvZAu X-Received: by 2002:a17:906:f953:: with SMTP id ld19mr6329705ejb.164.1616530347844; Tue, 23 Mar 2021 13:12:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1616530347; cv=none; d=google.com; s=arc-20160816; b=GkV4tzBelriaKHmkwyzw2QDk8A3rykb04xMfWr8fvPTQJKVEeeV6vjSs/a89snXsjs 3nGx1gLdtVPqNoXXYnzPFJywCLeCgErkhLpI3nsVyTmf/0mDChe/GpSL2E/H30j6uwTe zalhG7CL12fsVmDQXV+c+XPzeyi5Jvs97MZb8KoR1OawUGNBnQVxl8lcQZyuzH6NSgO9 qoZTcvwcnmS1rtzilq148zBxYSewjbnQQMaP5HLYDFkPFFV0HTsRRa4Fuu0u1ZGsJppL fPn7WDxG5k8fe3gbVpfi1vnuXgRNU4T381nkNbfscO4deeLHZ5yYCInKTf8d/Lot0PR+ yLrQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:user-agent:references:message-id :in-reply-to:subject:cc:to:from:date:dkim-signature; bh=LyHHHSMLFWUeygbV2lU1XgcbhGu+fq/FJm7L8suyFsw=; b=YJr+aSsc8p05r2Bltjaj666o4UzCeMP1mz5rD911LAoDrcARvsranzvAJ13ZLQCyaG fmTuX/KhJBhnRBAQR6x/AYj6gnhCIlk+r1MUeydCSMfOJD4MAZHU3p+dyjUaJH1iraKL 5PFodpQ3Kvbq7Acfg5VR1j/PTaM2WupD7gJQIwBpj5o9/Qn/ebWdbZX2ekTHdaH23GyS oC5J/N6TrJpDOMYPBPmJ69th4g+EifMza+OOu0RG0HIKqLJFQgAGw7RoQAS9wXnya7wi 9FgpGiZn/aDitWYDtX8HKLIUxNKjVIyqzerMO109uOCSiZVXfsTJvaNMrcGn5X/Ao155 6lGQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=pvagIcQf; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id gt5si8966ejb.181.2021.03.23.13.12.03; Tue, 23 Mar 2021 13:12:27 -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; dkim=pass header.i=@google.com header.s=20161025 header.b=pvagIcQf; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231293AbhCWUK7 (ORCPT + 99 others); Tue, 23 Mar 2021 16:10:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38550 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231281AbhCWUKb (ORCPT ); Tue, 23 Mar 2021 16:10:31 -0400 Received: from mail-ot1-x336.google.com (mail-ot1-x336.google.com [IPv6:2607:f8b0:4864:20::336]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 98392C061574 for ; Tue, 23 Mar 2021 13:10:30 -0700 (PDT) Received: by mail-ot1-x336.google.com with SMTP id w21-20020a9d63950000b02901ce7b8c45b4so20727863otk.5 for ; Tue, 23 Mar 2021 13:10:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:from:to:cc:subject:in-reply-to:message-id:references :user-agent:mime-version; bh=LyHHHSMLFWUeygbV2lU1XgcbhGu+fq/FJm7L8suyFsw=; b=pvagIcQfYI9eLKjZ4QGl1Q2Bs9IeTWQSzIY6slDMNYWnvJgF2c8mi2N4adhgHTzfqI /SNzU3qxYr1u9PIVkoQXXT7forjchfGfPxahqRaF3+ZItU2zsI6f3QdO1n4dctmeCKTG j7UGWys7lo5BpZ6hIiCWZZVycs7TH/l7dQDjs7C2SpHo7lVOrTjJYNGs/umOCg8DgsiQ h2RY+s+2k3XdtBv7oi+A3xGCnDCSeyVZ/jRJUxwEGLA8P+7toCLU8fYVOG+mn5vcAb9g DhJcA5BFdHDotGdShY6zhZyAGHIJdEwNsR+jln4otYC6raSVzSXjkuAyCPKcfVOOTV/2 DpqA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:in-reply-to:message-id :references:user-agent:mime-version; bh=LyHHHSMLFWUeygbV2lU1XgcbhGu+fq/FJm7L8suyFsw=; b=gEZCDJuslfvVujTfbyNGydalF9xyFwbw2saaLrq4h9O8gjgRPPIuygLM2pfD/yanuX OwZBBg/yqjZshwjDXQzaLyZUppdPcpNjuGWmjJp2DVp3Ar+kbIRtgAon5+jUiFhmJa+B gqhL0FK/f3rHpg8DEDcFoBZZdyiC/pJwuug5M+VX73Z2YsRB2calW8mtGGQN1NJXrWPO 1/x1Hn1NEdNULAbso2jscmwWP7NMd3i0DGGSSgvkQEGB4PbiSJ4Ep2qzJi0WQ53en8ti apIWkSIFfjg5hV1FC6/LPOnXie57r8222qFipVqgxyMCFRcocQUEwRcs6UN78fxsMmuA Xd9A== X-Gm-Message-State: AOAM532+W8BPukcppQ4oB5uoU4cW+ljSkr+LiFDmzSd3KrQY4BU7zIHu 1N85lsrCggo2k9s5TOWCUgvI7A== X-Received: by 2002:a9d:12cb:: with SMTP id g69mr30718otg.77.1616530229418; Tue, 23 Mar 2021 13:10:29 -0700 (PDT) Received: from eggly.attlocal.net (172-10-233-147.lightspeed.sntcca.sbcglobal.net. [172.10.233.147]) by smtp.gmail.com with ESMTPSA id z25sm1368942otj.68.2021.03.23.13.10.27 (version=TLS1 cipher=ECDHE-ECDSA-AES128-SHA bits=128/128); Tue, 23 Mar 2021 13:10:28 -0700 (PDT) Date: Tue, 23 Mar 2021 13:10:13 -0700 (PDT) From: Hugh Dickins X-X-Sender: hugh@eggly.anvils To: Johannes Weiner cc: Hugh Dickins , Andrew Morton , Matthew Wilcox , Michal Hocko , Zhou Guanghui , Zi Yan , Shakeel Butt , Roman Gushchin , linux-mm@kvack.org, cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, kernel-team@fb.com Subject: Re: [PATCH] mm: page_alloc: fix memcg accounting leak in speculative cache lookup In-Reply-To: Message-ID: References: <20210319071547.60973-1-hannes@cmpxchg.org> User-Agent: Alpine 2.11 (LSU 23 2013-08-11) MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, 23 Mar 2021, Johannes Weiner wrote: > From f6f062a3ec46f4fb083dcf6792fde9723f18cfc5 Mon Sep 17 00:00:00 2001 > From: Johannes Weiner > Date: Fri, 19 Mar 2021 02:17:00 -0400 > Subject: [PATCH] mm: page_alloc: fix allocation imbalances from speculative > cache lookup > > When the freeing of a higher-order page block (non-compound) races > with a speculative page cache lookup, __free_pages() needs to leave > the first order-0 page in the chunk to the lookup but free the buddy > pages that the lookup doesn't know about separately. > > There are currently two problems with it: > > 1. It checks PageHead() to see whether we're dealing with a compound > page after put_page_testzero(). But the speculative lookup could > have freed the page after our put and cleared PageHead, in which > case we would double free the tail pages. > > To fix this, test PageHead before the put and cache the result for > afterwards. > > 2. If such a higher-order page is charged to a memcg (e.g. !vmap > kernel stack)), only the first page of the block has page->memcg > set. That means we'll uncharge only one order-0 page from the > entire block, and leak the remainder. > > To fix this, add a split_page_memcg() before it starts freeing tail > pages, to ensure they all have page->memcg set up. > > While at it, also update the comments a bit to clarify what exactly is > happening to the page during that race. > > Fixes: e320d3012d25 mm/page_alloc.c: fix freeing non-compound pages > Reported-by: Hugh Dickins > Reported-by: Matthew Wilcox > Signed-off-by: Johannes Weiner > Cc: # 5.10+ This is great, thanks Hannes. Acked-by: Hugh Dickins I know that 5.10-stable rejected the two split_page_memcg() patches: we shall need those in, I'll send GregKH the fixups, but not today. > --- > mm/page_alloc.c | 41 +++++++++++++++++++++++++++++++++++------ > 1 file changed, 35 insertions(+), 6 deletions(-) > > diff --git a/mm/page_alloc.c b/mm/page_alloc.c > index c53fe4fa10bf..8aab1e87fa3c 100644 > --- a/mm/page_alloc.c > +++ b/mm/page_alloc.c > @@ -5112,10 +5112,9 @@ static inline void free_the_page(struct page *page, unsigned int order) > * the allocation, so it is easy to leak memory. Freeing more memory > * than was allocated will probably emit a warning. > * > - * If the last reference to this page is speculative, it will be released > - * by put_page() which only frees the first page of a non-compound > - * allocation. To prevent the remaining pages from being leaked, we free > - * the subsequent pages here. If you want to use the page's reference > + * This function isn't a put_page(). Don't let the put_page_testzero() > + * fool you, it's only to deal with speculative cache references. It > + * WILL free pages directly. If you want to use the page's reference > * count to decide when to free the allocation, you should allocate a > * compound page, and use put_page() instead of __free_pages(). > * > @@ -5124,11 +5123,41 @@ static inline void free_the_page(struct page *page, unsigned int order) > */ > void __free_pages(struct page *page, unsigned int order) > { > - if (put_page_testzero(page)) > + bool compound = PageHead(page); > + > + /* > + * Drop the base reference from __alloc_pages and free. In > + * case there is an outstanding speculative reference, from > + * e.g. the page cache, it will put and free the page later. > + */ > + if (likely(put_page_testzero(page))) { > free_the_page(page, order); > - else if (!PageHead(page)) > + return; > + } > + > + /* > + * Ok, the speculative reference will put and free the page. > + * > + * - If this was an order-0 page, we're done. > + * > + * - If the page was compound, the other side will free the > + * entire page and we're done here as well. Just note that > + * freeing clears PG_head, so it can only be read reliably > + * before the put_page_testzero(). > + * > + * - If the page was of higher order but NOT marked compound, > + * the other side will know nothing about our buddy pages > + * and only free the order-0 page at the start of our block. > + * We must split off and free the buddy pages here. > + * > + * The buddy pages aren't individually refcounted, so they > + * can't have any pending speculative references themselves. > + */ > + if (order > 0 && !compound) { > + split_page_memcg(page, 1 << order); > while (order-- > 0) > free_the_page(page + (1 << order), order); > + } > } > EXPORT_SYMBOL(__free_pages); > > -- > 2.31.0