linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/1] fs-writeback: Using spin_lock to check for work_list empty
@ 2011-08-31  5:11 Rajan Aggarwal
  2011-08-31  6:46 ` Rajan Aggarwal
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: Rajan Aggarwal @ 2011-08-31  5:11 UTC (permalink / raw)
  To: linux-fsdevel; +Cc: linux-kernel, Rajan Aggarwal

The bdi_writeback_thread function does not use spin_lock to
see if the work_list is empty.

If the list is not empty, and if an interrupt happens before we
set the current->state to TASK_RUNNING then we could be stuck in
a schedule() due to kernel preemption.

This patch acquires and releases the wb_lock to avoid this scenario.

Signed-off-by: Rajan Aggarwal <rajan.aggarwal85@gmail.com>
---
 fs/fs-writeback.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)

diff --git a/fs/fs-writeback.c b/fs/fs-writeback.c
index 04cf3b9..e333898 100644
--- a/fs/fs-writeback.c
+++ b/fs/fs-writeback.c
@@ -936,11 +936,14 @@ int bdi_writeback_thread(void *data)
 		if (pages_written)
 			wb->last_active = jiffies;
 
+		spin_lock_bh(&bdi->wb_lock);
 		set_current_state(TASK_INTERRUPTIBLE);
 		if (!list_empty(&bdi->work_list) || kthread_should_stop()) {
 			__set_current_state(TASK_RUNNING);
+			spin_unlock_bh(&bdi->wb_lock);
 			continue;
 		}
+		spin_unlock_bh(&bdi->wb_lock);
 
 		if (wb_has_dirty_io(wb) && dirty_writeback_interval)
 			schedule_timeout(msecs_to_jiffies(dirty_writeback_interval * 10));
-- 
1.7.4.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH 1/1] fs-writeback: Using spin_lock to check for work_list empty
  2011-08-31  5:11 [PATCH 1/1] fs-writeback: Using spin_lock to check for work_list empty Rajan Aggarwal
@ 2011-08-31  6:46 ` Rajan Aggarwal
  2011-08-31  6:51   ` Rajan Aggarwal
  2011-08-31 21:27 ` Andrew Morton
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 6+ messages in thread
From: Rajan Aggarwal @ 2011-08-31  6:46 UTC (permalink / raw)
  To: KAMEZAWA Hiroyuk; +Cc: linux-kernel, linux-fsdevel

Hi Kamezawa,

I noticed that you are responding to emails right now.

Can you please review the patch below and tell me whether it is
technically correct ?

Or, can you please suggest a suitable change for solving this ?

Thanks.
Kautuk

On Wed, Aug 31, 2011 at 10:41 AM, Rajan Aggarwal
<rajan.aggarwal85@gmail.com> wrote:
> The bdi_writeback_thread function does not use spin_lock to
> see if the work_list is empty.
>
> If the list is not empty, and if an interrupt happens before we
> set the current->state to TASK_RUNNING then we could be stuck in
> a schedule() due to kernel preemption.
>
> This patch acquires and releases the wb_lock to avoid this scenario.
>
> Signed-off-by: Rajan Aggarwal <rajan.aggarwal85@gmail.com>
> ---
>  fs/fs-writeback.c |    3 +++
>  1 files changed, 3 insertions(+), 0 deletions(-)
>
> diff --git a/fs/fs-writeback.c b/fs/fs-writeback.c
> index 04cf3b9..e333898 100644
> --- a/fs/fs-writeback.c
> +++ b/fs/fs-writeback.c
> @@ -936,11 +936,14 @@ int bdi_writeback_thread(void *data)
>                if (pages_written)
>                        wb->last_active = jiffies;
>
> +               spin_lock_bh(&bdi->wb_lock);
>                set_current_state(TASK_INTERRUPTIBLE);
>                if (!list_empty(&bdi->work_list) || kthread_should_stop()) {
>                        __set_current_state(TASK_RUNNING);
> +                       spin_unlock_bh(&bdi->wb_lock);
>                        continue;
>                }
> +               spin_unlock_bh(&bdi->wb_lock);
>
>                if (wb_has_dirty_io(wb) && dirty_writeback_interval)
>                        schedule_timeout(msecs_to_jiffies(dirty_writeback_interval * 10));
> --
> 1.7.4.1
>
>
--
To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 1/1] fs-writeback: Using spin_lock to check for work_list empty
  2011-08-31  6:46 ` Rajan Aggarwal
@ 2011-08-31  6:51   ` Rajan Aggarwal
  0 siblings, 0 replies; 6+ messages in thread
From: Rajan Aggarwal @ 2011-08-31  6:51 UTC (permalink / raw)
  To: Andrew Morton; +Cc: linux-kernel, linux-fsdevel

Hi Andrew,

Can you please review the patch below and tell me whether it is
technically correct ?

Or, can you please suggest a suitable change for solving this ?

Thanks.
Rajan.


> On Wed, Aug 31, 2011 at 10:41 AM, Rajan Aggarwal
> <rajan.aggarwal85@gmail.com> wrote:
>> The bdi_writeback_thread function does not use spin_lock to
>> see if the work_list is empty.
>>
>> If the list is not empty, and if an interrupt happens before we
>> set the current->state to TASK_RUNNING then we could be stuck in
>> a schedule() due to kernel preemption.
>>
>> This patch acquires and releases the wb_lock to avoid this scenario.
>>
>> Signed-off-by: Rajan Aggarwal <rajan.aggarwal85@gmail.com>
>> ---
>>  fs/fs-writeback.c |    3 +++
>>  1 files changed, 3 insertions(+), 0 deletions(-)
>>
>> diff --git a/fs/fs-writeback.c b/fs/fs-writeback.c
>> index 04cf3b9..e333898 100644
>> --- a/fs/fs-writeback.c
>> +++ b/fs/fs-writeback.c
>> @@ -936,11 +936,14 @@ int bdi_writeback_thread(void *data)
>>                if (pages_written)
>>                        wb->last_active = jiffies;
>>
>> +               spin_lock_bh(&bdi->wb_lock);
>>                set_current_state(TASK_INTERRUPTIBLE);
>>                if (!list_empty(&bdi->work_list) || kthread_should_stop()) {
>>                        __set_current_state(TASK_RUNNING);
>> +                       spin_unlock_bh(&bdi->wb_lock);
>>                        continue;
>>                }
>> +               spin_unlock_bh(&bdi->wb_lock);
>>
>>                if (wb_has_dirty_io(wb) && dirty_writeback_interval)
>>                        schedule_timeout(msecs_to_jiffies(dirty_writeback_interval * 10));
>> --
>> 1.7.4.1
>>
>>
>
--
To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 1/1] fs-writeback: Using spin_lock to check for work_list empty
  2011-08-31  5:11 [PATCH 1/1] fs-writeback: Using spin_lock to check for work_list empty Rajan Aggarwal
  2011-08-31  6:46 ` Rajan Aggarwal
@ 2011-08-31 21:27 ` Andrew Morton
  2011-09-01  7:54 ` KAMEZAWA Hiroyuki
  2011-09-01 11:56 ` Peter Zijlstra
  3 siblings, 0 replies; 6+ messages in thread
From: Andrew Morton @ 2011-08-31 21:27 UTC (permalink / raw)
  To: Rajan Aggarwal; +Cc: linux-fsdevel, linux-kernel, Wu Fengguang

On Wed, 31 Aug 2011 10:41:49 +0530
Rajan Aggarwal <rajan.aggarwal85@gmail.com> wrote:

> The bdi_writeback_thread function does not use spin_lock to
> see if the work_list is empty.
> 
> If the list is not empty, and if an interrupt happens before we
> set the current->state to TASK_RUNNING then we could be stuck in
> a schedule() due to kernel preemption.
> 
> This patch acquires and releases the wb_lock to avoid this scenario.
> 
> Signed-off-by: Rajan Aggarwal <rajan.aggarwal85@gmail.com>
> ---
>  fs/fs-writeback.c |    3 +++
>  1 files changed, 3 insertions(+), 0 deletions(-)
> 
> diff --git a/fs/fs-writeback.c b/fs/fs-writeback.c
> index 04cf3b9..e333898 100644
> --- a/fs/fs-writeback.c
> +++ b/fs/fs-writeback.c
> @@ -936,11 +936,14 @@ int bdi_writeback_thread(void *data)
>  		if (pages_written)
>  			wb->last_active = jiffies;
>  
> +		spin_lock_bh(&bdi->wb_lock);
>  		set_current_state(TASK_INTERRUPTIBLE);
>  		if (!list_empty(&bdi->work_list) || kthread_should_stop()) {
>  			__set_current_state(TASK_RUNNING);
> +			spin_unlock_bh(&bdi->wb_lock);
>  			continue;
>  		}
> +		spin_unlock_bh(&bdi->wb_lock);
>  
>  		if (wb_has_dirty_io(wb) && dirty_writeback_interval)
>  			schedule_timeout(msecs_to_jiffies(dirty_writeback_interval * 10));

I don't see anything particularly wrong with the current code.  If a
task gets preempted while in state TASK_INTERRUPTIBLE then it will
still be in that state when that task resumes running.

There might be some cross-CPU memory ordering issues in that code.  If
so, the effects would be:

a) list_empty() falsely thought to return "false": the thread will
   do one additional pointless loop and will then sleep.

b) list_empty() falsely thought to return "true": the thread will
   prematurely attempt to go to sleep, introducing a teent bit of
   additional latency in rare cases.  But I think this is a "can't
   happen" because of the memory barrier in
   set_current_state(TASK_INTERRUPTIBLE): if the task made this mistake
   running list_empty() then it will now be in state TASK_RUNNING and
   the schedule() calls will fall straight through.  I think.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 1/1] fs-writeback: Using spin_lock to check for work_list empty
  2011-08-31  5:11 [PATCH 1/1] fs-writeback: Using spin_lock to check for work_list empty Rajan Aggarwal
  2011-08-31  6:46 ` Rajan Aggarwal
  2011-08-31 21:27 ` Andrew Morton
@ 2011-09-01  7:54 ` KAMEZAWA Hiroyuki
  2011-09-01 11:56 ` Peter Zijlstra
  3 siblings, 0 replies; 6+ messages in thread
From: KAMEZAWA Hiroyuki @ 2011-09-01  7:54 UTC (permalink / raw)
  To: Rajan Aggarwal; +Cc: linux-fsdevel, linux-kernel

On Wed, 31 Aug 2011 10:41:49 +0530
Rajan Aggarwal <rajan.aggarwal85@gmail.com> wrote:

> The bdi_writeback_thread function does not use spin_lock to
> see if the work_list is empty.
> 
> If the list is not empty, and if an interrupt happens before we
> set the current->state to TASK_RUNNING then we could be stuck in
> a schedule() due to kernel preemption.
> 
> This patch acquires and releases the wb_lock to avoid this scenario.
> 
> Signed-off-by: Rajan Aggarwal <rajan.aggarwal85@gmail.com>

Hmm, even if it sleeps, bdi_wakeup_flusher() will wake up the thread.
But it seems there is an useful function schedule_timeout_interruptible().

Then, how about this ? Your concern will go away with this ?

==
>From 5558d23b72004a890dc37411aa7515996c8592fa Mon Sep 17 00:00:00 2001
From: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com>
Date: Thu, 1 Sep 2011 17:02:13 +0900
Subject: [PATCH] use schedule_timeout_interruptible() in bdi_writeback_thread

Use schedule_timeout_interruptible() rather than

 	set_current_state(TASK_INTERRUPTIBLE);
 	do some work
 	schedule_timeout()

Signed-off-by: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com>
---
 fs/fs-writeback.c |   22 +++++++++-------------
 1 files changed, 9 insertions(+), 13 deletions(-)

diff --git a/fs/fs-writeback.c b/fs/fs-writeback.c
index 04cf3b9..c538bda 100644
--- a/fs/fs-writeback.c
+++ b/fs/fs-writeback.c
@@ -936,22 +936,18 @@ int bdi_writeback_thread(void *data)
 		if (pages_written)
 			wb->last_active = jiffies;
 
-		set_current_state(TASK_INTERRUPTIBLE);
-		if (!list_empty(&bdi->work_list) || kthread_should_stop()) {
-			__set_current_state(TASK_RUNNING);
+		if (!list_empty(&bdi->work_list) || kthread_should_stop())
 			continue;
-		}
 
+		/*
+		 * If we have nothing to do, we can go sleep without any
+		 * timeout and save power. When a work is queued or
+		 * something is made dirty - we will be woken up.
+		 */
+		timeout = MAX_SCHEDULE_TIMEOUT;
 		if (wb_has_dirty_io(wb) && dirty_writeback_interval)
-			schedule_timeout(msecs_to_jiffies(dirty_writeback_interval * 10));
-		else {
-			/*
-			 * We have nothing to do, so can go sleep without any
-			 * timeout and save power. When a work is queued or
-			 * something is made dirty - we will be woken up.
-			 */
-			schedule();
-		}
+			timeout = msecs_to_jiffies(dirty_writeback_interval * 10);
+		schedule_timeout_interruptible(timeout);
 
 		try_to_freeze();
 	}
-- 
1.7.4.1

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH 1/1] fs-writeback: Using spin_lock to check for work_list empty
  2011-08-31  5:11 [PATCH 1/1] fs-writeback: Using spin_lock to check for work_list empty Rajan Aggarwal
                   ` (2 preceding siblings ...)
  2011-09-01  7:54 ` KAMEZAWA Hiroyuki
@ 2011-09-01 11:56 ` Peter Zijlstra
  3 siblings, 0 replies; 6+ messages in thread
From: Peter Zijlstra @ 2011-09-01 11:56 UTC (permalink / raw)
  To: Rajan Aggarwal; +Cc: linux-fsdevel, linux-kernel

On Wed, 2011-08-31 at 10:41 +0530, Rajan Aggarwal wrote:
> 
> If the list is not empty, and if an interrupt happens before we
> set the current->state to TASK_RUNNING then we could be stuck in
> a schedule() due to kernel preemption. 

No, look at PREEMPT_ACTIVE use in kernel/sched.c

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2011-09-01 11:56 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-08-31  5:11 [PATCH 1/1] fs-writeback: Using spin_lock to check for work_list empty Rajan Aggarwal
2011-08-31  6:46 ` Rajan Aggarwal
2011-08-31  6:51   ` Rajan Aggarwal
2011-08-31 21:27 ` Andrew Morton
2011-09-01  7:54 ` KAMEZAWA Hiroyuki
2011-09-01 11:56 ` Peter Zijlstra

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).