Received: by 2002:ab2:6991:0:b0:1f7:f6c3:9cb1 with SMTP id v17csp428989lqo; Wed, 8 May 2024 04:24:57 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXstwmElGVyX0siclP3U/DbfCQg8tzq7pcyPnp46tapKwYGttj4fD84bWfgHz7a+CN4zlLb5l4feBUeENhwXTca1QnSYvPsoxwGEJFR9g== X-Google-Smtp-Source: AGHT+IGsSg9RUis6n5Bkj76QJq7gMRVF1z4qgjuUVA2u4jeNjdOpMvdT1uqsdV68BywKnCE2ycBv X-Received: by 2002:a05:6214:519d:b0:6a0:d548:ce5 with SMTP id 6a1803df08f44-6a1514dc96cmr26782756d6.53.1715167496216; Wed, 08 May 2024 04:24:56 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715167496; cv=pass; d=google.com; s=arc-20160816; b=kqOCd389/NVrZbXhOxc3yX0ingN8i6CiC32zDZV9PZVTZpVP9iYeLl7wmF4VLOcC9y SI+crE5PjPAeb86zNlKrTh4OYCSHBrASCis+mcJ7R9jUKtxFM0pTXKlsyFtzg57iaciI Lyah/FbDp721GZYGUcUDvVuP+gVNZObc2Vn/YQRdXDx2US8b4kzxeL51bf00gvsZZSRM UutMoKNUpuGnqcbdQK+MZBmCp8Fz8uzatnUKeszWzGd8KNP2GC/CSYpW9tK9DBKTNguW e6qMM0dDW5EkDXDzJuCIhpx9+naZdBCPa/fkObB6OCmRDSv/f/Bquwf+vpwq7/12321D GbvQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=k7TbOelFCBPjD//aDEZ907vn9YCYdQxapVfhTuFqwGo=; fh=LiVX4wKQv7zOh7ct8JBqGK1DbuhMX+F5iCQqcZhg0+U=; b=Md81a6EGQXZR/qJY0DNRoxaiHj2sQz4gAfjhFPt/YvK27OwpZgnbtd8wP8ohEnRItk H/4fg7suXGBsZfDeXpEKEhMFT21lxOcaC+sPolJK/GHZTF59trhlrWphldgq17rlAC4b OUxgD3JsdvY6mp12JeREGoTxqUS7EA596b3PR0npbFs5F277fP/zVuPcaFfbMFJRHIro T6sQ9wnylNPGJWoOgsP72TNV2JXq2r53GEj3LeSqD4Km8OkDMfGbL2eRTizydcMhKv8F Py8anVWZCtfGj0Wxh3GrRgMQa10Bk0d7iDEtJfMXA5FxI2Rx3jeVBO2XY95GObIO+Q8r yWnw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=dDOFrl+K; arc=pass (i=1 spf=pass spfdomain=rivosinc.com dkim=pass dkdomain=rivosinc-com.20230601.gappssmtp.com); spf=pass (google.com: domain of linux-kernel+bounces-173131-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-173131-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id ey7-20020a0562140b6700b006a0d6a71d31si14157252qvb.139.2024.05.08.04.24.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 May 2024 04:24:56 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-173131-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=dDOFrl+K; arc=pass (i=1 spf=pass spfdomain=rivosinc.com dkim=pass dkdomain=rivosinc-com.20230601.gappssmtp.com); spf=pass (google.com: domain of linux-kernel+bounces-173131-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-173131-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id DE2F31C2086F for ; Wed, 8 May 2024 11:24:55 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A154981AD7; Wed, 8 May 2024 11:24:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b="dDOFrl+K" Received: from mail-wr1-f46.google.com (mail-wr1-f46.google.com [209.85.221.46]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B5D954C61F for ; Wed, 8 May 2024 11:24:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.46 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715167487; cv=none; b=A8OK4fGWBKGRzReJybim3d9D5t8xQc9tvZ/0BGkSySTVKzkxE8cqkUqChbNhgHYId4YB+xMS/vEgIH4H9F7iYWLzufrL1MSeVEKYC9N4v6E67Jmb3G0rkXuevSjvf2TXyrNLNCs0cZ1R203lZa3QMxB/45kgUGnr9LkZeel2Alc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715167487; c=relaxed/simple; bh=VABY+Ba0JMwODv7U/E8RCXCu+cNtq0sU3lmtzPLVeMc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=JXZx9zsxCtz7HLkLmvNY8aUo9LrgwHiUo9icIHSjEmndyJQuUI5cFHRXwj/CQ6rokx4b0xtiOB9UoPDgPGdYlwolv6amtVEMVpdTbFKLcB77dFu6NdyYlvh5cDDwsRRxP1YTLvDaf2M0ZlNbcNEVtSyDfSofIxTUTjY4s4z4hxw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com; spf=pass smtp.mailfrom=rivosinc.com; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b=dDOFrl+K; arc=none smtp.client-ip=209.85.221.46 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rivosinc.com Received: by mail-wr1-f46.google.com with SMTP id ffacd0b85a97d-34e0d8b737eso429966f8f.1 for ; Wed, 08 May 2024 04:24:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1715167484; x=1715772284; darn=vger.kernel.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=k7TbOelFCBPjD//aDEZ907vn9YCYdQxapVfhTuFqwGo=; b=dDOFrl+KtxyDvi3pnN6m+o8CSoO9AHR6OezIezR0N+v1dcZeUhIY7yRl/DUS++53UP q1RYmmEeL/vnnqt4XEh8eqdwDQjNf99z3QLBkfJIaiq23fj5ovFt4BRxmcj1TLc3Dlvs 8RwzGjlNQA4hLTsgFYTRK2AnBm1DdgzDQLPPd4pAIGZyk5Ffrsq/coaZaMp4DiL5x76r LYS/Ms1nYIxS53SUrVBHvaXdAa4QVUbsIAre8gsVk3FYRgAlt0/GMHZJUR3PpAw3EQ8F PuNAytbHDTsEo6tA6iEvYIYduEEyVVwu5W/xnDPHyXUBZlVLOJo7KnX/AL2KcERzBm7e ZZ7g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715167484; x=1715772284; 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=k7TbOelFCBPjD//aDEZ907vn9YCYdQxapVfhTuFqwGo=; b=uou6KV7M2Tx1Bp1y4yiHO32hf6qBMlH8yrH8dzU0w72ihjbNrFW2DeFdPPOhb3OQjo PEgOEwtrZFtUOBylZMXgunZYO7638kZvGpMDVh1UpFmKvDGVsH50zyS0ivWHMsa0kKzf ex+hd/kcBZj3H97MdxjyyLWp+kw2i7qcVuCqA+rJgGeIcEGJ0hUz0dzXrL0+a/QChHgM bSgcxA/y/VqscSiPXeRA8CG6cvs62ZiqF6aN4GU5+tlVyvqfop3vbxca5LxeDZcWUU/2 GG2Gjx3rdMQRfJePr8J5fA8typZDzIlSRNUJ5FbezgENj4fo3VT18sNUoNtpFXTImGoY Vkuw== X-Forwarded-Encrypted: i=1; AJvYcCUSgq0BBxkHX1pkIbN3CwMpMMFVnMq1tYWAmg2sJ6TeAWcxXBtDnUF2P3iodYIsHdjhj9fEiu/bWMjPmLZwnXmd7QevxsTHYuwH3Caa X-Gm-Message-State: AOJu0YyPn3ibh4feGtS9OaFigttM4QPCpBzodrDAoFbgFKWP6bzauc5J U+T9d6IoGyFlHGKr593sdCVODxblpnxk6gIF8ww5U2Z297jsqTUJNeqlDLekwJ8= X-Received: by 2002:a5d:6d52:0:b0:34d:b3a0:5614 with SMTP id ffacd0b85a97d-34fca4326b6mr2190198f8f.8.1715167483962; Wed, 08 May 2024 04:24:43 -0700 (PDT) Received: from localhost.localdomain (amontpellier-656-1-456-62.w92-145.abo.wanadoo.fr. [92.145.124.62]) by smtp.gmail.com with ESMTPSA id dn15-20020a0560000c0f00b0034d9012e74bsm15257565wrb.72.2024.05.08.04.24.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 May 2024 04:24:43 -0700 (PDT) From: Alexandre Ghiti To: Catalin Marinas , Will Deacon , Ryan Roberts , Mark Rutland , Paul Walmsley , Palmer Dabbelt , Albert Ou , Andrew Morton , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org, linux-mm@kvack.org Cc: Alexandre Ghiti Subject: [PATCH v2 6/9] mm: Use common huge_ptep_get_and_clear() function for riscv/arm64 Date: Wed, 8 May 2024 13:18:26 +0200 Message-Id: <20240508111829.16891-7-alexghiti@rivosinc.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240508111829.16891-1-alexghiti@rivosinc.com> References: <20240508111829.16891-1-alexghiti@rivosinc.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit After some adjustments, both architectures have the same implementation so move it to the generic code. Note that get_clear_contig() function is duplicated in the generic and the arm64 code because it is still used by some arm64 functions that will, in the next commits, be moved to the generic code. Once all have been moved, the arm64 version will be removed. Signed-off-by: Alexandre Ghiti --- arch/arm64/include/asm/pgtable.h | 14 +++++++++- arch/arm64/mm/hugetlbpage.c | 19 ++----------- arch/riscv/include/asm/pgtable.h | 4 ++- arch/riscv/mm/hugetlbpage.c | 21 ++------------ mm/contpte.c | 48 ++++++++++++++++++++++++++++++-- 5 files changed, 66 insertions(+), 40 deletions(-) diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgtable.h index 2e0415fd5083..7c2938cb70b9 100644 --- a/arch/arm64/include/asm/pgtable.h +++ b/arch/arm64/include/asm/pgtable.h @@ -1644,11 +1644,23 @@ static inline int ptep_set_access_flags(struct vm_area_struct *vma, #endif /* CONFIG_ARM64_CONTPTE */ -static inline int arch_contpte_get_num_contig(pte_t *ptep, unsigned long size, +int find_num_contig(struct mm_struct *mm, unsigned long addr, + pte_t *ptep, size_t *pgsize); + +static inline int arch_contpte_get_num_contig(struct mm_struct *mm, + unsigned long addr, + pte_t *ptep, unsigned long size, size_t *pgsize) { int contig_ptes = 0; + /* + * If the size is not passed, we need to go through the page table to + * find out the number of contiguous ptes. + */ + if (size == 0) + return find_num_contig(mm, addr, ptep, pgsize); + *pgsize = size; switch (size) { diff --git a/arch/arm64/mm/hugetlbpage.c b/arch/arm64/mm/hugetlbpage.c index cf44837369be..5ace4bf7ce35 100644 --- a/arch/arm64/mm/hugetlbpage.c +++ b/arch/arm64/mm/hugetlbpage.c @@ -93,8 +93,8 @@ int pud_huge(pud_t pud) #endif } -static int find_num_contig(struct mm_struct *mm, unsigned long addr, - pte_t *ptep, size_t *pgsize) +int find_num_contig(struct mm_struct *mm, unsigned long addr, + pte_t *ptep, size_t *pgsize) { pgd_t *pgdp = pgd_offset(mm, addr); p4d_t *p4dp; @@ -277,21 +277,6 @@ pte_t arch_make_huge_pte(pte_t entry, unsigned int shift, vm_flags_t flags) return entry; } -pte_t huge_ptep_get_and_clear(struct mm_struct *mm, - unsigned long addr, pte_t *ptep) -{ - int ncontig; - size_t pgsize; - pte_t orig_pte = __ptep_get(ptep); - - if (!pte_cont(orig_pte)) - return __ptep_get_and_clear(mm, addr, ptep); - - ncontig = find_num_contig(mm, addr, ptep, &pgsize); - - return get_clear_contig(mm, addr, ptep, pgsize, ncontig); -} - /* * huge_ptep_set_access_flags will update access flags (dirty, accesssed) * and write permission. diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pgtable.h index 0847a7fb8661..d976113a370d 100644 --- a/arch/riscv/include/asm/pgtable.h +++ b/arch/riscv/include/asm/pgtable.h @@ -532,7 +532,9 @@ static inline void __set_pte_at(struct mm_struct *mm, pte_t *ptep, pte_t pteval) #define PFN_PTE_SHIFT _PAGE_PFN_SHIFT #ifdef CONFIG_RISCV_ISA_SVNAPOT -static inline int arch_contpte_get_num_contig(pte_t *ptep, unsigned long size, +static inline int arch_contpte_get_num_contig(struct mm_struct *mm, + unsigned long addr, + pte_t *ptep, unsigned long size, size_t *pgsize) { unsigned long hugepage_shift; diff --git a/arch/riscv/mm/hugetlbpage.c b/arch/riscv/mm/hugetlbpage.c index 437b1df059eb..a757e0b2f090 100644 --- a/arch/riscv/mm/hugetlbpage.c +++ b/arch/riscv/mm/hugetlbpage.c @@ -187,7 +187,7 @@ int huge_ptep_set_access_flags(struct vm_area_struct *vma, if (!pte_napot(pte)) return ptep_set_access_flags(vma, addr, ptep, pte, dirty); - pte_num = arch_contpte_get_num_contig(ptep, 0, &pgsize); + pte_num = arch_contpte_get_num_contig(vma->vm_mm, addr, ptep, 0, &pgsize); orig_pte = get_clear_contig_flush(mm, addr, ptep, pte_num); @@ -202,21 +202,6 @@ int huge_ptep_set_access_flags(struct vm_area_struct *vma, return true; } -pte_t huge_ptep_get_and_clear(struct mm_struct *mm, - unsigned long addr, - pte_t *ptep) -{ - pte_t orig_pte = ptep_get(ptep); - int pte_num; - - if (!pte_napot(orig_pte)) - return ptep_get_and_clear(mm, addr, ptep); - - pte_num = arch_contpte_get_num_contig(ptep, 0, NULL); - - return get_clear_contig(mm, addr, ptep, pte_num); -} - void huge_ptep_set_wrprotect(struct mm_struct *mm, unsigned long addr, pte_t *ptep) @@ -231,7 +216,7 @@ void huge_ptep_set_wrprotect(struct mm_struct *mm, return; } - pte_num = arch_contpte_get_num_contig(ptep, 0, &pgsize); + pte_num = arch_contpte_get_num_contig(mm, addr, ptep, 0, &pgsize); orig_pte = get_clear_contig_flush(mm, addr, ptep, pte_num); orig_pte = pte_wrprotect(orig_pte); @@ -249,7 +234,7 @@ pte_t huge_ptep_clear_flush(struct vm_area_struct *vma, if (!pte_napot(pte)) return ptep_clear_flush(vma, addr, ptep); - pte_num = arch_contpte_get_num_contig(ptep, 0, NULL); + pte_num = arch_contpte_get_num_contig(vma->vm_mm, addr, ptep, 0, NULL); return get_clear_contig_flush(vma->vm_mm, addr, ptep, pte_num); } diff --git a/mm/contpte.c b/mm/contpte.c index 22e0de197bd3..68eb1634b922 100644 --- a/mm/contpte.c +++ b/mm/contpte.c @@ -22,6 +22,7 @@ * - huge_ptep_get() * - set_huge_pte_at() * - huge_pte_clear() + * - huge_ptep_get_and_clear() */ pte_t huge_ptep_get(pte_t *ptep) @@ -33,7 +34,7 @@ pte_t huge_ptep_get(pte_t *ptep) if (!pte_present(orig_pte) || !pte_cont(orig_pte)) return orig_pte; - ncontig = arch_contpte_get_num_contig(ptep, + ncontig = arch_contpte_get_num_contig(NULL, 0, ptep, page_size(pte_page(orig_pte)), &pgsize); @@ -87,7 +88,7 @@ void set_huge_pte_at(struct mm_struct *mm, unsigned long addr, int i; int ncontig; - ncontig = arch_contpte_get_num_contig(ptep, sz, &pgsize); + ncontig = arch_contpte_get_num_contig(mm, addr, ptep, sz, &pgsize); if (!pte_present(pte)) { for (i = 0; i < ncontig; i++, ptep++, addr += pgsize) @@ -111,8 +112,49 @@ void huge_pte_clear(struct mm_struct *mm, unsigned long addr, int i, ncontig; size_t pgsize; - ncontig = arch_contpte_get_num_contig(ptep, sz, &pgsize); + ncontig = arch_contpte_get_num_contig(mm, addr, ptep, sz, &pgsize); for (i = 0; i < ncontig; i++, addr += pgsize, ptep++) __pte_clear(mm, addr, ptep); } + +static pte_t get_clear_contig(struct mm_struct *mm, + unsigned long addr, + pte_t *ptep, + unsigned long pgsize, + unsigned long ncontig) +{ + pte_t orig_pte = __ptep_get(ptep); + unsigned long i; + + for (i = 0; i < ncontig; i++, addr += pgsize, ptep++) { + pte_t pte = __ptep_get_and_clear(mm, addr, ptep); + + /* + * If HW_AFDBM (arm64) or svadu (riscv) is enabled, then the HW + * could turn on the dirty or accessed bit for any page in the + * set, so check them all. + */ + 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_ptep_get_and_clear(struct mm_struct *mm, + unsigned long addr, pte_t *ptep) +{ + int ncontig; + size_t pgsize; + pte_t orig_pte = __ptep_get(ptep); + + if (!pte_cont(orig_pte)) + return __ptep_get_and_clear(mm, addr, ptep); + + ncontig = arch_contpte_get_num_contig(mm, addr, ptep, 0, &pgsize); + + return get_clear_contig(mm, addr, ptep, pgsize, ncontig); +} -- 2.39.2