@@ -429,26 +429,101 @@ static void inode_lru_list_add(struct inode *inode)
429429 inode -> i_state |= I_REFERENCED ;
430430}
431431
432+ static void inode_lru_list_del (struct inode * inode )
433+ {
434+ if (list_lru_del (& inode -> i_sb -> s_inode_lru , & inode -> i_lru ))
435+ this_cpu_dec (nr_unused );
436+ }
437+
432438/*
433439 * Add inode to LRU if needed (inode is unused and clean).
434440 *
435441 * Needs inode->i_lock held.
436442 */
437- void inode_add_lru (struct inode * inode )
443+ bool inode_add_lru (struct inode * inode )
438444{
439- if (!(inode -> i_state & (I_DIRTY_ALL | I_SYNC |
440- I_FREEING | I_WILL_FREE )) &&
441- !atomic_read (& inode -> i_count ) && inode -> i_sb -> s_flags & SB_ACTIVE )
442- inode_lru_list_add (inode );
445+ if (inode -> i_state &
446+ (I_DIRTY_ALL | I_SYNC | I_FREEING | I_WILL_FREE | I_PAGES ))
447+ return false;
448+ if (atomic_read (& inode -> i_count ))
449+ return false;
450+ if (!(inode -> i_sb -> s_flags & SB_ACTIVE ))
451+ return false;
452+ inode_lru_list_add (inode );
453+ return true;
443454}
444455
456+ /*
457+ * Usually, inodes become reclaimable when they are no longer
458+ * referenced and their page cache has been reclaimed. The following
459+ * API allows the VM to communicate cache population state to the VFS.
460+ *
461+ * However, on CONFIG_HIGHMEM we can't wait for the page cache to go
462+ * away: cache pages allocated in a large highmem zone could pin
463+ * struct inode memory allocated in relatively small lowmem zones. So
464+ * when CONFIG_HIGHMEM is enabled, we tie cache to the inode lifetime.
465+ */
445466
446- static void inode_lru_list_del (struct inode * inode )
467+ #ifndef CONFIG_HIGHMEM
468+ /**
469+ * inode_pages_set - mark the inode as holding page cache
470+ * @inode: the inode whose first cache page was just added
471+ *
472+ * Tell the VFS that this inode has populated page cache and must not
473+ * be reclaimed by the inode shrinker.
474+ *
475+ * The caller must hold the page lock of the just-added page: by
476+ * pinning the page, the page cache cannot become depopulated, and we
477+ * can safely set I_PAGES without a race check under the i_pages lock.
478+ *
479+ * This function acquires the i_lock.
480+ */
481+ void inode_pages_set (struct inode * inode )
447482{
483+ spin_lock (& inode -> i_lock );
484+ if (!(inode -> i_state & I_PAGES )) {
485+ inode -> i_state |= I_PAGES ;
486+ if (!list_empty (& inode -> i_lru )) {
487+ count_vm_event (PGINODERESCUE );
488+ inode_lru_list_del (inode );
489+ }
490+ }
491+ spin_unlock (& inode -> i_lock );
492+ }
448493
449- if (list_lru_del (& inode -> i_sb -> s_inode_lru , & inode -> i_lru ))
450- this_cpu_dec (nr_unused );
494+ /**
495+ * inode_pages_clear - mark the inode as not holding page cache
496+ * @inode: the inode whose last cache page was just removed
497+ *
498+ * Tell the VFS that the inode no longer holds page cache and that its
499+ * lifetime is to be handed over to the inode shrinker LRU.
500+ *
501+ * This function acquires the i_lock and the i_pages lock.
502+ */
503+ void inode_pages_clear (struct inode * inode )
504+ {
505+ struct address_space * mapping = & inode -> i_data ;
506+ bool add_to_lru = false;
507+ unsigned long flags ;
508+
509+ spin_lock (& inode -> i_lock );
510+
511+ xa_lock_irqsave (& mapping -> i_pages , flags );
512+ if ((inode -> i_state & I_PAGES ) && mapping_empty (mapping )) {
513+ inode -> i_state &= ~I_PAGES ;
514+ add_to_lru = true;
515+ }
516+ xa_unlock_irqrestore (& mapping -> i_pages , flags );
517+
518+ if (add_to_lru ) {
519+ WARN_ON_ONCE (!list_empty (& inode -> i_lru ));
520+ if (inode_add_lru (inode ))
521+ __count_vm_event (PGINODEDELAYED );
522+ }
523+
524+ spin_unlock (& inode -> i_lock );
451525}
526+ #endif /* CONFIG_HIGHMEM */
452527
453528/**
454529 * inode_sb_list_add - add inode to the superblock list of inodes
@@ -741,6 +816,8 @@ static enum lru_status inode_lru_isolate(struct list_head *item,
741816 if (!spin_trylock (& inode -> i_lock ))
742817 return LRU_SKIP ;
743818
819+ WARN_ON_ONCE (inode -> i_state & I_PAGES );
820+
744821 /*
745822 * Referenced or dirty inodes are still in use. Give them another pass
746823 * through the LRU as we canot reclaim them now.
@@ -760,7 +837,18 @@ static enum lru_status inode_lru_isolate(struct list_head *item,
760837 return LRU_ROTATE ;
761838 }
762839
763- if (inode_has_buffers (inode ) || inode -> i_data .nrpages ) {
840+ /*
841+ * Usually, populated inodes shouldn't be on the shrinker LRU,
842+ * but they can be briefly visible when a new page is added to
843+ * an inode that was already linked but inode_pages_set()
844+ * hasn't run yet to move them off.
845+ *
846+ * The other exception is on HIGHMEM systems: highmem cache
847+ * can pin lowmem struct inodes, and we might be in dire
848+ * straits in the lower zones. Purge cache to free the inode.
849+ */
850+ if (inode_has_buffers (inode ) || !mapping_empty (& inode -> i_data )) {
851+ #ifdef CONFIG_HIGHMEM
764852 __iget (inode );
765853 spin_unlock (& inode -> i_lock );
766854 spin_unlock (lru_lock );
@@ -777,6 +865,12 @@ static enum lru_status inode_lru_isolate(struct list_head *item,
777865 iput (inode );
778866 spin_lock (lru_lock );
779867 return LRU_RETRY ;
868+ #else
869+ list_lru_isolate (lru , & inode -> i_lru );
870+ spin_unlock (& inode -> i_lock );
871+ this_cpu_dec (nr_unused );
872+ return LRU_REMOVED ;
873+ #endif
780874 }
781875
782876 WARN_ON (inode -> i_state & I_NEW );
0 commit comments