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=-3.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED autolearn=no 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 24105C2D0A8 for ; Tue, 29 Sep 2020 01:17:33 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 9B1A72083B for ; Tue, 29 Sep 2020 01:17:30 +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="ZQdPo6H2" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 9B1A72083B 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 206576B005C; Mon, 28 Sep 2020 21:17:30 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1B63B6B005D; Mon, 28 Sep 2020 21:17:30 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0CE056B0062; Mon, 28 Sep 2020 21:17:30 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0077.hostedemail.com [216.40.44.77]) by kanga.kvack.org (Postfix) with ESMTP id DC2596B005C for ; Mon, 28 Sep 2020 21:17:24 -0400 (EDT) Received: from smtpin29.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 9ECB1181AE86A for ; Tue, 29 Sep 2020 01:17:24 +0000 (UTC) X-FDA: 77314336008.29.size50_000407c27186 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin29.hostedemail.com (Postfix) with ESMTP id 6DE9118086CDA for ; Tue, 29 Sep 2020 01:17:24 +0000 (UTC) X-HE-Tag: size50_000407c27186 X-Filterd-Recvd-Size: 4508 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) by imf06.hostedemail.com (Postfix) with ESMTP for ; Tue, 29 Sep 2020 01:17:23 +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=milDVUuSmfsrFdAiaYla+xJb7Ck9lXAUrHzP+4VmmLU=; b=ZQdPo6H20LXu1iZOWIBNJa8yEH uH3n4rP2XE4/AR9ONbmBuEIy496TgmygeYkD1I23N1f9tyJphyaMvsd1Zv1XrgNXdLYhk8/eYQ+ov DzcSrIEhB2j+cSuhENWH5SZ2cXk8PEgknrtmU1fkv2yzgwpHhCyRscOr/icVpPqOrlUe+zk+QBTGi oG3uda1pKDPTaajGAdui9H6LeinKU3/eYhZLxGBhf55M6mewss9Kh7yzsn/JGShzC6xdh3759Ebnx C1VMhq3s91BkO0oeumwx6fqbPvwvheLykwLuwA/yj9bnuNuoiovjxJzZ20/rFw2VXCq8lqw4aKQVR t93Mb1hw==; Received: from willy by casper.infradead.org with local (Exim 4.92.3 #3 (Red Hat Linux)) id 1kN4Gd-0003UE-Sw; Tue, 29 Sep 2020 01:17:19 +0000 Date: Tue, 29 Sep 2020 02:17:19 +0100 From: Matthew Wilcox To: Andrew Morton Cc: Nick Piggin , Hugh Dickins , Peter Zijlstra , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2] page_alloc: Fix freeing non-compound pages Message-ID: <20200929011719.GD30994@casper.infradead.org> References: <20200926213919.26642-1-willy@infradead.org> <20200928180307.7573f3b6128b5e3007dfc9f0@linux-foundation.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200928180307.7573f3b6128b5e3007dfc9f0@linux-foundation.org> 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 Mon, Sep 28, 2020 at 06:03:07PM -0700, Andrew Morton wrote: > On Sat, 26 Sep 2020 22:39:19 +0100 "Matthew Wilcox (Oracle)" wrote: > > > Here is a very rare race which leaks memory: > > Not worth a cc:stable? Yes, it probably should have been. I just assume the stablebot will pick up anything that has a Fixes: tag. > > 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 > > P0 finds 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) > > __free_pages(P0, 1), I assume. Good catch. That was what I meant to type. > > P0 has refcount 1 > > put_page(P0) > > but this is implicitly order 0 Right, because it's not a compound page. > > P1 is not freed > > huh. Yeah. Nasty, and we'll never know how often it was hit. > > 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. > > > > ... > > > > --- a/mm/page_alloc.c > > +++ b/mm/page_alloc.c > > @@ -4947,6 +4947,9 @@ 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); > > Well that's weird and scary looking. `page' has non-zero refcount yet > we go and free random followon pages. Methinks it merits an > explanatory comment? Well, poot. I lost that comment in the shuffling of patches. In a different tree, I have: @@ -4943,10 +4943,19 @@ static inline void free_the_page(struct page *page, unsi gned 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); Although I'm now thinking of making that comment into kernel-doc and turning it into advice to the caller rather than an internal note to other mm developers.