From patchwork Thu Aug 15 19:44:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Heiner Kallweit X-Patchwork-Id: 819839 Received: from mail-wm1-f53.google.com (mail-wm1-f53.google.com [209.85.128.53]) (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 4899E44C94 for ; Thu, 15 Aug 2024 19:44:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723751095; cv=none; b=DCefh3UwI2wXjXZrkdrhKCyD3OvCGcY64tuYpE0c4SI7gymnPpQGjbNR8mFL6AOIJRsFTzfJ9p8Nggd+OkeTqxkPO+Y0zGkkgz2D4dNnSU3REL9iLblyqyDtrFobmb0Q+lCwi6BhUl1A6VB9OqqVfXeO0Q68KCJAmrgV9jVcGJs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723751095; c=relaxed/simple; bh=CMpTTvHUOKqgeNHYOn5wtbgw5hnnPrV2Gp7M2hWyq2Q=; h=Message-ID:Date:MIME-Version:To:Cc:From:Subject:Content-Type; b=u47Aw19A4k2HH0IbszLVKKaiDtlZHjbsCNYqDm79UHsQhcrCDEMgYJqGbzq+bQgc22eKiwG7XlCpl7YCjcYtgVlLcyFyLnexzJHGqItLIN+ktGR92cHwti8D0/yAhTIOyoICf76qyYIZp+Jzf8nVQYbg4vzJWUetUix2DPCQ+kA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=OVZgYCBl; arc=none smtp.client-ip=209.85.128.53 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="OVZgYCBl" Received: by mail-wm1-f53.google.com with SMTP id 5b1f17b1804b1-42817f1eb1fso8905665e9.1 for ; Thu, 15 Aug 2024 12:44:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723751091; x=1724355891; darn=vger.kernel.org; h=content-transfer-encoding:autocrypt:subject:from:cc:to :content-language:user-agent:mime-version:date:message-id:from:to:cc :subject:date:message-id:reply-to; bh=LuD7XZILrrlrPF5jIGpzgheP7qDM8i1WXp8zII4FVG0=; b=OVZgYCBlaHHOzgBzT5kp4IdMTk3HfRq8HCI1hS7uFBdfs9JH4pwihTM6320VYRbewa Ih2vTS5hD/u6e7ucd7Z5h4AZHsCERTPn1gqifimVaO28d484w+KtCCjl34SKd11Q31/T rxVfk56w8fXEN0xsQl91QC6hS6BwmI0adK5oikrBbPHHB4iOmaX9c+/tkhrE8fJ/A64N b7H1YJpnKgtt568V9YaIAtPgk3mmvQeGmMn1KnwRnphaHD22B/gir+AYoMM+NSyqysZS D7o5k9i+/XYmejlDRUR3jjWtEFNZOgz5lnaTKFsrMMmu9fKodcZq27SZZmqb/3kMFKqE NOPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723751091; x=1724355891; h=content-transfer-encoding:autocrypt:subject:from:cc:to :content-language:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=LuD7XZILrrlrPF5jIGpzgheP7qDM8i1WXp8zII4FVG0=; b=mZ3PKreUQaMuHDUj5I0pzQm0TOy2YSaoncbBypj0xI3z5CGP4EoKo84cRlMUBJCwZV +frUQKO/FoLCchaocRUrhv94Ac6Y4YC+vwCsEuMIACrZQnXcgbMpMm0bY9DqhF9ZoMnz 4G/OgWBcngPk2286Sb3WjBD09KIJS0NOToSsN3ZnAObx9fMDiciwi2eCw3k3vwbX7+NA SIoWu4/TI8lxNfdKqurothbSRadCJ8JeVTWFGeCmK0Sd2n0owGId+1g1hEsNdCdrvl4b JRe/xSLqgtR0T1TrFHo8/ji1LahrGBLn1aveMfoHP+K7cB+lw0NTIp3dQRLa//D4Lsod IxIg== X-Gm-Message-State: AOJu0YzikPjzn6/WnNqC/a9vtOFjMfCuVsWWtgtY1b14j9ZU6sqwS7Dv pSuqoSgKgbHiqenT7GGCIrF1zvVVRxtkC+rJg/J1ckSResTbIKNHOKofwQ== X-Google-Smtp-Source: AGHT+IG5ZGC+Q6UEVvygqgKUCFK9NuunD4rCq+geey7w5oG0ZMURE2HP0ZQlOkjYSqgMwEowgFck/w== X-Received: by 2002:a5d:6942:0:b0:371:8eea:24b9 with SMTP id ffacd0b85a97d-3719469fc5cmr282194f8f.41.1723751091073; Thu, 15 Aug 2024 12:44:51 -0700 (PDT) Received: from ?IPV6:2a01:c23:bc0a:a000:cdc7:5daa:9b1b:b8bf? (dynamic-2a01-0c23-bc0a-a000-cdc7-5daa-9b1b-b8bf.c23.pool.telefonica.de. [2a01:c23:bc0a:a000:cdc7:5daa:9b1b:b8bf]) by smtp.googlemail.com with ESMTPSA id ffacd0b85a97d-3718984967dsm2227939f8f.34.2024.08.15.12.44.50 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 15 Aug 2024 12:44:50 -0700 (PDT) Message-ID: <32a2d535-d7c8-47da-a42f-b41d3fae972f@gmail.com> Date: Thu, 15 Aug 2024 21:44:50 +0200 Precedence: bulk X-Mailing-List: linux-i2c@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Content-Language: en-US To: Wolfram Sang Cc: "linux-i2c@vger.kernel.org" , Krzysztof Piotr Oledzki From: Heiner Kallweit Subject: [PATCH v2] i2c: core: Lock address during client device instantiation Autocrypt: addr=hkallweit1@gmail.com; keydata= xsFNBF/0ZFUBEAC0eZyktSE7ZNO1SFXL6cQ4i4g6Ah3mOUIXSB4pCY5kQ6OLKHh0FlOD5/5/ sY7IoIouzOjyFdFPnz4Bl3927ClT567hUJJ+SNaFEiJ9vadI6vZm2gcY4ExdIevYHWe1msJF MVE4yNwdS+UsPeCF/6CQQTzHc+n7DomE7fjJD5J1hOJjqz2XWe71fTvYXzxCFLwXXbBiqDC9 dNqOe5odPsa4TsWZ09T33g5n2nzTJs4Zw8fCy8rLqix/raVsqr8fw5qM66MVtdmEljFaJ9N8 /W56qGCp+H8Igk/F7CjlbWXiOlKHA25mPTmbVp7VlFsvsmMokr/imQr+0nXtmvYVaKEUwY2g 86IU6RAOuA8E0J5bD/BeyZdMyVEtX1kT404UJZekFytJZrDZetwxM/cAH+1fMx4z751WJmxQ J7mIXSPuDfeJhRDt9sGM6aRVfXbZt+wBogxyXepmnlv9K4A13z9DVLdKLrYUiu9/5QEl6fgI kPaXlAZmJsQfoKbmPqCHVRYj1lpQtDM/2/BO6gHASflWUHzwmBVZbS/XRs64uJO8CB3+V3fa cIivllReueGCMsHh6/8wgPAyopXOWOxbLsZ291fmZqIR0L5Y6b2HvdFN1Xhc+YrQ8TKK+Z4R mJRDh0wNQ8Gm89g92/YkHji4jIWlp2fwzCcx5+lZCQ1XdqAiHQARAQABzSZIZWluZXIgS2Fs bHdlaXQgPGhrYWxsd2VpdDFAZ21haWwuY29tPsLBjgQTAQgAOBYhBGxfqY/yOyXjyjJehXLe ig9U8DoMBQJf9GRVAhsDBQsJCAcCBhUKCQgLAgQWAgMBAh4BAheAAAoJEHLeig9U8DoMSycQ AJbfg8HZEK0ljV4M8nvdaiNixWAufrcZ+SD8zhbxl8GispK4F3Yo+20Y3UoZ7FcIidJWUUJL axAOkpI/70YNhlqAPMsuudlAieeYZKjIv1WV5ucNZ3VJ7dC+dlVqQdAr1iD869FZXvy91KhJ wYul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BPtIwa4tWR8B1Ma36S8Jk456k2n1id7M0LQ+eqstmp6Y+UB+pt9NX6t0Slw1NCdYTW gJezWTVKF7pmTdXszXGxlc9kTrVUz04PqPjnYbv5UWuDd2eyzGjrrFOsJEi8OK2d2j4FfF++ AzOMdW09JVqejQARAQABwsF2BBgBCAAgFiEEbF+pj/I7JePKMl6Fct6KD1TwOgwFAl/0ZFUC GwwACgkQct6KD1TwOgxUfg//eAoYc0Vm4NrxymfcY30UjHVD0LgSvU8kUmXxil3qhFPS7KA+ y7tgcKLHOkZkXMX5MLFcS9+SmrAjSBBV8omKoHNo+kfFx/dUAtz0lot8wNGmWb+NcHeKM1eb nwUMOEa1uDdfZeKef/U/2uHBceY7Gc6zPZPWgXghEyQMTH2UhLgeam8yglyO+A6RXCh+s6ak Wje7Vo1wGK4eYxp6pwMPJXLMsI0ii/2k3YPEJPv+yJf90MbYyQSbkTwZhrsokjQEaIfjrIk3 rQRjTve/J62WIO28IbY/mENuGgWehRlTAbhC4BLTZ5uYS0YMQCR7v9UGMWdNWXFyrOB6PjSu Trn9MsPoUc8qI72mVpxEXQDLlrd2ijEWm7Nrf52YMD7hL6rXXuis7R6zY8WnnBhW0uCfhajx q+KuARXC0sDLztcjaS3ayXonpoCPZep2Bd5xqE4Ln8/COCslP7E92W1uf1EcdXXIrx1acg21 H/0Z53okMykVs3a8tECPHIxnre2UxKdTbCEkjkR4V6JyplTS47oWMw3zyI7zkaadfzVFBxk2 lo/Tny+FX1Azea3Ce7oOnRUEZtWSsUidtIjmL8YUQFZYm+JUIgfRmSpMFq8JP4VH43GXpB/S OCrl+/xujzvoUBFV/cHKjEQYBxo+MaiQa1U54ykM2W4DnHb1UiEf5xDkFd4= Krzysztof reported an issue [0] which is caused by parallel attempts to instantiate the same I2C client device. This can happen if driver supports auto-detection, but certain devices are also instantiated explicitly. The original change isn't actually wrong, it just revealed that I2C core isn't prepared yet to handle this scenario. Calls to i2c_new_client_device() can be nested, therefore we can't use a simple mutex here. Parallel instantiation of devices at different addresses is ok, so we just have to prevent parallel instantiation at the same address. We can use a bitmap with one bit per 7-bit I2C client address, and atomic bit operations to set/check/clear bits. Now a parallel attempt to instantiate a device at the same address will result in -EBUSY being returned, avoiding the "sysfs: cannot create duplicate filename" splash. Note: This patch version includes small cosmetic changes to the Tested-by version, only functional change is that address locking is supported for slave addresses too. [0] https://lore.kernel.org/linux-i2c/9479fe4e-eb0c-407e-84c0-bd60c15baf74@ans.pl/T/#m12706546e8e2414d8f1a0dc61c53393f731685cc Fixes: caba40ec3531 ("eeprom: at24: Probe for DDR3 thermal sensor in the SPD case") Cc: stable@vger.kernel.org Tested-by: Krzysztof Piotr Oledzki Signed-off-by: Heiner Kallweit --- v2: - fixed comment style - use address locking for slave addresses too --- drivers/i2c/i2c-core-base.c | 28 ++++++++++++++++++++++++++++ include/linux/i2c.h | 3 +++ 2 files changed, 31 insertions(+) diff --git a/drivers/i2c/i2c-core-base.c b/drivers/i2c/i2c-core-base.c index b63f75e44..e39e8d792 100644 --- a/drivers/i2c/i2c-core-base.c +++ b/drivers/i2c/i2c-core-base.c @@ -915,6 +915,27 @@ int i2c_dev_irq_from_resources(const struct resource *resources, return 0; } +/* + * Serialize device instantiation in case it can be instantiated explicitly + * and by auto-detection + */ +static int i2c_lock_addr(struct i2c_adapter *adap, unsigned short addr, + unsigned short flags) +{ + if (!(flags & I2C_CLIENT_TEN) && + test_and_set_bit(addr, adap->addrs_in_instantiation)) + return -EBUSY; + + return 0; +} + +static void i2c_unlock_addr(struct i2c_adapter *adap, unsigned short addr, + unsigned short flags) +{ + if (!(flags & I2C_CLIENT_TEN)) + clear_bit(addr, adap->addrs_in_instantiation); +} + /** * i2c_new_client_device - instantiate an i2c device * @adap: the adapter managing the device @@ -962,6 +983,10 @@ i2c_new_client_device(struct i2c_adapter *adap, struct i2c_board_info const *inf goto out_err_silent; } + status = i2c_lock_addr(adap, client->addr, client->flags); + if (status) + goto out_err_silent; + /* Check for address business */ status = i2c_check_addr_busy(adap, i2c_encode_flags_to_addr(client)); if (status) @@ -993,6 +1018,8 @@ i2c_new_client_device(struct i2c_adapter *adap, struct i2c_board_info const *inf dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n", client->name, dev_name(&client->dev)); + i2c_unlock_addr(adap, client->addr, client->flags); + return client; out_remove_swnode: @@ -1004,6 +1031,7 @@ i2c_new_client_device(struct i2c_adapter *adap, struct i2c_board_info const *inf dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x (%d)\n", client->name, client->addr, status); + i2c_unlock_addr(adap, client->addr, client->flags); out_err_silent: if (need_put) put_device(&client->dev); diff --git a/include/linux/i2c.h b/include/linux/i2c.h index 07e33bbc9..e555d0cf8 100644 --- a/include/linux/i2c.h +++ b/include/linux/i2c.h @@ -761,6 +761,9 @@ struct i2c_adapter { struct regulator *bus_regulator; struct dentry *debugfs; + + /* 7bit address space */ + DECLARE_BITMAP(addrs_in_instantiation, 1 << 7); }; #define to_i2c_adapter(d) container_of(d, struct i2c_adapter, dev)