@@ -351,6 +351,19 @@ static void dax_disassociate_entry(void *entry, struct address_space *mapping,
351351 }
352352}
353353
354+ static struct page * dax_busy_page (void * entry )
355+ {
356+ unsigned long pfn ;
357+
358+ for_each_mapped_pfn (entry , pfn ) {
359+ struct page * page = pfn_to_page (pfn );
360+
361+ if (page_ref_count (page ) > 1 )
362+ return page ;
363+ }
364+ return NULL ;
365+ }
366+
354367/*
355368 * Find radix tree entry at given index. If it points to an exceptional entry,
356369 * return it with the radix tree entry locked. If the radix tree doesn't
@@ -492,6 +505,90 @@ static void *grab_mapping_entry(struct address_space *mapping, pgoff_t index,
492505 return entry ;
493506}
494507
508+ /**
509+ * dax_layout_busy_page - find first pinned page in @mapping
510+ * @mapping: address space to scan for a page with ref count > 1
511+ *
512+ * DAX requires ZONE_DEVICE mapped pages. These pages are never
513+ * 'onlined' to the page allocator so they are considered idle when
514+ * page->count == 1. A filesystem uses this interface to determine if
515+ * any page in the mapping is busy, i.e. for DMA, or other
516+ * get_user_pages() usages.
517+ *
518+ * It is expected that the filesystem is holding locks to block the
519+ * establishment of new mappings in this address_space. I.e. it expects
520+ * to be able to run unmap_mapping_range() and subsequently not race
521+ * mapping_mapped() becoming true.
522+ */
523+ struct page * dax_layout_busy_page (struct address_space * mapping )
524+ {
525+ pgoff_t indices [PAGEVEC_SIZE ];
526+ struct page * page = NULL ;
527+ struct pagevec pvec ;
528+ pgoff_t index , end ;
529+ unsigned i ;
530+
531+ /*
532+ * In the 'limited' case get_user_pages() for dax is disabled.
533+ */
534+ if (IS_ENABLED (CONFIG_FS_DAX_LIMITED ))
535+ return NULL ;
536+
537+ if (!dax_mapping (mapping ) || !mapping_mapped (mapping ))
538+ return NULL ;
539+
540+ pagevec_init (& pvec );
541+ index = 0 ;
542+ end = -1 ;
543+
544+ /*
545+ * If we race get_user_pages_fast() here either we'll see the
546+ * elevated page count in the pagevec_lookup and wait, or
547+ * get_user_pages_fast() will see that the page it took a reference
548+ * against is no longer mapped in the page tables and bail to the
549+ * get_user_pages() slow path. The slow path is protected by
550+ * pte_lock() and pmd_lock(). New references are not taken without
551+ * holding those locks, and unmap_mapping_range() will not zero the
552+ * pte or pmd without holding the respective lock, so we are
553+ * guaranteed to either see new references or prevent new
554+ * references from being established.
555+ */
556+ unmap_mapping_range (mapping , 0 , 0 , 1 );
557+
558+ while (index < end && pagevec_lookup_entries (& pvec , mapping , index ,
559+ min (end - index , (pgoff_t )PAGEVEC_SIZE ),
560+ indices )) {
561+ for (i = 0 ; i < pagevec_count (& pvec ); i ++ ) {
562+ struct page * pvec_ent = pvec .pages [i ];
563+ void * entry ;
564+
565+ index = indices [i ];
566+ if (index >= end )
567+ break ;
568+
569+ if (!radix_tree_exceptional_entry (pvec_ent ))
570+ continue ;
571+
572+ xa_lock_irq (& mapping -> i_pages );
573+ entry = get_unlocked_mapping_entry (mapping , index , NULL );
574+ if (entry )
575+ page = dax_busy_page (entry );
576+ put_unlocked_mapping_entry (mapping , index , entry );
577+ xa_unlock_irq (& mapping -> i_pages );
578+ if (page )
579+ break ;
580+ }
581+ pagevec_remove_exceptionals (& pvec );
582+ pagevec_release (& pvec );
583+ index ++ ;
584+
585+ if (page )
586+ break ;
587+ }
588+ return page ;
589+ }
590+ EXPORT_SYMBOL_GPL (dax_layout_busy_page );
591+
495592static int __dax_invalidate_mapping_entry (struct address_space * mapping ,
496593 pgoff_t index , bool trunc )
497594{
@@ -912,7 +1009,6 @@ static int dax_load_hole(struct address_space *mapping, void *entry,
9121009 unsigned long vaddr = vmf -> address ;
9131010 int ret = VM_FAULT_NOPAGE ;
9141011 struct page * zero_page ;
915- void * entry2 ;
9161012 pfn_t pfn ;
9171013
9181014 zero_page = ZERO_PAGE (0 );
@@ -922,13 +1018,8 @@ static int dax_load_hole(struct address_space *mapping, void *entry,
9221018 }
9231019
9241020 pfn = page_to_pfn_t (zero_page );
925- entry2 = dax_insert_mapping_entry (mapping , vmf , entry , pfn ,
926- RADIX_DAX_ZERO_PAGE , false);
927- if (IS_ERR (entry2 )) {
928- ret = VM_FAULT_SIGBUS ;
929- goto out ;
930- }
931-
1021+ dax_insert_mapping_entry (mapping , vmf , entry , pfn , RADIX_DAX_ZERO_PAGE ,
1022+ false);
9321023 vm_insert_mixed (vmf -> vma , vaddr , pfn );
9331024out :
9341025 trace_dax_load_hole (inode , vmf , ret );
@@ -1240,10 +1331,6 @@ static int dax_iomap_pte_fault(struct vm_fault *vmf, pfn_t *pfnp,
12401331
12411332 entry = dax_insert_mapping_entry (mapping , vmf , entry , pfn ,
12421333 0 , write && !sync );
1243- if (IS_ERR (entry )) {
1244- error = PTR_ERR (entry );
1245- goto error_finish_iomap ;
1246- }
12471334
12481335 /*
12491336 * If we are doing synchronous page fault and inode needs fsync,
@@ -1327,8 +1414,6 @@ static int dax_pmd_load_hole(struct vm_fault *vmf, struct iomap *iomap,
13271414 pfn = page_to_pfn_t (zero_page );
13281415 ret = dax_insert_mapping_entry (mapping , vmf , entry , pfn ,
13291416 RADIX_DAX_PMD | RADIX_DAX_ZERO_PAGE , false);
1330- if (IS_ERR (ret ))
1331- goto fallback ;
13321417
13331418 ptl = pmd_lock (vmf -> vma -> vm_mm , vmf -> pmd );
13341419 if (!pmd_none (* (vmf -> pmd ))) {
@@ -1450,8 +1535,6 @@ static int dax_iomap_pmd_fault(struct vm_fault *vmf, pfn_t *pfnp,
14501535
14511536 entry = dax_insert_mapping_entry (mapping , vmf , entry , pfn ,
14521537 RADIX_DAX_PMD , write && !sync );
1453- if (IS_ERR (entry ))
1454- goto finish_iomap ;
14551538
14561539 /*
14571540 * If we are doing synchronous page fault and inode needs fsync,
0 commit comments