There's nothing mysterious about it: Wait_on_page_bit

Source: Internet
Author: User

Wait_on_page_bit functions are often encapsulated in file systems, such as the following code in F2FS:

1431 void F2fs_wait_on_page_writeback (struct page *page,//wait for page writeback.
1432 Enum Page_type type)
1433 {
1434 if (Pagewriteback (page)) {
1435 struct F2fs_sb_info *sbi = F2FS_P_SB (page);
1436
1437 if (Is_merged_page (SBI, page, type))
1438 F2fs_submit_merged_bio (SBI, type, WRITE);
1439 Wait_on_page_writeback (page);
1440}
1441}

Wait_on_page_writeback is the IO rescheduling that will occur, following the Wait_on_page_writeback code:

520 static inline void wait_on_page_writeback (struct page *page)
521 {
522 if (Pagewriteback (page))//If a page is set up with the Pagewriteback flag, then wait and see when this position is finished
523 wait_on_page_bit (page, pg_writeback);
524}

The main function is wait_on_page_bit:

520 static inline void wait_on_page_writeback (struct page *page)
521 {
522 if (pagewriteback (page))///If a page is set to P Agewriteback sign, then wait, see when this position is finished
523 wait_on_page_bit (page, pg_writeback);
524}

 705  void  wait_on_page_bit (struct  page *page,  BIT_NR)  Span style= "color: #800080;" >706   { 707  define_wait_bit (WAIT, &page->flags, BIT_NR);  708   If  (Test_bit (BIT_NR, &page->flags))  710  __wait_on_bit (Page_waitqueue (page), & Wait, Bit_wait_io,  task_uninterruptible);  712 } 

Wait_on_page_bit is a very important function, each time you look at the OS source code, see Wait_on_page_bit must stop, just know what it is generally dry a thing,

But now to understand the file system in depth, this one can not, how to go deep? Further, first analyze the first sentence:

943 #defineDefine_wait_bit (name, Word, BIT)944 structWait_bit_queue name = { 945. Key =__wait_bit_key_initializer (Word, BIT),946. wait = { 947.Private=Current ,948. Func =Wake_bit_function,949. task_list = 950List_head_init ((name). wait.task_list),951},952}

Define_wait_bit is a macro that defines the structure body Wait_bit_queue:

   the struct wait_bit_queue {         wait_bit_key key;    $      wait_queue_t        wait;   Panax Notoginseng  };

Wait_bit_key is a struct that encapsulates the information about bit: including the bitmap of this array, and the specific wait for the first bit, and the waiting expiration time.

   - struct Wait_bit_key {       void            *flags;    in     int          bit_nr;    - #define WAIT_ATOMIC_T_BIT_NR    -1 to       long       timeout;    +  };

Then there is the wait_queue_t structure, which is are cheap in the kernel code:

   - struct __wait_queue {       int        flags;    a     void            *private;    at      wait_queue_func_t   func;//Wake up  process on task_list       list_head    task_list;    ;

Well, here Wait_bit_queue's structure is quite obvious, he placed two elements in it: 1) An element is responsible for "bit", 2) an element is responsible for the queue, and includes the parameters of the queue;

Then is the __wait_on_bit function, this function first to determine whether the bitmap is set our concern of the bit, if set up to wait, the implementation of the waiting code is the famous __wait_on_bit:

381 /*382 * To allow interruptible waiting and asynchronous (i.e. nonblocking) 383 * Waiting, the actions of __wait_on_bit ( ) and __wait_on_bit_lock () are384 * permitted return codes. Nonzero return codes halt waiting and return.385*/386 int__sched387__wait_on_bit (wait_queue_head_t *wq,structWait_bit_queue *Q,388Wait_bit_action_f *action, unsigned mode)389 {390     intRET =0;391 392      Do {393Prepare_to_wait (Wq, &q->wait, mode);394         if(Test_bit (Q->KEY.BIT_NR, q->key.flags))395ret = (*action) (&q->key);396} while(Test_bit (Q->KEY.BIT_NR, q->key.flags) &&!ret);397Finish_wait (Wq, &q->wait);398     returnret;399 } -Export_symbol (__wait_on_bit);401 

The main idea of the algorithm is still to detect a bit bit, then if the bit is still not cleared, then very embarrassed, you are going to invoke action, here we register the action is the function Bit_wait_io:

The Bit_wait_ioh function makes an IO dispatch, and our current process is bye-bye directly.

593__schedintBit_wait_io (structWait_bit_key *word)594 {595     if(Signal_pending_state (current->State , current))596         return 1;597io_schedule ();598     return 0;599 } -Export_symbol (bit_wait_io); -

This time, you have to doubt. The process is dispatched, so we agreed to put the current process into the waiting queue? Where did you put it? function __wait_on_bit inside the prepare_to_wait function:

159 /*note:we Use "set_current_state ()" _after_ the Wait-queue add,161 * because we need a memory barrier there on SMP, so this any162 * wake-function that tests for the wait-queue being active163 * would be guaranteed to see Waitqueue Addition _OR_ subsequent164 * Tests in this thread would see the wakeup have taken place.165 *166 * The Spin_unlock () itself is semi-permeable and only protects167 * one-to-one (it only protects stuff inside the critical region and168 * Stop s them from bleeding out-it would still allow subsequent169 * loads-move into the critical region).*/171 void172Prepare_to_wait (wait_queue_head_t *q, wait_queue_t *wait,intState )173 {174UnsignedLongflags;175 176Wait->flags &= ~wq_flag_exclusive;177Spin_lock_irqsave (&q->Lock, flags);178     if(List_empty (&wait->task_list))179__add_wait_queue (q, wait); theset_current_state (state);181Spin_unlock_irqrestore (&q->Lock, flags);182 }183Export_symbol (prepare_to_wait);

See, __add_wait_queue will add the wait queue header in the wait struct to the waiting queue associated with the page, and if this is blocked, then wait for the wake up silently. If the fluke is not set, then the Finish_wait function pulls you out of the waiting queue associated with the page.

The above analysis is a bit arbitrary, where is our starting point? Copy it again, the function f2fs_wait_on_page_writeback

1431 voidF2fs_wait_on_page_writeback (structPage *page,//wait for the page to write back.1432 enumpage_type type)1433 {1434     if(Pagewriteback (page)) {1435 structF2fs_sb_info *SBI =F2FS_P_SB (page);1436 1437       if(Is_merged_page (SBI, page, type))1438F2fs_submit_merged_bio (SBI, type, WRITE);1439wait_on_page_writeback (page);1440}1441}

This is understood, if the pagewriteback set, then when the front-line will call the Wait_on_page_writeback function, and then into a while loop to judge, if it is still placed, then you are still in the queue to sleep, or enter the next competitive process .

-----------------------------------

There is a question: in the above F2fs_wait_on_page_writeback function, if two processes at this time through the F2fs_wait_on_page_writeback function, the front and back feet, a command, then will not be able to smoothly through F2FS_ Wait_on_page_writeback's Barrier:

So what's going to happen behind F2fs_wait_on_page_writeback? Just to wait for this page to write back, to do the following things, what is the general situation below?

Before you write to this page, Wait_on_write is performed.

There's nothing mysterious about it: Wait_on_page_bit

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.