[{"id":3687030,"web_url":"http://patchwork.ozlabs.org/comment/3687030/","msgid":"<afszs9k8cgo87JzB@hovoldconsulting.com>","list_archive_url":null,"date":"2026-05-06T12:27:31","subject":"Re: [PATCH 5/8] i2c: core: fix adapter registration race","submitter":{"id":64666,"url":"http://patchwork.ozlabs.org/api/people/64666/","name":"Johan Hovold","email":"johan@kernel.org"},"content":"On Tue, May 05, 2026 at 04:25:44PM +0200, Johan Hovold wrote:\n> Adapters can be looked up based on their id using i2c_get_adapter()\n> which takes a reference to the embedded struct device.\n> \n> Make sure that the adapter (including its struct device) has been\n> initialised before adding it to the IDR to avoid accessing uninitialised\n> data which could, for example, lead to NULL-pointer dereferences or\n> use-after-free.\n> \n> Fixes: 6e13e6418418 (\"i2c: Add i2c_add_numbered_adapter()\")\n> Cc: stable@vger.kernel.org\t# 2.6.22\n> Signed-off-by: Johan Hovold <johan@kernel.org>\n> ---\n>  drivers/i2c/i2c-core-base.c | 8 ++++++--\n>  1 file changed, 6 insertions(+), 2 deletions(-)\n> \n> diff --git a/drivers/i2c/i2c-core-base.c b/drivers/i2c/i2c-core-base.c\n> index 31f7d43e4ab5..be909d6bc776 100644\n> --- a/drivers/i2c/i2c-core-base.c\n> +++ b/drivers/i2c/i2c-core-base.c\n> @@ -1587,6 +1587,10 @@ static int i2c_register_adapter(struct i2c_adapter *adap)\n>  \tif (res)\n>  \t\tgoto out_reg;\n>  \n> +\tmutex_lock(&core_lock);\n> +\tidr_replace(&i2c_adapter_idr, adap, adap->nr);\n> +\tmutex_unlock(&core_lock);\n\nSashiko points out that this needs to go before registering the adapter\n(for now) as i2c-dev registers the chardev from a bus notifier callback.\n\n> +\n>  \tdev_dbg(&adap->dev, \"adapter [%s] registered\\n\", adap->name);\n>  \n>  \t/* create pre-declared device nodes */\n\nJohan","headers":{"Return-Path":"\n <linux-i2c+bounces-17271-incoming=patchwork.ozlabs.org@vger.kernel.org>","X-Original-To":["incoming@patchwork.ozlabs.org","linux-i2c@vger.kernel.org"],"Delivered-To":"patchwork-incoming@legolas.ozlabs.org","Authentication-Results":["legolas.ozlabs.org;\n\tdkim=pass (2048-bit key;\n unprotected) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256\n header.s=k20201202 header.b=r1FTydVA;\n\tdkim-atps=neutral","legolas.ozlabs.org;\n spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org\n (client-ip=2600:3c04:e001:36c::12fc:5321; helo=tor.lore.kernel.org;\n envelope-from=linux-i2c+bounces-17271-incoming=patchwork.ozlabs.org@vger.kernel.org;\n receiver=patchwork.ozlabs.org)","smtp.subspace.kernel.org;\n\tdkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org\n header.b=\"r1FTydVA\"","smtp.subspace.kernel.org;\n arc=none smtp.client-ip=10.30.226.201"],"Received":["from tor.lore.kernel.org (tor.lore.kernel.org\n [IPv6:2600:3c04:e001:36c::12fc:5321])\n\t(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)\n\t key-exchange x25519 server-signature ECDSA (secp384r1) server-digest SHA384)\n\t(No client certificate requested)\n\tby legolas.ozlabs.org (Postfix) with ESMTPS id 4g9ZQm637fz1yJV\n\tfor <incoming@patchwork.ozlabs.org>; Wed, 06 May 2026 22:27:44 +1000 (AEST)","from smtp.subspace.kernel.org (conduit.subspace.kernel.org\n [100.90.174.1])\n\tby tor.lore.kernel.org (Postfix) with ESMTP id D35BD3023D87\n\tfor <incoming@patchwork.ozlabs.org>; Wed,  6 May 2026 12:27:35 +0000 (UTC)","from localhost.localdomain (localhost.localdomain [127.0.0.1])\n\tby smtp.subspace.kernel.org (Postfix) with ESMTP id EA73A4418DC;\n\tWed,  6 May 2026 12:27:34 +0000 (UTC)","from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org\n [10.30.226.201])\n\t(using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits))\n\t(No client certificate requested)\n\tby smtp.subspace.kernel.org (Postfix) with ESMTPS id 91320270540;\n\tWed,  6 May 2026 12:27:34 +0000 (UTC)","by smtp.kernel.org (Postfix) with ESMTPSA id 2D559C2BCB8;\n\tWed,  6 May 2026 12:27:34 +0000 (UTC)","from johan by xi.lan with local (Exim 4.98.2)\n\t(envelope-from <johan@kernel.org>)\n\tid 1wKbLf-00000000mJA-2yAn;\n\tWed, 06 May 2026 14:27:31 +0200"],"ARC-Seal":"i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116;\n\tt=1778070454; cv=none;\n b=TwmPz1EXjov77na56cgbz/AXW1uyFMottBxLJXvo13iDhEEJqeVEhhXvTHci8cPByDtXf07vdiwwJc4T9Y9OLlkT++rR1d48+OEYeWku5PmOWSSab6yDSNb3Uv3jjVBuSrK47LwepmgJrihRIgZ2t0KXReWeJEVzncrd9nMZBq4=","ARC-Message-Signature":"i=1; a=rsa-sha256; d=subspace.kernel.org;\n\ts=arc-20240116; t=1778070454; c=relaxed/simple;\n\tbh=tKWJWblkI9qX1sppogZuqjBQOVIpnFXXs9qstseDqig=;\n\th=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version:\n\t Content-Type:Content-Disposition:In-Reply-To;\n b=mqa0/bnAhrKHa0URVXgeVbB9qHuOlK0U7Mf2ylmrnZDyFco272DdvK9tlYWq2BbK+XfElA0H/fM/Qsi+gCp5xWWuYk4Ibt5b3B/Z4rFLGCI9OZPIiOkHOxY20W9DwiJCwRqrSqKZVho1yNA0B1DO1v034+lVx0w6311yjU+OW8M=","ARC-Authentication-Results":"i=1; smtp.subspace.kernel.org;\n dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org\n header.b=r1FTydVA; arc=none smtp.client-ip=10.30.226.201","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org;\n\ts=k20201202; t=1778070454;\n\tbh=tKWJWblkI9qX1sppogZuqjBQOVIpnFXXs9qstseDqig=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=r1FTydVAbOcOOmW2KKhG+vrmFTJvME9MKUip9b+zXaPv8QTFuaUYQrb6JC6yi5ooJ\n\t KQWTZKkP6FxXn31T5pI4KMogBMSKvreDTBzeCo44EZpNT3upIUqNCFDFssovZnDnr9\n\t wsqpM8FLT77zQgczcGce9UOVpnVgIW310s77yBEDZoQZok7gZOIvK5d2FpCk9zhbE7\n\t nv0aloBHmWLbTjJqoQkvcwQCeCN2iM6qGXSPebJv37d+Zc3YJ69aqYDm8irRzRmnUS\n\t 0UWOLac3peOBNRFI6V64jDrXlcIpHg2BA/6SK7XT8lP99h05FBWE+obZ/3HveZpRRr\n\t /dyHtUKcUae2g==","Date":"Wed, 6 May 2026 14:27:31 +0200","From":"Johan Hovold <johan@kernel.org>","To":"Wolfram Sang <wsa+renesas@sang-engineering.com>","Cc":"Andi Shyti <andi.shyti@kernel.org>, linux-i2c@vger.kernel.org,\n\tlinux-kernel@vger.kernel.org, stable@vger.kernel.org","Subject":"Re: [PATCH 5/8] i2c: core: fix adapter registration race","Message-ID":"<afszs9k8cgo87JzB@hovoldconsulting.com>","References":"<20260505142547.795054-1-johan@kernel.org>\n <20260505142547.795054-6-johan@kernel.org>","Precedence":"bulk","X-Mailing-List":"linux-i2c@vger.kernel.org","List-Id":"<linux-i2c.vger.kernel.org>","List-Subscribe":"<mailto:linux-i2c+subscribe@vger.kernel.org>","List-Unsubscribe":"<mailto:linux-i2c+unsubscribe@vger.kernel.org>","MIME-Version":"1.0","Content-Type":"text/plain; charset=us-ascii","Content-Disposition":"inline","In-Reply-To":"<20260505142547.795054-6-johan@kernel.org>"}}]