From nobody Fri Dec 19 15:19:21 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9778415444E for ; Tue, 27 Aug 2024 04:43:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724733824; cv=none; b=bZ5x9+BjYMW5fYvj+fdBpgcHhNPaOnfl1B/g1ysjJhcH+O+Pm1DsX3ylBFz1MG7FJO/Srlbtc8DWE60wQEFUh4oH0Y5MmupgIuPI7LP3oF6B76yEY1b+NJbe0sRW6ARlUGZ3xbFb7r4NXPEkqipqU+Yy17tgEM5MH8SdZEWJM2w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724733824; c=relaxed/simple; bh=oE1v1DaJeW6+p8FukWR9NR+vLSM0VFF2u5IAJp1dFrA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=IrURToXQ+5F120MikeyZy5l/J1d9Uz74D+jq31zLY6p9beIUYp45ykcJJdaFKMI3I+dmFmRxu+ik4GjSCHuKuhQWb1wB3DuNr6otV23mGHaHSpz00bnqnfxKF4BXabmveKoK2RTJ8aX1No2BQbzSYnkBVyDucH/4RXAGAC60e1k= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=GrCmUzjn; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=CLxlcd/u; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="GrCmUzjn"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="CLxlcd/u" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1724733819; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=i+fUqbH7N6o416MlatQIV7ilZsDNv7ET4pJ564xt/kA=; b=GrCmUzjnodUbtrPWy878U9T6adp6DQ56O9eGQNbh13JtQXBYW5KZ2OKpISEYNbl7xS7rEj 5dDd1qZHN94Y4TUe3LZTyPQ1pmFcjllafwjFKM5VYmjf9b7/muO/Cj2ExIJrgMVBdS2Qtz jeLtgFXsArUb2Ogleq8ZilEvVbwQrbfK2zisK2tTm0Klb/zpfnLnWKtS8sdjdDY4bw3EX7 bW7kpV7Ya/cAiwCjAgT50f0bkt0M3er/qGgoBlAaZUWPaDTrZ3t6Yo7NSrZmcUIH2gPwUx NJiKX40yLYOycyV2LXEA57Nhh9U3goIxXrwSNDwfLnQDF1Mo0/TDVzMCQmbdHg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1724733819; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=i+fUqbH7N6o416MlatQIV7ilZsDNv7ET4pJ564xt/kA=; b=CLxlcd/uKvGfDWFILqnXG8F+M28oW/JGfszhsVriTLtnBuOpZuFBp5kl4YEVOFE4jy/69s WSlR0RNfKEO2L7BQ== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v4 17/17] printk: Avoid false positive lockdep report for legacy printing Date: Tue, 27 Aug 2024 06:49:33 +0206 Message-Id: <20240827044333.88596-18-john.ogness@linutronix.de> In-Reply-To: <20240827044333.88596-1-john.ogness@linutronix.de> References: <20240827044333.88596-1-john.ogness@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Legacy console printing from printk() caller context may invoke the console driver from atomic context. This leads to a lockdep splat because the console driver will acquire a sleeping lock and the caller may already hold a spinning lock. This is noticed by lockdep on !PREEMPT_RT configurations because it will lead to a problem on PREEMPT_RT. However, on PREEMPT_RT the printing path from atomic context is always avoided and the console driver is always invoked from a dedicated thread. Thus the lockdep splat on !PREEMPT_RT is a false positive. For !PREEMPT_RT override the lock-context before invoking the console driver to avoid the false positive. Do not override the lock-context for PREEMPT_RT in order to allow lockdep to catch any real locking context issues related to the write callback usage. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- kernel/printk/printk.c | 83 ++++++++++++++++++++++++++++++++---------- 1 file changed, 63 insertions(+), 20 deletions(-) diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index ed4a3cb00095..8458f4a22419 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -2982,6 +2982,34 @@ bool printk_get_next_message(struct printk_message *= pmsg, u64 seq, return true; } =20 +/* + * Legacy console printing from printk() caller context does not respect + * raw_spinlock/spinlock nesting. For !PREEMPT_RT the lockdep warning is a + * false positive. For PREEMPT_RT the false positive condition does not + * occur. + * + * This map is used to temporarily establish LD_WAIT_SLEEP context for the + * console write() callback when legacy printing to avoid false positive + * lockdep complaints, thus allowing lockdep to continue to function for + * real issues. + */ +#ifdef CONFIG_PREEMPT_RT +static inline void printk_legacy_allow_spinlock_enter(void) { } +static inline void printk_legacy_allow_spinlock_exit(void) { } +#else +static DEFINE_WAIT_OVERRIDE_MAP(printk_legacy_map, LD_WAIT_SLEEP); + +static inline void printk_legacy_allow_spinlock_enter(void) +{ + lock_map_acquire_try(&printk_legacy_map); +} + +static inline void printk_legacy_allow_spinlock_exit(void) +{ + lock_map_release(&printk_legacy_map); +} +#endif /* CONFIG_PREEMPT_RT */ + /* * Used as the printk buffers for non-panic, serialized console printing. * This is for legacy (!CON_NBCON) as well as all boot (CON_BOOT) consoles. @@ -3031,31 +3059,46 @@ static bool console_emit_next_record(struct console= *con, bool *handover, int co con->dropped =3D 0; } =20 - /* - * While actively printing out messages, if another printk() - * were to occur on another CPU, it may wait for this one to - * finish. This task can not be preempted if there is a - * waiter waiting to take over. - * - * Interrupts are disabled because the hand over to a waiter - * must not be interrupted until the hand over is completed - * (@console_waiter is cleared). - */ - printk_safe_enter_irqsave(flags); - console_lock_spinning_enable(); + /* Write everything out to the hardware. */ =20 - /* Do not trace print latency. */ - stop_critical_timings(); + if (force_legacy_kthread() && !panic_in_progress()) { + /* + * With forced threading this function is in a task context + * (either legacy kthread or get_init_console_seq()). There + * is no need for concern about printk reentrance, handovers, + * or lockdep complaints. + */ =20 - /* Write everything out to the hardware. */ - con->write(con, outbuf, pmsg.outbuf_len); + con->write(con, outbuf, pmsg.outbuf_len); + con->seq =3D pmsg.seq + 1; + } else { + /* + * While actively printing out messages, if another printk() + * were to occur on another CPU, it may wait for this one to + * finish. This task can not be preempted if there is a + * waiter waiting to take over. + * + * Interrupts are disabled because the hand over to a waiter + * must not be interrupted until the hand over is completed + * (@console_waiter is cleared). + */ + printk_safe_enter_irqsave(flags); + console_lock_spinning_enable(); =20 - start_critical_timings(); + /* Do not trace print latency. */ + stop_critical_timings(); =20 - con->seq =3D pmsg.seq + 1; + printk_legacy_allow_spinlock_enter(); + con->write(con, outbuf, pmsg.outbuf_len); + printk_legacy_allow_spinlock_exit(); =20 - *handover =3D console_lock_spinning_disable_and_check(cookie); - printk_safe_exit_irqrestore(flags); + start_critical_timings(); + + con->seq =3D pmsg.seq + 1; + + *handover =3D console_lock_spinning_disable_and_check(cookie); + printk_safe_exit_irqrestore(flags); + } skip: return true; } --=20 2.39.2