Skip to contentSkip to conversation

move task_struct allocation to arch

PATCH In review David Howells 4 participants 20 messages Feb 14, 2002 – Feb 15, 2002

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

PATCH 1/1 move task_struct allocation to arch

Hi Linus,

This patch moves task_struct allocation from kernel/fork.c into arch/*/kernel/process.c.

Quoted 8 lines · David Mosberger

David.H> What might be worth doing is to move the task_struct slab David.H> cache and (de-)allocator out of fork.c and to stick it in David.H> the arch somewhere. Then archs aren't bound to have the two David.H> separate. So for a system that can handle lots of memory, David.H> you can allocate the thread_info, task_struct and David.H> supervisor stack all on one very large chunk if you so David.H> wish.

Quoted 5 lines

Could you do this? I'd prefer if task_info could be completely hidden inside the x86/sparc arch-specific code, but if things are set up such that we at least have the option to keep the stack, task_info, and task_struct in a single chunk of memory (and without pointers between them), I'd have much less of an issue with it.

David

arch/i386/kernel/process.c +44 -1
diff -uNr linux-2.5.5-pre1/arch/i386/kernel/process.c linux-tib-255p1/arch/i386/kernel/process.c
@@ -54,6 +54,8 @@
 
 int hlt_counter;
 
+static kmem_cache_t *task_struct_cachep;
+
 /*
  * Return saved PC of a blocked thread.
  */
@@ -522,8 +524,49 @@
 }
 
 /*
- * Free current thread data structures etc..
+ * allocate and free task and thread data structures etc..
  */
+void __init init_task_struct_cache(void)
+{
+	/* create a slab on which task_structs can be allocated */
+	task_struct_cachep =
+		kmem_cache_create("task_struct",
+				  sizeof(struct task_struct),0,
+				  SLAB_HWCACHE_ALIGN, NULL, NULL);
+	if (!task_struct_cachep)
+		panic("fork_init(): cannot create task_struct SLAB cache");
+
+}
+
+struct task_struct *dup_task_struct(struct task_struct *orig)
+{
+	struct task_struct *tsk;
+	struct thread_info *ti;
+
+	ti = alloc_thread_info();
+	if (!ti) return NULL;
+
+	tsk = kmem_cache_alloc(task_struct_cachep,GFP_ATOMIC);
+	if (!tsk) {
+		free_thread_info(ti);
+		return NULL;
+	}
+
+	*ti = *orig->thread_info;
+	*tsk = *orig;
+	tsk->thread_info = ti;
+	ti->task = tsk;
+	atomic_set(&tsk->usage,1);
+
+	return tsk;
+}
+
+void __put_task_struct(struct task_struct *tsk)
+{
+	free_thread_info(tsk->thread_info);
+	kmem_cache_free(task_struct_cachep,tsk);
+}
+
 void exit_thread(void)
 {
 	/* nothing to do ... */
arch/sparc64/kernel/process.c +46 -1

59 lines not on this page · raw message

include/asm-i386/processor.h +5 -0
diff -uNr linux-2.5.5-pre1/include/asm-i386/processor.h linux-tib-255p1/include/asm-i386/processor.h
@@ -425,6 +425,11 @@
 struct task_struct;
 struct mm_struct;
 
+/* task structure allocation */
+extern void init_task_struct_cache(void);
+extern struct task_struct *dup_task_struct(struct task_struct *orig);
+extern void __put_task_struct(struct task_struct *tsk);
+
 /* Free all resources held by a thread. */
 extern void release_thread(struct task_struct *);
 /*
include/asm-sparc64/processor.h +5 -0
diff -uNr linux-2.5.5-pre1/include/asm-sparc64/processor.h linux-tib-255p1/include/asm-sparc64/processor.h
@@ -175,6 +175,11 @@
 #define copy_segments(tsk, mm)		do { } while (0)
 #define release_segments(mm)		do { } while (0)
 
+/* task structure allocation */
+extern void init_task_struct_cache(void);
+extern struct task_struct *dup_task_struct(struct task_struct *orig);
+extern void __put_task_struct(struct task_struct *tsk);
+
 #define get_wchan(__TSK) \
 ({	extern void scheduling_functions_start_here(void); \
 	extern void scheduling_functions_end_here(void); \
kernel/fork.c +1 -38
diff -uNr linux-2.5.5-pre1/kernel/fork.c linux-tib-255p1/kernel/fork.c
@@ -30,8 +30,6 @@
 #include <asm/uaccess.h>
 #include <asm/mmu_context.h>
 
-static kmem_cache_t *task_struct_cachep;
-
 /* The idle threads do not count.. */
 int nr_threads;
 
@@ -74,13 +72,7 @@
 
 void __init fork_init(unsigned long mempages)
 {
-	/* create a slab on which task_structs can be allocated */
-	task_struct_cachep =
-		kmem_cache_create("task_struct",
-				  sizeof(struct task_struct),0,
-				  SLAB_HWCACHE_ALIGN, NULL, NULL);
-	if (!task_struct_cachep)
-		panic("fork_init(): cannot create task_struct SLAB cache");
+	init_task_struct_cache();
 
 	/*
 	 * The default maximum number of threads is set to a safe
@@ -93,35 +85,6 @@
 	init_task.rlim[RLIMIT_NPROC].rlim_max = max_threads/2;
 }
 
-struct task_struct *dup_task_struct(struct task_struct *orig)
-{
-	struct task_struct *tsk;
-	struct thread_info *ti;
-
-	ti = alloc_thread_info();
-	if (!ti) return NULL;
-
-	tsk = kmem_cache_alloc(task_struct_cachep,GFP_ATOMIC);
-	if (!tsk) {
-		free_thread_info(ti);
-		return NULL;
-	}
-
-	*ti = *orig->thread_info;
-	*tsk = *orig;
-	tsk->thread_info = ti;
-	ti->task = tsk;
-	atomic_set(&tsk->usage,1);
-
-	return tsk;
-}
-
-void __put_task_struct(struct task_struct *tsk)
-{
-	free_thread_info(tsk->thread_info);
-	kmem_cache_free(task_struct_cachep,tsk);
-}
-
 /* Protects next_safe and last_pid. */
 spinlock_t lastpid_lock = SPIN_LOCK_UNLOCKED;
 
#1 lore raw
Quoted 21 lines · David Howells

Hi Linus,

This patch moves task_struct allocation from kernel/fork.c into arch/*/kernel/process.c.

Quoted 13 lines · David Mosberger

David.H> What might be worth doing is to move the task_struct slab David.H> cache and (de-)allocator out of fork.c and to stick it in David.H> the arch somewhere. Then archs aren't bound to have the two David.H> separate. So for a system that can handle lots of memory, David.H> you can allocate the thread_info, task_struct and David.H> supervisor stack all on one very large chunk if you so David.H> wish.

Could you do this? I'd prefer if task_info could be completely hidden inside the x86/sparc arch-specific code, but if things are set up such that we at least have the option to keep the stack, task_info, and task_struct in a single chunk of memory (and without pointers between them), I'd have much less of an issue with it.

Is this the first in a multi-step patch series, or something like that?

You just duplicated code in a generic location and pasted it into the arch. Where's the gain in that? I do see the gain in letting the arch allocate the task struct, but surely your patch should provide a generic mechanism for an arch to call by default, instead of duplicating code??

Jeff

Signature · 3 lines
Jeff Garzik      | "I went through my candy like hot oatmeal
Building 1024    |  through an internally-buttered weasel."
MandrakeSoft     |             - goats.com
#2 lore raw
Quoted 2 lines

Is this the first in a multi-step patch series, or something like that?

What makes you ask that?

Quoted 4 lines

You just duplicated code in a generic location and pasted it into the arch. Where's the gain in that? I do see the gain in letting the arch allocate the task struct, but surely your patch should provide a generic mechanism for an arch to call by default, instead of duplicating code??

Hmmm... Is it worth going through all fun of creating another CONFIG_xxxx option to govern the inclusion of such code?

David

#3 lore raw
Quoted 3 lines · David Howells
Quoted 1 line

Is this the first in a multi-step patch series, or something like that?

What makes you ask that?

Because your patch just flat out duplicates code line for line into two arches.

Quoted 7 lines
Quoted 4 lines

You just duplicated code in a generic location and pasted it into the arch. Where's the gain in that? I do see the gain in letting the arch allocate the task struct, but surely your patch should provide a generic mechanism for an arch to call by default, instead of duplicating code??

Hmmm... Is it worth going through all fun of creating another CONFIG_xxxx option to govern the inclusion of such code?

I am wondering where you want to go with this, short term and long term. Is the implementation of this on other arches gonna look the same -- just line for line copy of code? With maybe ia64 as the lone exception?

Jeff

Signature · 3 lines
Jeff Garzik      | "I went through my candy like hot oatmeal
Building 1024    |  through an internally-buttered weasel."
MandrakeSoft     |             - goats.com
#4 lore raw
Quoted 7 lines · Jeff Garzik
Quoted 3 lines · David Howells
Quoted 1 line

Is this the first in a multi-step patch series, or something like that?

What makes you ask that?

Because your patch just flat out duplicates code line for line into two arches.

What I do to it next depends on the feedback I get back.

Quoted 4 lines

I am wondering where you want to go with this, short term and long term. Is the implementation of this on other arches gonna look the same -- just line for line copy of code? With maybe ia64 as the lone exception?

Ask Linus, he asked for the task_struct/thread_info split. Various people have complained about the two things being allocated separately (maintainers for m68k and ia64 archs certainly, and if I remember rightly, x86_64 as well, though I don't appear to have saved the message for that). However, DaveM (sparc64) appears to really be in favour of it.

In any case, this is just a corollary to my main aim of getting task ornaments included in the kernel. The userspace resumption notification was the true prerequisite, to support which I adjusted entry.S to make sure it didn't cost any extra clock cycles. This led to Linus requesting that everything that entry.S needs to access be separated out into another structure (so that entry.S never accesses task_struct).

David

#5 lore raw

Hi,

Quoted 5 lines · David Howells

Ask Linus, he asked for the task_struct/thread_info split. Various people have complained about the two things being allocated separately (maintainers for m68k and ia64 archs certainly, and if I remember rightly, x86_64 as well, though I don't appear to have saved the message for that). However, DaveM (sparc64) appears to really be in favour of it.

Ok, so we have the following allocation possibilities.
1. allocate task_struct+thread_info+stack together.
2. allocate task_struct and thread_info+stack.
3. allocate task_struct+thread_info and stack.

1. is what we have so far and I hope Linus actually means a task_struct/stack split. Linus, could you please clarify? Architectures without a thread register want either the first or second option. I don't really have an opinion, whether the first option should be made obsolete. For architectures with a thread register the third option makes most sense and is not really a problem, we only have to specify the dependency between task_struct and thread_info.

bye, Roman

#6 lore raw
In reply to David Howells
Quoted 5 lines · David Howells

Ask Linus, he asked for the task_struct/thread_info split. Various people have complained about the two things being allocated separately (maintainers for m68k and ia64 archs certainly, and if I remember rightly, x86_64 as well, though I don't appear to have saved the message for that). However, DaveM (sparc64) appears to really be in favour of it.

I'm not especially in favour of it for Alpha either.

As far as I can see all it's done is added a memory load on the fast path when a constant displacement folded into the address would previously have sufficed.

r~

#7 lore raw
In reply to David Howells
Quoted 11 lines · David Howells
Quoted 7 lines · Jeff Garzik
Quoted 3 lines · David Howells
Quoted 1 line

Is this the first in a multi-step patch series, or something like that?

What makes you ask that?

Because your patch just flat out duplicates code line for line into two arches.

What I do to it next depends on the feedback I get back.

Here's my feedback, then ;-)

Your patch would be ok IMHO if you additionally changed the arches to include task struct inside struct thread_info, getting things back down to a single allocation for thread_info+task_struct, with 'current' once again being a constant offset from the beginning of thread_info. [this might require resurrecting the old patches to move task struct definitions out of sched.h proper]

Basically, I think you are going in the right direction, but the implementation detail of flat out copying code was what caught my attention, and looks pretty bad to me. Two easy ways I can think of for providing generic code would be #include <asm-generic/foo.h>, or enabling generic code when feature macro HAVE_ARCH_FOO is not defined.

Quoted 6 lines
Quoted 4 lines

I am wondering where you want to go with this, short term and long term. Is the implementation of this on other arches gonna look the same -- just line for line copy of code? With maybe ia64 as the lone exception?

Ask Linus, he asked for the task_struct/thread_info split. Various people

[...]

Quoted 3 lines

any extra clock cycles. This led to Linus requesting that everything that entry.S needs to access be separated out into another structure (so that entry.S never accesses task_struct).

Seems a sane enough direction...

Jeff

Signature · 3 lines
Jeff Garzik      | "I went through my candy like hot oatmeal
Building 1024    |  through an internally-buttered weasel."
MandrakeSoft     |             - goats.com
#8 lore raw

Hi,

Quoted 5 lines · Jeff Garzik
Quoted 3 lines

any extra clock cycles. This led to Linus requesting that everything that entry.S needs to access be separated out into another structure (so that entry.S never accesses task_struct).

Seems a sane enough direction...

That wouldn't be a problem, if ia32 added the needed infrastructure to calculate the structure offsets. It's not really that difficult, look at the m68k implementation, which even gets dependencies right. AFAIK Keith also added the needed infra structure to his new build system.

bye, Roman

#9 lore raw
Quoted 5 lines

That wouldn't be a problem, if ia32 added the needed infrastructure to calculate the structure offsets. It's not really that difficult, look at the m68k implementation, which even gets dependencies right. AFAIK Keith also added the needed infra structure to his new build system.

But the offsets aren't fixed. The task structure does not lie adjacent to the thread_info structure.

David

#10 lore raw

Hi,

Quoted 5 lines · David Howells
Quoted 2 lines

That wouldn't be a problem, if ia32 added the needed infrastructure to calculate the structure offsets.

But the offsets aren't fixed. The task structure does not lie adjacent to the thread_info structure.

That's not the problem, I meant this sentence: "This led to Linus requesting that everything that entry.S needs to access be separated out into another structure." Splitting the task structure and the stack page is fine. Keeping the most important fields in the stack page is fine too, if the architecture requires it. But the decision what goes into thread_info, should not be made only to avoid access to task_struct from entry.S.

bye, Roman

#11 lore raw
Quoted 11 lines · Roman Zippel
Quoted 5 lines
Quoted 2 lines

That wouldn't be a problem, if ia32 added the needed infrastructure to calculate the structure offsets.

But the offsets aren't fixed. The task structure does not lie adjacent to the thread_info structure.

That's not the problem, I meant this sentence: "This led to Linus requesting that everything that entry.S needs to access be separated out into another structure." Splitting the task structure and the stack page is fine. Keeping the most important fields in the stack page is fine too, if the architecture requires it.

I think I see what you meant.

However, I'm not particularly keen on the M68K offset generator. I'm not sure why exactly, but it just doesn't seem the right way to do things... OTOH, without making the assembler able to parse C structs, I'm not sure there is a "right" way:-/

Quoted 2 lines

But the decision what goes into thread_info, should not be made only to avoid access to task_struct from entry.S.

How about I quote Linus (who explained what he wanted better than I did).

Firstly, in response to me having supplied a patch that made a set of four byte-size values as the status area in the task_struct:

| For the future, the biggest thing I'd like to see is actually to make
| "work" be a bitmap, because the "bytes are atomic" approach simply isn't
| portable anyway, so we might as well make things _explicitly_ atomic and
| use bit operations. Otherwise the alpha version of "work" would have to be
| four bytes per "bit" of information, which sounds really excessive.

And then after some discussion:

| In particular, there's been all that discussion about cache-coloring the
| "struct task_struct", and my personal suggestion for that whole can of
| worms is to have the "struct low_level" be in the one low cache-line, and
| make it contain a pointer to "struct task_struct" - and just split the two
| up completely. Then the low-level asm code would never have to even look
| at "task_struct", it would only look at this stuff.

(struct low_level became thread_info).

David

#12 lore raw

Hi,

Quoted 8 lines · David Howells

Firstly, in response to me having supplied a patch that made a set of four byte-size values as the status area in the task_struct:

| For the future, the biggest thing I'd like to see is actually to make
| "work" be a bitmap, because the "bytes are atomic" approach simply isn't
| portable anyway, so we might as well make things _explicitly_ atomic and
| use bit operations. Otherwise the alpha version of "work" would have to be
| four bytes per "bit" of information, which sounds really excessive.

As I mentioned before I more like the byte approach, since atomic bit field handling is quite expensive on most architectures, where a simple set/clear byte is only one or two instructions, if there is byte load/store instruction. So I'd really like to see to leave the decision to the architecture, whether to use bit or byte fields.

Quoted 10 lines

And then after some discussion:

| In particular, there's been all that discussion about cache-coloring the
| "struct task_struct", and my personal suggestion for that whole can of
| worms is to have the "struct low_level" be in the one low cache-line, and
| make it contain a pointer to "struct task_struct" - and just split the two
| up completely. Then the low-level asm code would never have to even look
| at "task_struct", it would only look at this stuff.

(struct low_level became thread_info).

That I can agree with. :)

bye, Roman

#13 lore raw
Quoted 14 lines · Roman Zippel

Hi,

Quoted 8 lines · David Howells

Firstly, in response to me having supplied a patch that made a set of four byte-size values as the status area in the task_struct:

| For the future, the biggest thing I'd like to see is actually to make
| "work" be a bitmap, because the "bytes are atomic" approach simply isn't
| portable anyway, so we might as well make things _explicitly_ atomic and
| use bit operations. Otherwise the alpha version of "work" would have to be
| four bytes per "bit" of information, which sounds really excessive.
Quoted 5 lines

As I mentioned before I more like the byte approach, since atomic bit field handling is quite expensive on most architectures, where a simple set/clear byte is only one or two instructions, if there is byte load/store instruction. So I'd really like to see to leave the decision to the architecture, whether to use bit or byte fields.

We have tons of code already using atomic test_and_set_bit type stuff... why not just make sure your bit set/clear stuff is fast? :)

Jeff

Signature · 3 lines
Jeff Garzik      | "I went through my candy like hot oatmeal
Building 1024    |  through an internally-buttered weasel."
MandrakeSoft     |             - goats.com
#14 lore raw

Hi,

Quoted 8 lines · Jeff Garzik
Quoted 5 lines

As I mentioned before I more like the byte approach, since atomic bit field handling is quite expensive on most architectures, where a simple set/clear byte is only one or two instructions, if there is byte load/store instruction. So I'd really like to see to leave the decision to the architecture, whether to use bit or byte fields.

We have tons of code already using atomic test_and_set_bit type stuff... why not just make sure your bit set/clear stuff is fast? :)

Because in this case there is no atomic test_and_(clear|set)_bit needed. We only need to clear the bit/byte before scheduling/signal delivery is started.

bye, Roman

#15 lore raw
In reply to Roman Zippel

Hi Linus,

Quoted 5 lines · Roman Zippel

As I mentioned before I more like the byte approach, since atomic bit field handling is quite expensive on most architectures, where a simple set/clear byte is only one or two instructions, if there is byte load/store instruction. So I'd really like to see to leave the decision to the architecture, whether to use bit or byte fields.

Should I move the convenience bit operations back to the arch header, so that the M68K guys can have byte-sized flags (which they can use TAS/TST on)?

David

#16 lore raw

Hi,

Quoted 2 lines · David Howells

Should I move the convenience bit operations back to the arch header, so that the M68K guys can have byte-sized flags (which they can use TAS/TST on)?

No tas needed, just byte store, so it's not only for m68k, ppc would benefit from it too and probably some more.

bye, Roman

#17 lore raw
In reply to David Howells
Quoted 2 lines · David Howells

Should I move the convenience bit operations back to the arch header, so that the M68K guys can have byte-sized flags (which they can use TAS/TST on)?

If you provide some way to override the convenience bit operations, then I suspect that you can let most folks use the byte ops. It's only older alphas that would absolutely have to play games.

Of course, I now have 9 flag bits defined, so I might not go back to bytes anyway...

r~

#18 lore raw
In reply to David Howells
Quoted 10 lines · David Howells

And then after some discussion:

| In particular, there's been all that discussion about cache-coloring the
| "struct task_struct", and my personal suggestion for that whole can of
| worms is to have the "struct low_level" be in the one low cache-line, and
| make it contain a pointer to "struct task_struct" - and just split the two
| up completely. Then the low-level asm code would never have to even look
| at "task_struct", it would only look at this stuff.

(struct low_level became thread_info).

I see. Yes, that makes sense. My mistake was following davem's lead and pulling everything out of struct thread_struct. In reality, I shouldn't have moved hardly anything and $8 should still point to current.

r~

#19 lore raw
In reply to David Howells
Quoted 6 lines · Jeff Garzik

Your patch would be ok IMHO if you additionally changed the arches to include task struct inside struct thread_info, getting things back down to a single allocation for thread_info+task_struct, with 'current' once again being a constant offset from the beginning of thread_info. [this might require resurrecting the old patches to move task struct definitions out of sched.h proper]

Actually, you don't really need the definition of task_struct, if include dependencies get really hairy... you really only need the size of the task struct.

Jeff

Signature · 3 lines
Jeff Garzik      | "I went through my candy like hot oatmeal
Building 1024    |  through an internally-buttered weasel."
MandrakeSoft     |             - goats.com