From nobody Mon Jun 8 20:53:21 2026 Received: from mail-vk1-f174.google.com (mail-vk1-f174.google.com [209.85.221.174]) (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 60C642030A for ; Tue, 26 May 2026 19:47:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779824868; cv=none; b=UC/xjorgQaW0tVukV8s5/Uu5W5zMIKLULAwWkHGg33wzOUpA4gBYuojht4Gp/l4TjrocN+/URvla0AnfDJxwVHQA+eb0hO05vVYoxTNxoAIil5uEoq8GuEd9REIOQGZ7vRyAfZxuTpYHnVIF7rryJ7S+RKaWZJsPtElTfzRogk4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779824868; c=relaxed/simple; bh=b/6M3t6zgpKHWKeqiCxOD4da11OGS04/0/XWAp+5W+o=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version:Content-Type; b=sYr2ZrsfRqaLhB68HvHdo8GYXF8wS7EyvBBoRTFzogyO0F3OJAmmDCmlLjpNG/QonaHipEQpxGGwU1Oz0ZJFmmlJ9GzpBRSdWZuViRq8iT06wQTA7YULi2d+214qpyhqdMD9cPUajVuJSEFeNX0KFcqT91mhuCU4bNOM3eFGsSM= 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=XzUyb8r1; arc=none smtp.client-ip=209.85.221.174 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="XzUyb8r1" Received: by mail-vk1-f174.google.com with SMTP id 71dfb90a1353d-57513a91514so3682178e0c.2 for ; Tue, 26 May 2026 12:47:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1779824866; x=1780429666; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=hlze5hdsepZaytQ8U+U6HGWJPcTingin6A/UL10jrG8=; b=XzUyb8r1+ixMDW74zv5/njWOJsBH4uZ8e0ugn/3YZ+qdt0IhRkOOTHp5nByu4xJcvw StlzEr0hUrGYxMP1+HWHSyg6WezhsA2X2oOA3VBQcsw09lmjR+PIhmkc2Gi886xOZnHt bOFhM6PV3xawU5/L3y1hnn4hwxmzZj2Prc4AkmSvPDnS+cb19O69QS3ukH0iYAPMspY7 QVripwONsi5jSJdUeeseXUNdP/8hw8ZmilVVViLLNi3Ytlv9s+SmLBcylnhGA2Y8W0eh cIPvyGB8SUUsz9ItCtbyEGLno4kCVuEpb1SB9uo/zcnPiPoSYu1n+EHmFHStImF21poW mcfQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779824866; x=1780429666; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=hlze5hdsepZaytQ8U+U6HGWJPcTingin6A/UL10jrG8=; b=k/ydpQwrDPF9JyTnbl+BDaSgruL3RV0NO1EGORBufHI2ZPdRiA6YDYHczlDuRTjspD ApOY5REr14BTHz5n0oG3dJZT2CvcncyfZvNMu0QZfsh33080MD7IdoJYHfGVnuB3PHzn O3yjdGMN0LbvM7g1x57wV//pjzrM+JKnA0NI/N9ktZ6jRuikiQF3jUJ1bJFDEHBy1Msh 5LfkBMBKVxXhBNbrj45vWzuLyzc9fBA1829D9tiuP26VlkxnEZiH5QSq0xweQF/3uv10 w7Nm+SEqk6atV3X+IeaVifan0HeIH5zZumvmHr4QOhA5sOalJSJVK0j2AnU30vopty9T b1Gg== X-Forwarded-Encrypted: i=1; AFNElJ/hgAxaGdyjT2sj3WsZs5VUmp08U6lmriO+fLdwUPcGgeVHmV7mfnX8QvyXJZoiDtjJFyEz+FAbt20nt70=@vger.kernel.org X-Gm-Message-State: AOJu0YyJu1MOGiHCBZxruNsr7+/sgbvXL4IbJfJ21ERtxYm05wQKVlkC kzUrzD7F9ylZNPhEhtQdxX11zjmd1SaWQ7HA6JyOfORNfFRLCOcO2HI= X-Gm-Gg: Acq92OENFxFCcSEOLENZZpEku5ZYHEA9UHSCkKN7opi5H/aDVDdAdTcP1SVmn15cpy4 Fmp+nF1FTj4KKcttfrqJJZ5UtJQZ0FGS/jtTI/TfFopteyYbd6GENLD8hUfWxN+l2Hw4BOPEgiw bQo3Ye9T9w3Nz3iTQocLT0qp9Bo85stbLKiQJgMFAR40vawOtnUVD3I0v6AU4ztW+gyjUtcedrR iWE938hOFLd4GubE5oQgxfQ1qpGGpbRSdHG2VatNZvJVPV04KuqE6f+URkdrWDiB9c8FPeS3D8h VNe64x+TntKHuwohBawLzmOL8OL9BhytKYAXRmL2k4GEjryaKT5Wht8ftir4cCpWPTfFDNYsBtX Pd396a8JrTWtTxg3zR+26OlDHYhVqUZiFtGHvUBk6MiYLQfA7HM/6yLFtuKw9fCuPriVpPycR4A 3uWhILZ98yrwZ3Nvzec4YAcafAMbHBYqmQRiutqzHANygwKX5fX/ksEzra04gmLMQ8axCe29Y= X-Received: by 2002:a05:6122:1796:b0:56c:db8b:504e with SMTP id 71dfb90a1353d-58663966692mr10651268e0c.13.1779824866223; Tue, 26 May 2026 12:47:46 -0700 (PDT) Received: from linux-dev ([186.158.238.108]) by smtp.gmail.com with ESMTPSA id 71dfb90a1353d-586f8f6cd40sm18487330e0c.16.2026.05.26.12.47.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 May 2026 12:47:45 -0700 (PDT) From: =?UTF-8?q?Nicol=C3=A1s=20Antinori?= To: Igor Korotin Cc: =?UTF-8?q?Nicol=C3=A1s=20Antinori?= , Alice Ryhl , Andreas Hindborg , Benno Lossin , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Boqun Feng , Daniel Almeida , Danilo Krummrich , Gary Guo , Miguel Ojeda , Trevor Gross , =?UTF-8?q?Onur=20=C3=96zkan?= , linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org Subject: [PATCH v2] i2c: rust: fix I2cAdapter refcounts double increment Date: Tue, 26 May 2026 16:47:26 -0300 Message-ID: <20260526194734.14378-1-nico.antinori.7@gmail.com> X-Mailer: git-send-email 2.53.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable When `I2cAdapter::get` executes, it first calls `bindings::i2c_get_adapter()` which increments the device and module reference counts. It then takes a reference to the raw pointer and converts it to an `ARef` via `.into()`. The implementation of `From<&T> for ARef` where `T: AlwaysRefCounted` unconditionally calls `T::inc_ref()`. This leads to a second call to `bindings::i2c_get_adapter()` inside `I2cAdapter::inc_ref()`. Since the returned `ARef` will only release a single reference when dropped via `dec_ref()`, this leaks one device and module reference count on every call. This fix was suggested by sashiko.dev. Link: https://sashiko.dev/#/patchset/20260521190937.248904-1-nico.antinori.= 7@gmail.com Signed-off-by: Nicol=C3=A1s Antinori --- Changelog: v2: add missing Link tag to commit message v1: https://lore.kernel.org/rust-for-linux/20260524181151.24988-1-nico.anti= nori.7@gmail.com rust/kernel/i2c.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/rust/kernel/i2c.rs b/rust/kernel/i2c.rs index 7b908f0c5a58..ca6476cf4399 100644 --- a/rust/kernel/i2c.rs +++ b/rust/kernel/i2c.rs @@ -405,7 +405,11 @@ pub fn get(index: i32) -> Result> { // SAFETY: `adapter` is non-null and points to a live `i2c_adapter= `. // `I2cAdapter` is #[repr(transparent)], so this cast is valid. - Ok(unsafe { (&*adapter.as_ptr().cast::>= ()).into() }) + Ok(unsafe { + ARef::from_raw(NonNull::new_unchecked( + adapter.as_ptr().cast::>(), + )) + }) } } -- 2.53.0