Skip to contentSkip to conversation

Add module_kernel_thread for threads that live in modules.

PATCH Rusty Russell 4 participants 10 messages Jun 16, 2003 – Jun 17, 2003

lore
Share & tools
lore
#0 lore raw

Hi Neil,

There are several problems with this patch. Ignoring the fact
that you use __module_get. Firstly, you bump the module count permentantly while the thread is running: how does it ever get unloaded? Secondly, modprobe becomes your parent.

There have been ambitious attempts to do a nice "thread
creation and stopping" interface before. Given the delicate logic involved in shutting threads down, I think this makes sense. Maybe something like:

/* Struct which identifies a kernel thread, handed to creator and
thread. */
struct kthread

{
	int pid;
	int should_die; /* Thread should exit when this is set. */

	/* User supplied arg... */
	void *arg;
};

struct kthread *create_thread(int (*fn)(struct kthread*), void *arg,

			      unsigned long flags,
			      const char *namefmt, ...);

void cleanup_thread(struct kthread *);

create_thread would use keventd to start the thread, and stop_thread would tell keventd to set should_die, wmb(), wake it up, and sys_wait() for it.

Thoughts?
Rusty.
--
Anyone who quotes me in their sig is an idiot. -- Rusty Russell.

#1 lore raw
Quoted 19 lines · Rusty Russell

There have been ambitious attempts to do a nice "thread
creation and stopping" interface before. Given the delicate logic involved in shutting threads down, I think this makes sense. Maybe something like:

/* Struct which identifies a kernel thread, handed to creator and
thread. */
struct kthread

{
	int pid;
	int should_die; /* Thread should exit when this is set. */

	/* User supplied arg... */
	void *arg;
};

struct kthread *create_thread(int (*fn)(struct kthread*), void *arg,

			      unsigned long flags,
			      const char *namefmt, ...);
Quoted 8 lines

void cleanup_thread(struct kthread *);

create_thread would use keventd to start the thread, and stop_thread would tell keventd to set should_die, wmb(), wake it up, and sys_wait() for it.

Thoughts?
Rusty.

Why using keventd? Personally I'd prefer a synchronous thread start/stop, particularly with the thread living in a module.
Maybe some generalisation of:

static DECLARE_WAIT_QUEUE_HEAD(wq_kthread);
static struct completion kthread_died;
static int should_die;

static int my_kthread(void *started)

{
	daemonize("my_kthread");

	complete((struct completion *)started);

	while (!should_die) {
		/* sleep for wq_kthread and do requested stuff */
	}

	complete_and_exit(&kthread_died, 0);
	/* never reached */
	return 0;
}

int my_kthread_create(...)

{
	DECLARE_COMPLETION(started);
	int pid;

	should_die = 0;
	init_completion(kthread_died);
	pid = kernel_thread(my_kthread, &startup, CLONE_FS|CLONE_FILES);
	if (pid <= 0)
		return -EAGAIN;

	wait_for_completion(&started);
	return pid;
}

void my_kthread_join(...)

{
	should_die = 1;	
	wake_up(&wq_kthread);
	wait_for_completion(&kthread_died);
}

Assuming the create/join things are called from module init/exit path this eliminates the need to bump the module refcnt.

To make this more generic I think it would be sufficient to move the start/exit completions into your struct kthread.

Martin

#2 lore raw
Quoted 9 lines · Martin Diehl
Quoted 7 lines

create_thread would use keventd to start the thread, and stop_thread would tell keventd to set should_die, wmb(), wake it up, and sys_wait() for it.

Thoughts?
Rusty.

Why using keventd?

keventd knows how to clean up children, handle SIGCHLD, etc. That code was hard-won.

And kernel threads which are parented by userspace processes tend to accidentally inherit things we'd rather they didn't. daemonize() and reparent_to_init() try to fix things up, but I'm still not sure we got it all.

Using keventd will tend to prevent mistakes.

#3 lore raw
In reply to Martin Diehl

In message <[email protected]> you write:

Quoted 3 lines

Why using keventd? Personally I'd prefer a synchronous thread start/stop, particularly with the thread living in a module.
Maybe some generalisation of:

It would be syncronous: but doing kernel_thread yourself means trying to clean up using daemonize et al, which is incomplete and always makes me nervous.

An implementation detail to users, but IMHO an important one.

Also, this replaces complete_and_exit: the thread can just exit. This simplifies things for the users, too...

Cheers,
Rusty.
--
Anyone who quotes me in their sig is an idiot. -- Rusty Russell.

#4 lore raw
Quoted 6 lines · Rusty Russell

In message <[email protected]> you write:

Quoted 3 lines

Why using keventd? Personally I'd prefer a synchronous thread start/stop, particularly with the thread living in a module.
Maybe some generalisation of:

It would be syncronous:

You mean your cleanup_thread would block for completion of the keventd stuff? Ok, this would work. But then, when calling cleanup_thread, f.e. we must not hold any semaphore which might be acquired by _any_ other work scheduled for keventd or we might end in deadlock (like the rtnl+hotplug issue we had seen recently).

Quoted 3 lines

but doing kernel_thread yourself means trying
to clean up using daemonize et al, which is incomplete and always makes me nervous.

I thought this was fixed in 2.5 for some time now, but seems I shouldn't rely on that ;-)

Quoted 4 lines

An implementation detail to users, but IMHO an important one.

Also, this replaces complete_and_exit: the thread can just exit. This simplifies things for the users, too...

Personally I do like the complete_and_exit thing as a simple and clear finalisation point. And if I didn't miss something above wrt. your cleanup_kthread being synchronous I'm not sure whether the locking implication do really make things easier - YMMV of course.

Thanks.
Martin

#5 lore raw

In message <[email protected]> you write:

Quoted 8 lines · Rusty Russell
Quoted 1 line

It would be syncronous:

You mean your cleanup_thread would block for completion of the keventd stuff? Ok, this would work. But then, when calling cleanup_thread, f.e. we must not hold any semaphore which might be acquired by _any_ other work scheduled for keventd or we might end in deadlock (like the rtnl+hotplug issue we had seen recently).

I think we're talking across each other: take a look at the existing kernel/kmod.c __call_usermodehelper to see how we wait at the moment.

Quoted 5 lines
Quoted 2 lines

Also, this replaces complete_and_exit: the thread can just exit. This simplifies things for the users, too...

Personally I do like the complete_and_exit thing as a simple and clear finalisation point.

Not as clean as "wait until the thread has exited", surely!

Cheers,
Rusty.
--
Anyone who quotes me in their sig is an idiot. -- Rusty Russell.

#6 lore raw
Quoted 8 lines · Rusty Russell
Quoted 5 lines

You mean your cleanup_thread would block for completion of the keventd stuff? Ok, this would work. But then, when calling cleanup_thread, f.e. we must not hold any semaphore which might be acquired by _any_ other work scheduled for keventd or we might end in deadlock (like the rtnl+hotplug issue we had seen recently).

I think we're talking across each other: take a look at the existing kernel/kmod.c __call_usermodehelper to see how we wait at the moment.

Maybe talking across each other... what I meant is some deadlock like this (IMHO possible both on UP and SMP):

rmmod (f.e.) keventd somewhere else

down(&some_sem)
cleanup_thread()

	.
	.						schedule_work(w1)
	.
	.		w1 (queued, or maybe running):
	.			down(&some_sem);
	.			...
	.			up(&some_sem);
	.
schedule_work(w2)
	.
	.		w2 (queued behind w1)
	.			should_die = 1
	.			sys_wait4()
	.			complete(thread_exit)
	.

/* some_sem still hold */
wait_for_completion(thread_exit)

Next time we schedule keventd w1 will be executed first which wants to acquire some_sem which is still hold by the rmmod-thread - which in turn blocks for completion of w2 which is queued behind w1 -> deadlock.

The point is the queueing in keventd combined with stuff waiting for keventd-completion could create some possibilities for lock order violation which are at least not very obvious.

IMHO cleanup_thread would be something one MUST NOT call with any lock hold, not even a semaphore if it might get acquired anywhere else in keventd-context.

Thanks.
Martin

#7 lore raw

In message <[email protected]> you write:

Quoted 3 lines

IMHO cleanup_thread would be something one MUST NOT call with any lock hold, not even a semaphore if it might get acquired anywhere else in keventd-context.

Err, yes. I thought this was fairly clear. cf. del_timer_sync and a timer function.

Cheers,
Rusty.
--
Anyone who quotes me in their sig is an idiot. -- Rusty Russell.

#8 lore raw
In reply to Rusty Russell
Quoted 1 line · On Monday June 16, [email protected] wrote:

Hi Neil,

Hi Rusty. Thanks for the comments... I probably should have Cc:ed you in the first place....

Quoted 5 lines

There are several problems with this patch. Ignoring the fact
that you use __module_get. Firstly, you bump the module count permentantly while the thread is running: how does it ever get unloaded? Secondly, modprobe becomes your parent.

We seem to have very different views of the problem, as you seem to be calling into question aspects that I thought were obviously correct.

__module_get:
In all the cases I am interested in (nfsd, lockd, lockd-helper-thread), the thread is started by code running inside the module and so there will be a reference held on the module while the thread is being started, thus __module_get is the correct thing to do as "we know we already have a refcount"...

module count bumped permananelt while thread is running:
well ofcourse, the thread runs code in the module which can only be done safely while we have a ref-count.
how does it ever get unloaded:
the thread exits, the refcount drops, and the module can be unloaded.
In the case of nfsd, the threads get signalled and die gracefully. For lockd, the number of users (nfs mounts or nfsd threads) drops to zero, the process is signalled, and exits gracefully. For the lockd helper threads, the complete there assigned task and exit.
The threads I am thinking of aren't running "whenever the module is loaded". They are running "whenever their service is needed".
modprobe becomes your parent:
No, modprobe has nothing to do with it in my case. rpc.nfsd, or mount_nfs or lockd might be the parent. I thought reparent_to_init handled all that. Apparently there are question marks over that which I wasn't aware of.

Quoted 20 lines

There have been ambitious attempts to do a nice "thread
creation and stopping" interface before. Given the delicate logic involved in shutting threads down, I think this makes sense. Maybe something like:

/* Struct which identifies a kernel thread, handed to creator and
thread. */
struct kthread

{
	int pid;
	int should_die; /* Thread should exit when this is set. */

	/* User supplied arg... */
	void *arg;
};

struct kthread *create_thread(int (*fn)(struct kthread*), void *arg,

			      unsigned long flags,
			      const char *namefmt, ...);
Quoted 7 lines

void cleanup_thread(struct kthread *);

create_thread would use keventd to start the thread, and stop_thread would tell keventd to set should_die, wmb(), wake it up, and sys_wait() for it.

Thoughts?

I don't want to have to call "cleanup_thread" or de-allocate the "struct kthread". I want to be able to SIGKILL a process and have it go away and release everything, including possibly the last refernce to the module.

In short, it really feels like we are trying to solve different problems :-)

I will have a look at keventd and see if it's services can be of assistance to solve my problem.

Thanks,
NeilBrown

#9 lore raw

In message <[email protected]> you write:

Quoted 5 lines · On Monday June 16, [email protected] wrote:
Quoted 1 line

Hi Neil,

Hi Rusty. Thanks for the comments... I probably should have Cc:ed you in the first place....

Yeah, that does tend to get faster response, but I know some hackers consider it completely optional 8(

Quoted 8 lines
Quoted 4 lines

There are several problems with this patch. Ignoring the fact
that you use __module_get. Firstly, you bump the module count permentantly while the thread is running: how does it ever get unloaded? Secondly, modprobe becomes your parent.

We seem to have very different views of the problem, as you seem to be calling into question aspects that I thought were obviously correct.

Quoted 6 lines

__module_get:
In all the cases I am interested in (nfsd, lockd, lockd-helper-thread), the thread is started by code running inside the module and so there will be a reference held on the module while the thread is being started, thus __module_get is the correct thing to do as "we know we already have a refcount"...

But do you wait for it? Theoretically the init code could have finished, and someone done rmmod, before this thread gets as far as __module_get, no?

Quoted 3 lines

module count bumped permananelt while thread is running:
well ofcourse, the thread runs code in the module which can only be done safely while we have a ref-count.

For future reference: this isn't quite true. If a function/thread is synchronously stopped by the exit code/failed init code then they don't need to hold a reference count: it still *can* hold a reference count, which really depends on whether the module should be considered "in use" by the thread... cf. timers.

Quoted 2 lines

The threads I am thinking of aren't running "whenever the module is loaded". They are running "whenever their service is needed".

My bad: I wasn't sure given my (admittedly brief) glance at the code. Thanks for clarifing!

Quoted 5 lines

modprobe becomes your parent:
No, modprobe has nothing to do with it in my case. rpc.nfsd, or mount_nfs or lockd might be the parent. I thought reparent_to_init handled all that. Apparently there are question marks over that which I wasn't aware of.

Andrew has been trying to kill it, and I think he's right. In practical terms, it's much easier to start from a clean environment than to clean up an unknown one, and keep that cleanup code uptodate.

Quoted 7 lines

I don't want to have to call "cleanup_thread" or de-allocate the "struct kthread". I want to be able to SIGKILL a process and have it go away and release everything, including possibly the last refernce to the module.

In short, it really feels like we are trying to solve different problems :-)

Agreed: threads under their own control are much simpler than ones under external control.

Quoted 2 lines

I will have a look at keventd and see if it's services can be of assistance to solve my problem.

I will think, which usually seems to help me when presented with new information 8)

Thanks!
Rusty.
--
Anyone who quotes me in their sig is an idiot. -- Rusty Russell.