Received: by 2002:a05:7412:da14:b0:e2:908c:2ebd with SMTP id fe20csp255797rdb; Fri, 6 Oct 2023 02:29:50 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHzqLe3RrF3D2YfsSk4eUT5RoCMKIy2G+JzHMNe0bnvwsrzgflf2Eh5t83yVZJIN/vaSdnT X-Received: by 2002:a05:6a00:1346:b0:68f:e0f0:85f4 with SMTP id k6-20020a056a00134600b0068fe0f085f4mr8591715pfu.25.1696584590285; Fri, 06 Oct 2023 02:29:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696584590; cv=none; d=google.com; s=arc-20160816; b=W3tm32xYkzzQSOen/cSnVIrSXWJEbe8TsJJylGVxeL4WNOTfyMU4BKCYUdZFVXGy9j EzesWoKGwQOzOTYv4iDCJjoQ7iiWTh4LrGx4wxKndnPQ4yzE/nShiuyRtQ7Kkxsn9CUh vAdFuIL4BKeEd/uqjhBvNU3+zB4yo4bUbz9tjxEX5CF5sY+dgCHfZ+/lP0iCmQfuKVat tUy+A5RD1mC/Y3amdkpNZNNmd/YSmY0nYyXWYNy7NKMABUzDoZDyVl5cU0SbPPwrmmI1 2H12D6ocX+BdVDFveErEtUB2/Qpvfqi9OEkqn5rT8lO/JdXvUI0EIA0cii3UWPr94NTo vu4w== 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:dkim-signature; bh=Aq5rrYNyOOK7uZVK+/VCm6y827G0ymcilBxUQWM6O8Q=; fh=0YNXORI2f1g3k8QaEEzBwjs/kwDlkOiTK/vvbTQGF20=; b=VMCF7jxGC81Qaj7xeL9Xi5mVPluhwPvs0eL+FKAlC6YsuqaeO3kc1Ld3EPidSb2oq2 vw5RHLAFvGTgTMZYAG6gCLzAU8oSzzV7f/LQUOvpc9vWwTB46e4zkWuaOJr3NO52Hgsl VFG3q0D8nKOWFqPYxaZi7EwGldlgL3kEBmPkdu9nKw80vsim1XZKgI5/ae2i2cllhNJp XCR5upjk0FChvtSnMO1nT5VM+gYej39DrAWVIUi8wWdB0VoQFjsEdNNmjI8cUhKP80W/ EKJnrYGdZG+8hfkPU1ZrxgenCm+CedfLD3KTb3EIDMFo1sHMzDSi+T4ha+dpvswxQPzp 181Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=3EeYVaol; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from pete.vger.email (pete.vger.email. [2620:137:e000::3:6]) by mx.google.com with ESMTPS id i189-20020a639dc6000000b0056c4189d6cbsi1176573pgd.171.2023.10.06.02.29.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 06 Oct 2023 02:29:50 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) client-ip=2620:137:e000::3:6; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=3EeYVaol; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id 6C22C8451EDE; Fri, 6 Oct 2023 02:29:47 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231384AbjJFJ3i (ORCPT + 99 others); Fri, 6 Oct 2023 05:29:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37922 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231305AbjJFJ3f (ORCPT ); Fri, 6 Oct 2023 05:29:35 -0400 Received: from mail-wm1-x330.google.com (mail-wm1-x330.google.com [IPv6:2a00:1450:4864:20::330]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A3399AB for ; Fri, 6 Oct 2023 02:29:33 -0700 (PDT) Received: by mail-wm1-x330.google.com with SMTP id 5b1f17b1804b1-406618d0992so18360855e9.0 for ; Fri, 06 Oct 2023 02:29:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1696584572; x=1697189372; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=Aq5rrYNyOOK7uZVK+/VCm6y827G0ymcilBxUQWM6O8Q=; b=3EeYVaolCSpQ0fincTxRnb6HJGNOfJlZQiX3F/X0t1TwjXzZ6AlSzBq1wKCKhy8dN/ NaKGKYWe/PIHa6ytuUquuovrOP2HrQCtNcbZiNV9EboXBY/7qaMJ40RdHU2AprI5uJnx QyMIVPDRZx2fy0typ0URXw5yCm5zvNg0QJqPW6NwmSqwcrnbeDgQ2BHQwDGF/VGyW+pZ 4BGYUdIZqTB6A4uwffawcPCpyai4WtG0W2jb+fToMXs+ZpUa+RiW6CUgh9pnSxUt8KFf RY1TJt1ua0YpqzTiw43I0+6devoQDQTKUz5eJc30WJAi5W1C/eSjtVswOxq4WYLvUDGf HAlg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696584572; x=1697189372; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Aq5rrYNyOOK7uZVK+/VCm6y827G0ymcilBxUQWM6O8Q=; b=Lj+IiEAK8j8xkmr6eDXtnz8Cm5lhaqzI5QSV1hMjo+Rkt31MtUnPr8F/75kspFev1f z2wOBMLleLEZgPI+3wvjwG0POqZjRRXAx+DzmPbOLsLRheCEvYoIoo/Ovmmelj3YVp3J WdmL18iI9MLjRzmDJINN+HX2BepuGgB1nZKz/D8VkdxhUbrNhPnAG0fkk8qO3xjKMHr5 ZnOrvGHTqNnfJPeaxW6/22viXO4NYodAYVJ0ib1yLB7A4+Ev+0Hk67UMwZXMC+MeSZfu SAZkhG6P92WoIAVDGaw4FdhywNot5UcAuittChWx67+b5afAE6vd2cgm7EJdwz6OXd4T NT7Q== X-Gm-Message-State: AOJu0YxOJ8IDqCTHKwOf+16qqYxpdeziysPMvghoMojN42s9Zbs0+1wJ 94aXgsPcss+wa9P2ImBiPn8Pig== X-Received: by 2002:a05:600c:2189:b0:401:bf87:9898 with SMTP id e9-20020a05600c218900b00401bf879898mr6919651wme.25.1696584572008; Fri, 06 Oct 2023 02:29:32 -0700 (PDT) Received: from alex-rivos.ba.rivosinc.com (amontpellier-656-1-456-62.w92-145.abo.wanadoo.fr. [92.145.124.62]) by smtp.gmail.com with ESMTPSA id t14-20020a1c770e000000b004054dcbf92asm3308911wmi.20.2023.10.06.02.29.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 06 Oct 2023 02:29:31 -0700 (PDT) From: Alexandre Ghiti To: Paul Walmsley , Palmer Dabbelt , Albert Ou , Mike Rapoport , linux-mm@kvack.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Cc: Alexandre Ghiti Subject: [PATCH 0/2] riscv: Fix Fix set_memory_XX() and set_direct_map_XX() Date: Fri, 6 Oct 2023 11:29:28 +0200 Message-Id: <20231006092930.15850-1-alexghiti@rivosinc.com> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Fri, 06 Oct 2023 02:29:47 -0700 (PDT) Those 2 patches fix the set_memory_XX() and set_direct_map_XX() APIs, which in turn fix STRICT_KERNEL_RWX and memfd_secret(). Those were broken since the permission changes were not applied to the linear mapping because the linear mapping is mapped using hugepages and walk_page_range_novma() does not split such mappings. To fix that, patch 1 disables PGD mappings in the linear mapping as it is hard to propagate changes at this level in *all* the page tables, this has the downside of disabling PMD mapping for sv32 and PUD (1GB) mapping for sv39 in the linear mapping (for specific kernels, we could add a Kconfig to enable ARCH_HAS_SET_DIRECT_MAP and STRICT_KERNEL_RWX if needed, I'm pretty sure we'll discuss that). patch 2 implements the split of the huge linear mappings so that walk_page_range_novma() can properly apply the permissions. The whole split is protected with mmap_sem in write mode, but I'm wondering if that's enough, any opinion on that is appreciated. Alexandre Ghiti (2): riscv: Don't use PGD entries for the linear mapping riscv: Fix set_memory_XX() and set_direct_map_XX() by splitting huge linear mappings arch/riscv/mm/init.c | 12 +- arch/riscv/mm/pageattr.c | 263 ++++++++++++++++++++++++++++++++------- 2 files changed, 227 insertions(+), 48 deletions(-) -- 2.39.2