Received: by 2002:a05:6358:489b:b0:bb:da1:e618 with SMTP id x27csp1054360rwn; Thu, 8 Sep 2022 12:38:21 -0700 (PDT) X-Google-Smtp-Source: AA6agR57z6rm8ePwC6XcISVXp+5gKUp6fgnKqdwfMoqhhLg4cNvtL/aQuo6IWhxtCaNjaEqhWcYv X-Received: by 2002:a17:902:7e06:b0:176:d7b3:137 with SMTP id b6-20020a1709027e0600b00176d7b30137mr9994304plm.31.1662665901357; Thu, 08 Sep 2022 12:38:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662665901; cv=none; d=google.com; s=arc-20160816; b=FgerxHQHZOFp3cAIEDPLgYv92jIV8DMplnqgWxyYcpvxDn7hDJTG8JFRqj8B5yJ465 T5sDOrhv67FtYKS9UBVKhQpxroRiga2NGQZ8tIz2UTkChqqA+rBmp6pcV7/BMGnRDBrG P1l6+OPImdb6u4t482ZfFtRJ7pUIJCieTA3ziRCkXx0GzHZTI+SAJ1qw2U0aQBBpATEW zWNoeEi9iQ8yMLTQwpAnhbaUUjjPdlAmN/B3ThSIWN1LZVaJnaZnpfXdumxgwA4RyjKC nvY8tOpaXeEdV2hjB4qYlJMFLwj2LQmTuqWkYL7sQgDkgnMzM8OG2xuvgrqk9ldUnL3u 6SvA== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=uAG7ulv3u8Y25koSZZsiniMQVBTMhkhOscGn2ifRLkY=; b=KCaKzeal2MajBX99FVRllvzhI7e2j0ou/3J8j1SlEN1P3JGHfr/Sm6ylGxHegBTCKP cVmjcFc4fb0Cs9vDWQ08u8PNx3ZStfe3Gn/jZ7w3yu/8FkSNXpq/1UGr1+MqFO3f8NeO 8qfGJWoO6pN5HIGLbGhr8fUet+iQTIc5wMyhM4xNkhppie/3EvVHJuCCwEekSTftJeQQ bAnVXaDILUBN+chwWZschurLVOaTS5OeJqne1OEJ0hJGPPeULssjXpxl2bbuZRpgV1Ia T+La2K2fCWRPXOCIaTx/oW2Zn9w9c6j/0YWpSijdGv4EjTvrLZ0PXj2tGzyNLn0x4zqd H5uA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rivosinc-com.20210112.gappssmtp.com header.s=20210112 header.b=8WoRFZkM; 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 q8-20020a170902dac800b00176d8e335desi8722717plx.184.2022.09.08.12.38.09; Thu, 08 Sep 2022 12:38:21 -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; dkim=pass header.i=@rivosinc-com.20210112.gappssmtp.com header.s=20210112 header.b=8WoRFZkM; 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 S232388AbiIHSu2 (ORCPT + 99 others); Thu, 8 Sep 2022 14:50:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33490 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232418AbiIHSuU (ORCPT ); Thu, 8 Sep 2022 14:50:20 -0400 Received: from mail-qt1-x835.google.com (mail-qt1-x835.google.com [IPv6:2607:f8b0:4864:20::835]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F0197EE512 for ; Thu, 8 Sep 2022 11:50:12 -0700 (PDT) Received: by mail-qt1-x835.google.com with SMTP id l5so13631035qtv.4 for ; Thu, 08 Sep 2022 11:50:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=uAG7ulv3u8Y25koSZZsiniMQVBTMhkhOscGn2ifRLkY=; b=8WoRFZkMD3Ij/8DnQt4Z7AsLmwPoBQy64SVQIrUpqVI+xI5Dl5BiIL0Bi3/ZrqQlxe Ka+9wW8bkGx0BRbTK1551bdLodCTt4Uh6JykjgnuQrr7WA1JSXjuMtGZOo4rMWPXncTH mmin5qOTQVgJI77mZUg8qPQsbiyT6JNe5yicOqFc2ffckMiNVfozy1CSZNV9+VueuFjE YoqgkapQAWXqTkIIJ+e4c609h52nE4RPgrYdzdndBlDp7rJPheFY9VO/z04NnuBaV69S Id0p0PT7FfU9P2kq6uuv2zWGoO/dJ0g0pSOsKXgd/00n/OsyRU79xqiAIy9VUtRbk1GD s90A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=uAG7ulv3u8Y25koSZZsiniMQVBTMhkhOscGn2ifRLkY=; b=IEkuSP9BL1+1om8Fk6G7ddhPEJbu6PZZ+XuTIyyJBfMyI+/qUmYPGFVUzXXi5CsJVn +VDDVX+bOzcPVTin7HPGZTQS6OwPqPcWYFLOdXEuYhqdKWBL0GT+SepFUPVOsS5i7bw0 hJizI263MnOJ6F7ul0kxTprRz6GRIiqjUD8h8LkHvlDUpQNTIAHZUj46jI92yjQBJtB4 k+VTnW5LeFSStLD6n7avzo7nLawSxyFezRcnDpsXqwuqCLs5cse0FlMw9EiAn7NSz+xZ sbe7b1rCdKFKKSwPn8qlgF97NVsLZlVA2qCmxaL/mUk776BLFD/Zj4I95GhXly8VixcY 0U1w== X-Gm-Message-State: ACgBeo3zl/vQ/19hiAgkOJc7tfD09itESA6VF3exWhhioKcATe7dB6px K2XIY+xlwUbAX+SPCphh8VnG7Q== X-Received: by 2002:a05:622a:11c6:b0:344:54b8:6382 with SMTP id n6-20020a05622a11c600b0034454b86382mr8762938qtk.571.1662663011004; Thu, 08 Sep 2022 11:50:11 -0700 (PDT) Received: from abrestic-xps.ba.rivosinc.com (pool-71-105-112-35.nycmny.fios.verizon.net. [71.105.112.35]) by smtp.gmail.com with ESMTPSA id z4-20020ac84544000000b00343057845f7sm14304453qtn.20.2022.09.08.11.50.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Sep 2022 11:50:10 -0700 (PDT) From: Andrew Bresticker To: Palmer Dabbelt Cc: Paul Walmsley , Atish Patra , Celeste Liu , dram , Ruizhe Pan , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Andrew Bresticker Subject: [PATCH v2] riscv: Make mmap() with PROT_WRITE imply PROT_READ Date: Thu, 8 Sep 2022 14:50:06 -0400 Message-Id: <20220908185006.1212126-1-abrestic@rivosinc.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220908170133.1159747-1-abrestic@rivosinc.com> References: <20220908170133.1159747-1-abrestic@rivosinc.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE 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 Commit 2139619bcad7 ("riscv: mmap with PROT_WRITE but no PROT_READ is invalid") made mmap() return EINVAL if PROT_WRITE was set wihtout PROT_READ with the justification that a write-only PTE is considered a reserved PTE permission bit pattern in the privileged spec. This check is unnecessary since RISC-V defines its protection_map such that PROT_WRITE maps to the same PTE permissions as PROT_WRITE|PROT_READ, and it is inconsistent with other architectures that don't support write-only PTEs, creating a potential software portability issue. Just remove the check altogether and let PROT_WRITE imply PROT_READ as is the case on other architectures. Note that this also allows PROT_WRITE|PROT_EXEC mappings which were disallowed prior to the aforementioned commit; PROT_READ is implied in such mappings as well. Fixes: 2139619bcad7 ("riscv: mmap with PROT_WRITE but no PROT_READ is invalid") Signed-off-by: Andrew Bresticker --- v1 -> v2: Update access_error() to account for write-implies-read --- arch/riscv/kernel/sys_riscv.c | 3 --- arch/riscv/mm/fault.c | 3 ++- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/arch/riscv/kernel/sys_riscv.c b/arch/riscv/kernel/sys_riscv.c index 571556bb9261..5d3f2fbeb33c 100644 --- a/arch/riscv/kernel/sys_riscv.c +++ b/arch/riscv/kernel/sys_riscv.c @@ -18,9 +18,6 @@ static long riscv_sys_mmap(unsigned long addr, unsigned long len, if (unlikely(offset & (~PAGE_MASK >> page_shift_offset))) return -EINVAL; - if (unlikely((prot & PROT_WRITE) && !(prot & PROT_READ))) - return -EINVAL; - return ksys_mmap_pgoff(addr, len, prot, flags, fd, offset >> (PAGE_SHIFT - page_shift_offset)); } diff --git a/arch/riscv/mm/fault.c b/arch/riscv/mm/fault.c index f2fbd1400b7c..d86f7cebd4a7 100644 --- a/arch/riscv/mm/fault.c +++ b/arch/riscv/mm/fault.c @@ -184,7 +184,8 @@ static inline bool access_error(unsigned long cause, struct vm_area_struct *vma) } break; case EXC_LOAD_PAGE_FAULT: - if (!(vma->vm_flags & VM_READ)) { + /* Write implies read */ + if (!(vma->vm_flags & (VM_READ | VM_WRITE))) { return true; } break; -- 2.25.1