Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp3541901pxb; Mon, 24 Jan 2022 11:48:48 -0800 (PST) X-Google-Smtp-Source: ABdhPJzF9MNhbFxMwSCWls7taM7WTlRkjXmmEbBL2p80s9zSkGQBbVX+3lhUnQKPrn1yq1tGilNg X-Received: by 2002:a17:90b:3b8a:: with SMTP id pc10mr3448741pjb.225.1643053728372; Mon, 24 Jan 2022 11:48:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643053728; cv=none; d=google.com; s=arc-20160816; b=w9dGeIWR+12gTG5gMhw5InY8f6Vk4hkp5zIbBK6sVFJjHmGr0hn9N03xXvfJj+3DRW UED8ke9/sqXRCV/JJye7Tk40b/xyaFauKFsuOxFcZs4mRxxdibChDE6ff9SBWrl7qNtD eTDWxtUsNv80E+Z3eoXoDtrxhsFRdRdIdhZM9q6eGrixJetmVlzEDSeGGbAolfJ7N/74 BUcwcua2OqRa63kmbsAxwWORwSLVheW3YNvMUZVk6WkpCW+tvA3tRmFg4i5TUscJqoFg SVfbspxOb/tMkh8ETeMpk3MMF/gLfBJJVoOCn50sd18LcpVsKN7sLD7BDQhQkKKofJWe rjvA== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=gDlDqsc+3DHa6tPXFigQr9tfOOtGP10nZ0GpFTN28Vg=; b=q6oy9KQGuF4csEJaiPNulBOcFRX6M/uIzSt8vUdKqSI5/Mt9PzD6bWtLmhNTEbzX9N 92nGJlOVpJ6/zxl32hvKPMiUwatqF/HVoxZPM4M2EE51CUXXCmuxNZ1LdS0iGy84i0M9 jMOBu1EFkEsCrUwRsFP4UOvZYVsdT/k5A2SKLJAnciQ5ILXgzz3BpY6vEbsPeuu/b07g uLq2M5xzEZoaiOhLBJmpiG7aYsOT3v0+CurMyHmBq4pUJPB+XPorNWm7ZtO7sYLc1/cF AY4bsJBM+JTw29fZ6KWSSCTT0jCtgCs5xJUOi3b6XaWO4MeA53JN7V/0TufmBb9tDXuU JN/Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=sJTI5Fax; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id w4si12548420plz.621.2022.01.24.11.48.35; Mon, 24 Jan 2022 11:48:48 -0800 (PST) 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=@linux.dev header.s=key1 header.b=sJTI5Fax; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244818AbiAXSC7 (ORCPT + 99 others); Mon, 24 Jan 2022 13:02:59 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59760 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235628AbiAXSC4 (ORCPT ); Mon, 24 Jan 2022 13:02:56 -0500 Received: from out0.migadu.com (out0.migadu.com [IPv6:2001:41d0:2:267::]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 20B77C06173B for ; Mon, 24 Jan 2022 10:02:56 -0800 (PST) X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1643047374; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=gDlDqsc+3DHa6tPXFigQr9tfOOtGP10nZ0GpFTN28Vg=; b=sJTI5FaxcGTwF846z3/ydLfSuNnKku2txX4G59n13W4L9nS0b5/BZHYgIv5bO/1jtrymZr N1C9m4U2R0bLFagsWmBm9B7vVmbidJJkP2vbPkTi26zaqAPAgKBoLNEMY/9EUt9kqiOAKH sSTjc3NiCkQdel9OZtDjRVLL0nSTows= From: andrey.konovalov@linux.dev To: Andrew Morton Cc: Andrey Konovalov , Marco Elver , Alexander Potapenko , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, linux-mm@kvack.org, Vincenzo Frascino , Catalin Marinas , Will Deacon , Mark Rutland , linux-arm-kernel@lists.infradead.org, Peter Collingbourne , Evgenii Stepanov , linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH v6 01/39] kasan, page_alloc: deduplicate should_skip_kasan_poison Date: Mon, 24 Jan 2022 19:02:09 +0100 Message-Id: <658b79f5fb305edaf7dc16bc52ea870d3220d4a8.1643047180.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Andrey Konovalov Currently, should_skip_kasan_poison() has two definitions: one for when CONFIG_DEFERRED_STRUCT_PAGE_INIT is enabled, one for when it's not. Instead of duplicating the checks, add a deferred_pages_enabled() helper and use it in a single should_skip_kasan_poison() definition. Also move should_skip_kasan_poison() closer to its caller and clarify all conditions in the comment. Signed-off-by: Andrey Konovalov --- Changes v2->v3: - Update patch description. --- mm/page_alloc.c | 55 +++++++++++++++++++++++++++++-------------------- 1 file changed, 33 insertions(+), 22 deletions(-) diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 3589febc6d31..25d4f9ad3525 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -378,25 +378,9 @@ int page_group_by_mobility_disabled __read_mostly; */ static DEFINE_STATIC_KEY_TRUE(deferred_pages); -/* - * Calling kasan_poison_pages() only after deferred memory initialization - * has completed. Poisoning pages during deferred memory init will greatly - * lengthen the process and cause problem in large memory systems as the - * deferred pages initialization is done with interrupt disabled. - * - * Assuming that there will be no reference to those newly initialized - * pages before they are ever allocated, this should have no effect on - * KASAN memory tracking as the poison will be properly inserted at page - * allocation time. The only corner case is when pages are allocated by - * on-demand allocation and then freed again before the deferred pages - * initialization is done, but this is not likely to happen. - */ -static inline bool should_skip_kasan_poison(struct page *page, fpi_t fpi_flags) +static inline bool deferred_pages_enabled(void) { - return static_branch_unlikely(&deferred_pages) || - (!IS_ENABLED(CONFIG_KASAN_GENERIC) && - (fpi_flags & FPI_SKIP_KASAN_POISON)) || - PageSkipKASanPoison(page); + return static_branch_unlikely(&deferred_pages); } /* Returns true if the struct page for the pfn is uninitialised */ @@ -447,11 +431,9 @@ defer_init(int nid, unsigned long pfn, unsigned long end_pfn) return false; } #else -static inline bool should_skip_kasan_poison(struct page *page, fpi_t fpi_flags) +static inline bool deferred_pages_enabled(void) { - return (!IS_ENABLED(CONFIG_KASAN_GENERIC) && - (fpi_flags & FPI_SKIP_KASAN_POISON)) || - PageSkipKASanPoison(page); + return false; } static inline bool early_page_uninitialised(unsigned long pfn) @@ -1271,6 +1253,35 @@ static int free_tail_pages_check(struct page *head_page, struct page *page) return ret; } +/* + * Skip KASAN memory poisoning when either: + * + * 1. Deferred memory initialization has not yet completed, + * see the explanation below. + * 2. Skipping poisoning is requested via FPI_SKIP_KASAN_POISON, + * see the comment next to it. + * 3. Skipping poisoning is requested via __GFP_SKIP_KASAN_POISON, + * see the comment next to it. + * + * Poisoning pages during deferred memory init will greatly lengthen the + * process and cause problem in large memory systems as the deferred pages + * initialization is done with interrupt disabled. + * + * Assuming that there will be no reference to those newly initialized + * pages before they are ever allocated, this should have no effect on + * KASAN memory tracking as the poison will be properly inserted at page + * allocation time. The only corner case is when pages are allocated by + * on-demand allocation and then freed again before the deferred pages + * initialization is done, but this is not likely to happen. + */ +static inline bool should_skip_kasan_poison(struct page *page, fpi_t fpi_flags) +{ + return deferred_pages_enabled() || + (!IS_ENABLED(CONFIG_KASAN_GENERIC) && + (fpi_flags & FPI_SKIP_KASAN_POISON)) || + PageSkipKASanPoison(page); +} + static void kernel_init_free_pages(struct page *page, int numpages, bool zero_tags) { int i; -- 2.25.1