Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp1285819pxf; Fri, 26 Mar 2021 05:10:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzZSSleJP38DCciTcD6MebGnFdW2SGt47kzNGpbsfdEelWJkr6dxPSA/pCF70c+1GMbEkGE X-Received: by 2002:a17:906:565a:: with SMTP id v26mr15289526ejr.516.1616760614492; Fri, 26 Mar 2021 05:10:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1616760614; cv=none; d=google.com; s=arc-20160816; b=ThGzmph4Zvgi6YUYO5El2cSbri87HKQ1tXtk8XIugTEgZzFya6O06NNs8B1crLv5qT Aegk6O2V/mygXdqDlMqxqqZvL6zKnAoRFEP+/NsfovNpaVbGhX3W5dEGXoMnPtq8CugH MewJI3PlOWSGLxSR2OoCJuxLGc1QrLHEAewfF2NWsMQpco4EAYXXk3mOHamAaRkAOeVU GFwwlDVoHYifIE1AO0UhQ/3n9Tp0pJOEYYOMbWCWYhi87ZZHW/PFdu+FFYYnfTH1hIA5 a+nQhtyLTWNzo4f7K+iH7mPkGUsKyLSqJ8S6maJmw4CwpCuEyDEml3XseYWiaFl5J/C3 Ishw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=6CNIiT7q+Ups+0Vc8DJQD9WSyDH1NQMZhuJ6B6CQvG0=; b=XBd6WG9qDexryMX8JK4Ag8PtJV/ASRxWETy63EHuI3D+UFpNvPMxNc2lt9SeWFqKj/ Xu+kZImQi/VMjQjcjY2qt4i8166uWzc8d9hQEU3Ly4pXbWGs3d7GrF1fpk26TKrPu3tt 1jqcPsBZ6U7T06/rSKEa3pBD6sTZf7wYcXNyfM/1sHOlZHbpnCvcVK1iR1IpiaWmEK/t vAeEOhbdZF2q5lqcA53dhlZ5NQVUTfgjCMmkhr+MpGfG2w2OSSFUkxl9X/d7T4vkLAd8 JHLPNRQwyC6ZqFiy6TkwHd9ldTHgMO7ImHJd6rMsiUk9S3IIuvb4rrHJJ9S+EVTe8RWo Pszw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b="AErjpU/d"; 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 r15si6692895edq.306.2021.03.26.05.09.51; Fri, 26 Mar 2021 05:10:14 -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=@infradead.org header.s=casper.20170209 header.b="AErjpU/d"; 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 S229848AbhCZMI5 (ORCPT + 99 others); Fri, 26 Mar 2021 08:08:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49360 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229866AbhCZMIj (ORCPT ); Fri, 26 Mar 2021 08:08:39 -0400 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 94B4EC0613B1; Fri, 26 Mar 2021 05:08:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=6CNIiT7q+Ups+0Vc8DJQD9WSyDH1NQMZhuJ6B6CQvG0=; b=AErjpU/dIo4bIDLo86LL6AWQL2 HyE8BVIZTayV2FqKgSgtRiLrJF/00Hm954Qx1zGZnzw/zMfyyN4EMfyFqq9MfwZIZNya2ibjjkmQb 1dxMOwVOpBScbtr1f4cJaxnSHm3hvyW3qOTGsuXkoQ6zSGilHwK5VhPAfCiRRrdkk/LW3cvrMs+KJ KZOLms9/Wv+Wi22Un8X4Bs+JyeK3GiBEm/99pWtCepRYXol+0gisuuaJ5XzkaNDGgktER4R5wf9j1 XrH7alkNmm2/q8hNsInB6CHqj7TbsaaqibnOxe37+AbIWuDgWGwSkKiOSwJaDN9Qa/rwkO9q8NdH9 6yRI+fyw==; Received: from willy by casper.infradead.org with local (Exim 4.94 #2 (Red Hat Linux)) id 1lPlFg-00Eljt-Pn; Fri, 26 Mar 2021 12:07:48 +0000 Date: Fri, 26 Mar 2021 12:07:44 +0000 From: Matthew Wilcox To: Hugh Dickins Cc: Johannes Weiner , Andrew Morton , 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 Message-ID: <20210326120744.GD1719932@casper.infradead.org> References: <20210319071547.60973-1-hannes@cmpxchg.org> <20210326025143.GB1719932@casper.infradead.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Mar 25, 2021 at 09:04:40PM -0700, Hugh Dickins wrote: > On Fri, 26 Mar 2021, Matthew Wilcox wrote: > > On Thu, Mar 25, 2021 at 06:55:42PM -0700, Hugh Dickins wrote: > > > The first reason occurred to me this morning. I thought I had been > > > clever to spot the PageHead race which you fix here. But now I just feel > > > very stupid not to have spotted the very similar memcg_data race. The > > > speculative racer may call mem_cgroup_uncharge() from __put_single_page(), > > > and the new call to split_page_memcg() do nothing because page_memcg(head) > > > is already NULL. > > > > > > And is it even safe there, to sprinkle memcg_data through all of those > > > order-0 subpages, when free_the_page() is about to be applied to a > > > series of descending orders? I could easily be wrong, but I think > > > free_pages_prepare()'s check_free_page() will find that is not > > > page_expected_state(). I forgot to say earlier; I did add a test (lib/test_free_pages.c). Doubling it up to check GFP_KERNEL | GFP_ACCOUNT and GFP_KERNEL | GFP_COMP | GFP_ACCOUNT should be reasonable. > > So back to something more like my original patch then? > > > > +++ b/mm/page_alloc.c > > @@ -5081,9 +5081,15 @@ void __free_pages(struct page *page, unsigned int order) > > { > > if (put_page_testzero(page)) > > free_the_page(page, order); > > - else if (!PageHead(page)) > > - while (order-- > 0) > > - free_the_page(page + (1 << order), order); > > + else if (!PageHead(page)) { > > + while (order-- > 0) { > > + struct page *tail = page + (1 << order); > > +#ifdef CONFIG_MEMCG > > + tail->memcg_data = page->memcg_data; > > +#endif > > + free_the_page(tail, order); > > + } > > + } > > } > > EXPORT_SYMBOL(__free_pages); > > > > We can cache page->memcg_data before calling put_page_testzero(), > > just like we cache the Head flag in Johannes' patch. > > If I still believed in e320d3012d25, yes, that would look right > (but I don't have much faith in my judgement after all this). > > I'd fallen in love with split_page_memcg() when you posted that > one, and was put off by your #ifdef, so got my priorities wrong > and went for the split_page_memcg(). Oh, the ifdef was just a strawman. I wouldn't want to see that upstream; something like: unsigned long memcg_data = __get_memcg_data(page); ... __set_memcg_data(tail, memcg_data); with the appropriate ifdefs hidden in memcontrol.h would be my preference. > > > But, after all that, I'm now thinking that Matthew's original > > > e320d3012d25 ("mm/page_alloc.c: fix freeing non-compound pages") > > > is safer reverted. The put_page_testzero() in __free_pages() was > > > not introduced for speculative pagecache: it was there in 2.4.0, > > > and atomic_dec_and_test() in 2.2, I don't have older trees to hand. > > > > I think you're confused in that last assertion. According to > > linux-fullhistory, the first introduction of __free_pages was 2.3.29pre3 > > (September 1999), where it did indeed use put_page_testzero: > > Not confused, just pontificating from a misleading subset of the data. > I knew there's an even-more-history-than-tglx git tree somewhere, but > what I usually look back to is 2.4 trees, plus a 2.2.26 tree - but of > course that's a late 2.2, from 2004, around the same time as 2.6.3. I suspect it got backported ... https://github.com/mpe/linux-fullhistory/wiki is what I'm using for my archaeology, and it doesn't have the stable branches (1.0, 1.2, 2.0, 2.2, 2.4), so I don't know for sure. Anyway, my point is that the truly ancient drivers *don't* depend on this behaviour because the function didn't even exist when they were written.