@@ -305,15 +305,30 @@ pte_t *__pte_offset_map(pmd_t *pmd, unsigned long addr, pmd_t *pmdvalp)
305305 return NULL ;
306306}
307307
308- pte_t * pte_offset_map_nolock (struct mm_struct * mm , pmd_t * pmd ,
309- unsigned long addr , spinlock_t * * ptlp )
308+ pte_t * pte_offset_map_readonly_nolock (struct mm_struct * mm , pmd_t * pmd ,
309+ unsigned long addr , spinlock_t * * ptlp )
310+ {
311+ pmd_t pmdval ;
312+ pte_t * pte ;
313+
314+ pte = __pte_offset_map (pmd , addr , & pmdval );
315+ if (likely (pte ))
316+ * ptlp = pte_lockptr (mm , & pmdval );
317+ return pte ;
318+ }
319+
320+ pte_t * pte_offset_map_maywrite_nolock (struct mm_struct * mm , pmd_t * pmd ,
321+ unsigned long addr , pmd_t * pmdvalp ,
322+ spinlock_t * * ptlp )
310323{
311324 pmd_t pmdval ;
312325 pte_t * pte ;
313326
314327 pte = __pte_offset_map (pmd , addr , & pmdval );
315328 if (likely (pte ))
316329 * ptlp = pte_lockptr (mm , & pmdval );
330+ if (pmdvalp )
331+ * pmdvalp = pmdval ;
317332 return pte ;
318333}
319334
@@ -347,14 +362,21 @@ pte_t *pte_offset_map_nolock(struct mm_struct *mm, pmd_t *pmd,
347362 * and disconnected table. Until pte_unmap(pte) unmaps and rcu_read_unlock()s
348363 * afterwards.
349364 *
350- * pte_offset_map_nolock(mm, pmd, addr, ptlp), above, is like pte_offset_map();
351- * but when successful, it also outputs a pointer to the spinlock in ptlp - as
352- * pte_offset_map_lock() does, but in this case without locking it. This helps
353- * the caller to avoid a later pte_lockptr(mm, *pmd), which might by that time
354- * act on a changed *pmd: pte_offset_map_nolock() provides the correct spinlock
355- * pointer for the page table that it returns. In principle, the caller should
356- * recheck *pmd once the lock is taken; in practice, no callsite needs that -
357- * either the mmap_lock for write, or pte_same() check on contents, is enough.
365+ * pte_offset_map_readonly_nolock(mm, pmd, addr, ptlp), above, is like
366+ * pte_offset_map(); but when successful, it also outputs a pointer to the
367+ * spinlock in ptlp - as pte_offset_map_lock() does, but in this case without
368+ * locking it. This helps the caller to avoid a later pte_lockptr(mm, *pmd),
369+ * which might by that time act on a changed *pmd: pte_offset_map_readonly_nolock()
370+ * provides the correct spinlock pointer for the page table that it returns.
371+ * For readonly case, the caller does not need to recheck *pmd after the lock is
372+ * taken, because the RCU lock will ensure that the PTE page will not be freed.
373+ *
374+ * pte_offset_map_maywrite_nolock(mm, pmd, addr, pmdvalp, ptlp), above, is like
375+ * pte_offset_map_readonly_nolock(); but when successful, it also outputs the
376+ * pdmval. For cases where pte or pmd entries may be modified, that is, maywrite
377+ * case, this can help the caller recheck *pmd once the lock is taken. In some
378+ * cases we can pass NULL to pmdvalp: either the mmap_lock for write, or
379+ * pte_same() check on contents, is also enough.
358380 *
359381 * Note that free_pgtables(), used after unmapping detached vmas, or when
360382 * exiting the whole mm, does not take page table lock before freeing a page
0 commit comments