From patchwork Thu Sep 29 02:12:26 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Huacai Chen X-Patchwork-Id: 12993391 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 D3B62C32771 for ; Thu, 29 Sep 2022 02:13:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C93688D0001; Wed, 28 Sep 2022 22:13:46 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C425F6B0074; Wed, 28 Sep 2022 22:13:46 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B095B8D0001; Wed, 28 Sep 2022 22:13:46 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 9E8446B0073 for ; Wed, 28 Sep 2022 22:13:46 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 62705C0C7A for ; Thu, 29 Sep 2022 02:13:46 +0000 (UTC) X-FDA: 79963502052.08.2131416 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by imf13.hostedemail.com (Postfix) with ESMTP id 94C2B20006 for ; Thu, 29 Sep 2022 02:13:44 +0000 (UTC) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 850CAB821C1; Thu, 29 Sep 2022 02:13:42 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 38951C433D7; Thu, 29 Sep 2022 02:13:38 +0000 (UTC) From: Huacai Chen To: Arnd Bergmann , Huacai Chen Cc: loongarch@lists.linux.dev, linux-arch@vger.kernel.org, Xuefeng Li , Guo Ren , Xuerui Wang , Jiaxun Yang , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Huacai Chen , Weihao Li Subject: [PATCH] LoongArch: Support access filter to /dev/mem interface Date: Thu, 29 Sep 2022 10:12:26 +0800 Message-Id: <20220929021226.318152-1-chenhuacai@loongson.cn> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1664417624; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:references; bh=cANs4goyLf3qJEzp6G9qU0dSYncqavejzflBRM5v5pQ=; b=XJahVlPhTe3gXo5MrSAL4dq/17amByDhylM+0kf38mUui5BI48ItbI3b6/VMlvLX+EJay1 7f66zruOg4+l+wLVKRvxbzg1rFiObjVttsFt8lCXs3Xdnhila4tk+8puxn7yM0duhOrDnl DPd51JqRX9EcOrQDEHbWOPRVBTpLcDQ= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf13.hostedemail.com: domain of "SRS0=QehY=2A=loongson.cn=chenhuacai@kernel.org" designates 145.40.68.75 as permitted sender) smtp.mailfrom="SRS0=QehY=2A=loongson.cn=chenhuacai@kernel.org" ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1664417624; a=rsa-sha256; cv=none; b=MSsyp/kIvjMM6E0m3ndhP3hzm64IRZdKwZhb43kwmpjN3z/fFeJA7mifJAAWD5x8iqaPZG wH5MIApDx43nKbVAzWwNxeTqVPiB3aFbD/amo42+SsTBY6g7vzDaSe/ipoWoWEXXM8joC2 IoV4IXkJkEUCJ6RcFO0AxAf25RSIAmA= Authentication-Results: imf13.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf13.hostedemail.com: domain of "SRS0=QehY=2A=loongson.cn=chenhuacai@kernel.org" designates 145.40.68.75 as permitted sender) smtp.mailfrom="SRS0=QehY=2A=loongson.cn=chenhuacai@kernel.org" X-Rspam-User: X-Stat-Signature: 6i3o6g99s7jfwcc8w9ku671ps7dampsq X-Rspamd-Queue-Id: 94C2B20006 X-Rspamd-Server: rspam08 X-HE-Tag: 1664417624-391663 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: Accidental access to /dev/mem is obviously disastrous, but specific access can be used by people debugging the kernel. So select GENERIC_ LIB_DEVMEM_IS_ALLOWED, as well as define ARCH_HAS_VALID_PHYS_ADDR_RANGE and related helpers, to support access filter to /dev/mem interface. Signed-off-by: Weihao Li Signed-off-by: Huacai Chen --- arch/loongarch/Kconfig | 1 + arch/loongarch/include/asm/io.h | 4 ++++ arch/loongarch/mm/mmap.c | 28 ++++++++++++++++++++++++++++ 3 files changed, 33 insertions(+) diff --git a/arch/loongarch/Kconfig b/arch/loongarch/Kconfig index fcb5f9489ffd..9c36eb29096a 100644 --- a/arch/loongarch/Kconfig +++ b/arch/loongarch/Kconfig @@ -70,6 +70,7 @@ config LOONGARCH select GENERIC_LIB_CMPDI2 select GENERIC_LIB_LSHRDI3 select GENERIC_LIB_UCMPDI2 + select GENERIC_LIB_DEVMEM_IS_ALLOWED select GENERIC_PCI_IOMAP select GENERIC_SCHED_CLOCK select GENERIC_SMP_IDLE_THREAD diff --git a/arch/loongarch/include/asm/io.h b/arch/loongarch/include/asm/io.h index 999944ea1cea..398d1a7b3dd6 100644 --- a/arch/loongarch/include/asm/io.h +++ b/arch/loongarch/include/asm/io.h @@ -107,4 +107,8 @@ extern void __memcpy_fromio(void *to, const volatile void __iomem *from, size_t #include +#define ARCH_HAS_VALID_PHYS_ADDR_RANGE +extern int valid_phys_addr_range(phys_addr_t addr, size_t size); +extern int valid_mmap_phys_addr_range(unsigned long pfn, size_t size); + #endif /* _ASM_IO_H */ diff --git a/arch/loongarch/mm/mmap.c b/arch/loongarch/mm/mmap.c index 381a569635a9..71d45bdffc9e 100644 --- a/arch/loongarch/mm/mmap.c +++ b/arch/loongarch/mm/mmap.c @@ -3,6 +3,7 @@ * Copyright (C) 2020-2022 Loongson Technology Corporation Limited */ #include +#include #include #include @@ -116,3 +117,30 @@ int __virt_addr_valid(volatile void *kaddr) return pfn_valid(PFN_DOWN(PHYSADDR(kaddr))); } EXPORT_SYMBOL_GPL(__virt_addr_valid); + +/* + * You really shouldn't be using read() or write() on /dev/mem. This might go + * away in the future. + */ +int valid_phys_addr_range(phys_addr_t addr, size_t size) +{ + /* + * Check whether addr is covered by a memory region without the + * MEMBLOCK_NOMAP attribute, and whether that region covers the + * entire range. In theory, this could lead to false negatives + * if the range is covered by distinct but adjacent memory regions + * that only differ in other attributes. However, few of such + * attributes have been defined, and it is debatable whether it + * follows that /dev/mem read() calls should be able traverse + * such boundaries. + */ + return memblock_is_region_memory(addr, size) && memblock_is_map_memory(addr); +} + +/* + * Do not allow /dev/mem mappings beyond the supported physical range. + */ +int valid_mmap_phys_addr_range(unsigned long pfn, size_t size) +{ + return !(((pfn << PAGE_SHIFT) + size) & ~(GENMASK_ULL(cpu_pabits, 0))); +}