From patchwork Wed Feb 2 08:44:58 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christophe Leroy X-Patchwork-Id: 12732714 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D4D8EC433FE for ; Wed, 2 Feb 2022 08:45:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245085AbiBBIpA convert rfc822-to-8bit (ORCPT ); Wed, 2 Feb 2022 03:45:00 -0500 Received: from mail-eopbgr90048.outbound.protection.outlook.com ([40.107.9.48]:8736 "EHLO FRA01-MR2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S245070AbiBBIpA (ORCPT ); Wed, 2 Feb 2022 03:45:00 -0500 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=WZWMOumrTSJJGQVmUz6mtthZO+BX2keaEIdhstOQUgcxc/x3oX3TozTUuC4Ft8dWLdI+l1RaYGHAYCKHxzI5imXrCm0sOU9fvNJGRmuykD+tLycAhKWfgUvctrSNCU+4kp+jnoyDzK5wnPMfp3awpVtjkgqNax96uWMqsN6UsrDgKnUlUglP/K8P2+Z+5INO0XN306claqKyoXGFn+M9lhMsWT8KCqdDpLUDqssTuSnUSeuknu7uHUav/GaXx8jqTHwC5U/LAbELC9yYc12120+k6CIG6GP6Y/+61c67OO2WB2diIaYl+I4Wfb2ac1P6DW2v/WuoeruWQ77HcoDu9g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=8wBo4nIdN2RFAwFq84yvv3HNVVFCq353gnyFKfCn4EQ=; b=OOWd6OZTkR5Yd7vzcx/VT/U7Xw9rFVIgnGiSdpSGaIPZ/gE19hfS9douJYv4JoNmYuIUoi0xEPT3UVibPbGL5MLud8TgAY6HMjkhFi91bFXY2vFWku/jy6pivuMeGqnhHpPUFJuN5fLWWk3tjE3DsgJt+kge2XLnaPIwIBBLn+S1Q3Ve8xtjQ/x0vXhveovKEq03HLlBoo4pchl3LZZaUoG8nSFQA85rUMlD286udJ4zNldVqpaG8+PDbH7hF5tdhLZxI6LrOXzwMpOxn+D2pEuyzzwWuLrFVQ/TcqCt5WzjsPi4RweDbq3nZuiBFFQg9fEk3swWDqAsT9eZXmnDBw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none Received: from MRZP264MB2988.FRAP264.PROD.OUTLOOK.COM (2603:10a6:501:31::15) by MR1P264MB4276.FRAP264.PROD.OUTLOOK.COM (2603:10a6:501:42::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4951.11; Wed, 2 Feb 2022 08:44:58 +0000 Received: from MRZP264MB2988.FRAP264.PROD.OUTLOOK.COM ([fe80::c9a2:1db0:5469:54e1]) by MRZP264MB2988.FRAP264.PROD.OUTLOOK.COM ([fe80::c9a2:1db0:5469:54e1%9]) with mapi id 15.20.4951.012; Wed, 2 Feb 2022 08:44:58 +0000 From: Christophe Leroy To: Andrey Ryabinin , Alexander Potapenko , Andrey Konovalov , Dmitry Vyukov , Andrew Morton CC: Christophe Leroy , "linux-kernel@vger.kernel.org" , "linux-mm@kvack.org" , "linux-hardening@vger.kernel.org" , "kasan-dev@googlegroups.com" Subject: [PATCH 2/4] mm/kasan: Move kasan_pXX_table() and kasan_early_shadow_page_entry() Thread-Topic: [PATCH 2/4] mm/kasan: Move kasan_pXX_table() and kasan_early_shadow_page_entry() Thread-Index: AQHYGBEobYxXe/WLd0imaMeHUpNeig== Date: Wed, 2 Feb 2022 08:44:58 +0000 Message-ID: <3fe9bf0867b2ffc7cd43fe7040ee18d245641ec1.1643791473.git.christophe.leroy@csgroup.eu> References: In-Reply-To: Accept-Language: fr-FR, en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: authentication-results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=csgroup.eu; x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: 22de4de2-d2b5-478a-b76a-08d9e6284b28 x-ms-traffictypediagnostic: MR1P264MB4276:EE_ x-microsoft-antispam-prvs: x-ms-oob-tlc-oobclassifiers: OLM:6430; x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: uI8b/v92ssuNqCBoj7rr9TnkNw653L/5dLA4aiJUCxM3ZwIEoU/t0TVeiUMseVVNRp8Xt8xfHW2Zs8/3cmHQ9iKlM7gTAF6NC5+LQkXUQjgfSg2uY4bubQVpUYTXmE2FFHRjpfSfHlNVfe+oIRqtubwpF1EdKc4xAjrX8I//MMPGPuXUEhNN1ZCgIA75Mo+lrWK8JsYrdTqG/LoS31+er+FowE0fm6ITuoZJB8aC2CMEzoLxfSi8jBb6I3V+wB41Q8ADlEHwC0LKUAeZ+RH4n5cph6S5IUt/aI/7EfIcV1l/AyhktmtCB3ZGbi4ClzqaTZRSuwZW7zYk63IYqRsrofPcbtydnLfPj60s9RG175w0ECH8YlBBVbM74z7JPRBJUNVikZrzP3tx9E1rPQa7PKAgmBuCzVq2lzF/L09uD30VgFZ7IuqxdW949Fijrh1yjlzvpkMZbOt7g3JhHPz4vW5AfAS9aaJmO0pwGfBVIl4Y4aIUdNfjbsiKFZa/48+Z3PfNe7KI3/LZkPFftFbrhPX3bxGMclSZQKUGx5tIyG28cLyV3wcbmfD2YZmuZG59qVCGZCZKQyKIj/x++a9fntIyaeF9mVGf1aF64qFSg9TqTVbq2wVDh/AMLNs85L56nluHUHvYYT4abCCiEECnSfOi2jtjmrzYbrbGyIt4AFSUIAtQS3MvMSHmJ6GLxFoKDQwDt382+MqucoGO7D1/vg== x-forefront-antispam-report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:MRZP264MB2988.FRAP264.PROD.OUTLOOK.COM;PTR:;CAT:NONE;SFS:(13230001)(4636009)(366004)(6486002)(2616005)(26005)(186003)(508600001)(76116006)(66946007)(66446008)(71200400001)(91956017)(5660300002)(36756003)(66556008)(66476007)(8936002)(64756008)(8676002)(2906002)(4326008)(122000001)(38100700002)(86362001)(316002)(110136005)(44832011)(54906003)(6512007)(6506007)(83380400001)(38070700005);DIR:OUT;SFP:1101; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?iso-8859-1?q?dqgUGT6Qod7qf1SE6nAmUjd?= =?iso-8859-1?q?4G8yGctp6SqIXypebggRqPmQPFQFIr2yb6GJZE6A0N1Kp1HmEZAu7qMQuGzz?= =?iso-8859-1?q?2ObbGuK5UbYnECJzk1LXS6QEGy8o5/UJhDX7Y1/QkpE4O8hHRFlutdE6wVDt?= =?iso-8859-1?q?xJNZJzMIdxiPuB9gkSW8N0nP7AaoZwHxwO/EAUigFd/l9DNTknOwB6PsbXVR?= =?iso-8859-1?q?UZ/39d7cu6AwF5jmENSEQf5WQ0keeHJzDYz5dneKXdYGEnJWZIeggfSehOIw?= =?iso-8859-1?q?a9p7Zrs1r2uVM8hh1FQQDrI3QdRnh3h7SDYvdL52r8z9SyflnKJeqBrzmzj8?= =?iso-8859-1?q?yJtLyY02VXSyn41xSZ5lNH6RMu1cb0oBPhvWcgO5gF8d+cIxkT6rU4t3ag15?= =?iso-8859-1?q?UaWShOn1OAxIw8wklQnAGyvkXYD7vnOZnNoBikMaQMebH/AOWrHUzPG2t8Cb?= =?iso-8859-1?q?f9DkUtrPp7Eb3ak7pw/9GRtaW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HW?= =?iso-8859-1?q?lnf1JMSe24Y3ge4EgP8PhM4A1FLYiDQrVgEiQSn8ARCC0/S15FKi50LsxcFW?= =?iso-8859-1?q?2QY0ZgvLP1zAVRxPN2M8A+aIq4LXJO0HFwZGZt6aRT0+gclT6s02YbElJxXo?= =?iso-8859-1?q?tGUhT1a/QRh1D/us2qCBnSzwG/HikkWM6Jg+yzUFRKeMLf/XjZki4frrcXwJ?= =?iso-8859-1?q?mw1C5JLdfODGBrVxtJVXi+e61zFXlr76rgFYgJBpNFiNKipnXOY+ONiEmyE5?= =?iso-8859-1?q?FkbT5SkQwRQy13MVyGM79b4LhXYg4QtrAZmqMzakudkmWSgIcNK8MoSf45ds?= =?iso-8859-1?q?0+nPlXeExpjj6pLz3l1hjwX0UPYhkNHfLrEwml9z0ClHd9H9gM/+LrT1Iboo?= =?iso-8859-1?q?9VlM=3D?= MIME-Version: 1.0 X-OriginatorOrg: csgroup.eu X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: MRZP264MB2988.FRAP264.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-Network-Message-Id: 22de4de2-d2b5-478a-b76a-08d9e6284b28 X-MS-Exchange-CrossTenant-originalarrivaltime: 02 Feb 2022 08:44:58.3050 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 9914def7-b676-4fda-8815-5d49fb3b45c8 X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: 5jqpnrEHw8N6wfhiQbsud8slVsQPpsED+BePjXS5fcdSsIR+gXpVlLM4i1zDSsurSuZ7Yj/4X4GFRSj/AMAvPhU5tOnX+Hq/1bUbVNcN72I= X-MS-Exchange-Transport-CrossTenantHeadersStamped: MR1P264MB4276 Precedence: bulk List-ID: X-Mailing-List: linux-hardening@vger.kernel.org In order to reuse them outside of mm/kasan/init.c, move the following helpers outside of init.c: kasan_p4d_table() kasan_pud_table() kasan_pmd_table() kasan_pte_table() kasan_early_shadow_page_entry() And make them available when KASAN is not selected. Inclusion of kasan.h in mm.h needs to be moved down a bit in order to get lm_alias() definition. Signed-off-by: Christophe Leroy Reported-by: kernel test robot Reported-by: kernel test robot --- include/linux/kasan.h | 57 +++++++++++++++++++++++++++++++++++++++++++ include/linux/mm.h | 3 ++- mm/kasan/init.c | 37 ---------------------------- 3 files changed, 59 insertions(+), 38 deletions(-) diff --git a/include/linux/kasan.h b/include/linux/kasan.h index c29778b25d8a..1629797198ec 100644 --- a/include/linux/kasan.h +++ b/include/linux/kasan.h @@ -28,6 +28,7 @@ struct kunit_kasan_expectation { #ifdef CONFIG_KASAN_SOFTWARE #include +#include /* Software KASAN implementations use shadow memory. */ @@ -81,6 +82,62 @@ static inline void kasan_disable_current(void) {} #endif /* CONFIG_KASAN_SOFTWARE */ +#if defined(CONFIG_KASAN_SOFTWARE) && CONFIG_PGTABLE_LEVELS > 4 +static inline bool kasan_p4d_table(pgd_t pgd) +{ + return pgd_page(pgd) == virt_to_page(lm_alias(kasan_early_shadow_p4d)); +} +#else +static inline bool kasan_p4d_table(pgd_t pgd) +{ + return false; +} +#endif +#if defined(CONFIG_KASAN_SOFTWARE) && CONFIG_PGTABLE_LEVELS > 3 +static inline bool kasan_pud_table(p4d_t p4d) +{ + return p4d_page(p4d) == virt_to_page(lm_alias(kasan_early_shadow_pud)); +} +#else +static inline bool kasan_pud_table(p4d_t p4d) +{ + return false; +} +#endif +#if defined(CONFIG_KASAN_SOFTWARE) && CONFIG_PGTABLE_LEVELS > 2 +static inline bool kasan_pmd_table(pud_t pud) +{ + return pud_page(pud) == virt_to_page(lm_alias(kasan_early_shadow_pmd)); +} +#else +static inline bool kasan_pmd_table(pud_t pud) +{ + return false; +} +#endif + +#ifdef CONFIG_KASAN_SOFTWARE +static inline bool kasan_pte_table(pmd_t pmd) +{ + return pmd_page(pmd) == virt_to_page(lm_alias(kasan_early_shadow_pte)); +} + +static inline bool kasan_early_shadow_page_entry(pte_t pte) +{ + return pte_page(pte) == virt_to_page(lm_alias(kasan_early_shadow_page)); +} +#else +static inline bool kasan_pte_table(pmd_t pmd) +{ + return false; +} + +static inline bool kasan_early_shadow_page_entry(pte_t pte) +{ + return false; +} +#endif + #ifdef CONFIG_KASAN_HW_TAGS DECLARE_STATIC_KEY_FALSE(kasan_flag_enabled); diff --git a/include/linux/mm.h b/include/linux/mm.h index e1a84b1e6787..b06ee84b3717 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -31,7 +31,6 @@ #include #include #include -#include struct mempolicy; struct anon_vma; @@ -121,6 +120,8 @@ extern int mmap_rnd_compat_bits __read_mostly; #define lm_alias(x) __va(__pa_symbol(x)) #endif +#include + /* * To prevent common memory management code establishing * a zero page mapping on a read fault. diff --git a/mm/kasan/init.c b/mm/kasan/init.c index cc64ed6858c6..e863071a49ef 100644 --- a/mm/kasan/init.c +++ b/mm/kasan/init.c @@ -30,53 +30,16 @@ unsigned char kasan_early_shadow_page[PAGE_SIZE] __page_aligned_bss; #if CONFIG_PGTABLE_LEVELS > 4 p4d_t kasan_early_shadow_p4d[MAX_PTRS_PER_P4D] __page_aligned_bss; -static inline bool kasan_p4d_table(pgd_t pgd) -{ - return pgd_page(pgd) == virt_to_page(lm_alias(kasan_early_shadow_p4d)); -} -#else -static inline bool kasan_p4d_table(pgd_t pgd) -{ - return false; -} #endif #if CONFIG_PGTABLE_LEVELS > 3 pud_t kasan_early_shadow_pud[MAX_PTRS_PER_PUD] __page_aligned_bss; -static inline bool kasan_pud_table(p4d_t p4d) -{ - return p4d_page(p4d) == virt_to_page(lm_alias(kasan_early_shadow_pud)); -} -#else -static inline bool kasan_pud_table(p4d_t p4d) -{ - return false; -} #endif #if CONFIG_PGTABLE_LEVELS > 2 pmd_t kasan_early_shadow_pmd[MAX_PTRS_PER_PMD] __page_aligned_bss; -static inline bool kasan_pmd_table(pud_t pud) -{ - return pud_page(pud) == virt_to_page(lm_alias(kasan_early_shadow_pmd)); -} -#else -static inline bool kasan_pmd_table(pud_t pud) -{ - return false; -} #endif pte_t kasan_early_shadow_pte[MAX_PTRS_PER_PTE + PTE_HWTABLE_PTRS] __page_aligned_bss; -static inline bool kasan_pte_table(pmd_t pmd) -{ - return pmd_page(pmd) == virt_to_page(lm_alias(kasan_early_shadow_pte)); -} - -static inline bool kasan_early_shadow_page_entry(pte_t pte) -{ - return pte_page(pte) == virt_to_page(lm_alias(kasan_early_shadow_page)); -} - static __init void *early_alloc(size_t size, int node) { void *ptr = memblock_alloc_try_nid(size, size, __pa(MAX_DMA_ADDRESS),