Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp5557477rwd; Sun, 18 Jun 2023 14:16:20 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4F1Uo8pBY+O2WH14OiGh5eYonzrfapXOFWAF7VIQwzfOuZ3IhOkoUcZ4k8VOprrBRzWp0C X-Received: by 2002:a17:90a:51:b0:25c:1047:93b5 with SMTP id 17-20020a17090a005100b0025c104793b5mr5836712pjb.24.1687122980580; Sun, 18 Jun 2023 14:16:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687122980; cv=none; d=google.com; s=arc-20160816; b=iHMSeQ524o8BMed3cD3EmhiKUhfS5jxcxF1mHL84NbFbqjqUU6xLdmrdPs51w7+XNf TxGA4+F157Aiyeg8/HIMYOKTrfmuV9xpb7A6mzi1FmsKztFLpbZsEJMKxDnd4MXiuFB6 JdMlhIPekQBomgHAmNQjtWsG1TjU75wOOO18QHKeZ7IqHzAt++itsHzYuCzlP4YqJzHP lfl+ZzGhoEFgoEsGHr9oCNpC0auCuXvatngTevbeCaX1oitZw42RhhsGPzzreDxBJZLq A6vCEMgUIz+0XEYZaeMztMzUjGZLPST54g/mfYX/Bm9+n/HUQ4nwS+0EA1VSR6cMn6Kj fcGA== 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=9cX0/f87JAv45RYrZSJ9jgOnCKM2CFM0JW61+kgnazo=; b=BuIU0sD9DM7jRVDlXkEQ8S4n7XGtkRVjCxZmCsH08Bv8F9aRQMMIiLASK4m3U8ikqa bmb/9hCSQ3yPr13nOq2t+Lon7eSl43p24X6CIK6kK1AgzpQe2xyca7I46c7Mcq4khwt9 PL49YggXobJRuLdHnr9qTwJBoV2EqeiIKjr7nUSJNJevjVdZpQA3oVawTou6/iiaIyN8 OtCqvePKV23xqeodNhtzfhIlCzrtp1XBlAQgfaGqZ4vuYvR2bEtBvYNBn8OOy3jZVlu0 Qn0DCvRyWBe3vMpIxNWLds6H12fYE/NCiGAL0OKbGdvdOZ5ZnhkkHunRcB4gaKfEbX4F H9Gg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=SvaFHhSN; 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 e35-20020a17090a6fa600b0025be5f3c52fsi6254883pjk.38.2023.06.18.14.16.07; Sun, 18 Jun 2023 14:16:20 -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=SvaFHhSN; 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 S229865AbjFRUvS (ORCPT + 99 others); Sun, 18 Jun 2023 16:51:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48774 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229679AbjFRUuO (ORCPT ); Sun, 18 Jun 2023 16:50:14 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EC603123; Sun, 18 Jun 2023 13:50:10 -0700 (PDT) Date: Sun, 18 Jun 2023 20:50:08 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1687121409; 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=9cX0/f87JAv45RYrZSJ9jgOnCKM2CFM0JW61+kgnazo=; b=SvaFHhSNp9DFQKlUH5/ag17by7siXDDCjrMqMzVFC1t4ge5T/AkfKI0QoXT8PNTSjow4WM fJRo0dUuXU2qx80g9SlCLkJUirEbCydC3wvB6OKMUtcP/oLpgz0XJkin5ZkbP1C0UAioHX Yp2liWTgbHasQKp1FUvQjp7qqB2nQqgOUw2QjtUgUCfSQZf6AnoushUT+MEGLmPe0ehJ7Q APrZ1ElFoM24mbN48icBtuw2Non2jXRsneWQwA4FSlCXh9PrxqZpOErkohMKuVHPY1Spme CKwgdDEs9DFa4wmxsxbNE44v7Dyp6WlZ/wejHH8y4M10aBDiDaEJyoIgdXoDdg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1687121409; 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=9cX0/f87JAv45RYrZSJ9jgOnCKM2CFM0JW61+kgnazo=; b=3dDoYo3/zIGQB187CYKA7+wBRL5ywZf0oOhQ1kp6LvChdoT/HbPYsIrQYOCqj8mRvy6EjW dxLAs8O1NAK+70Cg== 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: <168712140881.404.6124752955191607832.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: 9d9e522010eb5685d8b53e8a24320653d9d4cbbf Gitweb: https://git.kernel.org/tip/9d9e522010eb5685d8b53e8a24320653d9d4cbbf Author: Thomas Gleixner AuthorDate: Thu, 01 Jun 2023 22:16:34 +02:00 Committer: Thomas Gleixner CommitterDate: Sun, 18 Jun 2023 22:40:42 +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 | 43 ++++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/kernel/time/posix-timers.c b/kernel/time/posix-timers.c index 808a247..ed3c4a9 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) { -retry_delete: - spin_lock_irq(&timer->it_lock); + unsigned long flags; + + /* + * irqsave is required to make timer_wait_running() work. + */ + spin_lock_irqsave(&timer->it_lock, flags); +retry_delete: + /* + * 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);