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=-12.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,USER_AGENT_GIT 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 4FB8FC4363D for ; Tue, 22 Sep 2020 14:00:47 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id CB38E20719 for ; Tue, 22 Sep 2020 14:00:46 +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="LAK+hx0V" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org CB38E20719 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 53FC86B00E2; Tue, 22 Sep 2020 10:00:46 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5162B6B00E6; Tue, 22 Sep 2020 10:00:46 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 42AEB900063; Tue, 22 Sep 2020 10:00:46 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 2D8A06B00E2 for ; Tue, 22 Sep 2020 10:00:46 -0400 (EDT) Received: from smtpin25.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id D565082499A8 for ; Tue, 22 Sep 2020 14:00:45 +0000 (UTC) X-FDA: 77290858050.25.mint26_0e09ca92714e Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin25.hostedemail.com (Postfix) with ESMTP id 9C4741804E3B7 for ; Tue, 22 Sep 2020 14:00:33 +0000 (UTC) X-HE-Tag: mint26_0e09ca92714e X-Filterd-Recvd-Size: 3247 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) by imf21.hostedemail.com (Postfix) with ESMTP for ; Tue, 22 Sep 2020 14:00:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:MIME-Version: Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:In-Reply-To:References; bh=Yx8f8GkqtnDFX1ZsrbiU82PuTcVlvAe9aJmHSokocpE=; b=LAK+hx0VgwhamqafHhHwBfYKHN +j0fmrQS2tLZ1HDtIXhPvrc1EI/R4d4mDYvIPMY9sbkIztfhGpVz47iNQclmx88S08wQ+jkbWxYHr cuTuAql63wyZzIOL+o2YJUKVKWYSTe9ZNVhOzGkDVSMHpxPGFECrdc3YmSjfD4fhtZ4bRbK0zGfKk GR/udnDuZ9JBNMyW4pTIsAgYUBayjvc4dbgRfr/k7PNBPs1noOQWSD0Kcpzjap6JXTa2+JEsYsFo8 VBn+uetkK2R2GTo9gUiSXErrea4aIxCu+WxGYAykDL4NQq20n0ih1V2EirbhlqSq0B9HAa8lJHZ9Q h2buQGrw==; Received: from willy by casper.infradead.org with local (Exim 4.92.3 #3 (Red Hat Linux)) id 1kKiqH-0006t1-Da; Tue, 22 Sep 2020 14:00:25 +0000 From: "Matthew Wilcox (Oracle)" To: Andrew Morton Cc: "Matthew Wilcox (Oracle)" , Nick Piggin , Hugh Dickins , Peter Zijlstra , Daisuke Nishimura , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH] page_alloc: Fix freeing non-compound pages Date: Tue, 22 Sep 2020 15:00:17 +0100 Message-Id: <20200922140017.26387-1-willy@infradead.org> X-Mailer: git-send-email 2.21.3 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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: 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 *pag= e, unsigned int order) __free_pages_ok(page, order); } =20 +/* + * 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 + while (order-- > 0) + free_the_page(page + (1 << order), order); } EXPORT_SYMBOL(__free_pages); =20 --=20 2.28.0