@@ -660,17 +660,22 @@ int page_mapped_in_vma(struct page *page, struct vm_area_struct *vma)
660660 return 1 ;
661661}
662662
663+ struct page_referenced_arg {
664+ int mapcount ;
665+ int referenced ;
666+ unsigned long vm_flags ;
667+ struct mem_cgroup * memcg ;
668+ };
663669/*
664- * Subfunctions of page_referenced: page_referenced_one called
665- * repeatedly from either page_referenced_anon or page_referenced_file.
670+ * arg: page_referenced_arg will be passed
666671 */
667672int page_referenced_one (struct page * page , struct vm_area_struct * vma ,
668- unsigned long address , unsigned int * mapcount ,
669- unsigned long * vm_flags )
673+ unsigned long address , void * arg )
670674{
671675 struct mm_struct * mm = vma -> vm_mm ;
672676 spinlock_t * ptl ;
673677 int referenced = 0 ;
678+ struct page_referenced_arg * pra = arg ;
674679
675680 if (unlikely (PageTransHuge (page ))) {
676681 pmd_t * pmd ;
@@ -682,13 +687,12 @@ int page_referenced_one(struct page *page, struct vm_area_struct *vma,
682687 pmd = page_check_address_pmd (page , mm , address ,
683688 PAGE_CHECK_ADDRESS_PMD_FLAG , & ptl );
684689 if (!pmd )
685- goto out ;
690+ return SWAP_AGAIN ;
686691
687692 if (vma -> vm_flags & VM_LOCKED ) {
688693 spin_unlock (ptl );
689- * mapcount = 0 ; /* break early from loop */
690- * vm_flags |= VM_LOCKED ;
691- goto out ;
694+ pra -> vm_flags |= VM_LOCKED ;
695+ return SWAP_FAIL ; /* To break the loop */
692696 }
693697
694698 /* go ahead even if the pmd is pmd_trans_splitting() */
@@ -704,13 +708,12 @@ int page_referenced_one(struct page *page, struct vm_area_struct *vma,
704708 */
705709 pte = page_check_address (page , mm , address , & ptl , 0 );
706710 if (!pte )
707- goto out ;
711+ return SWAP_AGAIN ;
708712
709713 if (vma -> vm_flags & VM_LOCKED ) {
710714 pte_unmap_unlock (pte , ptl );
711- * mapcount = 0 ; /* break early from loop */
712- * vm_flags |= VM_LOCKED ;
713- goto out ;
715+ pra -> vm_flags |= VM_LOCKED ;
716+ return SWAP_FAIL ; /* To break the loop */
714717 }
715718
716719 if (ptep_clear_flush_young_notify (vma , address , pte )) {
@@ -727,113 +730,27 @@ int page_referenced_one(struct page *page, struct vm_area_struct *vma,
727730 pte_unmap_unlock (pte , ptl );
728731 }
729732
730- (* mapcount )-- ;
731-
732- if (referenced )
733- * vm_flags |= vma -> vm_flags ;
734- out :
735- return referenced ;
736- }
737-
738- static int page_referenced_anon (struct page * page ,
739- struct mem_cgroup * memcg ,
740- unsigned long * vm_flags )
741- {
742- unsigned int mapcount ;
743- struct anon_vma * anon_vma ;
744- pgoff_t pgoff ;
745- struct anon_vma_chain * avc ;
746- int referenced = 0 ;
747-
748- anon_vma = page_lock_anon_vma_read (page );
749- if (!anon_vma )
750- return referenced ;
751-
752- mapcount = page_mapcount (page );
753- pgoff = page -> index << (PAGE_CACHE_SHIFT - PAGE_SHIFT );
754- anon_vma_interval_tree_foreach (avc , & anon_vma -> rb_root , pgoff , pgoff ) {
755- struct vm_area_struct * vma = avc -> vma ;
756- unsigned long address = vma_address (page , vma );
757- /*
758- * If we are reclaiming on behalf of a cgroup, skip
759- * counting on behalf of references from different
760- * cgroups
761- */
762- if (memcg && !mm_match_cgroup (vma -> vm_mm , memcg ))
763- continue ;
764- referenced += page_referenced_one (page , vma , address ,
765- & mapcount , vm_flags );
766- if (!mapcount )
767- break ;
733+ if (referenced ) {
734+ pra -> referenced ++ ;
735+ pra -> vm_flags |= vma -> vm_flags ;
768736 }
769737
770- page_unlock_anon_vma_read (anon_vma );
771- return referenced ;
738+ pra -> mapcount -- ;
739+ if (!pra -> mapcount )
740+ return SWAP_SUCCESS ; /* To break the loop */
741+
742+ return SWAP_AGAIN ;
772743}
773744
774- /**
775- * page_referenced_file - referenced check for object-based rmap
776- * @page: the page we're checking references on.
777- * @memcg: target memory control group
778- * @vm_flags: collect encountered vma->vm_flags who actually referenced the page
779- *
780- * For an object-based mapped page, find all the places it is mapped and
781- * check/clear the referenced flag. This is done by following the page->mapping
782- * pointer, then walking the chain of vmas it holds. It returns the number
783- * of references it found.
784- *
785- * This function is only called from page_referenced for object-based pages.
786- */
787- static int page_referenced_file (struct page * page ,
788- struct mem_cgroup * memcg ,
789- unsigned long * vm_flags )
745+ static bool invalid_page_referenced_vma (struct vm_area_struct * vma , void * arg )
790746{
791- unsigned int mapcount ;
792- struct address_space * mapping = page -> mapping ;
793- pgoff_t pgoff = page -> index << (PAGE_CACHE_SHIFT - PAGE_SHIFT );
794- struct vm_area_struct * vma ;
795- int referenced = 0 ;
796-
797- /*
798- * The caller's checks on page->mapping and !PageAnon have made
799- * sure that this is a file page: the check for page->mapping
800- * excludes the case just before it gets set on an anon page.
801- */
802- BUG_ON (PageAnon (page ));
747+ struct page_referenced_arg * pra = arg ;
748+ struct mem_cgroup * memcg = pra -> memcg ;
803749
804- /*
805- * The page lock not only makes sure that page->mapping cannot
806- * suddenly be NULLified by truncation, it makes sure that the
807- * structure at mapping cannot be freed and reused yet,
808- * so we can safely take mapping->i_mmap_mutex.
809- */
810- BUG_ON (!PageLocked (page ));
811-
812- mutex_lock (& mapping -> i_mmap_mutex );
813-
814- /*
815- * i_mmap_mutex does not stabilize mapcount at all, but mapcount
816- * is more likely to be accurate if we note it after spinning.
817- */
818- mapcount = page_mapcount (page );
819-
820- vma_interval_tree_foreach (vma , & mapping -> i_mmap , pgoff , pgoff ) {
821- unsigned long address = vma_address (page , vma );
822- /*
823- * If we are reclaiming on behalf of a cgroup, skip
824- * counting on behalf of references from different
825- * cgroups
826- */
827- if (memcg && !mm_match_cgroup (vma -> vm_mm , memcg ))
828- continue ;
829- referenced += page_referenced_one (page , vma , address ,
830- & mapcount , vm_flags );
831- if (!mapcount )
832- break ;
833- }
750+ if (!mm_match_cgroup (vma -> vm_mm , memcg ))
751+ return true;
834752
835- mutex_unlock (& mapping -> i_mmap_mutex );
836- return referenced ;
753+ return false;
837754}
838755
839756/**
@@ -851,32 +768,47 @@ int page_referenced(struct page *page,
851768 struct mem_cgroup * memcg ,
852769 unsigned long * vm_flags )
853770{
854- int referenced = 0 ;
771+ int ret ;
855772 int we_locked = 0 ;
773+ struct page_referenced_arg pra = {
774+ .mapcount = page_mapcount (page ),
775+ .memcg = memcg ,
776+ };
777+ struct rmap_walk_control rwc = {
778+ .rmap_one = page_referenced_one ,
779+ .arg = (void * )& pra ,
780+ .anon_lock = page_lock_anon_vma_read ,
781+ };
856782
857783 * vm_flags = 0 ;
858- if (page_mapped (page ) && page_rmapping (page )) {
859- if (!is_locked && (!PageAnon (page ) || PageKsm (page ))) {
860- we_locked = trylock_page (page );
861- if (!we_locked ) {
862- referenced ++ ;
863- goto out ;
864- }
865- }
866- if (unlikely (PageKsm (page )))
867- referenced += page_referenced_ksm (page , memcg ,
868- vm_flags );
869- else if (PageAnon (page ))
870- referenced += page_referenced_anon (page , memcg ,
871- vm_flags );
872- else if (page -> mapping )
873- referenced += page_referenced_file (page , memcg ,
874- vm_flags );
875- if (we_locked )
876- unlock_page (page );
784+ if (!page_mapped (page ))
785+ return 0 ;
786+
787+ if (!page_rmapping (page ))
788+ return 0 ;
789+
790+ if (!is_locked && (!PageAnon (page ) || PageKsm (page ))) {
791+ we_locked = trylock_page (page );
792+ if (!we_locked )
793+ return 1 ;
877794 }
878- out :
879- return referenced ;
795+
796+ /*
797+ * If we are reclaiming on behalf of a cgroup, skip
798+ * counting on behalf of references from different
799+ * cgroups
800+ */
801+ if (memcg ) {
802+ rwc .invalid_vma = invalid_page_referenced_vma ;
803+ }
804+
805+ ret = rmap_walk (page , & rwc );
806+ * vm_flags = pra .vm_flags ;
807+
808+ if (we_locked )
809+ unlock_page (page );
810+
811+ return pra .referenced ;
880812}
881813
882814static int page_mkclean_one (struct page * page , struct vm_area_struct * vma ,
@@ -1700,6 +1632,14 @@ static int rmap_walk_file(struct page *page, struct rmap_walk_control *rwc)
17001632 struct vm_area_struct * vma ;
17011633 int ret = SWAP_AGAIN ;
17021634
1635+ /*
1636+ * The page lock not only makes sure that page->mapping cannot
1637+ * suddenly be NULLified by truncation, it makes sure that the
1638+ * structure at mapping cannot be freed and reused yet,
1639+ * so we can safely take mapping->i_mmap_mutex.
1640+ */
1641+ VM_BUG_ON (!PageLocked (page ));
1642+
17031643 if (!mapping )
17041644 return ret ;
17051645 mutex_lock (& mapping -> i_mmap_mutex );
@@ -1731,8 +1671,6 @@ static int rmap_walk_file(struct page *page, struct rmap_walk_control *rwc)
17311671
17321672int rmap_walk (struct page * page , struct rmap_walk_control * rwc )
17331673{
1734- VM_BUG_ON (!PageLocked (page ));
1735-
17361674 if (unlikely (PageKsm (page )))
17371675 return rmap_walk_ksm (page , rwc );
17381676 else if (PageAnon (page ))
0 commit comments