Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp1292912rwd; Thu, 1 Jun 2023 13:19:55 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4W+jno5hK1ifoIW05uFj32FZ4CSGyCEPRciL39q1VsXkMPXFME2mPqJ7hUO5HAG4YTu8dZ X-Received: by 2002:a17:903:1cf:b0:1b0:6c3e:363 with SMTP id e15-20020a17090301cf00b001b06c3e0363mr633694plh.0.1685650794836; Thu, 01 Jun 2023 13:19:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685650794; cv=none; d=google.com; s=arc-20160816; b=ULc9r/e7TGD7g3ygUsNSo0z7B2vLjt9WQYErakTgQX//ucO9MY3tcuLVen+ylzHK3Z Y4/J/dow4VThFVK9nYfGybzehiv/AP1OzGUpLjdw4c33Rwk3GNDHEeLWoCyDLQhjfPBw XQXwGMB9GurcfDAdZfmSbCSy85VkIlPI5qIqyp9b52FtUiqhL5pnkxFmGvK7MaYRrIth CzdTgooh6qCg0NHIALZO4bzF9hZVsAFcgI3cV3yefjBJggxPlwvZHYgRUZCUxkDc/U7L RtG0+FiAbsYFnj8KNpN0OS/2ru19Kh6MFT7pjWvMOfSpoH0fZ8f3FU9ScnXpGb62CS82 0byQ== 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; 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 c24-20020a170902d91800b001a6ee332903si1735966plz.347.2023.06.01.13.19.39; Thu, 01 Jun 2023 13:19:54 -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; 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 S231419AbjFAUQx (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 Content-Type: text/plain 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 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 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);