Skip to contentSkip to conversation

ipvs: Remove unused variable ret from sync_thread_master()

PATCH In review Geert Uytterhoeven 5 participants 18 messages Nov 12, 2013 – Nov 13, 2013

Subsystems (10)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ipvs: Remove unused variable ret from sync_thread_master()

net/netfilter/ipvs/ip_vs_sync.c: In function 'sync_thread_master':
net/netfilter/ipvs/ip_vs_sync.c:1640:8: warning: unused variable 'ret' [-Wunused-variable]

Introduced by commit 35a2af94c7ce7130ca292c68b1d27fcfdb648f6b ("sched/wait:
Make the __wait_event*() interface more friendly")

---
 net/netfilter/ipvs/ip_vs_sync.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/netfilter/ipvs/ip_vs_sync.c +1 -1
index f63c2388f38d..d258125c4202 100644
@@ -1637,7 +1637,7 @@ static int sync_thread_master(void *data)
 			continue;
 		}
 		while (ip_vs_send_sync_msg(tinfo->sock, sb->mesg) < 0) {
-			int ret = __wait_event_interruptible(*sk_sleep(sk),
+			__wait_event_interruptible(*sk_sleep(sk),
 						   sock_writeable(sk) ||
 						   kthread_should_stop());
 			if (unlikely(kthread_should_stop()))
Signature · 1 line
1.7.9.5
maintainer
#1 lore raw
Quoted 16 lines · Geert Uytterhoeven

net/netfilter/ipvs/ip_vs_sync.c: In function 'sync_thread_master':
net/netfilter/ipvs/ip_vs_sync.c:1640:8: warning: unused variable 'ret' [-Wunused-variable]

Introduced by commit 35a2af94c7ce7130ca292c68b1d27fcfdb648f6b ("sched/wait:
Make the __wait_event*() interface more friendly")

---
 net/netfilter/ipvs/ip_vs_sync.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/netfilter/ipvs/ip_vs_sync.c +0 -0
index f63c2388f38d..d258125c4202 100644

net/netfilter/ipvs/ip_vs_sync.c static int sync_thread_master(void *data) · line 1640

Quoted 4 lines
net/netfilter/ipvs/ip_vs_sync.c +0 -1
@@ -1637,7 +1637,7 @@ static int sync_thread_master(void *data)
 			continue;
 		}
 		while (ip_vs_send_sync_msg(tinfo->sock, sb->mesg) < 0) {
-			int ret = __wait_event_interruptible(*sk_sleep(sk),

So ideally there's be a comment here why we're using interruptible but then ignore interruptions.

Julian said (
http://lkml.kernel.org/r/[email protected] ):

" Yes, your patch looks ok to me. In the past we used ssleep() but IPVS users were confused why IPVS threads increase the load average. So, we switched to _interruptible calls and later the socket polling was added. "

So maybe add something like

			/*
			 * (Ab)use interruptible sleep to avoid increasing
			 * the load avg.
			 */
Quoted 7 lines
net/netfilter/ipvs/ip_vs_sync.c +1 -0
+			__wait_event_interruptible(*sk_sleep(sk),
 						   sock_writeable(sk) ||
 						   kthread_should_stop());
 			if (unlikely(kthread_should_stop()))
Signature · 1 line
1.7.9.5
#2 lore raw
Quoted 9 lines

net/netfilter/ipvs/ip_vs_sync.c static int sync_thread_master(void *data) · line 1640

Quoted 5 lines
net/netfilter/ipvs/ip_vs_sync.c +0 -1
@@ -1637,7 +1637,7 @@ static int sync_thread_master(void *data)
 			continue;
 		}
 		while (ip_vs_send_sync_msg(tinfo->sock, sb->mesg) < 0) {
-			int ret = __wait_event_interruptible(*sk_sleep(sk),

So ideally there's be a comment here why we're using interruptible but then ignore interruptions.

Quoted 8 lines

Julian said (
http://lkml.kernel.org/r/[email protected] ):

" Yes, your patch looks ok to me. In the past we used ssleep() but IPVS users were confused why IPVS threads increase the load average. So, we switched to _interruptible calls and later the socket polling was added. "

I've done this in the past so that the code sleeps interruptibly unless there is a signal pending - which would cause it to return early.

    /* Tell scheduler we are going to sleep... */
    if (signal_pending(current))
        /* We don't want waking immediately (again) */
        sleep_state = TASK_UNINTERRUPTIBLE;
    else
        sleep_state = TASK_INTERRUPTIBLE;
    set_current_state(sleep_state);

Shame there isn't a process flag to indicate that the process will sleep uninterruptibly and that it doesn't matter. So don't count to the load average and don't emit a warning if it has been sleeping for a long time.

David

maintainer
#3 lore raw
Quoted 22 lines · David Laight
Quoted 9 lines

net/netfilter/ipvs/ip_vs_sync.c static int sync_thread_master(void *data) · line 1640

Quoted 5 lines
net/netfilter/ipvs/ip_vs_sync.c +0 -1
@@ -1637,7 +1637,7 @@ static int sync_thread_master(void *data)
 			continue;
 		}
 		while (ip_vs_send_sync_msg(tinfo->sock, sb->mesg) < 0) {
-			int ret = __wait_event_interruptible(*sk_sleep(sk),

So ideally there's be a comment here why we're using interruptible but then ignore interruptions.

Quoted 8 lines

Julian said (
http://lkml.kernel.org/r/[email protected] ):

" Yes, your patch looks ok to me. In the past we used ssleep() but IPVS users were confused why IPVS threads increase the load average. So, we switched to _interruptible calls and later the socket polling was added. "

I've done this in the past so that the code sleeps interruptibly unless there is a signal pending - which would cause it to return early.

Quoted 7 lines
    /* Tell scheduler we are going to sleep... */
    if (signal_pending(current))
        /* We don't want waking immediately (again) */
        sleep_state = TASK_UNINTERRUPTIBLE;
    else
        sleep_state = TASK_INTERRUPTIBLE;
    set_current_state(sleep_state);

If this is for kernel threads, I think you can wipe the pending state; not entirely sure how signals interact with kthreads; Oleg will know.

#4 lore raw
Quoted 12 lines
Quoted 11 lines

I've done this in the past so that the code sleeps interruptibly unless there is a signal pending - which would cause it to return early.

    /* Tell scheduler we are going to sleep... */
    if (signal_pending(current))
        /* We don't want waking immediately (again) */
        sleep_state = TASK_UNINTERRUPTIBLE;
    else
        sleep_state = TASK_INTERRUPTIBLE;
    set_current_state(sleep_state);
Quoted 2 lines

If this is for kernel threads, I think you can wipe the pending state; not entirely sure how signals interact with kthreads; Oleg will know.

I did take me a while to manage to use kthreads, and we probably still have customers who insist on using pre 2.6.18 kernels!
(In which case the processes are children of a daemon that only return to userspace in order to exit.)

We also need to send signals (to get the process out of blocking socket calls), so I have to use force_sig() to get the signal noticed.

The other issue was tidyup - I had to find module_put_and_exit().

David

#5 lore raw
In reply to Peter Zijlstra
Quoted 11 lines · Peter Zijlstra
Quoted 8 lines · David Laight
    /* Tell scheduler we are going to sleep... */
    if (signal_pending(current))
        /* We don't want waking immediately (again) */
        sleep_state = TASK_UNINTERRUPTIBLE;
    else
        sleep_state = TASK_INTERRUPTIBLE;
    set_current_state(sleep_state);
Quoted 1 line

If this is for kernel threads, I think you can wipe the pending state;

Yes, unless this kthread does allow_signal() signal_pending() can't be true.

Oleg.

maintainer
#6 lore raw
In reply to David Laight
Quoted 4 lines · David Laight

Shame there isn't a process flag to indicate that the process will sleep uninterruptibly and that it doesn't matter. So don't count to the load average and don't emit a warning if it has been sleeping for a long time.

A process flag wouldn't work, because the task could block waiting for actual work to complete in other sleeps.

However, we could do something like the below; which would allow us writing things like:

	(void)___wait_event(*sk_sleep(sk),
			    sock_writeable(sk) || kthread_should_stop(),
			    TASK_UNINTERRUPTIBLE | TASK_IDLE, 0, 0,
			    schedule());

Marking the one wait-for-more-work as TASK_IDLE such that it doesn't contribute to the load avg.

---
 fs/proc/array.c              | 23 ++++++++++++-----------
 include/linux/sched.h        |  8 +++++---
 include/trace/events/sched.h |  3 ++-
 3 files changed, 19 insertions(+), 15 deletions(-)
fs/proc/array.c +12 -11
index 1bd2077187fd..da9a9c8b5bba 100644
@@ -133,17 +133,18 @@ static inline void task_name(struct seq_file *m, struct task_struct *p)
  * simple bit tests.
  */
 static const char * const task_state_array[] = {
-	"R (running)",		/*   0 */
-	"S (sleeping)",		/*   1 */
-	"D (disk sleep)",	/*   2 */
-	"T (stopped)",		/*   4 */
-	"t (tracing stop)",	/*   8 */
-	"Z (zombie)",		/*  16 */
-	"X (dead)",		/*  32 */
-	"x (dead)",		/*  64 */
-	"K (wakekill)",		/* 128 */
-	"W (waking)",		/* 256 */
-	"P (parked)",		/* 512 */
+	"R (running)",		/*    0 */
+	"S (sleeping)",		/*    1 */
+	"D (disk sleep)",	/*    2 */
+	"T (stopped)",		/*    4 */
+	"t (tracing stop)",	/*    8 */
+	"Z (zombie)",		/*   16 */
+	"X (dead)",		/*   32 */
+	"x (dead)",		/*   64 */
+	"K (wakekill)",		/*  128 */
+	"W (waking)",		/*  256 */
+	"P (parked)",		/*  512 */
+	"I (idle)",		/* 1024 */
 };
 
 static inline const char *get_task_state(struct task_struct *tsk)
include/linux/sched.h +5 -3
index 045b0d227846..a4a9e80688d8 100644
@@ -145,9 +145,10 @@ print_cfs_rq(struct seq_file *m, int cpu, struct cfs_rq *cfs_rq);
 #define TASK_WAKEKILL		128
 #define TASK_WAKING		256
 #define TASK_PARKED		512
-#define TASK_STATE_MAX		1024
+#define TASK_IDLE		1024
+#define TASK_STATE_MAX		2048
 
-#define TASK_STATE_TO_CHAR_STR "RSDTtZXxKWP"
+#define TASK_STATE_TO_CHAR_STR "RSDTtZXxKWPI"
 
 extern char ___assert_task_state[1 - 2*!!(
 		sizeof(TASK_STATE_TO_CHAR_STR)-1 != ilog2(TASK_STATE_MAX)+1)];
@@ -173,7 +174,8 @@ extern char ___assert_task_state[1 - 2*!!(
 			((task->state & (__TASK_STOPPED | __TASK_TRACED)) != 0)
 #define task_contributes_to_load(task)	\
 				((task->state & TASK_UNINTERRUPTIBLE) != 0 && \
-				 (task->flags & PF_FROZEN) == 0)
+				 (task->flags & PF_FROZEN) == 0 && \
+				 (task->state & TASK_IDLE) == 0)
 
 #define __set_task_state(tsk, state_value)		\
 	do { (tsk)->state = (state_value); } while (0)
include/trace/events/sched.h +2 -1
index 04c308413a5d..4553a2495c25 100644
@@ -144,7 +144,8 @@ TRACE_EVENT(sched_switch,
 		  __print_flags(__entry->prev_state & (TASK_STATE_MAX-1), "|",
 				{ 1, "S"} , { 2, "D" }, { 4, "T" }, { 8, "t" },
 				{ 16, "Z" }, { 32, "X" }, { 64, "x" },
-				{ 128, "K" }, { 256, "W" }, { 512, "P" }) : "R",
+				{ 128, "K" }, { 256, "W" }, { 512, "P" },
+				{ 1024, "I" }) : "R",
 		__entry->prev_state & TASK_STATE_MAX ? "+" : "",
 		__entry->next_comm, __entry->next_pid, __entry->next_prio)
 );
#7 lore raw
Quoted 13 lines · Peter Zijlstra
Quoted 4 lines · David Laight

Shame there isn't a process flag to indicate that the process will sleep uninterruptibly and that it doesn't matter. So don't count to the load average and don't emit a warning if it has been sleeping for a long time.

A process flag wouldn't work, because the task could block waiting for actual work to complete in other sleeps.

However, we could do something like the below; which would allow us writing things like:

Quoted 7 lines
	(void)___wait_event(*sk_sleep(sk),
			    sock_writeable(sk) || kthread_should_stop(),
			    TASK_UNINTERRUPTIBLE | TASK_IDLE, 0, 0,
			    schedule());

Marking the one wait-for-more-work as TASK_IDLE such that it doesn't contribute to the load avg.

Agreed, I thought about additional bit too.

Quoted 15 lines

static const char * const task_state_array[] = {

(quoted diff) +3 -11
-	"R (running)",		/*   0 */
-	"S (sleeping)",		/*   1 */
-	"D (disk sleep)",	/*   2 */
-	"T (stopped)",		/*   4 */
-	"t (tracing stop)",	/*   8 */
-	"Z (zombie)",		/*  16 */
-	"X (dead)",		/*  32 */
-	"x (dead)",		/*  64 */
-	"K (wakekill)",		/* 128 */
-	"W (waking)",		/* 256 */
-	"P (parked)",		/* 512 */
+	"R (running)",		/*    0 */
+	"S (sleeping)",		/*    1 */
+	"D (disk sleep)",	/*    2 */

Review

Quoted 10 lines
(quoted diff) +9 -0
+	"T (stopped)",		/*    4 */
+	"t (tracing stop)",	/*    8 */
+	"Z (zombie)",		/*   16 */
+	"X (dead)",		/*   32 */
+	"x (dead)",		/*   64 */
+	"K (wakekill)",		/*  128 */
+	"W (waking)",		/*  256 */
+	"P (parked)",		/*  512 */
+	"I (idle)",		/* 1024 */
 };

but I am not sure about what /proc/ should report in this case...

Review

Quoted 5 lines

#define task_contributes_to_load(task) \

(quoted diff) +2 -1
 				((task->state & TASK_UNINTERRUPTIBLE) != 0 && \
-				 (task->flags & PF_FROZEN) == 0)
+				 (task->flags & PF_FROZEN) == 0 && \
+				 (task->state & TASK_IDLE) == 0)

perhaps

	(task->state & (TASK_UNINTERRUPTIBLE | TASK_IDLE)) == TASK_UNINTERRUPTIBLE

can save an insn.

I am also wondering if it makes any sense to turn PF_FROZEN into TASK_FROZEN, something like (incomplete, probably racy) patch below. Note that it actually adds the new state, not the the qualifier.

Oleg.

include/linux/freezer.h +1 -1
@@ -23,7 +23,7 @@ extern unsigned int freeze_timeout_msecs
  */
 static inline bool frozen(struct task_struct *p)
 {
-	return p->flags & PF_FROZEN;
+	return p->state & TASK_FROZEN;
 }
 
 extern bool freezing_slow_path(struct task_struct *p);
kernel/freezer.c +5 -9
@@ -57,16 +57,13 @@ bool __refrigerator(bool check_kthr_stop
 	pr_debug("%s entered refrigerator\n", current->comm);
 
 	for (;;) {
-		set_current_state(TASK_UNINTERRUPTIBLE);
-
 		spin_lock_irq(&freezer_lock);
-		current->flags |= PF_FROZEN;
-		if (!freezing(current) ||
-		    (check_kthr_stop && kthread_should_stop()))
-			current->flags &= ~PF_FROZEN;
+		if (freezing(current) &&
+		    !(check_kthr_stop && kthread_should_stop()))
+			set_current_state(TASK_FROZEN);
 		spin_unlock_irq(&freezer_lock);
 
-		if (!(current->flags & PF_FROZEN))
+		if (!(current->state & TASK_FROZEN))
 			break;
 		was_frozen = true;
 		schedule();
@@ -148,8 +145,7 @@ void __thaw_task(struct task_struct *p)
 	 * refrigerator.
 	 */
 	spin_lock_irqsave(&freezer_lock, flags);
-	if (frozen(p))
-		wake_up_process(p);
+	try_to_wake_up(p, TASK_FROZEN, 0);
 	spin_unlock_irqrestore(&freezer_lock, flags);
 }
 
#8 lore raw

oom-kill && frozen()

Quoted 4 lines · Oleg Nesterov

I am also wondering if it makes any sense to turn PF_FROZEN into TASK_FROZEN, something like (incomplete, probably racy) patch below. Note that it actually adds the new state, not the the qualifier.

As for the current usage of PF_FROZEN... David, it seems that oom_scan_process_thread()->__thaw_task() is dead? Probably this was fine before, when __thaw_task() cleared the "need to freeze" condition, iirc it was PF_FROZEN.

But today __thaw_task() can't help, no? the task will simply schedule() in D state again.

Oleg.

#9 lore raw

Re: oom-kill && frozen()

Hello,

Quoted 5 lines · Oleg Nesterov
Quoted 3 lines · Oleg Nesterov

I am also wondering if it makes any sense to turn PF_FROZEN into TASK_FROZEN, something like (incomplete, probably racy) patch below. Note that it actually adds the new state, not the the qualifier.

Quoted 7 lines

As for the current usage of PF_FROZEN... David, it seems that oom_scan_process_thread()->__thaw_task() is dead? Probably this was fine before, when __thaw_task() cleared the "need to freeze" condition, iirc it was PF_FROZEN.

But today __thaw_task() can't help, no? the task will simply schedule() in D state again.

Yeah, it'll have to be actively excluded using e.g. PF_FREEZER_SKIP, which, BTW, can usually only be manipulated by the task itself. I've been saying this multiple times but this is yet another cost of having "frozen" as a separate completely alien task state, which is essentially close to being undefined when viewed from userland. We're spreading broken behaviors and complexity throughout the kernel by making this half broken state visible. :(

Thanks.

Signature · 1 line
tejun
#10 lore raw

Re: oom-kill && frozen()

Quoted 14 lines · Tejun Heo

Hello,

Quoted 10 lines · Oleg Nesterov
Quoted 3 lines · Oleg Nesterov

I am also wondering if it makes any sense to turn PF_FROZEN into TASK_FROZEN, something like (incomplete, probably racy) patch below. Note that it actually adds the new state, not the the qualifier.

As for the current usage of PF_FROZEN... David, it seems that oom_scan_process_thread()->__thaw_task() is dead? Probably this was fine before, when __thaw_task() cleared the "need to freeze" condition, iirc it was PF_FROZEN.

Quoted 5 lines
Quoted 2 lines

But today __thaw_task() can't help, no? the task will simply schedule() in D state again.

Yeah, it'll have to be actively excluded using e.g. PF_FREEZER_SKIP, which, BTW, can usually only be manipulated by the task itself.

Oh, yes, yes, yes, I agree. PF_FREEZER_SKIP and the growing number of freezable_schedule() makes this all more confusing.

In fact I was think about something like

	1. Add the new __TASK_FREEZABLE qualifier

	2. Turn freezable_schedule() into

		void freezable_schedule(void)
		{
			spin_lock_irq(&current->pi_lock);
			if (current->state)
				current->state |= __TASK_FREEZABLE
			spin_unlock_irq(&current->pi_lock);
			
			schedule();

			try_to_freeze();
		}

	3. Kill PF_FREEZER_SKIP/freezer_do_not_count/count/should_skip

	4. Change freeze_task() and fake_signal_wake_up()

		-	wake_up_state(p, TASK_INTERRUPTIBLE);
		+	wake_up_state(p, TASK_INTERRUPTIBLE | __TASK_FREEZABLE);

Unfortunately, this can only work if the caller can tolerate the false wakeup. We can even fix wait_for_vfork_done(), but say ptrace_stop() can't work this way.

And even if we can make this work, the very fact that freezable_schedule() does schedule() twice does not look right.

_Perhaps_ we can do something like "selective wakeup"? IOW, ignoring the races/details,

	1. Add __TASK_FROZEN qualifier _and_ state

	2. Change frozen(),

		static inline bool frozen(struct task_struct *p)
		{
			return p->state & __TASK_FROZEN;
		}

	2. Change freezable_schedule(),

		void freezable_schedule(void)
		{
			spin_lock_irq(&current->pi_lock);
			if (current->state)
				current->state |= __TASK_FROZEN;
			spin_unlock_irq(&current->pi_lock);
			
			schedule();
		}

	3. Change __refrigerator() to use saved_state | __TASK_FROZEN
	   too.

	4. Finally, change try_to_wake_up() path to do

		-	p->state = TASK_WAKING;
		+	p->state &= ~state;
		+	if (p->state & ~(TASK_DEAD | TASK_WAKEKILL | TASK_PARKED))
		+		return;
		+	else
		+		p->state = TASK_WAKING;

	   IOW, if the task sleeps in, say, TASK_INTERRUPTIBLE | __TASK_FROZEN
	   then it need both try_to_wake_up(TASK_INTERRUPTIBLE) and
	   try_to_wake_up(__TASK_FROZEN) to wake up.

	 5. Kill PF_FREEZER_SKIP / etc.

Unfortunately, 4. is obviously needs more changes, although at first glance nothing really nontrivial... we need a common helper for try_to_wake_up() and ttwu_remote() which checks/changes ->state and we need to avoid "stat" if we do not actually wake up.

Hmm. and this all makes me think that at least s/PF_FROZEN/TASK_FROZEN/ as a first step actually makes some sense... Note the "qualifier _and_ state" above.

Tejun, Peter, do you think this makes any sense? I am just curious, but "selective wakeup" looks potentially useful.

And what about oom_scan_process_thread() ? should we simply kill this dead frozen/__thaw_task code or should we change freezing() to respect TIF_MEMDIE?

Oleg.

maintainer
#11 lore raw

Re: oom-kill && frozen()

Quoted 13 lines · Oleg Nesterov
	4. Finally, change try_to_wake_up() path to do

		-	p->state = TASK_WAKING;
		+	p->state &= ~state;
		+	if (p->state & ~(TASK_DEAD | TASK_WAKEKILL | TASK_PARKED))
		+		return;
		+	else
		+		p->state = TASK_WAKING;

	   IOW, if the task sleeps in, say, TASK_INTERRUPTIBLE | __TASK_FROZEN
	   then it need both try_to_wake_up(TASK_INTERRUPTIBLE) and
	   try_to_wake_up(__TASK_FROZEN) to wake up.
Quoted 2 lines

Tejun, Peter, do you think this makes any sense? I am just curious, but "selective wakeup" looks potentially useful.

I've never looked at any of this freeze stuff, so I cannot comment too much. However we should be very careful not to add too much to relative hot paths for the relative rare case of freezing stuff.

#12 lore raw

Re: oom-kill && frozen()

Quoted 15 lines · Peter Zijlstra
Quoted 12 lines · Oleg Nesterov
	4. Finally, change try_to_wake_up() path to do

		-	p->state = TASK_WAKING;
		+	p->state &= ~state;
		+	if (p->state & ~(TASK_DEAD | TASK_WAKEKILL | TASK_PARKED))
		+		return;
		+	else
		+		p->state = TASK_WAKING;

	   IOW, if the task sleeps in, say, TASK_INTERRUPTIBLE | __TASK_FROZEN
	   then it need both try_to_wake_up(TASK_INTERRUPTIBLE) and
	   try_to_wake_up(__TASK_FROZEN) to wake up.
Quoted 6 lines
Quoted 2 lines

Tejun, Peter, do you think this makes any sense? I am just curious, but "selective wakeup" looks potentially useful.

I've never looked at any of this freeze stuff, so I cannot comment too much. However we should be very careful not to add too much to relative hot paths for the relative rare case of freezing stuff.

Yes, yes, sure.

Plus my description was confusing and incomplete (and I am sure I missed something more). I forgot to mention that freeze_task() should also add the FROZEN state if it sees FROZEN qualifier, this needs more changes.

So please forget. May be I'll _try_ to make something working (at least for discussion), but I am not sure.

Oleg.

#13 lore raw

__refrigerator() && saved task->state

In reply to Oleg Nesterov

Sorry for noise, but I am totally confused.

Could you please remind why __refrigerator() saves/restores task->state?

I can see only one reason: set_freezable() kernel threads which check kthread_should_stop() and do try_to_freeze() by hand.

But does this save/restore actually help?

For example kauditd_thread() looks obviously racy exactly because try_to_freeze() can return in TASK_INTERRUPTIBLE state after wake_up(kauditd_wait) was already called, we can miss an event.

At first glance it would be better to simply kill this logic? If it was called with ->state != 0, the caller is going to schedule() and it probably executes the wait_event-like code, in this case it would me more safe to pretend the task got a spurious wakeup?

(as for kauditd_thread() in particular, it looks wrong in any
case, even kthread_should_stop() check doesn't look right, it needs kthread_freezable_should_stop() afaics).

But I guess I missed something else...

Oleg.

maintainer
#14 lore raw

Re: __refrigerator() && saved task->state

Quoted 4 lines · Oleg Nesterov

At first glance it would be better to simply kill this logic? If it was called with ->state != 0, the caller is going to schedule() and it probably executes the wait_event-like code, in this case it would me more safe to pretend the task got a spurious wakeup?

Note that in general the kernel cannot deal with spurious wakeups :/

Most proper locks and wait primitives can, but there's enough open-coded crap out there that can not.

I tried..

#15 lore raw

Re: __refrigerator() && saved task->state

Quoted 11 lines · Peter Zijlstra
Quoted 4 lines · Oleg Nesterov

At first glance it would be better to simply kill this logic? If it was called with ->state != 0, the caller is going to schedule() and it probably executes the wait_event-like code, in this case it would me more safe to pretend the task got a spurious wakeup?

Note that in general the kernel cannot deal with spurious wakeups :/

Most proper locks and wait primitives can, but there's enough open-coded crap out there that can not.

Oh yes, I understand.

My point is, "restore the old state" in this case looks worse simply because you miss any wakeup in between which was going to clear that state. And afaics only kthreads can call __refrigerator() in !RUNNING.

But let me repeat, I am almost sure I missed something else.

Oleg.

maintainer
#16 lore raw
In reply to Oleg Nesterov
Quoted 43 lines · Oleg Nesterov
Quoted 13 lines · Peter Zijlstra
Quoted 4 lines · David Laight

Shame there isn't a process flag to indicate that the process will sleep uninterruptibly and that it doesn't matter. So don't count to the load average and don't emit a warning if it has been sleeping for a long time.

A process flag wouldn't work, because the task could block waiting for actual work to complete in other sleeps.

However, we could do something like the below; which would allow us writing things like:

Quoted 7 lines
	(void)___wait_event(*sk_sleep(sk),
			    sock_writeable(sk) || kthread_should_stop(),
			    TASK_UNINTERRUPTIBLE | TASK_IDLE, 0, 0,
			    schedule());

Marking the one wait-for-more-work as TASK_IDLE such that it doesn't contribute to the load avg.

Agreed, I thought about additional bit too.

Quoted 19 lines

static const char * const task_state_array[] = {

(quoted diff) +7 -11
-	"R (running)",		/*   0 */
-	"S (sleeping)",		/*   1 */
-	"D (disk sleep)",	/*   2 */
-	"T (stopped)",		/*   4 */
-	"t (tracing stop)",	/*   8 */
-	"Z (zombie)",		/*  16 */
-	"X (dead)",		/*  32 */
-	"x (dead)",		/*  64 */
-	"K (wakekill)",		/* 128 */
-	"W (waking)",		/* 256 */
-	"P (parked)",		/* 512 */
+	"R (running)",		/*    0 */
+	"S (sleeping)",		/*    1 */
+	"D (disk sleep)",	/*    2 */
+	"T (stopped)",		/*    4 */
+	"t (tracing stop)",	/*    8 */
+	"Z (zombie)",		/*   16 */
+	"X (dead)",		/*   32 */
Quoted 8 lines

Review

Quoted 6 lines
(quoted diff) +5 -0
+	"x (dead)",		/*   64 */
+	"K (wakekill)",		/*  128 */
+	"W (waking)",		/*  256 */
+	"P (parked)",		/*  512 */
+	"I (idle)",		/* 1024 */
 };

but I am not sure about what /proc/ should report in this case...

We have to put in something...

	BUILD_BUG_ON(1 + ilog2(TASK_STATE_MAX) != ARRAY_SIZE(task_state_array));

However, since we always set it together with TASK_UNINTERUPTIBLE userspace shouldn't actually ever see the I thing.

Quoted 11 lines

Review

Quoted 5 lines

#define task_contributes_to_load(task) \

(quoted diff) +2 -1
 				((task->state & TASK_UNINTERRUPTIBLE) != 0 && \
-				 (task->flags & PF_FROZEN) == 0)
+				 (task->flags & PF_FROZEN) == 0 && \
+				 (task->state & TASK_IDLE) == 0)

perhaps

	(task->state & (TASK_UNINTERRUPTIBLE | TASK_IDLE)) == TASK_UNINTERRUPTIBLE

can save an insn.

Fair enough.

include/linux/freezer.h extern unsigned int freeze_timeout_msecs · line 26

Quoted 12 lines

I am also wondering if it makes any sense to turn PF_FROZEN into TASK_FROZEN, something like (incomplete, probably racy) patch below. Note that it actually adds the new state, not the the qualifier.

include/linux/freezer.h +1 -1
@@ -23,7 +23,7 @@ extern unsigned int freeze_timeout_msecs
  */
 static inline bool frozen(struct task_struct *p)
 {
-	return p->flags & PF_FROZEN;
+	return p->state & TASK_FROZEN;

do we want == there? Does it make sense to allow it be set with other state flags?

Quoted 27 lines
include/linux/freezer.h +0 -0
 }
 
 extern bool freezing_slow_path(struct task_struct *p);
kernel/freezer.c +4 -7
@@ -57,16 +57,13 @@ bool __refrigerator(bool check_kthr_stop
 	pr_debug("%s entered refrigerator\n", current->comm);
 
 	for (;;) {
-		set_current_state(TASK_UNINTERRUPTIBLE);
-
 		spin_lock_irq(&freezer_lock);
-		current->flags |= PF_FROZEN;
-		if (!freezing(current) ||
-		    (check_kthr_stop && kthread_should_stop()))
-			current->flags &= ~PF_FROZEN;
+		if (freezing(current) &&
+		    !(check_kthr_stop && kthread_should_stop()))
+			set_current_state(TASK_FROZEN);
 		spin_unlock_irq(&freezer_lock);
 
-		if (!(current->flags & PF_FROZEN))
+		if (!(current->state & TASK_FROZEN))
 			break;
 		was_frozen = true;
 		schedule();

kernel/freezer.c void __thaw_task(struct task_struct *p) · line 150

Quoted 8 lines
kernel/freezer.c +1 -2
@@ -148,8 +145,7 @@ void __thaw_task(struct task_struct *p)
 	 * refrigerator.
 	 */
 	spin_lock_irqsave(&freezer_lock, flags);
-	if (frozen(p))
-		wake_up_process(p);
+	try_to_wake_up(p, TASK_FROZEN, 0);
 	spin_unlock_irqrestore(&freezer_lock, flags);
 }

Should work I suppose... I'm not entirely sure why that's a PF to begin with.

#17 lore raw
Quoted 11 lines · Peter Zijlstra
Quoted 8 lines · Oleg Nesterov

Review

Quoted 5 lines · Peter Zijlstra

static const char * const task_state_array[] = {
...

(quoted diff) +1 -0
+	"I (idle)",		/* 1024 */
 };

but I am not sure about what /proc/ should report in this case...

Quoted 6 lines

We have to put in something...

	BUILD_BUG_ON(1 + ilog2(TASK_STATE_MAX) != ARRAY_SIZE(task_state_array));

However, since we always set it together with TASK_UNINTERUPTIBLE userspace shouldn't actually ever see the I thing.

OOPS. I didn't know that get_task_state() does &= TASK_REPORT. So it can never report anything > EXIT_DEAD.

Perhaps we should change BUILD_BUG_ON() and shrink task_state_array?

	--- x/include/linux/sched.h
	+++ x/include/linux/sched.h
	@@ -163,7 +163,7 @@ extern char ___assert_task_state[1 - 2*!
	 /* get_task_state() */
	 #define TASK_REPORT		(TASK_RUNNING | TASK_INTERRUPTIBLE | \
					 TASK_UNINTERRUPTIBLE | __TASK_STOPPED | \
	-				 __TASK_TRACED)
	+				 __TASK_TRACED | EXIT_ZOMBIE | EXIT_DEAD)
	 
	 #define task_is_traced(task)	((task->state & __TASK_TRACED) != 0)
	 #define task_is_stopped(task)	((task->state & __TASK_STOPPED) != 0)
	--- x/fs/proc/array.c
	+++ x/fs/proc/array.c
	@@ -148,16 +148,12 @@ static const char * const task_state_arr
	 
	 static inline const char *get_task_state(struct task_struct *tsk)
	 {
	-	unsigned int state = (tsk->state & TASK_REPORT) | tsk->exit_state;
	+	unsigned int state = (tsk->state | tsk->exit_state) & TASK_REPORT;
		const char * const *p = &task_state_array[0];
	 
	-	BUILD_BUG_ON(1 + ilog2(TASK_STATE_MAX) != ARRAY_SIZE(task_state_array));
	+	BUILD_BUG_ON(1 + ilog2(TASK_REPORT) != ARRAY_SIZE(task_state_array));
	 
	-	while (state) {
	-		p++;
	-		state >>= 1;
	-	}
	-	return *p;
	+	return task_state_array[fls(state)];
	 }
	 
	 static inline void task_state(struct seq_file *m, struct pid_namespace *ns,
Quoted 4 lines
Quoted 4 lines

I am also wondering if it makes any sense to turn PF_FROZEN into TASK_FROZEN, something like (incomplete, probably racy) patch below. Note that it actually adds the new state, not the the qualifier.

Quoted 10 lines

include/linux/freezer.h extern unsigned int freeze_timeout_msecs · line 26

Quoted 8 lines
include/linux/freezer.h +1 -1
@@ -23,7 +23,7 @@ extern unsigned int freeze_timeout_msecs
  */
 static inline bool frozen(struct task_struct *p)
 {
-	return p->flags & PF_FROZEN;
+	return p->state & TASK_FROZEN;

do we want == there?

Not really, but if we add TASK_FROZEN then we should probably add task_is_frozen() just for consistency (frozen() should use this helper) and all task_is_* helpers do "&".

And,

Quoted 2 lines

Does it make sense to allow it be set with other
state flags?

Not sure, but _perhaps_ TASK_FROZEN | TASK_KILLABLE can be used too, say, we can add CGROUP_FROZEN_KILLABLE. Probably makes no sense, I dunno.

Quoted 18 lines
Quoted 18 lines
kernel/freezer.c +3 -6
@@ -57,16 +57,13 @@ bool __refrigerator(bool check_kthr_stop
 	pr_debug("%s entered refrigerator\n", current->comm);
 
 	for (;;) {
-		set_current_state(TASK_UNINTERRUPTIBLE);
-
 		spin_lock_irq(&freezer_lock);
-		current->flags |= PF_FROZEN;
-		if (!freezing(current) ||
-		    (check_kthr_stop && kthread_should_stop()))
-			current->flags &= ~PF_FROZEN;
+		if (freezing(current) &&
+		    !(check_kthr_stop && kthread_should_stop()))
+			set_current_state(TASK_FROZEN);
 		spin_unlock_irq(&freezer_lock);
 
Quoted 16 lines
Quoted 6 lines
kernel/freezer.c +1 -1
-		if (!(current->flags & PF_FROZEN))
+		if (!(current->state & TASK_FROZEN))
 			break;
 		was_frozen = true;
 		schedule();

kernel/freezer.c void __thaw_task(struct task_struct *p) · line 150

Quoted 8 lines
kernel/freezer.c +1 -2
@@ -148,8 +145,7 @@ void __thaw_task(struct task_struct *p)
 	 * refrigerator.
 	 */
 	spin_lock_irqsave(&freezer_lock, flags);
-	if (frozen(p))
-		wake_up_process(p);
+	try_to_wake_up(p, TASK_FROZEN, 0);
 	spin_unlock_irqrestore(&freezer_lock, flags);
 }

Should work I suppose...

And perhaps we can simplify this a bit. Not sure we can kill freezer_lock altogether, but at least __thaw_task() can avoid it.

But I am still not sure the new TASK_ state really makes sense, even if it looks a bit more clean to me.

OTOH, personally I think that /proc/pid/status should report "F (frozen)" if frozen(), but this doesn't need TASK_FROZEN of course.

Quoted 2 lines

I'm not entirely sure why that's a PF to begin
with.

Oh, it had more PF_'s until tj improved (and cleanuped) this code ;)

Oleg.