From nobody Tue Jun 9 01:18:43 2026 Received: from mail-vk1-f169.google.com (mail-vk1-f169.google.com [209.85.221.169]) (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 C078E2D131D for ; Sun, 24 May 2026 18:13:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779646437; cv=none; b=S2d3l1SoEiqjOopqbVR7JvAyK26D0j7apiqDMDqfj1AnYYk0I8OUmtIctQOR02JQB5mNRbRgfd91N97j1sO941FHoYctC4zKziefWXJ4Kb6+5kF2PjXWsq36LiIaNDBAgEwnP+jCfUsh/d3OO9BXJA0es7bSECqkTNuk7SkHCUc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779646437; c=relaxed/simple; bh=1ne2ZDqbJHJMuoQKGuFuMW3q7qazDc6jpKkmbQJ1Nn0=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version:Content-Type; b=Ruaa3x5NF0o6U+TDav97xG1+toCVWlLkko8BJKl8oXcPjmW4Uy/E6IzmOdHSsMgbFOEdnG5EbVUnsaBDipXIPT+Le6fK0clwH7pHk2nFnPZgrE7qokqSYJIbaP0vVw91g83F1HSU7p4tnO2wXJWisxYgBOOaeXIJYaeKjMUuZCg= 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=mzqj2z9N; arc=none smtp.client-ip=209.85.221.169 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="mzqj2z9N" Received: by mail-vk1-f169.google.com with SMTP id 71dfb90a1353d-57513733658so3021124e0c.3 for ; Sun, 24 May 2026 11:13:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1779646435; x=1780251235; 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=WCZNusicn19fLUcxp9u9pDb/isJJXf6XPw/IwzCutRQ=; b=mzqj2z9NthJdZnWuuOyJZnxpBYOeEnV9tigTMzD4Op8rZ9GioqYmA6GIZ9LG4IAoiQ lXwAW1MD0gixsGh08z0wZzYpu1Amqp8+O2JAQci6S00OXqDGPqMYiy8j3tt22w3aLjeY DylVhmQArhGFWP61yvFMQuNNzXu+CDJbSB74NAmFv3ZXgu9f84NShy2W3FygpwHduXzO 12XnUdj3YnD/gc8Seh4UZWAXhjjcYP4wL0hMYR+IWwXxta9U50czLm7Y3y61Ji4C1RYZ muWOX3zk3/L9VKwtvRzXceG5lij+nAGmz/YwrYFSPUMmVlkxCP6NAT4VLS84QOIX9M+K CRCw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779646435; x=1780251235; 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=WCZNusicn19fLUcxp9u9pDb/isJJXf6XPw/IwzCutRQ=; b=QyZ8btV4V8YIFNwUN8BSfSbktEdo87Z9Vtg+YK3mIDZxsXKFMMsXWP8l77SuS3j3/6 DoMyEOx3L8dUT82Tj7neuK1KfHllLmgrJp4ml81xJruC36m+H+8vOgBoVglzqBg9MTzA KTQgNIOB/Ss8Rr900nroXtspU7C/viO+cj5x0kslKGRJHM9yTSIpIe6KiGh6fnyPxTXK nKjeSPPSkathp/vIFRmWcMpxWpW4Zf+YWPqOodDmdyvb9RbznN7ZChIcnXjCzJHDgcUP j6qqL5avNv4qp+uEONmuppcM+v6GDm8ITbEgMsV9al3Y0pfX2+4br57NWlxiUYti7waw tfSw== X-Forwarded-Encrypted: i=1; AFNElJ+2PKayRyrst9zGJmE1q848vKnQ3VRi98O20Q6PQVWGkGMTsMN4OB4lbTXpZj6B9eFMhgoXBnT4fzAvDs8=@vger.kernel.org X-Gm-Message-State: AOJu0Yw0WLF2YKZ2DjtBWxlv7Api6FMxjEjFc+NGg6Q3bKr0DsNk+swM W30cG4FoePVs3rjyPAoD2LXnftQ88UxcVnpnyiPyNtBt0ub/2CzuANZ9NALJGgt/Vg== X-Gm-Gg: Acq92OEGDj4ApmkeCPjeMCSnSnunt6BybigY3twx3stfnR2eWK6+29bbYhcPZK+aLUn Xvuqm5+rr2VOLLARFj6Ii5YbzNkVEoDtyosUcjVwOdOmm7ty1wmrokO/aWwHaGVxlnx1Jsk5zTu QNcn07CjRpTyWHoXIn3wSoXUN1dDNrjPOyniJG3s2SDjdTAkLJjNUsWbhGaTbPKf6qo5MCbHHl3 mG/W+9+D2T4H4RwXkYgWuGYtI41qX20BWKkB1JsIl/9+4ClfwLJF9mosixtN0dZqeTUud0vwR6/ bxcA/VmnyfI/KSdtJWnuSUBLV1AoyVLc3auAJXdv8F+Q15XDcKSPGRRbjIA4MKuZhGFBxYst1uP MGX1CUdH4FM4zlcY78tQ8AEVPpZnHl4oo4rPxFsy5y9MM3Md/XN1rIHzahVJh6MFeXanOTBOX36 nV7DB7UszjA3wzaPZNMsLWGuSjWa9fU/BelJB6/0yOX+FnI5J4rKMaa67+eXi1IPP3qxWizUE= X-Received: by 2002:a05:6122:d82:b0:575:3c93:fac with SMTP id 71dfb90a1353d-5865e1bf878mr5302568e0c.3.1779646433745; Sun, 24 May 2026 11:13:53 -0700 (PDT) Received: from linux-dev ([186.158.238.108]) by smtp.gmail.com with ESMTPSA id 71dfb90a1353d-586f8f6cd40sm9836340e0c.16.2026.05.24.11.13.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 24 May 2026 11:13:53 -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 , linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org Subject: [PATCH] i2c: rust: fix I2cAdapter refcounts double increment Date: Sun, 24 May 2026 15:11:50 -0300 Message-ID: <20260524181151.24988-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. Signed-off-by: Nicol=C3=A1s Antinori --- 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> { =20 // 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::>(), + )) + }) } } =20 --=20 2.53.0