From nobody Mon Dec 1 22:02:39 2025 Received: from mail-pf1-f178.google.com (mail-pf1-f178.google.com [209.85.210.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D1EE13081C0 for ; Thu, 27 Nov 2025 19:43:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764272617; cv=none; b=tBDPoQ4iF9XOHfMbK9qTOcxl1INxzTxm3ChesQt+vhSSm7aQicRbod5un/7axxgr7c/CZJwPZ69XOxG4RgR4RD43o7ikmuCqtdr2f8FhN1n3rpbq5YZYMgPaTAk4hAESh3kvF1h35YV022gZ4TWCx/G+4FwrpEdlk1bXJaoLC9w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764272617; c=relaxed/simple; bh=6ZG1qS2Vd6hxGmpqzggBRITuy0L43pkATAdfn4T59z8=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=oUOROFarXjO/HytsJ00QlF6w+cm+SWeqDIJAt4ikj3HezqAMTlnTlfq8w8uJ5tG19rQ1JRYtpc18x5YJpXsnqkyDfHzvx4pnsp8ehYp6+XMow0gEayx+SIF1sCDkp/oZMTE/ZL4lZmGdcVYpKzU38eGhVFeoVaNOSeEImBy5MRE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=chrisdown.name; spf=pass smtp.mailfrom=chrisdown.name; dkim=pass (1024-bit key) header.d=chrisdown.name header.i=@chrisdown.name header.b=lSZFePDv; arc=none smtp.client-ip=209.85.210.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=chrisdown.name Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chrisdown.name Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chrisdown.name header.i=@chrisdown.name header.b="lSZFePDv" Received: by mail-pf1-f178.google.com with SMTP id d2e1a72fcca58-7b8bbf16b71so1426514b3a.2 for ; Thu, 27 Nov 2025 11:43:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chrisdown.name; s=google; t=1764272615; x=1764877415; darn=vger.kernel.org; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:from:to:cc:subject:date :message-id:reply-to; bh=z0x1B3o1ch+M9p3Hp3FX/pZfT+e2T90N3Hnx40yodGM=; b=lSZFePDv3nTuMECiqkAJ8ZoyRL4SNXcawgW0fXaxjENKWqfTks/efYEQvm5ILzJJAv 0ivPwpOsSfCQGDoppNropo7arBkli5i5PLUUQTBdnwBBwBD1QR0FitrE5sLTu1PUXHAD KhJ9IKnr4A62ob9cAvtRobwwx9m1nr2jPonew= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1764272615; x=1764877415; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=z0x1B3o1ch+M9p3Hp3FX/pZfT+e2T90N3Hnx40yodGM=; b=tufo03P5c5QVH7J8DdxCN+jwMmrnQy95XuHKj+HoV1+wTBoTtCny8cgD9q4/9PTxaS XVPu4ADLaXNpxsXp9phS0pnLZ1eFy4gE7VfFNRXhjoAoeqOkp5EHdEhW+Rbqcl6lMiBa OKBakNhVdaO/MxyEvtJcnvDP44RpGPM+/megVnAda2lpKhwbAwkbjz3ovaSm6krRKoXP ueBVO8aIw4kKPZPb1rsp7u8jEHCqELprjLY86Qlm4Nxk1ohVSX4yTtKmFBwFnKoDxaWL t0urKnnmjk9l3ztLa7mWk7dKKn8Fb8LnFmLsbZLH9Ng8sD3H2fXsHZHqDoZgUKg3xEct jb5w== X-Gm-Message-State: AOJu0YxiG/YoBMrQBjIHTmO9hKkft/ak+qLAnNbIu+V9wd9uxBYI3Whm yRqAu3sCwCsvrqjJdilhVR2RtoNFPUacT9w8GXqqhVpMClstfpa4rTa/5eEtdH0CNjc= X-Gm-Gg: ASbGncsPv0DVbpBawmYLUqQvf3d3Rnls4QLzYv+W6z5BbFbIfrfIeK+XfQgqQk6r0l3 Y71bVu1z3ctxAZwYfYJZG3UgrEfLimwKceSAMYAI6T9UB3AnETUExPJvF72pqKK9+tSh7UZVXhU QyOKZ96nyD0EyMGMpN0d38CP2IJQCTTxilPrSGwLtH5NOiN/6/cNpkVry1qPcZX/knFel1iaT71 nPdqleni/2+j3clKJ8hIVtDEwPqArdiAqC9hiPB5H6aCtKV/kUwanXjM8VO6t06fsKABKwU6LUL ZZJAZzWV93lvhSa1PFabf3BkXGav3lneDb9PPDqcLcPfeYHYVlQPbIDUzuWMiCWfPowP3v6MuRO UmR5XAUfjCrGLHj5Dsz6gx/PBabrVfAITKRiSbuBf4lpPs3tLClRonYd6wiVsfzOGab3ItWvkP6 4xeHr5X185 X-Google-Smtp-Source: AGHT+IFgHVpMNux+BiIoonb0wBexwK3SOmNegMo1qUNXZ3HBD3IB+qSfVV0dHZPpZqiwxXckWV7iQQ== X-Received: by 2002:aa7:989d:0:b0:7aa:ac12:2c33 with SMTP id d2e1a72fcca58-7c58c2a7354mr19687023b3a.1.1764272614871; Thu, 27 Nov 2025 11:43:34 -0800 (PST) Received: from localhost ([116.86.198.140]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7d15f9260a4sm2666692b3a.58.2025.11.27.11.43.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Nov 2025 11:43:34 -0800 (PST) Date: Fri, 28 Nov 2025 03:43:32 +0800 From: Chris Down To: Petr Mladek Cc: linux-kernel@vger.kernel.org, Greg Kroah-Hartman , Sergey Senozhatsky , Steven Rostedt , John Ogness , Geert Uytterhoeven , Tony Lindgren , kernel-team@fb.com Subject: [PATCH v8 06/21] printk: nbcon: Synchronise console unregistration against atomic flushers Message-ID: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/2.2.15 (2b349c5e) (2025-10-02) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The nbcon atomic flush path in __nbcon_atomic_flush_pending_con() calls nbcon_emit_next_record(), which uses console_srcu_read_flags() to read the console flags. console_srcu_read_flags() expects to be called under console_srcu_read_lock(), but the atomic flush path does not hold this lock. While console_srcu_read_flags() works without the lock in practice, this violates the SRCU contract: without holding the lock, unregister_console() cannot properly synchronise against concurrent atomic flushers, since synchronize_srcu() would not wait for them. Wrap the atomic flush critical section with console_srcu_read_lock() and console_srcu_read_unlock(). This ensures that: 1. unregister_console() can safely synchronise against atomic flushers via synchronize_srcu() before proceeding with console teardown. 2. The SRCU protection guarantees that console state remains valid (CON_SUSPENDED/CON_ENABLED) and that exit/cleanup routines will not run while the atomic flusher is operating. The locking is placed around the entire flush operation rather than just the flags read, as future changes will add additional SRCU-protected reads in this path. Signed-off-by: Chris Down --- kernel/printk/nbcon.c | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c index eb4c8faa213d..493c9e8b2dd5 100644 --- a/kernel/printk/nbcon.c +++ b/kernel/printk/nbcon.c @@ -1498,15 +1498,29 @@ static int __nbcon_atomic_flush_pending_con(struct = console *con, u64 stop_seq, { struct nbcon_write_context wctxt =3D { }; struct nbcon_context *ctxt =3D &ACCESS_PRIVATE(&wctxt, ctxt); + bool ctx_acquired =3D false; int err =3D 0; + int cookie; =20 ctxt->console =3D con; ctxt->spinwait_max_us =3D 2000; ctxt->prio =3D nbcon_get_default_prio(); ctxt->allow_unsafe_takeover =3D allow_unsafe_takeover; =20 - if (!nbcon_context_try_acquire(ctxt, false)) - return -EPERM; + /* + * Match the console_srcu_read_lock()/unlock expectation embedded in + * console_srcu_read_flags(), which is called from nbcon_emit_next_record= (). + * Without this, unregister_console() cannot synchronise against the + * atomic flusher. + */ + cookie =3D console_srcu_read_lock(); + + if (!nbcon_context_try_acquire(ctxt, false)) { + err =3D -EPERM; + goto out_unlock; + } + + ctx_acquired =3D true; =20 while (nbcon_seq_read(con) < stop_seq) { /* @@ -1514,8 +1528,11 @@ static int __nbcon_atomic_flush_pending_con(struct c= onsole *con, u64 stop_seq, * handed over or taken over. In both cases the context is no * longer valid. */ - if (!nbcon_emit_next_record(&wctxt, true)) - return -EAGAIN; + if (!nbcon_emit_next_record(&wctxt, true)) { + err =3D -EAGAIN; + ctx_acquired =3D false; + goto out_unlock; + } =20 if (!ctxt->backlog) { /* Are there reserved but not yet finalized records? */ @@ -1525,7 +1542,10 @@ static int __nbcon_atomic_flush_pending_con(struct c= onsole *con, u64 stop_seq, } } =20 - nbcon_context_release(ctxt); +out_unlock: + if (ctx_acquired) + nbcon_context_release(ctxt); + console_srcu_read_unlock(cookie); return err; } =20 --=20 2.51.2