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=-4.5 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, NICE_REPLY_A,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 34AB7C4727E for ; Tue, 29 Sep 2020 01:03:12 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 9724B21734 for ; Tue, 29 Sep 2020 01:03:11 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=kernel.org header.i=@kernel.org header.b="obogJ7/b" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 9724B21734 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=linux-foundation.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id D28078E0001; Mon, 28 Sep 2020 21:03:10 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CD9046B005D; Mon, 28 Sep 2020 21:03:10 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BEE928E0001; Mon, 28 Sep 2020 21:03:10 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id A692B6B005C for ; Mon, 28 Sep 2020 21:03:10 -0400 (EDT) Received: from smtpin09.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 71731181AE864 for ; Tue, 29 Sep 2020 01:03:10 +0000 (UTC) X-FDA: 77314300140.09.thumb17_200d3ea27186 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin09.hostedemail.com (Postfix) with ESMTP id 51D31180AD801 for ; Tue, 29 Sep 2020 01:03:10 +0000 (UTC) X-HE-Tag: thumb17_200d3ea27186 X-Filterd-Recvd-Size: 2968 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by imf40.hostedemail.com (Postfix) with ESMTP for ; Tue, 29 Sep 2020 01:03:09 +0000 (UTC) Received: from localhost.localdomain (c-73-231-172-41.hsd1.ca.comcast.net [73.231.172.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 91BDA207C4; Tue, 29 Sep 2020 01:03:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1601341388; bh=qUqNMPceAYwULHvwzL9M1XQiqeX3zMRo9KGrVYcVkjE=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=obogJ7/blmlFwHh/iTIA/RUNLMj7jn6jcs7QiXkMIruzgBluF9VFsVfT3i15rgo7O XbQJ1BDtk1Q6M+lMrbdoR5ZTN5JZ/xgxWJvJeVPWtXFrEkS91d7lN8T3IAVHH8UvKa gt7PQeeXFm+elZPC6zZ3J2gzctGqE/Ey1Q5hOAW8= Date: Mon, 28 Sep 2020 18:03:07 -0700 From: Andrew Morton To: "Matthew Wilcox (Oracle)" 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: <20200928180307.7573f3b6128b5e3007dfc9f0@linux-foundation.org> In-Reply-To: <20200926213919.26642-1-willy@infradead.org> References: <20200926213919.26642-1-willy@infradead.org> X-Mailer: Sylpheed 3.5.1 (GTK+ 2.24.31; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit 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 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? > 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. > P0 has refcount 1 > put_page(P0) but this is implicitly order 0 > P1 is not freed huh. > 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?