Received: by 2002:ab2:784b:0:b0:1fd:adc2:8405 with SMTP id m11csp486613lqp; Mon, 10 Jun 2024 09:50:21 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCURFo2Yl92boa0/9n+AGNF6Nf/yj4jUPDtOc27TPq8Yn1ti9of22LXKA74pCAWdWuMlSRHJjVwa6V2Y2VMHMUbiAXzWhn/1UCq1he/mSw== X-Google-Smtp-Source: AGHT+IHO3TcgVNz41DpQdxzm0FkiIctqY8MIKdCnWEQvPhunnT6KzpdNtXrw2z9YxzXi/80hSI3K X-Received: by 2002:a50:8e45:0:b0:57c:546b:d303 with SMTP id 4fb4d7f45d1cf-57c546bd3a3mr6311575a12.25.1718038221838; Mon, 10 Jun 2024 09:50:21 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1718038221; cv=pass; d=google.com; s=arc-20160816; b=LnWMZ13tMFdMik3BGEKA5dYEaa1hqoeVjgthpjE/cg8ITKpOdvdy9nQT+QY+5nH+V1 YbZyYFFo4AhbUM1AB5xcNRGWXFC3AGcfLz7G2jJaG+nr7hnCTD4nhkwhisxcndmBEFv1 ftjZbgdkZKFvI/p+zkCkWgdXxoWh24BMo3AZrtQoNwTJq+loauIbKqfo5Q3Pafut0hrB RjWlGp8nyavY5hCUqEi6WdfZFWZlGmwEAxZbit3TXUvZ+bmsf+Yc/JE7xSdU0/fWNddc qEivAeXyyAzGYD2mv+/I+57iq8K9I4PNjlfBsJ9KkdOR6B0oJrLewwofT5QlTte7sj0a lG/A== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=date:mime-version:list-unsubscribe:list-subscribe:list-id :precedence:references:subject:cc:to:from:dkim-signature :dkim-signature:message-id; bh=hZthBClJqDYnocBuqiMS4yLZv+QB2bQbndBZ4lDgqqQ=; fh=joBs/8L+orz77ylyCy6wqNwwi35f7sYhLR+/TQjQjhI=; b=BwklcHXEWDdgNig1uDIQevY00cVsri0P4XTEKnBZNq+3mX3nhVdHaBe9U5I/MjihBu XqqjXGiw2r3EePaxld0rYRp+UckRZ4OuVhN7ptod2yma1eo6B+dqo0+uAher2KnbIUiV tHgsTdL6HuLc7VhS02mfUSbnMyT7xdjzwzvVqHkQdogk9DATZo4MNzIZK5YyW7RGh2tH yJXmdozkAFoXGO4GGwGmjv83AKox+UTlEFgaTS7Z0nJPRD7Yq6tvun3xzLFjjZDhCKS5 0zRJRPz2a28XpKMQpTJ//2LBDXmpoZ9fzNywtsRNS2xtI3wfpwY4nU2u3jKBp+1Ft+55 0IRg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=M+YnR1AP; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; arc=pass (i=1 spf=pass spfdomain=linutronix.de dkim=pass dkdomain=linutronix.de dmarc=pass fromdomain=linutronix.de); spf=pass (google.com: domain of linux-kernel+bounces-208601-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-208601-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id 4fb4d7f45d1cf-57c81b642d7si1201099a12.396.2024.06.10.09.50.21 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Jun 2024 09:50:21 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-208601-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=M+YnR1AP; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; arc=pass (i=1 spf=pass spfdomain=linutronix.de dkim=pass dkdomain=linutronix.de dmarc=pass fromdomain=linutronix.de); spf=pass (google.com: domain of linux-kernel+bounces-208601-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-208601-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 8E6DA1F21359 for ; Mon, 10 Jun 2024 16:50:21 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1080C1581F7; Mon, 10 Jun 2024 16:42:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="M+YnR1AP"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="fyaRYZMm" Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 82CB0156C68 for ; Mon, 10 Jun 2024 16:42:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718037774; cv=none; b=opldI4fqdpCRGrC0aX0dYhkB0pAfOFE1bmHVGbC7WGN7TO1T110mLyxXc5HmyFfIkQAxrmiVehtY/OhBPrM4xMoE7cyK/hFv1Wp7SOa7MTq2fRukPw7ASZaN7GBJMpt9lcubbOu7QKsUNpS2BznQMN2ki6TBwM3Sx6Z1fjahWcI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718037774; c=relaxed/simple; bh=SeQ2OVxjlLINqynDEK9vT3MFjf860yuU+9xoy6p/o24=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=D2bu8xX+PXIbYjaUrXQ7Hd/wAj1K3iD56RAqixCyDgvJEgmmwyQ6osKdRm7ynQopEQcLiOj5hs/urOqHomI7r74QI+rkCeQIlE9YXzAWJHeQHVH6yb5D+rvIpQQPy/RkYq25JmkxJychIlPWWIjUjKVdK5G/8fobcALiRGvnw88= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=M+YnR1AP; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=fyaRYZMm; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Message-ID: <20240610164027.729743690@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1718037770; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=hZthBClJqDYnocBuqiMS4yLZv+QB2bQbndBZ4lDgqqQ=; b=M+YnR1APSqUzzBQlmb2mgoBY3RQs5sF9wpg8fJ5I1jnes5kArHvjBIhUlp5ltDGf+mSLed KjxuS7dNWyEFENAzg2rrLUaJWfTyrriU6V6BNQ3PpHVgX5vI+ORD3fjpiJpjetXFuQKxwn ctULEnawGL86Ce27FuFx1BRn7BdbxKM/YGUB6H31MjUbEli1jgGwlrk5E/YHO2IA6AQG8/ U1kzYmGwxhaQu5YimOp1MzWotzvKlrfYSWVtp2mBvaNuqpDLHrKzjW0KvidLRiW6nCD8UG IYostn++mTpjtEm+J6lF0ZdfK7/bnoHoTDw8/4OCApYjIBDxFhVcbv2WR7/c8Q== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1718037770; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=hZthBClJqDYnocBuqiMS4yLZv+QB2bQbndBZ4lDgqqQ=; b=fyaRYZMmxHpt5QNepDFFFvh4F4/79CcyeFWrXpiWvjIhSxIMDsI8neIBoplLONZhAN8gt7 O6XOAWdoVvh72nBg== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V3 36/51] posix-timers: Add a refcount to struct k_itimer References: <20240610163452.591699700@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Date: Mon, 10 Jun 2024 18:42:49 +0200 (CEST) To cure the SIG_IGN handling for posix interval timers, the preallocated sigqueue needs to be embedded into struct k_itimer to prevent life time races of all sorts. To make this work correctly this needs reference counting so that timer deletion does not free the timer prematuraly when there is a signal queued or delivered concurrently. Add a rcuref to the posix timer part. Signed-off-by: Thomas Gleixner --- include/linux/posix-timers.h | 14 ++++++++++++++ kernel/time/posix-timers.c | 7 ++++--- 2 files changed, 18 insertions(+), 3 deletions(-) --- a/include/linux/posix-timers.h +++ b/include/linux/posix-timers.h @@ -6,11 +6,13 @@ #include #include #include +#include #include #include struct kernel_siginfo; struct task_struct; +struct k_itimer; static inline clockid_t make_process_cpuclock(const unsigned int pid, const clockid_t clock) @@ -105,6 +107,7 @@ static inline void posix_cputimers_rt_wa void posixtimer_rearm_itimer(struct task_struct *p); bool posixtimer_deliver_signal(struct kernel_siginfo *info); +void posixtimer_free_timer(struct k_itimer *timer); /* Init task static initializer */ #define INIT_CPU_TIMERBASE(b) { \ @@ -129,6 +132,7 @@ static inline void posix_cputimers_group u64 cpu_limit) { } static inline void posixtimer_rearm_itimer(struct task_struct *p) { } static inline bool posixtimer_deliver_signal(struct kernel_siginfo *info) { return false; } +static inline void posixtimer_free_timer(struct k_itimer *timer) { } #endif #ifdef CONFIG_POSIX_CPU_TIMERS_TASK_WORK @@ -156,6 +160,7 @@ static inline void posix_cputimers_init_ * @it_signal: Pointer to the creators signal struct * @it_pid: The pid of the process/task targeted by the signal * @it_process: The task to wakeup on clock_nanosleep (CPU timers) + * @rcuref: Reference count for life time management * @sigq: Pointer to preallocated sigqueue * @it: Union representing the various posix timer type * internals. @@ -180,6 +185,7 @@ struct k_itimer { struct task_struct *it_process; }; struct sigqueue *sigq; + rcuref_t rcuref; union { struct { struct hrtimer timer; @@ -200,4 +206,12 @@ void set_process_cpu_timer(struct task_s int update_rlimit_cpu(struct task_struct *task, unsigned long rlim_new); +#ifdef CONFIG_POSIX_TIMERS +static inline void posixtimer_putref(struct k_itimer *tmr) +{ + if (rcuref_put(&tmr->rcuref)) + posixtimer_free_timer(tmr); +} +#endif /* !CONFIG_POSIX_TIMERS */ + #endif --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -417,6 +417,7 @@ static struct k_itimer * alloc_posix_tim return NULL; } clear_siginfo(&tmr->sigq->info); + rcuref_init(&tmr->rcuref, 1); return tmr; } @@ -427,7 +428,7 @@ static void k_itimer_rcu_free(struct rcu kmem_cache_free(posix_timers_cache, tmr); } -static void posix_timer_free(struct k_itimer *tmr) +void posixtimer_free_timer(struct k_itimer *tmr) { put_pid(tmr->it_pid); sigqueue_free(tmr->sigq); @@ -439,7 +440,7 @@ static void posix_timer_unhash_and_free( spin_lock(&hash_lock); hlist_del_rcu(&tmr->t_hash); spin_unlock(&hash_lock); - posix_timer_free(tmr); + posixtimer_putref(tmr); } static int common_timer_create(struct k_itimer *new_timer) @@ -474,7 +475,7 @@ static int do_timer_create(clockid_t whi */ new_timer_id = posix_timer_add(new_timer); if (new_timer_id < 0) { - posix_timer_free(new_timer); + posixtimer_free_timer(new_timer); return new_timer_id; }