Skip to contentSkip to conversation

PF_flags cleaups

RFC PATCH In review Peter Zijlstra 5 participants 12 messages Sep 20, 2010

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

cover PF_flags cleaups

Because we recently ran out of PF_flags, try and clean up.

Patches are on top of -tip, which already includes the PF_ALIGNWARN removal.

#1 lore raw

RFC PATCH 1/2 kernel: remove PF_FLUSHER

PF_FLUSHER is only ever set, not tested, remove it.

---
 fs/fs-writeback.c     |    2 +-
 include/linux/sched.h |    1 -
 mm/backing-dev.c      |    2 +-
 3 files changed, 2 insertions(+), 3 deletions(-)
fs/fs-writeback.c +1 -1
Index: linux-2.6/fs/fs-writeback.c
===================================================================
@@ -782,7 +782,7 @@ int bdi_writeback_thread(void *data)
 	struct backing_dev_info *bdi = wb->bdi;
 	long pages_written;
 
-	current->flags |= PF_FLUSHER | PF_SWAPWRITE;
+	current->flags |= PF_SWAPWRITE;
 	set_freezable();
 	wb->last_active = jiffies;
 
include/linux/sched.h +0 -1
Index: linux-2.6/include/linux/sched.h
===================================================================
@@ -1702,7 +1702,6 @@ extern void thread_group_times(struct ta
 #define PF_DUMPCORE	0x00000200	/* dumped core */
 #define PF_SIGNALED	0x00000400	/* killed by a signal */
 #define PF_MEMALLOC	0x00000800	/* Allocating memory */
-#define PF_FLUSHER	0x00001000	/* responsible for disk writeback */
 #define PF_USED_MATH	0x00002000	/* if unset the fpu must be initialized before use */
 #define PF_FREEZING	0x00004000	/* freeze in progress. do not account to load */
 #define PF_NOFREEZE	0x00008000	/* this thread should not be frozen */
mm/backing-dev.c +1 -1
Index: linux-2.6/mm/backing-dev.c
===================================================================
@@ -360,7 +360,7 @@ static int bdi_forker_thread(void *ptr)
 {
 	struct bdi_writeback *me = ptr;
 
-	current->flags |= PF_FLUSHER | PF_SWAPWRITE;
+	current->flags |= PF_SWAPWRITE;
 	set_freezable();
 
 	/*
#3 lore raw

RFC PATCH 2/2 kernel: extract thread types from task_struct::flags

In reply to Peter Zijlstra

Free up a few more PF_flags by moving thread types out to their own variable.

Initially I compressed the types into less bits inside task_struct::flags, but Thomas suggested I move them to their own field.

---
 fs/exec.c                   |    4 +-
 fs/xfs/linux-2.6/xfs_aops.c |    2 -
 include/linux/init_task.h   |    3 +
 include/linux/kvm_host.h    |    4 +-
 include/linux/sched.h       |   79 +++++++++++++++++++++++++++-----------------
 include/linux/swap.h        |    2 -
 kernel/exit.c               |    3 +
 kernel/fork.c               |   12 +++++-
 kernel/ptrace.c             |    2 -
 kernel/rtmutex-tester.c     |    2 -
 kernel/rtmutex_common.h     |    2 -
 kernel/sched.c              |    7 ++-
 kernel/workqueue.c          |    4 +-
 lib/is_single_threaded.c    |    2 -
 mm/oom_kill.c               |    4 +-
 mm/vmscan.c                 |    3 +
 16 files changed, 83 insertions(+), 52 deletions(-)
fs/exec.c +2 -2
Index: linux-2.6/fs/exec.c
===================================================================
@@ -1401,7 +1401,7 @@ int do_execve(const char * filename,
 	if (retval < 0)
 		goto out;
 
-	current->flags &= ~PF_KTHREAD;
+	task_set_type(current, tt_user);
 	retval = search_binary_handler(bprm,regs);
 	if (retval < 0)
 		goto out;
@@ -1651,7 +1651,7 @@ static inline int zap_threads(struct tas
 	for_each_process(g) {
 		if (g == tsk->group_leader)
 			continue;
-		if (g->flags & PF_KTHREAD)
+		if (task_type(g) >= tt_kernel)
 			continue;
 		p = g;
 		do {
fs/xfs/linux-2.6/xfs_aops.c +1 -1
Index: linux-2.6/fs/xfs/linux-2.6/xfs_aops.c
===================================================================
@@ -1067,7 +1067,7 @@ xfs_vm_writepage(
 	 * filesystems like XFS, btrfs and ext4 have to take care of this
 	 * by themselves.
 	 */
-	if ((current->flags & (PF_MEMALLOC|PF_KSWAPD)) == PF_MEMALLOC)
+	if (task_type(current) != tt_kswapd && (current->flags & PF_MEMALLOC))
 		goto redirty;
 
 	/*
include/linux/init_task.h +2 -1
Index: linux-2.6/include/linux/init_task.h
===================================================================
@@ -117,7 +117,8 @@ extern struct cred init_cred;
 	.state		= 0,						\
 	.stack		= &init_thread_info,				\
 	.usage		= ATOMIC_INIT(2),				\
-	.flags		= PF_KTHREAD,					\
+	.flags		= 0,						\
+	.threadtype	= tt_kernel,					\
 	.lock_depth	= -1,						\
 	.prio		= MAX_PRIO-20,					\
 	.static_prio	= MAX_PRIO-20,					\
include/linux/kvm_host.h +2 -2
Index: linux-2.6/include/linux/kvm_host.h
===================================================================
@@ -509,13 +509,13 @@ static inline int kvm_deassign_device(st
 static inline void kvm_guest_enter(void)
 {
 	account_system_vtime(current);
-	current->flags |= PF_VCPU;
+	task_set_type(current, tt_vcpu);
 }
 
 static inline void kvm_guest_exit(void)
 {
 	account_system_vtime(current);
-	current->flags &= ~PF_VCPU;
+	task_set_type(current, tt_user);
 }
 
 static inline gpa_t gfn_to_gpa(gfn_t gfn)
include/linux/sched.h +49 -30

97 lines not on this page · raw message

include/linux/swap.h +1 -1
Index: linux-2.6/include/linux/swap.h
===================================================================
@@ -23,7 +23,7 @@ struct bio;
 
 static inline int current_is_kswapd(void)
 {
-	return current->flags & PF_KSWAPD;
+	return task_type(current) == tt_kswapd;
 }
 
 /*
kernel/exit.c +2 -1
Index: linux-2.6/kernel/exit.c
===================================================================
@@ -431,7 +431,8 @@ void daemonize(const char *name, ...)
 	 * We don't want to have TIF_FREEZE set if the system-wide hibernation
 	 * or suspend transition begins right now.
 	 */
-	current->flags |= (PF_NOFREEZE | PF_KTHREAD);
+	task_set_type(current, tt_kernel);
+	current->flags |= PF_NOFREEZE;
 
 	if (current->nsproxy != &init_nsproxy) {
 		get_nsproxy(&init_nsproxy);
kernel/fork.c +10 -2
Index: linux-2.6/kernel/fork.c
===================================================================
@@ -570,7 +570,7 @@ struct mm_struct *get_task_mm(struct tas
 	task_lock(task);
 	mm = task->mm;
 	if (mm) {
-		if (task->flags & PF_KTHREAD)
+		if (task_type(task) >= tt_kernel)
 			mm = NULL;
 		else
 			atomic_inc(&mm->mm_users);
@@ -910,8 +910,16 @@ static int copy_signal(unsigned long clo
 static void copy_flags(unsigned long clone_flags, struct task_struct *p)
 {
 	unsigned long new_flags = p->flags;
+	unsigned long new_type;
 
-	new_flags &= ~(PF_SUPERPRIV | PF_WQ_WORKER);
+	if (task_type(p) >= tt_kernel)
+		new_type = tt_kernel;
+	else
+		new_type = tt_user;
+
+	task_set_type(p, new_type);
+
+	new_flags &= ~PF_SUPERPRIV;
 	new_flags |= PF_FORKNOEXEC;
 	new_flags |= PF_STARTING;
 	p->flags = new_flags;
kernel/ptrace.c +1 -1
Index: linux-2.6/kernel/ptrace.c
===================================================================
@@ -170,7 +170,7 @@ int ptrace_attach(struct task_struct *ta
 	audit_ptrace(task);
 
 	retval = -EPERM;
-	if (unlikely(task->flags & PF_KTHREAD))
+	if (unlikely(task_type(task) >= tt_kernel))
 		goto out;
 	if (same_thread_group(task, current))
 		goto out;
kernel/rtmutex-tester.c +1 -1
Index: linux-2.6/kernel/rtmutex-tester.c
===================================================================
@@ -259,7 +259,7 @@ static int test_func(void *data)
 	struct test_thread_data *td = data;
 	int ret;
 
-	current->flags |= PF_MUTEX_TESTER;
+	task_set_type(current, tt_mutex_tester);
 	set_freezable();
 	allow_signal(SIGHUP);
 
kernel/rtmutex_common.h +1 -1
Index: linux-2.6/kernel/rtmutex_common.h
===================================================================
@@ -26,7 +26,7 @@ extern void schedule_rt_mutex_test(struc
 
 #define schedule_rt_mutex(_lock)				\
   do {								\
-	if (!(current->flags & PF_MUTEX_TESTER))		\
+	if (task_type(current) != tt_mutex_tester)		\
 		schedule();					\
 	else							\
 		schedule_rt_mutex_test(_lock);			\
kernel/sched.c +4 -3
Index: linux-2.6/kernel/sched.c
===================================================================
@@ -2322,7 +2322,7 @@ static inline void ttwu_post_activation(
 	}
 #endif
 	/* if a worker is waking up, notify workqueue */
-	if ((p->flags & PF_WQ_WORKER) && success)
+	if (task_type(p) == tt_worker && success)
 		wq_worker_waking_up(p, cpu_of(rq));
 }
 
@@ -3380,7 +3380,8 @@ void account_system_time(struct task_str
 	struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
 	cputime64_t tmp;
 
-	if ((p->flags & PF_VCPU) && (irq_count() - hardirq_offset == 0)) {
+
+	if (task_type(p) == tt_vcpu && (irq_count() - hardirq_offset == 0)) {
 		account_guest_time(p, cputime, cputime_scaled);
 		return;
 	}
@@ -3772,7 +3773,7 @@ asmlinkage void __sched schedule(void)
 			 * task to maintain concurrency.  If so, wake
 			 * up the task.
 			 */
-			if (prev->flags & PF_WQ_WORKER) {
+			if (task_type(prev) == tt_worker) {
 				struct task_struct *to_wakeup;
 
 				to_wakeup = wq_worker_sleeping(prev, cpu);
kernel/workqueue.c +2 -2
Index: linux-2.6/kernel/workqueue.c
===================================================================
@@ -1888,14 +1888,14 @@ static int worker_thread(void *__worker)
 	struct global_cwq *gcwq = worker->gcwq;
 
 	/* tell the scheduler that this is a workqueue worker */
-	worker->task->flags |= PF_WQ_WORKER;
+	task_set_type(worker->task, tt_worker);
 woke_up:
 	spin_lock_irq(&gcwq->lock);
 
 	/* DIE can be set only while we're idle, checking here is enough */
 	if (worker->flags & WORKER_DIE) {
 		spin_unlock_irq(&gcwq->lock);
-		worker->task->flags &= ~PF_WQ_WORKER;
+		task_set_type(worker->task, tt_kernel);
 		return 0;
 	}
 
lib/is_single_threaded.c +1 -1
Index: linux-2.6/lib/is_single_threaded.c
===================================================================
@@ -31,7 +31,7 @@ bool current_is_single_threaded(void)
 	ret = false;
 	rcu_read_lock();
 	for_each_process(p) {
-		if (unlikely(p->flags & PF_KTHREAD))
+		if (unlikely(task_type(p) >= tt_kernel))
 			continue;
 		if (unlikely(p == task->group_leader))
 			continue;
mm/oom_kill.c +2 -2
Index: linux-2.6/mm/oom_kill.c
===================================================================
@@ -126,7 +126,7 @@ static bool oom_unkillable_task(struct t
 {
 	if (is_global_init(p))
 		return true;
-	if (p->flags & PF_KTHREAD)
+	if (task_type(p) >= tt_kernel)
 		return true;
 
 	/* When mem_cgroup_out_of_memory() and p is not member of the group */
@@ -356,7 +356,7 @@ static void dump_tasks(const struct mem_
 
 	pr_info("[ pid ]   uid  tgid total_vm      rss cpu oom_adj oom_score_adj name\n");
 	for_each_process(p) {
-		if (p->flags & PF_KTHREAD)
+		if (task_type(p) >= tt_kernel)
 			continue;
 		if (mem && !task_in_mem_cgroup(p, mem))
 			continue;
mm/vmscan.c +2 -1
Index: linux-2.6/mm/vmscan.c
===================================================================
@@ -2316,7 +2316,8 @@ static int kswapd(void *p)
 	 * us from recursively trying to free more memory as we're
 	 * trying to free the first piece of memory in the first place).
 	 */
-	tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
+	task_set_type(tsk, tt_kswapd);
+	tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE;
 	set_freezable();
 
 	order = 0;
#7 lore raw
In reply to Peter Zijlstra
Quoted 4 lines · Peter Zijlstra

Because we recently ran out of PF_flags, try and clean up.

Patches are on top of -tip, which already includes the PF_ALIGNWARN removal.

Looks ok by me conceptually, but I _really_ hate the naming of that second patch and the pointless churn.

I also find it disgusting to have that [set_]task_type() "helper" abstraction layer. It doesn't actually help anything at all, and just makes it less clear what is going on.

So instead of code like this:

if (task_type(p) >= tt_kernel)

why the heck isn't it just a much more readable old code (with just a renamed new field) and just do

if (task->task_type & TT_KTHREAD)

instead? Even if you were to want to use an enum rather than a set of #define's, the all-caps thing makes it visually obvious that we're talking about some constant rather than a variable named "tt_kernel".

I dunno. I'd personally also just keep it as a bitmask thing, if only to make the patch more readable. IOW, the patch _should_ have just turned

        tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;

into

        tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE;
        tsk->task_type |= TT_KSWAPD;

and then the patch would always have been an "obvious straightforward identity transform".

For example, look at the mess you did:

- new_flags &= ~(PF_SUPERPRIV | PF_WQ_WORKER);
+ if (task_type(p) >= tt_kernel)
+ new_type = tt_kernel;
+ else
+ new_type = tt_user;
+
+ task_set_type(p, new_type);
+

and look how much straightforward it would have been had you just kept the same simple semantics with just a new field:

- new_flags &= ~(PF_SUPERPRIV | PF_WQ_WORKER);
+ new_type &= ~(TT_SUPERPRIV | TT_WQ_WORKER);

and nobody could possibly have any objections to a straightforward "move the task type flags into a separate field" patch.

No?

Linus

maintainer
#8 lore raw
Quoted 16 lines · Linus Torvalds
Quoted 4 lines · Peter Zijlstra

Because we recently ran out of PF_flags, try and clean up.

Patches are on top of -tip, which already includes the PF_ALIGNWARN removal.

Looks ok by me conceptually, but I _really_ hate the naming of that second patch and the pointless churn.

and look how much straightforward it would have been had you just kept the same simple semantics with just a new field:

(quoted diff) +1 -1
-       new_flags &= ~(PF_SUPERPRIV | PF_WQ_WORKER);
+       new_type &= ~(TT_SUPERPRIV | TT_WQ_WORKER);
Quoted 2 lines

and nobody could possibly have any objections to a straightforward "move the task type flags into a separate field" patch.

Sure, can do. Like said, my initial approach was to compress these type bits into fewer bits by converting all these individual bits (PF_KSWAPD, PF_WQ_WORKER, etc) into a linear range which spans less bits.

But indeed, if we're OK with adding a new field (which is I think the biggest question, and your reply seems imply you don't mind at all), then keeping the old structure and moving them over to a new field will generate a much saner patch.

(I only left the helper functions in in case people would object to adding another field and we'd need to really compress bits again).

One point though, I noticed we actually expose p->flags to userspace, which basically makes PF_flags an ABI, do we care?

#9 lore raw
Quoted 3 lines · Peter Zijlstra

One point though, I noticed we actually expose p->flags to userspace, which basically makes PF_flags an ABI, do we care?

Hmm. Where? And no, I don't think we care. But let's keep it in mind if something subtle breaks in surprising ways.

Linus

#2 lore raw

Re: [RFC][PATCH 1/2] kernel: remove PF_FLUSHER

In reply to Peter Zijlstra
Quoted 1 line · Peter Zijlstra

PF_FLUSHER is only ever set, not tested, remove it.

Looks good, it probably didn't get dropped appropriately when current_is_pdflush() was killed.

Signature · 1 line
Jens Axboe
maintainer
#10 lore raw
In reply to Linus Torvalds
Quoted 7 lines · Linus Torvalds
Quoted 3 lines · Peter Zijlstra

One point though, I noticed we actually expose p->flags to userspace, which basically makes PF_flags an ABI, do we care?

Hmm. Where? And no, I don't think we care. But let's keep it in mind if something subtle breaks in surprising ways.

fs/proc/array.c:do_task_stat()

I'll rework the patches and simply move the bits around to another field. We'll see what falls over.. :-)

#11 lore raw
Quoted 2 lines · Peter Zijlstra

fs/proc/array.c:do_task_stat()

Ok, I doubt anybody cares. I think that thing is entirely historical, and we probably should never have done it in the first place. I wonder if we should just replace that field with a zero.

We've moved flag bits around in task->flags (and moved them to thread->flags) before, so that /proc/<pid>/stat field has not been stable historically either. I can't imagine what could break - it would have broken many times before too.

Linus

maintainer
#4 lore raw

Re: [RFC][PATCH 2/2] kernel: extract thread types from task_struct::flags

In reply to Peter Zijlstra
Quoted 5 lines · On Mon, 20 Sep 2010 17:13:36 +0200 Peter Zijlstra <[email protected]>…

Free up a few more PF_flags by moving thread types out to their own variable.

Initially I compressed the types into less bits inside task_struct::flags, but Thomas suggested I move them to their own field.

There doesn't seem to be a huge point to all this, but I guess there's some sense in separating "what type of thread this is" from "attributes of this thread". Maybe. At the expense of a larger task_struct.

Quoted 3 lines

+/*
+ * Types >= tt_kernel imply the old PF_KTHREAD
+ */

Perhaps that should be encapsulated into another helper function

kernel/fork.c struct mm_struct *get_task_mm(struct tas · line 573

Quoted 8 lines
kernel/fork.c +1 -1
@@ -570,7 +570,7 @@ struct mm_struct *get_task_mm(struct tas
 	task_lock(task);
 	mm = task->mm;
 	if (mm) {
-		if (task->flags & PF_KTHREAD)
+		if (task_type(task) >= tt_kernel)

rather than open-coded everywhere.

maintainer
#5 lore raw

Re: [RFC][PATCH 2/2] kernel: extract thread types from task_struct::flags

Quoted 5 lines · Andrew Morton
Quoted 2 lines · On Mon, 20 Sep 2010 17:13:36 +0200 Peter Zijlstra <[email protected]>…

Free up a few more PF_flags by moving thread types out to their own variable.

Quoted 7 lines
Quoted 3 lines

Initially I compressed the types into less bits inside task_struct::flags, but Thomas suggested I move them to their own field.

There doesn't seem to be a huge point to all this, but I guess there's some sense in separating "what type of thread this is" from "attributes of this thread". Maybe. At the expense of a larger task_struct.

Yeah, with the PF_FLUSHER and PF_ALIGNWARN things removed we again have 2 bits free in flags.

And that's the amount -rt consumes (it adds 2 more: I'm a $FOO thread, flags).

But having looked at it, I came up with the compress I'm a $FOO thread, idea. Which would free up a few more bits for future use.

At that time Thomas suggested I move it into a separate field, and then Linus said, don't compact the types, simply transfer the bits we have.

Anyway, it was all very much RFC.. and comments I got :-)

So can you agree with Linus' proposal of simply moving all type like bits over to a new word, or would you rather see the linear type field with an extra helper; possibly part of the existing ->flags field?

maintainer
#6 lore raw

Re: [RFC][PATCH 2/2] kernel: extract thread types from task_struct::flags

Quoted 3 lines · On Mon, 20 Sep 2010 21:25:16 +0200 Peter Zijlstra <[email protected]> wrote:

So can you agree with Linus' proposal of simply moving all type like bits over to a new word, or would you rather see the linear type field with an extra helper; possibly part of the existing ->flags field?

No strong feelings either way, really.

One thing we should keep an eye out for is the size/speed of code at the client callsites. Plucking a field of bits out of the flags word and then treating that as a scalar would generate quite a bit of code, I expect. Let's not do that.