Skip to content

Commit

Permalink
signal: simplify set_user_sigmask/restore_user_sigmask
Browse files Browse the repository at this point in the history
task->saved_sigmask and ->restore_sigmask are only used in the ret-from-
syscall paths.  This means that set_user_sigmask() can save ->blocked in
->saved_sigmask and do set_restore_sigmask() to indicate that ->blocked
was modified.

This way the callers do not need 2 sigset_t's passed to set/restore and
restore_user_sigmask() renamed to restore_saved_sigmask_unless() turns
into the trivial helper which just calls restore_saved_sigmask().

Link: http://lkml.kernel.org/r/[email protected]
Signed-off-by: Oleg Nesterov <[email protected]>
Cc: Deepa Dinamani <[email protected]>
Cc: Arnd Bergmann <[email protected]>
Cc: Jens Axboe <[email protected]>
Cc: Davidlohr Bueso <[email protected]>
Cc: Eric Wong <[email protected]>
Cc: Jason Baron <[email protected]>
Cc: Thomas Gleixner <[email protected]>
Cc: Al Viro <[email protected]>
Cc: Eric W. Biederman <[email protected]>
Cc: David Laight <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
oleg-nesterov authored and torvalds committed Jul 17, 2019
1 parent e2d9018 commit b772434
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 108 deletions.
20 changes: 8 additions & 12 deletions fs/aio.c
Original file line number Diff line number Diff line change
Expand Up @@ -2094,7 +2094,6 @@ SYSCALL_DEFINE6(io_pgetevents,
const struct __aio_sigset __user *, usig)
{
struct __aio_sigset ksig = { NULL, };
sigset_t ksigmask, sigsaved;
struct timespec64 ts;
bool interrupted;
int ret;
Expand All @@ -2105,14 +2104,14 @@ SYSCALL_DEFINE6(io_pgetevents,
if (usig && copy_from_user(&ksig, usig, sizeof(ksig)))
return -EFAULT;

ret = set_user_sigmask(ksig.sigmask, &ksigmask, &sigsaved, ksig.sigsetsize);
ret = set_user_sigmask(ksig.sigmask, ksig.sigsetsize);
if (ret)
return ret;

ret = do_io_getevents(ctx_id, min_nr, nr, events, timeout ? &ts : NULL);

interrupted = signal_pending(current);
restore_user_sigmask(ksig.sigmask, &sigsaved, interrupted);
restore_saved_sigmask_unless(interrupted);
if (interrupted && !ret)
ret = -ERESTARTNOHAND;

Expand All @@ -2130,7 +2129,6 @@ SYSCALL_DEFINE6(io_pgetevents_time32,
const struct __aio_sigset __user *, usig)
{
struct __aio_sigset ksig = { NULL, };
sigset_t ksigmask, sigsaved;
struct timespec64 ts;
bool interrupted;
int ret;
Expand All @@ -2142,14 +2140,14 @@ SYSCALL_DEFINE6(io_pgetevents_time32,
return -EFAULT;


ret = set_user_sigmask(ksig.sigmask, &ksigmask, &sigsaved, ksig.sigsetsize);
ret = set_user_sigmask(ksig.sigmask, ksig.sigsetsize);
if (ret)
return ret;

ret = do_io_getevents(ctx_id, min_nr, nr, events, timeout ? &ts : NULL);

interrupted = signal_pending(current);
restore_user_sigmask(ksig.sigmask, &sigsaved, interrupted);
restore_saved_sigmask_unless(interrupted);
if (interrupted && !ret)
ret = -ERESTARTNOHAND;

Expand Down Expand Up @@ -2198,7 +2196,6 @@ COMPAT_SYSCALL_DEFINE6(io_pgetevents,
const struct __compat_aio_sigset __user *, usig)
{
struct __compat_aio_sigset ksig = { NULL, };
sigset_t ksigmask, sigsaved;
struct timespec64 t;
bool interrupted;
int ret;
Expand All @@ -2209,14 +2206,14 @@ COMPAT_SYSCALL_DEFINE6(io_pgetevents,
if (usig && copy_from_user(&ksig, usig, sizeof(ksig)))
return -EFAULT;

ret = set_compat_user_sigmask(ksig.sigmask, &ksigmask, &sigsaved, ksig.sigsetsize);
ret = set_compat_user_sigmask(ksig.sigmask, ksig.sigsetsize);
if (ret)
return ret;

ret = do_io_getevents(ctx_id, min_nr, nr, events, timeout ? &t : NULL);

interrupted = signal_pending(current);
restore_user_sigmask(ksig.sigmask, &sigsaved, interrupted);
restore_saved_sigmask_unless(interrupted);
if (interrupted && !ret)
ret = -ERESTARTNOHAND;

Expand All @@ -2234,7 +2231,6 @@ COMPAT_SYSCALL_DEFINE6(io_pgetevents_time64,
const struct __compat_aio_sigset __user *, usig)
{
struct __compat_aio_sigset ksig = { NULL, };
sigset_t ksigmask, sigsaved;
struct timespec64 t;
bool interrupted;
int ret;
Expand All @@ -2245,14 +2241,14 @@ COMPAT_SYSCALL_DEFINE6(io_pgetevents_time64,
if (usig && copy_from_user(&ksig, usig, sizeof(ksig)))
return -EFAULT;

ret = set_compat_user_sigmask(ksig.sigmask, &ksigmask, &sigsaved, ksig.sigsetsize);
ret = set_compat_user_sigmask(ksig.sigmask, ksig.sigsetsize);
if (ret)
return ret;

ret = do_io_getevents(ctx_id, min_nr, nr, events, timeout ? &t : NULL);

interrupted = signal_pending(current);
restore_user_sigmask(ksig.sigmask, &sigsaved, interrupted);
restore_saved_sigmask_unless(interrupted);
if (interrupted && !ret)
ret = -ERESTARTNOHAND;

Expand Down
12 changes: 4 additions & 8 deletions fs/eventpoll.c
Original file line number Diff line number Diff line change
Expand Up @@ -2313,19 +2313,17 @@ SYSCALL_DEFINE6(epoll_pwait, int, epfd, struct epoll_event __user *, events,
size_t, sigsetsize)
{
int error;
sigset_t ksigmask, sigsaved;

/*
* If the caller wants a certain signal mask to be set during the wait,
* we apply it here.
*/
error = set_user_sigmask(sigmask, &ksigmask, &sigsaved, sigsetsize);
error = set_user_sigmask(sigmask, sigsetsize);
if (error)
return error;

error = do_epoll_wait(epfd, events, maxevents, timeout);

restore_user_sigmask(sigmask, &sigsaved, error == -EINTR);
restore_saved_sigmask_unless(error == -EINTR);

return error;
}
Expand All @@ -2338,19 +2336,17 @@ COMPAT_SYSCALL_DEFINE6(epoll_pwait, int, epfd,
compat_size_t, sigsetsize)
{
long err;
sigset_t ksigmask, sigsaved;

/*
* If the caller wants a certain signal mask to be set during the wait,
* we apply it here.
*/
err = set_compat_user_sigmask(sigmask, &ksigmask, &sigsaved, sigsetsize);
err = set_compat_user_sigmask(sigmask, sigsetsize);
if (err)
return err;

err = do_epoll_wait(epfd, events, maxevents, timeout);

restore_user_sigmask(sigmask, &sigsaved, err == -EINTR);
restore_saved_sigmask_unless(err == -EINTR);

return err;
}
Expand Down
11 changes: 3 additions & 8 deletions fs/io_uring.c
Original file line number Diff line number Diff line change
Expand Up @@ -2400,7 +2400,6 @@ static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
const sigset_t __user *sig, size_t sigsz)
{
struct io_cq_ring *ring = ctx->cq_ring;
sigset_t ksigmask, sigsaved;
int ret;

if (io_cqring_events(ring) >= min_events)
Expand All @@ -2410,21 +2409,17 @@ static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
#ifdef CONFIG_COMPAT
if (in_compat_syscall())
ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
&ksigmask, &sigsaved, sigsz);
sigsz);
else
#endif
ret = set_user_sigmask(sig, &ksigmask,
&sigsaved, sigsz);
ret = set_user_sigmask(sig, sigsz);

if (ret)
return ret;
}

ret = wait_event_interruptible(ctx->wait, io_cqring_events(ring) >= min_events);

if (sig)
restore_user_sigmask(sig, &sigsaved, ret == -ERESTARTSYS);

restore_saved_sigmask_unless(ret == -ERESTARTSYS);
if (ret == -ERESTARTSYS)
ret = -EINTR;

Expand Down
34 changes: 12 additions & 22 deletions fs/select.c
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,6 @@ static long do_pselect(int n, fd_set __user *inp, fd_set __user *outp,
const sigset_t __user *sigmask, size_t sigsetsize,
enum poll_time_type type)
{
sigset_t ksigmask, sigsaved;
struct timespec64 ts, end_time, *to = NULL;
int ret;

Expand All @@ -753,12 +752,12 @@ static long do_pselect(int n, fd_set __user *inp, fd_set __user *outp,
return -EINVAL;
}

ret = set_user_sigmask(sigmask, &ksigmask, &sigsaved, sigsetsize);
ret = set_user_sigmask(sigmask, sigsetsize);
if (ret)
return ret;

ret = core_sys_select(n, inp, outp, exp, to);
restore_user_sigmask(sigmask, &sigsaved, ret == -ERESTARTNOHAND);
restore_saved_sigmask_unless(ret == -ERESTARTNOHAND);
ret = poll_select_copy_remaining(&end_time, tsp, type, ret);

return ret;
Expand Down Expand Up @@ -1086,7 +1085,6 @@ SYSCALL_DEFINE5(ppoll, struct pollfd __user *, ufds, unsigned int, nfds,
struct __kernel_timespec __user *, tsp, const sigset_t __user *, sigmask,
size_t, sigsetsize)
{
sigset_t ksigmask, sigsaved;
struct timespec64 ts, end_time, *to = NULL;
int ret;

Expand All @@ -1099,17 +1097,16 @@ SYSCALL_DEFINE5(ppoll, struct pollfd __user *, ufds, unsigned int, nfds,
return -EINVAL;
}

ret = set_user_sigmask(sigmask, &ksigmask, &sigsaved, sigsetsize);
ret = set_user_sigmask(sigmask, sigsetsize);
if (ret)
return ret;

ret = do_sys_poll(ufds, nfds, to);

restore_user_sigmask(sigmask, &sigsaved, ret == -EINTR);
restore_saved_sigmask_unless(ret == -EINTR);
/* We can restart this syscall, usually */
if (ret == -EINTR)
ret = -ERESTARTNOHAND;

ret = poll_select_copy_remaining(&end_time, tsp, PT_TIMESPEC, ret);

return ret;
Expand All @@ -1121,7 +1118,6 @@ SYSCALL_DEFINE5(ppoll_time32, struct pollfd __user *, ufds, unsigned int, nfds,
struct old_timespec32 __user *, tsp, const sigset_t __user *, sigmask,
size_t, sigsetsize)
{
sigset_t ksigmask, sigsaved;
struct timespec64 ts, end_time, *to = NULL;
int ret;

Expand All @@ -1134,17 +1130,16 @@ SYSCALL_DEFINE5(ppoll_time32, struct pollfd __user *, ufds, unsigned int, nfds,
return -EINVAL;
}

ret = set_user_sigmask(sigmask, &ksigmask, &sigsaved, sigsetsize);
ret = set_user_sigmask(sigmask, sigsetsize);
if (ret)
return ret;

ret = do_sys_poll(ufds, nfds, to);

restore_user_sigmask(sigmask, &sigsaved, ret == -EINTR);
restore_saved_sigmask_unless(ret == -EINTR);
/* We can restart this syscall, usually */
if (ret == -EINTR)
ret = -ERESTARTNOHAND;

ret = poll_select_copy_remaining(&end_time, tsp, PT_OLD_TIMESPEC, ret);

return ret;
Expand Down Expand Up @@ -1319,7 +1314,6 @@ static long do_compat_pselect(int n, compat_ulong_t __user *inp,
void __user *tsp, compat_sigset_t __user *sigmask,
compat_size_t sigsetsize, enum poll_time_type type)
{
sigset_t ksigmask, sigsaved;
struct timespec64 ts, end_time, *to = NULL;
int ret;

Expand All @@ -1342,12 +1336,12 @@ static long do_compat_pselect(int n, compat_ulong_t __user *inp,
return -EINVAL;
}

ret = set_compat_user_sigmask(sigmask, &ksigmask, &sigsaved, sigsetsize);
ret = set_compat_user_sigmask(sigmask, sigsetsize);
if (ret)
return ret;

ret = compat_core_sys_select(n, inp, outp, exp, to);
restore_user_sigmask(sigmask, &sigsaved, ret == -ERESTARTNOHAND);
restore_saved_sigmask_unless(ret == -ERESTARTNOHAND);
ret = poll_select_copy_remaining(&end_time, tsp, type, ret);

return ret;
Expand Down Expand Up @@ -1402,7 +1396,6 @@ COMPAT_SYSCALL_DEFINE5(ppoll_time32, struct pollfd __user *, ufds,
unsigned int, nfds, struct old_timespec32 __user *, tsp,
const compat_sigset_t __user *, sigmask, compat_size_t, sigsetsize)
{
sigset_t ksigmask, sigsaved;
struct timespec64 ts, end_time, *to = NULL;
int ret;

Expand All @@ -1415,17 +1408,16 @@ COMPAT_SYSCALL_DEFINE5(ppoll_time32, struct pollfd __user *, ufds,
return -EINVAL;
}

ret = set_compat_user_sigmask(sigmask, &ksigmask, &sigsaved, sigsetsize);
ret = set_compat_user_sigmask(sigmask, sigsetsize);
if (ret)
return ret;

ret = do_sys_poll(ufds, nfds, to);

restore_user_sigmask(sigmask, &sigsaved, ret == -EINTR);
restore_saved_sigmask_unless(ret == -EINTR);
/* We can restart this syscall, usually */
if (ret == -EINTR)
ret = -ERESTARTNOHAND;

ret = poll_select_copy_remaining(&end_time, tsp, PT_OLD_TIMESPEC, ret);

return ret;
Expand All @@ -1437,7 +1429,6 @@ COMPAT_SYSCALL_DEFINE5(ppoll_time64, struct pollfd __user *, ufds,
unsigned int, nfds, struct __kernel_timespec __user *, tsp,
const compat_sigset_t __user *, sigmask, compat_size_t, sigsetsize)
{
sigset_t ksigmask, sigsaved;
struct timespec64 ts, end_time, *to = NULL;
int ret;

Expand All @@ -1450,17 +1441,16 @@ COMPAT_SYSCALL_DEFINE5(ppoll_time64, struct pollfd __user *, ufds,
return -EINVAL;
}

ret = set_compat_user_sigmask(sigmask, &ksigmask, &sigsaved, sigsetsize);
ret = set_compat_user_sigmask(sigmask, sigsetsize);
if (ret)
return ret;

ret = do_sys_poll(ufds, nfds, to);

restore_user_sigmask(sigmask, &sigsaved, ret == -EINTR);
restore_saved_sigmask_unless(ret == -EINTR);
/* We can restart this syscall, usually */
if (ret == -EINTR)
ret = -ERESTARTNOHAND;

ret = poll_select_copy_remaining(&end_time, tsp, PT_TIMESPEC, ret);

return ret;
Expand Down
3 changes: 1 addition & 2 deletions include/linux/compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,7 @@ typedef struct {
compat_sigset_word sig[_COMPAT_NSIG_WORDS];
} compat_sigset_t;

int set_compat_user_sigmask(const compat_sigset_t __user *usigmask,
sigset_t *set, sigset_t *oldset,
int set_compat_user_sigmask(const compat_sigset_t __user *umask,
size_t sigsetsize);

struct compat_sigaction {
Expand Down
12 changes: 10 additions & 2 deletions include/linux/sched/signal.h
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,6 @@ void task_join_group_stop(struct task_struct *task);
static inline void set_restore_sigmask(void)
{
set_thread_flag(TIF_RESTORE_SIGMASK);
WARN_ON(!test_thread_flag(TIF_SIGPENDING));
}

static inline void clear_tsk_restore_sigmask(struct task_struct *task)
Expand Down Expand Up @@ -451,7 +450,6 @@ static inline bool test_and_clear_restore_sigmask(void)
static inline void set_restore_sigmask(void)
{
current->restore_sigmask = true;
WARN_ON(!test_thread_flag(TIF_SIGPENDING));
}
static inline void clear_tsk_restore_sigmask(struct task_struct *task)
{
Expand Down Expand Up @@ -484,6 +482,16 @@ static inline void restore_saved_sigmask(void)
__set_current_blocked(&current->saved_sigmask);
}

extern int set_user_sigmask(const sigset_t __user *umask, size_t sigsetsize);

static inline void restore_saved_sigmask_unless(bool interrupted)
{
if (interrupted)
WARN_ON(!test_thread_flag(TIF_SIGPENDING));
else
restore_saved_sigmask();
}

static inline sigset_t *sigmask_to_save(void)
{
sigset_t *res = &current->blocked;
Expand Down
4 changes: 0 additions & 4 deletions include/linux/signal.h
Original file line number Diff line number Diff line change
Expand Up @@ -273,10 +273,6 @@ extern int group_send_sig_info(int sig, struct kernel_siginfo *info,
struct task_struct *p, enum pid_type type);
extern int __group_send_sig_info(int, struct kernel_siginfo *, struct task_struct *);
extern int sigprocmask(int, sigset_t *, sigset_t *);
extern int set_user_sigmask(const sigset_t __user *usigmask, sigset_t *set,
sigset_t *oldset, size_t sigsetsize);
extern void restore_user_sigmask(const void __user *usigmask,
sigset_t *sigsaved, bool interrupted);
extern void set_current_blocked(sigset_t *);
extern void __set_current_blocked(const sigset_t *);
extern int show_unhandled_signals;
Expand Down
Loading

0 comments on commit b772434

Please sign in to comment.