Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp196742ybt; Thu, 9 Jul 2020 19:48:02 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzvW52PvW/JOUkoW9YZaNJ5FD1wo8941G94oiey4UPKhWoYK4nOaIVLK1LOx3oInaOxBmfR X-Received: by 2002:a50:fc88:: with SMTP id f8mr44322593edq.314.1594349282673; Thu, 09 Jul 2020 19:48:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1594349282; cv=none; d=google.com; s=arc-20160816; b=UL6dDcbuCGrOmNsmxuwJzFqk73bqleZ3H8gwCjXG1EVWUynfc+flfBzOcG7S6iYOJb PO+9w/td1TE22P/pj+N21VjOMKpW/xtAGVg/o7+I+K9sYjIoVWtIA+VHR657+u1HL3g3 8I+8WMPCXGr6Bwnx+DrHJfU1Dl0N4QO30wnjHWIY6FnlBddUKuRSQMN77fOpEnCyC3rv TVFqsq9SmMQBHdiyTvwtOrgqCZ7qT6rnh4UjA1yvOYWADgy8mTFZTlM/XUV4RGTrExNh E83R9WHi2OJ1ELENuvvBSpVm0n4SY/2yC1uJjnF7L9FHxwxk+jKLxGZrFX8TEL/92mPU KauA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=2dy4AVqCcP7kztGsFeA5Fvb3GAiUWtChvdkBa1QUZbw=; b=DOMLo9SdQhhEb9ASE0M+oTCmJtJGlrYwNYK/zs/9/lqRqyxklxsBbZlhrJmspEPYNH Y0ucoUtRJRXFXSmQMkPRRF007hHMqPrzq0NoKuVn4QmHYOyV0D+/F8a4YXqDWjIXYZLp w46+PBD+bp0h9eE3f9RLeVW6IAU6QL0w66oedDnNEt24tR1aNurrRCHb7L7u/ZZf6T3U bO5+2r0PAFQCi5ErZQEerjhBX1jsNQY+W05vxvxq8/OWugjy9/8SxD9EhyGOW2tUC+3/ wjBU9f77KMNztFYcbOrIZGqf/e26jRsA7vKxnIOozECqnv0Ne/bSOYqjHPpnl8V8SXF9 jhIg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sifive.com header.s=google header.b=VJffGKFC; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id d4si3348223edv.535.2020.07.09.19.47.39; Thu, 09 Jul 2020 19:48:02 -0700 (PDT) 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=@sifive.com header.s=google header.b=VJffGKFC; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726925AbgGJCn4 (ORCPT + 99 others); Thu, 9 Jul 2020 22:43:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48008 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726496AbgGJCnz (ORCPT ); Thu, 9 Jul 2020 22:43:55 -0400 Received: from mail-ot1-x343.google.com (mail-ot1-x343.google.com [IPv6:2607:f8b0:4864:20::343]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 259CDC08C5CE for ; Thu, 9 Jul 2020 19:43:55 -0700 (PDT) Received: by mail-ot1-x343.google.com with SMTP id c25so3168150otf.7 for ; Thu, 09 Jul 2020 19:43:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=2dy4AVqCcP7kztGsFeA5Fvb3GAiUWtChvdkBa1QUZbw=; b=VJffGKFCkiCfIhl//sN2TFR4d7nLOgxwKcao79heBIkTO6Ld6tufLnLyu/FoWWwYLP IARF1L4e2re0mmDqsUzYo9R0GgCiNjOBnSeVCkjQgOdxfxpmxt5pg7Oi9bos4oxMR3wi 6xACx79JJkTqymJCNptZEVkRnBt7sRsUrErdSkBE2DLAv/rRMuJlZWWp+iF1aO3gC751 99d6CjUBXeORsfUbyEbLYv7/Biw7XQYW5iPEFpAckftmv48DZlzbp7GtVjdP6W07PbFY mmOtmGIQgAiv6mxic1OoV1ezpp4EkpaKQeOGbfnSA9nUiOg2yHJXyEkp796PqHCKvIsV fpsg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=2dy4AVqCcP7kztGsFeA5Fvb3GAiUWtChvdkBa1QUZbw=; b=GTAHipO5Frtb4Op/sddqJmB6SIdYmwqHyxUdZ5TWV9kyBBoB17Fcs7d4EV3dp//lli J1b187yFZf8o5opnXno6ydMt3WyMjRI7y3k9OB1BOsSLsTNvnGl0yVgi83uZydPndxcc f+zPlbGnHLSJCup8Yah6wQ3373gtjdMInwK/O/hEhI9h21yGSlH2gQhJlDn8I8TGaSKh rqXzuA1veY8rFLqdn1LBRWJQro6DzppxSfmqXAIVoK/ZLPDGNrsc1btG5KhFPgLsqK8B UF4drfCNYQmP02ju54SoGhCQQKxfxI1/bUYQbyJlONmNz8eSVgjtfU5vd6s4sQBZZHbS 2gRg== X-Gm-Message-State: AOAM531H52HMke4MsNZ+u/dxcUyfgaDxi0uPoTd+TnvNnGoDbCBNKily NLKioJPNTKMGR5/MLJwv9Rfb9haUaUk0qr5LjV7EKw== X-Received: by 2002:a9d:222:: with SMTP id 31mr34143429otb.127.1594349034129; Thu, 09 Jul 2020 19:43:54 -0700 (PDT) MIME-Version: 1.0 References: <7faa60aa4a606b5c5c1ae374d82a7eee6c764b38.1592292685.git.zong.li@sifive.com> In-Reply-To: From: Zong Li Date: Fri, 10 Jul 2020 10:43:42 +0800 Message-ID: Subject: Re: [PATCH 2/2] riscv: Support CONFIG_STRICT_DEVMEM To: Palmer Dabbelt Cc: Paul Walmsley , linux-riscv , "linux-kernel@vger.kernel.org List" Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Jul 10, 2020 at 4:08 AM Palmer Dabbelt wrote: > > On Tue, 16 Jun 2020 00:45:47 PDT (-0700), zong.li@sifive.com wrote: > > Implement the 'devmem_is_allowed()' interface for RISC-V, like some of > > other architectures have done. It will be called from range_is_allowed() > > when userpsace attempts to access /dev/mem. > > In fact, it's exactly the same (down to a few words of the comment) to the > others that I checked. I'm going to put a generic version in lib/ instead, > I've sent out the patches. > OK, no problem, thanks for improving it. > > Access to exclusive IOMEM and kernel RAM is denied unless > > CONFIG_STRICT_DEVMEM is set to 'n'. > > > > Test it by devmem, the result as follows: > > > > - CONFIG_STRICT_DEVMEM=y > > $ devmem 0x10010000 > > 0x00000000 > > $ devmem 0x80200000 > > 0x0000106F > > > > - CONFIG_STRICT_DEVMEM is not set > > $ devmem 0x10010000 > > devmem: mmap: Operation not permitted > > $ devmem 0x80200000 > > devmem: mmap: Operation not permitted > > > > Signed-off-by: Zong Li > > --- > > arch/riscv/Kconfig | 1 + > > arch/riscv/include/asm/io.h | 2 ++ > > arch/riscv/mm/init.c | 19 +++++++++++++++++++ > > 3 files changed, 22 insertions(+) > > > > diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig > > index 128192e14ff2..ffd7841ede4c 100644 > > --- a/arch/riscv/Kconfig > > +++ b/arch/riscv/Kconfig > > @@ -16,6 +16,7 @@ config RISCV > > select ARCH_HAS_BINFMT_FLAT > > select ARCH_HAS_DEBUG_VIRTUAL if MMU > > select ARCH_HAS_DEBUG_WX > > + select ARCH_HAS_DEVMEM_IS_ALLOWED > > select ARCH_HAS_GCOV_PROFILE_ALL > > select ARCH_HAS_GIGANTIC_PAGE > > select ARCH_HAS_MMIOWB > > diff --git a/arch/riscv/include/asm/io.h b/arch/riscv/include/asm/io.h > > index 3835c3295dc5..04ac65ab93ce 100644 > > --- a/arch/riscv/include/asm/io.h > > +++ b/arch/riscv/include/asm/io.h > > @@ -147,4 +147,6 @@ __io_writes_outs(outs, u64, q, __io_pbr(), __io_paw()) > > > > #include > > > > +extern int devmem_is_allowed(unsigned long pfn); > > + > > #endif /* _ASM_RISCV_IO_H */ > > diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c > > index bbe816e03b2f..5e7e61519acc 100644 > > --- a/arch/riscv/mm/init.c > > +++ b/arch/riscv/mm/init.c > > @@ -517,6 +517,25 @@ void mark_rodata_ro(void) > > } > > #endif > > > > +#ifdef CONFIG_STRICT_DEVMEM > > +#include > > +/* > > + * devmem_is_allowed() checks to see if /dev/mem access to a certain address > > + * is valid. The argument is a physical page number. > > + * > > + * Disallow access to system RAM as well as device-exclusive MMIO regions. > > + * This effectively disable read()/write() on /dev/mem. > > + */ > > +int devmem_is_allowed(unsigned long pfn) > > +{ > > + if (iomem_is_exclusive(pfn << PAGE_SHIFT)) > > + return 0; > > + if (!page_is_ram(pfn)) > > + return 1; > > + return 0; > > +} > > +#endif > > + > > void __init resource_init(void) > > { > > struct memblock_region *region;