Received: by 2002:a89:d88:0:b0:1fa:5c73:8e2d with SMTP id eb8csp1876396lqb; Sun, 26 May 2024 23:39:24 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXrU46spjsYaxNiqC5MUEP8dUV3rDi0EvbY2uIsnbG5biIT0sDNbX4bCJgnmqfu+6K+q6TXrN0c7j9GqWWezWdepNH+mJNAGen1N5q78A== X-Google-Smtp-Source: AGHT+IEc3cdVgrudBD8oAoqgGNgA7JkATNtEwhNC55xrmBugnRzQ8oL5/b9yX9jauuWo3htWrm4p X-Received: by 2002:a05:622a:14a:b0:43a:f2be:6ce with SMTP id d75a77b69052e-43fb0f014dcmr122082951cf.60.1716791963781; Sun, 26 May 2024 23:39:23 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716791963; cv=pass; d=google.com; s=arc-20160816; b=Q5mM9YGFgPjwgewe86I8Q4UeXHuIp5wz7JWVYu/hmr8mQ6V3nrMBulgaiQQL2jy7gW uO1kHvsrAKw4pBYxdT1oBib6+PWDej0d/jvzUojmH3XSklRJbhlgFVQFcZk72S2XJWp5 wPhXkq/H6JB0FXO/L8vFb5zpmm0vBfAa8Mk3Ju9tvG9ZfA1/D5JMzrEDvgGpPlzmtZa8 Vi0WFxRL5FxH9WVJ0qyjIS5kuTnx6EQojDCtg9HQN6XJhYbihicsbv690v4y+7cyqHd9 3xDnYV/LsFsoxrIhwrP9cuebcRhOcpq4wKsC793pZy1bu7uKEo/GYWXYXbPPJemxwyLQ izmA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:dkim-signature:dkim-signature:from; bh=6LjCfsdgyf7GkeZyIQRXWJVLAkjljpAa6X7oN+BZvyM=; fh=/NZX815RJy/ecOe1WbZ023sSZUJKZnpaeIZwQG5Ym7U=; b=jQLOcFiVksvx+fruWnIz+ZRp6STdD3sACFUn9Cx/2ARmsOAWmn6J1beloNH05+Zgw2 FOtxjR//SdXY18ybYxLarsQZ3X0cGpfjVIrPnUDRPh9GRF9blEkfS40ZsfUw6mBMauST vhPsShxWBMLjQ1ymNpZxjBthhngjG2Lq5L6rtLwk/GyZU11hYCJd155THHg4vLM/6j0S Ins/BY6Ahb82d1gwKm8YZJA1MXbnzzQ7k0YztJLkWBwCY73zH6oIeqn+voYeYOHo17kz cqm+YC4b+pIn8gWxT3bX6cMlqTPbFR1QdlEbfXYtefYwqYcZ60r0z12lNDKnvToQdBec UNVg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=sWqQnTrc; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; arc=pass (i=1 spf=pass spfdomain=linutronix.de dkim=pass dkdomain=linutronix.de dmarc=pass fromdomain=linutronix.de); spf=pass (google.com: domain of linux-kernel+bounces-190080-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-190080-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id d75a77b69052e-43fb18b30c8si82345731cf.434.2024.05.26.23.39.23 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 26 May 2024 23:39:23 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-190080-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=sWqQnTrc; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; arc=pass (i=1 spf=pass spfdomain=linutronix.de dkim=pass dkdomain=linutronix.de dmarc=pass fromdomain=linutronix.de); spf=pass (google.com: domain of linux-kernel+bounces-190080-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-190080-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 849DE1C20D05 for ; Mon, 27 May 2024 06:39:23 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id CD72840870; Mon, 27 May 2024 06:38:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="sWqQnTrc"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="L2NgUy/X" 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 38032224D1 for ; Mon, 27 May 2024 06:38:22 +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=1716791903; cv=none; b=E5gwVujxe8byuPT8KWASbF3qb0Z6gZEKJVt3e7ps2z3QVrScZY/UfBReHLOqKnSojrSviS3GdLFiIS4NSMQIqeUbQjR7g0UcnYUSXJlOhjtb2tlM+WbzVxoZNtMFivaS4A8USneXg7ZeP+TCNLzJtTURbqr4/sDWZ/+241I4WIo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716791903; c=relaxed/simple; bh=axuNSOvCefXcmGgJa7oB4htrGdCQVV47smWqzTggUFE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=dYOk6FqmHQQttRMdo24gErITJz5QLqvoEgm5NPG9oBGCV0Uv6SuT9g9LYGz4KpQg+Ay5Y8qmP0bVyvSkvsK4jVkuRX6fLLnDPu4S09wp6WvwQVkJPetiPXxxfUOJ5KMr9GP+q+FpLG51l1BXzlyuaFZCZ5pJEv/LcEPc8k9da8E= 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=sWqQnTrc; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=L2NgUy/X; 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 From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1716791900; 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=6LjCfsdgyf7GkeZyIQRXWJVLAkjljpAa6X7oN+BZvyM=; b=sWqQnTrceODO+INDu27A0Twd5g9PL6uCSqYw7uIqk7lqjflRUYy8aeZThDHJi8bvok6r1w pxC5kWIcZh9S2fGSRTlD87PdomHuDPTZWjUjRiPuzRJVoXNE/dsjE3ofF58Z2KqY7VoCMK HuPf9klnpD6Pt/4bvYBeRVOA4ZA3E9dV6MMxdZlM82ShSe7Xm7R0Mbe9F2fzb2aMktDoOp UM1Nv+G+7inulNUican3Szs332LZDbsEPGgzVzI/zkBI1v2gY53ftSxC6G2OHjVDCMX1kp 1spA1wHmqSutZQsrtpWsX50uUIUhfg8NAXpAEM73ERIxjDdWwdDbUddk4hHLeA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1716791900; 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=6LjCfsdgyf7GkeZyIQRXWJVLAkjljpAa6X7oN+BZvyM=; b=L2NgUy/XBJuRceM8c2cC4vVv9ZgRuTSnQG1gnLXL93wthMHAzsivf0k7e9T1rAlRIdpwhm 4KvWkuPvufCmlpAg== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Greg Kroah-Hartman Subject: [PATCH printk v6 10/30] console: Improve console_srcu_read_flags() comments Date: Mon, 27 May 2024 08:43:29 +0206 Message-Id: <20240527063749.391035-11-john.ogness@linutronix.de> In-Reply-To: <20240527063749.391035-1-john.ogness@linutronix.de> References: <20240527063749.391035-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: 8bit It was not clear when exactly console_srcu_read_flags() must be used vs. directly reading @console->flags. Refactor and clarify that console_srcu_read_flags() is only needed if the console is registered or the caller is in a context where the registration status of the console may change (due to another context). The function requires the caller holds @console_srcu, which will ensure that the caller sees an appropriate @flags value for the registered console and that exit/cleanup routines will not run if the console is in the process of unregistration. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- include/linux/console.h | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/include/linux/console.h b/include/linux/console.h index 33a029d976c3..aea4f5aa4a45 100644 --- a/include/linux/console.h +++ b/include/linux/console.h @@ -428,28 +428,34 @@ extern void console_list_unlock(void) __releases(console_mutex); extern struct hlist_head console_list; /** - * console_srcu_read_flags - Locklessly read the console flags + * console_srcu_read_flags - Locklessly read flags of a possibly registered + * console * @con: struct console pointer of console to read flags from * - * This function provides the necessary READ_ONCE() and data_race() - * notation for locklessly reading the console flags. The READ_ONCE() - * in this function matches the WRITE_ONCE() when @flags are modified - * for registered consoles with console_srcu_write_flags(). + * Locklessly reading @con->flags provides a consistent read value because + * there is at most one CPU modifying @con->flags and that CPU is using only + * read-modify-write operations to do so. * - * Only use this function to read console flags when locklessly - * iterating the console list via srcu. + * Requires console_srcu_read_lock to be held, which implies that @con might + * be a registered console. The purpose of holding console_srcu_read_lock is + * to guarantee that the console state is valid (CON_SUSPENDED/CON_ENABLED) + * and that no exit/cleanup routines will run if the console is currently + * undergoing unregistration. + * + * If the caller is holding the console_list_lock or it is _certain_ that + * @con is not and will not become registered, the caller may read + * @con->flags directly instead. * * Context: Any context. + * Return: The current value of the @con->flags field. */ static inline short console_srcu_read_flags(const struct console *con) { WARN_ON_ONCE(!console_srcu_read_lock_is_held()); /* - * Locklessly reading console->flags provides a consistent - * read value because there is at most one CPU modifying - * console->flags and that CPU is using only read-modify-write - * operations to do so. + * The READ_ONCE() matches the WRITE_ONCE() when @flags are modified + * for registered consoles with console_srcu_write_flags(). */ return data_race(READ_ONCE(con->flags)); } -- 2.39.2