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 X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1EE7EC433C1 for ; Mon, 22 Mar 2021 09:55:51 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 7FED761927 for ; Mon, 22 Mar 2021 09:55:50 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 7FED761927 Authentication-Results: mail.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=suse.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 5BE986B0095; Mon, 22 Mar 2021 05:36:59 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 548016B0096; Mon, 22 Mar 2021 05:36:59 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 39D886B0098; Mon, 22 Mar 2021 05:36:59 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0179.hostedemail.com [216.40.44.179]) by kanga.kvack.org (Postfix) with ESMTP id 1B21B6B0095 for ; Mon, 22 Mar 2021 05:36:59 -0400 (EDT) Received: from smtpin14.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 764CC1803A834 for ; Mon, 22 Mar 2021 09:55:49 +0000 (UTC) X-FDA: 77947053618.14.2BDC473 Received: from mx2.suse.de (mx2.suse.de [195.135.220.15]) by imf19.hostedemail.com (Postfix) with ESMTP id C625190009E8 for ; Mon, 22 Mar 2021 09:55:48 +0000 (UTC) X-Virus-Scanned: by amavisd-new at test-mx.suse.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1616406947; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=B/2ozR1dCacTTh1U7cY+Z5HNl+3fI6dqE+W2ZDjDPYM=; b=Z6gyafmxwULS2CLLmU6+9rxjA+6VdYu/ZVVzqZlu7ZW2khYcqWt3VZyW5URwptKGH6FfHt sOevZnUMpkVLYHYkXIePZhVkyMlsMuiNktVNXkSE6PL2a/NmVOtnGSkGFZjvGIPtrfHSNB S8V4aYfbUQ73RzmerBVBOySZw2B6MjI= Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 817FFAD38; Mon, 22 Mar 2021 09:55:47 +0000 (UTC) Date: Mon, 22 Mar 2021 10:55:30 +0100 From: Michal Hocko To: Johannes Weiner Cc: Andrew Morton , Matthew Wilcox , Hugh Dickins , 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: References: <20210319071547.60973-1-hannes@cmpxchg.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210319071547.60973-1-hannes@cmpxchg.org> X-Stat-Signature: g5jjrhs9i5uhninfda865ysptrromwkm X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: C625190009E8 Received-SPF: none (suse.com>: No applicable sender policy available) receiver=imf19; identity=mailfrom; envelope-from=""; helo=mx2.suse.de; client-ip=195.135.220.15 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1616406948-787915 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: On Fri 19-03-21 03:15:47, Johannes Weiner wrote: > 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. > > However, 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. > > Add a split_page_memcg() to __free_pages() right before it starts > taking the higher-order page apart and freeing its individual > constituent pages. This ensures all of them will have the memcg > linkage set up for correct uncharging. Also update the comments a bit > to clarify what exactly is happening to the page during that race. > > This bug is old and has its roots in the speculative page cache patch > and adding cgroup accounting of kernel pages. There are no known user > reports. A backport to stable is therefor not warranted. > > Reported-by: Matthew Wilcox > Signed-off-by: Johannes Weiner Acked-by: Michal Hocko Thanks! > --- > mm/page_alloc.c | 33 +++++++++++++++++++++++++++------ > 1 file changed, 27 insertions(+), 6 deletions(-) > > diff --git a/mm/page_alloc.c b/mm/page_alloc.c > index c53fe4fa10bf..f4bd56656402 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,33 @@ 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)) > + /* > + * 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; > + } > + > + /* > + * The speculative reference will put and free the page. > + * > + * However, if the speculation was into a higher-order page > + * chunk that isn't marked compound, the other side will know > + * nothing about our buddy pages and only free the order-0 > + * page at the start of our chunk! 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 (!PageHead(page) && order > 0) { > + split_page_memcg(page, 1 << order); > while (order-- > 0) > free_the_page(page + (1 << order), order); > + } > } > EXPORT_SYMBOL(__free_pages); > > -- > 2.30.1 > -- Michal Hocko SUSE Labs