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,USER_AGENT_GIT autolearn=unavailable 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 D4D85C4727D for ; Sat, 26 Sep 2020 21:39:30 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id B169423719 for ; Sat, 26 Sep 2020 21:39:29 +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="uJKw/s1s" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org B169423719 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 D2FBB6B005C; Sat, 26 Sep 2020 17:39:28 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CB99B6B005D; Sat, 26 Sep 2020 17:39:28 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B80AE6B0068; Sat, 26 Sep 2020 17:39:28 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0130.hostedemail.com [216.40.44.130]) by kanga.kvack.org (Postfix) with ESMTP id 989446B005C for ; Sat, 26 Sep 2020 17:39:28 -0400 (EDT) Received: from smtpin15.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 57A2D181AE867 for ; Sat, 26 Sep 2020 21:39:28 +0000 (UTC) X-FDA: 77306529216.15.bikes45_310b46227173 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin15.hostedemail.com (Postfix) with ESMTP id 32F421814B0C1 for ; Sat, 26 Sep 2020 21:39:28 +0000 (UTC) X-HE-Tag: bikes45_310b46227173 X-Filterd-Recvd-Size: 5631 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) by imf17.hostedemail.com (Postfix) with ESMTP for ; Sat, 26 Sep 2020 21:39:26 +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=mqpR5mKaToPhyrhfrt21itaiYs65hzitILARZIXZNQs=; b=uJKw/s1sTnI9hMT0VNpJW8BESD b6fpkWuw+RMZxi0/HFWNbIqFgzQIbELWbmGR2mBdl0Rr6ln2t4KoP6imCK8aiE5KqRAWhjFZIYBiZ 6yJgWF5WfBiMK6sqaAUByVrc315BtcaJDtL37FesygX+tQNkzUp9ls0M5zdgWOUeXwiaT1d3ir9I9 90g9wT/lh99UiRBjT1hSAkOiWoehTBWVaMkiQGuzBnKWIF+gp7V4gDdwvY7krZUgjFqCLDt2+QdBn G78w2wN97kCLW9OYJTGiAN1GONbRu9oVV2ZWOBuDWt9NjaKIISnuMskDsF0DTNEI6Apj3X/4N8jeq 5N8MqPHg==; Received: from willy by casper.infradead.org with local (Exim 4.92.3 #3 (Red Hat Linux)) id 1kMHub-0006wS-O3; Sat, 26 Sep 2020 21:39:21 +0000 From: "Matthew Wilcox (Oracle)" To: Andrew Morton Cc: "Matthew Wilcox (Oracle)" , Nick Piggin , Hugh Dickins , Peter Zijlstra , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v2] page_alloc: Fix freeing non-compound pages Date: Sat, 26 Sep 2020 22:39:19 +0100 Message-Id: <20200926213919.26642-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 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) 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) --- v2: Add a test module. Verified it works by: (1) loading the test module on an unmodified kernel and watching it OOM (2) modifying __free_pages() with my v1 patch that neglected to add the PageHead test and hitting VM_BUG_ON_PAGE(PageTail(page)) (3) applying all of this patch and seeing it survive lib/Kconfig.debug | 9 +++++++++ lib/Makefile | 1 + lib/test_free_pages.c | 42 ++++++++++++++++++++++++++++++++++++++++++ mm/page_alloc.c | 3 +++ 4 files changed, 55 insertions(+) create mode 100644 lib/test_free_pages.c diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug index e068c3c7189a..eed59af0e907 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug @@ -2369,6 +2369,15 @@ config TEST_HMM =20 If unsure, say N. =20 +config TEST_FREE_PAGES + tristate "Test freeing pages" + help + Test that a memory leak does not occur due to a race between + freeing a block of pages and a speculative page reference. + Loading this module is safe if your kernel has the bug fixed. + If the bug is not fixed, it will leak gigabytes of memory and + probably OOM your system. + config TEST_FPU tristate "Test floating point operations in kernel space" depends on X86 && !KCOV_INSTRUMENT_ALL diff --git a/lib/Makefile b/lib/Makefile index a4a4c6864f51..071b687b7363 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -99,6 +99,7 @@ obj-$(CONFIG_TEST_BLACKHOLE_DEV) +=3D test_blackhole_de= v.o obj-$(CONFIG_TEST_MEMINIT) +=3D test_meminit.o obj-$(CONFIG_TEST_LOCKUP) +=3D test_lockup.o obj-$(CONFIG_TEST_HMM) +=3D test_hmm.o +obj-$(CONFIG_TEST_FREE_PAGES) +=3D test_free_pages.o =20 # # CFLAGS for compiling floating point code inside the kernel. x86/Makefi= le turns diff --git a/lib/test_free_pages.c b/lib/test_free_pages.c new file mode 100644 index 000000000000..074e76bd76b2 --- /dev/null +++ b/lib/test_free_pages.c @@ -0,0 +1,42 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * test_free_pages.c: Check that free_pages() doesn't leak memory + * Copyright (c) 2020 Oracle + * Author: Matthew Wilcox + */ + +#include +#include +#include + +static void test_free_pages(gfp_t gfp) +{ + unsigned int i; + + for (i =3D 0; i < 1000 * 1000; i++) { + unsigned long addr =3D __get_free_pages(gfp, 3); + struct page *page =3D virt_to_page(addr); + + /* Simulate page cache getting a speculative reference */ + get_page(page); + free_pages(addr, 3); + put_page(page); + } +} + +static int m_in(void) +{ + test_free_pages(GFP_KERNEL); + test_free_pages(GFP_KERNEL | __GFP_COMP); + + return 0; +} + +static void m_ex(void) +{ +} + +module_init(m_in); +module_exit(m_ex); +MODULE_AUTHOR("Matthew Wilcox "); +MODULE_LICENSE("GPL"); diff --git a/mm/page_alloc.c b/mm/page_alloc.c index fab5e97dc9ca..9b259c76e285 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -4947,6 +4947,9 @@ void __free_pages(struct page *page, unsigned int o= rder) { 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); =20 --=20 2.28.0