LCOV - code coverage report
Current view: top level - kernel/sched - wait_bit.c (source / functions) Hit Total Coverage
Test: combined.info Lines: 61 97 62.9 %
Date: 2022-03-28 13:20:08 Functions: 11 18 61.1 %
Branches: 17 46 37.0 %

           Branch data     Line data    Source code
       1                 :            : // SPDX-License-Identifier: GPL-2.0-only
       2                 :            : /*
       3                 :            :  * The implementation of the wait_bit*() and related waiting APIs:
       4                 :            :  */
       5                 :            : #include "sched.h"
       6                 :            : 
       7                 :            : #define WAIT_TABLE_BITS 8
       8                 :            : #define WAIT_TABLE_SIZE (1 << WAIT_TABLE_BITS)
       9                 :            : 
      10                 :            : static wait_queue_head_t bit_wait_table[WAIT_TABLE_SIZE] __cacheline_aligned;
      11                 :            : 
      12                 :     255434 : wait_queue_head_t *bit_waitqueue(void *word, int bit)
      13                 :            : {
      14                 :     255434 :         const int shift = BITS_PER_LONG == 32 ? 5 : 6;
      15                 :     255434 :         unsigned long val = (unsigned long)word << shift | bit;
      16                 :            : 
      17                 :     255434 :         return bit_wait_table + hash_long(val, WAIT_TABLE_BITS);
      18                 :            : }
      19                 :            : EXPORT_SYMBOL(bit_waitqueue);
      20                 :            : 
      21                 :       3884 : int wake_bit_function(struct wait_queue_entry *wq_entry, unsigned mode, int sync, void *arg)
      22                 :            : {
      23                 :       3884 :         struct wait_bit_key *key = arg;
      24                 :       3884 :         struct wait_bit_queue_entry *wait_bit = container_of(wq_entry, struct wait_bit_queue_entry, wq_entry);
      25                 :            : 
      26         [ +  + ]:       3884 :         if (wait_bit->key.flags != key->flags ||
      27   [ +  -  -  + ]:       7716 :                         wait_bit->key.bit_nr != key->bit_nr ||
      28                 :       3858 :                         test_bit(key->bit_nr, key->flags))
      29                 :         26 :                 return 0;
      30                 :            : 
      31                 :       3858 :         return autoremove_wake_function(wq_entry, mode, sync, key);
      32                 :            : }
      33                 :            : EXPORT_SYMBOL(wake_bit_function);
      34                 :            : 
      35                 :            : /*
      36                 :            :  * To allow interruptible waiting and asynchronous (i.e. nonblocking)
      37                 :            :  * waiting, the actions of __wait_on_bit() and __wait_on_bit_lock() are
      38                 :            :  * permitted return codes. Nonzero return codes halt waiting and return.
      39                 :            :  */
      40                 :            : int __sched
      41                 :       3793 : __wait_on_bit(struct wait_queue_head *wq_head, struct wait_bit_queue_entry *wbq_entry,
      42                 :            :               wait_bit_action_f *action, unsigned mode)
      43                 :            : {
      44                 :       3793 :         int ret = 0;
      45                 :            : 
      46                 :       3793 :         do {
      47                 :       3793 :                 prepare_to_wait(wq_head, &wbq_entry->wq_entry, mode);
      48         [ +  + ]:       3793 :                 if (test_bit(wbq_entry->key.bit_nr, wbq_entry->key.flags))
      49                 :       3790 :                         ret = (*action)(&wbq_entry->key, mode);
      50   [ -  +  -  - ]:       3793 :         } while (test_bit(wbq_entry->key.bit_nr, wbq_entry->key.flags) && !ret);
      51                 :            : 
      52                 :       3793 :         finish_wait(wq_head, &wbq_entry->wq_entry);
      53                 :            : 
      54                 :       3793 :         return ret;
      55                 :            : }
      56                 :            : EXPORT_SYMBOL(__wait_on_bit);
      57                 :            : 
      58                 :       3793 : int __sched out_of_line_wait_on_bit(void *word, int bit,
      59                 :            :                                     wait_bit_action_f *action, unsigned mode)
      60                 :            : {
      61                 :       3793 :         struct wait_queue_head *wq_head = bit_waitqueue(word, bit);
      62                 :       3793 :         DEFINE_WAIT_BIT(wq_entry, word, bit);
      63                 :            : 
      64                 :       3793 :         return __wait_on_bit(wq_head, &wq_entry, action, mode);
      65                 :            : }
      66                 :            : EXPORT_SYMBOL(out_of_line_wait_on_bit);
      67                 :            : 
      68                 :          0 : int __sched out_of_line_wait_on_bit_timeout(
      69                 :            :         void *word, int bit, wait_bit_action_f *action,
      70                 :            :         unsigned mode, unsigned long timeout)
      71                 :            : {
      72                 :          0 :         struct wait_queue_head *wq_head = bit_waitqueue(word, bit);
      73                 :          0 :         DEFINE_WAIT_BIT(wq_entry, word, bit);
      74                 :            : 
      75                 :          0 :         wq_entry.key.timeout = jiffies + timeout;
      76                 :            : 
      77                 :          0 :         return __wait_on_bit(wq_head, &wq_entry, action, mode);
      78                 :            : }
      79                 :            : EXPORT_SYMBOL_GPL(out_of_line_wait_on_bit_timeout);
      80                 :            : 
      81                 :            : int __sched
      82                 :         67 : __wait_on_bit_lock(struct wait_queue_head *wq_head, struct wait_bit_queue_entry *wbq_entry,
      83                 :            :                         wait_bit_action_f *action, unsigned mode)
      84                 :            : {
      85                 :         67 :         int ret = 0;
      86                 :            : 
      87                 :         67 :         for (;;) {
      88                 :         67 :                 prepare_to_wait_exclusive(wq_head, &wbq_entry->wq_entry, mode);
      89         [ +  + ]:         67 :                 if (test_bit(wbq_entry->key.bit_nr, wbq_entry->key.flags)) {
      90                 :         66 :                         ret = action(&wbq_entry->key, mode);
      91                 :            :                         /*
      92                 :            :                          * See the comment in prepare_to_wait_event().
      93                 :            :                          * finish_wait() does not necessarily takes wwq_head->lock,
      94                 :            :                          * but test_and_set_bit() implies mb() which pairs with
      95                 :            :                          * smp_mb__after_atomic() before wake_up_page().
      96                 :            :                          */
      97         [ -  + ]:         66 :                         if (ret)
      98                 :          0 :                                 finish_wait(wq_head, &wbq_entry->wq_entry);
      99                 :            :                 }
     100         [ +  - ]:         67 :                 if (!test_and_set_bit(wbq_entry->key.bit_nr, wbq_entry->key.flags)) {
     101         [ +  - ]:         67 :                         if (!ret)
     102                 :         67 :                                 finish_wait(wq_head, &wbq_entry->wq_entry);
     103                 :         67 :                         return 0;
     104         [ #  # ]:          0 :                 } else if (ret) {
     105                 :          0 :                         return ret;
     106                 :            :                 }
     107                 :            :         }
     108                 :            : }
     109                 :            : EXPORT_SYMBOL(__wait_on_bit_lock);
     110                 :            : 
     111                 :         67 : int __sched out_of_line_wait_on_bit_lock(void *word, int bit,
     112                 :            :                                          wait_bit_action_f *action, unsigned mode)
     113                 :            : {
     114                 :         67 :         struct wait_queue_head *wq_head = bit_waitqueue(word, bit);
     115                 :         67 :         DEFINE_WAIT_BIT(wq_entry, word, bit);
     116                 :            : 
     117                 :         67 :         return __wait_on_bit_lock(wq_head, &wq_entry, action, mode);
     118                 :            : }
     119                 :            : EXPORT_SYMBOL(out_of_line_wait_on_bit_lock);
     120                 :            : 
     121                 :     197727 : void __wake_up_bit(struct wait_queue_head *wq_head, void *word, int bit)
     122                 :            : {
     123                 :     197727 :         struct wait_bit_key key = __WAIT_BIT_KEY_INITIALIZER(word, bit);
     124                 :            : 
     125         [ +  + ]:     197727 :         if (waitqueue_active(wq_head))
     126                 :       3881 :                 __wake_up(wq_head, TASK_NORMAL, 1, &key);
     127                 :     197727 : }
     128                 :            : EXPORT_SYMBOL(__wake_up_bit);
     129                 :            : 
     130                 :            : /**
     131                 :            :  * wake_up_bit - wake up a waiter on a bit
     132                 :            :  * @word: the word being waited on, a kernel virtual address
     133                 :            :  * @bit: the bit of the word being waited on
     134                 :            :  *
     135                 :            :  * There is a standard hashed waitqueue table for generic use. This
     136                 :            :  * is the part of the hashtable's accessor API that wakes up waiters
     137                 :            :  * on a bit. For instance, if one were to have waiters on a bitflag,
     138                 :            :  * one would call wake_up_bit() after clearing the bit.
     139                 :            :  *
     140                 :            :  * In order for this to function properly, as it uses waitqueue_active()
     141                 :            :  * internally, some kind of memory barrier must be done prior to calling
     142                 :            :  * this. Typically, this will be smp_mb__after_atomic(), but in some
     143                 :            :  * cases where bitflags are manipulated non-atomically under a lock, one
     144                 :            :  * may need to use a less regular barrier, such fs/inode.c's smp_mb(),
     145                 :            :  * because spin_unlock() does not guarantee a memory barrier.
     146                 :            :  */
     147                 :     197277 : void wake_up_bit(void *word, int bit)
     148                 :            : {
     149                 :     197277 :         __wake_up_bit(bit_waitqueue(word, bit), word, bit);
     150                 :     197277 : }
     151                 :            : EXPORT_SYMBOL(wake_up_bit);
     152                 :            : 
     153                 :        450 : wait_queue_head_t *__var_waitqueue(void *p)
     154                 :            : {
     155                 :        450 :         return bit_wait_table + hash_ptr(p, WAIT_TABLE_BITS);
     156                 :            : }
     157                 :            : EXPORT_SYMBOL(__var_waitqueue);
     158                 :            : 
     159                 :            : static int
     160                 :          0 : var_wake_function(struct wait_queue_entry *wq_entry, unsigned int mode,
     161                 :            :                   int sync, void *arg)
     162                 :            : {
     163                 :          0 :         struct wait_bit_key *key = arg;
     164                 :          0 :         struct wait_bit_queue_entry *wbq_entry =
     165                 :          0 :                 container_of(wq_entry, struct wait_bit_queue_entry, wq_entry);
     166                 :            : 
     167         [ #  # ]:          0 :         if (wbq_entry->key.flags != key->flags ||
     168         [ #  # ]:          0 :             wbq_entry->key.bit_nr != key->bit_nr)
     169                 :            :                 return 0;
     170                 :            : 
     171                 :          0 :         return autoremove_wake_function(wq_entry, mode, sync, key);
     172                 :            : }
     173                 :            : 
     174                 :          0 : void init_wait_var_entry(struct wait_bit_queue_entry *wbq_entry, void *var, int flags)
     175                 :            : {
     176                 :          0 :         *wbq_entry = (struct wait_bit_queue_entry){
     177                 :            :                 .key = {
     178                 :            :                         .flags  = (var),
     179                 :            :                         .bit_nr = -1,
     180                 :            :                 },
     181                 :            :                 .wq_entry = {
     182                 :            :                         .flags   = flags,
     183                 :            :                         .private = current,
     184                 :            :                         .func    = var_wake_function,
     185                 :          0 :                         .entry   = LIST_HEAD_INIT(wbq_entry->wq_entry.entry),
     186                 :            :                 },
     187                 :            :         };
     188                 :          0 : }
     189                 :            : EXPORT_SYMBOL(init_wait_var_entry);
     190                 :            : 
     191                 :        450 : void wake_up_var(void *var)
     192                 :            : {
     193                 :        450 :         __wake_up_bit(__var_waitqueue(var), var, -1);
     194                 :        450 : }
     195                 :            : EXPORT_SYMBOL(wake_up_var);
     196                 :            : 
     197                 :          0 : __sched int bit_wait(struct wait_bit_key *word, int mode)
     198                 :            : {
     199                 :          0 :         schedule();
     200         [ #  # ]:          0 :         if (signal_pending_state(mode, current))
     201                 :          0 :                 return -EINTR;
     202                 :            : 
     203                 :            :         return 0;
     204                 :            : }
     205                 :            : EXPORT_SYMBOL(bit_wait);
     206                 :            : 
     207                 :       3856 : __sched int bit_wait_io(struct wait_bit_key *word, int mode)
     208                 :            : {
     209                 :       3856 :         io_schedule();
     210         [ -  + ]:       3856 :         if (signal_pending_state(mode, current))
     211                 :          0 :                 return -EINTR;
     212                 :            : 
     213                 :            :         return 0;
     214                 :            : }
     215                 :            : EXPORT_SYMBOL(bit_wait_io);
     216                 :            : 
     217                 :          0 : __sched int bit_wait_timeout(struct wait_bit_key *word, int mode)
     218                 :            : {
     219         [ #  # ]:          0 :         unsigned long now = READ_ONCE(jiffies);
     220                 :            : 
     221         [ #  # ]:          0 :         if (time_after_eq(now, word->timeout))
     222                 :            :                 return -EAGAIN;
     223                 :          0 :         schedule_timeout(word->timeout - now);
     224         [ #  # ]:          0 :         if (signal_pending_state(mode, current))
     225                 :          0 :                 return -EINTR;
     226                 :            : 
     227                 :            :         return 0;
     228                 :            : }
     229                 :            : EXPORT_SYMBOL_GPL(bit_wait_timeout);
     230                 :            : 
     231                 :          0 : __sched int bit_wait_io_timeout(struct wait_bit_key *word, int mode)
     232                 :            : {
     233         [ #  # ]:          0 :         unsigned long now = READ_ONCE(jiffies);
     234                 :            : 
     235         [ #  # ]:          0 :         if (time_after_eq(now, word->timeout))
     236                 :            :                 return -EAGAIN;
     237                 :          0 :         io_schedule_timeout(word->timeout - now);
     238         [ #  # ]:          0 :         if (signal_pending_state(mode, current))
     239                 :          0 :                 return -EINTR;
     240                 :            : 
     241                 :            :         return 0;
     242                 :            : }
     243                 :            : EXPORT_SYMBOL_GPL(bit_wait_io_timeout);
     244                 :            : 
     245                 :         30 : void __init wait_bit_init(void)
     246                 :            : {
     247                 :         30 :         int i;
     248                 :            : 
     249         [ +  + ]:       7710 :         for (i = 0; i < WAIT_TABLE_SIZE; i++)
     250                 :       7680 :                 init_waitqueue_head(bit_wait_table + i);
     251                 :         30 : }

Generated by: LCOV version 1.14