Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp2044863pxk; Sat, 26 Sep 2020 14:41:16 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz/XX9XQmtUCUysrX33Jw6EwN/CYSVuZYpGmAe719NC2TDmw8svSscRdy/BI8rNLbAaZnz6 X-Received: by 2002:a17:906:e4f:: with SMTP id q15mr9314005eji.155.1601156475762; Sat, 26 Sep 2020 14:41:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1601156475; cv=none; d=google.com; s=arc-20160816; b=rgHTtW8gsEALK8r260Y9RdruGnEZL8M7p+0AEiajZ8QdGD/wuHUX6rpTh6MQxyWjl0 IMPbxf5vDPf2WL+YHuHtiSQ00TIKiyyXc2ARZlNkboJevyZoqtfKHDaVpn90MbMVVgiM b0etx32fzi8M4ifKv8cZ7fBwf1iedKP354DiYXbv27qVB5Z6K8umaQTnw+YTjB/JuCgL Ws0GO87qlIdEzKIzS5rPozANAmYautDCg6lwBd3c9g0VV5wsl/dAah/d0+DLkvVQSa8G vJ9sR44WtqsE2SGOLqFmxWZEQxTMFCtuMb5p6pWbpF4yovVt/AZR6VrAMLYQ1OXtb7QZ yHbQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=mqpR5mKaToPhyrhfrt21itaiYs65hzitILARZIXZNQs=; b=YIohWtWiUjXUBmSWjxQ6c1LmdP4zJRTFEhS5ZGTNAW/i6Zfha4yFk+5Q813Dii0uOG dHA3BYP7mZi/SHku7szgcI48Nh0HfOcaEPRclcqREJDrSS1sQBBoOYHdU4JYKyFu5IDg 78beGscP2gfpFzyVYVM+/I/UtpJmBj4InGSbWNEf5TmFHeqq2RX5MyR9ogd9onRyYSul 4wOaLi+C6a77r7vYrdyWjGTb/InPcXrGa6VpX5dfkqiHnShLN6D3YE0ErdqkDFkUKMSj uw2g0BqxL9vkJoCTx+pIohNeEGskbOhERBIS4W6UNQZASOYqZm5XvufEI9w068yd02nM S7Tw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b="uJKw/s1s"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id wq8si4633505ejb.480.2020.09.26.14.40.52; Sat, 26 Sep 2020 14:41:15 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b="uJKw/s1s"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726840AbgIZVj1 (ORCPT + 99 others); Sat, 26 Sep 2020 17:39:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59808 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726242AbgIZVj1 (ORCPT ); Sat, 26 Sep 2020 17:39:27 -0400 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E7BBDC0613CE for ; Sat, 26 Sep 2020 14:39:26 -0700 (PDT) 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: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 If unsure, say N. +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) += test_blackhole_dev.o obj-$(CONFIG_TEST_MEMINIT) += test_meminit.o obj-$(CONFIG_TEST_LOCKUP) += test_lockup.o obj-$(CONFIG_TEST_HMM) += test_hmm.o +obj-$(CONFIG_TEST_FREE_PAGES) += test_free_pages.o # # CFLAGS for compiling floating point code inside the kernel. x86/Makefile 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 = 0; i < 1000 * 1000; i++) { + unsigned long addr = __get_free_pages(gfp, 3); + struct page *page = 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 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); -- 2.28.0