From nobody Thu Dec 18 04:46:26 2025 Received: from mail-wr1-f50.google.com (mail-wr1-f50.google.com [209.85.221.50]) (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 B6B7C1F8F09 for ; Fri, 21 Mar 2025 13:07:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.50 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742562463; cv=none; b=I2oHMPKPSxw+OD4ttHxe3CAQouIgQ9pkDkr/1WtsazV6XYXcdde53De6YUi4AUlnz98CqKeqewQVa/TS0ea8/oIkRGfznGTUnFybE59sJmtrshVp8wrysRbRHBQFli5zhaw2BOzCJFJA+bE/D9hleO7n25gkpgD58/XFsSJnW9Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742562463; c=relaxed/simple; bh=3hu/TngcPm/TeOTChz/9uPROqKOaSIqDhVY9hzEgLFI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=D+Gia5OlatJrz96bje7EHeezR4Eti4oaSAHcEKwX7RAqetX11YwJakOlmHRraDTKQVIywO9GZtQS99Yv86q4ZKZ7Sj16lznhEsMFSLad+HcsAISxbhE+bKfsgdGtVsLEILa78IxZGjjeJwTGFmnZj7YRNwgSrPwB4OvtAJK2ros= 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=UUwB57u/; arc=none smtp.client-ip=209.85.221.50 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 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="UUwB57u/" Received: by mail-wr1-f50.google.com with SMTP id ffacd0b85a97d-3912baafc58so1601080f8f.1 for ; Fri, 21 Mar 2025 06:07:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1742562460; x=1743167260; 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=+81In4ilDZ/dsxug/tXtQcTDxsQusJyemdqH5+0jxZc=; b=UUwB57u/oG4Shv2MT5DLIE15Sq/A3H+62nGwtcwwabh5HyoAQMRO7JXsM6DWc/rWA2 ur079LoSl7eMmlo9nsKA5SBabObKefAAF/bFMDZV8zlM43KyWDW5DLBKRCpMdo8XYkm7 HNfzfZ1ZBud9bO3s5f1K073U8PHJc4/UsT1O8X9Sn5bvTaT2SX3HngXKyfAPBjIcJAaF 4uaodLOBTwhRLKTntDEmCWnZ7mXGMjP9K9aKYGBUwzM4sj0dyr/YmgFlOt9bGUeU8xYt 88P3cxqwBkhzEj+bj5Z5p+fD4IuZ3ofCXVF6hS22LunOzLo0pDnUOWcmDzhSKIRe69fU S3rw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742562460; x=1743167260; 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=+81In4ilDZ/dsxug/tXtQcTDxsQusJyemdqH5+0jxZc=; b=iU+pKQgZhnhlnJog3Cjyi4owB3f/pHiWVXFW/5V044KkyvU7mmgLWKg98kSzgjXDFH eLJPayjn+rITXLTVKxfxQmWRMqV0J7CBLVABeCS1HmoP8k+OPJVY9U/ceKaJe1Og22AD t2Afr+K+FXP9jV7P6tNlnfwudnJsSlhrTUGCfYvr7FMp9mQ2BOEdnt66HLhXmswlxbHK HlSh9lvJ2YjWDcvY42fqgsgUZntv2bryWXRrfP7e6gOwxeDNopU6tdxrJmi3PgMlPdbd n2hO6a0Shjr/h4ZSBAsrkgfQTsotLbb0SLpaLM/t0lTXd43/LhX0MqjeVTUA9859j9C3 yKPQ== X-Forwarded-Encrypted: i=1; AJvYcCX+TQmPCXZUMXn+FWpzVUtEnFq0ddRNbh0w1di8JkPXMlTM/RhlptOG6jN1/TFYDK/kBb92JoKcIOJZkTs=@vger.kernel.org X-Gm-Message-State: AOJu0Yxxxu+wLQpCbwppWi+9V0ZkLfHaB+uyzkTNgr0eCbO3B+8Sqek/ A9IotnbUQh/lcmZS2pA24kvZ0V4+bAwIE1tLYdOgk+wllg8891f/Tfdif/i0z0E= X-Gm-Gg: ASbGncsVSnF/O3/dAA/ZzuZ8PO2RLuphMlmzKiPt9katY3MjejV4QzZQrXFONvcRzlp LwwlTeSHRVrjls7ESek2R8I0x1YbbGxq9II9qXbQrhfWR02VMqEVjsKVWJ8mctXL2IBl7cePsBJ a5n28/K7TQoIpJPGKWyNG4BW7RW7+K2QplEll609lDzEzeLtieaD6GChTQdofE+FyuZ9kIgGdVv Z2AvId0md4Ra9r1pDHBEQc50aWpx8lweDo2GjW7CZZVbpzGrywc4dUn1pNbars9vziGEt4OnfxQ lS8IVzhWOj1ircqJNQyvcTjiZISBYqkW/0fvE4fRFAWaZrF+u4NnX4tFJhv7+ub/kpumrA== X-Google-Smtp-Source: AGHT+IFGcNIQFtkSrVrMaEJyHkzdoC3QT/2GtmdRgmqzsJ6C1xedRKK8otT6Nr1+/A9BoBAqiRGLXw== X-Received: by 2002:a05:6000:1f86:b0:391:4743:6de4 with SMTP id ffacd0b85a97d-3997f940e28mr2542677f8f.50.1742562459890; Fri, 21 Mar 2025 06:07:39 -0700 (PDT) Received: from alex-rivos.ba.rivosinc.com ([2001:861:3382:ef90:3d12:52fe:c1cc:c94]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3997f9efd6csm2364857f8f.90.2025.03.21.06.07.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 21 Mar 2025 06:07:39 -0700 (PDT) From: Alexandre Ghiti To: Catalin Marinas , Will Deacon , Ryan Roberts , Mark Rutland , Matthew Wilcox , Paul Walmsley , Palmer Dabbelt , Alexandre Ghiti , 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 v5 1/9] riscv: Safely remove huge_pte_offset() when manipulating NAPOT ptes Date: Fri, 21 Mar 2025 14:06:27 +0100 Message-Id: <20250321130635.227011-2-alexghiti@rivosinc.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20250321130635.227011-1-alexghiti@rivosinc.com> References: <20250321130635.227011-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: quoted-printable Content-Type: text/plain; charset="utf-8" The pte_t pointer is expected to point to the first entry of the NAPOT mapping so no need to use huge_pte_offset(), similarly to what is done in arm64. Signed-off-by: Alexandre Ghiti --- arch/riscv/mm/hugetlbpage.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/arch/riscv/mm/hugetlbpage.c b/arch/riscv/mm/hugetlbpage.c index 375dd96bb4a0..3192ad804279 100644 --- a/arch/riscv/mm/hugetlbpage.c +++ b/arch/riscv/mm/hugetlbpage.c @@ -287,7 +287,6 @@ int huge_ptep_set_access_flags(struct vm_area_struct *v= ma, =20 order =3D napot_cont_order(pte); pte_num =3D napot_pte_num(order); - ptep =3D huge_pte_offset(mm, addr, napot_cont_size(order)); orig_pte =3D get_clear_contig_flush(mm, addr, ptep, pte_num); =20 if (pte_dirty(orig_pte)) @@ -334,7 +333,6 @@ void huge_ptep_set_wrprotect(struct mm_struct *mm, =20 order =3D napot_cont_order(pte); pte_num =3D napot_pte_num(order); - ptep =3D huge_pte_offset(mm, addr, napot_cont_size(order)); orig_pte =3D get_clear_contig_flush(mm, addr, ptep, pte_num); =20 orig_pte =3D pte_wrprotect(orig_pte); --=20 2.39.2 From nobody Thu Dec 18 04:46:26 2025 Received: from mail-wm1-f42.google.com (mail-wm1-f42.google.com [209.85.128.42]) (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 E04C21F8F09 for ; Fri, 21 Mar 2025 13:08:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742562525; cv=none; b=C0hApX1VYz4LYNv6+YMdpHizUNCt5MyKN1ckSEnZZZ08VKEzwqJAcv0SxwSJKK8NmGhGkGvxbd7S69idXREjgaX5zDu1iw+28R663Iv5HUg4/SzidvkkdkRmG5T8Z/Xx5iyTkhEh33aCKqsA/Ktoo7MJiaXTSsa8Cyw90j2Ejao= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742562525; c=relaxed/simple; bh=V1sWkJCuwpwz4JJIIt0mK7fY4+5g/wtAnyL8x184+bI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=CvsDt4XQv0N+gjYRIHZNzuKo2hEW367qyHess77MjIqerDaGKn7wLO46QNhw4AgIOSj6Fzpf235qGsgmYNVnAZUOb21zzcRShMn4KMrWEIIKeZwGbA013SLuVI6WwApAafpCpstC+udIn5qglN3AYipzfdC+NacgjmjRQleVooc= 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=lBMGAZlh; arc=none smtp.client-ip=209.85.128.42 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 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="lBMGAZlh" Received: by mail-wm1-f42.google.com with SMTP id 5b1f17b1804b1-43cfe574976so11845625e9.1 for ; Fri, 21 Mar 2025 06:08:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1742562522; x=1743167322; 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=MHcTeoTN4G5JoOrCU+WuSXOyCDmNaouJRLSJHK0ciYw=; b=lBMGAZlh1DEQfsBlGSN0mYa7FjwHiSx0oRcgdaONtTvaXnu3oicH86SvQ2UIEQxC7H 1WOIoo1ggor1NYE205y3yMMDivzolKHH07yRweNdU/rWvFG6DnqRUUaDA5UgnPr5UfLM dCm/wGmaLlTb4xvsPo1SGfkCrB4IfjOrriIllEEd8JEmTqeIjvF/ekkUrPov/6zgr12Z tYKFSw1MFli4PmdSYKkUym+X6MVvzcWwSe0JyhsvC02U0qnpMTO519OYtaiMr9wf4cnB hBa4RNNDQQTQr96KcEKlAcDSjJb7fA0UOHrb1n8jup5Cr/2iiKV7hdxvUZ1Vy3Tz3RR3 1qyA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742562522; x=1743167322; 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=MHcTeoTN4G5JoOrCU+WuSXOyCDmNaouJRLSJHK0ciYw=; b=TPeXc7+CZlhmTqz3cgfqAhXIcOuCD/b2oruKrgH4d5WcHOImmk34i5BIS68Fo3V99U UaYRlAwWowpUgiaciCmlCz28LXNJ58tK8muT8M5bGOHo3a+lOaKMuYZk9As+Wn4wQZBE rmRv11wDMRGy2cBOKtPAfHMYH+ogiyq7mPyZj22umDL9G1rTqb/r5ZWDDmD7sxHopOMf AvrqbGqcBVQtQKYeyNTwD6mkjSjTTfCfleJyR+/sOU6vjZaFKYgijGRD8Prn9O+iOL87 hgZaAbY5nJ1illRPy7DrEpI5TkVkOlKuHKEIt6cdUe92HutLviTaQOtbxXPnAdh9USaj BURQ== X-Forwarded-Encrypted: i=1; AJvYcCVk/hci1riJAoqlLduVjsisiOaVpiDZYUMNq8K3buvsGaIBfCDKxNdLlIACedPRrw93qWINrQ7sAzyDMVM=@vger.kernel.org X-Gm-Message-State: AOJu0Yxsh08e4xBJO0+GejusPAqI3xD4Iago+3760zKN8aCftrcdrGwN qMGf77RQe9fFinDM3DZBgWyYsHYe3lpoKBGpg0/OOF0kbs3wuYacqQWEgAcmRZY= X-Gm-Gg: ASbGnct9scPam4D8JGp7Uh4wozqu882iDC1b+r74OCdYrU/wzuO7YPtJ+UUzAWOQiML ZXQwf4Qvb21/ggccuOlKtVIBVU2kYlTqagFPRUsqU6kr1ELSb8n2OH+4HHcaZJAiJUrbuog82Va ufkv3ZR78Yczf9E9nVtr2dMV7AlXQQoEfwHweyovtnUc6QxMDB2FURnTM8l5D58VbqoWwOlFq90 yMvFQLPP+dKoImynkWR0wOjEGdyJF0jKIqQPHKMKet4TaV0c9O5l6UODXZHgOcu0AhtpfYVYEbI mWzH9pA0s38zJkAulBPevJ1Ss4vQZA8uk0F4FgOULbBUZh5oTotLTqbhts7zAUleazRHIF4Rak+ iGnkV X-Google-Smtp-Source: AGHT+IEMJBrD5VHSiFntLl7eq4OXY9R2l0VvwmSKG3LcIwl+yD5imfUnEeI+G2u9jHEtl52hrfshfg== X-Received: by 2002:a05:600c:46d4:b0:43c:e6d1:efe7 with SMTP id 5b1f17b1804b1-43d50a3b62emr18553825e9.26.1742562521606; Fri, 21 Mar 2025 06:08:41 -0700 (PDT) Received: from alex-rivos.ba.rivosinc.com ([2001:861:3382:ef90:3d12:52fe:c1cc:c94]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-43d43fdec51sm78104015e9.27.2025.03.21.06.08.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 21 Mar 2025 06:08:41 -0700 (PDT) From: Alexandre Ghiti To: Catalin Marinas , Will Deacon , Ryan Roberts , Mark Rutland , Matthew Wilcox , Paul Walmsley , Palmer Dabbelt , Alexandre Ghiti , 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 v5 2/9] riscv: Restore the pfn in a NAPOT pte when manipulated by core mm code Date: Fri, 21 Mar 2025 14:06:28 +0100 Message-Id: <20250321130635.227011-3-alexghiti@rivosinc.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20250321130635.227011-1-alexghiti@rivosinc.com> References: <20250321130635.227011-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: quoted-printable Content-Type: text/plain; charset="utf-8" The core mm code expects to be able to extract the pfn from a pte. NAPOT mappings work differently since its ptes actually point to the first pfn of the mapping, the other bits being used to encode the size of the mapping. So modify ptep_get() so that it returns a pte value that contains the *real* pfn (which is then different from what the HW expects) and right before storing the ptes to the page table, reset the pfn LSBs to the size of the mapping. And make sure that all NAPOT mappings are set using set_ptes(). Signed-off-by: Alexandre Ghiti --- arch/riscv/include/asm/pgtable-64.h | 11 ++ arch/riscv/include/asm/pgtable.h | 155 ++++++++++++++++++++++++---- arch/riscv/mm/hugetlbpage.c | 15 ++- 3 files changed, 152 insertions(+), 29 deletions(-) diff --git a/arch/riscv/include/asm/pgtable-64.h b/arch/riscv/include/asm/p= gtable-64.h index 0897dd99ab8d..cddbe426f618 100644 --- a/arch/riscv/include/asm/pgtable-64.h +++ b/arch/riscv/include/asm/pgtable-64.h @@ -104,6 +104,17 @@ enum napot_cont_order { #define napot_cont_mask(order) (~(napot_cont_size(order) - 1UL)) #define napot_pte_num(order) BIT(order) =20 +static inline bool is_napot_order(unsigned int order) +{ + unsigned int napot_order; + + for_each_napot_order(napot_order) + if (order =3D=3D napot_order) + return true; + + return false; +} + #ifdef CONFIG_RISCV_ISA_SVNAPOT #define HUGE_MAX_HSTATE (2 + (NAPOT_ORDER_MAX - NAPOT_CONT_ORDER_BASE)) #else diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pgta= ble.h index 050fdc49b5ad..2e62d7e607db 100644 --- a/arch/riscv/include/asm/pgtable.h +++ b/arch/riscv/include/asm/pgtable.h @@ -296,6 +296,17 @@ static inline unsigned long pte_napot(pte_t pte) return pte_val(pte) & _PAGE_NAPOT; } =20 +#define pte_valid_napot(pte) (pte_present(pte) && pte_napot(pte)) + +/* + * contpte is what we expose to the core mm code, this is not exactly a na= pot + * mapping since the size is not encoded in the pfn yet. + */ +static inline pte_t pte_mkcont(pte_t pte) +{ + return __pte(pte_val(pte) | _PAGE_NAPOT); +} + static inline pte_t pte_mknapot(pte_t pte, unsigned int order) { int pos =3D order - 1 + _PAGE_PFN_SHIFT; @@ -305,6 +316,12 @@ static inline pte_t pte_mknapot(pte_t pte, unsigned in= t order) return __pte((pte_val(pte) & napot_mask) | napot_bit | _PAGE_NAPOT); } =20 +/* pte at entry must *not* encode the mapping size in the pfn LSBs. */ +static inline pte_t pte_clear_napot(pte_t pte) +{ + return __pte(pte_val(pte) & ~_PAGE_NAPOT); +} + #else =20 static __always_inline bool has_svnapot(void) { return false; } @@ -314,17 +331,24 @@ static inline unsigned long pte_napot(pte_t pte) return 0; } =20 +static inline pte_t pte_clear_napot(pte_t pte) +{ + return pte; +} + +static inline pte_t pte_mknapot(pte_t pte, unsigned int order) +{ + return pte; +} + +#define pte_valid_napot(pte) false + #endif /* CONFIG_RISCV_ISA_SVNAPOT */ =20 /* Yields the page frame number (PFN) of a page table entry */ static inline unsigned long pte_pfn(pte_t pte) { - unsigned long res =3D __page_val_to_pfn(pte_val(pte)); - - if (has_svnapot() && pte_napot(pte)) - res =3D res & (res - 1UL); - - return res; + return __page_val_to_pfn(pte_val(pte)); } =20 #define pte_page(x) pfn_to_page(pte_pfn(x)) @@ -559,8 +583,13 @@ static inline void __set_pte_at(struct mm_struct *mm, = pte_t *ptep, pte_t pteval) =20 #define PFN_PTE_SHIFT _PAGE_PFN_SHIFT =20 -static inline void set_ptes(struct mm_struct *mm, unsigned long addr, - pte_t *ptep, pte_t pteval, unsigned int nr) +static inline pte_t ___ptep_get(pte_t *ptep) +{ + return READ_ONCE(*ptep); +} + +static inline void ___set_ptes(struct mm_struct *mm, unsigned long addr, + pte_t *ptep, pte_t pteval, unsigned int nr) { page_table_check_ptes_set(mm, ptep, pteval, nr); =20 @@ -569,10 +598,13 @@ static inline void set_ptes(struct mm_struct *mm, uns= igned long addr, if (--nr =3D=3D 0) break; ptep++; + + if (unlikely(pte_valid_napot(pteval))) + continue; + pte_val(pteval) +=3D 1 << _PAGE_PFN_SHIFT; } } -#define set_ptes set_ptes =20 static inline void pte_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep) @@ -587,17 +619,6 @@ extern int ptep_set_access_flags(struct vm_area_struct= *vma, unsigned long addre extern int ptep_test_and_clear_young(struct vm_area_struct *vma, unsigned = long address, pte_t *ptep); =20 -#define __HAVE_ARCH_PTEP_GET_AND_CLEAR -static inline pte_t ptep_get_and_clear(struct mm_struct *mm, - unsigned long address, pte_t *ptep) -{ - pte_t pte =3D __pte(atomic_long_xchg((atomic_long_t *)ptep, 0)); - - page_table_check_pte_clear(mm, pte); - - return pte; -} - #define __HAVE_ARCH_PTEP_SET_WRPROTECT static inline void ptep_set_wrprotect(struct mm_struct *mm, unsigned long address, pte_t *ptep) @@ -627,6 +648,100 @@ static inline int ptep_clear_flush_young(struct vm_ar= ea_struct *vma, return ptep_test_and_clear_young(vma, address, ptep); } =20 +#ifdef CONFIG_RISCV_ISA_SVNAPOT +static inline pte_t pte_napot_clear_pfn(pte_t *ptep, pte_t pte) +{ + /* + * The pte we load has the N bit set and the size of the mapping in + * the pfn LSBs: keep the N bit and replace the mapping size with + * the *real* pfn since the core mm code expects to find it there. + * The mapping size will be reset just before being written to the + * page table in set_ptes(). + */ + if (unlikely(pte_valid_napot(pte))) { + unsigned int order =3D napot_cont_order(pte); + int pos =3D order - 1 + _PAGE_PFN_SHIFT; + unsigned long napot_mask =3D ~GENMASK(pos, _PAGE_PFN_SHIFT); + pte_t *orig_ptep =3D PTR_ALIGN_DOWN(ptep, sizeof(*ptep) * napot_pte_num(= order)); + + pte =3D __pte((pte_val(pte) & napot_mask) + ((ptep - orig_ptep) << _PAGE= _PFN_SHIFT)); + } + + return pte; +} + +#define __HAVE_ARCH_PTEP_GET_AND_CLEAR +static inline pte_t ptep_get_and_clear(struct mm_struct *mm, + unsigned long address, pte_t *ptep) +{ + pte_t pte =3D __pte(atomic_long_xchg((atomic_long_t *)ptep, 0)); + + pte =3D pte_napot_clear_pfn(ptep, pte); + + page_table_check_pte_clear(mm, pte); + + return pte; +} + +static inline pte_t __ptep_get(pte_t *ptep) +{ + pte_t pte =3D ___ptep_get(ptep); + + return pte_napot_clear_pfn(ptep, pte); +} + +static inline void __set_ptes(struct mm_struct *mm, unsigned long addr, + pte_t *ptep, pte_t pteval, unsigned int nr) +{ + if (unlikely(pte_valid_napot(pteval))) { + unsigned int order =3D ilog2(nr); + + if (!is_napot_order(order)) { + /* + * Something's weird, we are given a NAPOT pte but the + * size of the mapping is not a known NAPOT mapping + * size, so clear the NAPOT bit and map this without + * NAPOT support: core mm only manipulates pte with the + * real pfn so we know the pte is valid without the N + * bit. + */ + pr_err("Incorrect NAPOT mapping, resetting.\n"); + pteval =3D pte_clear_napot(pteval); + } else { + /* + * NAPOT ptes that arrive here only have the N bit set + * and their pfn does not contain the mapping size, so + * set that here. + */ + pteval =3D pte_mknapot(pteval, order); + } + } + + ___set_ptes(mm, addr, ptep, pteval, nr); +} + +#define ptep_get __ptep_get +#define set_ptes __set_ptes +#define set_contptes(mm, addr, ptep, pte, nr, pgsize) \ + set_ptes(mm, addr, ptep, pte, nr) +#else +#define ptep_get ___ptep_get +#define __ptep_get ___ptep_get +#define set_ptes ___set_ptes +#define __set_ptes ___set_ptes + +#define __HAVE_ARCH_PTEP_GET_AND_CLEAR +static inline pte_t ptep_get_and_clear(struct mm_struct *mm, + unsigned long address, pte_t *ptep) +{ + pte_t pte =3D __pte(atomic_long_xchg((atomic_long_t *)ptep, 0)); + + page_table_check_pte_clear(mm, pte); + + return pte; +} +#endif /* CONFIG_RISCV_ISA_SVNAPOT */ + #define pgprot_nx pgprot_nx static inline pgprot_t pgprot_nx(pgprot_t _prot) { diff --git a/arch/riscv/mm/hugetlbpage.c b/arch/riscv/mm/hugetlbpage.c index 3192ad804279..60b7e738b31a 100644 --- a/arch/riscv/mm/hugetlbpage.c +++ b/arch/riscv/mm/hugetlbpage.c @@ -190,7 +190,7 @@ pte_t arch_make_huge_pte(pte_t entry, unsigned int shif= t, vm_flags_t flags) =20 for_each_napot_order(order) { if (shift =3D=3D napot_cont_shift(order)) { - entry =3D pte_mknapot(entry, order); + entry =3D pte_mkcont(entry); break; } } @@ -267,8 +267,7 @@ void set_huge_pte_at(struct mm_struct *mm, =20 clear_flush(mm, addr, ptep, pgsize, pte_num); =20 - for (i =3D 0; i < pte_num; i++, ptep++, addr +=3D pgsize) - set_pte_at(mm, addr, ptep, pte); + set_ptes(mm, addr, ptep, pte, pte_num); } =20 int huge_ptep_set_access_flags(struct vm_area_struct *vma, @@ -280,7 +279,7 @@ int huge_ptep_set_access_flags(struct vm_area_struct *v= ma, struct mm_struct *mm =3D vma->vm_mm; unsigned long order; pte_t orig_pte; - int i, pte_num; + int pte_num; =20 if (!pte_napot(pte)) return ptep_set_access_flags(vma, addr, ptep, pte, dirty); @@ -295,8 +294,7 @@ int huge_ptep_set_access_flags(struct vm_area_struct *v= ma, if (pte_young(orig_pte)) pte =3D pte_mkyoung(pte); =20 - for (i =3D 0; i < pte_num; i++, addr +=3D PAGE_SIZE, ptep++) - set_pte_at(mm, addr, ptep, pte); + set_ptes(mm, addr, ptep, pte, pte_num); =20 return true; } @@ -324,7 +322,7 @@ void huge_ptep_set_wrprotect(struct mm_struct *mm, pte_t pte =3D ptep_get(ptep); unsigned long order; pte_t orig_pte; - int i, pte_num; + int pte_num; =20 if (!pte_napot(pte)) { ptep_set_wrprotect(mm, addr, ptep); @@ -337,8 +335,7 @@ void huge_ptep_set_wrprotect(struct mm_struct *mm, =20 orig_pte =3D pte_wrprotect(orig_pte); =20 - for (i =3D 0; i < pte_num; i++, addr +=3D PAGE_SIZE, ptep++) - set_pte_at(mm, addr, ptep, orig_pte); + set_ptes(mm, addr, ptep, orig_pte, pte_num); } =20 pte_t huge_ptep_clear_flush(struct vm_area_struct *vma, --=20 2.39.2 From nobody Thu Dec 18 04:46:26 2025 Received: from mail-wm1-f45.google.com (mail-wm1-f45.google.com [209.85.128.45]) (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 D04C21F8F09 for ; Fri, 21 Mar 2025 13:09:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742562587; cv=none; b=FPMHCrl+n6uz3ptmkWTvSPbv+6YjYdSCSGvIBBMzh17mzT3MORWZKv5xsg6As6fVe0hMH56LCeFfDtFm/XOObWZqzGvKRWY1WHMgy+Ba7UUHHdhvp6HvtRmhC98sTxLHXsCsLPjkXoLUr7A4WPF0RBEwnJtd/81dBjpH+n236cw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742562587; c=relaxed/simple; bh=p3RauSFB+74ijXsM1H7UMKUd6CXeWNsI0FXkrs+Bl2Y=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=mIh0zxw7jINbzomv2Hri22RmZzLSsK/FCJyPpg4npcna9U8LuihoETYi0wkmLT2UoKBNgQYSdsLu1wgDsXtxi9mCQwGkwArEAxahzKSfJgeQ5i8Uv6O/nJx9nU13FSDxKEs0WRwn2QKLfYKYQ6YlLBDHq4iw3g7W8aPj9hDc3e8= 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=HKRYh3W4; arc=none smtp.client-ip=209.85.128.45 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 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="HKRYh3W4" Received: by mail-wm1-f45.google.com with SMTP id 5b1f17b1804b1-43cfa7e7f54so12055355e9.1 for ; Fri, 21 Mar 2025 06:09:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1742562583; x=1743167383; 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=e8EtFYpkeeZSqWY8m05eSUUviRVNOc3GBKbkMNHHDeA=; b=HKRYh3W4aNQ4oXGfpb7f5alz1+EUUrSbSUEq4EU7CiEGo47lcZ0pF3QSDM8QETX/AF WEYjlm7r+1/h+2oXq2jF4KJYoqtIM3+/vnhk/LLltHhUXFBnRLEjsCSrzZ03lpbqlNaz NuFYfigFdK+aTNyCBTq+CqhaLFpN5m/VbnXRWt0TOVRVZ/MiPK33rrX2qUxxHKXMl4s0 s+ZiW6lwFLayr9jVdTB8UuNwqlwo+efuxFgpFLmPQUr3jDL38OOzWj5LLL2k8PONH8u3 aVJrkYeZIaIofvSOvATU/jpu73xBzNKDlnZGcqR3LWpnPiqkJj5NYSGrClLk1LLAlaWq r1ug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742562583; x=1743167383; 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=e8EtFYpkeeZSqWY8m05eSUUviRVNOc3GBKbkMNHHDeA=; b=ZgpH/t2KCGPRA2Jc8qfZ8Lj4+DrTkhE9Njn/oDlj58qr8jeU9/sxlYI2LeqGUt3u7g ghBMiDDxVVsSkaS8RyBjFx4/NGUk7iwok/SU9sxEVxzRgSedZAFLWBYRRFBjfCDRWoYU 8bPnn5cFxIahLUG/kn9thvJShHR3LSRHxDiMGJE4UC0RA3InTUimLmZFxXayZIxr/K+U HcUy6L0OPxcRHdZpbH+EhvZwFRyIw+iz3fos0yQ7SRlL6JTXyGvK8BBwD4TnRepMNcgQ ERyPZukg9gEVwZbsbKbQev9+QrpT0X9qcnhDq8E0cy1xxUor1uM0rLNlcJHj0AJYM8tE 3/kQ== X-Forwarded-Encrypted: i=1; AJvYcCWpoVlgYDhOzonh1qLNmCkr9OsC6ko+HNfSQxn4YzaF1d+7tMeRsDlTWBhDhBQHU7/zqH7d7xAit5gjVuk=@vger.kernel.org X-Gm-Message-State: AOJu0YwXGY0BTRMkyDVO53DwLEVoZ4EEoRo8s1Tu830ZmiUI3lPNJsTc TrRlx63KIIhzx5Fvojd23L1bxILREVA8X6KPxGtyRdeF78HAV1D/hPnQivczbuk= X-Gm-Gg: ASbGncs7uqG4HtHDg9zI5PcSEMv6Lqa0m/ZIYMC6fazVRv4nD9k58rnofqCgtaHEyDC LGMMFc/jZftfWyWXdmYAmuNhIH6ELudgfaYf9KWEfYPEe4dPQTP/nZMYhAv7JTiDKLh40fYkhaM AD+0LlHpP8b4LF8D/lBArOBRtT4/TXYYPzUFN7QqUBYObIj6Om3ZqqwzoOvU+pRkEBaOL8XKs5T M4FqRthJegieJJKDn4e+hD5uu/P9tc77w4JnQZfC0UrrNQ6HaKgfy/aoq6jMjWcL5dmWOmYy7u0 U5a42rtj/5Tre70sW2t3l6Ld286KVaNgKy2gZT/h6Tc+MySG/vkQN1ANI5jy7EoQIFzAdw== X-Google-Smtp-Source: AGHT+IFrlDybl2QKCVYqfxTyu64dXX62tEzPiiOD5kkpTcHrUIRBIEyVsyFQKXhsbfpJSLh/l0FWog== X-Received: by 2002:a05:600c:4e4b:b0:439:9737:675b with SMTP id 5b1f17b1804b1-43d502f8536mr33623225e9.7.1742562582938; Fri, 21 Mar 2025 06:09:42 -0700 (PDT) Received: from alex-rivos.ba.rivosinc.com ([2001:861:3382:ef90:3d12:52fe:c1cc:c94]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3997f9a3c7fsm2377861f8f.34.2025.03.21.06.09.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 21 Mar 2025 06:09:42 -0700 (PDT) From: Alexandre Ghiti To: Catalin Marinas , Will Deacon , Ryan Roberts , Mark Rutland , Matthew Wilcox , Paul Walmsley , Palmer Dabbelt , Alexandre Ghiti , 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 v5 3/9] mm: Use common huge_ptep_get() function for riscv/arm64 Date: Fri, 21 Mar 2025 14:06:29 +0100 Message-Id: <20250321130635.227011-4-alexghiti@rivosinc.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20250321130635.227011-1-alexghiti@rivosinc.com> References: <20250321130635.227011-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: quoted-printable Content-Type: text/plain; charset="utf-8" After some adjustments, both architectures have the same implementation so move it to the generic code. Signed-off-by: Alexandre Ghiti --- arch/arm64/Kconfig | 1 + arch/arm64/include/asm/hugetlb.h | 3 +- arch/arm64/include/asm/pgtable.h | 57 +++++++++++++++++++++++++-- arch/arm64/mm/hugetlbpage.c | 66 ++------------------------------ arch/riscv/Kconfig | 1 + arch/riscv/include/asm/hugetlb.h | 6 +-- arch/riscv/include/asm/pgtable.h | 45 ++++++++++++++++++++++ arch/riscv/mm/hugetlbpage.c | 62 +++--------------------------- include/linux/hugetlb_contpte.h | 12 ++++++ mm/Kconfig | 3 ++ mm/Makefile | 1 + mm/hugetlb_contpte.c | 32 ++++++++++++++++ 12 files changed, 161 insertions(+), 128 deletions(-) create mode 100644 include/linux/hugetlb_contpte.h create mode 100644 mm/hugetlb_contpte.c diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig index 940343beb3d4..5a1e1bc73c15 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -113,6 +113,7 @@ config ARM64 select ARCH_WANT_DEFAULT_BPF_JIT select ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT select ARCH_WANT_FRAME_POINTERS + select ARCH_WANT_GENERAL_HUGETLB_CONTPTE select ARCH_WANT_HUGE_PMD_SHARE if ARM64_4K_PAGES || (ARM64_16K_PAGES && = !ARM64_VA_BITS_36) select ARCH_WANT_LD_ORPHAN_WARN select ARCH_WANTS_EXECMEM_LATE diff --git a/arch/arm64/include/asm/hugetlb.h b/arch/arm64/include/asm/huge= tlb.h index 07fbf5bf85a7..0604e01dca97 100644 --- a/arch/arm64/include/asm/hugetlb.h +++ b/arch/arm64/include/asm/hugetlb.h @@ -13,6 +13,7 @@ #include #include #include +#include =20 #ifdef CONFIG_ARCH_ENABLE_HUGEPAGE_MIGRATION #define arch_hugetlb_migration_supported arch_hugetlb_migration_supported @@ -53,8 +54,6 @@ extern pte_t huge_ptep_clear_flush(struct vm_area_struct = *vma, #define __HAVE_ARCH_HUGE_PTE_CLEAR extern void huge_pte_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep, unsigned long sz); -#define __HAVE_ARCH_HUGE_PTEP_GET -extern pte_t huge_ptep_get(struct mm_struct *mm, unsigned long addr, pte_t= *ptep); =20 void __init arm64_hugetlb_cma_reserve(void); =20 diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgta= ble.h index 0b2a2ad1b9e8..af8156929c1d 100644 --- a/arch/arm64/include/asm/pgtable.h +++ b/arch/arm64/include/asm/pgtable.h @@ -420,9 +420,10 @@ static inline pte_t pte_advance_pfn(pte_t pte, unsigne= d long nr) return pfn_pte(pte_pfn(pte) + nr, pte_pgprot(pte)); } =20 -static inline void __set_ptes(struct mm_struct *mm, - unsigned long __always_unused addr, - pte_t *ptep, pte_t pte, unsigned int nr) +static inline void ___set_ptes(struct mm_struct *mm, + unsigned long __always_unused addr, + pte_t *ptep, pte_t pte, unsigned int nr, + size_t pgsize) { page_table_check_ptes_set(mm, ptep, pte, nr); __sync_cache_and_tags(pte, nr); @@ -433,10 +434,15 @@ static inline void __set_ptes(struct mm_struct *mm, if (--nr =3D=3D 0) break; ptep++; - pte =3D pte_advance_pfn(pte, 1); + pte =3D pte_advance_pfn(pte, pgsize >> PAGE_SHIFT); } } =20 +#define __set_ptes(mm, addr, ptep, pte, nr) \ + ___set_ptes(mm, addr, ptep, pte, nr, PAGE_SIZE) + +#define set_contptes ___set_ptes + /* * Hugetlb definitions. */ @@ -1825,6 +1831,49 @@ static inline void clear_young_dirty_ptes(struct vm_= area_struct *vma, =20 #endif /* CONFIG_ARM64_CONTPTE */ =20 +static inline bool __hugetlb_valid_size(unsigned long size) +{ + switch (size) { +#ifndef __PAGETABLE_PMD_FOLDED + case PUD_SIZE: + return pud_sect_supported(); +#endif + case CONT_PMD_SIZE: + case PMD_SIZE: + case CONT_PTE_SIZE: + return true; + } + + return false; +} + +static inline int arch_contpte_get_num_contig(pte_t *ptep, + unsigned long size, + size_t *pgsize) +{ + int contig_ptes =3D 1; + + if (pgsize) + *pgsize =3D size; + + switch (size) { + case CONT_PMD_SIZE: + if (pgsize) + *pgsize =3D PMD_SIZE; + contig_ptes =3D CONT_PMDS; + break; + case CONT_PTE_SIZE: + if (pgsize) + *pgsize =3D PAGE_SIZE; + contig_ptes =3D CONT_PTES; + break; + default: + WARN_ON(!__hugetlb_valid_size(size)); + } + + return contig_ptes; +} + #endif /* !__ASSEMBLY__ */ =20 #endif /* __ASM_PGTABLE_H */ diff --git a/arch/arm64/mm/hugetlbpage.c b/arch/arm64/mm/hugetlbpage.c index b3a7fafe8892..60a2bb7575c1 100644 --- a/arch/arm64/mm/hugetlbpage.c +++ b/arch/arm64/mm/hugetlbpage.c @@ -49,22 +49,6 @@ void __init arm64_hugetlb_cma_reserve(void) } #endif /* CONFIG_CMA */ =20 -static bool __hugetlb_valid_size(unsigned long size) -{ - switch (size) { -#ifndef __PAGETABLE_PMD_FOLDED - case PUD_SIZE: - return pud_sect_supported(); -#endif - case CONT_PMD_SIZE: - case PMD_SIZE: - case CONT_PTE_SIZE: - return true; - } - - return false; -} - #ifdef CONFIG_ARCH_ENABLE_HUGEPAGE_MIGRATION bool arch_hugetlb_migration_supported(struct hstate *h) { @@ -98,50 +82,6 @@ static int find_num_contig(struct mm_struct *mm, unsigne= d long addr, return CONT_PTES; } =20 -static inline int num_contig_ptes(unsigned long size, size_t *pgsize) -{ - int contig_ptes =3D 1; - - *pgsize =3D size; - - switch (size) { - case CONT_PMD_SIZE: - *pgsize =3D PMD_SIZE; - contig_ptes =3D CONT_PMDS; - break; - case CONT_PTE_SIZE: - *pgsize =3D PAGE_SIZE; - contig_ptes =3D CONT_PTES; - break; - default: - WARN_ON(!__hugetlb_valid_size(size)); - } - - return contig_ptes; -} - -pte_t huge_ptep_get(struct mm_struct *mm, unsigned long addr, pte_t *ptep) -{ - int ncontig, i; - size_t pgsize; - pte_t orig_pte =3D __ptep_get(ptep); - - if (!pte_present(orig_pte) || !pte_cont(orig_pte)) - return orig_pte; - - ncontig =3D num_contig_ptes(page_size(pte_page(orig_pte)), &pgsize); - for (i =3D 0; i < ncontig; i++, ptep++) { - pte_t pte =3D __ptep_get(ptep); - - if (pte_dirty(pte)) - orig_pte =3D pte_mkdirty(orig_pte); - - if (pte_young(pte)) - orig_pte =3D pte_mkyoung(orig_pte); - } - return orig_pte; -} - /* * Changing some bits of contiguous entries requires us to follow a * Break-Before-Make approach, breaking the whole contiguous set @@ -221,7 +161,7 @@ void set_huge_pte_at(struct mm_struct *mm, unsigned lon= g addr, unsigned long pfn, dpfn; pgprot_t hugeprot; =20 - ncontig =3D num_contig_ptes(sz, &pgsize); + ncontig =3D arch_contpte_get_num_contig(ptep, sz, &pgsize); =20 if (!pte_present(pte)) { for (i =3D 0; i < ncontig; i++, ptep++, addr +=3D pgsize) @@ -382,7 +322,7 @@ void huge_pte_clear(struct mm_struct *mm, unsigned long= addr, int i, ncontig; size_t pgsize; =20 - ncontig =3D num_contig_ptes(sz, &pgsize); + ncontig =3D arch_contpte_get_num_contig(ptep, sz, &pgsize); =20 for (i =3D 0; i < ncontig; i++, addr +=3D pgsize, ptep++) __pte_clear(mm, addr, ptep); @@ -394,7 +334,7 @@ pte_t huge_ptep_get_and_clear(struct mm_struct *mm, uns= igned long addr, int ncontig; size_t pgsize; =20 - ncontig =3D num_contig_ptes(sz, &pgsize); + ncontig =3D arch_contpte_get_num_contig(ptep, sz, &pgsize); return get_clear_contig(mm, addr, ptep, pgsize, ncontig); } =20 diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig index 7612c52e9b1e..2a5b2a9f2816 100644 --- a/arch/riscv/Kconfig +++ b/arch/riscv/Kconfig @@ -79,6 +79,7 @@ config RISCV select ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT if MMU select ARCH_WANT_FRAME_POINTERS select ARCH_WANT_GENERAL_HUGETLB if !RISCV_ISA_SVNAPOT + select ARCH_WANT_GENERAL_HUGETLB_CONTPTE if RISCV_ISA_SVNAPOT select ARCH_WANT_HUGE_PMD_SHARE if 64BIT select ARCH_WANT_LD_ORPHAN_WARN if !XIP_KERNEL select ARCH_WANT_OPTIMIZE_DAX_VMEMMAP diff --git a/arch/riscv/include/asm/hugetlb.h b/arch/riscv/include/asm/huge= tlb.h index 446126497768..69393346ade0 100644 --- a/arch/riscv/include/asm/hugetlb.h +++ b/arch/riscv/include/asm/hugetlb.h @@ -4,6 +4,9 @@ =20 #include #include +#ifdef CONFIG_ARCH_WANT_GENERAL_HUGETLB_CONTPTE +#include +#endif =20 static inline void arch_clear_hugetlb_flags(struct folio *folio) { @@ -44,9 +47,6 @@ int huge_ptep_set_access_flags(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep, pte_t pte, int dirty); =20 -#define __HAVE_ARCH_HUGE_PTEP_GET -pte_t huge_ptep_get(struct mm_struct *mm, unsigned long addr, pte_t *ptep); - pte_t arch_make_huge_pte(pte_t entry, unsigned int shift, vm_flags_t flags= ); #define arch_make_huge_pte arch_make_huge_pte =20 diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pgta= ble.h index 2e62d7e607db..286fe1a32ded 100644 --- a/arch/riscv/include/asm/pgtable.h +++ b/arch/riscv/include/asm/pgtable.h @@ -296,6 +296,8 @@ static inline unsigned long pte_napot(pte_t pte) return pte_val(pte) & _PAGE_NAPOT; } =20 +#define pte_cont pte_napot + #define pte_valid_napot(pte) (pte_present(pte) && pte_napot(pte)) =20 /* @@ -606,6 +608,49 @@ static inline void ___set_ptes(struct mm_struct *mm, u= nsigned long addr, } } =20 +#ifdef CONFIG_RISCV_ISA_SVNAPOT +/* + * Some hugetlb functions can be called on !present ptes, so we must use t= he + * size parameter when it is passed. + */ +static inline int arch_contpte_get_num_contig(pte_t *ptep, unsigned long s= ize, + size_t *pgsize) +{ + unsigned long hugepage_shift; + pte_t __pte; + + if (size) { + if (size >=3D PGDIR_SIZE) + hugepage_shift =3D PGDIR_SHIFT; + else if (size >=3D P4D_SIZE) + hugepage_shift =3D P4D_SHIFT; + else if (size >=3D PUD_SIZE) + hugepage_shift =3D PUD_SHIFT; + else if (size >=3D PMD_SIZE) + hugepage_shift =3D PMD_SHIFT; + else + hugepage_shift =3D PAGE_SHIFT; + } else { + /* + * We must read the raw value of the pte to get the size of + * the mapping + */ + __pte =3D ___ptep_get(ptep); + + /* Make sure __pte is not a swap entry */ + BUG_ON(!pte_valid_napot(__pte)); + + hugepage_shift =3D PAGE_SHIFT; + size =3D napot_cont_size(napot_cont_order(__pte)); + } + + if (pgsize) + *pgsize =3D BIT(hugepage_shift); + + return size >> hugepage_shift; +} +#endif + static inline void pte_clear(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 60b7e738b31a..b9eb6b7b214d 100644 --- a/arch/riscv/mm/hugetlbpage.c +++ b/arch/riscv/mm/hugetlbpage.c @@ -3,30 +3,6 @@ #include =20 #ifdef CONFIG_RISCV_ISA_SVNAPOT -pte_t huge_ptep_get(struct mm_struct *mm, unsigned long addr, pte_t *ptep) -{ - unsigned long pte_num; - int i; - pte_t orig_pte =3D ptep_get(ptep); - - if (!pte_present(orig_pte) || !pte_napot(orig_pte)) - return orig_pte; - - pte_num =3D napot_pte_num(napot_cont_order(orig_pte)); - - for (i =3D 0; i < pte_num; i++, ptep++) { - pte_t pte =3D ptep_get(ptep); - - if (pte_dirty(pte)) - orig_pte =3D pte_mkdirty(orig_pte); - - if (pte_young(pte)) - orig_pte =3D pte_mkyoung(orig_pte); - } - - return orig_pte; -} - pte_t *huge_pte_alloc(struct mm_struct *mm, struct vm_area_struct *vma, unsigned long addr, @@ -215,26 +191,6 @@ static void clear_flush(struct mm_struct *mm, flush_tlb_range(&vma, saddr, addr); } =20 -static int num_contig_ptes_from_size(unsigned long sz, size_t *pgsize) -{ - unsigned long hugepage_shift; - - if (sz >=3D PGDIR_SIZE) - hugepage_shift =3D PGDIR_SHIFT; - else if (sz >=3D P4D_SIZE) - hugepage_shift =3D P4D_SHIFT; - else if (sz >=3D PUD_SIZE) - hugepage_shift =3D PUD_SHIFT; - else if (sz >=3D PMD_SIZE) - hugepage_shift =3D PMD_SHIFT; - else - hugepage_shift =3D PAGE_SHIFT; - - *pgsize =3D 1 << hugepage_shift; - - return sz >> hugepage_shift; -} - /* * When dealing with NAPOT mappings, the privileged specification indicate= s that * "if an update needs to be made, the OS generally should first mark all = of the @@ -252,7 +208,7 @@ void set_huge_pte_at(struct mm_struct *mm, size_t pgsize; int i, pte_num; =20 - pte_num =3D num_contig_ptes_from_size(sz, &pgsize); + pte_num =3D arch_contpte_get_num_contig(ptep, sz, &pgsize); =20 if (!pte_present(pte)) { for (i =3D 0; i < pte_num; i++, ptep++, addr +=3D pgsize) @@ -277,15 +233,13 @@ int huge_ptep_set_access_flags(struct vm_area_struct = *vma, int dirty) { struct mm_struct *mm =3D vma->vm_mm; - unsigned long order; pte_t orig_pte; int pte_num; =20 if (!pte_napot(pte)) return ptep_set_access_flags(vma, addr, ptep, pte, dirty); =20 - order =3D napot_cont_order(pte); - pte_num =3D napot_pte_num(order); + pte_num =3D arch_contpte_get_num_contig(ptep, 0, NULL); orig_pte =3D get_clear_contig_flush(mm, addr, ptep, pte_num); =20 if (pte_dirty(orig_pte)) @@ -303,14 +257,13 @@ pte_t huge_ptep_get_and_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep, unsigned long sz) { - size_t pgsize; pte_t orig_pte =3D ptep_get(ptep); int pte_num; =20 if (!pte_napot(orig_pte)) return ptep_get_and_clear(mm, addr, ptep); =20 - pte_num =3D num_contig_ptes_from_size(sz, &pgsize); + pte_num =3D arch_contpte_get_num_contig(ptep, sz, NULL); =20 return get_clear_contig(mm, addr, ptep, pte_num); } @@ -320,7 +273,6 @@ void huge_ptep_set_wrprotect(struct mm_struct *mm, pte_t *ptep) { pte_t pte =3D ptep_get(ptep); - unsigned long order; pte_t orig_pte; int pte_num; =20 @@ -329,8 +281,7 @@ void huge_ptep_set_wrprotect(struct mm_struct *mm, return; } =20 - order =3D napot_cont_order(pte); - pte_num =3D napot_pte_num(order); + pte_num =3D arch_contpte_get_num_contig(ptep, 0, NULL); orig_pte =3D get_clear_contig_flush(mm, addr, ptep, pte_num); =20 orig_pte =3D pte_wrprotect(orig_pte); @@ -348,7 +299,7 @@ pte_t huge_ptep_clear_flush(struct vm_area_struct *vma, if (!pte_napot(pte)) return ptep_clear_flush(vma, addr, ptep); =20 - pte_num =3D napot_pte_num(napot_cont_order(pte)); + pte_num =3D arch_contpte_get_num_contig(ptep, 0, NULL); =20 return get_clear_contig_flush(vma->vm_mm, addr, ptep, pte_num); } @@ -367,8 +318,7 @@ void huge_pte_clear(struct mm_struct *mm, return; } =20 - pte_num =3D num_contig_ptes_from_size(sz, &pgsize); - + pte_num =3D arch_contpte_get_num_contig(ptep, sz, &pgsize); for (i =3D 0; i < pte_num; i++, addr +=3D pgsize, ptep++) pte_clear(mm, addr, ptep); } diff --git a/include/linux/hugetlb_contpte.h b/include/linux/hugetlb_contpt= e.h new file mode 100644 index 000000000000..2ea17e4fe36b --- /dev/null +++ b/include/linux/hugetlb_contpte.h @@ -0,0 +1,12 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Copyright (C) 2025 Rivos Inc. + */ + +#ifndef _LINUX_HUGETLB_CONTPTE_H +#define _LINUX_HUGETLB_CONTPTE_H + +#define __HAVE_ARCH_HUGE_PTEP_GET +extern pte_t huge_ptep_get(struct mm_struct *mm, unsigned long addr, pte_t= *ptep); + +#endif /* _LINUX_HUGETLB_CONTPTE_H */ diff --git a/mm/Kconfig b/mm/Kconfig index 1b501db06417..f9d3f3d49f3e 100644 --- a/mm/Kconfig +++ b/mm/Kconfig @@ -853,6 +853,9 @@ config NOMMU_INITIAL_TRIM_EXCESS config ARCH_WANT_GENERAL_HUGETLB bool =20 +config ARCH_WANT_GENERAL_HUGETLB_CONTPTE + bool + config ARCH_WANTS_THP_SWAP def_bool n =20 diff --git a/mm/Makefile b/mm/Makefile index 850386a67b3e..76e8b995f551 100644 --- a/mm/Makefile +++ b/mm/Makefile @@ -96,6 +96,7 @@ obj-$(CONFIG_MIGRATION) +=3D migrate.o obj-$(CONFIG_NUMA) +=3D memory-tiers.o obj-$(CONFIG_DEVICE_MIGRATION) +=3D migrate_device.o obj-$(CONFIG_TRANSPARENT_HUGEPAGE) +=3D huge_memory.o khugepaged.o +obj-$(CONFIG_ARCH_WANT_GENERAL_HUGETLB_CONTPTE) +=3D hugetlb_contpte.o obj-$(CONFIG_PAGE_COUNTER) +=3D page_counter.o obj-$(CONFIG_MEMCG_V1) +=3D memcontrol-v1.o obj-$(CONFIG_MEMCG) +=3D memcontrol.o vmpressure.o diff --git a/mm/hugetlb_contpte.c b/mm/hugetlb_contpte.c new file mode 100644 index 000000000000..500d0b96a680 --- /dev/null +++ b/mm/hugetlb_contpte.c @@ -0,0 +1,32 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright 2025 Rivos Inc. + */ + +#include +#include +#include + +pte_t huge_ptep_get(struct mm_struct *mm, unsigned long addr, pte_t *ptep) +{ + int ncontig, i; + pte_t orig_pte =3D __ptep_get(ptep); + + if (!pte_present(orig_pte) || !pte_cont(orig_pte)) + return orig_pte; + + ncontig =3D arch_contpte_get_num_contig(ptep, + page_size(pte_page(orig_pte)), + NULL); + + for (i =3D 0; i < ncontig; i++, ptep++) { + pte_t pte =3D __ptep_get(ptep); + + if (pte_dirty(pte)) + orig_pte =3D pte_mkdirty(orig_pte); + + if (pte_young(pte)) + orig_pte =3D pte_mkyoung(orig_pte); + } + return orig_pte; +} --=20 2.39.2 From nobody Thu Dec 18 04:46:26 2025 Received: from mail-wm1-f54.google.com (mail-wm1-f54.google.com [209.85.128.54]) (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 5C917226D07 for ; Fri, 21 Mar 2025 13:10:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742562648; cv=none; b=ZF5XzWZxCCbFxBjYmIAE11cE/FGFyvIqWfGMZpckiHpr31kaYYsqApW7btVU0D4LGKJAqWQP4KS1tgCSc6YgMZc46xvHGl9shyH19557Mp80pBZm6r5OmyNMThQKf6h3721+kWceLWwPC0kiSx6MSXdrAoqkNb2gtg70+Q0k/GE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742562648; c=relaxed/simple; bh=ETRCJuYPtU2+TR+Hw6l7tljEQEEMTwguCLbd3zIo78E=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=M3qyLEiY7OYPVH8JyU9+QeatarmSL+OwArBUD+vmhgMVhlFOridVX1y5LT3lRjFC9aEjLvfGzMXkuvIz3rwa9S/Ib2z9KdEaDSalJLgMrA6TtglqKa7TzuIoFPb/woOoxW56gk+NCMLVpFK087aCiE/fEJu4DobHg5MNUSKiqP8= 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=T1G+EX2k; arc=none smtp.client-ip=209.85.128.54 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 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="T1G+EX2k" Received: by mail-wm1-f54.google.com with SMTP id 5b1f17b1804b1-43d04ea9d9aso9110225e9.3 for ; Fri, 21 Mar 2025 06:10:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1742562645; x=1743167445; 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=eAv+4atVMvRknGVbTPj58gq6ySa46Mekau0XoGNQASg=; b=T1G+EX2ktMEI6amnKJAbK6I5IjpcZCRaAUrZUwyyBcOimrMY9SKhd7hgqrOwOHz+GO y2kr1CqUFKi1vrCRG/RACIsQ0rur5xsyUz68QqCEtmxgclQG0MwRXTi9JmK1luYv+9g+ tvoBsGqAGC6fsvzDUtxQxHYFJSHD1A9wgDUbbjyFPYpaZ2d7YeaLr5HtdSwGsjPz90jl paJJCvWn51wt+OjkS8nt1MYJjrAgNR//ZyC2Jk8segZvu5X6wTBJQ/XmSj/aNWQUtael /wUscGH+MedcFFB2EEtIiecd+jEri+NTsB27N/cBgYBwGCdQ141NJaLQ/3OndY66ZexV 2PUg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742562645; x=1743167445; 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=eAv+4atVMvRknGVbTPj58gq6ySa46Mekau0XoGNQASg=; b=jDX6B8c3uq52/bXoS/GOagfn57ap2+Z/5D+D1Y26YFeF8c4Jp6o9caUfpR0SWyC1At F9dC/ZDoARM9rePUJivvSuaJMGKHsG6aqXM8OSv/NtHSe2OcJoHILaKwNh7cjYx5JFVi uYpfzqcteNJjWtggT22S902+94oEOwsheomqy0hUZDNWP8KDQkPTSkyXF1jOY9jFm4s1 sK1ky1WeffWGxkTyyV9P0qOcvu2ZQqPdX/lbcMuwVAVMiwIecK8SvSUKapaKPc7frKsN nZCJVyqmY7CMpcVcaY7aJ8QbxwiK31WsKoLvZGNZi6bJA5vXQsqU6yXNgGQfUicS6kHI v4og== X-Forwarded-Encrypted: i=1; AJvYcCVBGgfPr6Mje332WFZif8cDR3tcHBdpY6n1UebzfpBvdZUg1yrtXL9XLdnAW6YrUafUTDaQODFvYEHI4WQ=@vger.kernel.org X-Gm-Message-State: AOJu0YyppNxUO6tZIAVzZ0hvRt0aRHeT17wMdcSyrN3hj3PV7Y88xLui pgtwcc93GSnP/c2tSzCophlrlrsIDmnC5QaUTjynGZMPuHQjKkZDyE8yOBGBKX8= X-Gm-Gg: ASbGncua8YXSYlY+apktO6spnLRFBltbOaHVOgby7ybRcv0j5KQiLRo4hCAu6XN1n77 yQhw+LQsmEdHqesW/XU0TjaZz8PeIL+xEAe61/7bMgk0PGGg6eXDDIgh9KVORqROLdM2+atqz6s KDbmqv/l1EJpwBzgoasF0lmVxZPGq+gT5bEktDJb+SSo48MzExPolXgPyg3hA/gTRAnzhKLMQVN NDxMCve/aZu21d0c2xaJxmuhVk7xCATA9qe2X+lQc8Sk3bPIhxP8YsDi2OdFT/+MfWaMap1cptK qYMYEt2mWc9fbuk7cJBSodf1/tq7F3EEL8rEIA7oeS8P64moxMOG6yDLdVPUKs31rGrEtQ== X-Google-Smtp-Source: AGHT+IFFzxLTl7dkoWPQa6CIZER6lpnHzrVNqhIgyBgY3Y3nEtgERu0W1NS2+ffacirYXfcMWjF6NQ== X-Received: by 2002:a05:600c:83cf:b0:439:91dd:cf9c with SMTP id 5b1f17b1804b1-43d509ec70cmr37202275e9.10.1742562644586; Fri, 21 Mar 2025 06:10:44 -0700 (PDT) Received: from alex-rivos.ba.rivosinc.com ([2001:861:3382:ef90:3d12:52fe:c1cc:c94]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-43d42b8fcd4sm47982695e9.1.2025.03.21.06.10.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 21 Mar 2025 06:10:44 -0700 (PDT) From: Alexandre Ghiti To: Catalin Marinas , Will Deacon , Ryan Roberts , Mark Rutland , Matthew Wilcox , Paul Walmsley , Palmer Dabbelt , Alexandre Ghiti , 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 v5 4/9] mm: Use common set_huge_pte_at() function for riscv/arm64 Date: Fri, 21 Mar 2025 14:06:30 +0100 Message-Id: <20250321130635.227011-5-alexghiti@rivosinc.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20250321130635.227011-1-alexghiti@rivosinc.com> References: <20250321130635.227011-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: quoted-printable Content-Type: text/plain; charset="utf-8" After some adjustments, both architectures have the same implementation so move it to the generic code. Signed-off-by: Alexandre Ghiti --- arch/arm64/include/asm/hugetlb.h | 3 -- arch/arm64/mm/hugetlbpage.c | 56 -------------------------------- arch/riscv/include/asm/hugetlb.h | 5 --- arch/riscv/include/asm/pgtable.h | 13 ++++---- arch/riscv/mm/hugetlbpage.c | 50 ---------------------------- include/linux/hugetlb_contpte.h | 5 +++ mm/hugetlb_contpte.c | 56 ++++++++++++++++++++++++++++++++ 7 files changed, 68 insertions(+), 120 deletions(-) diff --git a/arch/arm64/include/asm/hugetlb.h b/arch/arm64/include/asm/huge= tlb.h index 0604e01dca97..cfdc04e11585 100644 --- a/arch/arm64/include/asm/hugetlb.h +++ b/arch/arm64/include/asm/hugetlb.h @@ -35,9 +35,6 @@ static inline void arch_clear_hugetlb_flags(struct folio = *folio) =20 pte_t arch_make_huge_pte(pte_t entry, unsigned int shift, vm_flags_t flags= ); #define arch_make_huge_pte arch_make_huge_pte -#define __HAVE_ARCH_HUGE_SET_HUGE_PTE_AT -extern void set_huge_pte_at(struct mm_struct *mm, unsigned long addr, - pte_t *ptep, pte_t pte, unsigned long sz); #define __HAVE_ARCH_HUGE_PTEP_SET_ACCESS_FLAGS extern int huge_ptep_set_access_flags(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep, diff --git a/arch/arm64/mm/hugetlbpage.c b/arch/arm64/mm/hugetlbpage.c index 60a2bb7575c1..6feb90ed2e7d 100644 --- a/arch/arm64/mm/hugetlbpage.c +++ b/arch/arm64/mm/hugetlbpage.c @@ -128,62 +128,6 @@ static pte_t get_clear_contig_flush(struct mm_struct *= mm, return orig_pte; } =20 -/* - * Changing some bits of contiguous entries requires us to follow a - * Break-Before-Make approach, breaking the whole contiguous set - * before we can change any entries. See ARM DDI 0487A.k_iss10775, - * "Misprogramming of the Contiguous bit", page D4-1762. - * - * This helper performs the break step for use cases where the - * original pte is not needed. - */ -static void clear_flush(struct mm_struct *mm, - unsigned long addr, - pte_t *ptep, - unsigned long pgsize, - unsigned long ncontig) -{ - struct vm_area_struct vma =3D TLB_FLUSH_VMA(mm, 0); - unsigned long i, saddr =3D addr; - - for (i =3D 0; i < ncontig; i++, addr +=3D pgsize, ptep++) - __ptep_get_and_clear(mm, addr, ptep); - - flush_tlb_range(&vma, saddr, addr); -} - -void set_huge_pte_at(struct mm_struct *mm, unsigned long addr, - pte_t *ptep, pte_t pte, unsigned long sz) -{ - size_t pgsize; - int i; - int ncontig; - unsigned long pfn, dpfn; - pgprot_t hugeprot; - - ncontig =3D arch_contpte_get_num_contig(ptep, sz, &pgsize); - - if (!pte_present(pte)) { - for (i =3D 0; i < ncontig; i++, ptep++, addr +=3D pgsize) - __set_ptes(mm, addr, ptep, pte, 1); - return; - } - - if (!pte_cont(pte)) { - __set_ptes(mm, addr, ptep, pte, 1); - return; - } - - pfn =3D pte_pfn(pte); - dpfn =3D pgsize >> PAGE_SHIFT; - hugeprot =3D pte_pgprot(pte); - - clear_flush(mm, addr, ptep, pgsize, ncontig); - - for (i =3D 0; i < ncontig; i++, ptep++, addr +=3D pgsize, pfn +=3D dpfn) - __set_ptes(mm, addr, ptep, pfn_pte(pfn, hugeprot), 1); -} - pte_t *huge_pte_alloc(struct mm_struct *mm, struct vm_area_struct *vma, unsigned long addr, unsigned long sz) { diff --git a/arch/riscv/include/asm/hugetlb.h b/arch/riscv/include/asm/huge= tlb.h index 69393346ade0..7049a17b819d 100644 --- a/arch/riscv/include/asm/hugetlb.h +++ b/arch/riscv/include/asm/hugetlb.h @@ -24,11 +24,6 @@ bool arch_hugetlb_migration_supported(struct hstate *h); void huge_pte_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep, unsigned long sz); =20 -#define __HAVE_ARCH_HUGE_SET_HUGE_PTE_AT -void set_huge_pte_at(struct mm_struct *mm, - unsigned long addr, pte_t *ptep, pte_t pte, - unsigned long sz); - #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, diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pgta= ble.h index 286fe1a32ded..5b34b3c9c0f9 100644 --- a/arch/riscv/include/asm/pgtable.h +++ b/arch/riscv/include/asm/pgtable.h @@ -715,9 +715,8 @@ static inline pte_t pte_napot_clear_pfn(pte_t *ptep, pt= e_t pte) return pte; } =20 -#define __HAVE_ARCH_PTEP_GET_AND_CLEAR -static inline pte_t ptep_get_and_clear(struct mm_struct *mm, - unsigned long address, pte_t *ptep) +static inline pte_t __ptep_get_and_clear(struct mm_struct *mm, + unsigned long address, pte_t *ptep) { pte_t pte =3D __pte(atomic_long_xchg((atomic_long_t *)ptep, 0)); =20 @@ -775,9 +774,8 @@ static inline void __set_ptes(struct mm_struct *mm, uns= igned long addr, #define set_ptes ___set_ptes #define __set_ptes ___set_ptes =20 -#define __HAVE_ARCH_PTEP_GET_AND_CLEAR -static inline pte_t ptep_get_and_clear(struct mm_struct *mm, - unsigned long address, pte_t *ptep) +static inline pte_t __ptep_get_and_clear(struct mm_struct *mm, + unsigned long address, pte_t *ptep) { pte_t pte =3D __pte(atomic_long_xchg((atomic_long_t *)ptep, 0)); =20 @@ -787,6 +785,9 @@ static inline pte_t ptep_get_and_clear(struct mm_struct= *mm, } #endif /* CONFIG_RISCV_ISA_SVNAPOT */ =20 +#define __HAVE_ARCH_PTEP_GET_AND_CLEAR +#define ptep_get_and_clear __ptep_get_and_clear + #define pgprot_nx pgprot_nx static inline pgprot_t pgprot_nx(pgprot_t _prot) { diff --git a/arch/riscv/mm/hugetlbpage.c b/arch/riscv/mm/hugetlbpage.c index b9eb6b7b214d..75faeacc8138 100644 --- a/arch/riscv/mm/hugetlbpage.c +++ b/arch/riscv/mm/hugetlbpage.c @@ -176,56 +176,6 @@ pte_t arch_make_huge_pte(pte_t entry, unsigned int shi= ft, vm_flags_t flags) return entry; } =20 -static void clear_flush(struct mm_struct *mm, - unsigned long addr, - pte_t *ptep, - unsigned long pgsize, - unsigned long ncontig) -{ - struct vm_area_struct vma =3D TLB_FLUSH_VMA(mm, 0); - unsigned long i, saddr =3D addr; - - for (i =3D 0; i < ncontig; i++, addr +=3D pgsize, ptep++) - ptep_get_and_clear(mm, addr, ptep); - - flush_tlb_range(&vma, saddr, addr); -} - -/* - * When dealing with NAPOT mappings, the privileged specification indicate= s that - * "if an update needs to be made, the OS generally should first mark all = of the - * PTEs invalid, then issue SFENCE.VMA instruction(s) covering all 4 KiB r= egions - * within the range, [...] then update the PTE(s), as described in Section - * 4.2.1.". That's the equivalent of the Break-Before-Make approach used by - * arm64. - */ -void set_huge_pte_at(struct mm_struct *mm, - unsigned long addr, - pte_t *ptep, - pte_t pte, - unsigned long sz) -{ - size_t pgsize; - int i, pte_num; - - pte_num =3D arch_contpte_get_num_contig(ptep, sz, &pgsize); - - if (!pte_present(pte)) { - for (i =3D 0; i < pte_num; i++, ptep++, addr +=3D pgsize) - set_ptes(mm, addr, ptep, pte, 1); - return; - } - - if (!pte_napot(pte)) { - set_ptes(mm, addr, ptep, pte, 1); - return; - } - - clear_flush(mm, addr, ptep, pgsize, pte_num); - - set_ptes(mm, addr, ptep, pte, pte_num); -} - int huge_ptep_set_access_flags(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep, diff --git a/include/linux/hugetlb_contpte.h b/include/linux/hugetlb_contpt= e.h index 2ea17e4fe36b..135b68bd09ca 100644 --- a/include/linux/hugetlb_contpte.h +++ b/include/linux/hugetlb_contpte.h @@ -9,4 +9,9 @@ #define __HAVE_ARCH_HUGE_PTEP_GET extern pte_t huge_ptep_get(struct mm_struct *mm, unsigned long addr, pte_t= *ptep); =20 +#define __HAVE_ARCH_HUGE_SET_HUGE_PTE_AT +extern void set_huge_pte_at(struct mm_struct *mm, + unsigned long addr, pte_t *ptep, pte_t pte, + unsigned long sz); + #endif /* _LINUX_HUGETLB_CONTPTE_H */ diff --git a/mm/hugetlb_contpte.c b/mm/hugetlb_contpte.c index 500d0b96a680..cbf93ffcd882 100644 --- a/mm/hugetlb_contpte.c +++ b/mm/hugetlb_contpte.c @@ -30,3 +30,59 @@ pte_t huge_ptep_get(struct mm_struct *mm, unsigned long = addr, pte_t *ptep) } return orig_pte; } + +/* + * ARM64: Changing some bits of contiguous entries requires us to follow a + * Break-Before-Make approach, breaking the whole contiguous set + * before we can change any entries. See ARM DDI 0487A.k_iss10775, + * "Misprogramming of the Contiguous bit", page D4-1762. + * + * RISCV: When dealing with NAPOT mappings, the privileged specification + * indicates that "if an update needs to be made, the OS generally should = first + * mark all of the PTEs invalid, then issue SFENCE.VMA instruction(s) cove= ring + * all 4 KiB regions within the range, [...] then update the PTE(s), as + * described in Section 4.2.1.". That's the equivalent of the Break-Before= -Make + * approach used by arm64. + * + * This helper performs the break step for use cases where the + * original pte is not needed. + */ +static void clear_flush(struct mm_struct *mm, + unsigned long addr, + pte_t *ptep, + unsigned long pgsize, + unsigned long ncontig) +{ + struct vm_area_struct vma =3D TLB_FLUSH_VMA(mm, 0); + unsigned long i, saddr =3D addr; + + for (i =3D 0; i < ncontig; i++, addr +=3D pgsize, ptep++) + __ptep_get_and_clear(mm, addr, ptep); + + flush_tlb_range(&vma, saddr, addr); +} + +void set_huge_pte_at(struct mm_struct *mm, unsigned long addr, + pte_t *ptep, pte_t pte, unsigned long sz) +{ + size_t pgsize; + int i; + int ncontig; + + ncontig =3D arch_contpte_get_num_contig(ptep, sz, &pgsize); + + if (!pte_present(pte)) { + for (i =3D 0; i < ncontig; i++, ptep++, addr +=3D pgsize) + __set_ptes(mm, addr, ptep, pte, 1); + return; + } + + if (!pte_cont(pte)) { + __set_ptes(mm, addr, ptep, pte, 1); + return; + } + + clear_flush(mm, addr, ptep, pgsize, ncontig); + + set_contptes(mm, addr, ptep, pte, ncontig, pgsize); +} --=20 2.39.2 From nobody Thu Dec 18 04:46:26 2025 Received: from mail-wr1-f53.google.com (mail-wr1-f53.google.com [209.85.221.53]) (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 C9C4D224237 for ; Fri, 21 Mar 2025 13:11:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742562710; cv=none; b=aBvUvM47aZIeX9E9b0nZgDM4RsxL/SZPd+hfOGu+T2eTuTPDcQByCG/0/bwrDFb3ww8UAhMKkbccMO4wcygJPr+zANwko4xts2a+eAHESE2KQgujXdc0fKQwZjALNLufRj/iAWVSQmJbHwmyOoecPb7qd0xME5npRGaBZU2XUQQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742562710; c=relaxed/simple; bh=idmTROmFvJl3glK8KRym15s6eskRmivlfopnYW7zJZ0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=j2dJSLVLALday4vUFRHBdFugv/CudisortM65OEcfInN3iLyHtCRCrowJvr245wwVKJ7oiuxeMd2Xq27oU9Jfs1iJWC2McDXShzljzyOfA9+uDZ2PLs9UXsO3EykRb8bPuEowx5y2lrj9N6R3eahv+OQ1nE1luc4QSBmFrBU3CY= 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=XGzmPQ5+; arc=none smtp.client-ip=209.85.221.53 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 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="XGzmPQ5+" Received: by mail-wr1-f53.google.com with SMTP id ffacd0b85a97d-39149bccb69so1829419f8f.2 for ; Fri, 21 Mar 2025 06:11:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1742562706; x=1743167506; 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=g+Ba9lH6i+4JHX31VX+vvn1rf15Jq1WxTsiq92NtkJo=; b=XGzmPQ5+OeR6O0YAqNvVlfi3AXbKve3yKP0grCJN2dmEBm/CD0Z+Lfv8cInnAJgvAt 4v4q078SNhZBOigAwuRRTRL1CK0+K/dyWJQz8eamI8NMW2VFiJLeg4o/877FKxrVQn/U X4Pa8UWIuJC6GsrLocWqyYM+aN/tf68/3ZENBrfWjQKN9zcD9iPL0pJOOeThxdfzI6eO sTqvrnGGJj8LRnGVpf8QAPr9sJgiVMt/eYth+SdmNmX5OaduuIYeImwwiZP+k4AtQmr3 iS3HrkkV36BYwL9+SdyTTNB2Mx01nX+PwJC6WrDgVdcDItNV4kbg+hq+xqZ//b90QtkE jxzw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742562706; x=1743167506; 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=g+Ba9lH6i+4JHX31VX+vvn1rf15Jq1WxTsiq92NtkJo=; b=JEmIyqud/VWY8LZkpA055xc+tOtSkJ6nh8bal49tJBgXiwqjTOvvSeqK7jETvdOcgS 7pf+Ccek1RtpY00FpV6G8tBrVSO0R8/dix7DjtNnUqJ+/Y00gHi4ObixE1BtUdBe76eo 0/hVo3xdc74UqkEeeqLerjGot+ffDUUhpuHgl9Fh5OZfHxxxH85WdMvtxUWowrRRzSvg YnyUqjhOu12D2xC2pRKEQT5y/rLBxBcLWI0B45ojaKrdjVDyb8cGLJv0U/vqsvjIQDPU 0X6e8qPgInVxsdM4en5/9edW8eV9dxmkF2jiOu8rz39bsrP7eQrcLG3isvgmAADD62Ax aRKg== X-Forwarded-Encrypted: i=1; AJvYcCVsQKL6PmwNSH0dexIqwmjecYMMhTpS+T08YjYvpVf6z0awoQRMNawysqJL8i9NxGNGztMxu+57zRaK/as=@vger.kernel.org X-Gm-Message-State: AOJu0Yw8ZNC+AjmJbSC2yoGfbh42vJtrwAoSI4WMPTu2kuOMpccvwwoF 3JK2UWm6VvAvsi0LW4aj69hGPLh7LyvyEhZ9Jlo59vC7NBdcITSolGuK7u+8Ilw= X-Gm-Gg: ASbGncs7LIkrI/ugbYir8HnTiHTspCyM+5iplVkizrX5e/NBIe1VrwP7PYZ/JkdQk9v 79jSVPwvdFHkFm0tLq0b53RFpB95pjWjdpCU3bYjJn+NKdnB6/JYtubJtqNnGlR0A15w5a/QSHJ cop3r+CHNUAlj09xAL6JolaTZme+iB0XNVCG5mQ6VyQEnT+bUMZboAUzt9GLNlKQ2z9TGN5VgjR NlJZnLekK+nlwGDwtwkI7mCOu0pyXwK2idmVAKQZlrt056EHnnOM/BO5yZTOe1RxjaluK+YpWrr hfQ7449SmRFqT8ch/IFE5DjT1WFpitsKrjxajYWgdl9o/0S2qCzppygo2LNDHzQsNTZCFQ== X-Google-Smtp-Source: AGHT+IEGZPDoYAPQGYmkPYx5aU5RVlINcZRG7cNrGB3gR0sMYyiWL8XPHTT0J17da9GbtboJpKfvoQ== X-Received: by 2002:a05:6000:154a:b0:38f:3224:660b with SMTP id ffacd0b85a97d-3997f90a69bmr2874098f8f.22.1742562705888; Fri, 21 Mar 2025 06:11:45 -0700 (PDT) Received: from alex-rivos.ba.rivosinc.com ([2001:861:3382:ef90:3d12:52fe:c1cc:c94]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3997f9b26a6sm2310275f8f.44.2025.03.21.06.11.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 21 Mar 2025 06:11:45 -0700 (PDT) From: Alexandre Ghiti To: Catalin Marinas , Will Deacon , Ryan Roberts , Mark Rutland , Matthew Wilcox , Paul Walmsley , Palmer Dabbelt , Alexandre Ghiti , 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 v5 5/9] mm: Use common huge_pte_clear() function for riscv/arm64 Date: Fri, 21 Mar 2025 14:06:31 +0100 Message-Id: <20250321130635.227011-6-alexghiti@rivosinc.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20250321130635.227011-1-alexghiti@rivosinc.com> References: <20250321130635.227011-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: quoted-printable Content-Type: text/plain; charset="utf-8" Both architectures have the same implementation so move it to generic code. Signed-off-by: Alexandre Ghiti --- arch/arm64/include/asm/hugetlb.h | 3 --- arch/arm64/mm/hugetlbpage.c | 12 ------------ arch/riscv/include/asm/hugetlb.h | 4 ---- arch/riscv/include/asm/pgtable.h | 5 +++-- arch/riscv/mm/hugetlbpage.c | 19 ------------------- include/linux/hugetlb_contpte.h | 4 ++++ mm/hugetlb_contpte.c | 12 ++++++++++++ 7 files changed, 19 insertions(+), 40 deletions(-) diff --git a/arch/arm64/include/asm/hugetlb.h b/arch/arm64/include/asm/huge= tlb.h index cfdc04e11585..ed75631ad63c 100644 --- a/arch/arm64/include/asm/hugetlb.h +++ b/arch/arm64/include/asm/hugetlb.h @@ -48,9 +48,6 @@ extern void huge_ptep_set_wrprotect(struct mm_struct *mm, #define __HAVE_ARCH_HUGE_PTEP_CLEAR_FLUSH extern pte_t huge_ptep_clear_flush(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep); -#define __HAVE_ARCH_HUGE_PTE_CLEAR -extern void huge_pte_clear(struct mm_struct *mm, unsigned long addr, - pte_t *ptep, unsigned long sz); =20 void __init arm64_hugetlb_cma_reserve(void); =20 diff --git a/arch/arm64/mm/hugetlbpage.c b/arch/arm64/mm/hugetlbpage.c index 6feb90ed2e7d..99728b02a3ca 100644 --- a/arch/arm64/mm/hugetlbpage.c +++ b/arch/arm64/mm/hugetlbpage.c @@ -260,18 +260,6 @@ pte_t arch_make_huge_pte(pte_t entry, unsigned int shi= ft, vm_flags_t flags) return entry; } =20 -void huge_pte_clear(struct mm_struct *mm, unsigned long addr, - pte_t *ptep, unsigned long sz) -{ - int i, ncontig; - size_t pgsize; - - ncontig =3D arch_contpte_get_num_contig(ptep, sz, &pgsize); - - for (i =3D 0; i < ncontig; i++, addr +=3D pgsize, ptep++) - __pte_clear(mm, addr, ptep); -} - pte_t huge_ptep_get_and_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep, unsigned long sz) { diff --git a/arch/riscv/include/asm/hugetlb.h b/arch/riscv/include/asm/huge= tlb.h index 7049a17b819d..467bc30c2153 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 =20 #ifdef CONFIG_RISCV_ISA_SVNAPOT -#define __HAVE_ARCH_HUGE_PTE_CLEAR -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 pte_t huge_ptep_get_and_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep, diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pgta= ble.h index 5b34b3c9c0f9..72d3592454d3 100644 --- a/arch/riscv/include/asm/pgtable.h +++ b/arch/riscv/include/asm/pgtable.h @@ -651,8 +651,8 @@ static inline int arch_contpte_get_num_contig(pte_t *pt= ep, unsigned long size, } #endif =20 -static inline void pte_clear(struct mm_struct *mm, - unsigned long addr, pte_t *ptep) +static inline void __pte_clear(struct mm_struct *mm, + unsigned long addr, pte_t *ptep) { __set_pte_at(mm, ptep, __pte(0)); } @@ -787,6 +787,7 @@ static inline pte_t __ptep_get_and_clear(struct mm_stru= ct *mm, =20 #define __HAVE_ARCH_PTEP_GET_AND_CLEAR #define ptep_get_and_clear __ptep_get_and_clear +#define pte_clear __pte_clear =20 #define pgprot_nx pgprot_nx static inline pgprot_t pgprot_nx(pgprot_t _prot) diff --git a/arch/riscv/mm/hugetlbpage.c b/arch/riscv/mm/hugetlbpage.c index 75faeacc8138..fe82284c3dc4 100644 --- a/arch/riscv/mm/hugetlbpage.c +++ b/arch/riscv/mm/hugetlbpage.c @@ -254,25 +254,6 @@ pte_t huge_ptep_clear_flush(struct vm_area_struct *vma, return get_clear_contig_flush(vma->vm_mm, addr, ptep, pte_num); } =20 -void huge_pte_clear(struct mm_struct *mm, - unsigned long addr, - pte_t *ptep, - unsigned long sz) -{ - size_t pgsize; - pte_t pte =3D ptep_get(ptep); - int i, pte_num; - - if (!pte_napot(pte)) { - pte_clear(mm, addr, ptep); - return; - } - - pte_num =3D arch_contpte_get_num_contig(ptep, sz, &pgsize); - for (i =3D 0; i < pte_num; i++, addr +=3D pgsize, ptep++) - pte_clear(mm, addr, ptep); -} - static bool is_napot_size(unsigned long size) { unsigned long order; diff --git a/include/linux/hugetlb_contpte.h b/include/linux/hugetlb_contpt= e.h index 135b68bd09ca..e6aa9befa78c 100644 --- a/include/linux/hugetlb_contpte.h +++ b/include/linux/hugetlb_contpte.h @@ -14,4 +14,8 @@ extern void set_huge_pte_at(struct mm_struct *mm, unsigned long addr, pte_t *ptep, pte_t pte, unsigned long sz); =20 +#define __HAVE_ARCH_HUGE_PTE_CLEAR +extern void huge_pte_clear(struct mm_struct *mm, unsigned long addr, + pte_t *ptep, unsigned long sz); + #endif /* _LINUX_HUGETLB_CONTPTE_H */ diff --git a/mm/hugetlb_contpte.c b/mm/hugetlb_contpte.c index cbf93ffcd882..e881b302dd63 100644 --- a/mm/hugetlb_contpte.c +++ b/mm/hugetlb_contpte.c @@ -86,3 +86,15 @@ void set_huge_pte_at(struct mm_struct *mm, unsigned long= addr, =20 set_contptes(mm, addr, ptep, pte, ncontig, pgsize); } + +void huge_pte_clear(struct mm_struct *mm, unsigned long addr, + pte_t *ptep, unsigned long sz) +{ + int i, ncontig; + size_t pgsize; + + ncontig =3D arch_contpte_get_num_contig(ptep, sz, &pgsize); + + for (i =3D 0; i < ncontig; i++, addr +=3D pgsize, ptep++) + __pte_clear(mm, addr, ptep); +} --=20 2.39.2 From nobody Thu Dec 18 04:46:26 2025 Received: from mail-wm1-f46.google.com (mail-wm1-f46.google.com [209.85.128.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 60E1E1E376E for ; Fri, 21 Mar 2025 13:12:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742562771; cv=none; b=B6cYHt2RfqIFgG+w6QTchsT7DnoyfepQD4hL1squ7okXfyZ33c3gkOJfzyflqTK2GwVW6GPBJrrxximbh+gGgSg9vea8f2sWNgaDAEb9Dd3BpN2WtPC/fKp5T9rb6XUjXRMl57ocpaMs0UCF6eoWHINY3JtIfMSIyqe8AzXCheI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742562771; c=relaxed/simple; bh=N/5p1FcoIMzGJaMT97eHIHTKEwVr8Hfl1l+4m+sR1mw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=rIWaYcJ/bo8f4kkF/HS6FQ61l1gu37/X/86K0+5vyZta3BpacDfsd/jQfToQs0kN/r6eiXtaxSg9ynJh9ojqyU4Hiu6GLagf6kfszjABXvn1h8YDj377v+PNGmR7u2rxTKlMXU5IJeuwRPus12KpHIy6wc8Bw8Q3dGhstc29niQ= 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=uqeZoXIb; arc=none smtp.client-ip=209.85.128.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 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="uqeZoXIb" Received: by mail-wm1-f46.google.com with SMTP id 5b1f17b1804b1-43ce71582e9so13627505e9.1 for ; Fri, 21 Mar 2025 06:12:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1742562768; x=1743167568; 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=tW5CZGKlc6dgOhT2HYLkHYDVp73Jiwu1Hc2Rif5lDWM=; b=uqeZoXIbYta/VfL5ZA/3uL7FjhYNRhcXDB1IWMJDe2G+HUL5LD1NXZ5Rz15NBxzjWl KSBXwoce+jj3XTXw4ko0fbbXhs/c+6mmpbjLmRtydIqPSwHifaAu/RmXApR5yf5nJfP8 HEEMv7+W5ZfF2Mc1S+KjmjSbH+ylgGwghrv5cnkFxLsxJqWLmFnWfWIgvn0gduOk9LTI NYj42hwngqKqfZbLnY6uKrE7DkaJS3S3ZTCJ2KdZ6iCIN6vq6kMxrwUXyOZPDHeWxH0A p5w/Ru3pcuQHiMEDOZJdRB3AzlJMZPA9zRZhCaSH2zXBKzz2FRwxyB2TaEcTrtgGgebR qZTQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742562768; x=1743167568; 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=tW5CZGKlc6dgOhT2HYLkHYDVp73Jiwu1Hc2Rif5lDWM=; b=mPXsJazoJ2QU6WnYNmW01cL2PnaHU2WfQcr8QDpU8KsfhMFK+6+2qGB2wsfH2AttjH 9otow4iUwI7ymZlevZLvPtz/GuaVoulrHb46oGGcKm8E8GPFvpLDBu5dQ3dqYDk3LGaL y70UIqy5FettAeMTNH2o188NkiPrqeLDD8jFfBlIMRSted4WUfYLBn7Uh38/4pFak9JO HnPZ4mHkk28i3bS9+HKCeIrPyu0G0jDWJQbO/Bln2s9qNRmi8mbFs59KKJScVh8zwKdu NsoQUG+Mnx5nYX1MLdSuhUELjpNkKLOf4lwAXwFlEFWQmIzIoFFlT3a7faokTUBj/Q0W y+Og== X-Forwarded-Encrypted: i=1; AJvYcCUp2RH2UlNy76M91shaYwdYDPflL92ETT04GSmpd4T6E8z9Sh7+PF2aeiezAeCRZu8qxb3E9Q+73O0BaEw=@vger.kernel.org X-Gm-Message-State: AOJu0Yxbb4NsO68tVNK9Tmlsz6hFhNxl/uZNuMgN1C3+geuCwRnxqVmP sUd4xknJbffLfFNEuRAi0U3YniT9jsdp9vs3Oo/dGG4UE2c0YPw1vUr3kGfuwnw= X-Gm-Gg: ASbGncuUznIK/hplZWYLIm8+RwmS5XausKVCXNqc8hFGFJFaVFFBJd5qwaLik5hzXk0 0sm9I02Fk3nfpBzFeDOKr7Y5MSSfPX4GFNNyW5/RlU8usSG4PqP8L7yBseSLu9nymnzyu9io3Is XRdfGGhO5I8z7BPE0Ax5wUfseLd1g6miI1ZInXO9FFUW8PBEFPMhSqkT85p3ERrhhMTFoOm8FI3 +4nOdCWnj+Yca7oobRqvQQPXilVP9x1uxRVKNg9hGUWfaxRpwaOXMHOk7f3YKf1O3abyuPHXVL4 xfGqDyVGrj+QfdebfCEAUcbZ0qg0teEkVc3eE6JTUne/l12+9uNjNTMHuaKemAWS1q+GRQ== X-Google-Smtp-Source: AGHT+IEUtROIBbu/cy/pImYWjl+q4lRngYo9z7idT6KT9+6QFPXCkP6BZJtyCAPZK3CSSAem4Gc0LA== X-Received: by 2002:a5d:64af:0:b0:391:2db0:2961 with SMTP id ffacd0b85a97d-3997f932efcmr3394631f8f.38.1742562767493; Fri, 21 Mar 2025 06:12:47 -0700 (PDT) Received: from alex-rivos.ba.rivosinc.com ([2001:861:3382:ef90:3d12:52fe:c1cc:c94]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3997f9e66c7sm2356167f8f.79.2025.03.21.06.12.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 21 Mar 2025 06:12:47 -0700 (PDT) From: Alexandre Ghiti To: Catalin Marinas , Will Deacon , Ryan Roberts , Mark Rutland , Matthew Wilcox , Paul Walmsley , Palmer Dabbelt , Alexandre Ghiti , 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 v5 6/9] mm: Use common huge_ptep_get_and_clear() function for riscv/arm64 Date: Fri, 21 Mar 2025 14:06:32 +0100 Message-Id: <20250321130635.227011-7-alexghiti@rivosinc.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20250321130635.227011-1-alexghiti@rivosinc.com> References: <20250321130635.227011-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: quoted-printable Content-Type: text/plain; charset="utf-8" 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/mm/hugetlbpage.c | 10 ------- arch/riscv/include/asm/hugetlb.h | 5 ---- arch/riscv/mm/hugetlbpage.c | 15 ---------- include/linux/hugetlb_contpte.h | 5 ++++ mm/hugetlb_contpte.c | 51 ++++++++++++++++++++++++++++++++ 6 files changed, 56 insertions(+), 33 deletions(-) diff --git a/arch/arm64/include/asm/hugetlb.h b/arch/arm64/include/asm/huge= tlb.h index ed75631ad63c..9b1c25775bea 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 a= ddr, - pte_t *ptep, unsigned long sz); #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/mm/hugetlbpage.c b/arch/arm64/mm/hugetlbpage.c index 99728b02a3ca..62a66ce2b2fe 100644 --- a/arch/arm64/mm/hugetlbpage.c +++ b/arch/arm64/mm/hugetlbpage.c @@ -260,16 +260,6 @@ pte_t arch_make_huge_pte(pte_t entry, unsigned int shi= ft, vm_flags_t flags) return entry; } =20 -pte_t huge_ptep_get_and_clear(struct mm_struct *mm, unsigned long addr, - pte_t *ptep, unsigned long sz) -{ - int ncontig; - size_t pgsize; - - ncontig =3D arch_contpte_get_num_contig(ptep, sz, &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/huge= tlb.h index 467bc30c2153..0fbb6b19df79 100644 --- a/arch/riscv/include/asm/hugetlb.h +++ b/arch/riscv/include/asm/hugetlb.h @@ -20,11 +20,6 @@ bool arch_hugetlb_migration_supported(struct hstate *h); #endif =20 #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, - unsigned long sz); - #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/mm/hugetlbpage.c b/arch/riscv/mm/hugetlbpage.c index fe82284c3dc4..87168123d4a2 100644 --- a/arch/riscv/mm/hugetlbpage.c +++ b/arch/riscv/mm/hugetlbpage.c @@ -203,21 +203,6 @@ int huge_ptep_set_access_flags(struct vm_area_struct *= vma, return true; } =20 -pte_t huge_ptep_get_and_clear(struct mm_struct *mm, - unsigned long addr, - pte_t *ptep, unsigned long sz) -{ - pte_t orig_pte =3D ptep_get(ptep); - int pte_num; - - if (!pte_napot(orig_pte)) - return ptep_get_and_clear(mm, addr, ptep); - - pte_num =3D arch_contpte_get_num_contig(ptep, sz, 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) diff --git a/include/linux/hugetlb_contpte.h b/include/linux/hugetlb_contpt= e.h index e6aa9befa78c..1c8f46ff95ea 100644 --- a/include/linux/hugetlb_contpte.h +++ b/include/linux/hugetlb_contpte.h @@ -18,4 +18,9 @@ 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); =20 +#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, + unsigned long sz); + #endif /* _LINUX_HUGETLB_CONTPTE_H */ diff --git a/mm/hugetlb_contpte.c b/mm/hugetlb_contpte.c index e881b302dd63..82f49eb79ffb 100644 --- a/mm/hugetlb_contpte.c +++ b/mm/hugetlb_contpte.c @@ -98,3 +98,54 @@ void huge_pte_clear(struct mm_struct *mm, unsigned long = addr, for (i =3D 0; i < ncontig; i++, addr +=3D pgsize, ptep++) __pte_clear(mm, addr, ptep); } + +/* + * ARM: Changing some bits of contiguous entries requires us to follow a + * Break-Before-Make approach, breaking the whole contiguous set + * before we can change any entries. See ARM DDI 0487A.k_iss10775, + * "Misprogramming of the Contiguous bit", page D4-1762. + * + * RISCV: When dealing with NAPOT mappings, the privileged specification + * indicates that "if an update needs to be made, the OS generally should = first + * mark all of the PTEs invalid, then issue SFENCE.VMA instruction(s) cove= ring + * all 4 KiB regions within the range, [...] then update the PTE(s), as + * described in Section 4.2.1.". That's the equivalent of the Break-Before= -Make + * approach used by arm64. + * + * This helper performs the break step. + */ +static pte_t get_clear_contig(struct mm_struct *mm, + unsigned long addr, + pte_t *ptep, + unsigned long pgsize, + unsigned long ncontig) +{ + pte_t pte, tmp_pte; + bool present; + + pte =3D __ptep_get_and_clear(mm, addr, ptep); + present =3D pte_present(pte); + while (--ncontig) { + ptep++; + addr +=3D pgsize; + tmp_pte =3D __ptep_get_and_clear(mm, addr, ptep); + if (present) { + if (pte_dirty(tmp_pte)) + pte =3D pte_mkdirty(pte); + if (pte_young(tmp_pte)) + pte =3D pte_mkyoung(pte); + } + } + return pte; +} + +pte_t huge_ptep_get_and_clear(struct mm_struct *mm, + unsigned long addr, pte_t *ptep, unsigned long sz) +{ + int ncontig; + size_t pgsize; + + ncontig =3D arch_contpte_get_num_contig(ptep, sz, &pgsize); + + return get_clear_contig(mm, addr, ptep, pgsize, ncontig); +} --=20 2.39.2 From nobody Thu Dec 18 04:46:26 2025 Received: from mail-wr1-f47.google.com (mail-wr1-f47.google.com [209.85.221.47]) (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 DF4571EA7C1 for ; Fri, 21 Mar 2025 13:13:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742562833; cv=none; b=mpbuGtuW0pDzJNC6yZk4cLn6+6DRx9OYjsrEjQzIoi35Gul/3vpAMuXg5U6i/ys0rUuBZMz0AYIgGjoV4MYh87YyA73QVBMO2OTdOzkCptbcCGvQo06IsINlfAA9AAAElfPj9/ZqyaClUCtwznBaTpvv79bYgX3POZeyiS9xL7c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742562833; c=relaxed/simple; bh=43UsiKfmf8robAsEDJqmZ4aOxFSdPN1YR+8iwyD6dTc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=FHtWFWqpG9RNiX3DEWIGod0OD/XkVPZTzIztgkA75HYxbZsoj2OTqoGmyzjakzRZICWaF2K6msNnDBWALYg872f+9vV2HGe8ad0+s/r8F67pLLaFTo9nj0uS3FKKmx7ojJhnNwyvbYr12Tx2vLzWHu68vVJmu5+jahZ7EGzkvqY= 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=z28qBbal; arc=none smtp.client-ip=209.85.221.47 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 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="z28qBbal" Received: by mail-wr1-f47.google.com with SMTP id ffacd0b85a97d-3914bc3e01aso1327905f8f.2 for ; Fri, 21 Mar 2025 06:13:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1742562829; x=1743167629; 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=ws5N2/PtP1Eu6ihB8rghsKDx2GXJblSk7INWyBMy7EM=; b=z28qBbal587pBT5QKHo0sU3w6pgkRaND5KXT10UeVCR60JibZfbkfQbIXiHNtSDfmK LCSyqEoNVIgLzvtEp+afPSwQVDaxZy4Ey9RDE8CL/9cAhz9PuoY3IdmH0woZXo06Hlvs Fk62itnbQwwa748+1U93FVFPdE5xlxIc7/8GwD+V+lm/qw9mMOiWt2Li48I5LYyig23G wku5SuMS4SWph21pqAQirWVmm7lkEXvjHso0pmnjmf+F3DKKCW0tkXH9PupUu68TQ3d/ UBJ9sZLWdvtIwXdqwowZ6CyJwQ+jMvb064mXMOaAw24vUrEGTVVy10ldS0q4IfwxApTr BhQw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742562829; x=1743167629; 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=ws5N2/PtP1Eu6ihB8rghsKDx2GXJblSk7INWyBMy7EM=; b=DNSFHwVDITPIvRLOeuagtHC5RqRP+B3xvz2XqBy+DtSxdvW+qqAzgF5QGK/qhnlK8a rKkfYiL8te4RFJ4Ajhki8ulBlEaRoBg2NKtmTKfI5fYKsXeHWfgjSvqBviQWiAgNBBgw ZCvfGfpb4O8DFCNficsht9wgrmPrSRZ38BU+TEnXfkWNTb4D8uozlYu/MjDMkYtaGPSP sApVqoUxQ90o24yDe7/qO1owzodSeTV0KoFpHg0WJCPqu6K0MOa7tSxGOyVF4SxrE5Dk clLt6EFlYQk1Qnyaz/KaHA5ktB1hAZp6coiNpdKZLSChHOiDJCf6Z4WIU/U4BpeSy463 zscw== X-Forwarded-Encrypted: i=1; AJvYcCVx/VjlqWWwov3s1xOHRZig2Z5sA9+7MTBRG64urtb5zKwkIPcggTJv/Yk6gCGkzJ0pI7cYefRmJd3LBOs=@vger.kernel.org X-Gm-Message-State: AOJu0YxkU7EL5g7LjhHtjn/onKr1M6u+FUy6iXCxYg+d6DqCbzoeC967 wqSPM1urWtAlbydiuboacPeTJdwO6UTpjdUeH/yDIRrSzDiyqSO+Y2iuDu/1/y8= X-Gm-Gg: ASbGnctbkds7t2m2W9mlSwh6fXDjjpF/QDxBDL8Luqg/ARBbsnlNdFkPiIVzsoC/hVn D9e2Pfe4B2b4636rGLjI/EV0TTfqGZ3BXb6SuOf9sAYAGBswobNNbPwNi0V+ozQNezco9nJ/l/K hwyk9CzNPwjS+bUQoDOmsg0GKolTSWDu4P1aiEwQwo33xkMp4aQ2hL8HQVbzZ5AM0K3+XZN+hEo xIHalo7RutRe2uLh1WOqaPGBfruxxWOBGbT1hRdh8UVbkoHg+Lbxoad7d+CbzNMFf5g7o408wzE cMLaRx6VXKw6GdBKxd/zK9niizrpK96VTUUGdXrd920ZUTKuOL1Kk3If9WKDUaVYuFbVZqZ7Xlg sX9Zf X-Google-Smtp-Source: AGHT+IGUJnxhjyGu7ufB6wyGpdYdbKS7+6k2eyHYgFKxVHRlB/Xm7E8vrXAFhp0hvlUzI9R7KED60g== X-Received: by 2002:a5d:64af:0:b0:391:2db0:2961 with SMTP id ffacd0b85a97d-3997f932efcmr3397837f8f.38.1742562828869; Fri, 21 Mar 2025 06:13:48 -0700 (PDT) Received: from alex-rivos.ba.rivosinc.com ([2001:861:3382:ef90:3d12:52fe:c1cc:c94]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3997f9e674fsm2292970f8f.80.2025.03.21.06.13.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 21 Mar 2025 06:13:48 -0700 (PDT) From: Alexandre Ghiti To: Catalin Marinas , Will Deacon , Ryan Roberts , Mark Rutland , Matthew Wilcox , Paul Walmsley , Palmer Dabbelt , Alexandre Ghiti , 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 v5 7/9] mm: Use common huge_ptep_set_access_flags() function for riscv/arm64 Date: Fri, 21 Mar 2025 14:06:33 +0100 Message-Id: <20250321130635.227011-8-alexghiti@rivosinc.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20250321130635.227011-1-alexghiti@rivosinc.com> References: <20250321130635.227011-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: quoted-printable Content-Type: text/plain; charset="utf-8" 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/include/asm/hugetlb.h | 4 -- arch/arm64/include/asm/pgtable.h | 15 +++++- arch/arm64/mm/hugetlbpage.c | 69 +-------------------------- arch/riscv/include/asm/hugetlb.h | 5 -- arch/riscv/include/asm/pgtable.h | 11 +++-- arch/riscv/mm/hugetlbpage.c | 32 ++----------- arch/riscv/mm/pgtable.c | 6 +-- include/linux/hugetlb_contpte.h | 5 ++ mm/hugetlb_contpte.c | 81 ++++++++++++++++++++++++++++++-- 9 files changed, 110 insertions(+), 118 deletions(-) diff --git a/arch/arm64/include/asm/hugetlb.h b/arch/arm64/include/asm/huge= tlb.h index 9b1c25775bea..29a9dac52cef 100644 --- a/arch/arm64/include/asm/hugetlb.h +++ b/arch/arm64/include/asm/hugetlb.h @@ -35,10 +35,6 @@ static inline void arch_clear_hugetlb_flags(struct folio= *folio) =20 pte_t arch_make_huge_pte(pte_t entry, unsigned int shift, vm_flags_t flags= ); #define arch_make_huge_pte arch_make_huge_pte -#define __HAVE_ARCH_HUGE_PTEP_SET_ACCESS_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_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/pgta= ble.h index af8156929c1d..9b5c57e56691 100644 --- a/arch/arm64/include/asm/pgtable.h +++ b/arch/arm64/include/asm/pgtable.h @@ -1847,12 +1847,23 @@ static inline bool __hugetlb_valid_size(unsigned lo= ng size) return false; } =20 -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 =3D 1; =20 + /* + * If the size is not passed, we need to go through the page table to + * find out the number of contiguous ptes. + */ + if (size =3D=3D 0) + return find_num_contig(mm, addr, ptep, pgsize); + if (pgsize) *pgsize =3D size; =20 diff --git a/arch/arm64/mm/hugetlbpage.c b/arch/arm64/mm/hugetlbpage.c index 62a66ce2b2fe..03cb757f7935 100644 --- a/arch/arm64/mm/hugetlbpage.c +++ b/arch/arm64/mm/hugetlbpage.c @@ -63,8 +63,8 @@ bool arch_hugetlb_migration_supported(struct hstate *h) } #endif =20 -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 =3D pgd_offset(mm, addr); p4d_t *p4dp; @@ -260,71 +260,6 @@ pte_t arch_make_huge_pte(pte_t entry, unsigned int shi= ft, vm_flags_t flags) return entry; } =20 -/* - * 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) !=3D pte_write(__ptep_get(ptep))) - return 1; - - for (i =3D 0; i < ncontig; i++) { - pte_t orig_pte =3D __ptep_get(ptep + i); - - if (pte_dirty(pte) !=3D pte_dirty(orig_pte)) - return 1; - - if (pte_young(pte) !=3D 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 =3D 0; - unsigned long pfn =3D pte_pfn(pte), dpfn; - struct mm_struct *mm =3D 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 =3D find_num_contig(mm, addr, ptep, &pgsize); - dpfn =3D pgsize >> PAGE_SHIFT; - - if (!__cont_access_flags_changed(ptep, pte, ncontig)) - return 0; - - orig_pte =3D 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 =3D pte_mkdirty(pte); - - if (pte_young(orig_pte)) - pte =3D pte_mkyoung(pte); - - hugeprot =3D pte_pgprot(pte); - for (i =3D 0; i < ncontig; i++, ptep++, addr +=3D pgsize, pfn +=3D dpfn) - __set_ptes(mm, addr, ptep, pfn_pte(pfn, hugeprot), 1); - - return 1; -} - void huge_ptep_set_wrprotect(struct mm_struct *mm, unsigned long addr, pte_t *ptep) { diff --git a/arch/riscv/include/asm/hugetlb.h b/arch/riscv/include/asm/huge= tlb.h index 0fbb6b19df79..bf533c2cef84 100644 --- a/arch/riscv/include/asm/hugetlb.h +++ b/arch/riscv/include/asm/hugetlb.h @@ -28,11 +28,6 @@ pte_t huge_ptep_clear_flush(struct vm_area_struct *vma, void huge_ptep_set_wrprotect(struct mm_struct *mm, unsigned long addr, pte_t *ptep); =20 -#define __HAVE_ARCH_HUGE_PTEP_SET_ACCESS_FLAGS -int huge_ptep_set_access_flags(struct vm_area_struct *vma, - unsigned long addr, pte_t *ptep, - pte_t pte, int dirty); - pte_t arch_make_huge_pte(pte_t entry, unsigned int shift, vm_flags_t flags= ); #define arch_make_huge_pte arch_make_huge_pte =20 diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pgta= ble.h index 72d3592454d3..081385e0d10a 100644 --- a/arch/riscv/include/asm/pgtable.h +++ b/arch/riscv/include/asm/pgtable.h @@ -613,7 +613,9 @@ static inline void ___set_ptes(struct mm_struct *mm, un= signed long addr, * Some hugetlb functions can be called on !present ptes, so we must use t= he * size parameter when it is passed. */ -static inline int arch_contpte_get_num_contig(pte_t *ptep, unsigned long s= ize, +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; @@ -657,9 +659,8 @@ static inline void __pte_clear(struct mm_struct *mm, __set_pte_at(mm, ptep, __pte(0)); } =20 -#define __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS /* defined in mm/pgtable.c */ -extern int ptep_set_access_flags(struct vm_area_struct *vma, unsigned long= address, - pte_t *ptep, pte_t entry, int dirty); +extern int __ptep_set_access_flags(struct vm_area_struct *vma, unsigned lo= ng address, + pte_t *ptep, pte_t entry, int dirty); #define __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG /* defined in mm/pgtable.c */ extern int ptep_test_and_clear_young(struct vm_area_struct *vma, unsigned = long address, pte_t *ptep); @@ -788,6 +789,8 @@ static inline pte_t __ptep_get_and_clear(struct mm_stru= ct *mm, #define __HAVE_ARCH_PTEP_GET_AND_CLEAR #define ptep_get_and_clear __ptep_get_and_clear #define pte_clear __pte_clear +#define __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS +#define ptep_set_access_flags __ptep_set_access_flags =20 #define pgprot_nx pgprot_nx static inline pgprot_t pgprot_nx(pgprot_t _prot) diff --git a/arch/riscv/mm/hugetlbpage.c b/arch/riscv/mm/hugetlbpage.c index 87168123d4a2..b2046f4bd445 100644 --- a/arch/riscv/mm/hugetlbpage.c +++ b/arch/riscv/mm/hugetlbpage.c @@ -176,33 +176,6 @@ pte_t arch_make_huge_pte(pte_t entry, unsigned int shi= ft, vm_flags_t flags) return entry; } =20 -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 =3D vma->vm_mm; - pte_t orig_pte; - int pte_num; - - if (!pte_napot(pte)) - return ptep_set_access_flags(vma, addr, ptep, pte, dirty); - - pte_num =3D arch_contpte_get_num_contig(ptep, 0, NULL); - orig_pte =3D get_clear_contig_flush(mm, addr, ptep, pte_num); - - if (pte_dirty(orig_pte)) - pte =3D pte_mkdirty(pte); - - if (pte_young(orig_pte)) - pte =3D 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) @@ -216,7 +189,8 @@ void huge_ptep_set_wrprotect(struct mm_struct *mm, return; } =20 - pte_num =3D arch_contpte_get_num_contig(ptep, 0, NULL); + pte_num =3D arch_contpte_get_num_contig(mm, addr, ptep, 0, NULL); + orig_pte =3D get_clear_contig_flush(mm, addr, ptep, pte_num); =20 orig_pte =3D pte_wrprotect(orig_pte); @@ -234,7 +208,7 @@ pte_t huge_ptep_clear_flush(struct vm_area_struct *vma, if (!pte_napot(pte)) return ptep_clear_flush(vma, addr, ptep); =20 - pte_num =3D arch_contpte_get_num_contig(ptep, 0, NULL); + pte_num =3D arch_contpte_get_num_contig(vma->vm_mm, addr, ptep, 0, NULL); =20 return get_clear_contig_flush(vma->vm_mm, addr, ptep, pte_num); } diff --git a/arch/riscv/mm/pgtable.c b/arch/riscv/mm/pgtable.c index 4ae67324f992..af8b3769a349 100644 --- a/arch/riscv/mm/pgtable.c +++ b/arch/riscv/mm/pgtable.c @@ -5,9 +5,9 @@ #include #include =20 -int ptep_set_access_flags(struct vm_area_struct *vma, - unsigned long address, pte_t *ptep, - pte_t entry, int dirty) +int __ptep_set_access_flags(struct vm_area_struct *vma, + unsigned long address, pte_t *ptep, + pte_t entry, int dirty) { asm goto(ALTERNATIVE("nop", "j %l[svvptc]", 0, RISCV_ISA_EXT_SVVPTC, 1) : : : : svvptc); diff --git a/include/linux/hugetlb_contpte.h b/include/linux/hugetlb_contpt= e.h index 1c8f46ff95ea..e129578f6500 100644 --- a/include/linux/hugetlb_contpte.h +++ b/include/linux/hugetlb_contpte.h @@ -23,4 +23,9 @@ extern pte_t huge_ptep_get_and_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep, unsigned long sz); =20 +#define __HAVE_ARCH_HUGE_PTEP_SET_ACCESS_FLAGS +extern int huge_ptep_set_access_flags(struct vm_area_struct *vma, + unsigned long addr, pte_t *ptep, + pte_t pte, int dirty); + #endif /* _LINUX_HUGETLB_CONTPTE_H */ diff --git a/mm/hugetlb_contpte.c b/mm/hugetlb_contpte.c index 82f49eb79ffb..b4c409d11195 100644 --- a/mm/hugetlb_contpte.c +++ b/mm/hugetlb_contpte.c @@ -15,7 +15,7 @@ pte_t huge_ptep_get(struct mm_struct *mm, unsigned long a= ddr, pte_t *ptep) if (!pte_present(orig_pte) || !pte_cont(orig_pte)) return orig_pte; =20 - ncontig =3D arch_contpte_get_num_contig(ptep, + ncontig =3D arch_contpte_get_num_contig(mm, addr, ptep, page_size(pte_page(orig_pte)), NULL); =20 @@ -69,7 +69,7 @@ void set_huge_pte_at(struct mm_struct *mm, unsigned long = addr, int i; int ncontig; =20 - ncontig =3D arch_contpte_get_num_contig(ptep, sz, &pgsize); + ncontig =3D arch_contpte_get_num_contig(mm, addr, ptep, sz, &pgsize); =20 if (!pte_present(pte)) { for (i =3D 0; i < ncontig; i++, ptep++, addr +=3D pgsize) @@ -93,7 +93,7 @@ void huge_pte_clear(struct mm_struct *mm, unsigned long a= ddr, int i, ncontig; size_t pgsize; =20 - ncontig =3D arch_contpte_get_num_contig(ptep, sz, &pgsize); + ncontig =3D arch_contpte_get_num_contig(mm, addr, ptep, sz, &pgsize); =20 for (i =3D 0; i < ncontig; i++, addr +=3D pgsize, ptep++) __pte_clear(mm, addr, ptep); @@ -145,7 +145,80 @@ pte_t huge_ptep_get_and_clear(struct mm_struct *mm, int ncontig; size_t pgsize; =20 - ncontig =3D arch_contpte_get_num_contig(ptep, sz, &pgsize); + ncontig =3D arch_contpte_get_num_contig(mm, addr, ptep, sz, &pgsize); =20 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) !=3D pte_write(__ptep_get(ptep))) + return 1; + + for (i =3D 0; i < ncontig; i++) { + pte_t orig_pte =3D __ptep_get(ptep + i); + + if (pte_dirty(pte) !=3D pte_dirty(orig_pte)) + return 1; + + if (pte_young(pte) !=3D 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 =3D get_clear_contig(mm, addr, ptep, pgsize, ncontig); + struct vm_area_struct vma =3D 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 =3D 0; + struct mm_struct *mm =3D vma->vm_mm; + pte_t orig_pte; + + if (!pte_cont(pte)) + return __ptep_set_access_flags(vma, addr, ptep, pte, dirty); + + ncontig =3D arch_contpte_get_num_contig(vma->vm_mm, addr, ptep, 0, &pgsiz= e); + + if (!__cont_access_flags_changed(ptep, pte, ncontig)) + return 0; + + orig_pte =3D 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 =3D pte_mkdirty(pte); + + if (pte_young(orig_pte)) + pte =3D pte_mkyoung(pte); + + set_contptes(mm, addr, ptep, pte, ncontig, pgsize); + + return 1; +} --=20 2.39.2 From nobody Thu Dec 18 04:46:26 2025 Received: from mail-wr1-f43.google.com (mail-wr1-f43.google.com [209.85.221.43]) (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 2D2CF1EA7C1 for ; Fri, 21 Mar 2025 13:14:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742562894; cv=none; b=aB22c0/58DSOmzhN4oBcrCFeMZYZ2/oFSqZMiesTnwF95VWxKgcEOPuiQaHTpxfda3LQQzFaD9mckHzWTHgwWju/ADCGefssUo6LmwrONqIL72CgdKq+/ruoo6AGhmNN+Ol7P37wruVz6kuRcKBWAWoKLiqoHYZQ32IQtVrG3pY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742562894; c=relaxed/simple; bh=lmavtcPyTtLYZ2+vrTeUXw3teb+/K/n3qiMuem1Xy1o=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=f1WqrPJS0jyZTn14YDe31KTjHptyxnAXMZrAzYuME8TODybtb19ZydLcLFYd6MDEwDwn/oqI8F5rMA0CjQHj7mQpo9HSbesCjzCFel1Sp8YTMDynswDWMLP6DYZ3Onh5+06DAusH6g6eEHfYubXUmcxoF64D8zRmrnW7nSOB5f8= 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=W19MMX24; arc=none smtp.client-ip=209.85.221.43 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 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="W19MMX24" Received: by mail-wr1-f43.google.com with SMTP id ffacd0b85a97d-3912d2c89ecso1778461f8f.2 for ; Fri, 21 Mar 2025 06:14:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1742562890; x=1743167690; 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=JOImW0XTENLZqMOVas1NKgXRQH/ppGnjlH3sp/4Pu3c=; b=W19MMX24Sy7qd9CkoPcLIqrV+DPHglWCB2iRltM5BPk7S8e2xBqbguXRLejMcvWMQC WJuNSiUPRWwn8aE6Fo64TBo7kLeVKIv61OZvFXD0K05aNXkOWtynb1qoUhCVBLT7uk5/ LgX+6rAnzU7eu5d00NVlZXU69zzsityBUzusZwQ+M0blz4UoCI5zAEt1rkmVfSMCfXe7 4TRUna7ttc53BTnagIZyR4Uofh9su1WnJTBImJ8cdZWlSskjIHW8x+oUgQF5ng3IhmFS ZtQhKjfuUJJAjPfLj9wwRoqHNQDj7SH1+db/FTKyhfg1GzLIt+VMSlmJgRLYkbTUvk/T Ndjg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742562890; x=1743167690; 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=JOImW0XTENLZqMOVas1NKgXRQH/ppGnjlH3sp/4Pu3c=; b=b7t6dRJoxbWCGsZgWjxc1pVvPjSJvIwriQwKiqqSgI5AgD+20uT1yAMwkQkx2nqUVC AqSw7I+t6636nZt2dzfjU2JRV4fME/wPsCoD3A4Ebi8Fsp6PRUixMV4YTdyNJwe9w/pL t8HR086qts3zMgqhqUVNFLuXMffwGgFxR///pFib3FwinVIR6Xm+y7JU7SNpHu/1YTTj 6/GVzl5CfdA6WSxAcksuISoMCC9lC3Pt45l7bQMy2Ytifn4rfjvLzTVZC2dkty6PqfUq rfZVlQ7PNABvkZDqz0xNJ9EEPtcnYwJ7F+OBx6rroycY1I9K9NZ8ePaUiEZdo7DiuK5y sPUQ== X-Forwarded-Encrypted: i=1; AJvYcCX5Aj9AcU8/otCxODarMS7r12FycPpecH0bkTg16jcs//ljdjshccE9TYMmMiUC9VpIxumwVFQOjcil5h8=@vger.kernel.org X-Gm-Message-State: AOJu0YxPYy1M9yNxCfNpc2LuUKNODQnIleQxDLZB0QScWX1iUj10yKKP Z5iDurWYW/fRpwXvCUL+OtzsyK/+jC1ZAaBvnlfb3kHxzBC4Eg0dSJ8Hj/+plQM= X-Gm-Gg: ASbGncsPbQ+osD5BpKQCnjVMzkrko7O22UcgZ8tpO73DcZJvkMF3YXFAuFGw6JWA0r7 G6oCNYta+rHJD33JcFDT5iE52hVeu/O3kJLUYH8aJUvbQVxLD8eoksyCC4OCVlyc80fIsmA0VhS 3052XkmesuO6A9pJllVOsBSXvliQEnDp2jHnLSNwl74BG2+KV+6wH6u1FSLGucqKEha41EJWi3T UWcRAq2U8lTQX0zsIUTUcNQDvM6fQKAeUNmLa6gSei1IcyyVQAA9A+B/fwnDDlgF+36UQrJBEMJ ENqhsPeKpjMAlMItxhnOrcuSrSeO+tPAPPioXuGzKCKQxrh4if12Un+SthJSa+gz/7XaiQ== X-Google-Smtp-Source: AGHT+IG4Bh4b8ljgMGozvKhj+gGf/zTKUltGlpRU5vbjoRJl/qtI6QCkciAO8DnHWDVRjHELB4GFxw== X-Received: by 2002:a05:6000:381:b0:399:737f:4e02 with SMTP id ffacd0b85a97d-3997f938798mr3602833f8f.39.1742562890297; Fri, 21 Mar 2025 06:14:50 -0700 (PDT) Received: from alex-rivos.ba.rivosinc.com ([2001:861:3382:ef90:3d12:52fe:c1cc:c94]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3997f9eef37sm2294241f8f.85.2025.03.21.06.14.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 21 Mar 2025 06:14:49 -0700 (PDT) From: Alexandre Ghiti To: Catalin Marinas , Will Deacon , Ryan Roberts , Mark Rutland , Matthew Wilcox , Paul Walmsley , Palmer Dabbelt , Alexandre Ghiti , 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 v5 8/9] mm: Use common huge_ptep_set_wrprotect() function for riscv/arm64 Date: Fri, 21 Mar 2025 14:06:34 +0100 Message-Id: <20250321130635.227011-9-alexghiti@rivosinc.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20250321130635.227011-1-alexghiti@rivosinc.com> References: <20250321130635.227011-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: quoted-printable Content-Type: text/plain; charset="utf-8" After some adjustments, both architectures have the same implementation so move it to the generic code. Signed-off-by: Alexandre Ghiti --- arch/arm64/include/asm/hugetlb.h | 3 --- arch/arm64/mm/hugetlbpage.c | 27 --------------------------- arch/riscv/include/asm/hugetlb.h | 4 ---- arch/riscv/include/asm/pgtable.h | 7 ++++--- arch/riscv/mm/hugetlbpage.c | 22 ---------------------- include/linux/hugetlb_contpte.h | 4 ++++ mm/hugetlb_contpte.c | 20 ++++++++++++++++++++ 7 files changed, 28 insertions(+), 59 deletions(-) diff --git a/arch/arm64/include/asm/hugetlb.h b/arch/arm64/include/asm/huge= tlb.h index 29a9dac52cef..f568467e8ba2 100644 --- a/arch/arm64/include/asm/hugetlb.h +++ b/arch/arm64/include/asm/hugetlb.h @@ -35,9 +35,6 @@ static inline void arch_clear_hugetlb_flags(struct folio = *folio) =20 pte_t arch_make_huge_pte(pte_t entry, unsigned int shift, vm_flags_t flags= ); #define arch_make_huge_pte arch_make_huge_pte -#define __HAVE_ARCH_HUGE_PTEP_SET_WRPROTECT -extern void huge_ptep_set_wrprotect(struct mm_struct *mm, - unsigned long addr, pte_t *ptep); #define __HAVE_ARCH_HUGE_PTEP_CLEAR_FLUSH extern pte_t huge_ptep_clear_flush(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep); diff --git a/arch/arm64/mm/hugetlbpage.c b/arch/arm64/mm/hugetlbpage.c index 03cb757f7935..17f1ed34356d 100644 --- a/arch/arm64/mm/hugetlbpage.c +++ b/arch/arm64/mm/hugetlbpage.c @@ -260,33 +260,6 @@ pte_t arch_make_huge_pte(pte_t entry, unsigned int shi= ft, vm_flags_t flags) return entry; } =20 -void huge_ptep_set_wrprotect(struct mm_struct *mm, - unsigned long addr, pte_t *ptep) -{ - unsigned long pfn, dpfn; - pgprot_t hugeprot; - int ncontig, i; - size_t pgsize; - pte_t pte; - - if (!pte_cont(__ptep_get(ptep))) { - __ptep_set_wrprotect(mm, addr, ptep); - return; - } - - ncontig =3D find_num_contig(mm, addr, ptep, &pgsize); - dpfn =3D pgsize >> PAGE_SHIFT; - - pte =3D get_clear_contig_flush(mm, addr, ptep, pgsize, ncontig); - pte =3D pte_wrprotect(pte); - - hugeprot =3D pte_pgprot(pte); - pfn =3D pte_pfn(pte); - - for (i =3D 0; i < ncontig; i++, ptep++, addr +=3D pgsize, pfn +=3D dpfn) - __set_ptes(mm, addr, ptep, pfn_pte(pfn, hugeprot), 1); -} - pte_t huge_ptep_clear_flush(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep) { diff --git a/arch/riscv/include/asm/hugetlb.h b/arch/riscv/include/asm/huge= tlb.h index bf533c2cef84..4c692dd82779 100644 --- a/arch/riscv/include/asm/hugetlb.h +++ b/arch/riscv/include/asm/hugetlb.h @@ -24,10 +24,6 @@ bool arch_hugetlb_migration_supported(struct hstate *h); pte_t huge_ptep_clear_flush(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep); =20 -#define __HAVE_ARCH_HUGE_PTEP_SET_WRPROTECT -void huge_ptep_set_wrprotect(struct mm_struct *mm, - unsigned long addr, pte_t *ptep); - pte_t arch_make_huge_pte(pte_t entry, unsigned int shift, vm_flags_t flags= ); #define arch_make_huge_pte arch_make_huge_pte =20 diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pgta= ble.h index 081385e0d10a..c41b49948ee9 100644 --- a/arch/riscv/include/asm/pgtable.h +++ b/arch/riscv/include/asm/pgtable.h @@ -665,9 +665,8 @@ extern int __ptep_set_access_flags(struct vm_area_struc= t *vma, unsigned long add extern int ptep_test_and_clear_young(struct vm_area_struct *vma, unsigned = long address, pte_t *ptep); =20 -#define __HAVE_ARCH_PTEP_SET_WRPROTECT -static inline void ptep_set_wrprotect(struct mm_struct *mm, - unsigned long address, pte_t *ptep) +static inline void __ptep_set_wrprotect(struct mm_struct *mm, + unsigned long address, pte_t *ptep) { atomic_long_and(~(unsigned long)_PAGE_WRITE, (atomic_long_t *)ptep); } @@ -791,6 +790,8 @@ static inline pte_t __ptep_get_and_clear(struct mm_stru= ct *mm, #define pte_clear __pte_clear #define __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS #define ptep_set_access_flags __ptep_set_access_flags +#define __HAVE_ARCH_PTEP_SET_WRPROTECT +#define ptep_set_wrprotect __ptep_set_wrprotect =20 #define pgprot_nx pgprot_nx static inline pgprot_t pgprot_nx(pgprot_t _prot) diff --git a/arch/riscv/mm/hugetlbpage.c b/arch/riscv/mm/hugetlbpage.c index b2046f4bd445..db13f7bcdd54 100644 --- a/arch/riscv/mm/hugetlbpage.c +++ b/arch/riscv/mm/hugetlbpage.c @@ -176,28 +176,6 @@ pte_t arch_make_huge_pte(pte_t entry, unsigned int shi= ft, vm_flags_t flags) return entry; } =20 -void huge_ptep_set_wrprotect(struct mm_struct *mm, - unsigned long addr, - pte_t *ptep) -{ - pte_t pte =3D ptep_get(ptep); - pte_t orig_pte; - int pte_num; - - if (!pte_napot(pte)) { - ptep_set_wrprotect(mm, addr, ptep); - return; - } - - pte_num =3D arch_contpte_get_num_contig(mm, addr, ptep, 0, NULL); - - orig_pte =3D get_clear_contig_flush(mm, addr, ptep, pte_num); - - orig_pte =3D pte_wrprotect(orig_pte); - - set_ptes(mm, addr, ptep, orig_pte, pte_num); -} - pte_t huge_ptep_clear_flush(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep) diff --git a/include/linux/hugetlb_contpte.h b/include/linux/hugetlb_contpt= e.h index e129578f6500..9ec8792a2f4d 100644 --- a/include/linux/hugetlb_contpte.h +++ b/include/linux/hugetlb_contpte.h @@ -28,4 +28,8 @@ extern int huge_ptep_set_access_flags(struct vm_area_stru= ct *vma, unsigned long addr, pte_t *ptep, pte_t pte, int dirty); =20 +#define __HAVE_ARCH_HUGE_PTEP_SET_WRPROTECT +extern void huge_ptep_set_wrprotect(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 b4c409d11195..629878765081 100644 --- a/mm/hugetlb_contpte.c +++ b/mm/hugetlb_contpte.c @@ -222,3 +222,23 @@ int huge_ptep_set_access_flags(struct vm_area_struct *= vma, =20 return 1; } + +void huge_ptep_set_wrprotect(struct mm_struct *mm, + unsigned long addr, pte_t *ptep) +{ + int ncontig; + size_t pgsize; + pte_t pte; + + if (!pte_cont(__ptep_get(ptep))) { + __ptep_set_wrprotect(mm, addr, ptep); + return; + } + + ncontig =3D arch_contpte_get_num_contig(mm, addr, ptep, 0, &pgsize); + + pte =3D get_clear_contig_flush(mm, addr, ptep, pgsize, ncontig); + pte =3D pte_wrprotect(pte); + + set_contptes(mm, addr, ptep, pte, ncontig, pgsize); +} --=20 2.39.2 From nobody Thu Dec 18 04:46:26 2025 Received: from mail-wm1-f53.google.com (mail-wm1-f53.google.com [209.85.128.53]) (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 9512A1CAA81 for ; Fri, 21 Mar 2025 13:15:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742562955; cv=none; b=JNZhhDOfOTCiGXH4LMGf/Hi9C6lPa2CRgeDGgKRZUGl3yOa1FmDFja2bCi4j+6HquVhVL0wd6pHNn4qFxOKLduhNCV5o8kAI/u8A/66jFT+vmKWDCcYOHOnls+L+X0xlIlAsfGJaKQlPY/ooBxhIFejfpLFH+zM2NR3P7k+Tk/k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742562955; c=relaxed/simple; bh=XZb4/7ED/yOzxaYKXsmRsh+ALKUE7iBR0oHSgbefUGA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=cqWSwMnPb5EhKpgUeiaSvdLLH9l17nTFKG654TUxmH0t1/Q6AQ9JVBD9guqBY9NLTrmUGeCJJ6upQDCf948TDYvpj7l8bKC9Ojf+ZkkXa+tS3mdYfOAnAl4YrgCMWv8tW75qUekAbXtYlohTmz42ISo4Jtq7vN6Xb0BCtnpzHyM= 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=rgiQaPpp; arc=none smtp.client-ip=209.85.128.53 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 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="rgiQaPpp" Received: by mail-wm1-f53.google.com with SMTP id 5b1f17b1804b1-4394a823036so19480765e9.0 for ; Fri, 21 Mar 2025 06:15:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1742562952; x=1743167752; 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=Vd9K/fWiMAnzVZgWbX0wvtztqz4Hm8uA/kr1foq/koQ=; b=rgiQaPppG2pz/Yd077kDMO7ash9l4bOZw52TLn218EugReSkYfezU211Sd1dTrwboy mlRNXKBSNMNllmxOj08R0EHZM67YZ/ehU3fvlnzBFoFeXmDGW7dXscfo6xX/mvVrv6LU kpRIHZ38Ig4NxUltfFvE1o/eaCq+6DrX4TKSplx2RXifgZ+tTifKthGhUerzWkYG3AF+ aP8FGhbIyZhjHnfw8W0fgrN8mU17QJQ6S7FSd3jmuOkASge+yzjoAItIQY4ZKi9hT4Xp K0f08bie8q9OdA1xVEiEAirHyJ02fXDaTka2SJgpIy2i+EzWPcreH3R/01ImSJ7s4dRr y0Jg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742562952; x=1743167752; 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=Vd9K/fWiMAnzVZgWbX0wvtztqz4Hm8uA/kr1foq/koQ=; b=IfPBOG456tynxTOdFJOXIhC9WCkFQ50pEeGQjYFaTldZU1JlUVEpuWDJwBnRhaBHLs ON8FVYKXGnOGn6XimZqR0ml3lvVIrf/zqW7zxKdn2tOLzELWIV1yM2KG63JP27GwDVE8 rNKJc1k5sImX3U5+zv6eWi9AM5ZNG6KQzYIvSnFZm5gSAOcq6Lad4pU/rRn1bxKW4bs7 9ZaMgVXNTc9Hsl5IsjPoNQnqjKAr6HiMxIv/KlOeEV9LkUL37BBSFGMMuaxd5co+J0mJ cJ5xIufA4tG2Y7FJKr+XTOo9hKl3pTS7XPxlUB9l1fs1h93HHl5vsJKXVkP3qacf+kpr sksw== X-Forwarded-Encrypted: i=1; AJvYcCXClUWgJIt85yoqaejfSulDdpjdkYVU4rsdL1FC625A3EeJAQ+weeOxJRg/ECBKQgBSOEuln/NAjRDfXC8=@vger.kernel.org X-Gm-Message-State: AOJu0Yyxf6xDYCK0Rx5/qnyIlQEGm9XeV08DQ6kKrmdVQ2L72W46xDmG wD4Em7yRrBLrOid5IscTEtxpxa4OauhmotPDoSTFHykW2NNszK7e4h9nf3c3Svo= X-Gm-Gg: ASbGnctmtNxloxOl8ro+evzq9/325Qu9/w8XLLVEsUECs8eXv9P2g7aDxCMYqtg8Rpw 21ybnkbdiz2FiBtXxQg/iXbUcjFxyNmG+vl0C+59w7h9lP5T8TX8CNhY+Mivvk/9H1tNAoWmb08 99rW9HXzZXkvo8doOnmuHE1Xtk7TpdpTthXZluDz6p8RkNE0G/bVF09ObvmnD9b7shdyasbFeMw B3Cvmu/ihRD4TEXNioFND8W5edDAJEY1IxV262c+umXY4k6K3VhuUE/k3uFXB64m6kC04CEJo7h hDlKk09NRn33HiRPiExtyRBlCNxcSfE8Xtgjkb6PZatgpTmfIZSQ1ZfykAG/EonnlGgBHQ== X-Google-Smtp-Source: AGHT+IEeSuJJCualMyRGxNrWV663ipohlvvxYL4TpbesKMPlj8TlM2yLM3TCv4QBVXMIE7MjteYhiA== X-Received: by 2002:a05:600c:83cf:b0:43c:ec28:d310 with SMTP id 5b1f17b1804b1-43d509ec838mr36046425e9.10.1742562951783; Fri, 21 Mar 2025 06:15:51 -0700 (PDT) Received: from alex-rivos.ba.rivosinc.com ([2001:861:3382:ef90:3d12:52fe:c1cc:c94]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-43d4fdb06b9sm26435515e9.36.2025.03.21.06.15.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 21 Mar 2025 06:15:51 -0700 (PDT) From: Alexandre Ghiti To: Catalin Marinas , Will Deacon , Ryan Roberts , Mark Rutland , Matthew Wilcox , Paul Walmsley , Palmer Dabbelt , Alexandre Ghiti , 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 v5 9/9] mm: Use common huge_ptep_clear_flush() function for riscv/arm64 Date: Fri, 21 Mar 2025 14:06:35 +0100 Message-Id: <20250321130635.227011-10-alexghiti@rivosinc.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20250321130635.227011-1-alexghiti@rivosinc.com> References: <20250321130635.227011-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: quoted-printable Content-Type: text/plain; charset="utf-8" After some adjustments, both architectures have the same implementation so move it to the generic code. Signed-off-by: Alexandre Ghiti --- arch/arm64/include/asm/hugetlb.h | 3 -- arch/arm64/mm/hugetlbpage.c | 60 -------------------------------- arch/riscv/include/asm/hugetlb.h | 7 +--- arch/riscv/mm/hugetlbpage.c | 54 ---------------------------- include/linux/hugetlb_contpte.h | 4 +++ mm/hugetlb_contpte.c | 14 ++++++++ 6 files changed, 19 insertions(+), 123 deletions(-) diff --git a/arch/arm64/include/asm/hugetlb.h b/arch/arm64/include/asm/huge= tlb.h index f568467e8ba2..368600764127 100644 --- a/arch/arm64/include/asm/hugetlb.h +++ b/arch/arm64/include/asm/hugetlb.h @@ -35,9 +35,6 @@ static inline void arch_clear_hugetlb_flags(struct folio = *folio) =20 pte_t arch_make_huge_pte(pte_t entry, unsigned int shift, vm_flags_t flags= ); #define arch_make_huge_pte arch_make_huge_pte -#define __HAVE_ARCH_HUGE_PTEP_CLEAR_FLUSH -extern pte_t huge_ptep_clear_flush(struct vm_area_struct *vma, - unsigned long addr, pte_t *ptep); =20 void __init arm64_hugetlb_cma_reserve(void); =20 diff --git a/arch/arm64/mm/hugetlbpage.c b/arch/arm64/mm/hugetlbpage.c index 17f1ed34356d..08316cf4b104 100644 --- a/arch/arm64/mm/hugetlbpage.c +++ b/arch/arm64/mm/hugetlbpage.c @@ -82,52 +82,6 @@ int find_num_contig(struct mm_struct *mm, unsigned long = addr, return CONT_PTES; } =20 -/* - * Changing some bits of contiguous entries requires us to follow a - * Break-Before-Make approach, breaking the whole contiguous set - * before we can change any entries. See ARM DDI 0487A.k_iss10775, - * "Misprogramming of the Contiguous bit", page D4-1762. - * - * This helper performs the break step. - */ -static pte_t get_clear_contig(struct mm_struct *mm, - unsigned long addr, - pte_t *ptep, - unsigned long pgsize, - unsigned long ncontig) -{ - pte_t pte, tmp_pte; - bool present; - - pte =3D __ptep_get_and_clear(mm, addr, ptep); - present =3D pte_present(pte); - while (--ncontig) { - ptep++; - addr +=3D pgsize; - tmp_pte =3D __ptep_get_and_clear(mm, addr, ptep); - if (present) { - if (pte_dirty(tmp_pte)) - pte =3D pte_mkdirty(pte); - if (pte_young(tmp_pte)) - pte =3D pte_mkyoung(pte); - } - } - return pte; -} - -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 =3D get_clear_contig(mm, addr, ptep, pgsize, ncontig); - struct vm_area_struct vma =3D TLB_FLUSH_VMA(mm, 0); - - flush_tlb_range(&vma, addr, addr + (pgsize * ncontig)); - return orig_pte; -} - pte_t *huge_pte_alloc(struct mm_struct *mm, struct vm_area_struct *vma, unsigned long addr, unsigned long sz) { @@ -260,20 +214,6 @@ pte_t arch_make_huge_pte(pte_t entry, unsigned int shi= ft, vm_flags_t flags) return entry; } =20 -pte_t huge_ptep_clear_flush(struct vm_area_struct *vma, - unsigned long addr, pte_t *ptep) -{ - struct mm_struct *mm =3D vma->vm_mm; - size_t pgsize; - int ncontig; - - if (!pte_cont(__ptep_get(ptep))) - return ptep_clear_flush(vma, addr, ptep); - - ncontig =3D find_num_contig(mm, addr, ptep, &pgsize); - return get_clear_contig_flush(mm, addr, ptep, pgsize, ncontig); -} - static int __init hugetlbpage_init(void) { /* diff --git a/arch/riscv/include/asm/hugetlb.h b/arch/riscv/include/asm/huge= tlb.h index 4c692dd82779..63c7e4fa342a 100644 --- a/arch/riscv/include/asm/hugetlb.h +++ b/arch/riscv/include/asm/hugetlb.h @@ -20,14 +20,9 @@ bool arch_hugetlb_migration_supported(struct hstate *h); #endif =20 #ifdef CONFIG_RISCV_ISA_SVNAPOT -#define __HAVE_ARCH_HUGE_PTEP_CLEAR_FLUSH -pte_t huge_ptep_clear_flush(struct vm_area_struct *vma, - unsigned long addr, pte_t *ptep); - pte_t arch_make_huge_pte(pte_t entry, unsigned int shift, vm_flags_t flags= ); #define arch_make_huge_pte arch_make_huge_pte - -#endif /*CONFIG_RISCV_ISA_SVNAPOT*/ +#endif /* CONFIG_RISCV_ISA_SVNAPOT */ =20 #include =20 diff --git a/arch/riscv/mm/hugetlbpage.c b/arch/riscv/mm/hugetlbpage.c index db13f7bcdd54..a6176415432a 100644 --- a/arch/riscv/mm/hugetlbpage.c +++ b/arch/riscv/mm/hugetlbpage.c @@ -121,45 +121,6 @@ unsigned long hugetlb_mask_last_page(struct hstate *h) return 0UL; } =20 -static pte_t get_clear_contig(struct mm_struct *mm, - unsigned long addr, - pte_t *ptep, - unsigned long ncontig) -{ - pte_t pte, tmp_pte; - bool present; - - pte =3D ptep_get_and_clear(mm, addr, ptep); - present =3D pte_present(pte); - while (--ncontig) { - ptep++; - addr +=3D PAGE_SIZE; - tmp_pte =3D ptep_get_and_clear(mm, addr, ptep); - if (present) { - if (pte_dirty(tmp_pte)) - pte =3D pte_mkdirty(pte); - if (pte_young(tmp_pte)) - pte =3D pte_mkyoung(pte); - } - } - return pte; -} - -static pte_t get_clear_contig_flush(struct mm_struct *mm, - unsigned long addr, - pte_t *ptep, - unsigned long pte_num) -{ - pte_t orig_pte =3D get_clear_contig(mm, addr, ptep, pte_num); - struct vm_area_struct vma =3D TLB_FLUSH_VMA(mm, 0); - bool valid =3D !pte_none(orig_pte); - - if (valid) - flush_tlb_range(&vma, addr, addr + (PAGE_SIZE * pte_num)); - - return orig_pte; -} - pte_t arch_make_huge_pte(pte_t entry, unsigned int shift, vm_flags_t flags) { unsigned long order; @@ -176,21 +137,6 @@ pte_t arch_make_huge_pte(pte_t entry, unsigned int shi= ft, vm_flags_t flags) return entry; } =20 -pte_t huge_ptep_clear_flush(struct vm_area_struct *vma, - unsigned long addr, - pte_t *ptep) -{ - pte_t pte =3D ptep_get(ptep); - int pte_num; - - if (!pte_napot(pte)) - return ptep_clear_flush(vma, addr, ptep); - - pte_num =3D arch_contpte_get_num_contig(vma->vm_mm, addr, ptep, 0, NULL); - - return get_clear_contig_flush(vma->vm_mm, addr, ptep, pte_num); -} - static bool is_napot_size(unsigned long size) { unsigned long order; diff --git a/include/linux/hugetlb_contpte.h b/include/linux/hugetlb_contpt= e.h index 9ec8792a2f4d..e217a3412b13 100644 --- a/include/linux/hugetlb_contpte.h +++ b/include/linux/hugetlb_contpte.h @@ -32,4 +32,8 @@ extern int huge_ptep_set_access_flags(struct vm_area_stru= ct *vma, extern void huge_ptep_set_wrprotect(struct mm_struct *mm, unsigned long addr, pte_t *ptep); =20 +#define __HAVE_ARCH_HUGE_PTEP_CLEAR_FLUSH +extern pte_t huge_ptep_clear_flush(struct vm_area_struct *vma, + unsigned long addr, pte_t *ptep); + #endif /* _LINUX_HUGETLB_CONTPTE_H */ diff --git a/mm/hugetlb_contpte.c b/mm/hugetlb_contpte.c index 629878765081..1dc211d6fbe1 100644 --- a/mm/hugetlb_contpte.c +++ b/mm/hugetlb_contpte.c @@ -242,3 +242,17 @@ void huge_ptep_set_wrprotect(struct mm_struct *mm, =20 set_contptes(mm, addr, ptep, pte, ncontig, pgsize); } + +pte_t huge_ptep_clear_flush(struct vm_area_struct *vma, + unsigned long addr, pte_t *ptep) +{ + struct mm_struct *mm =3D vma->vm_mm; + size_t pgsize; + int ncontig; + + if (!pte_cont(__ptep_get(ptep))) + return ptep_clear_flush(vma, addr, ptep); + + ncontig =3D arch_contpte_get_num_contig(mm, addr, ptep, 0, &pgsize); + return get_clear_contig_flush(mm, addr, ptep, pgsize, ncontig); +} --=20 2.39.2