Skip to content

Commit

Permalink
exit: Implement kthread_exit
Browse files Browse the repository at this point in the history
The way the per task_struct exit_code is used by kernel threads is not
quite compatible how it is used by userspace applications.  The low
byte of the userspace exit_code value encodes the exit signal.  While
kthreads just use the value as an int holding ordinary kernel function
exit status like -EPERM.

Add kthread_exit to clearly separate the two kinds of uses.

Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com>
  • Loading branch information
Eric W. Biederman committed Dec 13, 2021
1 parent eb55e71 commit bbda86e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
1 change: 1 addition & 0 deletions include/linux/kthread.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ void *kthread_probe_data(struct task_struct *k);
int kthread_park(struct task_struct *k);
void kthread_unpark(struct task_struct *k);
void kthread_parkme(void);
void kthread_exit(long result) __noreturn;

int kthreadd(void *unused);
extern struct task_struct *kthreadd_task;
Expand Down
23 changes: 19 additions & 4 deletions kernel/kthread.c
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,21 @@ void kthread_parkme(void)
}
EXPORT_SYMBOL_GPL(kthread_parkme);

/**
* kthread_exit - Cause the current kthread return @result to kthread_stop().
* @result: The integer value to return to kthread_stop().
*
* While kthread_exit can be called directly, it exists so that
* functions which do some additional work in non-modular code such as
* module_put_and_kthread_exit can be implemented.
*
* Does not return.
*/
void __noreturn kthread_exit(long result)
{
do_exit(result);
}

static int kthread(void *_create)
{
static const struct sched_param param = { .sched_priority = 0 };
Expand All @@ -286,13 +301,13 @@ static int kthread(void *_create)
done = xchg(&create->done, NULL);
if (!done) {
kfree(create);
do_exit(-EINTR);
kthread_exit(-EINTR);
}

if (!self) {
create->result = ERR_PTR(-ENOMEM);
complete(done);
do_exit(-ENOMEM);
kthread_exit(-ENOMEM);
}

self->threadfn = threadfn;
Expand Down Expand Up @@ -326,7 +341,7 @@ static int kthread(void *_create)
__kthread_parkme(self);
ret = threadfn(data);
}
do_exit(ret);
kthread_exit(ret);
}

/* called from kernel_clone() to get node information for about to be created task */
Expand Down Expand Up @@ -627,7 +642,7 @@ EXPORT_SYMBOL_GPL(kthread_park);
* instead of calling wake_up_process(): the thread will exit without
* calling threadfn().
*
* If threadfn() may call do_exit() itself, the caller must ensure
* If threadfn() may call kthread_exit() itself, the caller must ensure
* task_struct can't go away.
*
* Returns the result of threadfn(), or %-EINTR if wake_up_process()
Expand Down
1 change: 1 addition & 0 deletions tools/objtool/check.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ static bool __dead_end_function(struct objtool_file *file, struct symbol *func,
"panic",
"do_exit",
"do_task_dead",
"kthread_exit",
"make_task_dead",
"__module_put_and_exit",
"complete_and_exit",
Expand Down

0 comments on commit bbda86e

Please sign in to comment.