Received: by 2002:ab2:3141:0:b0:1ed:23cc:44d1 with SMTP id i1csp170555lqg; Fri, 1 Mar 2024 01:22:29 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVVtQnkK3Ws9SXuMU18Zq89ZP1jNYZWBzTo/2VCnlbTMV3Ps1VwLU5piotY/fz4iNrY131QB0U5a5l+LePLxtaeZwgSBQzQ7MwI8gIGHA== X-Google-Smtp-Source: AGHT+IGz+gFbn02qF3zJX9p3GtjnmQlHrMFhPcXdhsRBLy0XpaiDJbuXJNyXCi+PWr5nid65i6Ig X-Received: by 2002:a05:6830:20c8:b0:6e4:a125:2acf with SMTP id z8-20020a05683020c800b006e4a1252acfmr1183212otq.20.1709284949510; Fri, 01 Mar 2024 01:22:29 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709284949; cv=pass; d=google.com; s=arc-20160816; b=WgwQVmMJFQHWcrBYcks6ISfrTq7wrA2SsndLSjdtsW/XIXHcuLfm/Zhy5d7d0ipdFp 72A3ncjdRNGZwFsxy22GU2UD5YiIvX2cRcCueF0S+0kiYTr9kZTCoS0gAKAiIhNJBsBJ zbzeYowPWPEFe7cFR1H917WOkAoUCewwW8kptPpRmNBNJ82GMfwJLvgL/DfNKMKnqKLe qcW7hbGkPX+EvWS15vN3aMnrDCC18RmD0+sbJXrWdle+NKXXYw1qh1UDMzbeROGSGzCo DCmmXqw7/8baDrSxX7kxQtWCugWiWrX17+1tBM2KXK0o587WN5DikKEyDCSvIdCK0kiJ pOVw== 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=fHzJQXP5BEIeqfOJsZUcjaIxOTpiaXVWcZpI+r0iAkE=; fh=lBt7TwUn0SIa5Lkn3QMfwWSryhVwKCkV1k7SoQf8x2Q=; b=RppaHCQM9qWn5aV1l+4k5qo+OQRZ8UHrff7ZOg2c/qlUCwfssAHovP4UU3elbJl0+Q ZiFQYKZ8+dvHeG5BYDx7UGakRPmthaSq31csilKEtoFDCmmchNkfSHuzVTmEkBqAk3Lr Z4dgryDNrkSi7h8Yzaxu/1rWQgD1dRqdeH0GMLf/9isUzs0irop2uSyxIoiI+eNAN+bJ TA8A1uOXxQghUu74g4V4d2p3Yr2C2cKDVVOrO+BLFzAHihpYoy4CRobpbMAFMURS6dQv inNlfwCGARLItCpCoyqWWe7T37w21xVxtlqL4MtXqw/K/C+x5AaGz1E4MKxVdjCZ13MF yqaw==; 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=w8pbEbCM; 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-88167-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-88167-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id o7-20020a63e347000000b005e45abbc54asi3256079pgj.186.2024.03.01.01.22.29 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Mar 2024 01:22:29 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-88167-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=w8pbEbCM; 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-88167-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-88167-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 28CD2283AFA for ; Fri, 1 Mar 2024 09:22:29 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 136CA6A346; Fri, 1 Mar 2024 09:22:24 +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="w8pbEbCM" 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 2F2EA1E886 for ; Fri, 1 Mar 2024 09:22:20 +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=1709284943; cv=none; b=rUsiW9+9u1lM92mrVymojIIE5ZKzeMm28wJmtaNlMKwhOlOiyL3KcgSEOpB07niDNWqPF2Al3OIXZ3ajQ5t0/aIdXwIv9Fs/BL4fhuY4IAnU2ZgCs2qrqmChlxi13Sx3ZK2JQMV15ce2ySpTUjFBXUjUuGUcrwXo6cKjMg6fHFA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709284943; c=relaxed/simple; bh=ewaz0+30TL4hNr+v/Sd93KkOOVe261sfJ+PpKwEwyfg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=WwRgUOv1+IEc/4PFEXBSAuZbYR31O0g7uR1JW63nInLzjNEOJyNhgGnFsxJUhwt19rMKNC5leSIkDv08RvyyfB/ug7tWiTp05XO0rQ8UXsZDI0Y2YHSOJwMNkBhBaUrfilQMi3BkTOPPYkd0YPnbaLiHVuEmf0Zqx1om3oOnavc= 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=w8pbEbCM; 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-33e17fc5aceso535686f8f.0 for ; Fri, 01 Mar 2024 01:22:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1709284939; x=1709889739; 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=fHzJQXP5BEIeqfOJsZUcjaIxOTpiaXVWcZpI+r0iAkE=; b=w8pbEbCMWfcSnm2Ysd0YIf+XZOkFG9506GVk0bLQC9RueKznXYTf13pvdyeUM/GyRu umhWfLQvWuiesqLBMwcdx2nfkc4HXvyVZQfEXKiNXqCI6MD+MoQC/7cADCsbTisiuVuA H3gZCGCbe9EW/bcHox2HB9blbapN1SxBKFQXM09wJHJM7WAWkG5MURZR6gdxNJk5LNyy xrAhznTK7+zGrofTh+VGeq0qLJTawxrPOiJNmP14YfF4vZqW++NwT0VVkehu/KFCRKNW IyHRkm6EiTLbyK9/sm8svqG7rc6sMLb/pWIDSdFjF4QSNoqvuappKXDWnEHc6ImvoYFu GrMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709284939; x=1709889739; 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=fHzJQXP5BEIeqfOJsZUcjaIxOTpiaXVWcZpI+r0iAkE=; b=JFp98gXG4iKxXWFg9/4ff1QJV7+bW2tRTvWX6I3pdpOhr5HDebWk5GhF5DvM1pUvsd ZW0wCVqySvOf9hiDwaUI9iUi3ucMq6Jz/zZUmDmhHOivpVrmlFQrKJDKFalLyF8on+L0 MbHvDUtrHEniRryVLoJENZBlmbreytcQ3865z74OeoziwuqyA8S2MZ6wUPkfPPSKFBX9 OFcLs2sy/mZpd+mxQieJIWvNL/NJp0lKTx+1L/BJXq6METkG5o9uCf3vU1aYdWIz1UtT ZjNkGpwvfIGrkyPzc07IQE3HNI7ZX85528z5yBEqvIuiGUYlquNX0OAxUfTyx7ySN6nK T/Lw== X-Forwarded-Encrypted: i=1; AJvYcCXtHx41ziMl5ANVaTRZbohS0wP+0r2QQA43QrGZeiM3gs3dMPEbMJgUEIa+WMMbX8y1u8Xl1etJ4XH30ujOJz4AoQjYjp/AMxucICBC X-Gm-Message-State: AOJu0Yy6gNDPiE/7Hjzk5vlCO0Tc/wIVgbdSONUopSBsKOx6UtxpCRqe kA4225haHUgf0p77j/LZqMrClOClg4+Ed3WmLdPyZmL35FkoSm/EEHO670koZ3g= X-Received: by 2002:adf:f84c:0:b0:33d:39db:a0f8 with SMTP id d12-20020adff84c000000b0033d39dba0f8mr1044349wrq.7.1709284939471; Fri, 01 Mar 2024 01:22:19 -0800 (PST) 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 e13-20020a5d594d000000b0033e12b2e567sm3302518wri.35.2024.03.01.01.22.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Mar 2024 01:22:19 -0800 (PST) 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 7/9] mm: Use common huge_ptep_set_access_flags() function for riscv/arm64 Date: Fri, 1 Mar 2024 10:14:53 +0100 Message-Id: <20240301091455.246686-8-alexghiti@rivosinc.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240301091455.246686-1-alexghiti@rivosinc.com> References: <20240301091455.246686-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 Both architectures have almost the same implementation: __cont_access_flags_changed() is also correct on riscv and brings the same benefits (ie don't do anything if the flags are unchanged). As in the previous commit, get_clear_contig_flush() is duplicated in both the arch and the generic codes, it will be removed from the arch code when the last reference there gets moved to the generic code. Signed-off-by: Alexandre Ghiti --- arch/arm64/mm/hugetlbpage.c | 65 -------------------------------- arch/riscv/mm/hugetlbpage.c | 29 --------------- mm/contpte.c | 74 +++++++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 94 deletions(-) diff --git a/arch/arm64/mm/hugetlbpage.c b/arch/arm64/mm/hugetlbpage.c index a2d33cbc7da5..b8abbd5ace4f 100644 --- a/arch/arm64/mm/hugetlbpage.c +++ b/arch/arm64/mm/hugetlbpage.c @@ -284,71 +284,6 @@ pte_t arch_make_huge_pte(pte_t entry, unsigned int shift, vm_flags_t flags) return entry; } -/* - * huge_ptep_set_access_flags will update access flags (dirty, accesssed) - * and write permission. - * - * For a contiguous huge pte range we need to check whether or not write - * permission has to change only on the first pte in the set. Then for - * all the contiguous ptes we need to check whether or not there is a - * discrepancy between dirty or young. - */ -static int __cont_access_flags_changed(pte_t *ptep, pte_t pte, int ncontig) -{ - int i; - - if (pte_write(pte) != pte_write(ptep_get(ptep))) - return 1; - - for (i = 0; i < ncontig; i++) { - pte_t orig_pte = ptep_get(ptep + i); - - if (pte_dirty(pte) != pte_dirty(orig_pte)) - return 1; - - if (pte_young(pte) != pte_young(orig_pte)) - return 1; - } - - return 0; -} - -int huge_ptep_set_access_flags(struct vm_area_struct *vma, - unsigned long addr, pte_t *ptep, - pte_t pte, int dirty) -{ - int ncontig, i; - size_t pgsize = 0; - unsigned long pfn = pte_pfn(pte), dpfn; - struct mm_struct *mm = vma->vm_mm; - pgprot_t hugeprot; - pte_t orig_pte; - - if (!pte_cont(pte)) - return ptep_set_access_flags(vma, addr, ptep, pte, dirty); - - ncontig = find_num_contig(mm, addr, ptep, &pgsize); - dpfn = pgsize >> PAGE_SHIFT; - - if (!__cont_access_flags_changed(ptep, pte, ncontig)) - return 0; - - orig_pte = get_clear_contig_flush(mm, addr, ptep, pgsize, ncontig); - - /* Make sure we don't lose the dirty or young state */ - if (pte_dirty(orig_pte)) - pte = pte_mkdirty(pte); - - if (pte_young(orig_pte)) - pte = pte_mkyoung(pte); - - hugeprot = pte_pgprot(pte); - for (i = 0; i < ncontig; i++, ptep++, addr += pgsize, pfn += dpfn) - set_pte_at(mm, addr, ptep, pfn_pte(pfn, hugeprot)); - - return 1; -} - void huge_ptep_set_wrprotect(struct mm_struct *mm, unsigned long addr, pte_t *ptep) { diff --git a/arch/riscv/mm/hugetlbpage.c b/arch/riscv/mm/hugetlbpage.c index 4c441664db8a..fdb633844d4b 100644 --- a/arch/riscv/mm/hugetlbpage.c +++ b/arch/riscv/mm/hugetlbpage.c @@ -173,35 +173,6 @@ pte_t arch_make_huge_pte(pte_t entry, unsigned int shift, vm_flags_t flags) return entry; } -int huge_ptep_set_access_flags(struct vm_area_struct *vma, - unsigned long addr, - pte_t *ptep, - pte_t pte, - int dirty) -{ - struct mm_struct *mm = vma->vm_mm; - size_t pgsize; - pte_t orig_pte; - int pte_num; - - if (!pte_napot(pte)) - return ptep_set_access_flags(vma, addr, ptep, pte, dirty); - - 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); - - if (pte_dirty(orig_pte)) - pte = pte_mkdirty(pte); - - if (pte_young(orig_pte)) - pte = pte_mkyoung(pte); - - set_ptes(mm, addr, ptep, pte, pte_num); - - return true; -} - void huge_ptep_set_wrprotect(struct mm_struct *mm, unsigned long addr, pte_t *ptep) diff --git a/mm/contpte.c b/mm/contpte.c index 8d2ab391e0d8..3b65c49c6d4b 100644 --- a/mm/contpte.c +++ b/mm/contpte.c @@ -21,6 +21,7 @@ * set_huge_pte_at() * huge_pte_clear() * huge_ptep_get_and_clear() + * huge_ptep_set_access_flags() */ pte_t huge_ptep_get(pte_t *ptep) @@ -156,3 +157,76 @@ pte_t huge_ptep_get_and_clear(struct mm_struct *mm, return get_clear_contig(mm, addr, ptep, pgsize, ncontig); } + +/* + * huge_ptep_set_access_flags will update access flags (dirty, accesssed) + * and write permission. + * + * For a contiguous huge pte range we need to check whether or not write + * permission has to change only on the first pte in the set. Then for + * all the contiguous ptes we need to check whether or not there is a + * discrepancy between dirty or young. + */ +static int __cont_access_flags_changed(pte_t *ptep, pte_t pte, int ncontig) +{ + int i; + + if (pte_write(pte) != pte_write(ptep_get(ptep))) + return 1; + + for (i = 0; i < ncontig; i++) { + pte_t orig_pte = ptep_get(ptep + i); + + if (pte_dirty(pte) != pte_dirty(orig_pte)) + return 1; + + if (pte_young(pte) != pte_young(orig_pte)) + return 1; + } + + return 0; +} + +static pte_t get_clear_contig_flush(struct mm_struct *mm, + unsigned long addr, + pte_t *ptep, + unsigned long pgsize, + unsigned long ncontig) +{ + pte_t orig_pte = get_clear_contig(mm, addr, ptep, pgsize, ncontig); + struct vm_area_struct vma = TLB_FLUSH_VMA(mm, 0); + + flush_tlb_range(&vma, addr, addr + (pgsize * ncontig)); + return orig_pte; +} + +int huge_ptep_set_access_flags(struct vm_area_struct *vma, + unsigned long addr, pte_t *ptep, + pte_t pte, int dirty) +{ + int ncontig; + size_t pgsize = 0; + struct mm_struct *mm = vma->vm_mm; + pte_t orig_pte; + + if (!pte_cont(pte)) + return ptep_set_access_flags(vma, addr, ptep, pte, dirty); + + ncontig = arch_contpte_get_num_contig(vma->vm_mm, addr, ptep, 0, &pgsize); + + if (!__cont_access_flags_changed(ptep, pte, ncontig)) + return 0; + + orig_pte = get_clear_contig_flush(mm, addr, ptep, pgsize, ncontig); + + /* Make sure we don't lose the dirty or young state */ + if (pte_dirty(orig_pte)) + pte = pte_mkdirty(pte); + + if (pte_young(orig_pte)) + pte = pte_mkyoung(pte); + + set_contptes(mm, addr, ptep, pte, ncontig, pgsize); + + return 1; +} -- 2.39.2