From patchwork Mon Jan 27 09:35:27 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 13951129 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 DC24BC0218C for ; Mon, 27 Jan 2025 09:41:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7444C28011C; Mon, 27 Jan 2025 04:41:53 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7257C280115; Mon, 27 Jan 2025 04:41:53 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5E3CA28011C; Mon, 27 Jan 2025 04:41:53 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 3FBE7280115 for ; Mon, 27 Jan 2025 04:41:53 -0500 (EST) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id C0BD8122181 for ; Mon, 27 Jan 2025 09:41:52 +0000 (UTC) X-FDA: 83052740064.02.909F559 Received: from mail-wr1-f51.google.com (mail-wr1-f51.google.com [209.85.221.51]) by imf30.hostedemail.com (Postfix) with ESMTP id CA23D80008 for ; Mon, 27 Jan 2025 09:41:50 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=zez3wvdC; spf=pass (imf30.hostedemail.com: domain of alexghiti@rivosinc.com designates 209.85.221.51 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=1737970910; 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=/Ib7BO4DbRcQ5HoSRv1ee1R7qySu3xzgQBO2hzqpTQY=; b=fHtrFDecOi9tJRynWLX1m5Ajit1g9b1wnvA4hbjZAdKuBMQ3u7YBwCbCtFfW1ix51VpUmU QRIxy49/9Lx8+UOSXrmxAhjLrWUC9VQQjiYr3JHVsUgCxvqeaHMUvUGA6e81BsSaHziMHv 6urVqfQmi6XJAYPm6WVTKtGgkS7X41A= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737970910; a=rsa-sha256; cv=none; b=LxWbffQgjdj9OmMf9skeHLYU+88HYvDS+bo2VQAqU72D6Pb4mga1+RxlAmPPXuRq/yOFp3 xDXMuy6QCHj8Qg9VmLNQ5MNQraSatq4XsXOlExyi/TuRmlwqqcm3MkdoQ1rURBAZ2OyWlo KyMV1JTWjSdcJGEVMZcvTDZ1/KZcCro= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=zez3wvdC; spf=pass (imf30.hostedemail.com: domain of alexghiti@rivosinc.com designates 209.85.221.51 as permitted sender) smtp.mailfrom=alexghiti@rivosinc.com; dmarc=none Received: by mail-wr1-f51.google.com with SMTP id ffacd0b85a97d-38633b5dbcfso4694927f8f.2 for ; Mon, 27 Jan 2025 01:41:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1737970909; x=1738575709; 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=/Ib7BO4DbRcQ5HoSRv1ee1R7qySu3xzgQBO2hzqpTQY=; b=zez3wvdC6ekjC0gTE6GHzYHtogmjFK05XIPG7gOtG/qNSjdd6QdjH6q4XO4nHtjXF0 kFuV1VqnJXJf7QPiqZRXj0Daq5tVOE/MfRKNotAsxeO9l7QhOF9kCB+Yf/E0zEe0/i3n DMKd9Eq3ZI8/idiPIAwPAOexNOffrGtc2QzdhIJ+U8tK/WVAUJltDraI0X5piZsjQsPD Y7GM6EsWfDVtZbjons4Ccicsju0GifZK7omJnWKh9WwjCiw+v/mJ64Vb/wO0kKEWOo+4 6gb0fUJkeciaPRzXbtYP2gXNnfsk+RIJ+VHB/wzCiv3aAlgRzgfQsDsX6Ff8s8/lDStJ S+Dg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737970909; x=1738575709; 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=/Ib7BO4DbRcQ5HoSRv1ee1R7qySu3xzgQBO2hzqpTQY=; b=ORZMXwR76FoBQlNwC2tUAalOyTvaRb9Lw5NZvSscQmroO/uUm7+xhpOX6ww6X8qdXE v0X+WhX7bAfDK840TwHSk5PdlvQlO+uiOfzfeUEofMf1sBCGwvqmbOxB5g2s8hEsErj7 nLxXmhAmEtN0+zDDuWhuWBAJ32TG2s7GABx7y4+4zYFGLOFADVJ2TeblwK/ZtBiJFT2G 02cU4rF+jHQITExcOtaLRI27YM/1w5YlL9wi4qpKG2PuvRehloUUZfZxteobvcYOAxx6 8TGGbJFRwFT8ra7hT+pbDcwCULeFjX3arqLkBM3tY1+EdDXJwIdLiLq3sqwCAretjsyM KH/A== X-Forwarded-Encrypted: i=1; AJvYcCXfIZWdlZAeMbQ7p8A7mCqjUT4euvMqOMG/zHAUxjFHwCNfIK7B8opuB5JXHbvaypty/CgvpYa2Bg==@kvack.org X-Gm-Message-State: AOJu0YziKmz47Kg+oWlSx1tYxlNBvx7ri8I9VwGhrGeOlFBnARiW1Lue GDiUsuqfreR9dzWmkdX5MdvAVEQiYb/lqwvLLJY6ams4AlHDYwiTx9sMmNSNXwc= X-Gm-Gg: ASbGncv2ZIxzd4BxfLfRTxR3cZs6cpxfDvGbmKjmaTCR5WG35XloxMNbl42Go3Y/7oN /bvhLGliCnTVHtExgf374HxD9LQnlEQh51enG5U3uSn0SkKA1DvORiG7lnXS9lIOz5t/R2DBKRJ 8gKXSzDFgYjeEbJl8HgtX31Ekbp/RhzsMiF3CXOL18e3ij0rbPgvm0UsSKssbgV9bo+Z/AJqQOY 5JSwVtsAkACGCmQf26elIZjWsB21HqBMh1WtdcCo1sND4XnVsSnNxf4Bg8xXy7gPm/LVIxRJ6ni OGx+0ZOUjzxhtm8BUPZiN3M3t2YS5ps= X-Google-Smtp-Source: AGHT+IE0wvAkHX9Lz2nK7eyxGx0auM1eq+y4AS6IYBZCccECBQROh0UBY1MLjeLntJwSdARX6LIQ6w== X-Received: by 2002:a5d:64a3:0:b0:385:f909:eb2c with SMTP id ffacd0b85a97d-38bf57a77a2mr48324115f8f.38.1737970909255; Mon, 27 Jan 2025 01:41:49 -0800 (PST) Received: from alex-rivos.ba.rivosinc.com ([2001:861:3382:ef90:3e22:3e78:ce5a:32c3]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-38c2a1c42fcsm10707051f8f.96.2025.01.27.01.41.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Jan 2025 01:41:49 -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 v4 6/9] mm: Use common huge_ptep_get_and_clear() function for riscv/arm64 Date: Mon, 27 Jan 2025 10:35:27 +0100 Message-Id: <20250127093530.19548-7-alexghiti@rivosinc.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20250127093530.19548-1-alexghiti@rivosinc.com> References: <20250127093530.19548-1-alexghiti@rivosinc.com> MIME-Version: 1.0 X-Stat-Signature: gb7bn13jw6bwy6hd3nqwuxybsg46uymf X-Rspamd-Queue-Id: CA23D80008 X-Rspam-User: X-Rspamd-Server: rspam06 X-HE-Tag: 1737970910-922952 X-HE-Meta: U2FsdGVkX19jiGxkO8m6bNbl13ecBpHOOh9wT7x5uND9nzy8tibdMw2hK8HkJy4uXqHJiHAgJTELT6msP2wWnEmX95rwrFq8Km/ppBJYC8Dh10xVTWng3Np0H9HTNozYvOsfGO2vrIvHmKHdRgIBV56goPvPpG4zW3frKfNIFpWCyWok4K9u3MBxZQXtjXuRh2RFCGNSxO5MIIkrZbLLvAieuIzbvScnCNpwOp5UGNI59ZncOolaVjaiY21ppOztegNoyKM2VPpXF3aMX8ydSrvEac5NeRAkGIGF2wNvv/KZJbF+IyrzfVhf05/qs2aRnMBX+D010uwey6lB2WkMHdzzUgiZ3VprJ+RQGarVvSGcXbIExanUyvTpknwGb+EIk0wcQfHyugh+Kf+ZjVQJ/nmU3GtJAi1LzLprBg5MvWTGPi/NsvxSYBEz35ByrqXwnHvu2+S/Wwa9HPFJc2jz1CXjkKuF02P+Nt6cTm9j5P8w6a4CndXVUsJHqQYJ1uBcsrWH06pb/IiQG7Ex1KAzZNjRVJbZiami9/bw64tAHT6/UUZ4Ok7b8q714pUATzOKgH4izN61gRCcRP11B+M2wWeG8RIu6SlsOT4QfzcAxAeFYs4oHBPQpEqK7C2hfG2vrwPpoC/WlCOW75oVv+Db5BMMdXNW2RAtZRttOfQZ17/Tmgz72TZrYtbDKqinOr8MUnPjyIx4BbkXFmb0W/TKpC78udgbYQjp/vVJrCI7PPoeQ8dtvaHw5xRRI2LQnafuARINZTtaCsE27b8Gf+GCBxiwI1OATxUOSmZodOd8ooB+fLYERA21l+XCfNb6/aPze+vlVplosYM959Y27Hw52e2l5uvAuOQSHni/Y5zfZZwttz4HOq3ZBdA2IpJZAAHY8m12G7JPnaHKvn3Y+SU7UZh9imgjcQjNzTbxus5Q7GUa2OoYcx/BTYPKJrGNpOcXRH7ODGW6q78CCxEOFd3 NZ3cw/Ww p2JD7TDBa5z5Hdpw00YAkg8isQH9ivb/16RvljhLTejoF/bOesssAhZiESDtX1zAI8IOeUsQb1gl4CVgYjEpvYEQ4GW5iqmwwXvDNGMbrSjVPEVn6S13Ln7XpOZ51MtIOVff3GDhMXmC7yAYpXkUOXynC9J0w12Pgm1dpUmAiDywVnDKh+ZoxXHc/s+tLRBa+Iq+u+S1y0xOuSq5TfTf2VLD4axn/uGRYpdcd5833PHL85+/VDu+ung2zhOAso3In+Iw902rHxHz2wu9y/Z6Dq6ZEYCUB44glAgGwfo/LmmPULlRw4U+5o8ROcab2xuPThRnqzB9KD6HULNOfLoNxJjujO3UIsM1ZH62hM99Tv7/ub+aOVcSmQUGsgszPHzQb1Z+Ws2OqZps3WGlPftYm3clGOs8PsapxmM7WqMIP0F029076/c07qM3SlkHTEh6EvMReMkH5gQqvID6n8bpeksGElg== X-Bogosity: 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: 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/hugetlb.h | 3 -- arch/arm64/include/asm/pgtable.h | 15 ++++++++-- arch/arm64/mm/hugetlbpage.c | 19 ++----------- arch/riscv/include/asm/hugetlb.h | 4 --- arch/riscv/include/asm/pgtable.h | 4 ++- arch/riscv/mm/hugetlbpage.c | 23 ++++----------- include/linux/hugetlb_contpte.h | 4 +++ mm/hugetlb_contpte.c | 48 ++++++++++++++++++++++++++++++-- 8 files changed, 72 insertions(+), 48 deletions(-) diff --git a/arch/arm64/include/asm/hugetlb.h b/arch/arm64/include/asm/hugetlb.h index e4acaedea149..5c605a0a2017 100644 --- a/arch/arm64/include/asm/hugetlb.h +++ b/arch/arm64/include/asm/hugetlb.h @@ -39,9 +39,6 @@ pte_t arch_make_huge_pte(pte_t entry, unsigned int shift, vm_flags_t flags); extern 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_AND_CLEAR -extern pte_t huge_ptep_get_and_clear(struct mm_struct *mm, - unsigned long addr, pte_t *ptep); #define __HAVE_ARCH_HUGE_PTEP_SET_WRPROTECT extern void huge_ptep_set_wrprotect(struct mm_struct *mm, unsigned long addr, pte_t *ptep); diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgtable.h index cebbfcfb0e53..c339b568ac51 100644 --- a/arch/arm64/include/asm/pgtable.h +++ b/arch/arm64/include/asm/pgtable.h @@ -1831,12 +1831,23 @@ static inline void clear_young_dirty_ptes(struct vm_area_struct *vma, #endif /* CONFIG_ARM64_CONTPTE */ -static inline int arch_contpte_get_num_contig(pte_t *ptep, - unsigned long size, +extern 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); + if (pgsize) *pgsize = size; diff --git a/arch/arm64/mm/hugetlbpage.c b/arch/arm64/mm/hugetlbpage.c index 541358f50b64..0b7a53fee55d 100644 --- a/arch/arm64/mm/hugetlbpage.c +++ b/arch/arm64/mm/hugetlbpage.c @@ -79,8 +79,8 @@ bool arch_hugetlb_migration_supported(struct hstate *h) } #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/hugetlb.h b/arch/riscv/include/asm/hugetlb.h index ca9930cdf2e6..0fbb6b19df79 100644 --- a/arch/riscv/include/asm/hugetlb.h +++ b/arch/riscv/include/asm/hugetlb.h @@ -20,10 +20,6 @@ bool arch_hugetlb_migration_supported(struct hstate *h); #endif #ifdef CONFIG_RISCV_ISA_SVNAPOT -#define __HAVE_ARCH_HUGE_PTEP_GET_AND_CLEAR -pte_t huge_ptep_get_and_clear(struct mm_struct *mm, - unsigned long addr, pte_t *ptep); - #define __HAVE_ARCH_HUGE_PTEP_CLEAR_FLUSH pte_t huge_ptep_clear_flush(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep); diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pgtable.h index 08b24c0a579b..705d666e014d 100644 --- a/arch/riscv/include/asm/pgtable.h +++ b/arch/riscv/include/asm/pgtable.h @@ -590,7 +590,9 @@ static inline void __set_ptes(struct mm_struct *mm, unsigned long addr, } #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 e2093e7266a5..b44023336fd9 100644 --- a/arch/riscv/mm/hugetlbpage.c +++ b/arch/riscv/mm/hugetlbpage.c @@ -186,7 +186,8 @@ 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, NULL); + pte_num = arch_contpte_get_num_contig(vma->vm_mm, addr, ptep, 0, NULL); + orig_pte = get_clear_contig_flush(mm, addr, ptep, pte_num); if (pte_dirty(orig_pte)) @@ -200,21 +201,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) @@ -228,7 +214,8 @@ void huge_ptep_set_wrprotect(struct mm_struct *mm, return; } - pte_num = arch_contpte_get_num_contig(ptep, 0, NULL); + pte_num = arch_contpte_get_num_contig(mm, addr, ptep, 0, NULL); + orig_pte = get_clear_contig_flush(mm, addr, ptep, pte_num); orig_pte = pte_wrprotect(orig_pte); @@ -246,7 +233,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/include/linux/hugetlb_contpte.h b/include/linux/hugetlb_contpte.h index d9892a047b2b..20d3a3e14e14 100644 --- a/include/linux/hugetlb_contpte.h +++ b/include/linux/hugetlb_contpte.h @@ -18,4 +18,8 @@ extern void set_huge_pte_at(struct mm_struct *mm, extern void huge_pte_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep, unsigned long sz); +#define __HAVE_ARCH_HUGE_PTEP_GET_AND_CLEAR +extern pte_t huge_ptep_get_and_clear(struct mm_struct *mm, + unsigned long addr, pte_t *ptep); + #endif /* _LINUX_HUGETLB_CONTPTE_H */ diff --git a/mm/hugetlb_contpte.c b/mm/hugetlb_contpte.c index c76d6b3d0121..0c86c6f77c29 100644 --- a/mm/hugetlb_contpte.c +++ b/mm/hugetlb_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(struct mm_struct *mm, unsigned long addr, pte_t *ptep) @@ -32,7 +33,7 @@ pte_t huge_ptep_get(struct mm_struct *mm, unsigned long addr, 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(mm, addr, ptep, page_size(pte_page(orig_pte)), NULL); @@ -86,7 +87,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) @@ -110,8 +111,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); +}