signal: refactor some functions
v3 cover signal: refactor some functions
Hey,
This series refactors a bunch of functions in signal.c to simplify parts of the code.
As requested, v3 drops all 3 commits that were pure coding-style changes.
The greatest single change is declaring the static do_sigpending() helper as void which makes it possible to remove a bunch of unnecessary checks in the syscalls later on.
Thanks!
Christian
Christian Brauner (17):
signal: make force_sigsegv() void
signal: make kill_as_cred_perm() return bool
signal: make may_ptrace_stop() return bool
signal: make do_sigpending() void
signal: simplify rt_sigaction()
signal: make kill_ok_by_cred() return bool
signal: make sig_handler_ignored() return bool
signal: make sig_task_ignored() return bool
signal: make sig_ignored() return bool
signal: make has_pending_signals() return bool
signal: make recalc_sigpending_tsk() return bool
signal: make unhandled_signal() return bool
signal: make flush_sigqueue_mask() void
signal: make wants_signal() return bool
signal: make legacy_queue() return bool
signal: make sigkill_pending() return bool
signal: make get_signal() return bool
include/linux/sched/signal.h | 2 +- include/linux/signal.h | 4 +- kernel/signal.c | 170 +++++++++++++++++------------------ 3 files changed, 86 insertions(+), 90 deletions(-)
Signature · 1 line
2.17.0
PATCH v3 1/17 signal: make force_sigsegv() void
force_sigsegv() returned 0 unconditionally so it doesn't make sense to have it return at all. In addition, there are no callers that check force_sigsegv()'s return value.
---v2->v3:
* unchangedv1->v2:
* unchangedv0->v1:
* unchanged
--- include/linux/sched/signal.h | 2 +- kernel/signal.c | 7 ++----- 2 files changed, 3 insertions(+), 6 deletions(-)
include/linux/sched/signal.h +1 -1
@@ -314,7 +314,7 @@ int force_sig_pkuerr(void __user *addr, u32 pkey);int force_sig_ptrace_errno_trap (int errno,void __user *addr);extern int send_sig_info (int ,struct siginfo *,struct task_struct *); -extern int force_sigsegv (int ,struct task_struct *); +extern void force_sigsegv (int sig,struct task_struct *p);extern int force_sig_info (int ,struct siginfo *,struct task_struct *);extern int __kill_pgrp_info (int sig,struct siginfo *info,struct pid *pgrp);extern int kill_pid_info (int sig,struct siginfo *info,struct pid *pid);
kernel/signal.c +2 -5
@@ -1468,8 +1468,7 @@ send_sig(int sig, struct task_struct *p, int priv)return send_sig_info (sig,__si_special (priv), p); } -void -force_sig (int sig,struct task_struct *p) +void force_sig (int sig,struct task_struct *p) {force_sig_info (sig,SEND_SIG_PRIV , p); }
@@ -1480,8 +1479,7 @@ force_sig(int sig, struct task_struct *p)* the problem was already a SIGSEGV, we'll want to * make sure we don't even try to deliver the signal.. */ -int -force_sigsegv (int sig,struct task_struct *p) +void force_sigsegv (int sig,struct task_struct *p) {if (sig ==SIGSEGV ) {unsigned long flags;
@@ -1490,7 +1488,6 @@ force_sigsegv(int sig, struct task_struct *p)spin_unlock_irqrestore (&p->sighand->siglock, flags); }force_sig (SIGSEGV , p); -return 0 ; } int force_sig_fault(int sig, int code, void __user *addr
Signature · 1 line
2.17.0
PATCH v3 2/17 signal: make kill_as_cred_perm() return bool
In reply to Christian Braunerkill_as_cred_perm() already behaves like a boolean function. Let's actually declare it as such too.
---v2->v3:
* unchangedv1->v2:
* unchangedv0->v1:
* simplify to use a single return
--- kernel/signal.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-)
kernel/signal.c +7 -6
@@ -1349,14 +1349,15 @@ static int kill_proc_info(int sig, struct siginfo *info, pid_t pid)return error; } -static int kill_as_cred_perm (const struct cred *cred, -struct task_struct *target) +static inline bool kill_as_cred_perm (const struct cred *cred, +struct task_struct *target) {const struct cred *pcred =__task_cred (target); -if (!uid_eq (cred->euid, pcred->suid) && !uid_eq (cred->euid, pcred->uid) && - !uid_eq (cred->uid, pcred->suid) && !uid_eq (cred->uid, pcred->uid)) -return 0 ; -return 1 ; + +return uid_eq (cred->euid, pcred->suid) || +uid_eq (cred->euid, pcred->uid) || +uid_eq (cred->uid, pcred->suid) || +uid_eq (cred->uid, pcred->uid); }/* like kill_pid_info(), but doesn't use uid/euid of "current" */
Signature · 1 line
2.17.0
PATCH v3 3/17 signal: make may_ptrace_stop() return bool
In reply to Christian Braunermay_ptrace_stop() already behaves like a boolean function. Let's actually declare it as such too.
---v2->v3:
* [Oleg Nestorov] s/likely/unlikely logicv1->v2:
* unchangedv0->v1:
* unchanged
--- kernel/signal.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
kernel/signal.c +4 -4
@@ -1889,10 +1889,10 @@ static void do_notify_parent_cldstop(struct task_struct *tsk,spin_unlock_irqrestore (&sighand->siglock, flags); } -static inline int may_ptrace_stop (void ) +static inline bool may_ptrace_stop (void ) {if (!likely (current->ptrace)) -return 0 ; +return false;/* * Are we in the middle of do_coredump? * If so and our tracer is also part of the coredump stopping
@@ -1908,9 +1908,9 @@ static inline int may_ptrace_stop(void)*/ if (unlikely (current->mm->core_state) &&unlikely (current->mm == current->parent->mm)) -return 0 ; +return false; -return 1 ; +return true; }/*
Signature · 1 line
2.17.0
PATCH v3 4/17 signal: make do_sigpending() void
In reply to Christian Braunerdo_sigpending() returned 0 unconditionally so it doesn't make sense to have it return at all. This allows us to simplify a bunch of syscall callers.
---v2->v3:
* added Oleg's Acked-byv1->v2:
* unchangedv0->v1:
* unchanged
--- kernel/signal.c | 41 ++++++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 21 deletions(-)
kernel/signal.c +20 -21
@@ -2767,7 +2767,7 @@ COMPAT_SYSCALL_DEFINE4(rt_sigprocmask, int, how, compat_sigset_t __user *, nset, }#endif -static int do_sigpending (sigset_t *set) +static void do_sigpending (sigset_t *set) {spin_lock_irq (¤t->sighand->siglock); sigorsets(set, ¤t->pending.signal,
@@ -2776,7 +2776,6 @@ static int do_sigpending(sigset_t *set)/* Outside the lock because only this thread touches it. */ sigandsets (set, ¤t->blocked, set); -return 0 ; }/**
@@ -2788,15 +2787,16 @@ static int do_sigpending(sigset_t *set) SYSCALL_DEFINE2(rt_sigpending, sigset_t __user *, uset, size_t, sigsetsize) {sigset_t set; -int err;if (sigsetsize >sizeof (*uset))return -EINVAL ; - err =do_sigpending (&set); -if (!err &©_to_user (uset, &set, sigsetsize)) - err = -EFAULT ; -return err; +do_sigpending (&set); + +if (copy_to_user (uset, &set, sigsetsize)) +return -EFAULT ; + +return 0 ; }#ifdef CONFIG_COMPAT
@@ -2804,15 +2804,13 @@ COMPAT_SYSCALL_DEFINE2(rt_sigpending, compat_sigset_t __user *, uset, compat_size_t, sigsetsize) {sigset_t set; -int err;if (sigsetsize >sizeof (*uset))return -EINVAL ; - err =do_sigpending (&set); -if (!err) - err =put_compat_sigset (uset, &set, sigsetsize); -return err; +do_sigpending (&set); + +return put_compat_sigset (uset, &set, sigsetsize); }#endif
@@ -3647,25 +3645,26 @@ int __compat_save_altstack(compat_stack_t __user *uss, unsigned long sp) SYSCALL_DEFINE1(sigpending, old_sigset_t __user *, uset) {sigset_t set; -int err;if (sizeof (old_sigset_t) >sizeof (*uset))return -EINVAL ; - err =do_sigpending (&set); -if (!err &©_to_user (uset, &set,sizeof (old_sigset_t))) - err = -EFAULT ; -return err; +do_sigpending (&set); + +if (copy_to_user (uset, &set,sizeof (old_sigset_t))) +return -EFAULT ; + +return 0 ; }#ifdef CONFIG_COMPAT COMPAT_SYSCALL_DEFINE1(sigpending, compat_old_sigset_t __user *, set32) {sigset_t set; -int err =do_sigpending (&set); -if (!err) - err =put_user (set.sig[0 ], set32); -return err; + +do_sigpending (&set); + +return put_user (set.sig[0 ], set32); }#endif
Signature · 1 line
2.17.0
PATCH v3 5/17 signal: simplify rt_sigaction()
In reply to Christian BraunerThe goto is not needed and does not add any clarity. Simply return -EINVAL on unexpected sigset_t struct size directly.
---v2->v3:
* added Oleg's Acked-byv1->v2:
* [Christoph Hellwig] additional cleanupsv0->v1:
* unchanged
--- kernel/signal.c | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-)
kernel/signal.c +10 -12
@@ -3735,25 +3735,23 @@ SYSCALL_DEFINE4(rt_sigaction, int, sig, size_t, sigsetsize) {struct k_sigaction new_sa, old_sa; -int ret = -EINVAL ; +int ret;/* XXX: Don't preclude handling different sized sigset_t's. */ if (sigsetsize !=sizeof (sigset_t)) - goto out; +return -EINVAL ; -if (act) { -if (copy_from_user (&new_sa.sa, act,sizeof (new_sa.sa))) -return -EFAULT ; - } +if (act &©_from_user (&new_sa.sa, act,sizeof (new_sa.sa))) +return -EFAULT ; ret =do_sigaction (sig, act ? &new_sa :NULL , oact ? &old_sa :NULL ); +if (ret) +return ret; -if (!ret && oact) { -if (copy_to_user (oact, &old_sa.sa,sizeof (old_sa.sa))) -return -EFAULT ; - } -out: -return ret; +if (oact &©_to_user (oact, &old_sa.sa,sizeof (old_sa.sa))) +return -EFAULT ; + +return 0 ; }#ifdef CONFIG_COMPAT COMPAT_SYSCALL_DEFINE4(rt_sigaction, int, sig,
Signature · 1 line
2.17.0
PATCH v3 6/17 signal: make kill_ok_by_cred() return bool
In reply to Christian Braunerkill_ok_by_cred() already behaves like a boolean function. Let's actually declare it as such too.
---v2->v3:
* unchangedv1->v2:
* unchangedv0->v1:
* unchanged
--- kernel/signal.c | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-)
kernel/signal.c +6 -11
@@ -717,21 +717,16 @@ static inline bool si_fromuser(const struct siginfo *info)/* * called with RCU read lock from check_kill_permission() */ -static int kill_ok_by_cred (struct task_struct *t) +static bool kill_ok_by_cred (struct task_struct *t) {const struct cred *cred =current_cred ();const struct cred *tcred =__task_cred (t); -if (uid_eq (cred->euid, tcred->suid) || -uid_eq (cred->euid, tcred->uid) || -uid_eq (cred->uid, tcred->suid) || -uid_eq (cred->uid, tcred->uid)) -return 1 ; - -if (ns_capable (tcred->user_ns,CAP_KILL )) -return 1 ; - -return 0 ; +return uid_eq (cred->euid, tcred->suid) || +uid_eq (cred->euid, tcred->uid) || +uid_eq (cred->uid, tcred->suid) || +uid_eq (cred->uid, tcred->uid) || +ns_capable (tcred->user_ns,CAP_KILL ); }/*
Signature · 1 line
2.17.0
PATCH v3 7/17 signal: make sig_handler_ignored() return bool
In reply to Christian Braunersig_handler_ignored() already behaves like a boolean function. Let's actually declare it as such too.
---v2->v3:
* unchangedv1->v2:
* unchangedv0->v1:
* patch added
--- kernel/signal.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/signal.c +2 -2
@@ -65,11 +65,11 @@ static void __user *sig_handler(struct task_struct *t, int sig)return t->sighand->action[sig -1 ].sa.sa_handler; } -static int sig_handler_ignored (void __user *handler,int sig) +static inline bool sig_handler_ignored (void __user *handler,int sig) {/* Is it explicitly or implicitly ignored? */ return handler ==SIG_IGN || - (handler ==SIG_DFL &&sig_kernel_ignore (sig)); + (handler ==SIG_DFL &&sig_kernel_ignore (sig)); }static int sig_task_ignored (struct task_struct *t,int sig,bool force)
Signature · 1 line
2.17.0
PATCH v3 8/17 signal: make sig_task_ignored() return bool
In reply to Christian Braunersig_task_ignored() already behaves like a boolean function. Let's actually declare it as such too.
---v2->v3:
* unchangedv1->v2:
* unchangedv0->v1:
* patch added
--- kernel/signal.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/signal.c +2 -2
@@ -72,7 +72,7 @@ static inline bool sig_handler_ignored(void __user *handler, int sig) (handler ==SIG_DFL &&sig_kernel_ignore (sig)); } -static int sig_task_ignored (struct task_struct *t,int sig,bool force) +static bool sig_task_ignored (struct task_struct *t,int sig,bool force) {void __user *handler;
@@ -80,7 +80,7 @@ static int sig_task_ignored(struct task_struct *t, int sig, bool force)if (unlikely (t->signal->flags &SIGNAL_UNKILLABLE ) && handler ==SIG_DFL && !(force &&sig_kernel_only (sig))) -return 1 ; +return true;return sig_handler_ignored (handler, sig); }
Signature · 1 line
2.17.0
PATCH v3 9/17 signal: make sig_ignored() return bool
In reply to Christian Braunersig_ignored() already behaves like a boolean function. Let's actually declare it as such too.
---v2->v3:
* unchangedv1->v2:
* unchangedv0->v1:
* patch added
--- kernel/signal.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
kernel/signal.c +3 -3
@@ -85,7 +85,7 @@ static bool sig_task_ignored(struct task_struct *t, int sig, bool force)return sig_handler_ignored (handler, sig); } -static int sig_ignored (struct task_struct *t,int sig,bool force) +static bool sig_ignored (struct task_struct *t,int sig,bool force) {/* * Blocked signals are never ignored, since the
@@ -93,7 +93,7 @@ static int sig_ignored(struct task_struct *t, int sig, bool force)* unblocked. */ if (sigismember (&t->blocked, sig) ||sigismember (&t->real_blocked, sig)) -return 0 ; +return false;/* * Tracers may want to know about even ignored signal unless it
@@ -101,7 +101,7 @@ static int sig_ignored(struct task_struct *t, int sig, bool force)* by SIGNAL_UNKILLABLE task. */ if (t->ptrace && sig !=SIGKILL ) -return 0 ; +return false;return sig_task_ignored (t, sig, force); }
Signature · 1 line
2.17.0
PATCH v3 10/17 signal: make has_pending_signals() return bool
In reply to Christian Braunerhas_pending_signals() already behaves like a boolean function. Let's actually declare it as such too.
---v2->v3:
* [Oleg Nestorov] remove other non-functional changes v1->v2:
* unchangedv0->v1:
* patch added
--- kernel/signal.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/signal.c +1 -1
@@ -110,7 +110,7 @@ static bool sig_ignored(struct task_struct *t, int sig, bool force)* Re-calculate pending state from the set of locally pending * signals, globally pending signals, and blocked signals. */ -static inline int has_pending_signals (sigset_t *signal,sigset_t *blocked) +static inline bool has_pending_signals (sigset_t *signal,sigset_t *blocked) {unsigned long ready;long i;
Signature · 1 line
2.17.0
PATCH v3 11/17 signal: make recalc_sigpending_tsk() return bool
In reply to Christian Braunerrecalc_sigpending_tsk() already behaves like a boolean function. Let's actually declare it as such too.
---v2->v3:
* unchangedv1->v2:
* unchangedv0->v1:
* patch added
--- kernel/signal.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
kernel/signal.c +4 -3
@@ -138,20 +138,21 @@ static inline bool has_pending_signals(sigset_t *signal, sigset_t *blocked)#define PENDING (p,b) has_pending_signals(&(p)->signal, (b)) -static int recalc_sigpending_tsk (struct task_struct *t) +static bool recalc_sigpending_tsk (struct task_struct *t) {if ((t->jobctl &JOBCTL_PENDING_MASK ) ||PENDING (&t->pending, &t->blocked) ||PENDING (&t->signal->shared_pending, &t->blocked)) {set_tsk_thread_flag (t,TIF_SIGPENDING ); -return 1 ; +return true; } +/* * We must never clear the flag in another thread, or in current * when it's possible the current syscall is returning -ERESTART*. * So we don't clear it here, and only callers who know they should do. */ -return 0 ; +return false; }/*
Signature · 1 line
2.17.0
PATCH v3 12/17 signal: make unhandled_signal() return bool
In reply to Christian Braunerunhandled_signal() already behaves like a boolean function. Let's actually declare it as such too.
All callers treat it as such too.
---v2->v3:
* unchangedv1->v2:
* unchangedv0->v1:
* patch added
--- include/linux/signal.h | 2 +- kernel/signal.c | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-)
include/linux/signal.h +1 -1
@@ -284,7 +284,7 @@ static inline void disallow_signal(int sig)extern struct kmem_cache *sighand_cachep; -int unhandled_signal (struct task_struct *tsk,int sig); +extern bool unhandled_signal (struct task_struct *tsk,int sig);/* * In POSIX a signal is sent either to a specific thread (Linux task)
kernel/signal.c +5 -3
@@ -505,13 +505,15 @@ flush_signal_handlers(struct task_struct *t, int force_default) } } -int unhandled_signal (struct task_struct *tsk,int sig) +bool unhandled_signal (struct task_struct *tsk,int sig) {void __user *handler = tsk->sighand->action[sig-1 ].sa.sa_handler;if (is_global_init (tsk)) -return 1 ; +return true; +if (handler !=SIG_IGN && handler !=SIG_DFL ) -return 0 ; +return false; +/* if ptraced, let the tracer determine */ return !tsk->ptrace; }
Signature · 1 line
2.17.0
PATCH v3 13/17 signal: make flush_sigqueue_mask() void
In reply to Christian BraunerThe return value of flush_sigqueue_mask() is never checked anywhere.
---v2->v3:
* unchangedv1->v2:
* unchangedv0->v1:
* patch added
--- kernel/signal.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
kernel/signal.c +2 -3
@@ -687,14 +687,14 @@ void signal_wake_up_state(struct task_struct *t, unsigned int state)* * All callers must be holding the siglock. */ -static int flush_sigqueue_mask (sigset_t *mask,struct sigpending *s) +static void flush_sigqueue_mask (sigset_t *mask,struct sigpending *s) {struct sigqueue *q, *n;sigset_t m;sigandsets (&m, mask, &s->signal);if (sigisemptyset (&m)) -return 0 ; +return ;sigandnsets (&s->signal, &s->signal, mask);list_for_each_entry_safe (q, n, &s->list, list) {
@@ -703,7 +703,6 @@ static int flush_sigqueue_mask(sigset_t *mask, struct sigpending *s)__sigqueue_free (q); } } -return 1 ; }static inline int is_si_special (const struct siginfo *info)
Signature · 1 line
2.17.0
PATCH v3 14/17 signal: make wants_signal() return bool
In reply to Christian Braunerwants_signal() already behaves like a boolean function. Let's actually declare it as such too.
---v2->v3:
* unchangedv1->v2:
* unchangedv0->v1:
* patch added
--- kernel/signal.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-)
kernel/signal.c +9 -5
@@ -879,16 +879,20 @@ static bool prepare_signal(int sig, struct task_struct *p, bool force)* as soon as they're available, so putting the signal on the shared queue * will be equivalent to sending it to one such thread. */ -static inline int wants_signal (int sig,struct task_struct *p) +static inline bool wants_signal (int sig,struct task_struct *p) {if (sigismember (&p->blocked, sig)) -return 0 ; +return false; +if (p->flags &PF_EXITING ) -return 0 ; +return false; +if (sig ==SIGKILL ) -return 1 ; +return true; +if (task_is_stopped_or_traced (p)) -return 0 ; +return false; +return task_curr (p) || !signal_pending (p); }
Signature · 1 line
2.17.0
PATCH v3 15/17 signal: make legacy_queue() return bool
In reply to Christian Braunerlegacy_queue() already behaves like a boolean function. Let's actually declare it as such too.
---v2->v3:
* unchangedv1->v2:
* unchangedv0->v1:
* patch added
--- kernel/signal.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/signal.c +1 -1
@@ -972,7 +972,7 @@ static void complete_signal(int sig, struct task_struct *p, int group)return ; } -static inline int legacy_queue (struct sigpending *signals,int sig) +static inline bool legacy_queue (struct sigpending *signals,int sig) {return (sig <SIGRTMIN ) &&sigismember (&signals->signal, sig); }
Signature · 1 line
2.17.0
PATCH v3 16/17 signal: make sigkill_pending() return bool
In reply to Christian Braunersigkill_pending() already behaves like a boolean function. Let's actually declare it as such too.
---v2->v3:
* [Oleg Nestorov] fix wrong function name in commit message v1->v2:
* unchangedv0->v1:
* patch added
--- kernel/signal.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
kernel/signal.c +3 -3
@@ -1918,10 +1918,10 @@ static inline bool may_ptrace_stop(void)* Return non-zero if there is a SIGKILL that should be waking us up. * Called with the siglock held. */ -static int sigkill_pending (struct task_struct *tsk) +static bool sigkill_pending (struct task_struct *tsk) { -return sigismember (&tsk->pending.signal,SIGKILL ) || -sigismember (&tsk->signal->shared_pending.signal,SIGKILL ); +return sigismember (&tsk->pending.signal,SIGKILL ) || +sigismember (&tsk->signal->shared_pending.signal,SIGKILL ); }/*
Signature · 1 line
2.17.0
PATCH v3 17/17 signal: make get_signal() return bool
In reply to Christian Braunermake get_signal() already behaves like a boolean function. Let's actually declare it as such too.
---v2->v3:
* unchangedv1->v2:
* unchangedv0->v1:
* patch added
--- include/linux/signal.h | 2 +- kernel/signal.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-)
include/linux/signal.h +1 -1
@@ -262,7 +262,7 @@ extern void set_current_blocked(sigset_t *);extern void __set_current_blocked (const sigset_t *);extern int show_unhandled_signals; -extern int get_signal (struct ksignal *ksig); +extern bool get_signal (struct ksignal *ksig);extern void signal_setup_done (int failed,struct ksignal *ksig,int stepping);extern void exit_signals (struct task_struct *tsk);extern void kernel_sigaction (int ,__sighandler_t );
kernel/signal.c +2 -2
@@ -2301,7 +2301,7 @@ static int ptrace_signal(int signr, siginfo_t *info)return signr; } -int get_signal (struct ksignal *ksig) +bool get_signal (struct ksignal *ksig) {struct sighand_struct *sighand = current->sighand;struct signal_struct *signal = current->signal;
@@ -2311,7 +2311,7 @@ int get_signal(struct ksignal *ksig)task_work_run ();if (unlikely (uprobe_deny_signal ())) -return 0 ; +return false;/* * Do this once, we can't return to user-mode if freezing() == T.
Signature · 1 line
2.17.0