@@ -62,7 +62,7 @@ static int do_adjust_pte(struct vm_area_struct *vma, unsigned long address,
62
62
}
63
63
64
64
static int adjust_pte (struct vm_area_struct * vma , unsigned long address ,
65
- unsigned long pfn , struct vm_fault * vmf )
65
+ unsigned long pfn , bool need_lock )
66
66
{
67
67
spinlock_t * ptl ;
68
68
pgd_t * pgd ;
@@ -99,12 +99,11 @@ static int adjust_pte(struct vm_area_struct *vma, unsigned long address,
99
99
if (!pte )
100
100
return 0 ;
101
101
102
- /*
103
- * If we are using split PTE locks, then we need to take the page
104
- * lock here. Otherwise we are using shared mm->page_table_lock
105
- * which is already locked, thus cannot take it.
106
- */
107
- if (ptl != vmf -> ptl ) {
102
+ if (need_lock ) {
103
+ /*
104
+ * Use nested version here to indicate that we are already
105
+ * holding one similar spinlock.
106
+ */
108
107
spin_lock_nested (ptl , SINGLE_DEPTH_NESTING );
109
108
if (unlikely (!pmd_same (pmdval , pmdp_get_lockless (pmd )))) {
110
109
pte_unmap_unlock (pte , ptl );
@@ -114,7 +113,7 @@ static int adjust_pte(struct vm_area_struct *vma, unsigned long address,
114
113
115
114
ret = do_adjust_pte (vma , address , pfn , pte );
116
115
117
- if (ptl != vmf -> ptl )
116
+ if (need_lock )
118
117
spin_unlock (ptl );
119
118
pte_unmap (pte );
120
119
@@ -123,9 +122,10 @@ static int adjust_pte(struct vm_area_struct *vma, unsigned long address,
123
122
124
123
static void
125
124
make_coherent (struct address_space * mapping , struct vm_area_struct * vma ,
126
- unsigned long addr , pte_t * ptep , unsigned long pfn ,
127
- struct vm_fault * vmf )
125
+ unsigned long addr , pte_t * ptep , unsigned long pfn )
128
126
{
127
+ const unsigned long pmd_start_addr = ALIGN_DOWN (addr , PMD_SIZE );
128
+ const unsigned long pmd_end_addr = pmd_start_addr + PMD_SIZE ;
129
129
struct mm_struct * mm = vma -> vm_mm ;
130
130
struct vm_area_struct * mpnt ;
131
131
unsigned long offset ;
@@ -141,6 +141,14 @@ make_coherent(struct address_space *mapping, struct vm_area_struct *vma,
141
141
*/
142
142
flush_dcache_mmap_lock (mapping );
143
143
vma_interval_tree_foreach (mpnt , & mapping -> i_mmap , pgoff , pgoff ) {
144
+ /*
145
+ * If we are using split PTE locks, then we need to take the pte
146
+ * lock. Otherwise we are using shared mm->page_table_lock which
147
+ * is already locked, thus cannot take it.
148
+ */
149
+ bool need_lock = IS_ENABLED (CONFIG_SPLIT_PTE_PTLOCKS );
150
+ unsigned long mpnt_addr ;
151
+
144
152
/*
145
153
* If this VMA is not in our MM, we can ignore it.
146
154
* Note that we intentionally mask out the VMA
@@ -151,7 +159,12 @@ make_coherent(struct address_space *mapping, struct vm_area_struct *vma,
151
159
if (!(mpnt -> vm_flags & VM_MAYSHARE ))
152
160
continue ;
153
161
offset = (pgoff - mpnt -> vm_pgoff ) << PAGE_SHIFT ;
154
- aliases += adjust_pte (mpnt , mpnt -> vm_start + offset , pfn , vmf );
162
+ mpnt_addr = mpnt -> vm_start + offset ;
163
+
164
+ /* Avoid deadlocks by not grabbing the same PTE lock again. */
165
+ if (mpnt_addr >= pmd_start_addr && mpnt_addr < pmd_end_addr )
166
+ need_lock = false;
167
+ aliases += adjust_pte (mpnt , mpnt_addr , pfn , need_lock );
155
168
}
156
169
flush_dcache_mmap_unlock (mapping );
157
170
if (aliases )
@@ -194,7 +207,7 @@ void update_mmu_cache_range(struct vm_fault *vmf, struct vm_area_struct *vma,
194
207
__flush_dcache_folio (mapping , folio );
195
208
if (mapping ) {
196
209
if (cache_is_vivt ())
197
- make_coherent (mapping , vma , addr , ptep , pfn , vmf );
210
+ make_coherent (mapping , vma , addr , ptep , pfn );
198
211
else if (vma -> vm_flags & VM_EXEC )
199
212
__flush_icache_all ();
200
213
}
0 commit comments