From patchwork Fri Mar 1 09:14:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 13578201 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id B3310C5478C for ; Fri, 1 Mar 2024 09:22:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 317D76B0074; Fri, 1 Mar 2024 04:22:23 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 2C8E86B0087; Fri, 1 Mar 2024 04:22:23 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 169FF6B0093; Fri, 1 Mar 2024 04:22:23 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 04ABE6B0074 for ; Fri, 1 Mar 2024 04:22:23 -0500 (EST) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id AD76AA16CC for ; Fri, 1 Mar 2024 09:22:22 +0000 (UTC) X-FDA: 81847929324.20.C70B2EA Received: from mail-wr1-f54.google.com (mail-wr1-f54.google.com [209.85.221.54]) by imf25.hostedemail.com (Postfix) with ESMTP id ED620A0006 for ; Fri, 1 Mar 2024 09:22:20 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=g+J7fmMG; spf=pass (imf25.hostedemail.com: domain of alexghiti@rivosinc.com designates 209.85.221.54 as permitted sender) smtp.mailfrom=alexghiti@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1709284941; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=fHzJQXP5BEIeqfOJsZUcjaIxOTpiaXVWcZpI+r0iAkE=; b=oLKveQXJpafUoeIUIsHnfncYtMBbdVlyuqaBK0ikhnFark5cAz6RD5435j8gvleza0lx/D qWNtqryKiw9MNjWs5P6Sc7pmZtkEZiXWybQqMua4dlLwFNroIZmLDiu9sKk+GxkdyHHkzs aY+nslFsxc1xb+y+V0EphCJlzqY7CiM= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1709284941; a=rsa-sha256; cv=none; b=wkG26GFXhzXUTrr6jkut9be8rO0diJoacmwsTSR6vbXjd4xgN4IbY8EPWuFu7eb8W8gj4e Md7XHiDL2mbeIHXPewCqGymp9SOgaGbFKCXvZ6B5/6Oqd67q3KbOih5vf4mwavi4rx+ZvE 3n6crCqe3wibhxOFY6H9YhbzeY0Gfbg= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=g+J7fmMG; spf=pass (imf25.hostedemail.com: domain of alexghiti@rivosinc.com designates 209.85.221.54 as permitted sender) smtp.mailfrom=alexghiti@rivosinc.com; dmarc=none Received: by mail-wr1-f54.google.com with SMTP id ffacd0b85a97d-33e17fc5aceso535685f8f.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=kvack.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=g+J7fmMGDzr4lj5STiJvKdLKTu56mPJOShDypqAwK9WSD17mQaEPUmDvF9KAwgY5El JXHYmioDqvIuT69qs4Ci0rrY1A/2844J8V4vGh+YJx7UFp4xNLJicVQqZ0KWjTBa2Q9U GF5E2g1u3dpA7LskOL6TNaqQvt+AYj5lZZ/NWofCBRpcYfq9qQW7Ojy8moKc9yE73LSQ A2ZBKda/n16OYynaB6M7hTcWJAzxiCHNobjKhIDzl73PkQUMGGwR7iRV+haDUu5jfnpb PR0W0dYBBG4ZP5o0xKfBycgK4bJajppxj94I5hHKmMVAKZxBJPnVsuFE7BMAkgGd2hqH J3Zw== 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=dcsLzT7wzekmirhGxXC8oYzTQzluDW4boXKYtOqfB3WvXO85L7yEjV6PSZlyJ5ObyR MfdCXAMizEKr+UbDJGYgSPvYec34lT0q08z2bNTWM8gHyWQUSNNK4DjpnCanprt+q6TS clTnCs2eSX2UXIHbfxOMjQYsJQqc5I4N5oOwJEwsUUDObG1elE3xaT6irOu2CdZvnc3L /H941ZKZXorVZpvZPniSPwh2AViI+Cf0N61q1t14ISCsXNGoHK6/Ref5gk6Gy7uvuAoq Qrzt44zrpd3kgOy9weeQYlAbv9cRx/Z46yUUUUvalJd/QwAwIh4b3bshezJOossyUh0u r33A== X-Forwarded-Encrypted: i=1; AJvYcCU654IZxEB4LIGUQ3PrxUADj6bHaAUReZiYVvsIpzCrC4rSvUT/zVfY8gfJbEm9Vynwe/IJZJKs1YaC9vtX0e8+t/k= X-Gm-Message-State: AOJu0YzL5of5xdgx0qWEwr1zuYERL7eJUeBhCbD958poVgJNqbUArerH G5D0sfMzMA01s2xZ8HoUeWcDuIWh5CSNhcsc0uz1fsZtabekwBmYCaIOu/9Lbd0= X-Google-Smtp-Source: AGHT+IETq5VRJHPuwhCoVEqwX4u/EPRIvAhXp1XnZCRkR4LhBkbxP9zDwDTrac3tIekTrNx6Id8PTw== 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> MIME-Version: 1.0 X-Stat-Signature: bafu14fcre463dysmdtejhf3ekgsn115 X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: ED620A0006 X-Rspam-User: X-HE-Tag: 1709284940-256916 X-HE-Meta: U2FsdGVkX18pc74qQNJVlQFd0dd0OAJ3Wbgz4jEtf297FB3lKp0QFpmNGxts1AzXdse6h7ucFv9AZPQ/E7VdZaf3lmz4bAI+BXlq2eKWCbNyYDP/9B+Y5DP9AmZMoXPp3tN7RBhLj0XPvjzXf+d9aEAeBN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ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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; +}