From patchwork Thu Jun 1 20:16:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Gleixner X-Patchwork-Id: 102174 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp590835vqr; Thu, 1 Jun 2023 13:19:58 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4RU4mYgl8I5nHK/y4iTS0hYGEkIO/UbfnuljTEgx3VBA9Ifihh3+0j3fDhbgfp9M9sS363 X-Received: by 2002:aa7:88c3:0:b0:64c:aa98:a69f with SMTP id k3-20020aa788c3000000b0064caa98a69fmr9869986pff.1.1685650797707; Thu, 01 Jun 2023 13:19:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685650797; cv=none; d=google.com; s=arc-20160816; b=l1IdW36ZZmxAs23iFFdI1NXAdMZG6E0/rB4TrF6T4pk6Cd0LBKdj0Jl+ek87T2sx+I qkDNi08BisfTureffL18/UBd91lgA8Qk7Zk7wxFrP9BNa+V6vrp2A7XaiFKE7wbG/u/T h88p7Io/nXc9QIhIQkJiyMKy78ts0r1BI+m1j6YS4Zl1iKPbSDiE0nQqqyL2oL60Mtvh yWG3WuwokHG5H1/lte5Ll0re1vCxbAa2J5oI6ZjbU+qrkykHdNTDTGu0tn52WdnNH66q NE3BLTiQHw0p92I4r5nu6m7uLaVB7OkWnbdruTPSV73kS2t27TV4zxbCM6DigH6wOFqL Xhwg== 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=CwGBlqmKEQjwSDozI8S2kLe+3TCN87aLMIOp5VDv7Yo=; b=YPJfZYi0Udst/Af65WEIjK2FXqXk5f7jt7vleWzGDc+D7tLoqNzVTTbVyiKuggErMk o7MJf+auQYgrNEkWBjk8QDFuL2S+sf5+VQ72SnMyJ+rLyuea0rM3QJj7fZkZq0goo5q/ s9K/lgL7kMrgLUgJtmDfoxUP5fpOsPO4s9VVrgZ59/9T0CdTr4LHc1UL57PRoBRfvywq q8byIeBQvVE+zjqWVS/a36k50YyQx9UQil109EihqzzcN/eS0Om2WgRohzgju1uTWNyx 5E+vqhKk+v1yD9R+vcT6rE5tBLudzAId7NB9m+Kuqmn6vDen2wlUp1c7pHcGktYSCWrb RnkA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=MbokPsgX; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; 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 d20-20020a637354000000b0053fcc7aaa03si621170pgn.805.2023.06.01.13.19.39; Thu, 01 Jun 2023 13:19:57 -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=MbokPsgX; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; 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 S231398AbjFAUQx (ORCPT + 99 others); Thu, 1 Jun 2023 16:16:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47370 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231424AbjFAUQq (ORCPT ); Thu, 1 Jun 2023 16:16:46 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 781B319A for ; Thu, 1 Jun 2023 13:16:36 -0700 (PDT) From: Thomas Gleixner DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1685650595; 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=CwGBlqmKEQjwSDozI8S2kLe+3TCN87aLMIOp5VDv7Yo=; b=MbokPsgXQMDNv7RNVO+0eBCQzFtJDABdNrVwcuIhJFGOpBxI7iZk7N3Lf4ecd4UNLoCbq7 3Sk5PooWUffOQRWLBgwKcwVMpwYrN5ISgIwgyo/pw9v+m5hWYz9yR4xxIG1gyxJtzODfI1 KINTxE6naVjgXEFg8V/Dnp4nGqZS9IZqnFDm2GOVLyYKHgUZPrYuwNNf5X8iJsJlPRFUHW HwQYTqtKntPD3de43mc3nsRmUsJjml1LCuR5vvS2Yfxu0q0NT7icDsAuqH12cjb1AdCcBe gJktk4KLY3ygJemQcidCNvjOI2JFKydu/dO2FUmDRen6skqR9+6XuOWVw8norA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1685650595; 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=CwGBlqmKEQjwSDozI8S2kLe+3TCN87aLMIOp5VDv7Yo=; b=cioAJpBD7R1LfKEzNJPsS7H4nXCl5LrT0+fYZYwHt0BHG9t2CRqx9v25ghIKjBb0YwEMVc /E+ApqhtWDxxkGDQ== To: Frederic Weisbecker Cc: LKML , Anna-Maria Behnsen , Peter Zijlstra , Sebastian Siewior , syzbot+5c54bd3eb218bb595aa9@syzkaller.appspotmail.com, Dmitry Vyukov , Michael Kerrisk Subject: [patch v2a 01/20] posix-timers: Prevent RT livelock in itimer_delete() In-Reply-To: <877csndn49.ffs@tglx> References: <20230425181827.219128101@linutronix.de> <20230425183312.862346341@linutronix.de> <87bkj03qmq.ffs@tglx> <875y9743dt.ffs@tglx> <877csndn49.ffs@tglx> Date: Thu, 01 Jun 2023 22:16:34 +0200 Message-ID: <87v8g7c50d.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?1767532971076545571?= 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 Reviewed-by: Frederic Weisbecker --- V2: Remove bogus comments vs. posix CPU timers - Frederic V2a: Send the real fixed up version --- kernel/time/posix-timers.c | 43 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 8 deletions(-) --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -1037,27 +1037,52 @@ 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. + * + * 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 *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);