From patchwork Wed May 8 19:19:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 1933169 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=IBqn1x4e; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.a=rsa-sha256 header.s=20230601 header.b=ITdXruC5; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:3::133; helo=bombadil.infradead.org; envelope-from=kvm-riscv-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=patchwork.ozlabs.org) Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4VZQ6q4KzHz20fc for ; Thu, 9 May 2024 05:23:47 +1000 (AEST) 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=Q6qcGyucrrhLCAZYh73B20Ar789dILrfYvwQVFsS2Hc=; b=IBqn1x4endzDxF XToZubcTlza8xbnacKRBTiPfs5afhySSubulbB5ZxR+cRDTepSfeFO0Z1Kpf42kDXnG8n9SKabB9M xtq0CPmXgB9/ngzfTTLNz+IkRYDtut4fSTBiqeBS+xoPEtE3FY8jmTlJ6r4GUpYUqhauMXBm9fGde 8GMC3JujIjdPGPwDWIEZIFsPnEXf4GAXYbcndbA5CB24QOxeQ6GmoBaZhhz1JS3STITkkW2XlBDEm eefc7/z/d4RWONo+azH2KwTRjXImaAq7AM61srA0f5cTRNvUWygD55g4inx8+vrHG6JGoHq0HnYjn 1AcRRk54pDCHmtjzTdAw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1s4mtG-0000000GiMg-0lE9; Wed, 08 May 2024 19:23:46 +0000 Received: from mail-wm1-x32a.google.com ([2a00:1450:4864:20::32a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1s4mtC-0000000GiK3-3acE for kvm-riscv@lists.infradead.org; Wed, 08 May 2024 19:23:44 +0000 Received: by mail-wm1-x32a.google.com with SMTP id 5b1f17b1804b1-41ebcf01013so855135e9.0 for ; Wed, 08 May 2024 12:23:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1715196220; x=1715801020; darn=lists.infradead.org; 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=99tlcIPBJBGOnw31Oe30kKMBjt8TwvO6BCavKhZeDaw=; b=ITdXruC5GguL6WoGtm/qK61hLx6XeqZXMfbp0mUEp6cxCSRTGWlJWcD5/EuWVhNNGc uhWECY47I/rK27q/Hb28q1u1rxJnYartBFTaJ2xgDf7dPT/6Q8aS7zTenkkZ1/QvhwDI +8rvR2O4mm5zVK0slgqHXrCQfWqDGWPRbhbvvtRnIsgNnuY0artGAFnEUI3e54eosuaf ubbcZDP179cjJPz/VG5BTib/TC0nL8MxR0lNXbMJUtRb9ZknxUvgQHhIbLeVg+KMpk4v /cf5h39f64vOnWnRRl9bRwWVwAoCz3AALyN5uk30AxlDwVfgPQdzBkC7UcUPrB+4dguy d1Rg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715196220; x=1715801020; 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=99tlcIPBJBGOnw31Oe30kKMBjt8TwvO6BCavKhZeDaw=; b=aXk3UXNONpl2xp/iJDXl72bfUnLgxmKT3JnS9VtgDTl+dfcbLF3fO1dyp0mo2H+3Mr nxSzWYxVe5cgUa92FPXSjwc/Z1zHaM0moRJk4SOyicY738Zd3O8hVbYtc8pYNrLRRB/k 2TXaZOYoVc2eGQNIYoVxmmDtDZJ2REjI08omljq8SE3ajSnA1MN/PqHHaOy69IeMX/j5 auq1j/5ZUC1KrDSwwPVinos+eBaOh9Gqd0/T6f9rXMZXZOsbg/TH8SW6fQaojspzIR9I P/0ENF4SqM7tsCgBxt/TFKH3o9OvQSBeUxnvgUYJ1OM1ALKxtgeggxH7hJrarDsjltds EjMw== X-Forwarded-Encrypted: i=1; AJvYcCUDRdcnU+0FF8SiorQ3awU9tU6+/qXHDfssekMv/2JiNryB77+eYNVzFGUdB3SXbPCUuBLlWaqwQ2EH2kvF+h0zqBfZrj71rfjejCpmiQ== X-Gm-Message-State: AOJu0Yx8KX4se/K0R7FrEUFBARkf+mL1tAzTvizceXY/8Xwl0MO5bNBE Fq8jGMd4BwPxQmLqNRXwtdrTPERQsOrZxpSeEBR99n0W1TIjSSXqUyeuNEX04EI= X-Google-Smtp-Source: AGHT+IFuqFkdx1GrRMX9VnSrFsQl595h33hB5u187da/mHSb1Bien42UkrHvFd9ITnGVH1r4SnsHfw== X-Received: by 2002:a05:600c:3ca9:b0:41b:ed36:e055 with SMTP id 5b1f17b1804b1-41fbcb4b4fbmr4761445e9.7.1715196220131; Wed, 08 May 2024 12:23:40 -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 5b1f17b1804b1-41f42e74625sm47255965e9.0.2024.05.08.12.23.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 May 2024 12:23:39 -0700 (PDT) From: Alexandre Ghiti To: Ryan Roberts , Catalin Marinas , Will Deacon , Alexander Potapenko , Marco Elver , Dmitry Vyukov , Paul Walmsley , Palmer Dabbelt , Albert Ou , Ard Biesheuvel , Anup Patel , Atish Patra , Andrey Ryabinin , Andrey Konovalov , Vincenzo Frascino , Andrew Morton , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, kasan-dev@googlegroups.com, linux-riscv@lists.infradead.org, linux-efi@vger.kernel.org, kvm@vger.kernel.org, kvm-riscv@lists.infradead.org, linux-mm@kvack.org Cc: Alexandre Ghiti Subject: [PATCH 04/12] mm, riscv, arm64: Use common ptep_get_lockless() function Date: Wed, 8 May 2024 21:19:23 +0200 Message-Id: <20240508191931.46060-5-alexghiti@rivosinc.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240508191931.46060-1-alexghiti@rivosinc.com> References: <20240508191931.46060-1-alexghiti@rivosinc.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240508_122342_973816_8BC98311 X-CRM114-Status: GOOD ( 20.64 ) X-Spam-Score: 0.0 (/) X-Spam-Report: Spam detection software, running on the system "bombadil.infradead.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: Make riscv use the contpte aware ptep_get_lockless() function from arm64. Signed-off-by: Alexandre Ghiti --- arch/arm64/include/asm/pgtable.h | 11 +---- arch/arm64/mm/contpte.c | 57 arch/riscv/include/asm/pgtable.h | 2 + i [...] Content analysis details: (0.0 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [2a00:1450:4864:20:0:0:0:32a listed in] [list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid X-BeenThere: kvm-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: "kvm-riscv" Errors-To: kvm-riscv-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org Make riscv use the contpte aware ptep_get_lockless() function from arm64. Signed-off-by: Alexandre Ghiti --- arch/arm64/include/asm/pgtable.h | 11 +---- arch/arm64/mm/contpte.c | 57 -------------------------- arch/riscv/include/asm/pgtable.h | 2 + include/linux/contpte.h | 1 + mm/contpte.c | 69 ++++++++++++++++++++++++++++++++ 5 files changed, 73 insertions(+), 67 deletions(-) diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgtable.h index e85b3a052a02..8a0603257436 100644 --- a/arch/arm64/include/asm/pgtable.h +++ b/arch/arm64/include/asm/pgtable.h @@ -1384,7 +1384,6 @@ extern void ptep_modify_prot_commit(struct vm_area_struct *vma, * where it is possible and makes sense to do so. The PTE_CONT bit is considered * a private implementation detail of the public ptep API (see below). */ -extern pte_t contpte_ptep_get_lockless(pte_t *orig_ptep); extern void contpte_clear_full_ptes(struct mm_struct *mm, unsigned long addr, pte_t *ptep, unsigned int nr, int full); extern pte_t contpte_get_and_clear_full_ptes(struct mm_struct *mm, @@ -1430,16 +1429,8 @@ static inline unsigned int pte_batch_hint(pte_t *ptep, pte_t pte) extern pte_t ptep_get(pte_t *ptep); #define ptep_get ptep_get +extern pte_t ptep_get_lockless(pte_t *ptep); #define ptep_get_lockless ptep_get_lockless -static inline pte_t ptep_get_lockless(pte_t *ptep) -{ - pte_t pte = __ptep_get(ptep); - - if (likely(!pte_valid_cont(pte))) - return pte; - - return contpte_ptep_get_lockless(ptep); -} static inline void set_pte(pte_t *ptep, pte_t pte) { diff --git a/arch/arm64/mm/contpte.c b/arch/arm64/mm/contpte.c index e225e458856e..5e9e40145085 100644 --- a/arch/arm64/mm/contpte.c +++ b/arch/arm64/mm/contpte.c @@ -28,63 +28,6 @@ static void contpte_try_unfold_partial(struct mm_struct *mm, unsigned long addr, } } -pte_t contpte_ptep_get_lockless(pte_t *orig_ptep) -{ - /* - * The ptep_get_lockless() API requires us to read and return *orig_ptep - * so that it is self-consistent, without the PTL held, so we may be - * racing with other threads modifying the pte. Usually a READ_ONCE() - * would suffice, but for the contpte case, we also need to gather the - * access and dirty bits from across all ptes in the contiguous block, - * and we can't read all of those neighbouring ptes atomically, so any - * contiguous range may be unfolded/modified/refolded under our feet. - * Therefore we ensure we read a _consistent_ contpte range by checking - * that all ptes in the range are valid and have CONT_PTE set, that all - * pfns are contiguous and that all pgprots are the same (ignoring - * access/dirty). If we find a pte that is not consistent, then we must - * be racing with an update so start again. If the target pte does not - * have CONT_PTE set then that is considered consistent on its own - * because it is not part of a contpte range. - */ - - pgprot_t orig_prot; - unsigned long pfn; - pte_t orig_pte; - pgprot_t prot; - pte_t *ptep; - pte_t pte; - int i; - -retry: - orig_pte = __ptep_get(orig_ptep); - - if (!pte_valid_cont(orig_pte)) - return orig_pte; - - orig_prot = pte_pgprot(pte_mkold(pte_mkclean(orig_pte))); - ptep = arch_contpte_align_down(orig_ptep); - pfn = pte_pfn(orig_pte) - (orig_ptep - ptep); - - for (i = 0; i < CONT_PTES; i++, ptep++, pfn++) { - pte = __ptep_get(ptep); - prot = pte_pgprot(pte_mkold(pte_mkclean(pte))); - - if (!pte_valid_cont(pte) || - pte_pfn(pte) != pfn || - pgprot_val(prot) != pgprot_val(orig_prot)) - goto retry; - - if (pte_dirty(pte)) - orig_pte = pte_mkdirty(orig_pte); - - if (pte_young(pte)) - orig_pte = pte_mkyoung(orig_pte); - } - - return orig_pte; -} -EXPORT_SYMBOL_GPL(contpte_ptep_get_lockless); - void contpte_clear_full_ptes(struct mm_struct *mm, unsigned long addr, pte_t *ptep, unsigned int nr, int full) { diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pgtable.h index ebfe6b16529e..62cad1b974f1 100644 --- a/arch/riscv/include/asm/pgtable.h +++ b/arch/riscv/include/asm/pgtable.h @@ -785,6 +785,8 @@ static inline int ptep_clear_flush_young(struct vm_area_struct *vma, extern pte_t ptep_get(pte_t *ptep); #define ptep_get ptep_get +extern pte_t ptep_get_lockless(pte_t *ptep); +#define ptep_get_lockless ptep_get_lockless extern void set_ptes(struct mm_struct *mm, unsigned long addr, pte_t *ptep, pte_t pteval, unsigned int nr); #define set_ptes set_ptes diff --git a/include/linux/contpte.h b/include/linux/contpte.h index 54d10204e9af..01da4bfc3af6 100644 --- a/include/linux/contpte.h +++ b/include/linux/contpte.h @@ -8,6 +8,7 @@ * a private implementation detail of the public ptep API (see below). */ pte_t contpte_ptep_get(pte_t *ptep, pte_t orig_pte); +pte_t contpte_ptep_get_lockless(pte_t *orig_ptep); void __contpte_try_fold(struct mm_struct *mm, unsigned long addr, pte_t *ptep, pte_t pte); void contpte_try_fold(struct mm_struct *mm, unsigned long addr, diff --git a/mm/contpte.c b/mm/contpte.c index 566745d7842f..060e0bc1a2a3 100644 --- a/mm/contpte.c +++ b/mm/contpte.c @@ -42,6 +42,7 @@ * - huge_ptep_clear_flush() * - ptep_get() * - set_ptes() + * - ptep_get_lockless() */ pte_t huge_ptep_get(pte_t *ptep) @@ -589,4 +590,72 @@ __always_inline void set_ptes(struct mm_struct *mm, unsigned long addr, contpte_set_ptes(mm, addr, ptep, pte, nr); } } + +pte_t contpte_ptep_get_lockless(pte_t *orig_ptep) +{ + /* + * The ptep_get_lockless() API requires us to read and return *orig_ptep + * so that it is self-consistent, without the PTL held, so we may be + * racing with other threads modifying the pte. Usually a READ_ONCE() + * would suffice, but for the contpte case, we also need to gather the + * access and dirty bits from across all ptes in the contiguous block, + * and we can't read all of those neighbouring ptes atomically, so any + * contiguous range may be unfolded/modified/refolded under our feet. + * Therefore we ensure we read a _consistent_ contpte range by checking + * that all ptes in the range are valid and have CONT_PTE set, that all + * pfns are contiguous and that all pgprots are the same (ignoring + * access/dirty). If we find a pte that is not consistent, then we must + * be racing with an update so start again. If the target pte does not + * have CONT_PTE set then that is considered consistent on its own + * because it is not part of a contpte range. + */ + + pgprot_t orig_prot; + unsigned long pfn; + pte_t orig_pte; + pgprot_t prot; + pte_t *ptep; + pte_t pte; + int i, ncontig; + +retry: + orig_pte = __ptep_get(orig_ptep); + + if (!pte_valid_cont(orig_pte)) + return orig_pte; + + orig_prot = pte_pgprot(pte_mkold(pte_mkclean(orig_pte))); + ptep = arch_contpte_align_down(orig_ptep); + ncontig = arch_contpte_get_num_contig(NULL, 0, ptep, 0, NULL); + pfn = pte_pfn(orig_pte) - (orig_ptep - ptep); + + for (i = 0; i < ncontig; i++, ptep++, pfn++) { + pte = __ptep_get(ptep); + prot = pte_pgprot(pte_mkold(pte_mkclean(pte))); + + if (!pte_valid_cont(pte) || + pte_pfn(pte) != pfn || + pgprot_val(prot) != pgprot_val(orig_prot)) + goto retry; + + if (pte_dirty(pte)) + orig_pte = pte_mkdirty(orig_pte); + + if (pte_young(pte)) + orig_pte = pte_mkyoung(orig_pte); + } + + return orig_pte; +} +EXPORT_SYMBOL_GPL(contpte_ptep_get_lockless); + +__always_inline pte_t ptep_get_lockless(pte_t *ptep) +{ + pte_t pte = __ptep_get(ptep); + + if (likely(!pte_valid_cont(pte))) + return pte; + + return contpte_ptep_get_lockless(ptep); +} #endif /* CONFIG_THP_CONTPTE */