From patchwork Sun Jun 18 20:50:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: tip-bot2 for Thomas Gleixner X-Patchwork-Id: 109685 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp2648957vqr; Sun, 18 Jun 2023 14:36:28 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5mVa5zmQiPwsSs/+8qPFCyhY8Is7GG9MAjatfMZvXVhz1e4L90B3s8t/r78FqBEyx6y3+C X-Received: by 2002:aca:f0b:0:b0:396:11b3:5851 with SMTP id 11-20020aca0f0b000000b0039611b35851mr7802829oip.54.1687124188037; Sun, 18 Jun 2023 14:36:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687124188; cv=none; d=google.com; s=arc-20160816; b=vmZ1fZwdC3Kxm8K29+bSH1BZMM02gN9uTFhTpanN3zZ8Zc3W1TwBSqm63IqZtv8BVu BrCp7C2JQetuHZHg1N4R7J2vFW3tLmD70Q5P9ibPk0BIR8vP7wGjoepo1GfRLBfMnga0 AeFC3nR0a1W4tYwus7M4vUZrn7gs3U3wZBosnWlR2t/3j9vvYNTIgvqkqR1PHA+3MeGc toeicytVpTI+zwnus1LdViDQIzygHO7zOYGpAcFvYhy4MjPjdlXJgqeoJeWF/0KEKOC7 LEkrkzNWCAtlJ/yxDJuQWZ+Wk+1mA5QT+G6cUI8f2TVJjLXEMXb6AoD8zwyfM7P6IPDB HZoA== 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 header.b="3dDoYo3/"; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id lk3-20020a17090b33c300b0025efb838ac0si2175752pjb.179.2023.06.18.14.36.14; Sun, 18 Jun 2023 14:36:27 -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 header.b="3dDoYo3/"; 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 S229866AbjFRUvS (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 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1764176692698493747?= X-GMAIL-MSGID: =?utf-8?q?1769077932500251681?= 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);