Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp1705129rwb; Thu, 29 Sep 2022 00:39:53 -0700 (PDT) X-Google-Smtp-Source: AMsMyM60yZZYUoHL6VofKObQBp/jnkCUhsyfsHCZwlNeb6kJkjC2KXX9/RlQBt9/E7NKyNVyQ1dx X-Received: by 2002:a05:6402:114a:b0:454:85e4:2295 with SMTP id g10-20020a056402114a00b0045485e42295mr1970984edw.348.1664437193145; Thu, 29 Sep 2022 00:39:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664437193; cv=none; d=google.com; s=arc-20160816; b=J/J9dGu0n5utdn4mYnWhN3/2Oh6H7QJlr/5tKwOEmB0oGOH76nEdw4nfQECUoLzai2 EcljX2rM3ZvlvCU1pPqxJfe7XGr325qLXZUNqps1cds/0jC4odNATIv9B3xLhD1/LaqD bz4rwO/oVq85lHApya2HFFDydvWdBoyQSyBzhLe1lYdgwWv4QdQT1AEUif55CgDm2WYA u5B7sNBXg2MAb8CKe/sg5gvFSCzuNQHaMraOKH8a15OW3LOu9D6z20GLnCU7vAHQ3bGE 5C5Y+2/xBGTsY/IQLulTHc7GGTdxGzvz7pZHxLT6N3l5L1POWwq8Xsvjh4PgsyxZP220 7aQQ== 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; bh=aBZgcQ3xRlSDTTZ5sPelJmMypjPDL0q+pOX01IqTY4w=; b=AA+UwiHj4fzat0VUhC0K+AWCt52QusMBqRXchZq5Ah52AzHGQ5VPwG8+ackV0wS37J ZeUivTYcAAQ8Ok1b4W+bOBu8tbbqzxrXgnVsqLGspqLO3G5F8BV0MbB6Ts9fBvAU/fIX 0YXWlqlYzdD5gJ40yc86C0HGexPIt7o0dF2Ke3Tz5qiCZwtJKGLRltpnPueO9u8OikFj Iu827vIweLdMxxAJUkeW0myTtFiAcQKOZrZa5/ofVa9OliL4utf2dSX6P+30t3qunKiE QrpnuIUmg8OGqdarXKGPU/RbHRgivMoRQ9RULYHyqNme/UWzZADbyvsTxv1lNhMwCtTP ITLQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id m3-20020a17090679c300b0072ed60fb78asi6971550ejo.548.2022.09.29.00.39.28; Thu, 29 Sep 2022 00:39:53 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235214AbiI2HYl (ORCPT + 99 others); Thu, 29 Sep 2022 03:24:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57072 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235334AbiI2HYO (ORCPT ); Thu, 29 Sep 2022 03:24:14 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0E927133CAF; Thu, 29 Sep 2022 00:23:53 -0700 (PDT) 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 C1A7FB82378; Thu, 29 Sep 2022 07:23:51 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 80465C433D6; Thu, 29 Sep 2022 07:23:47 +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 V2] LoongArch: Support access filter to /dev/mem interface Date: Thu, 29 Sep 2022 15:22:32 +0800 Message-Id: <20220929072232.562727-1-chenhuacai@loongson.cn> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-6.7 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_HI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- V2: Fix build warnings reported by kernel test robot. arch/loongarch/Kconfig | 1 + arch/loongarch/include/asm/io.h | 4 ++++ arch/loongarch/mm/mmap.c | 29 +++++++++++++++++++++++++++++ 3 files changed, 34 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..fbe1a4856fc4 100644 --- a/arch/loongarch/mm/mmap.c +++ b/arch/loongarch/mm/mmap.c @@ -3,6 +3,8 @@ * Copyright (C) 2020-2022 Loongson Technology Corporation Limited */ #include +#include +#include #include #include @@ -116,3 +118,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))); +} -- 2.31.1