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=-9.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI, SIGNED_OFF_BY,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 D69FFC4363D for ; Thu, 24 Sep 2020 11:07:24 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id AD06B20702 for ; Thu, 24 Sep 2020 11:07:23 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="WThpDVP8" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org AD06B20702 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 343AB900018; Thu, 24 Sep 2020 07:07:23 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2CED2900017; Thu, 24 Sep 2020 07:07:23 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1BDD3900018; Thu, 24 Sep 2020 07:07:23 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0008.hostedemail.com [216.40.44.8]) by kanga.kvack.org (Postfix) with ESMTP id 00379900017 for ; Thu, 24 Sep 2020 07:07:22 -0400 (EDT) Received: from smtpin11.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id BB5993635 for ; Thu, 24 Sep 2020 11:07:22 +0000 (UTC) X-FDA: 77297678724.11.class80_2f0f7482715e Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin11.hostedemail.com (Postfix) with ESMTP id 9E78B180F8B82 for ; Thu, 24 Sep 2020 11:07:22 +0000 (UTC) X-HE-Tag: class80_2f0f7482715e X-Filterd-Recvd-Size: 5118 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) by imf49.hostedemail.com (Postfix) with ESMTP for ; Thu, 24 Sep 2020 11:07:22 +0000 (UTC) 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=xHIvmY7SxVL7QZZfXEa16TQCn1PJXqfXa3DjNUUN44k=; b=WThpDVP8mqSB79pWIGHyJJ/AN5 dayM0iJlJ2Uno57sSCPp5a6sv2TaMjaZW1tFbmsY3bkXQ38+XAbhiZ4Ueybtrafrx9YyP4okZgST1 OjM2lt8rizcNcYmaE+wCDPsW9//mo4aS1fB0nl6OQvSaFa8zB33xSnO8GxgaF7sYQB44n6UMZq+8v cMyj4wJW3JnuOikTvkfma41UBLB3na4DDzOmruIAVGD8qKTQLVuFOwrK+TPXu7yu76M/KUm+SZ4ee 1D24ePf/LLN25uP+cObw7FXf0ALjJyhgzRHk8K+XlnwAEsFu+TaKjIl7aZ/DQXIHMiL5fy7/RayCb bhMzLh2A==; Received: from willy by casper.infradead.org with local (Exim 4.92.3 #3 (Red Hat Linux)) id 1kLP5k-0000FL-QV; Thu, 24 Sep 2020 11:07:14 +0000 Date: Thu, 24 Sep 2020 12:07:12 +0100 From: Matthew Wilcox To: peterz@infradead.org Cc: Andrew Morton , Hugh Dickins , Daisuke Nishimura , linux-mm@kvack.org, linux-kernel@vger.kernel.org, npiggin@gmail.com Subject: Re: [PATCH] page_alloc: Fix freeing non-compound pages Message-ID: <20200924110712.GU32101@casper.infradead.org> References: <20200922140017.26387-1-willy@infradead.org> <20200924090002.GG1362448@hirez.programming.kicks-ass.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200924090002.GG1362448@hirez.programming.kicks-ass.net> 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 Thu, Sep 24, 2020 at 11:00:02AM +0200, peterz@infradead.org wrote: > On Tue, Sep 22, 2020 at 03:00:17PM +0100, Matthew Wilcox (Oracle) wrote: > > Here is a very rare race which leaks memory: > > > > Page P0 is allocated to the page cache. > > Page P1 is free. > > > > Thread A Thread B Thread C > > find_get_entry(): > > xas_load() returns P0 > > Removes P0 from page cache > > Frees P0 > > P0 merged with its buddy P1 > > alloc_pages(GFP_KERNEL, 1) returns P0 > > P0 has refcount 1 > > page_cache_get_speculative(P0) > > P0 has refcount 2 > > __free_pages(P0) > > P0 has refcount 1 > > put_page(P0) > > P1 is not freed > > > > Fix this by freeing all the pages in __free_pages() that won't be freed > > by the call to put_page(). It's usually not a good idea to split a page, > > but this is a very unlikely scenario. > > > > Fixes: e286781d5f2e ("mm: speculative page references") > > Signed-off-by: Matthew Wilcox (Oracle) > > --- > > mm/page_alloc.c | 9 +++++++++ > > 1 file changed, 9 insertions(+) > > > > diff --git a/mm/page_alloc.c b/mm/page_alloc.c > > index fab5e97dc9ca..5db74797db39 100644 > > --- a/mm/page_alloc.c > > +++ b/mm/page_alloc.c > > @@ -4943,10 +4943,19 @@ static inline void free_the_page(struct page *page, unsigned int order) > > __free_pages_ok(page, order); > > } > > > > +/* > > + * If we free a non-compound allocation, another thread may have a > > + * speculative reference to the first page. It has no way of knowing > > + * about the rest of the allocation, so we have to free all but the > > + * first page here. > > + */ > > 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); > > } > > EXPORT_SYMBOL(__free_pages); > > So the obvious question I have here is why not teach put_page() to free > the whole thing? That's more complicated. It looks like this: Fix this by converting P0 into a compound page if it is not freed by __free_pages(). Fixes: e286781d5f2e ("mm: speculative page references") Signed-off-by: Matthew Wilcox (Oracle) diff --git a/mm/page_alloc.c b/mm/page_alloc.c index fab5e97dc9ca..3e9f6e6694e7 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -4943,10 +4943,25 @@ static inline void free_the_page(struct page *page, unsigned int order) __free_pages_ok(page, order); } +/* + * Have to be careful when freeing a non-compound allocation in case somebody + * else takes a temporary reference on the first page and then calls put_page() + */ void __free_pages(struct page *page, unsigned int order) { - if (put_page_testzero(page)) - free_the_page(page, order); + if (likely(page_ref_freeze(page, 1))) + goto free; + if (likely(order == 0 || PageHead(page))) { + if (put_page_testzero(page)) + goto free; + return; + } + + prep_compound_page(page, order); + put_page(page); + return; +free: + free_the_page(page, order); } EXPORT_SYMBOL(__free_pages);