From patchwork Thu Jun 1 19:00:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Gleixner X-Patchwork-Id: 102143 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp552635vqr; Thu, 1 Jun 2023 12:07:03 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7QVYceiLG7foLsWrr7sGpXmdG3tKB/brodeh9rv+8OKVbmB8sjimOpSQ7ruu/mkK4E2lhi X-Received: by 2002:a17:903:1107:b0:1aa:ee36:40a5 with SMTP id n7-20020a170903110700b001aaee3640a5mr341083plh.34.1685646423157; Thu, 01 Jun 2023 12:07:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685646423; cv=none; d=google.com; s=arc-20160816; b=qJI93xp/vv7B5rP9XStIIhZwuEib9a8L6Rh24aWBIag798A1K0GeFewrQkY1j4aSay EFXd+U8T2IMP5G1QvdTJrG/2vBLGp4i+AZKI4ckLe+Qy13lzIcEKl0L4ukM9j9G/soQG zxrqt0gpicL7tavy7lz5pSb9AwDwSQpOpQCUeom3rig5jVB3DPblgJMtvEKbajje9iz0 j3oDwkGaHvt9FPQq4tpizrYNLHfIKXXLyACmqM22Tj99FuKU7QgLMxwNwpJ7gJQlPml7 6AhHbw0O0q46GbaZVKzOyEt/ODx+/0sKVqE86ajYritqGECvyLt1crpLyZWvpGO07MxH Q3Hg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:message-id:date:references :in-reply-to:subject:cc:to:dkim-signature:dkim-signature:from; bh=hm9lpOVRIrgDf0upILVFvudPRQ9b6fMMZIjbFvAMhEo=; b=WNKaPVMgaxIj5gw5ftNsCw/ytdF7qw3yjoF9E8V/GpU9rVGf/Mf8AIJeHU1pLTRhtd ZYShX2kiIpZWJpmCUp/A34eYnmdZABd5PB6Vvcm0mPRCBD2hZVTzWKFFdLo/L3MVBXtC pt3ltSbiSiK5E2vc/2yo6p/26QocZUTJSx//rj7TocbxVAWfZkhNd/PwK5zLYCK/yuqH GTDtIs26A9XK5/Brj0AXM9+Oa9qs8J94LiHh9ZOynJ9hzSaj/GUlxqkUVfxPmNvzfOfD xr9Z1N80bAfIJ77Rxwyt7kw4xs647rqrFIxv7HxHq40J3x0wSsZH7z7rrHETjeFkaeoX HnOg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b="L/9eu7og"; dkim=neutral (no key) header.i=@linutronix.de header.b=YCfxYHue; 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 b7-20020a170902650700b001a24521e826si3081758plk.61.2023.06.01.12.06.50; Thu, 01 Jun 2023 12:07:03 -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="L/9eu7og"; dkim=neutral (no key) header.i=@linutronix.de header.b=YCfxYHue; 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 S231794AbjFATAK (ORCPT + 99 others); Thu, 1 Jun 2023 15:00:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49962 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229691AbjFATAJ (ORCPT ); Thu, 1 Jun 2023 15:00:09 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F257A186 for ; Thu, 1 Jun 2023 12:00:07 -0700 (PDT) From: Thomas Gleixner DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1685646006; 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: in-reply-to:in-reply-to:references:references; bh=hm9lpOVRIrgDf0upILVFvudPRQ9b6fMMZIjbFvAMhEo=; b=L/9eu7og8PvzcG6lVIZiYNDU3/RloRDUOS+nXmXQvBn9gFxZgDThnsLEwCg9yeAxeOX35j ka5rRuV2CDq2jGxaGTlhTBSwSiBslepIM6aXKsiDRO2Tu37m7nV5JBc9yZBVuhicJeW39O gtgcPPsayEqcdfIAhLdJfR6ca2szt0EWp4PelT5/bxBjTvk3J+oFQ27gLY0DeONpDudGO5 K0/9RL/LuMiXChK01YuwA1VU17HDI0m+wiQyKWvsS0urJjIFB0lrfA+X67BkfI9sZRvLHw vJIS8yNysI/6GivhOjRgiC5ebYMfVlyb2zodrNlUvh6BDOnOcAdbISM1plGxIw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1685646006; 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: in-reply-to:in-reply-to:references:references; bh=hm9lpOVRIrgDf0upILVFvudPRQ9b6fMMZIjbFvAMhEo=; b=YCfxYHueNPG/c89plEMtgUPCwf5d0dNIsGSbTD+HO3AronIzZHIBVO80IhasGaMXrVT4wM dag0fNFBKVWBZ6CA== To: Frederic Weisbecker Cc: LKML , Anna-Maria Behnsen , Peter Zijlstra , Sebastian Siewior , syzbot+5c54bd3eb218bb595aa9@syzkaller.appspotmail.com, Dmitry Vyukov , Michael Kerrisk Subject: [patch v2 01/20] posix-timers: Prevent RT livelock in itimer_delete() In-Reply-To: <875y9743dt.ffs@tglx> References: <20230425181827.219128101@linutronix.de> <20230425183312.862346341@linutronix.de> <87bkj03qmq.ffs@tglx> <875y9743dt.ffs@tglx> Date: Thu, 01 Jun 2023 21:00:06 +0200 Message-ID: <877csndn49.ffs@tglx> MIME-Version: 1.0 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?1767528384028005035?= 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. On RT kernels this is a potential livelock when the exiting task preempted the hrtimer soft interrupt. 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 --- V2: Remove the bogus claims about posix CPU timers - Frederic --- kernel/time/posix-timers.c | 50 +++++++++++++++++++++++++++++++++++++-------- 1 file changed, 42 insertions(+), 8 deletions(-) --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -1037,27 +1037,59 @@ SYSCALL_DEFINE1(timer_delete, timer_t, t } /* - * 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; +retry_delete: + /* + * 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. + * + * The CONFIG_POSIX_CPU_TIMERS_TASK_WORK=y case is + * irrelevant here because obviously the exiting task + * cannot be expiring timer in task work concurrently. + * Ditto for CONFIG_POSIX_CPU_TIMERS_TASK_WORK=n as the + * tick interrupt cannot run on this CPU because the above + * spin_lock disabled interrupts. + * + * 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 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 +1099,12 @@ void exit_itimers(struct task_struct *ts 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);