Received: by 2002:a05:6a10:6744:0:0:0:0 with SMTP id w4csp1313575pxu; Thu, 8 Oct 2020 08:30:50 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz9R/6SX0Mqnf3Y1RyavsRSkLJBhc8xzLCZGMXJT3qaM9ame8htzxPVp7xmJwU3698+3bzh X-Received: by 2002:a17:906:1d53:: with SMTP id o19mr9757959ejh.468.1602171049593; Thu, 08 Oct 2020 08:30:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1602171049; cv=none; d=google.com; s=arc-20160816; b=CL0NxQMZXseOL1k69IhZpCFya8Jq+o2Jq2rYeHzuZN2HsDhy1XwfQ3020XYjxV7j2q Ys9Nha+UzmZxkhxGJl9uhVbIOnf+WHnLAiZForDyMWBM5mV0iYln1CFQlyNr9lFR8/bl 64BqpLjt2kr8MbYmWIvLYt5XUCNqWjspJZ+x91FTFS3qcNbtixn/vtkI4UVEkH4m3yrP 6/GP/FrzbqkByGz653OZxcl3shXMXSNP5EP9d5J6q2Ejj4gMBaV4MlDLClA6mvuhWrYk ifGx3t1GqB5EncsEfJB6APJNiU+DDpI+CGinJnHvIHDEY/A2vpkmtcT34d6QFyDBjv/R o6lA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=WdNU1/1s6MZ8klR4m0H2ADstj9Y1OQ9mPuj9tKk20pA=; b=gLXie7GzK08mJ8SqGwD7A/H6u167M8aBC7pkh9KLvx5SL07IFvYXRhMNwTFf5ArD8t 8xXEne6EmlHLF7Yp1eEfcMpQlMJMxubumRhQn8TdDK51S7IRXI+3lPBpjhFsLssDGXWG 7+kiUBZH5/gqz8Kc040ZSp8qljB0UJKGWkMIKce+LwoUrrD49AmB5IEIbodJYhHGNy5R u+D5J4DfUQexKk/DZSgZLNjnveK11qnU4KAPZCOBVBv3fo3po1FSDmv2oz/Q4wb0fK/9 LpqiHa5RXN7wIjoSF+I2fOBzlWIQsRHDJcrF9jjPMYOdeqklBXmbmVW8qlI0+UzKp243 IjiQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel-dk.20150623.gappssmtp.com header.s=20150623 header.b=pVezvOiI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id a2si3781361edr.352.2020.10.08.08.30.25; Thu, 08 Oct 2020 08:30:49 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel-dk.20150623.gappssmtp.com header.s=20150623 header.b=pVezvOiI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731014AbgJHP2D (ORCPT + 99 others); Thu, 8 Oct 2020 11:28:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37448 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730999AbgJHP2B (ORCPT ); Thu, 8 Oct 2020 11:28:01 -0400 Received: from mail-io1-xd43.google.com (mail-io1-xd43.google.com [IPv6:2607:f8b0:4864:20::d43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EFB2CC0613D5 for ; Thu, 8 Oct 2020 08:27:59 -0700 (PDT) Received: by mail-io1-xd43.google.com with SMTP id d197so6625679iof.0 for ; Thu, 08 Oct 2020 08:27:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=WdNU1/1s6MZ8klR4m0H2ADstj9Y1OQ9mPuj9tKk20pA=; b=pVezvOiIaURw+DUbX9JEP3YVGuWO2zROTmevBe0w+81wMug4KZiRH8oCa0c41Yusxm WBAdGoKQKTyNCWGKhtKBa0geWPAlAsop0ydosh4xytArI4Ko107Arx2d05C/R3P2Lb/c 02RGSYZDGefIKlBRJ5yB5cczFuFlvTHwkaFLsuHug3NhHHGDcRrEZw9I/0EiPoIyYox8 4UBJv3wDU0Eppa0LODVDaNRuwV7TT3tjzqjqvVI6a1kjcEQwyMlVl57YZVUhZWUgwSpn tRA+t8xcda9KhsDydwjR3UoAHfIGSMg8otwbG9syYyYG7MVP0Pfu/3BotOib2IAbBoFA iQ2Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=WdNU1/1s6MZ8klR4m0H2ADstj9Y1OQ9mPuj9tKk20pA=; b=jz6sd83xZn91wOF5VWMDmbrHCHgvGO63l/iFkf5JhbSPRG6o8yv2iWlgwpzDomqA4j zGFY3TaN/7bS3YiaWooEA3ujO+OMaBUlPtbjqu5Cs8HVHIOylpyF8y13tBGEBjYOmwUO hG/Z4HFFse4Verh+2Cq/v+LTatokNLbiwXsPtkO4G60BkgaJkvAbMaq3Yf9lF0hqabQe TT3Vu7YYvKAC7ZtlaoiaNRTKHsyTNZiXH867P9gO1NEPvGGDNea8xsNOZgPk2/gSdQOu k+0/klMrVIkr64y91rek0ZH/iEjHSxECeIW0cnpI5S8jgKEA+k+QPOpv7kzcGx+hEOKU KB3A== X-Gm-Message-State: AOAM533l2uEWdtwlmIIsDhfKsW6Va8TEVexpONZsaalhiKsP8augCjL5 BqCQyDFy4jGOOdN+xwHBpYJymNcxvYHC2w== X-Received: by 2002:a02:c785:: with SMTP id n5mr7430189jao.128.1602170878850; Thu, 08 Oct 2020 08:27:58 -0700 (PDT) Received: from p1.localdomain ([65.144.74.34]) by smtp.gmail.com with ESMTPSA id l77sm2866260ill.4.2020.10.08.08.27.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Oct 2020 08:27:57 -0700 (PDT) From: Jens Axboe To: linux-kernel@vger.kernel.org, io-uring@vger.kernel.org Cc: peterz@infradead.org, oleg@redhat.com, tglx@linutronix.de, Jens Axboe Subject: [PATCH 3/4] kernel: add support for TIF_NOTIFY_SIGNAL Date: Thu, 8 Oct 2020 09:27:51 -0600 Message-Id: <20201008152752.218889-4-axboe@kernel.dk> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20201008152752.218889-1-axboe@kernel.dk> References: <20201008152752.218889-1-axboe@kernel.dk> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This adds TIF_NOTIFY_SIGNAL handling in the generic code, which if set, will return true if signal_pending() is used in a wait loop. That causes an exit of the loop so that notify_signal tracehooks can be run. If the wait loop is currently inside a system call, the system call is restarted once task_work has been processed. x86 is using the generic entry code, add the necessary TIF_NOTIFY_SIGNAL definitions for it. Signed-off-by: Jens Axboe --- arch/x86/include/asm/thread_info.h | 2 ++ arch/x86/kernel/signal.c | 5 ++++- include/linux/entry-common.h | 6 +++++- include/linux/entry-kvm.h | 4 ++-- include/linux/sched/signal.h | 11 ++++++++++- include/linux/tracehook.h | 27 +++++++++++++++++++++++++++ kernel/entry/common.c | 2 +- kernel/entry/kvm.c | 3 +++ 8 files changed, 54 insertions(+), 6 deletions(-) diff --git a/arch/x86/include/asm/thread_info.h b/arch/x86/include/asm/thread_info.h index 267701ae3d86..86ade67f21b7 100644 --- a/arch/x86/include/asm/thread_info.h +++ b/arch/x86/include/asm/thread_info.h @@ -93,6 +93,7 @@ struct thread_info { #define TIF_NOTSC 16 /* TSC is not accessible in userland */ #define TIF_IA32 17 /* IA32 compatibility process */ #define TIF_SLD 18 /* Restore split lock detection on context switch */ +#define TIF_NOTIFY_SIGNAL 19 /* signal notifications exist */ #define TIF_MEMDIE 20 /* is terminating due to OOM killer */ #define TIF_POLLING_NRFLAG 21 /* idle is polling for TIF_NEED_RESCHED */ #define TIF_IO_BITMAP 22 /* uses I/O bitmap */ @@ -123,6 +124,7 @@ struct thread_info { #define _TIF_NOTSC (1 << TIF_NOTSC) #define _TIF_IA32 (1 << TIF_IA32) #define _TIF_SLD (1 << TIF_SLD) +#define _TIF_NOTIFY_SIGNAL (1 << TIF_NOTIFY_SIGNAL) #define _TIF_POLLING_NRFLAG (1 << TIF_POLLING_NRFLAG) #define _TIF_IO_BITMAP (1 << TIF_IO_BITMAP) #define _TIF_FORCED_TF (1 << TIF_FORCED_TF) diff --git a/arch/x86/kernel/signal.c b/arch/x86/kernel/signal.c index be0d7d4152ec..cd140bbf8520 100644 --- a/arch/x86/kernel/signal.c +++ b/arch/x86/kernel/signal.c @@ -808,7 +808,10 @@ void arch_do_signal(struct pt_regs *regs) { struct ksignal ksig; - if (get_signal(&ksig)) { + if (test_thread_flag(TIF_NOTIFY_SIGNAL)) + tracehook_notify_signal(); + + if (task_sigpending(current) && get_signal(&ksig)) { /* Whee! Actually deliver the signal. */ handle_signal(&ksig, regs); return; diff --git a/include/linux/entry-common.h b/include/linux/entry-common.h index 159c7476b11b..f4234aaac36c 100644 --- a/include/linux/entry-common.h +++ b/include/linux/entry-common.h @@ -37,6 +37,10 @@ # define _TIF_UPROBE (0) #endif +#ifndef _TIF_NOTIFY_SIGNAL +# define _TIF_NOTIFY_SIGNAL (0) +#endif + /* * TIF flags handled in syscall_enter_from_usermode() */ @@ -69,7 +73,7 @@ #define EXIT_TO_USER_MODE_WORK \ (_TIF_SIGPENDING | _TIF_NOTIFY_RESUME | _TIF_UPROBE | \ - _TIF_NEED_RESCHED | _TIF_PATCH_PENDING | \ + _TIF_NEED_RESCHED | _TIF_PATCH_PENDING | _TIF_NOTIFY_SIGNAL | \ ARCH_EXIT_TO_USER_MODE_WORK) /** diff --git a/include/linux/entry-kvm.h b/include/linux/entry-kvm.h index 0cef17afb41a..9b93f8584ff7 100644 --- a/include/linux/entry-kvm.h +++ b/include/linux/entry-kvm.h @@ -11,8 +11,8 @@ # define ARCH_XFER_TO_GUEST_MODE_WORK (0) #endif -#define XFER_TO_GUEST_MODE_WORK \ - (_TIF_NEED_RESCHED | _TIF_SIGPENDING | \ +#define XFER_TO_GUEST_MODE_WORK \ + (_TIF_NEED_RESCHED | _TIF_SIGPENDING | _TIF_NOTIFY_SIGNAL | \ _TIF_NOTIFY_RESUME | ARCH_XFER_TO_GUEST_MODE_WORK) struct kvm_vcpu; diff --git a/include/linux/sched/signal.h b/include/linux/sched/signal.h index 404145dc536e..9bc13ade2ff9 100644 --- a/include/linux/sched/signal.h +++ b/include/linux/sched/signal.h @@ -360,6 +360,15 @@ static inline int task_sigpending(struct task_struct *p) static inline int signal_pending(struct task_struct *p) { +#ifdef TIF_NOTIFY_SIGNAL + /* + * TIF_NOTIFY_SIGNAL isn't really a signal, but it requires the same + * behavior in terms of ensuring that we break out of wait loops + * so that notify signal callbacks can be processed. + */ + if (unlikely(test_tsk_thread_flag(p, TIF_NOTIFY_SIGNAL))) + return 1; +#endif return task_sigpending(p); } @@ -507,7 +516,7 @@ 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)); + WARN_ON(!signal_pending(current)); else restore_saved_sigmask(); } diff --git a/include/linux/tracehook.h b/include/linux/tracehook.h index b480e1a07ed8..bec952f51439 100644 --- a/include/linux/tracehook.h +++ b/include/linux/tracehook.h @@ -198,4 +198,31 @@ static inline void tracehook_notify_resume(struct pt_regs *regs) blkcg_maybe_throttle_current(); } +/* + * called by exit_to_user_mode_loop() if ti_work & _TIF_NOTIFY_SIGNAL. This + * is currently used by TWA_SIGNAL based task_work, which requires breaking + * wait loops to ensure that task_work is noticed and run. + */ +static inline void tracehook_notify_signal(void) +{ +#ifdef TIF_NOTIFY_SIGNAL + clear_thread_flag(TIF_NOTIFY_SIGNAL); + smp_mb__after_atomic(); + if (current->task_works) + task_work_run(); +#endif +} + +/* + * Called when we have work to process from exit_to_user_mode_loop() + */ +static inline void set_notify_signal(struct task_struct *task) +{ +#ifdef TIF_NOTIFY_SIGNAL + if (!test_and_set_tsk_thread_flag(task, TIF_NOTIFY_SIGNAL) && + !wake_up_state(task, TASK_INTERRUPTIBLE)) + kick_process(task); +#endif +} + #endif /* */ diff --git a/kernel/entry/common.c b/kernel/entry/common.c index d20ab4ac7183..89a068252897 100644 --- a/kernel/entry/common.c +++ b/kernel/entry/common.c @@ -157,7 +157,7 @@ static unsigned long exit_to_user_mode_loop(struct pt_regs *regs, if (ti_work & _TIF_PATCH_PENDING) klp_update_patch_state(current); - if (ti_work & _TIF_SIGPENDING) + if (ti_work & (_TIF_SIGPENDING | _TIF_NOTIFY_SIGNAL)) arch_do_signal(regs); if (ti_work & _TIF_NOTIFY_RESUME) { diff --git a/kernel/entry/kvm.c b/kernel/entry/kvm.c index b6678a5e3cf6..49972ee99aff 100644 --- a/kernel/entry/kvm.c +++ b/kernel/entry/kvm.c @@ -8,6 +8,9 @@ static int xfer_to_guest_mode_work(struct kvm_vcpu *vcpu, unsigned long ti_work) do { int ret; + if (ti_work & _TIF_NOTIFY_SIGNAL) + tracehook_notify_signal(); + if (ti_work & _TIF_SIGPENDING) { kvm_handle_signal_exit(vcpu); return -EINTR; -- 2.28.0