Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp5766842rwd; Mon, 5 Jun 2023 08:17:03 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7lEenZee9GqShjsysrAiNW5Be53waupNkgfQexWlLnk+nho7Hsli+bNMk5UK1exHGBFo/e X-Received: by 2002:a92:d581:0:b0:33b:2ea9:9f8e with SMTP id a1-20020a92d581000000b0033b2ea99f8emr186364iln.12.1685978222633; Mon, 05 Jun 2023 08:17:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685978222; cv=none; d=google.com; s=arc-20160816; b=oPVihIY/QlslP8v1w0U69C0TO842XA+EYcgnYrj1s6jFl4VFcU5dPoQNQN3+S+01ye rmDGrxUuFqfYedJY6+PA/8tYMfsbF2YF103ylQEeJk7COggTRYJgXCn88hiDmb3ePZw7 CLRH9WKWwVeT5DpRL2HlLdTryEWJvHXHNvEuHpuB3l5+tG/D72kXEeks49ePa++Glt3h 5hQZB3c3oggidB+CgOYba2XHVl5IvEpJ935AaDphlSsrbcQeN//KzVoUrg5+U6K6n8mP 85s5MnYL5PQed79xu6hurK+zkZZZ/JRoGXo42I+an0/yZ5ZvOeneXgNmWCF5E7p/iyXQ tZLw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:robot-unsubscribe :robot-id:message-id:mime-version:references:in-reply-to:cc:subject :to:reply-to:sender:from:dkim-signature:dkim-signature:date; bh=WRKARvUliXIIvLU+7X5+u+u6JXY8zYPAdY7I9M16Eqg=; b=rSHrjrhLoxbd8jjmJdPSWvxLnjJUT6n5wicJEHJZJr558BiM9g6T+RgdzL1f2lnQ+3 amJveFxR1LI8IphMQEOz6Y+fBIWSe6tiFZ8cIkP6wOvnY1CUYJNPrbrB2duI2H/88sO5 r12ENETvqi4M0L2v1imYEGZS7qnSYf48Rzq2xXJi4CllpHuqogjpxgFRjZ2FclhXbjos +tjQkiI1MrUgq/+LoXbFoqTJZptfzFjU6B8jqNQ89qauHvNoPeQnONjOaSXzFoarqXfp Ax1k8nuDNYds3U7cM1GajG1jvjIcP1ku2HNLHMJLiIoYr0sS3HTFl9UFDqbgeWRPOmPT 27ZA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b="GQH/ENpn"; dkim=neutral (no key) header.i=@linutronix.de; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id i133-20020a636d8b000000b0053f0cdab819si5630419pgc.366.2023.06.05.08.16.49; Mon, 05 Jun 2023 08:17:02 -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=@linutronix.de header.s=2020 header.b="GQH/ENpn"; dkim=neutral (no key) header.i=@linutronix.de; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234400AbjFEPJj (ORCPT + 99 others); Mon, 5 Jun 2023 11:09:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48044 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234279AbjFEPIf (ORCPT ); Mon, 5 Jun 2023 11:08:35 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F1C25F3; Mon, 5 Jun 2023 08:08:33 -0700 (PDT) Date: Mon, 05 Jun 2023 15:08:32 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1685977712; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=WRKARvUliXIIvLU+7X5+u+u6JXY8zYPAdY7I9M16Eqg=; b=GQH/ENpnqWkeAgZQIB2sv8Yciow3CdoT97JAgeh0L72URzCN2rSp+UFmzr73KjgQRwWpvY BR8f6WIsKPDa+5dNebgLqcmKi2hD//De7v8urwRErkMhHKT+KRNEc8jyrFdpMxI2YoinRK 7EF6hsu6c/gJ7pRb9DUJd5gi4AM49LBpZ4I/oDqNZPXqJjPFF0vWJq7y454bAFTGdDHSIL L9SjznRNRyPfJEIK2/Tr9/Rgn6TCHX6u0aSqCe+8CyFE7HOuonR7hMgfNVXowSs50+zscH NgQJ9OZSYPQAOncubj89dLNFycwrAeMNstLd3gIqrgMTtHRJMeQWXVt1oMHtFA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1685977712; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=WRKARvUliXIIvLU+7X5+u+u6JXY8zYPAdY7I9M16Eqg=; b=JpRstdjkoVoYlSMUbCvDg8gyZqa67YpM84p4+zyfLfGRwsqMot1rbSw5knEIIIrVKKaVCj bVZ84FmzA1Y2/iDg== From: "tip-bot2 for Thomas Gleixner" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: timers/core] posix-timers: Prevent RT livelock in itimer_delete() Cc: Thomas Gleixner , Frederic Weisbecker , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <87v8g7c50d.ffs@tglx> References: <87v8g7c50d.ffs@tglx> MIME-Version: 1.0 Message-ID: <168597771212.404.434816099112734866.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham 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 The following commit has been merged into the timers/core branch of tip: Commit-ID: 1b59b2577582f9cf3d0f17245675a76859175cc1 Gitweb: https://git.kernel.org/tip/1b59b2577582f9cf3d0f17245675a76859175cc1 Author: Thomas Gleixner AuthorDate: Thu, 01 Jun 2023 22:16:34 +02:00 Committer: Thomas Gleixner CommitterDate: Mon, 05 Jun 2023 17:03:36 +02:00 posix-timers: Prevent RT livelock in itimer_delete() itimer_delete() has a retry loop when the timer is concurrently expired. On non-RT kernels this just spin-waits until the timer callback has completed, except for posix CPU timers which have HAVE_POSIX_CPU_TIMERS_TASK_WORK enabled. In that case and on RT kernels the existing task could live lock when preempting the task which does the timer delivery. Replace spin_unlock() with an invocation of timer_wait_running() to handle it the same way as the other retry loops in the posix timer code. Fixes: ec8f954a40da ("posix-timers: Use a callback for cancel synchronization on PREEMPT_RT") Signed-off-by: Thomas Gleixner Reviewed-by: Frederic Weisbecker Link: https://lore.kernel.org/r/87v8g7c50d.ffs@tglx --- kernel/time/posix-timers.c | 41 ++++++++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/kernel/time/posix-timers.c b/kernel/time/posix-timers.c index 808a247..2d835c2 100644 --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -1037,27 +1037,52 @@ retry_delete: } /* - * return timer owned by the process, used by exit_itimers + * Delete a timer if it is armed, remove it from the hash and schedule it + * for RCU freeing. */ static void itimer_delete(struct k_itimer *timer) { + unsigned long flags; + retry_delete: - spin_lock_irq(&timer->it_lock); + /* + * irqsave is required to make timer_wait_running() work. + */ + spin_lock_irqsave(&timer->it_lock, flags); + /* + * Even if the timer is not longer accessible from other tasks + * it still might be armed and queued in the underlying timer + * mechanism. Worse, that timer mechanism might run the expiry + * function concurrently. + */ if (timer_delete_hook(timer) == TIMER_RETRY) { - spin_unlock_irq(&timer->it_lock); + /* + * Timer is expired concurrently, prevent livelocks + * and pointless spinning on RT. + * + * timer_wait_running() drops timer::it_lock, which opens + * the possibility for another task to delete the timer. + * + * That's not possible here because this is invoked from + * do_exit() only for the last thread of the thread group. + * So no other task can access and delete that timer. + */ + if (WARN_ON_ONCE(timer_wait_running(timer, &flags) != timer)) + return; + goto retry_delete; } list_del(&timer->list); - spin_unlock_irq(&timer->it_lock); + spin_unlock_irqrestore(&timer->it_lock, flags); release_posix_timer(timer, IT_ID_SET); } /* - * This is called by do_exit or de_thread, only when nobody else can - * modify the signal->posix_timers list. Yet we need sighand->siglock - * to prevent the race with /proc/pid/timers. + * Invoked from do_exit() when the last thread of a thread group exits. + * At that point no other task can access the timers of the dying + * task anymore. */ void exit_itimers(struct task_struct *tsk) { @@ -1067,10 +1092,12 @@ void exit_itimers(struct task_struct *tsk) if (list_empty(&tsk->signal->posix_timers)) return; + /* Protect against concurrent read via /proc/$PID/timers */ spin_lock_irq(&tsk->sighand->siglock); list_replace_init(&tsk->signal->posix_timers, &timers); spin_unlock_irq(&tsk->sighand->siglock); + /* The timers are not longer accessible via tsk::signal */ while (!list_empty(&timers)) { tmr = list_first_entry(&timers, struct k_itimer, list); itimer_delete(tmr);