@@ -739,45 +739,159 @@ EXPORT_SYMBOL(__page_cache_alloc);
739739 * at a cost of "thundering herd" phenomena during rare hash
740740 * collisions.
741741 */
742- wait_queue_head_t * page_waitqueue (struct page * page )
742+ #define PAGE_WAIT_TABLE_BITS 8
743+ #define PAGE_WAIT_TABLE_SIZE (1 << PAGE_WAIT_TABLE_BITS)
744+ static wait_queue_head_t page_wait_table [PAGE_WAIT_TABLE_SIZE ] __cacheline_aligned ;
745+
746+ static wait_queue_head_t * page_waitqueue (struct page * page )
743747{
744- return bit_waitqueue (page , 0 ) ;
748+ return & page_wait_table [ hash_ptr (page , PAGE_WAIT_TABLE_BITS )] ;
745749}
746- EXPORT_SYMBOL (page_waitqueue );
747750
748- void wait_on_page_bit ( struct page * page , int bit_nr )
751+ void __init pagecache_init ( void )
749752{
750- DEFINE_WAIT_BIT ( wait , & page -> flags , bit_nr ) ;
753+ int i ;
751754
752- if (test_bit (bit_nr , & page -> flags ))
753- __wait_on_bit (page_waitqueue (page ), & wait , bit_wait_io ,
754- TASK_UNINTERRUPTIBLE );
755+ for (i = 0 ; i < PAGE_WAIT_TABLE_SIZE ; i ++ )
756+ init_waitqueue_head (& page_wait_table [i ]);
757+
758+ page_writeback_init ();
755759}
756- EXPORT_SYMBOL (wait_on_page_bit );
757760
758- int wait_on_page_bit_killable (struct page * page , int bit_nr )
761+ struct wait_page_key {
762+ struct page * page ;
763+ int bit_nr ;
764+ int page_match ;
765+ };
766+
767+ struct wait_page_queue {
768+ struct page * page ;
769+ int bit_nr ;
770+ wait_queue_t wait ;
771+ };
772+
773+ static int wake_page_function (wait_queue_t * wait , unsigned mode , int sync , void * arg )
759774{
760- DEFINE_WAIT_BIT (wait , & page -> flags , bit_nr );
775+ struct wait_page_key * key = arg ;
776+ struct wait_page_queue * wait_page
777+ = container_of (wait , struct wait_page_queue , wait );
778+
779+ if (wait_page -> page != key -> page )
780+ return 0 ;
781+ key -> page_match = 1 ;
761782
762- if (!test_bit (bit_nr , & page -> flags ))
783+ if (wait_page -> bit_nr != key -> bit_nr )
784+ return 0 ;
785+ if (test_bit (key -> bit_nr , & key -> page -> flags ))
763786 return 0 ;
764787
765- return __wait_on_bit (page_waitqueue (page ), & wait ,
766- bit_wait_io , TASK_KILLABLE );
788+ return autoremove_wake_function (wait , mode , sync , key );
767789}
768790
769- int wait_on_page_bit_killable_timeout (struct page * page ,
770- int bit_nr , unsigned long timeout )
791+ void wake_up_page_bit (struct page * page , int bit_nr )
771792{
772- DEFINE_WAIT_BIT (wait , & page -> flags , bit_nr );
793+ wait_queue_head_t * q = page_waitqueue (page );
794+ struct wait_page_key key ;
795+ unsigned long flags ;
773796
774- wait .key .timeout = jiffies + timeout ;
775- if (!test_bit (bit_nr , & page -> flags ))
776- return 0 ;
777- return __wait_on_bit (page_waitqueue (page ), & wait ,
778- bit_wait_io_timeout , TASK_KILLABLE );
797+ key .page = page ;
798+ key .bit_nr = bit_nr ;
799+ key .page_match = 0 ;
800+
801+ spin_lock_irqsave (& q -> lock , flags );
802+ __wake_up_locked_key (q , TASK_NORMAL , & key );
803+ /*
804+ * It is possible for other pages to have collided on the waitqueue
805+ * hash, so in that case check for a page match. That prevents a long-
806+ * term waiter
807+ *
808+ * It is still possible to miss a case here, when we woke page waiters
809+ * and removed them from the waitqueue, but there are still other
810+ * page waiters.
811+ */
812+ if (!waitqueue_active (q ) || !key .page_match ) {
813+ ClearPageWaiters (page );
814+ /*
815+ * It's possible to miss clearing Waiters here, when we woke
816+ * our page waiters, but the hashed waitqueue has waiters for
817+ * other pages on it.
818+ *
819+ * That's okay, it's a rare case. The next waker will clear it.
820+ */
821+ }
822+ spin_unlock_irqrestore (& q -> lock , flags );
823+ }
824+ EXPORT_SYMBOL (wake_up_page_bit );
825+
826+ static inline int wait_on_page_bit_common (wait_queue_head_t * q ,
827+ struct page * page , int bit_nr , int state , bool lock )
828+ {
829+ struct wait_page_queue wait_page ;
830+ wait_queue_t * wait = & wait_page .wait ;
831+ int ret = 0 ;
832+
833+ init_wait (wait );
834+ wait -> func = wake_page_function ;
835+ wait_page .page = page ;
836+ wait_page .bit_nr = bit_nr ;
837+
838+ for (;;) {
839+ spin_lock_irq (& q -> lock );
840+
841+ if (likely (list_empty (& wait -> task_list ))) {
842+ if (lock )
843+ __add_wait_queue_tail_exclusive (q , wait );
844+ else
845+ __add_wait_queue (q , wait );
846+ SetPageWaiters (page );
847+ }
848+
849+ set_current_state (state );
850+
851+ spin_unlock_irq (& q -> lock );
852+
853+ if (likely (test_bit (bit_nr , & page -> flags ))) {
854+ io_schedule ();
855+ if (unlikely (signal_pending_state (state , current ))) {
856+ ret = - EINTR ;
857+ break ;
858+ }
859+ }
860+
861+ if (lock ) {
862+ if (!test_and_set_bit_lock (bit_nr , & page -> flags ))
863+ break ;
864+ } else {
865+ if (!test_bit (bit_nr , & page -> flags ))
866+ break ;
867+ }
868+ }
869+
870+ finish_wait (q , wait );
871+
872+ /*
873+ * A signal could leave PageWaiters set. Clearing it here if
874+ * !waitqueue_active would be possible (by open-coding finish_wait),
875+ * but still fail to catch it in the case of wait hash collision. We
876+ * already can fail to clear wait hash collision cases, so don't
877+ * bother with signals either.
878+ */
879+
880+ return ret ;
881+ }
882+
883+ void wait_on_page_bit (struct page * page , int bit_nr )
884+ {
885+ wait_queue_head_t * q = page_waitqueue (page );
886+ wait_on_page_bit_common (q , page , bit_nr , TASK_UNINTERRUPTIBLE , false);
887+ }
888+ EXPORT_SYMBOL (wait_on_page_bit );
889+
890+ int wait_on_page_bit_killable (struct page * page , int bit_nr )
891+ {
892+ wait_queue_head_t * q = page_waitqueue (page );
893+ return wait_on_page_bit_common (q , page , bit_nr , TASK_KILLABLE , false);
779894}
780- EXPORT_SYMBOL_GPL (wait_on_page_bit_killable_timeout );
781895
782896/**
783897 * add_page_wait_queue - Add an arbitrary waiter to a page's wait queue
@@ -793,6 +907,7 @@ void add_page_wait_queue(struct page *page, wait_queue_t *waiter)
793907
794908 spin_lock_irqsave (& q -> lock , flags );
795909 __add_wait_queue (q , waiter );
910+ SetPageWaiters (page );
796911 spin_unlock_irqrestore (& q -> lock , flags );
797912}
798913EXPORT_SYMBOL_GPL (add_page_wait_queue );
@@ -874,23 +989,19 @@ EXPORT_SYMBOL_GPL(page_endio);
874989 * __lock_page - get a lock on the page, assuming we need to sleep to get it
875990 * @page: the page to lock
876991 */
877- void __lock_page (struct page * page )
992+ void __lock_page (struct page * __page )
878993{
879- struct page * page_head = compound_head (page );
880- DEFINE_WAIT_BIT (wait , & page_head -> flags , PG_locked );
881-
882- __wait_on_bit_lock (page_waitqueue (page_head ), & wait , bit_wait_io ,
883- TASK_UNINTERRUPTIBLE );
994+ struct page * page = compound_head (__page );
995+ wait_queue_head_t * q = page_waitqueue (page );
996+ wait_on_page_bit_common (q , page , PG_locked , TASK_UNINTERRUPTIBLE , true);
884997}
885998EXPORT_SYMBOL (__lock_page );
886999
887- int __lock_page_killable (struct page * page )
1000+ int __lock_page_killable (struct page * __page )
8881001{
889- struct page * page_head = compound_head (page );
890- DEFINE_WAIT_BIT (wait , & page_head -> flags , PG_locked );
891-
892- return __wait_on_bit_lock (page_waitqueue (page_head ), & wait ,
893- bit_wait_io , TASK_KILLABLE );
1002+ struct page * page = compound_head (__page );
1003+ wait_queue_head_t * q = page_waitqueue (page );
1004+ return wait_on_page_bit_common (q , page , PG_locked , TASK_KILLABLE , true);
8941005}
8951006EXPORT_SYMBOL_GPL (__lock_page_killable );
8961007
0 commit comments