From patchwork Mon Jan 27 09:35:28 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 13951130 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 66CC6C0218C for ; Mon, 27 Jan 2025 09:42:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F3497280140; Mon, 27 Jan 2025 04:42:54 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id EBE1628013F; Mon, 27 Jan 2025 04:42:54 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D37F9280140; Mon, 27 Jan 2025 04:42:54 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id B5CD328013F for ; Mon, 27 Jan 2025 04:42:54 -0500 (EST) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 6401BA20C6 for ; Mon, 27 Jan 2025 09:42:54 +0000 (UTC) X-FDA: 83052742668.01.9CD8620 Received: from mail-wr1-f51.google.com (mail-wr1-f51.google.com [209.85.221.51]) by imf22.hostedemail.com (Postfix) with ESMTP id 67417C0003 for ; Mon, 27 Jan 2025 09:42:52 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=bikNPKUH; dmarc=none; spf=pass (imf22.hostedemail.com: domain of alexghiti@rivosinc.com designates 209.85.221.51 as permitted sender) smtp.mailfrom=alexghiti@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737970972; a=rsa-sha256; cv=none; b=Yg+mHP7DCguI+viSxfBDITJuzo2K75ItrRafPEFs9zxhS6O08jjpEdpgHfBTquNtdFmSEI mB+K0tQD5q0fRCvzsGnAvfYrC/Ot+BgVX2MtU0a7EafUw1YvRUia7zViP/FAklqIjCrGK4 X8gDyzwhCyraF3B3oSNZsNaAr8fXhiM= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=bikNPKUH; dmarc=none; spf=pass (imf22.hostedemail.com: domain of alexghiti@rivosinc.com designates 209.85.221.51 as permitted sender) smtp.mailfrom=alexghiti@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1737970972; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=GkmyUQAgkdeDfCGOVLuC8IWvmfEC8o5jHyW1gtJjCzs=; b=y1nrTxVIBzfymLsx4xQdXRgyHlRR/R/1g+nMwCVVI8dM8oJN7jaLqiedrmVoAiii3cKh77 UzJtsDppzXUcdEkpMZukM+CNsXVy4Yx7NjjNXx30uAGgQs61HMixeH2W3KdOYkLl74x4YX tPlelePdvdcxOW87RZWa5Fhda5lZOJQ= Received: by mail-wr1-f51.google.com with SMTP id ffacd0b85a97d-3862a921123so2762325f8f.3 for ; Mon, 27 Jan 2025 01:42:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1737970971; x=1738575771; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=GkmyUQAgkdeDfCGOVLuC8IWvmfEC8o5jHyW1gtJjCzs=; b=bikNPKUHLaPsLZk8YpNSUappgX1/UJmIwmWKuqcFz062dZ3gm1cCKgHyWMg5a3htI7 HzVuehQJn17eaYUOLTv4wmOGNIwrOJg/JiKCyVVBKrPDlUtQkG5+C9eLU0jpeILV3YWL 89L9ORDfyTfeD8fShCb1ky8gePf9FlZZxmdXR3XDmW3ucKv4CcAK2v6PCwjNdUcMcB5Z PZhJ8cVqdc1ZtewtpI8wvpmhS2YrNHMC9DsbjVqyIJoNyJ7Mlz3zC4PRNQX3lOBkw91w sRhzodwY/MJ8SEJVmlXrgOsb9sEAJvoNY6xHTti/71useAQfbEL25GacY1s7lmpNIdIg 4vlA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737970971; x=1738575771; 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=GkmyUQAgkdeDfCGOVLuC8IWvmfEC8o5jHyW1gtJjCzs=; b=qEHmJWus0dKLrnDMPP7ER+G05TYS+VsVdHpoxLkewJLPRRtLQdKCTN/L6Lwk08PQJ0 STu38o/GLXRzkEm24zUOC3IkoAHFaB0PlqaHfZjsKLGXG4ggLlsZ/SYwAI8jmCU8H1Cp Frb7eB8B8/uxsHiHhibFuI8PKx9z9YSPp7wjDZjDPtZJi5Yw6wPceQoVgpFNGIx4LckF LEwPcZsCdNFoJqrTCownQIXeMZnr5Wpv3eQErg9Q3vbuVdBDdjpja7WE96tJVTyGw6FL pHnSCzkjZ1W0Sn16LVowoWg72u21o246RSqCViwnsqpCz+MkOStZ9tVT7qE5PJsXZhNy YWdg== X-Forwarded-Encrypted: i=1; AJvYcCU6h/qmFXKnEDt/1Uo1zfNCiCEXFABxDF4JiUa8lCKCDkOrh734fudrjP69nDOg/ptZ35AKloCUMQ==@kvack.org X-Gm-Message-State: AOJu0Yy12OeuzOClBI/e4HA7sEhEO041lSjc5AeKMwB/rbyGJ2KMzesq +FYzcdBHYiwCkgvEB9HrR8vZbsE+UaWlMbbRuX8E6o3P4ct4Vq5252u+MgFjO/k= X-Gm-Gg: ASbGnctzD7L9FfRicBngBZt0UQmBE0Q/Mjd3QonUghGEiIhmr2MZX2icxu1V+Fq3bW4 Dfw4idopxIUZg+B27+OM7vPPYYBqpaQFeBBNCn92+sWwCv+6YFiBP84EO1dAaojC3eCHGWNSkZY bIbv2bdFmPcURsBNy4YNpkwX5S53X6a8ayFLvMdrcYyberQutRY/AZx2yxUOkO/DvPY2VypPfZV NxrOctx/TeMx85ByTHDkEpXzzN/vR8ui0r3sBNb27+B8mmGxi0p5AsAJ8kDw2poFnq+PT0f2F96 pJCAr1KY19n7+7rrZCSy55QwoXocbGM= X-Google-Smtp-Source: AGHT+IFkGD+876PDgJO8tAmQAHU0ol+3F5plCgX8+RfXXi3SGv5kqaXmgh3Sp+SaS77G8Nc/FNbA7g== X-Received: by 2002:a05:6000:1ac6:b0:385:f07b:93da with SMTP id ffacd0b85a97d-38bf57bf69bmr33979049f8f.47.1737970970947; Mon, 27 Jan 2025 01:42:50 -0800 (PST) Received: from alex-rivos.ba.rivosinc.com ([2001:861:3382:ef90:3e22:3e78:ce5a:32c3]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-38c2a17d6edsm10380540f8f.40.2025.01.27.01.42.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Jan 2025 01:42:50 -0800 (PST) From: Alexandre Ghiti To: Catalin Marinas , Will Deacon , Ryan Roberts , Mark Rutland , Paul Walmsley , Palmer Dabbelt , Albert Ou , Andrew Morton , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org, linux-mm@kvack.org Cc: Alexandre Ghiti Subject: [PATCH v4 7/9] mm: Use common huge_ptep_set_access_flags() function for riscv/arm64 Date: Mon, 27 Jan 2025 10:35:28 +0100 Message-Id: <20250127093530.19548-8-alexghiti@rivosinc.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20250127093530.19548-1-alexghiti@rivosinc.com> References: <20250127093530.19548-1-alexghiti@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 67417C0003 X-Stat-Signature: cfaakn4h4fwh9e5qxz65qntwhnnamat9 X-Rspam-User: X-HE-Tag: 1737970972-65377 X-HE-Meta: U2FsdGVkX190+855t4uls+nocLIvxnZPXbSeBX/u0mQfjFAW1/V/O32InJRVuayCNdsHUnAup8igAR4cl8gWtZOp/bqxv2RXacgviu/5zp9BnRWqUedYaNVKpwRuomOFmClhidkwVepLYvDuPEx3uYpBsYlOfzswQkNjUJYH+MZcz8HW0WpDs0oPP1VhXpHWXScJvyZEOfJibGCIb2Vxu5h0kf6SHcBDIi54MZK3PCFRZqO96rqsLt5uoTlUSbl0zjLI3cay+sdBdUx0S45ShLSrLAnpfBt9RmWPKaxb0TNvMCLH5CXX8vcp06eV9wAmA06Up73wxKh+UW0mBNhsbLS5lHyaQnYSkZcl+N15YIgOYePt6aZO7OCnG6g/FwJceZTrmbOQ2xdliVmi3y1gUOJ0YfUP8hvqCKzPUMHqoq/K3EWYgWYZLbZ0EqHvknysFtCPaz7zJWnaFoZwJBG7m1/VLEyqHf370sj6bK2HEb154omFRky1LKthtkZkLE6bW4YvCGvcEgxBHH4wWLrF6WlAbS3UAcWH90D2VMlC37P1xa6UoI55RnN8AaJFChU/vnX3FTCDEU1jcceP/uTkBZUO6CQyorOn1dS+kds+jawEm7ijy4qEhMlALAIUqnwR2zSSwe1wPQ9kAM4MRb2H3msREe7ppw9fAtSA9Vs9+CdKPHbIFOYw3K+vqjattjOMwKcucmcDUEnxQAApGVmo4tKoH+9oAV8gZRFHizr7XKHaNhNggOJF46LeNaBnUf5tqwz38J2FK0TWdeMYno/B5kBBbIC6h2+DqTJmnhOOuTyUE0umTPIKLDERchBlCpZ8/sP3ThTJt9Ft+QR8MSWOVKiQnaPVsNNDbCrNKTriUwSp8aACTcBL2R6KPq1sXwDYQ5AQDWco8ppXI0GZsFZVI6a0kehCdb/6DzQvK6Vyy1hyK3kS2/FG+PKwaIm9I4hLeS8liiwKd/eAyhQJx7a YtBR+pEg X7eeneGCAD8oNzjZfe1Zx0DZG5hOdI8Pf7Ba+LOJy9XVXwPIyg7YkdTieYErQcLKXkr6fZMehLsMIBGZQjhc6x39G10H/W6sfb32X81rBQAcyZ+aH3ubhJIYaE0wNHmR9eedMuW3v7XkIsoxYcCpjvr+BuNgHT8yG+4TTOnZmkXZ74BlobozUGUmXmJYx2pChirec+Vn0GlMVsqTriu7oa7OH2vLwETWJjykeq/jsC9YTKz3kp81wMe9LuPNlcBBpJRJTc1qJjIjIBVjrAAMr9VxqDz9RwKry+gRlFrlEpViIhtwM+TXzNshwGO63qjNc5OnxImEXxQ8ghZCgbjfXpw8ZDjleUgqLN0h1TeR6dOEcFMoaSTD5R7OUExGvJSzlE8fuf8I7kkY8XLYWuECbl23mLkaEfWNPZz56RPp/wBX8PU29uk+rftrYV8VFMBDjs5WzvlFPzApdeOLUlyr2Vjde1A== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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/mm/hugetlbpage.c | 65 --------------------------- arch/riscv/include/asm/hugetlb.h | 5 --- arch/riscv/include/asm/pgtable.h | 7 +-- arch/riscv/mm/hugetlbpage.c | 28 ------------ arch/riscv/mm/pgtable.c | 6 +-- include/linux/hugetlb_contpte.h | 5 +++ mm/hugetlb_contpte.c | 75 ++++++++++++++++++++++++++++++++ 8 files changed, 87 insertions(+), 108 deletions(-) diff --git a/arch/arm64/include/asm/hugetlb.h b/arch/arm64/include/asm/hugetlb.h index 5c605a0a2017..654f5f2f03a3 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) 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/mm/hugetlbpage.c b/arch/arm64/mm/hugetlbpage.c index 0b7a53fee55d..643ba2043f0f 100644 --- a/arch/arm64/mm/hugetlbpage.c +++ b/arch/arm64/mm/hugetlbpage.c @@ -277,71 +277,6 @@ pte_t arch_make_huge_pte(pte_t entry, unsigned int shift, vm_flags_t flags) return entry; } -/* - * huge_ptep_set_access_flags will update access flags (dirty, accesssed) - * and write permission. - * - * For a contiguous huge pte range we need to check whether or not write - * permission has to change only on the first pte in the set. Then for - * all the contiguous ptes we need to check whether or not there is a - * discrepancy between dirty or young. - */ -static int __cont_access_flags_changed(pte_t *ptep, pte_t pte, int ncontig) -{ - int i; - - if (pte_write(pte) != pte_write(__ptep_get(ptep))) - return 1; - - for (i = 0; i < ncontig; i++) { - pte_t orig_pte = __ptep_get(ptep + i); - - if (pte_dirty(pte) != pte_dirty(orig_pte)) - return 1; - - if (pte_young(pte) != pte_young(orig_pte)) - return 1; - } - - return 0; -} - -int huge_ptep_set_access_flags(struct vm_area_struct *vma, - unsigned long addr, pte_t *ptep, - pte_t pte, int dirty) -{ - int ncontig, i; - size_t pgsize = 0; - unsigned long pfn = pte_pfn(pte), dpfn; - struct mm_struct *mm = vma->vm_mm; - pgprot_t hugeprot; - pte_t orig_pte; - - if (!pte_cont(pte)) - return __ptep_set_access_flags(vma, addr, ptep, pte, dirty); - - ncontig = find_num_contig(mm, addr, ptep, &pgsize); - dpfn = pgsize >> PAGE_SHIFT; - - if (!__cont_access_flags_changed(ptep, pte, ncontig)) - return 0; - - orig_pte = get_clear_contig_flush(mm, addr, ptep, pgsize, ncontig); - - /* Make sure we don't lose the dirty or young state */ - if (pte_dirty(orig_pte)) - pte = pte_mkdirty(pte); - - if (pte_young(orig_pte)) - pte = pte_mkyoung(pte); - - hugeprot = pte_pgprot(pte); - for (i = 0; i < ncontig; i++, ptep++, addr += pgsize, pfn += dpfn) - __set_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/hugetlb.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); -#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 diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pgtable.h index 705d666e014d..290d5fbfe031 100644 --- a/arch/riscv/include/asm/pgtable.h +++ b/arch/riscv/include/asm/pgtable.h @@ -629,9 +629,8 @@ static inline void __pte_clear(struct mm_struct *mm, __set_pte_at(mm, ptep, __pte(0)); } -#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 long 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); @@ -740,6 +739,8 @@ static inline pte_t ptep_get(pte_t *ptep) #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 #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 b44023336fd9..0e2ca7327479 100644 --- a/arch/riscv/mm/hugetlbpage.c +++ b/arch/riscv/mm/hugetlbpage.c @@ -173,34 +173,6 @@ pte_t arch_make_huge_pte(pte_t entry, unsigned int shift, vm_flags_t flags) return entry; } -int huge_ptep_set_access_flags(struct vm_area_struct *vma, - unsigned long addr, - pte_t *ptep, - pte_t pte, - int dirty) -{ - struct mm_struct *mm = vma->vm_mm; - pte_t orig_pte; - int pte_num; - - if (!pte_napot(pte)) - return ptep_set_access_flags(vma, addr, ptep, pte, dirty); - - pte_num = arch_contpte_get_num_contig(vma->vm_mm, addr, ptep, 0, NULL); - - orig_pte = get_clear_contig_flush(mm, addr, ptep, pte_num); - - if (pte_dirty(orig_pte)) - pte = pte_mkdirty(pte); - - if (pte_young(orig_pte)) - pte = pte_mkyoung(pte); - - set_ptes(mm, addr, ptep, pte, pte_num); - - return true; -} - void huge_ptep_set_wrprotect(struct mm_struct *mm, unsigned long addr, pte_t *ptep) diff --git a/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 -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_contpte.h index 20d3a3e14e14..fea47035ac38 100644 --- a/include/linux/hugetlb_contpte.h +++ b/include/linux/hugetlb_contpte.h @@ -22,4 +22,9 @@ extern void huge_pte_clear(struct mm_struct *mm, unsigned long addr, extern pte_t huge_ptep_get_and_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep); +#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 0c86c6f77c29..49950c1ce615 100644 --- a/mm/hugetlb_contpte.c +++ b/mm/hugetlb_contpte.c @@ -13,6 +13,7 @@ * - __set_ptes() * - __ptep_get_and_clear() * - __pte_clear() + * - __ptep_set_access_flags() * - pte_cont() * - arch_contpte_get_num_contig() */ @@ -23,6 +24,7 @@ * - set_huge_pte_at() * - huge_pte_clear() * - huge_ptep_get_and_clear() + * - huge_ptep_set_access_flags() */ pte_t huge_ptep_get(struct mm_struct *mm, unsigned long addr, pte_t *ptep) @@ -157,3 +159,76 @@ pte_t huge_ptep_get_and_clear(struct mm_struct *mm, return get_clear_contig(mm, addr, ptep, pgsize, ncontig); } + +/* + * huge_ptep_set_access_flags will update access flags (dirty, accesssed) + * and write permission. + * + * For a contiguous huge pte range we need to check whether or not write + * permission has to change only on the first pte in the set. Then for + * all the contiguous ptes we need to check whether or not there is a + * discrepancy between dirty or young. + */ +static int __cont_access_flags_changed(pte_t *ptep, pte_t pte, int ncontig) +{ + int i; + + if (pte_write(pte) != pte_write(__ptep_get(ptep))) + return 1; + + for (i = 0; i < ncontig; i++) { + pte_t orig_pte = __ptep_get(ptep + i); + + if (pte_dirty(pte) != pte_dirty(orig_pte)) + return 1; + + if (pte_young(pte) != pte_young(orig_pte)) + return 1; + } + + return 0; +} + +static pte_t get_clear_contig_flush(struct mm_struct *mm, + unsigned long addr, + pte_t *ptep, + unsigned long pgsize, + unsigned long ncontig) +{ + pte_t orig_pte = get_clear_contig(mm, addr, ptep, pgsize, ncontig); + struct vm_area_struct vma = TLB_FLUSH_VMA(mm, 0); + + flush_tlb_range(&vma, addr, addr + (pgsize * ncontig)); + return orig_pte; +} + +int huge_ptep_set_access_flags(struct vm_area_struct *vma, + unsigned long addr, pte_t *ptep, + pte_t pte, int dirty) +{ + int ncontig; + size_t pgsize = 0; + struct mm_struct *mm = vma->vm_mm; + pte_t orig_pte; + + if (!pte_cont(pte)) + return __ptep_set_access_flags(vma, addr, ptep, pte, dirty); + + ncontig = arch_contpte_get_num_contig(vma->vm_mm, addr, ptep, 0, &pgsize); + + if (!__cont_access_flags_changed(ptep, pte, ncontig)) + return 0; + + orig_pte = get_clear_contig_flush(mm, addr, ptep, pgsize, ncontig); + + /* Make sure we don't lose the dirty or young state */ + if (pte_dirty(orig_pte)) + pte = pte_mkdirty(pte); + + if (pte_young(orig_pte)) + pte = pte_mkyoung(pte); + + set_contptes(mm, addr, ptep, pte, ncontig, pgsize); + + return 1; +}