Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp75385rwl; Thu, 6 Apr 2023 13:26:45 -0700 (PDT) X-Google-Smtp-Source: AKy350aRB3oloHC897Krn8ETxAcuqnUM+bE+/jfe6xWtN6OvUEwAGgylqVKWmaIEEwvC5qk/C20k X-Received: by 2002:a17:906:24d7:b0:933:3b2e:6016 with SMTP id f23-20020a17090624d700b009333b2e6016mr249185ejb.7.1680812805008; Thu, 06 Apr 2023 13:26:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680812804; cv=none; d=google.com; s=arc-20160816; b=friZIwmQj7jBkjNQ5LWJLoRwePmfsPYyeIAOWrbJ2l682n4Y6vmrg6dJqije9Af9RS Al5akq+vFySuKbVYCzNBQUdl17DG3ArfzSL7osbxVq65oYNN/W8bkBgXbrpGg6MAEFDO N9fIHfu4yV/DPdVkS6IpZMrRkrFHPo68zFnXZMNFMUUfvT30rrmLjYoVw3F3v3oIUQh0 sSsnE9EgL/k7srEQnAU2f6sLhPZsbj4EdY/93iXyUni/WanIc3STqHRKzv/W7Pk/i0rX GYGbu0xRxt6oF/x3s3k0+57x+tM+dQFVgwQSvEOlzNERu71XzUT9NzIX2oEX/pDDpYcx WMMQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=hPBwWzsuf10OLaAVTCaYjPLONJGSSpf1kqp034sRoAQ=; b=uGss+IOH4CNaGvqW6VgctfBBmKcJF6H9DDMP/wv0+EEWGqZ5t9WBzVWzq23Yow23nO IOHXbnF2xSxTP5A6MTvsUXdS+LO43nb9yGuVJwmEo59QOUIv5+hW4qmQGpFxy++d1mbo dPBh0CrJSkBe07+Zm8o6Sft/hAf2IemSM/P/m+oW3aGEVJ0tno4Io8WBbAnzxjivlmas w9l3z8Vo2x/MnTA++eYY3K2uXjSDFoa0OyQAdkWd0MkJcuSA3EcjE4nmlp62y8F+t+sU +shh9pmAxqhfiHN0GgYqYUgTa4wFRd4TdxJgTq5IEKiGd+BqPdHDhFe1nRj2r52h6oVj Op6Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=QMxAnedN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id xh1-20020a170906da8100b0092b44a1e319si1722310ejb.677.2023.04.06.13.26.18; Thu, 06 Apr 2023 13:26:44 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=QMxAnedN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239359AbjDFUXT (ORCPT + 99 others); Thu, 6 Apr 2023 16:23:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57358 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239094AbjDFUW5 (ORCPT ); Thu, 6 Apr 2023 16:22:57 -0400 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1E2F77EFA; Thu, 6 Apr 2023 13:22:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=hPBwWzsuf10OLaAVTCaYjPLONJGSSpf1kqp034sRoAQ=; b=QMxAnedNp7IDVbovhyH++LCRJa 4AkAwNcWM32tR82dPxgmMEEsUQ3ZaRv5ym5AZNngOMn0457baygSoYcvSZWm71mPbfm7t9TMz0kJw 3+sG7uwRZvBMD1QRs4f6wJzviS4fh99DPkCng9rycKGGGaKVFwIarCVwf+KnAc/p7H7YW32ESdVJA JuXeGeQwD8TC9Cws53icodLy4XpKoNl1MSB0FTJ8cjmQJY+jMFFSBDTY9io9eILKatzSNBfvw4P6B mpHxtqMfNvFWPiZxqQXTLSTNBRJ8C1lZDTdxKvfIAC/HStAVN66oIEARkzzswIBpl8514ER4pdSBr iVERAXsg==; Received: from j130084.upc-j.chello.nl ([24.132.130.84] helo=noisy.programming.kicks-ass.net) by casper.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1pkW7o-0008k6-W2; Thu, 06 Apr 2023 20:22:37 +0000 Received: from hirez.programming.kicks-ass.net (hirez.programming.kicks-ass.net [192.168.1.225]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (Client did not present a certificate) by noisy.programming.kicks-ass.net (Postfix) with ESMTPS id D2DB3300202; Thu, 6 Apr 2023 22:22:27 +0200 (CEST) Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id B702A212E36AE; Thu, 6 Apr 2023 22:22:27 +0200 (CEST) Date: Thu, 6 Apr 2023 22:22:27 +0200 From: Peter Zijlstra To: Marco Elver Cc: Thomas Gleixner , Ingo Molnar , Oleg Nesterov , "Eric W. Biederman" , linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Dmitry Vyukov , kasan-dev@googlegroups.com Subject: Re: [PATCH v6 1/2] posix-timers: Prefer delivery of signals to the current thread Message-ID: <20230406202227.GD405948@hirez.programming.kicks-ass.net> References: <20230316123028.2890338-1-elver@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230316123028.2890338-1-elver@google.com> X-Spam-Status: No, score=-2.5 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_NONE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Mar 16, 2023 at 01:30:27PM +0100, Marco Elver wrote: > From: Dmitry Vyukov > > POSIX timers using the CLOCK_PROCESS_CPUTIME_ID clock prefer the main > thread of a thread group for signal delivery. However, this has a > significant downside: it requires waking up a potentially idle thread. > > Instead, prefer to deliver signals to the current thread (in the same > thread group) if SIGEV_THREAD_ID is not set by the user. This does not > change guaranteed semantics, since POSIX process CPU time timers have > never guaranteed that signal delivery is to a specific thread (without > SIGEV_THREAD_ID set). > > The effect is that we no longer wake up potentially idle threads, and > the kernel is no longer biased towards delivering the timer signal to > any particular thread (which better distributes the timer signals esp. > when multiple timers fire concurrently). > > Signed-off-by: Dmitry Vyukov > Suggested-by: Oleg Nesterov > Reviewed-by: Oleg Nesterov > Signed-off-by: Marco Elver Acked-by: Peter Zijlstra (Intel) > --- > kernel/signal.c | 25 ++++++++++++++++++++++--- > 1 file changed, 22 insertions(+), 3 deletions(-) > > diff --git a/kernel/signal.c b/kernel/signal.c > index 8cb28f1df294..605445fa27d4 100644 > --- a/kernel/signal.c > +++ b/kernel/signal.c > @@ -1003,8 +1003,7 @@ static void complete_signal(int sig, struct task_struct *p, enum pid_type type) > /* > * Now find a thread we can wake up to take the signal off the queue. > * > - * If the main thread wants the signal, it gets first crack. > - * Probably the least surprising to the average bear. > + * Try the suggested task first (may or may not be the main thread). > */ > if (wants_signal(sig, p)) > t = p; > @@ -1970,8 +1969,23 @@ int send_sigqueue(struct sigqueue *q, struct pid *pid, enum pid_type type) > > ret = -1; > rcu_read_lock(); > + /* > + * This function is used by POSIX timers to deliver a timer signal. > + * Where type is PIDTYPE_PID (such as for timers with SIGEV_THREAD_ID > + * set), the signal must be delivered to the specific thread (queues > + * into t->pending). > + * > + * Where type is not PIDTYPE_PID, signals must just be delivered to the > + * current process. In this case, prefer to deliver to current if it is > + * in the same thread group as the target, as it avoids unnecessarily > + * waking up a potentially idle task. > + */ > t = pid_task(pid, type); > - if (!t || !likely(lock_task_sighand(t, &flags))) > + if (!t) > + goto ret; > + if (type != PIDTYPE_PID && same_thread_group(t, current)) > + t = current; > + if (!likely(lock_task_sighand(t, &flags))) > goto ret; > > ret = 1; /* the signal is ignored */ > @@ -1993,6 +2007,11 @@ int send_sigqueue(struct sigqueue *q, struct pid *pid, enum pid_type type) > q->info.si_overrun = 0; > > signalfd_notify(t, sig); > + /* > + * If the type is not PIDTYPE_PID, we just use shared_pending, which > + * won't guarantee that the specified task will receive the signal, but > + * is sufficient if t==current in the common case. > + */ > pending = (type != PIDTYPE_PID) ? &t->signal->shared_pending : &t->pending; > list_add_tail(&q->list, &pending->list); > sigaddset(&pending->signal, sig); > -- > 2.40.0.rc1.284.g88254d51c5-goog >