From patchwork Fri Apr 28 12:01:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 13226298 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 6CD5CC77B61 for ; Fri, 28 Apr 2023 12:01:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-Id:Date:Subject:Cc :To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=riGd0WgNKNr3q3tXN6IK8+C7dVHq63ySQH0rs4nimFw=; b=eCSs68eRqXCjDY YkmftIwk5CdGxBq1uV9IKz9P0OkJCNX26BKMidDLaxT6oNctA91uMui0Ow54l4lRGXaisbBki6jqT rJejh/ITJMkKANxxGVtXUXMGQyhU1rmz4ao1FOrlpYCmymFqq0vpfVbjngEmWS95n9clGzb2Rz94Z Z3LE0YojryH3JAnipXNxa0VZSV1w8tzqJm1eLyR1WiP30e3zsgrg62qR1c3VCl+9gzcEfZwKxrW8U s9QDDHSgLNFx1arXb9ZZezbN5JkXQgv6Ivo2+iyc3SV++yN57jIsVeAfkpT8A3+KuRVobEbtVb4Rd 0BTOZ28Y8lmKY/EVDH7g==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1psMn8-009WUW-00; Fri, 28 Apr 2023 12:01:34 +0000 Received: from mail-wm1-x331.google.com ([2a00:1450:4864:20::331]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1psMn4-009WPt-2B for linux-riscv@lists.infradead.org; Fri, 28 Apr 2023 12:01:32 +0000 Received: by mail-wm1-x331.google.com with SMTP id 5b1f17b1804b1-3f19ab99540so73884805e9.2 for ; Fri, 28 Apr 2023 05:01:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1682683284; x=1685275284; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=ZPte/pKtqTO+AZafGTxmXrOl5pSZO0/CWxo2rC3YnZs=; b=D9Ug2iDIaCBa455YEXB0pLqOEYTMOM1r/7fk8K9lGH1dNTS8fVFclpVBPxfNrGJLOw KaQBHohnzVJrWOaRs8uJgZ9g9w/jVBvgyEEVfLlPH5frFMgPgjCB/dUCd5TpesPNHPp7 VrNuAtBR+oi1LVL8zOJEyZKH935LdKrl4d8GQ78+nxplASPonmGwAb8TeyBvZDvg4wdP DO14ek8TP3GH/KvuVxbY0GT3jdZg6pNDls+UXYFUL6++7KkNbAFKS9V5ET2WxDvAJm6M VPjNt1HQ8u+x7ZzEhAuhPDeinB+Hg/eyLHkOg8dW4EeLwzj0vv4xERJpi2a8H06ZcgoZ giQA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682683284; x=1685275284; 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=ZPte/pKtqTO+AZafGTxmXrOl5pSZO0/CWxo2rC3YnZs=; b=hjLs0W2M/EIcNhq5/46iMwmvXJMWYMLP85GO4+7zGEDR8E9/G5xdlALxiTQ8irg/UB sOfT+t0CBUa9Qi5Qv7H1WwKsJGlHj4g3GVIssEiKgUN/URemP7Ph+S37I1ngkfV1VtxQ 0AGaeOdcnKwKPvXpVSJ8wPv5Ka0b23TQnSabX7e44NQzh0BRp6nljj6expjMeudRSSJs 5RFojLNN/gk2qhq+fM7ita+/qz9aEMABH1nePeISjYARrUhDKma+I4qQA89AsV3tT2Sm 1yg4H41e4nfQy4tM3v/CZ2B4KuFl4DKYkcM3V+UNydxf3oXXKwFW/g8DlxkPzNGBfBP7 UHVg== X-Gm-Message-State: AC+VfDyhykFa2jA6VSlQWizMuq+5Y5VTAZIJjJfd7cney20Ode2rl2lZ sPThSVMX9Rbz9+6tj+g2dx0hlw== X-Google-Smtp-Source: ACHHUZ56NW1lU+tU1Ept9DHFEa6yhwXbLGLBDRLT8LZpAyx3okj6Vj4I+4BWrBpdsk1NON7Fn9nJ0A== X-Received: by 2002:a05:600c:2183:b0:3f1:76d7:ae2b with SMTP id e3-20020a05600c218300b003f176d7ae2bmr4023406wme.13.1682683283971; Fri, 28 Apr 2023 05:01:23 -0700 (PDT) Received: from alex-rivos.home (amontpellier-656-1-456-62.w92-145.abo.wanadoo.fr. [92.145.124.62]) by smtp.gmail.com with ESMTPSA id m18-20020a7bcb92000000b003f24f245f57sm15383740wmi.42.2023.04.28.05.01.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 Apr 2023 05:01:23 -0700 (PDT) From: Alexandre Ghiti To: Paul Walmsley , Palmer Dabbelt , Albert Ou , Qinglin Pan , Andrew Jones , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Cc: Alexandre Ghiti Subject: [PATCH -fixes 1/2] riscv: Fix huge_ptep_set_wrprotect when PTE is a NAPOT Date: Fri, 28 Apr 2023 14:01:19 +0200 Message-Id: <20230428120120.21620-1-alexghiti@rivosinc.com> X-Mailer: git-send-email 2.37.2 MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230428_050130_960272_175895B6 X-CRM114-Status: UNSURE ( 9.83 ) X-CRM114-Notice: Please train this message. X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org We need to avoid inconsistencies across the PTEs that form a NAPOT region, so when we write protect such a region, we should clear and flush all the PTEs to make sure that any of those PTEs is not cached which would result in such inconsistencies (arm64 does the same). Fixes: f2aeb0118ddd ("riscv: mm: support Svnapot in hugetlb page") Signed-off-by: Alexandre Ghiti Reviewed-by: Andrew Jones --- arch/riscv/mm/hugetlbpage.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/arch/riscv/mm/hugetlbpage.c b/arch/riscv/mm/hugetlbpage.c index a163a3e0f0d4..238d00bdac14 100644 --- a/arch/riscv/mm/hugetlbpage.c +++ b/arch/riscv/mm/hugetlbpage.c @@ -218,6 +218,7 @@ void huge_ptep_set_wrprotect(struct mm_struct *mm, { pte_t pte = ptep_get(ptep); unsigned long order; + pte_t orig_pte; int i, pte_num; if (!pte_napot(pte)) { @@ -228,9 +229,12 @@ void huge_ptep_set_wrprotect(struct mm_struct *mm, order = napot_cont_order(pte); pte_num = napot_pte_num(order); ptep = huge_pte_offset(mm, addr, napot_cont_size(order)); + orig_pte = get_clear_contig_flush(mm, addr, ptep, pte_num); + + orig_pte = pte_wrprotect(orig_pte); for (i = 0; i < pte_num; i++, addr += PAGE_SIZE, ptep++) - ptep_set_wrprotect(mm, addr, ptep); + set_pte_at(mm, addr, ptep, orig_pte); } pte_t huge_ptep_clear_flush(struct vm_area_struct *vma, From patchwork Fri Apr 28 12:01:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 13226299 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id E23F5C77B61 for ; Fri, 28 Apr 2023 12:02:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=cobBRV71fMPhUwPBN91/IXnVW1iOf1taRglut+gvUIY=; b=uFfrRLwRUIROB5 pqnEuRGnJWKgG3S4j96xpXi7IApMdxoLJCYboZ2tfJIMpFQc8fU8BXOezzlSNczXtbCE33clvInF1 8RU/sNqM+9kWqVuS0AcNlmhpMvIZiLAEAHbJ7+pnE4dFiw0bgbUslpgwHgPbiLYJrC1CG1jFe5Uti p36W3SaYgRD18AvnOLmZYuedSERkNRUCTMVlBFH/H+KbOilpYBOYt614iXAedta34gK85CSQ5owMs GsCSOeJzY9rPisA0ss1lTrQfbwVwD1UlRVf+GIqmmaegLoPeqT1wovGa6sZFkBiFDdVcyAkecRXSd yFBcaXpBD6DaVH+WKsMA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1psMo3-009X3W-0p; Fri, 28 Apr 2023 12:02:31 +0000 Received: from mail-wm1-x336.google.com ([2a00:1450:4864:20::336]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1psMo0-009Wzz-2r for linux-riscv@lists.infradead.org; Fri, 28 Apr 2023 12:02:30 +0000 Received: by mail-wm1-x336.google.com with SMTP id 5b1f17b1804b1-3f315712406so57950455e9.0 for ; Fri, 28 Apr 2023 05:02:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1682683345; x=1685275345; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=i+IhJdlbzPopAgcnIQcMyy0yuzqBnz1h8mDocxofWCY=; b=aLYgN7VlR46N9WcDi53goFnZv+vG6n3pLEhhSP9AnlnYDiiuuH7hkDMkrbFyl/q8IU QqyxBo3ttSlxPV99BPEA855FtbCjlLBq+2yUTvTQrQdE3zs6TkyppYM8+4iJm0cSlyTZ 4kJvdtC6YfTYisDIciOvBeSXMzj6JftKhpHLkM7+dbEncXDxKStkW/Yeosa1Ci8x2Xew TRKfi2Xbgzs5uPExZM9N2S626aiDKkYDfPfeLt0iG40V81YMQo2BzohjA180oQd3gl9r zn5JUkkEJIGXv7OzhafPO6q+n3QKY7b0LRP/qIzGwu7mBU9lh01fnfzJTPUA2ueO4c+s Tysw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682683345; x=1685275345; 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:message-id:reply-to; bh=i+IhJdlbzPopAgcnIQcMyy0yuzqBnz1h8mDocxofWCY=; b=hbdAZYhw3rjqudMe+CTieDjIbMiBxAgxCdGLdfRJTLdX6k4CPxnl5/WYZ/wpapPvG2 Kx3+kZ3jHSfmDGoudEjy5Xd24JPPJ/26TAyrw0GfNmoHSmpCzN+ClC0jeIiuAfls7uF6 RGvDfA1gF06akvVAkdFLqFgwPQQdKLFFvrXbpCCxwx4D2Kr2khGUJ5+OKYXVFvx0hd8T gDV0ZEYQPmPC3cuPNJtaC69SbjG42xT4N1e5Fghy/E08YxbI2ftv2zMnvdvXIsUweZzt bGQCmLHFKGfsA5GH3D2PCDHTF7eerLiVxMTCEDKlI5eba6/aZNWofINBlSyNVNMTl6pd y2Sw== X-Gm-Message-State: AC+VfDxvWZ0uyZ18aoykIZnEX9z6YbXrg9TgPFzPeH+7VvN+AUozagrw +3Pt+JDuyk3HnwC3e6lyQHOcXjnarrkHFKp3KH0= X-Google-Smtp-Source: ACHHUZ5VIm/9FvBnliMEeY0Op5aLkD+MKAJ4pG2os4QAW4erFcKj2zX2YCBI9BaqYR1JhwIUBX2zdw== X-Received: by 2002:a5d:68cb:0:b0:2f0:2c3c:b858 with SMTP id p11-20020a5d68cb000000b002f02c3cb858mr6046924wrw.31.1682683344886; Fri, 28 Apr 2023 05:02:24 -0700 (PDT) Received: from alex-rivos.home (amontpellier-656-1-456-62.w92-145.abo.wanadoo.fr. [92.145.124.62]) by smtp.gmail.com with ESMTPSA id x8-20020a05600c21c800b003f2390bdd0csm15487607wmj.32.2023.04.28.05.02.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 Apr 2023 05:02:24 -0700 (PDT) From: Alexandre Ghiti To: Paul Walmsley , Palmer Dabbelt , Albert Ou , Qinglin Pan , Andrew Jones , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Cc: Alexandre Ghiti Subject: [PATCH -fixes 2/2] riscv: Implement missing huge_ptep_get Date: Fri, 28 Apr 2023 14:01:20 +0200 Message-Id: <20230428120120.21620-2-alexghiti@rivosinc.com> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20230428120120.21620-1-alexghiti@rivosinc.com> References: <20230428120120.21620-1-alexghiti@rivosinc.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230428_050228_946061_3144196B X-CRM114-Status: GOOD ( 11.79 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org huge_ptep_get must be reimplemented in order to go through all the PTEs of a NAPOT region: this is needed because the HW can update the A/D bits of any of the PTE that constitutes the NAPOT region. Fixes: f2aeb0118ddd ("riscv: mm: support Svnapot in hugetlb page") Signed-off-by: Alexandre Ghiti Reviewed-by: Andrew Jones --- arch/riscv/include/asm/hugetlb.h | 3 +++ arch/riscv/mm/hugetlbpage.c | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/arch/riscv/include/asm/hugetlb.h b/arch/riscv/include/asm/hugetlb.h index fe6f23006641..ce1ebda1a49a 100644 --- a/arch/riscv/include/asm/hugetlb.h +++ b/arch/riscv/include/asm/hugetlb.h @@ -36,6 +36,9 @@ int huge_ptep_set_access_flags(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep, pte_t pte, int dirty); +#define __HAVE_ARCH_HUGE_PTEP_GET +pte_t huge_ptep_get(pte_t *ptep); + pte_t arch_make_huge_pte(pte_t entry, unsigned int shift, vm_flags_t flags); #define arch_make_huge_pte arch_make_huge_pte diff --git a/arch/riscv/mm/hugetlbpage.c b/arch/riscv/mm/hugetlbpage.c index 238d00bdac14..e0ef56dc57b9 100644 --- a/arch/riscv/mm/hugetlbpage.c +++ b/arch/riscv/mm/hugetlbpage.c @@ -3,6 +3,30 @@ #include #ifdef CONFIG_RISCV_ISA_SVNAPOT +pte_t huge_ptep_get(pte_t *ptep) +{ + unsigned long pte_num; + int i; + pte_t orig_pte = ptep_get(ptep); + + if (!pte_present(orig_pte) || !pte_napot(orig_pte)) + return orig_pte; + + pte_num = napot_pte_num(napot_cont_order(orig_pte)); + + for (i = 0; i < pte_num; i++, ptep++) { + pte_t pte = ptep_get(ptep); + + if (pte_dirty(pte)) + orig_pte = pte_mkdirty(orig_pte); + + if (pte_young(pte)) + orig_pte = pte_mkyoung(orig_pte); + } + + return orig_pte; +} + pte_t *huge_pte_alloc(struct mm_struct *mm, struct vm_area_struct *vma, unsigned long addr,