Skip to contentSkip to conversation

write fault on dax mapping and usage of set_pte_at.

DISCUSSION Jan Kara 3 participants 8 messages Feb 21, 2019 – Mar 2, 2019 1 message not in the archive

Subsystems (3)
lore
Share & tools
lore

Jan Kara

author reviewer
#1 lore raw
In reply to a message not in the archive

Hi Aneesh,

Quoted 17 lines · Aneesh Kumar K.V

We found this while testing dax with XFS, but i guess this is true for other file systems too. The stack trace looks as

[c00000000007610c] set_pte_at+0x3c/0x190
LR [c000000000378628] insert_pfn+0x208/0x280

Call Trace:

[c0000002125df980] [8000000000000104] 0x8000000000000104 (unreliable) [c0000002125df9c0] [c000000000378488] insert_pfn+0x68/0x280 [c0000002125dfa30] [c0000000004a5494] dax_iomap_pte_fault.isra.7+0x734/0xa40 [c0000002125dfb50] [c000000000627250] __xfs_filemap_fault+0x280/0x2d0 [c0000002125dfbb0] [c000000000373abc] do_wp_page+0x48c/0xa40 [c0000002125dfc00] [c000000000379170] __handle_mm_fault+0x8d0/0x1fd0 [c0000002125dfd00] [c00000000037a9b0] handle_mm_fault+0x140/0x250 [c0000002125dfd40] [c000000000074bb0] __do_page_fault+0x300/0xd60 [c0000002125dfe20] [c00000000000acf4] handle_page_fault+0x18

Quoted 8 lines

Now that is WARN_ON in set_pte_at which is

	VM_WARN_ON(pte_hw_valid(*ptep) && !pte_protnone(*ptep));

Multiple architecture optimize set_pte_at based on the assumption that we will never use set_pte_at to update a valid pte entry. This helps in avoid flushing tlb etc. We should be using ptep_set_access_flags for this.

Hum, I didn't know about this assumption and neither did lot of other people reviewing DAX patches. Is this documented somewhere?

Quoted 2 lines

I guess iomap code doesn't handle this correctly? Or am I missing some other ways we can end up flushing tlb?

So for RW->RO transition we use ptep_clear_flush() in dax_entry_mkclean() so that one is certainly safe. Similarly for unmapping. The RO->RW transition does not seem to have any TLB flush so there TLB could still carry stale information but it's the same as with normal page faults on invalid PTEs or with protection faults for normal pages (see e.g. finish_mkwrite_fault()). The only thing that's remaining is a situation when we replace a PTE with zero page with a PTE pointing to a real storage (block allocation on protection fault). However in this case we do unmap_mapping_pages() in dax_insert_entry() so the PTE actually gets cleared before we install a new correct block mapping. So this case is safe as well. Am I missing something?

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#2 lore raw
Quoted 35 lines · Jan Kara

Hi Aneesh,

Quoted 17 lines · Aneesh Kumar K.V

We found this while testing dax with XFS, but i guess this is true for other file systems too. The stack trace looks as

[c00000000007610c] set_pte_at+0x3c/0x190
LR [c000000000378628] insert_pfn+0x208/0x280

Call Trace:

[c0000002125df980] [8000000000000104] 0x8000000000000104 (unreliable) [c0000002125df9c0] [c000000000378488] insert_pfn+0x68/0x280 [c0000002125dfa30] [c0000000004a5494] dax_iomap_pte_fault.isra.7+0x734/0xa40 [c0000002125dfb50] [c000000000627250] __xfs_filemap_fault+0x280/0x2d0 [c0000002125dfbb0] [c000000000373abc] do_wp_page+0x48c/0xa40 [c0000002125dfc00] [c000000000379170] __handle_mm_fault+0x8d0/0x1fd0 [c0000002125dfd00] [c00000000037a9b0] handle_mm_fault+0x140/0x250 [c0000002125dfd40] [c000000000074bb0] __do_page_fault+0x300/0xd60 [c0000002125dfe20] [c00000000000acf4] handle_page_fault+0x18

Quoted 8 lines

Now that is WARN_ON in set_pte_at which is

	VM_WARN_ON(pte_hw_valid(*ptep) && !pte_protnone(*ptep));

Multiple architecture optimize set_pte_at based on the assumption that we will never use set_pte_at to update a valid pte entry. This helps in avoid flushing tlb etc. We should be using ptep_set_access_flags for this.

Hum, I didn't know about this assumption and neither did lot of other people reviewing DAX patches. Is this documented somewhere?

Quoted 2 lines

I guess iomap code doesn't handle this correctly? Or am I missing some other ways we can end up flushing tlb?

Quoted 6 lines

So for RW->RO transition we use ptep_clear_flush() in dax_entry_mkclean() so that one is certainly safe. Similarly for unmapping. The RO->RW transition does not seem to have any TLB flush so there TLB could still carry stale information but it's the same as with normal page faults on invalid PTEs or with protection faults for normal pages (see e.g. finish_mkwrite_fault()).

I am not sure i understood that. RO -> RW transition can have stale TLB entries with RO mapping in them. So architecture do flush TLB during the fault, some may not. We do have a NestMMU issue with that transition which requires us to do mark the pte invalid and flush TLB for that transition. (see commit bd5050e38aec3055ff4257ade987d808ac93b582 )

For invalid PTEs we should have a TLB entry at all.

finish_mkwrite_fault do use wp_page_reuse() which does the right thing.

Quoted 7 lines

The only thing that's remaining is a situation
when we replace a PTE with zero page with a PTE pointing to a real storage (block allocation on protection fault). However in this case we do unmap_mapping_pages() in dax_insert_entry() so the PTE actually gets cleared before we install a new correct block mapping. So this case is safe as well. Am I missing something?

Do pfn_mkwrite callback need to insert the pfn details for a RO->RW fault type? Can't we skip that pfn insert and let finish_mkwrite_fault handle that pte update?

-aneesh

#3 lore raw
Quoted 18 lines · Aneesh Kumar K.V
Quoted 10 lines · Jan Kara

Hi Aneesh,

Quoted 6 lines · Aneesh Kumar K.V

We found this while testing dax with XFS, but i guess this is true for other file systems too. The stack trace looks as

I guess iomap code doesn't handle this correctly? Or am I missing some other ways we can end up flushing tlb?

Quoted 6 lines

So for RW->RO transition we use ptep_clear_flush() in dax_entry_mkclean() so that one is certainly safe. Similarly for unmapping. The RO->RW transition does not seem to have any TLB flush so there TLB could still carry stale information but it's the same as with normal page faults on invalid PTEs or with protection faults for normal pages (see e.g. finish_mkwrite_fault()).

Quoted 7 lines

I am not sure i understood that. RO -> RW transition can have stale TLB entries with RO mapping in them. So architecture do flush TLB during the fault, some may not. We do have a NestMMU issue with that transition which requires us to do mark the pte invalid and flush TLB for that transition. (see commit bd5050e38aec3055ff4257ade987d808ac93b582 )

For invalid PTEs we should have a TLB entry at all.

For invalid PTEs we should not have a TLB entry at all.

-aneesh

author reviewer
#4 lore raw
In reply to Aneesh Kumar K.V
Quoted 25 lines · Aneesh Kumar K.V
Quoted 24 lines · Jan Kara

Hi Aneesh,

Quoted 21 lines · Aneesh Kumar K.V

We found this while testing dax with XFS, but i guess this is true for other file systems too. The stack trace looks as

[c00000000007610c] set_pte_at+0x3c/0x190
LR [c000000000378628] insert_pfn+0x208/0x280

Call Trace:

[c0000002125df980] [8000000000000104] 0x8000000000000104 (unreliable) [c0000002125df9c0] [c000000000378488] insert_pfn+0x68/0x280 [c0000002125dfa30] [c0000000004a5494] dax_iomap_pte_fault.isra.7+0x734/0xa40 [c0000002125dfb50] [c000000000627250] __xfs_filemap_fault+0x280/0x2d0 [c0000002125dfbb0] [c000000000373abc] do_wp_page+0x48c/0xa40 [c0000002125dfc00] [c000000000379170] __handle_mm_fault+0x8d0/0x1fd0 [c0000002125dfd00] [c00000000037a9b0] handle_mm_fault+0x140/0x250 [c0000002125dfd40] [c000000000074bb0] __do_page_fault+0x300/0xd60 [c0000002125dfe20] [c00000000000acf4] handle_page_fault+0x18

Now that is WARN_ON in set_pte_at which is

	VM_WARN_ON(pte_hw_valid(*ptep) && !pte_protnone(*ptep));
Quoted 7 lines
Quoted 7 lines
Quoted 4 lines

Multiple architecture optimize set_pte_at based on the assumption that we will never use set_pte_at to update a valid pte entry. This helps in avoid flushing tlb etc. We should be using ptep_set_access_flags for this.

Hum, I didn't know about this assumption and neither did lot of other people reviewing DAX patches. Is this documented somewhere?

Any answer here?

Quoted 10 lines
Quoted 9 lines
Quoted 2 lines

I guess iomap code doesn't handle this correctly? Or am I missing some other ways we can end up flushing tlb?

So for RW->RO transition we use ptep_clear_flush() in dax_entry_mkclean() so that one is certainly safe. Similarly for unmapping. The RO->RW transition does not seem to have any TLB flush so there TLB could still carry stale information but it's the same as with normal page faults on invalid PTEs or with protection faults for normal pages (see e.g. finish_mkwrite_fault()).

Quoted 5 lines

I am not sure i understood that. RO -> RW transition can have stale TLB entries with RO mapping in them. So architecture do flush TLB during the fault, some may not. We do have a NestMMU issue with that transition which requires us to do mark the pte invalid and flush TLB for that transition. (see commit bd5050e38aec3055ff4257ade987d808ac93b582 )

OK, I see.

Quoted 3 lines

For invalid PTEs we should have a TLB entry at all.

finish_mkwrite_fault do use wp_page_reuse() which does the right thing.

Yes, sorry. Somehow I've misread the code.

Quoted 11 lines
Quoted 7 lines

The only thing that's remaining is a situation
when we replace a PTE with zero page with a PTE pointing to a real storage (block allocation on protection fault). However in this case we do unmap_mapping_pages() in dax_insert_entry() so the PTE actually gets cleared before we install a new correct block mapping. So this case is safe as well. Am I missing something?

Do pfn_mkwrite callback need to insert the pfn details for a RO->RW fault type? Can't we skip that pfn insert and let finish_mkwrite_fault handle that pte update?

Yes, pfn_mkwrite() must fully update the PTE as the PTE update must happen under a lock that is private to DAX code. Using ptep_set_access_flags() in iomap code isn't going to be simple either. I have to think whether / how that is possible.

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#5 lore raw
Quoted 31 lines · Jan Kara
Quoted 30 lines · Aneesh Kumar K.V
Quoted 29 lines · Jan Kara

Hi Aneesh,

Quoted 25 lines · Aneesh Kumar K.V

We found this while testing dax with XFS, but i guess this is true for other file systems too. The stack trace looks as

[c00000000007610c] set_pte_at+0x3c/0x190
LR [c000000000378628] insert_pfn+0x208/0x280

Call Trace:

[c0000002125df980] [8000000000000104] 0x8000000000000104 (unreliable) [c0000002125df9c0] [c000000000378488] insert_pfn+0x68/0x280 [c0000002125dfa30] [c0000000004a5494] dax_iomap_pte_fault.isra.7+0x734/0xa40 [c0000002125dfb50] [c000000000627250] __xfs_filemap_fault+0x280/0x2d0 [c0000002125dfbb0] [c000000000373abc] do_wp_page+0x48c/0xa40 [c0000002125dfc00] [c000000000379170] __handle_mm_fault+0x8d0/0x1fd0 [c0000002125dfd00] [c00000000037a9b0] handle_mm_fault+0x140/0x250 [c0000002125dfd40] [c000000000074bb0] __do_page_fault+0x300/0xd60 [c0000002125dfe20] [c00000000000acf4] handle_page_fault+0x18

Now that is WARN_ON in set_pte_at which is

	VM_WARN_ON(pte_hw_valid(*ptep) && !pte_protnone(*ptep));

Multiple architecture optimize set_pte_at based on the assumption that we will never use set_pte_at to update a valid pte entry. This helps in avoid flushing tlb etc. We should be using ptep_set_access_flags for this.

Quoted 5 lines
Quoted 2 lines
Quoted 2 lines

Hum, I didn't know about this assumption and neither did lot of other people reviewing DAX patches. Is this documented somewhere?

Any answer here?

Not in the form of kernel documentation. But we had related fixes which got applied earlier

cee216a696b2004017a5ecb583366093d90b1568
56eecdb912b536a4fa97fb5bfe5a940a54d79be6

Which also imply we possibly want to document this properly.

We use:
For a RO -> RW: update ptep_set_access_flags
From an invalid pte to valid: set_pte_at
For read/modify/write sequence we have: ptep_prot_modify_start/ptep_prot_modify_commit

ptep_set_access_flags do carry a comment which explains it is used when relaxing the access.

-aneesh

#6 lore raw
In reply to Jan Kara
Quoted 6 lines · Jan Kara
Quoted 4 lines · Aneesh Kumar K.V
Quoted 3 lines · Jan Kara

Hi Aneesh,

On Thu 21-02-19 12:52:39, Aneesh Kumar K.V wrote:

Quoted 8 lines
Quoted 3 lines

Do pfn_mkwrite callback need to insert the pfn details for a RO->RW fault type? Can't we skip that pfn insert and let finish_mkwrite_fault handle that pte update?

Yes, pfn_mkwrite() must fully update the PTE as the PTE update must happen under a lock that is private to DAX code. Using ptep_set_access_flags() in iomap code isn't going to be simple either. I have to think whether / how that is possible.

Can we use ptep_clear_flush followed by set_pte_at()?

-aneesh

author reviewer
#7 lore raw
Quoted 12 lines · Aneesh Kumar K.V
Quoted 10 lines · Jan Kara
Quoted 8 lines · Aneesh Kumar K.V
Quoted 3 lines · Jan Kara

Hi Aneesh,

On Thu 21-02-19 12:52:39, Aneesh Kumar K.V wrote:

Do pfn_mkwrite callback need to insert the pfn details for a RO->RW fault type? Can't we skip that pfn insert and let finish_mkwrite_fault handle that pte update?

Quoted 6 lines
Quoted 4 lines

Yes, pfn_mkwrite() must fully update the PTE as the PTE update must happen under a lock that is private to DAX code. Using ptep_set_access_flags() in iomap code isn't going to be simple either. I have to think whether / how that is possible.

Can we use ptep_clear_flush followed by set_pte_at()?

So in the end the thing was simpler than I thought. Does attached patch fix the warnings for you?

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR

From ab5d1bbd74bc67982203b79a3748e3784a71b589 Mon Sep 17 00:00:00 2001
From: Jan Kara <[email protected]>
Date: Fri, 1 Mar 2019 15:16:11 +0100
Subject: [PATCH] mm: Fix modifying of page protection by insert_pfn()

Aneesh has reported that PPC triggers the following warning when excercising DAX code:

[c00000000007610c] set_pte_at+0x3c/0x190
LR [c000000000378628] insert_pfn+0x208/0x280

Call Trace:

[c0000002125df980] [8000000000000104] 0x8000000000000104 (unreliable) [c0000002125df9c0] [c000000000378488] insert_pfn+0x68/0x280 [c0000002125dfa30] [c0000000004a5494] dax_iomap_pte_fault.isra.7+0x734/0xa40 [c0000002125dfb50] [c000000000627250] __xfs_filemap_fault+0x280/0x2d0 [c0000002125dfbb0] [c000000000373abc] do_wp_page+0x48c/0xa40 [c0000002125dfc00] [c000000000379170] __handle_mm_fault+0x8d0/0x1fd0 [c0000002125dfd00] [c00000000037a9b0] handle_mm_fault+0x140/0x250 [c0000002125dfd40] [c000000000074bb0] __do_page_fault+0x300/0xd60 [c0000002125dfe20] [c00000000000acf4] handle_page_fault+0x18

Now that is WARN_ON in set_pte_at which is

        VM_WARN_ON(pte_hw_valid(*ptep) && !pte_protnone(*ptep));

The problem is that on some architectures set_pte_at() cannot cope with a situation where there is already some (different) valid entry present.

Use ptep_set_access_flags() instead to modify the pfn which is built to deal with modifying existing PTE.

---
 mm/memory.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
mm/memory.c +6 -5
index e11ca9dd823f..ebca3071278a 100644
@@ -1546,10 +1546,12 @@ static vm_fault_t insert_pfn(struct vm_area_struct *vma, unsigned long addr,
 				WARN_ON_ONCE(!is_zero_pfn(pte_pfn(*pte)));
 				goto out_unlock;
 			}
-			entry = *pte;
-			goto out_mkwrite;
-		} else
-			goto out_unlock;
+			entry = pte_mkyoung(*pte);
+			entry = maybe_mkwrite(pte_mkdirty(entry), vma);
+			if (ptep_set_access_flags(vma, addr, pte, entry, 1))
+				update_mmu_cache(vma, addr, pte);
+		}
+		goto out_unlock;
 	}
 
 	/* Ok, finally just insert the thing.. */
@@ -1558,7 +1560,6 @@ static vm_fault_t insert_pfn(struct vm_area_struct *vma, unsigned long addr,
 	else
 		entry = pte_mkspecial(pfn_t_pte(pfn, prot));
 
-out_mkwrite:
 	if (mkwrite) {
 		entry = pte_mkyoung(entry);
 		entry = maybe_mkwrite(pte_mkdirty(entry), vma);
Signature · 1 line
2.16.4
#8 lore raw
Quoted 18 lines · IST Jan Kara
Quoted 17 lines · Aneesh Kumar K.V
Quoted 14 lines · Jan Kara
Quoted 8 lines · Aneesh Kumar K.V
Quoted 3 lines · Jan Kara

Hi Aneesh,

On Thu 21-02-19 12:52:39, Aneesh Kumar K.V wrote:

Do pfn_mkwrite callback need to insert the pfn details for a RO->RW fault type? Can't we skip that pfn insert and let finish_mkwrite_fault handle that pte update?

Yes, pfn_mkwrite() must fully update the PTE as the PTE update must happen under a lock that is private to DAX code. Using ptep_set_access_flags() in iomap code isn't going to be simple either. I have to think whether / how that is possible.

Quoted 7 lines
Quoted 1 line

Can we use ptep_clear_flush followed by set_pte_at()?

So in the end the thing was simpler than I thought. Does attached patch fix the warnings for you?

Honza

Hi Jan,

Yes, the patch attached by you does indeed fix the warnings.

Signature · 1 line
chandan