Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp7365033rwr; Tue, 25 Apr 2023 11:50:11 -0700 (PDT) X-Google-Smtp-Source: AKy350YzHprrExQrfncpW4L7mtJImfHHi7b3tv4/4XNgvNgmkMsl2ROfK1cgk3BnDXgzc51VhHJM X-Received: by 2002:a17:90b:3605:b0:247:56b3:4f2 with SMTP id ml5-20020a17090b360500b0024756b304f2mr27133571pjb.7.1682448611420; Tue, 25 Apr 2023 11:50:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682448611; cv=none; d=google.com; s=arc-20160816; b=uvVGOMGVjuxQX11fpOOacYaEqTaGWAVOxvNWVulN5bKvRXdbthRv4NgtpNta3ydPNr 5PhWGQv+S8oFKt8e2rnu32dL9QzTw1jL8mnnZaLTP35BWm8ZsyNdCptfGJ/DGELrgQoa jbHFH1MCHG7xNHDhGI8sVzgpq7UVcBKPLK51VyYbL5tpqhmxncEXojIMs7ApGETvmVTw JUgRLw6SZjZ0FCphVCLnn41Dn2BUp+E9ifzOyrpKX3xmN28ZgbNzF9aXiCLhEsgSyN0h Bdc3W5XbgAF5y0ZCAQGLPc+H6OBikrCuM7FqUeORx2htE1VFmI28QfAS/fSDHiSkwz8f 5s6A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:date:mime-version:references:subject:cc:to:from :dkim-signature:dkim-signature:message-id; bh=gYiHlRz0OuPVt4oauTqBjN/lQMg3nN/+xtqOB9QDFLM=; b=XRwpGRaBnjnXyNaoHN6ZUx9SpPSPVQG1hDU8zCDyYMzaJt/brIGCT318STe7rrZQIz xfw2FObIBBWIEcpDNo4wxWy4ybNXIZK5+FOheif+9kMybpQe7N2xAUBZL4xi6DhWu0aw kOMv7YcRYx+uZOQFjnxKaDEJQA2Mdu0P242wGkvALZG1vj4HADFc5dQa1fdFBKBgXHwL LpdGO3ss3uuMGsroEokKxH5yNR9ftGHPXot6uRwJO2aoaunrlIDSx063mVkVGzeQuAJo laLYVylrRffpqkV5AbFbrjYmXzoW1tw0ItwpNqh/n0owC5RW2XAj+857uotkNQrjDUmA C8Rg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=aqcvRFat; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=SJZ5cGBx; 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 k23-20020a6568d7000000b00503a029385asi10357675pgt.849.2023.04.25.11.49.58; Tue, 25 Apr 2023 11:50:11 -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=aqcvRFat; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=SJZ5cGBx; 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 S234672AbjDYStK (ORCPT + 99 others); Tue, 25 Apr 2023 14:49:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44604 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234673AbjDYStG (ORCPT ); Tue, 25 Apr 2023 14:49:06 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6C77216F2A for ; Tue, 25 Apr 2023 11:48:59 -0700 (PDT) Message-ID: <20230425183312.862346341@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1682448537; 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: references:references; bh=gYiHlRz0OuPVt4oauTqBjN/lQMg3nN/+xtqOB9QDFLM=; b=aqcvRFatWKA1jUrTiFWkwr0WYWB/qsrXbVkKW/GKowlhruGLnx28U6sXceSZWrgseV2bSs HAKdLpi3VH82mU1mcJqsph9GSEEZC1foLE0AO3fWCRXIyLnNr5U9rTUM1XQ7rwsy6JLY8V otBItxu9Iyl94fORNDZmbwFwb+5j/v/DLnY+QQV+4EG1nnc9I0FhiqsnnNeYt9zXAD5Xaz Z8m3V7BJhcmjV1G1BUAYR5lPF2TuAHGDATus9lTnaq9PZjqvHewfhdcbEuaGU6wAJhKwRk 8WjkEcqs7U3HLmfsmX+zXaJPjxdREDSvZoBBqW4AJs9AbL3oNlYfm+cpLNhHdA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1682448537; 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: references:references; bh=gYiHlRz0OuPVt4oauTqBjN/lQMg3nN/+xtqOB9QDFLM=; b=SJZ5cGBxyiarbsIRTK7MtaJ1euTQ5JFdAatIvEPnJ6q2pZ7FXUOEQo3TworOoicsK4Logo N4IGTgnNBbUgcQDA== From: Thomas Gleixner To: LKML Cc: Frederic Weisbecker , Anna-Maria Behnsen , Peter Zijlstra , Sebastian Siewior , syzbot+5c54bd3eb218bb595aa9@syzkaller.appspotmail.com, Dmitry Vyukov , Michael Kerrisk Subject: [patch 01/20] posix-timers: Prevent RT livelock in itimer_delete() References: <20230425181827.219128101@linutronix.de> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Date: Tue, 25 Apr 2023 20:48:56 +0200 (CEST) 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,URIBL_BLOCKED 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. This only affects hrtimer based timers as Posix CPU timers cannot be concurrently expired. For CONFIG_POSIX_CPU_TIMERS_TASK_WORK=y this is obviously impossible as the task cannot run task work and exit at the same time. The CONFIG_POSIX_CPU_TIMERS_TASK_WORK=n (only non-RT) is prevented because interrupts are disabled. 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 Cc: Sebastian Siewior --- 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);