From patchwork Mon Jun 6 07:48:13 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kefeng Wang X-Patchwork-Id: 12870087 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 62EE1CCA481 for ; Mon, 6 Jun 2022 07:38:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 124688D000B; Mon, 6 Jun 2022 03:38:44 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EAB498D0007; Mon, 6 Jun 2022 03:38:43 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CD6058D000A; Mon, 6 Jun 2022 03:38:43 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id AEC938D0009 for ; Mon, 6 Jun 2022 03:38:43 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 89B7460772 for ; Mon, 6 Jun 2022 07:38:43 +0000 (UTC) X-FDA: 79547008926.28.0503E36 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by imf06.hostedemail.com (Postfix) with ESMTP id BED8E180036 for ; Mon, 6 Jun 2022 07:38:37 +0000 (UTC) Received: from dggpemm500023.china.huawei.com (unknown [172.30.72.54]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4LGlg76WWFzgYqd; Mon, 6 Jun 2022 15:36:51 +0800 (CST) Received: from dggpemm500001.china.huawei.com (7.185.36.107) by dggpemm500023.china.huawei.com (7.185.36.83) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Mon, 6 Jun 2022 15:38:38 +0800 Received: from localhost.localdomain.localdomain (10.175.113.25) by dggpemm500001.china.huawei.com (7.185.36.107) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Mon, 6 Jun 2022 15:38:37 +0800 From: Kefeng Wang To: , , , , CC: , , , , Kefeng Wang Subject: [PATCH v4 4/6] mm: ioremap: Add ioremap/iounmap_allowed() Date: Mon, 6 Jun 2022 15:48:13 +0800 Message-ID: <20220606074815.139265-5-wangkefeng.wang@huawei.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20220606074815.139265-1-wangkefeng.wang@huawei.com> References: <20220606074815.139265-1-wangkefeng.wang@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.175.113.25] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To dggpemm500001.china.huawei.com (7.185.36.107) X-CFilter-Loop: Reflected X-Rspam-User: Authentication-Results: imf06.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf06.hostedemail.com: domain of wangkefeng.wang@huawei.com designates 45.249.212.187 as permitted sender) smtp.mailfrom=wangkefeng.wang@huawei.com X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: BED8E180036 X-Stat-Signature: 8rxnfyfokgxff617tw9mezuqbfogr7td X-HE-Tag: 1654501117-678128 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: Add special hook for architecture to verify addr, size or prot when ioremap() or iounmap(), which will make the generic ioremap more useful. ioremap_allowed() return an int, - NULL means continue to remap - error code means skip remap and return directly iounmap_allowed() return an int, - 0 means continue to vunmap - error code means skip vunmap and return directly Acked-by: Andrew Morton Signed-off-by: Kefeng Wang --- include/asm-generic/io.h | 25 +++++++++++++++++++++++++ mm/ioremap.c | 13 ++++++++++--- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/include/asm-generic/io.h b/include/asm-generic/io.h index e6ffa2519f08..9429387a3e65 100644 --- a/include/asm-generic/io.h +++ b/include/asm-generic/io.h @@ -964,6 +964,31 @@ static inline void iounmap(volatile void __iomem *addr) #elif defined(CONFIG_GENERIC_IOREMAP) #include +/* + * Arch code can implement the following two special hooks when using GENERIC_IOREMAP + * ioremap_allowed() return an int, + * - 0 means continue to remap + * - error code means skip remap and return directly + * iounmap_allowed() return an int, + * - 0 means continue to vunmap + * - error code means skip vunmap and return directly + */ +#ifndef ioremap_allowed +#define ioremap_allowed ioremap_allowed +static inline int ioremap_allowed(phys_addr_t phys_addr, size_t size, unsigned long prot) +{ + return 0; +} +#endif + +#ifndef iounmap_allowed +#define iounmap_allowed iounmap_allowed +static inline int iounmap_allowed(void __iomem *addr) +{ + return 0; +} +#endif + void __iomem *ioremap_prot(phys_addr_t phys_addr, size_t size, unsigned long prot); void iounmap(volatile void __iomem *addr); diff --git a/mm/ioremap.c b/mm/ioremap.c index 7cb9996b0c12..196c93c0beb8 100644 --- a/mm/ioremap.c +++ b/mm/ioremap.c @@ -27,8 +27,10 @@ void __iomem *ioremap_prot(phys_addr_t phys_addr, size_t size, unsigned long pro phys_addr -= offset; size = PAGE_ALIGN(size + offset); - area = get_vm_area_caller(size, VM_IOREMAP, - __builtin_return_address(0)); + if (ioremap_allowed(phys_addr, size, prot)) + return NULL; + + area = get_vm_area_caller(size, VM_IOREMAP, __builtin_return_address(0)); if (!area) return NULL; vaddr = (unsigned long)area->addr; @@ -45,6 +47,11 @@ EXPORT_SYMBOL(ioremap_prot); void iounmap(volatile void __iomem *addr) { - vunmap((void *)((unsigned long)addr & PAGE_MASK)); + void __iomem *vaddr = (void __iomem *)((unsigned long)addr & PAGE_MASK); + + if (iounmap_allowed(vaddr)) + return; + + vunmap((void __force *)vaddr); } EXPORT_SYMBOL(iounmap);