Skip to contentSkip to conversation

Small cleanup of bdi_forker_thread()

PATCH In review Jan Kara 4 participants 16 messages Sep 2, 2011 – Sep 8, 2011

Subsystems (2)
lore
Share & tools
lore

Jan Kara

author
#0 lore raw

v3 cover Small cleanup of bdi_forker_thread()

Hi Jens,

so after I realized how I underestimated subtlety of bdi_forker_thread()
here are two small cleanups to it. The first one puts clearing of BDI_pending in a more natural place, the second patch adds a comment so that fools like me don't try to optimize the function in a wrong place...

Honza

author
#1 lore raw

PATCH v3 1/2 mm: Cleanup clearing of BDI_pending bit in bdi_forker_thread()

bdi_forker_thread() clears BDI_pending bit at the end of the main loop. However clearing of this bit must not be done in some cases which is handled by calling 'continue' from switch statement. That's kind of unusual construct and without a good reason so change the function into more intuitive code flow.

CC Wu Fengguang <[email protected]>
CC Andrew Morton <[email protected]>
---
 mm/backing-dev.c |   23 ++++++++++++++---------
 1 files changed, 14 insertions(+), 9 deletions(-)
mm/backing-dev.c +14 -9
index d6edf8d..94a047b 100644
@@ -359,6 +359,17 @@ static unsigned long bdi_longest_inactive(void)
 	return max(5UL * 60 * HZ, interval);
 }
 
+/*
+ * Clear pending bit and wakeup anybody waiting for flusher thread creation or
+ * shutdown
+ */
+static void bdi_clear_pending(struct backing_dev_info *bdi)
+{
+	clear_bit(BDI_pending, &bdi->state);
+	smp_mb__after_clear_bit();
+	wake_up_bit(&bdi->state, BDI_pending);
+}
+
 static int bdi_forker_thread(void *ptr)
 {
 	struct bdi_writeback *me = ptr;
@@ -469,11 +480,13 @@ static int bdi_forker_thread(void *ptr)
 				spin_unlock_bh(&bdi->wb_lock);
 				wake_up_process(task);
 			}
+			bdi_clear_pending(bdi);
 			break;
 
 		case KILL_THREAD:
 			__set_current_state(TASK_RUNNING);
 			kthread_stop(task);
+			bdi_clear_pending(bdi);
 			break;
 
 		case NO_ACTION:
@@ -489,16 +502,8 @@ static int bdi_forker_thread(void *ptr)
 			else
 				schedule_timeout(msecs_to_jiffies(dirty_writeback_interval * 10));
 			try_to_freeze();
-			/* Back to the main loop */
-			continue;
+			break;
 		}
-
-		/*
-		 * Clear pending bit and wakeup anybody waiting to tear us down.
-		 */
-		clear_bit(BDI_pending, &bdi->state);
-		smp_mb__after_clear_bit();
-		wake_up_bit(&bdi->state, BDI_pending);
 	}
 
 	return 0;
Signature · 1 line
1.7.1
Reviewed-by on v3 1/2
#2 lore raw

Re: [PATCH 1/2] mm: Cleanup clearing of BDI_pending bit in bdi_forker_thread()

Quoted 8 lines · Jan Kara

bdi_forker_thread() clears BDI_pending bit at the end of the main loop. However clearing of this bit must not be done in some cases which is handled by calling 'continue' from switch statement. That's kind of unusual construct and without a good reason so change the function into more intuitive code flow.

CC Wu Fengguang <[email protected]>
CC Andrew Morton <[email protected]>

It's pure code refactor.

Thanks,
Fengguang

#3 lore raw

Re: [PATCH 1/2] mm: Cleanup clearing of BDI_pending bit in bdi_forker_thread()

Quoted 6 lines · Wu Fengguang
Quoted 5 lines · Jan Kara

bdi_forker_thread() clears BDI_pending bit at the end of the main loop. However clearing of this bit must not be done in some cases which is handled by calling 'continue' from switch statement. That's kind of unusual construct and without a good reason so change the function into more intuitive code flow.

Quoted 7 lines
Quoted 3 lines
CC Wu Fengguang <[email protected]>
CC Andrew Morton <[email protected]>

It's pure code refactor.

But I do suspect it will slightly increase the code size. What do you think?

Thanks,
Fengguang

author
#4 lore raw

Re: [PATCH 1/2] mm: Cleanup clearing of BDI_pending bit in bdi_forker_thread()

Quoted 11 lines · Wu Fengguang
Quoted 10 lines · Wu Fengguang
Quoted 8 lines · Jan Kara

bdi_forker_thread() clears BDI_pending bit at the end of the main loop. However clearing of this bit must not be done in some cases which is handled by calling 'continue' from switch statement. That's kind of unusual construct and without a good reason so change the function into more intuitive code flow.

CC Wu Fengguang <[email protected]>
CC Andrew Morton <[email protected]>
Quoted 6 lines
Quoted 3 lines

It's pure code refactor.

But I do suspect it will slightly increase the code size. What do you think?

I haven't checked, maybe it will if the compiler is not clever enough to
merge two occurences of the function which is going to be inlined. But the overhead will be really small and the code is not really performance critical so I think clarity has priority.

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#5 lore raw

Re: [PATCH 1/2] mm: Cleanup clearing of BDI_pending bit in bdi_forker_thread()

Quoted 14 lines · Jan Kara
Quoted 13 lines · Wu Fengguang
Quoted 12 lines · Wu Fengguang
Quoted 8 lines · Jan Kara

bdi_forker_thread() clears BDI_pending bit at the end of the main loop. However clearing of this bit must not be done in some cases which is handled by calling 'continue' from switch statement. That's kind of unusual construct and without a good reason so change the function into more intuitive code flow.

CC Wu Fengguang <[email protected]>
CC Andrew Morton <[email protected]>

It's pure code refactor.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

But I do suspect it will slightly increase the code size. What do you think?

I haven't checked, maybe it will if the compiler is not clever enough to
merge two occurences of the function which is going to be inlined. But the overhead will be really small and the code is not really performance critical so I think clarity has priority.

I have no problem then. It's not a big matter.

Thanks,
Fengguang

author
#6 lore raw

PATCH v3 2/2 mm: Add comment explaining task state setting in bdi_forker_thread()

In reply to Jan Kara
CC Wu Fengguang <[email protected]>
CC Andrew Morton <[email protected]>
---
 mm/backing-dev.c |    7 +++++++
 1 files changed, 7 insertions(+), 0 deletions(-)
mm/backing-dev.c +7 -0
index 94a047b..a87da52 100644
@@ -401,6 +401,13 @@ static int bdi_forker_thread(void *ptr)
 		}
 
 		spin_lock_bh(&bdi_lock);
+		/*
+		 * In the following loop we are going to check whether we have
+		 * some work to do without any synchronization with tasks
+		 * waking us up to do work for them. So we have to set task
+		 * state already here so that we don't miss wakeups coming
+		 * after we verify some condition.
+		 */
 		set_current_state(TASK_INTERRUPTIBLE);
 
 		list_for_each_entry(bdi, &bdi_list, bdi_list) {
Signature · 1 line
1.7.1
Acked-by on v3 2/2
#7 lore raw

Re: [PATCH 2/2] mm: Add comment explaining task state setting in bdi_forker_thread()

mm/backing-dev.c static int bdi_forker_thread(void *ptr) · line 408

Quoted 9 lines
mm/backing-dev.c +5 -0
@@ -401,6 +401,13 @@ static int bdi_forker_thread(void *ptr)
 		}
 
 		spin_lock_bh(&bdi_lock);
+		/*
+		 * In the following loop we are going to check whether we have
+		 * some work to do without any synchronization with tasks
+		 * waking us up to do work for them. So we have to set task
+		 * state already here so that we don't miss wakeups coming

s/already/early/ ?

Quoted 7 lines
mm/backing-dev.c +2 -0
+		 * after we verify some condition.
+		 */
 		set_current_state(TASK_INTERRUPTIBLE);
 
 		list_for_each_entry(bdi, &bdi_list, bdi_list) {
Signature · 1 line
1.7.1
author
#8 lore raw

Re: [PATCH 2/2] mm: Add comment explaining task state setting in bdi_forker_thread()

Quoted 11 lines · Wu Fengguang

mm/backing-dev.c static int bdi_forker_thread(void *ptr) · line 408

Quoted 9 lines
mm/backing-dev.c +5 -0
@@ -401,6 +401,13 @@ static int bdi_forker_thread(void *ptr)
 		}
 
 		spin_lock_bh(&bdi_lock);
+		/*
+		 * In the following loop we are going to check whether we have
+		 * some work to do without any synchronization with tasks
+		 * waking us up to do work for them. So we have to set task
+		 * state already here so that we don't miss wakeups coming

s/already/early/ ?

Thanks for review. We'd have to substitute 'already here' with 'early'
for the sentence to make make sense. But frankly I don't see why one would be better than the other one...

Honza

Quoted 10 lines
Quoted 7 lines
mm/backing-dev.c +2 -0
+		 * after we verify some condition.
+		 */
 		set_current_state(TASK_INTERRUPTIBLE);
 
 		list_for_each_entry(bdi, &bdi_list, bdi_list) {
Signature · 1 line
1.7.1
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#9 lore raw

Re: [PATCH 2/2] mm: Add comment explaining task state setting in bdi_forker_thread()

Quoted 12 lines · Jan Kara
Quoted 11 lines · Wu Fengguang

mm/backing-dev.c static int bdi_forker_thread(void *ptr) · line 408

Quoted 9 lines
mm/backing-dev.c +5 -0
@@ -401,6 +401,13 @@ static int bdi_forker_thread(void *ptr)
 		}
 
 		spin_lock_bh(&bdi_lock);
+		/*
+		 * In the following loop we are going to check whether we have
+		 * some work to do without any synchronization with tasks
+		 * waking us up to do work for them. So we have to set task
+		 * state already here so that we don't miss wakeups coming

s/already/early/ ?

Quoted 3 lines

Thanks for review. We'd have to substitute 'already here' with 'early'
for the sentence to make make sense. But frankly I don't see why one would be better than the other one...

You are the native English speaker? OK, I have no more problems... ;)

Thanks,
Fengguang

author
#10 lore raw

Re: [PATCH 2/2] mm: Add comment explaining task state setting in bdi_forker_thread()

Quoted 13 lines · Wu Fengguang
Quoted 12 lines · Jan Kara
Quoted 11 lines · Wu Fengguang

mm/backing-dev.c static int bdi_forker_thread(void *ptr) · line 408

Quoted 9 lines
mm/backing-dev.c +5 -0
@@ -401,6 +401,13 @@ static int bdi_forker_thread(void *ptr)
 		}
 
 		spin_lock_bh(&bdi_lock);
+		/*
+		 * In the following loop we are going to check whether we have
+		 * some work to do without any synchronization with tasks
+		 * waking us up to do work for them. So we have to set task
+		 * state already here so that we don't miss wakeups coming

s/already/early/ ?

Quoted 5 lines
Quoted 3 lines

Thanks for review. We'd have to substitute 'already here' with 'early'
for the sentence to make make sense. But frankly I don't see why one would be better than the other one...

You are the native English speaker? OK, I have no more problems... ;)

No, I'm not a native English speaker. Maybe that's why I don't see that
much difference :-))

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
maintainer
#11 lore raw

Re: [PATCH 2/2] mm: Add comment explaining task state setting in bdi_forker_thread()

Quoted 14 lines · On Mon, 5 Sep 2011 17:53:47 +0200 Jan Kara <[email protected]> wrote:
Quoted 13 lines · Wu Fengguang
Quoted 12 lines · Jan Kara
Quoted 11 lines · Wu Fengguang

Review static int bdi_forker_thread(void *ptr) · line 408

Quoted 9 lines
(quoted diff) +5 -0
@@ -401,6 +401,13 @@ static int bdi_forker_thread(void *ptr)
 		}
 
 		spin_lock_bh(&bdi_lock);
+		/*
+		 * In the following loop we are going to check whether we have
+		 * some work to do without any synchronization with tasks
+		 * waking us up to do work for them. So we have to set task
+		 * state already here so that we don't miss wakeups coming

s/already/early/ ?

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

Thanks for review. We'd have to substitute 'already here' with 'early'
for the sentence to make make sense. But frankly I don't see why one would be better than the other one...

You are the native English speaker? OK, I have no more problems... ;)

No, I'm not a native English speaker. Maybe that's why I don't see that
much difference :-))

grammar can often be fixed/improved by deleting stuff ;)

mm/backing-dev.c renamed from mm/backing-dev.c~mm-add-comment-explaining-task-state-setting-in-bdi_forker_thread-fix +2 -3
@@ -404,9 +404,8 @@ static int bdi_forker_thread(void *ptr)
 		/*
 		 * In the following loop we are going to check whether we have
 		 * some work to do without any synchronization with tasks
-		 * waking us up to do work for them. So we have to set task
-		 * state already here so that we don't miss wakeups coming
-		 * after we verify some condition.
+		 * waking us up to do work for them. Set the task state here
+		 * so that we don't miss wakeups after verifying conditions.
 		 */
 		set_current_state(TASK_INTERRUPTIBLE);
 

_

#12 lore raw

Re: [PATCH 2/2] mm: Add comment explaining task state setting in bdi_forker_thread()

Quoted 5 lines

grammar can often be fixed/improved by deleting stuff ;)

mm/backing-dev.c static int bdi_forker_thread(void *ptr) · line 410

Quoted 10 lines
mm/backing-dev.c renamed from mm/backing-dev.c~mm-add-comment-explaining-task-state-setting-in-bdi_forker_thread-fix +2 -3
@@ -404,9 +404,8 @@ static int bdi_forker_thread(void *ptr)
 		/*
 		 * In the following loop we are going to check whether we have
 		 * some work to do without any synchronization with tasks
-		 * waking us up to do work for them. So we have to set task
-		 * state already here so that we don't miss wakeups coming
-		 * after we verify some condition.
+		 * waking us up to do work for them. Set the task state here
+		 * so that we don't miss wakeups after verifying conditions.
 		 */
 		set_current_state(TASK_INTERRUPTIBLE);

That's nice and readable, thanks!

Regards,
Fengguang

#13 lore raw
In reply to Jan Kara
Quoted 7 lines · Jan Kara

Hi Jens,

so after I realized how I underestimated subtlety of bdi_forker_thread()
here are two small cleanups to it. The first one puts clearing of BDI_pending in a more natural place, the second patch adds a comment so that fools like me don't try to optimize the function in a wrong place...

Thanks Jan, I've queued it up for 3.1. I've marked it as stable, looks like we need it from 2.6.36 and above.

Signature · 1 line
Jens Axboe
author
#14 lore raw
Quoted 11 lines · Jens Axboe
Quoted 7 lines · Jan Kara

Hi Jens,

so after I realized how I underestimated subtlety of bdi_forker_thread()
here are two small cleanups to it. The first one puts clearing of BDI_pending in a more natural place, the second patch adds a comment so that fools like me don't try to optimize the function in a wrong place...

Thanks Jan, I've queued it up for 3.1. I've marked it as stable, looks like we need it from 2.6.36 and above.

Thanks. But is it really a stable material? It's just a cleanup after
all...

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#15 lore raw
Quoted 10 lines · Jan Kara
Quoted 9 lines · Jens Axboe
Quoted 7 lines · Jan Kara

Hi Jens,

so after I realized how I underestimated subtlety of bdi_forker_thread()
here are two small cleanups to it. The first one puts clearing of BDI_pending in a more natural place, the second patch adds a comment so that fools like me don't try to optimize the function in a wrong place...

Quoted 4 lines
Quoted 2 lines

Thanks Jan, I've queued it up for 3.1. I've marked it as stable, looks like we need it from 2.6.36 and above.

Thanks. But is it really a stable material? It's just a cleanup after
all...

I guess it's not strictly stable material, since it's not fixing a problematic issue.

Signature · 1 line
Jens Axboe