sched: fix nr_uninterruptible accounting of frozen tasks really

commit e3c8ca8336 (sched: do not count frozen tasks toward load) broke
the nr_uninterruptible accounting on freeze/thaw. On freeze the task
is excluded from accounting with a check for (task->flags &
PF_FROZEN), but that flag is cleared before the task is thawed. So
while we prevent that the task with state TASK_UNINTERRUPTIBLE
is accounted to nr_uninterruptible on freeze we decrement
nr_uninterruptible on thaw.

Use a separate flag which is handled by the freezing task itself. Set
it before calling the scheduler with TASK_UNINTERRUPTIBLE state and
clear it after we return from frozen state.

Cc: <stable@kernel.org>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>

+9 -1
+2 -1
include/linux/sched.h
··· 209 209 ((task->state & (__TASK_STOPPED | __TASK_TRACED)) != 0) 210 210 #define task_contributes_to_load(task) \ 211 211 ((task->state & TASK_UNINTERRUPTIBLE) != 0 && \ 212 - (task->flags & PF_FROZEN) == 0) 212 + (task->flags & PF_FREEZING) == 0) 213 213 214 214 #define __set_task_state(tsk, state_value) \ 215 215 do { (tsk)->state = (state_value); } while (0) ··· 1680 1680 #define PF_MEMALLOC 0x00000800 /* Allocating memory */ 1681 1681 #define PF_FLUSHER 0x00001000 /* responsible for disk writeback */ 1682 1682 #define PF_USED_MATH 0x00002000 /* if unset the fpu must be initialized before use */ 1683 + #define PF_FREEZING 0x00004000 /* freeze in progress. do not account to load */ 1683 1684 #define PF_NOFREEZE 0x00008000 /* this thread should not be frozen */ 1684 1685 #define PF_FROZEN 0x00010000 /* frozen for system suspend */ 1685 1686 #define PF_FSTRANS 0x00020000 /* inside a filesystem transaction */
+7
kernel/freezer.c
··· 44 44 recalc_sigpending(); /* We sent fake signal, clean it up */ 45 45 spin_unlock_irq(&current->sighand->siglock); 46 46 47 + /* prevent accounting of that task to load */ 48 + current->flags |= PF_FREEZING; 49 + 47 50 for (;;) { 48 51 set_current_state(TASK_UNINTERRUPTIBLE); 49 52 if (!frozen(current)) 50 53 break; 51 54 schedule(); 52 55 } 56 + 57 + /* Remove the accounting blocker */ 58 + current->flags &= ~PF_FREEZING; 59 + 53 60 pr_debug("%s left refrigerator\n", current->comm); 54 61 __set_current_state(save); 55 62 }