mm: rmap: fix cache flush on THP pages
Subsystems (6)
PATCH 1/5 mm: rmap: fix cache flush on THP pages
The flush_cache_page() only remove a PAGE_SIZE sized range from the cache. However, it does not cover the full pages in a THP except a head page. Replace it with flush_cache_range() to fix this issue. At least, no problems were found due to this. Maybe because the architectures that have virtual indexed caches is less.
--- mm/rmap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
mm/rmap.c +1 -1
@@ -974,7 +974,7 @@ static bool page_mkclean_one(struct page *page, struct vm_area_struct *vma,if (!pmd_dirty (*pmd) && !pmd_write (*pmd))continue ; -flush_cache_page (vma, address,page_to_pfn (page)); +flush_cache_range (vma, address, address +HPAGE_PMD_SIZE ); entry =pmdp_invalidate (vma, address, pmd); entry =pmd_wrprotect (entry); entry =pmd_mkclean (entry);
Signature · 1 line
2.11.0
PATCH 2/5 dax: fix cache flush on PMD-mapped pages
The flush_cache_page() only remove a PAGE_SIZE sized range from the cache. However, it does not cover the full pages in a THP except a head page. Replace it with flush_cache_range() to fix this issue.
--- fs/dax.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/dax.c +1 -1
@@ -857,7 +857,7 @@ static void dax_entry_mkclean(struct address_space *mapping, pgoff_t index,if (!pmd_dirty (*pmdp) && !pmd_write (*pmdp)) goto unlock_pmd; -flush_cache_page (vma, address, pfn); +flush_cache_range (vma, address, address +HPAGE_PMD_SIZE ); pmd =pmdp_invalidate (vma, address, pmdp); pmd =pmd_wrprotect (pmd); pmd =pmd_mkclean (pmd);
Signature · 1 line
2.11.0
Re: [PATCH 2/5] dax: fix cache flush on PMD-mapped pages
Quoted 15 lines · Muchun Song
The
flush_cache_page()only remove aPAGE_SIZEsized range from the cache. However, it does not cover the full pages in a THP except a head page. Replace it withflush_cache_range()to fix this issue.Fixes f729c8c9b24f ("dax: wrprotect pmd_t in dax_mapping_entry_mkclean")Signed-off-by Muchun Song <[email protected]>--- fs/dax.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)fs/dax.c +0
-0
fs/dax.c static void dax_entry_mkclean(struct address_space *mapping, pgoff_t index, · line 860
Quoted 5 lines
fs/dax.c +1
-1@@ -857,7 +857,7 @@ static void dax_entry_mkclean(struct address_space *mapping, pgoff_t index,if (!pmd_dirty (*pmdp) && !pmd_write (*pmdp)) goto unlock_pmd; -flush_cache_page (vma, address, pfn); +flush_cache_range (vma, address, address +HPAGE_PMD_SIZE );
Same comment as for the previous one.
PATCH 3/5 mm: page_vma_mapped: support checking if a pfn is mapped into a vma
In reply to Muchun Songpage_vma_mapped_walk() is supposed to check if a page is mapped into a vma. However, not all page frames (e.g. PFN_DEV) have a associated struct page with it. There is going to be some duplicate codes similar with this function if someone want to check if a pfn (without a struct page) is mapped into a vma. So add support for checking if a pfn is mapped into a vma. In the next patch, the dax will use this new feature.
--- include/linux/rmap.h | 13 +++++++++-- mm/internal.h | 25 +++++++++++++------- mm/page_vma_mapped.c | 65 +++++++++++++++++++++++++++++++++------------------- 3 files changed, 70 insertions(+), 33 deletions(-)
include/linux/rmap.h +11 -2
@@ -204,9 +204,18 @@ int make_device_exclusive_range(struct mm_struct *mm, unsigned long start,#define PVMW_SYNC (1 << 0)/* Look for migarion entries rather than present PTEs */ #define PVMW_MIGRATION (1 << 1) +/* Walk the page table by checking the pfn instead of a struct page */ +#define PVMW_PFN_WALK (1 << 2)struct page_vma_mapped_walk { -struct page *page; +union { +struct page *page; +struct { +unsigned long pfn; +unsigned int nr; +pgoff_t index; + }; + };struct vm_area_struct *vma;unsigned long address;pmd_t *pmd;
@@ -218,7 +227,7 @@ struct page_vma_mapped_walk {static inline void page_vma_mapped_walk_done (struct page_vma_mapped_walk *pvmw) {/* HugeTLB pte is set to the relevant page table entry without pte_mapped. */ -if (pvmw->pte && !PageHuge (pvmw->page)) +if (pvmw->pte && ((pvmw->flags &PVMW_PFN_WALK ) || !PageHuge (pvmw->page)))pte_unmap (pvmw->pte);if (pvmw->ptl)spin_unlock (pvmw->ptl);
mm/internal.h +17 -8
@@ -478,25 +478,34 @@ vma_address(struct page *page, struct vm_area_struct *vma) }/* -* Then at what user virtual address will none of the page be found in vma? -* Assumes that vma_address() already returned a good starting address. -* If page is a compound head, the entire compound page is considered. +* Return the end of user virtual address at the specific offset within +* a vma. */ static inline unsigned long -vma_address_end (struct page *page,struct vm_area_struct *vma) +vma_pgoff_address_end (pgoff_t pgoff,unsigned long nr_pages, +struct vm_area_struct *vma) { -pgoff_t pgoff;unsigned long address; -VM_BUG_ON_PAGE (PageKsm (page), page);/* KSM page->index unusable */ - pgoff =page_to_pgoff (page) +compound_nr (page); - address = vma->vm_start + ((pgoff - vma->vm_pgoff) <<PAGE_SHIFT ); + address = vma->vm_start + ((pgoff + nr_pages - vma->vm_pgoff) <<PAGE_SHIFT );/* Check for address beyond vma (or wrapped through 0?) */ if (address < vma->vm_start || address > vma->vm_end) address = vma->vm_end;return address; } +/* +* Then at what user virtual address will none of the page be found in vma? +* Assumes that vma_address() already returned a good starting address. +* If page is a compound head, the entire compound page is considered. +*/ +static inline unsigned long +vma_address_end (struct page *page,struct vm_area_struct *vma) +{ +VM_BUG_ON_PAGE (PageKsm (page), page);/* KSM page->index unusable */ +return vma_pgoff_address_end (page_to_pgoff (page),compound_nr (page), vma); +} +static inline struct file *maybe_unlock_mmap_for_io (struct vm_fault *vmf,struct file *fpin) {
mm/page_vma_mapped.c +42 -23
137 lines not on this page · raw message
Signature · 1 line
2.11.0
Re: [PATCH 3/5] mm: page_vma_mapped: support checking if a pfn is mapped into a vma
include/linux/rmap.h struct page_vma_mapped_walk { · line 230
Quoted 1 line · Muchun Song
include/linux/rmap.h +1
-0+if (pvmw->pte && ((pvmw->flags &PVMW_PFN_WALK ) || !PageHuge (pvmw->page)))
Please avoid the overly long line here and in a few other places.
Quoted 2 lines
+/*
+ * Then at what user virtual address will none of the page be found in vma?
Doesn't parse, what is this trying to say?
Re: [PATCH 3/5] mm: page_vma_mapped: support checking if a pfn is mapped into a vma
Quoted 5 lines · Christoph Hellwig
Quoted 1 line · Muchun Song
+ if (
pvmw->pte&& ((pvmw->flags&PVMW_PFN_WALK) || !PageHuge(pvmw->page)))Please avoid the overly long line here and in a few other places.
OK.
Quoted 5 lines
Quoted 3 lines
+/*
+ * Then at what user virtual address will none of the page be found in vma?Doesn't parse, what is this trying to say?
Well, I am also confused.
BTW, this is not introduced by me, it is introduced by:
commit 37ffe9f4d7ff ("mm/thp: fix vma_address() if virtual address
below file offset")
If it is really confusing, I can replace this line with:
"Return the end user virtual address of a page within a vma"
Thanks.
kernel test robot
build botRe: [PATCH 3/5] mm: page_vma_mapped: support checking if a pfn is mapped into a vma
In reply to Muchun SongThank you for the patch! Yet something to improve:
Hi Muchun,
Thank you for the patch! Yet something to improve:
[auto build test ERROR on hnaz-mm/master]
url: https://github.com/0day-ci/linux/commits/Muchun-Song/mm-rmap-fix-cache-flush-on-THP-pages/20220121-160020 base: https://github.com/hnaz/linux-mm master config: powerpc-allnoconfig (https://download.01.org/0day-ci/archive/20220125/202201250022.zqRQezMj-lkp(a)intel.com/config) compiler: powerpc-linux-gcc (GCC) 11.2.0
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/0c83393977dfe53edadec9793328b7281e8af7aa
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Muchun-Song/mm-rmap-fix-cache-flush-on-THP-pages/20220121-160020 git checkout 0c83393977dfe53edadec9793328b7281e8af7aa
# save the config file to linux build tree
mkdir build_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.2.0 make.cross O=build_dir ARCH=powerpc SHELL=/bin/bash
If you fix the issue, kindly add following tag as appropriate
All errors (new ones prefixed by >>):
mm/page_vma_mapped.c: In function 'page_vma_mapped_walk':
>> mm/page_vma_mapped.c:246:37: error: implicit declaration of function 'pmd_pfn'; did you mean 'pmd_off'? [-Werror=implicit-function-declaration]
246 | if (pmd_pfn(pmde) != pfn)
| ^~~~~~~
| pmd_offcc1: some warnings being treated as errors
vim +246 mm/page_vma_mapped.c
134
135 /**
136 * page_vma_mapped_walk - check if @pvmw->page or @pvmw->pfn is mapped in
137 * @pvmw->vma at @pvmw->address
138 * @pvmw: pointer to struct page_vma_mapped_walk. page (or pfn and nr and
139 * index), vma, address and flags must be set. pmd, pte and ptl must be NULL.
140 *
141 * Returns true if the page or pfn is mapped in the vma. @pvmw->pmd and
142 * @pvmw->pte point to relevant page table entries. @pvmw->ptl is locked.
143 * @pvmw->address is adjusted if needed (for PTE-mapped THPs).
144 *
145 * If @pvmw->pmd is set but @pvmw->pte is not, you have found PMD-mapped page
146 * (usually THP or Huge DEVMAP). For PMD-mapped page, you should run
147 * page_vma_mapped_walk() in a loop to find all PTEs that map the huge page.
148 *
149 * For HugeTLB pages, @pvmw->pte is set to the relevant page table entry
150 * regardless of which page table level the page is mapped at. @pvmw->pmd is
151 * NULL.
152 *
153 * Returns false if there are no more page table entries for the page or pfn in
154 * the vma. @pvmw->ptl is unlocked and @pvmw->pte is unmapped.
155 *
156 * If you need to stop the walk before page_vma_mapped_walk() returned false,
157 * use page_vma_mapped_walk_done(). It will do the housekeeping.
158 */
159 bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)
160 {
161 struct mm_struct *mm = pvmw->vma->vm_mm;
162 struct page *page;
163 unsigned long end;
164 unsigned long pfn;
165 pgd_t *pgd;
166 p4d_t *p4d;
167 pud_t *pud;
168 pmd_t pmde;
169
170 if (pvmw->flags & PVMW_PFN_WALK) {
171 page = NULL;
172 pfn = pvmw->pfn;
173 } else {
174 page = pvmw->page;
175 pfn = page_to_pfn(page);
176 }
177
178 /* The only possible pmd mapping has been handled on last iteration */
179 if (pvmw->pmd && !pvmw->pte)
180 return not_found(pvmw);
181
182 if (unlikely(page && PageHuge(page))) {
183 /* The only possible mapping was handled on last iteration */
184 if (pvmw->pte)
185 return not_found(pvmw);
186
187 /* when pud is not present, pte will be NULL */
188 pvmw->pte = huge_pte_offset(mm, pvmw->address, page_size(page));
189 if (!pvmw->pte)
190 return false;
191
192 pvmw->ptl = huge_pte_lockptr(page_hstate(page), mm, pvmw->pte);
193 spin_lock(pvmw->ptl);
194 if (!check_pte(pvmw))
195 return not_found(pvmw);
196 return true;
197 }
198
199 /*
200 * Seek to next pte only makes sense for THP.
201 * But more important than that optimization, is to filter out
202 * any PageKsm page: whose page->index misleads vma_address()
203 * and vma_address_end() to disaster.
204 */
205 if (page)
206 end = PageTransCompound(page) ?
207 vma_address_end(page, pvmw->vma) :
208 pvmw->address + PAGE_SIZE;
209 else
210 end = vma_pgoff_address_end(pvmw->index, pvmw->nr, pvmw->vma);
211
212 if (pvmw->pte)
213 goto next_pte;
214 restart:
215 do {
216 pgd = pgd_offset(mm, pvmw->address);
217 if (!pgd_present(*pgd)) {
218 step_forward(pvmw, PGDIR_SIZE);
219 continue;
220 }
221 p4d = p4d_offset(pgd, pvmw->address);
222 if (!p4d_present(*p4d)) {
223 step_forward(pvmw, P4D_SIZE);
224 continue;
225 }
226 pud = pud_offset(p4d, pvmw->address);
227 if (!pud_present(*pud)) {
228 step_forward(pvmw, PUD_SIZE);
229 continue;
230 }
231
232 pvmw->pmd = pmd_offset(pud, pvmw->address);
233 /*
234 * Make sure the pmd value isn't cached in a register by the
235 * compiler and used as a stale value after we've observed a
236 * subsequent update.
237 */
238 pmde = READ_ONCE(*pvmw->pmd);
239
240 if (pmd_leaf(pmde) || is_pmd_migration_entry(pmde)) {
241 pvmw->ptl = pmd_lock(mm, pvmw->pmd);
242 pmde = *pvmw->pmd;
243 if (likely(pmd_leaf(pmde))) {
244 if (pvmw->flags & PVMW_MIGRATION)
245 return not_found(pvmw);
> 246 if (pmd_pfn(pmde) != pfn)
247 return not_found(pvmw);
248 return true;
249 }
250 if (!pmd_present(pmde)) {
251 swp_entry_t entry;
252
253 if (!thp_migration_supported() ||
254 !(pvmw->flags & PVMW_MIGRATION))
255 return not_found(pvmw);
256 entry = pmd_to_swp_entry(pmde);
257 if (!is_migration_entry(entry) ||
258 page_to_pfn(pfn_swap_entry_to_page(entry)) != pfn)
259 return not_found(pvmw);
260 return true;
261 }
262 /* THP pmd was split under us: handle on pte level */
263 spin_unlock(pvmw->ptl);
264 pvmw->ptl = NULL;
265 } else if (!pmd_present(pmde)) {
266 /*
267 * If PVMW_SYNC, take and drop THP pmd lock so that we
268 * cannot return prematurely, while zap_huge_pmd() has
269 * cleared *pmd but not decremented compound_mapcount().
270 */
271 if ((pvmw->flags & PVMW_SYNC) && page &&
272 PageTransCompound(page)) {
273 spinlock_t *ptl = pmd_lock(mm, pvmw->pmd);
274
275 spin_unlock(ptl);
276 }
277 step_forward(pvmw, PMD_SIZE);
278 continue;
279 }
280 if (!map_pte(pvmw))
281 goto next_pte;
282 this_pte:
283 if (check_pte(pvmw))
284 return true;
285 next_pte:
286 do {
287 pvmw->address += PAGE_SIZE;
288 if (pvmw->address >= end)
289 return not_found(pvmw);
290 /* Did we cross page table boundary? */
291 if ((pvmw->address & (PMD_SIZE - PAGE_SIZE)) == 0) {
292 if (pvmw->ptl) {
293 spin_unlock(pvmw->ptl);
294 pvmw->ptl = NULL;
295 }
296 pte_unmap(pvmw->pte);
297 pvmw->pte = NULL;
298 goto restart;
299 }
300 pvmw->pte++;
301 if ((pvmw->flags & PVMW_SYNC) && !pvmw->ptl) {
302 pvmw->ptl = pte_lockptr(mm, pvmw->pmd);
303 spin_lock(pvmw->ptl);
304 }
305 } while (pte_none(*pvmw->pte));
306
307 if (!pvmw->ptl) {
308 pvmw->ptl = pte_lockptr(mm, pvmw->pmd);
309 spin_lock(pvmw->ptl);
310 }
311 goto this_pte;
312 } while (pvmw->address < end);
313
314 return false;
315 }
316
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/[email protected]
PATCH 4/5 dax: fix missing writeprotect the pte entry
In reply to Muchun SongCurrently dax_mapping_entry_mkclean() fails to clean and write protect the pte entry within a DAX PMD entry during an *sync operation. This can result in data loss in the following sequence:
1) process A mmap write to DAX PMD, dirtying PMD radix tree entry and making the pmd entry dirty and writeable.
2) process B mmap with the @offset (e.g. 4K) and @length (e.g. 4K) write to the same file, dirtying PMD radix tree entry (already done in 1)) and making the pte entry dirty and writeable.
3) fsync, flushing out PMD data and cleaning the radix tree entry. We currently fail to mark the pte entry as clean and write protected since the vma of process B is not covered in dax_entry_mkclean().
4) process B writes to the pte. These don't cause any page faults since the pte entry is dirty and writeable. The radix tree entry remains clean.
5) fsync, which fails to flush the dirty PMD data because the radix tree entry was clean.
6) crash - dirty data that should have been fsync'd as part of 5) could still have been in the processor cache, and is lost.
Reuse some infrastructure of page_mkclean_one() to let DAX can handle similar case to fix this issue.
--- fs/dax.c | 78 +++++----------------------------------------------- include/linux/rmap.h | 9 ++++++ mm/internal.h | 27 ++++++++++++------ mm/rmap.c | 69 ++++++++++++++++++++++++++++++++++------------ 4 files changed, 85 insertions(+), 98 deletions(-)
fs/dax.c +7 -71
@@ -25,6 +25,7 @@#include <linux/sizes.h> #include <linux/mmu_notifier.h> #include <linux/iomap.h> +#include <linux/rmap.h> #include <asm/pgalloc.h> #define CREATE_TRACE_POINTS
@@ -801,86 +802,21 @@ static void *dax_insert_entry(struct xa_state *xas,return entry; } -static inline -unsigned long pgoff_address (pgoff_t pgoff,struct vm_area_struct *vma) -{ -unsigned long address; - - address = vma->vm_start + ((pgoff - vma->vm_pgoff) <<PAGE_SHIFT ); -VM_BUG_ON_VMA (address < vma->vm_start || address >= vma->vm_end, vma); -return address; -} -/* Walk all mappings of a given index of a file and writeprotect them */ -static void dax_entry_mkclean (struct address_space *mapping,pgoff_t index, -unsigned long pfn) +static void dax_entry_mkclean (struct address_space *mapping,unsigned long pfn, +unsigned long npfn,pgoff_t pgoff_start) {struct vm_area_struct *vma; -pte_t pte, *ptep =NULL ; -pmd_t *pmdp =NULL ; -spinlock_t *ptl; +pgoff_t pgoff_end = pgoff_start + npfn -1 ;i_mmap_lock_read (mapping); - vma_interval_tree_foreach(vma, &mapping->i_mmap, index, index) { -struct mmu_notifier_range range; -unsigned long address; - + vma_interval_tree_foreach(vma, &mapping->i_mmap, pgoff_start, pgoff_end) {cond_resched ();if (!(vma->vm_flags &VM_SHARED ))continue ; - address =pgoff_address (index, vma); - -/* -* follow_invalidate_pte() will use the range to call -* mmu_notifier_invalidate_range_start() on our behalf before -* taking any lock. -*/ -if (follow_invalidate_pte (vma->vm_mm, address, &range, &ptep, - &pmdp, &ptl)) -continue ; - -/* -* No need to call mmu_notifier_invalidate_range() as we are -* downgrading page table protection not changing it to point -* to a new page. -* -* See Documentation/vm/mmu_notifier.rst -*/ -if (pmdp) { -#ifdef CONFIG_FS_DAX_PMD -pmd_t pmd; - -if (pfn !=pmd_pfn (*pmdp)) - goto unlock_pmd; -if (!pmd_dirty (*pmdp) && !pmd_write (*pmdp)) - goto unlock_pmd; - -flush_cache_range (vma, address, address +HPAGE_PMD_SIZE ); - pmd =pmdp_invalidate (vma, address, pmdp); - pmd =pmd_wrprotect (pmd); - pmd =pmd_mkclean (pmd); -set_pmd_at (vma->vm_mm, address, pmdp, pmd); -unlock_pmd: -#endif -spin_unlock (ptl); - }else { -if (pfn !=pte_pfn (*ptep)) - goto unlock_pte; -if (!pte_dirty (*ptep) && !pte_write (*ptep)) - goto unlock_pte; - -flush_cache_page (vma, address, pfn); - pte =ptep_clear_flush (vma, address, ptep); - pte =pte_wrprotect (pte); - pte =pte_mkclean (pte); -set_pte_at (vma->vm_mm, address, ptep, pte); -unlock_pte: -pte_unmap_unlock (ptep, ptl); - } - -mmu_notifier_invalidate_range_end (&range); +pfn_mkclean_range (pfn, npfn, pgoff_start, vma); }i_mmap_unlock_read (mapping); }
@@ -948,7 +884,7 @@ static int dax_writeback_one(struct xa_state *xas, struct dax_device *dax_dev, count =1UL <<dax_entry_order (entry); index = xas->xa_index & ~(count -1 ); -dax_entry_mkclean (mapping, index, pfn); +dax_entry_mkclean (mapping, pfn, count, index);dax_flush (dax_dev,page_address (pfn_to_page (pfn)), count *PAGE_SIZE );/* * After we have flushed the cache, we can clear the dirty tag. There
include/linux/rmap.h +9 -0
@@ -236,6 +236,15 @@ static inline void page_vma_mapped_walk_done(struct page_vma_mapped_walk *pvmw)bool page_vma_mapped_walk (struct page_vma_mapped_walk *pvmw);/* +* Cleans the PTEs of shared mappings. +* (and since clean PTEs should also be readonly, write protects them too) +* +* returns the number of cleaned PTEs. +*/ +int pfn_mkclean_range (unsigned long pfn,int npfn,pgoff_t pgoff_start, +struct vm_area_struct *vma); + +/* * Used by swapoff to help locate where page is expected in vma. */ unsigned long page_address_in_vma (struct page *,struct vm_area_struct *);
mm/internal.h +18 -9
@@ -449,26 +449,22 @@ extern void clear_page_mlock(struct page *page);extern pmd_t maybe_pmd_mkwrite (pmd_t pmd,struct vm_area_struct *vma);/* -* At what user virtual address is page expected in vma? -* Returns -EFAULT if all of the page is outside the range of vma. -* If page is a compound head, the entire compound page is considered. +* Return the start of user virtual address at the specific offset within +* a vma. */ static inline unsigned long -vma_address (struct page *page,struct vm_area_struct *vma) +vma_pgoff_address (pgoff_t pgoff,unsigned long nr_pages, +struct vm_area_struct *vma) { -pgoff_t pgoff;unsigned long address; -VM_BUG_ON_PAGE (PageKsm (page), page);/* KSM page->index unusable */ - pgoff =page_to_pgoff (page);if (pgoff >= vma->vm_pgoff) { address = vma->vm_start + ((pgoff - vma->vm_pgoff) <<PAGE_SHIFT );/* Check for address beyond vma (or wrapped through 0?) */ if (address < vma->vm_start || address >= vma->vm_end) address = -EFAULT ; - }else if (PageHead (page) && - pgoff +compound_nr (page) -1 >= vma->vm_pgoff) { + }else if (pgoff + nr_pages -1 >= vma->vm_pgoff) {/* Test above avoids possibility of wrap to 0 on 32-bit */ address = vma->vm_start; }else {
@@ -477,6 +473,19 @@ vma_address(struct page *page, struct vm_area_struct *vma)return address; } + +/* +* At what user virtual address is page expected in vma? +* Returns -EFAULT if all of the page is outside the range of vma. +* If page is a compound head, the entire compound page is considered. +*/ +static inline unsigned long +vma_address (struct page *page,struct vm_area_struct *vma) +{ +VM_BUG_ON_PAGE (PageKsm (page), page);/* KSM page->index unusable */ +return vma_pgoff_address (page_to_pgoff (page),compound_nr (page), vma); +} +/* * Return the end of user virtual address at the specific offset within * a vma.
mm/rmap.c +51 -18
103 lines not on this page · raw message
Signature · 1 line
2.11.0
Re: [PATCH 4/5] dax: fix missing writeprotect the pte entry
Quoted 2 lines · Muchun Song
Reuse some infrastructure of
page_mkclean_one()to let DAX can handle similar case to fix this issue.
Can you split out some of the infrastructure changes into proper well-documented preparation patches?
fs/dax.c static void *dax_insert_entry(struct xa_state *xas, · line 813
Quoted 8 lines
fs/dax.c +2
-4+pgoff_t pgoff_end = pgoff_start + npfn -1 ;i_mmap_lock_read (mapping); - vma_interval_tree_foreach(vma, &mapping->i_mmap, index, index) { -struct mmu_notifier_range range; -unsigned long address; - + vma_interval_tree_foreach(vma, &mapping->i_mmap, pgoff_start, pgoff_end) {
Please avoid the overly long lines here. Just using start and end might be an easy option.
Re: [PATCH 4/5] dax: fix missing writeprotect the pte entry
Quoted 7 lines · Christoph Hellwig
Quoted 2 lines · Muchun Song
Reuse some infrastructure of
page_mkclean_one()to let DAX can handle similar case to fix this issue.Can you split out some of the infrastructure changes into proper well-documented preparation patches?
Will do. I'll introduce page_vma_mkclean_one in a prepared patch and then fix the DAX issue in a separate patch. Thanks for your suggestions.
Quoted 10 lines
fs/dax.c static void *dax_insert_entry(struct xa_state *xas, · line 813
Quoted 9 lines
fs/dax.c +2
-4+pgoff_t pgoff_end = pgoff_start + npfn -1 ;i_mmap_lock_read (mapping); - vma_interval_tree_foreach(vma, &mapping->i_mmap, index, index) { -struct mmu_notifier_range range; -unsigned long address; - + vma_interval_tree_foreach(vma, &mapping->i_mmap, pgoff_start, pgoff_end) {
Quoted 3 lines
Please avoid the overly long lines here. Just using start and end might be an easy option.
Will do.
Thanks.
PATCH 5/5 mm: remove range parameter from follow_invalidate_pte()
In reply to Muchun SongThe only user (DAX) of range parameter of follow_invalidate_pte() is gone, it safe to remove the range paramter and make it static to simlify the code.
--- include/linux/mm.h | 3 --- mm/memory.c | 23 +++-------------------- 2 files changed, 3 insertions(+), 23 deletions(-)
include/linux/mm.h +0 -3
@@ -1814,9 +1814,6 @@ void free_pgd_range(struct mmu_gather *tlb, unsigned long addr, unsigned long end, unsigned long floor, unsigned long ceiling);int copy_page_range (struct vm_area_struct *dst_vma,struct vm_area_struct *src_vma); -int follow_invalidate_pte (struct mm_struct *mm,unsigned long address, -struct mmu_notifier_range *range,pte_t **ptepp, -pmd_t **pmdpp,spinlock_t **ptlp);int follow_pte (struct mm_struct *mm,unsigned long address,pte_t **ptepp,spinlock_t **ptlp); int follow_pfn(struct vm_area_struct *vma, unsigned long address,
mm/memory.c +3 -20
@@ -4869,9 +4869,8 @@ int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address) }#endif /* __PAGETABLE_PMD_FOLDED */ -int follow_invalidate_pte (struct mm_struct *mm,unsigned long address, -struct mmu_notifier_range *range,pte_t **ptepp, -pmd_t **pmdpp,spinlock_t **ptlp) +static int follow_invalidate_pte (struct mm_struct *mm,unsigned long address, +pte_t **ptepp,pmd_t **pmdpp,spinlock_t **ptlp) {pgd_t *pgd;p4d_t *p4d;
@@ -4898,31 +4897,17 @@ int follow_invalidate_pte(struct mm_struct *mm, unsigned long address,if (!pmdpp) goto out; -if (range) { -mmu_notifier_range_init (range,MMU_NOTIFY_CLEAR ,0 , -NULL , mm, address &PMD_MASK , - (address &PMD_MASK ) +PMD_SIZE ); -mmu_notifier_invalidate_range_start (range); - } *ptlp =pmd_lock (mm, pmd);if (pmd_huge (*pmd)) { *pmdpp = pmd;return 0 ; }spin_unlock (*ptlp); -if (range) -mmu_notifier_invalidate_range_end (range); }if (pmd_none (*pmd) ||unlikely (pmd_bad (*pmd))) goto out; -if (range) { -mmu_notifier_range_init (range,MMU_NOTIFY_CLEAR ,0 ,NULL , mm, - address &PAGE_MASK , - (address &PAGE_MASK ) +PAGE_SIZE ); -mmu_notifier_invalidate_range_start (range); - } ptep =pte_offset_map_lock (mm, pmd, address, ptlp);if (!pte_present (*ptep)) goto unlock;
@@ -4930,8 +4915,6 @@ int follow_invalidate_pte(struct mm_struct *mm, unsigned long address,return 0 ; unlock:pte_unmap_unlock (ptep, *ptlp); -if (range) -mmu_notifier_invalidate_range_end (range); out:return -EINVAL ; }
@@ -4960,7 +4943,7 @@ int follow_invalidate_pte(struct mm_struct *mm, unsigned long address,int follow_pte (struct mm_struct *mm,unsigned long address,pte_t **ptepp,spinlock_t **ptlp) { -return follow_invalidate_pte (mm, address,NULL , ptepp,NULL , ptlp); +return follow_invalidate_pte (mm, address, ptepp,NULL , ptlp); }EXPORT_SYMBOL_GPL (follow_pte);
Signature · 1 line
2.11.0
Quoted 6 lines · Muchun Song
The
flush_cache_page()only remove aPAGE_SIZEsized range from the cache. However, it does not cover the full pages in a THP except a head page. Replace it withflush_cache_range()to fix this issue. At least, no problems were found due to this. Maybe because the architectures that have virtual indexed caches is less.
Yeah, actually flush_cache_page()/flush_cache_range() are no-op for the most architectures which have THP supported, i.e. x86, aarch64, powerpc, etc.
And currently just tmpfs and read-only files support PMD-mapped THP, but both don't have to do writeback. And it seems DAX doesn't have writeback either, which uses __set_page_dirty_no_writeback() for set_page_dirty. So this code should never be called IIUC.
But anyway your fix looks correct to me. Reviewed-by: Yang Shi <[email protected]>
Quoted 23 lines
Fixes f27176cfc363 ("mm: convert page_mkclean_one() to use page_vma_mapped_walk()")Signed-off-by Muchun Song <[email protected]>--- mm/rmap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)mm/rmap.c +1
-2@@ -974,7 +974,7 @@ static bool page_mkclean_one(struct page *page, struct vm_area_struct *vma,if (!pmd_dirty (*pmd) && !pmd_write (*pmd))continue ; -flush_cache_page (vma, address,page_to_pfn (page)); +flush_cache_range (vma, address, address +HPAGE_PMD_SIZE ); entry =pmdp_invalidate (vma, address, pmd); entry =pmd_wrprotect (entry); entry =pmd_mkclean (entry); --2.11.0
mm/rmap.c static bool page_mkclean_one(struct page *page, struct vm_area_struct *vma, · line 977
Quoted 12 lines · Muchun Song
1 file changed, 1 insertion(+), 1 deletion(-)mm/rmap.c +1
-1@@ -974,7 +974,7 @@ static bool page_mkclean_one(struct page *page, struct vm_area_struct *vma,if (!pmd_dirty (*pmd) && !pmd_write (*pmd))continue ; -flush_cache_page (vma, address,page_to_pfn (page)); +flush_cache_range (vma, address, address +HPAGE_PMD_SIZE );
Do we need a flush_cache_folio here given that we must be dealing with what effectively is a folio here?
Also please avoid the overly long line.
Quoted 12 lines · Christoph Hellwig
Quoted 10 lines · Muchun Song
1 file changed, 1 insertion(+), 1 deletion(-)mm/rmap.c +0
-0@@ -974,7 +974,7 @@ static bool page_mkclean_one(struct page *page, struct vm_area_struct *vma,if (!pmd_dirty (*pmd) && !pmd_write (*pmd))continue ;
Quoted 5 lines
mm/rmap.c static bool page_mkclean_one(struct page *page, struct vm_area_struct *vma, · line 977
Quoted 2 lines
mm/rmap.c +1
-1-flush_cache_page (vma, address,page_to_pfn (page)); +flush_cache_range (vma, address, address +HPAGE_PMD_SIZE );Do we need a
flush_cache_foliohere given that we must be dealing with what effectively is a folio here?
I think it is a future improvement. I suspect it will be easy if someone wants to backport this patch. If we do not want someone to do this, I think it is better to introduce flush_cache_folio in this patch. What do you think?
Quoted 3 lines
Also please avoid the overly long line.
OK.
Thanks.