From nobody Mon Jun 8 07:24:46 2026 Received: from mail-yx1-f47.google.com (mail-yx1-f47.google.com [74.125.224.47]) (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 2600138F633 for ; Sun, 31 May 2026 18:23:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.224.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780251822; cv=none; b=C0azewcAO4czmcEntupp8bht6sJpm6WHRRfaPTLbM0diIzP1HtsgdAcf1DXIsfReqdFzjo8HXbO1+Do9T2qv0Tx6GevNRUlGJG1O6eiwP5/u/x1m/jCziTYHHteLSHsx2Hn18HB58I7XIGUbyEGPq83vNjvqaOM40NWzK7/S8Z0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780251822; c=relaxed/simple; bh=duauwQPdNOaCX8n/BA/ifDVSSVZPB4Cn2kr/4bwpX0Y=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version:Content-Type; b=juF04zqRNfzBTqdvyb5uW7cR6EwNqYXPOJVUndBm2ShUnTm8H73lfzqoFwSjG9ooULxU3H4xxNyx7lYaVYJPZF7ylA2PW/UwgaysrfZaMfixP3Q0fwUHoHlekL9zcYPqbAPmm+gci7vVL327KIq6J8prjwE+bmNZYnZRPz7jHGg= 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=WjSlTWkE; arc=none smtp.client-ip=74.125.224.47 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="WjSlTWkE" Received: by mail-yx1-f47.google.com with SMTP id 956f58d0204a3-66061993294so1046640d50.3 for ; Sun, 31 May 2026 11:23:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1780251820; x=1780856620; 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=HZU06z9RTZCXfgLkK92JhL7C0HWwV9dSVvTqen1OyJs=; b=WjSlTWkEdLcQYDdnjuP32ZAoffPgM/40D+gDsrAtoNhD40xMTZMiOqHMYA+7IqMICP wBvQRo3MXARGrGDu9kwAvezqquKXFewypvGgZESgTIdXdRHNOa6rt4mH0dzlYMBQJFiG ACO0oKksbLklWE6+IH1GY9Tm16bJYBUF1uoT/g0zjESPo2NzQK6bFHU2KDEk2XvyjFDz JRONKWM3H/PlQLsuJnKUdp9BvFB7tXJ41LCOkOB0TLzUqPhkykWkDuiryK6pOyB/hZiX GPeA/PI30eKBd4Jz2mzMzEjYJ17MnzYYyrN5/DCejALUy3BWBncW+dms6/M5KiNoxwc4 1JYQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780251820; x=1780856620; 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=HZU06z9RTZCXfgLkK92JhL7C0HWwV9dSVvTqen1OyJs=; b=Ks0WgOsfsmZDatG4K9LE+G5toqpnq6aT1fVylJbAuoyUnsDcLmeI5loYOPHa2B/o7b d9GWSudEYbT3xUEy7dfuCV3OojDA7ZEg3NpaIg7hISpFq+xf5X1BkwLdEEPhYveeL8o3 OdeTxMAyxMmWE2nBxY49/nJHskWhJBLgcvqPghNAlgS/8BVM0tQWbX7vie+YJwJ5kn0J +YpWUa63XaF3zFUE8xANE+GDOs8GkGHwi9LSFE8apGrNoWku56ymcdXkpVr0hbvS0CO5 caG4kgUubNPSx6BJb7O8KVMawH5AiwETaAnlDf5X85Z5fKddfxn9JEpa5YV5IDqVcwZG HIuw== X-Forwarded-Encrypted: i=1; AFNElJ+bl93HCn8fZMiWB/Wq4iluYzLQTAnpXwGHvuilrZrKoYsw3QJB9NV+IbjeTBZ24gKNT/p49wmSKVDlOQs=@vger.kernel.org X-Gm-Message-State: AOJu0YzAn1OYxWv0v5UnW/8rqlCcU+HyUkQGE1b0sLvZAOOGyEdZje8n uOWMuF/cnn9BsKgpcycE2w/CFd8TI45QA0UGPCdYNXRjruQ1WZQZBmg= X-Gm-Gg: Acq92OHkwxuXEfUgJVItyZXFnsxbBbnGDALGu6zf/fv4TQN14tD3PgFfcDQs6ZYZCzg rZH3h47eEcXAkA3A8DhMNidEnSmLBacrD6Wpoftf8h4t/601QW4UYCKWR4T0+QcHwtz61z01NFv B7gtL/w76dSaWJAmJylTO5NXHgAto5SdPl8fAxRPTy1/Pat6qavnjDETAKpRVOrP5h7g8UCQTDl gpmuToxF39mi+NJDXVaAa9TzDcqT6ieHx9RltW3Dv0tO7n8WlWYFLmLEAxvk3tUp+FH3lb+y74+ czfcmjSuCRlVOowFQJaaOUzi5PplomhroswfN79LVu3PAe6RA+ex3HIZ1n2sKhayPqZoqx4Gc0y h7OdUFllocXrYOrenwPchKwxHcIhJeEN7KxGufYMv5MNomn87vx2UfvEG06RDfc/Iaay3xGIcKO yoBChAhBv1e55Gv3R0efLfLlm86ifHT6gc0hIrmvCpUiwhxbqeX9r0l2bnBKKRx7fpsbiuT1o= X-Received: by 2002:a05:690e:1243:b0:660:6ead:8993 with SMTP id 956f58d0204a3-6606eae2101mr3974894d50.27.1780251820159; Sun, 31 May 2026 11:23:40 -0700 (PDT) Received: from linux-dev ([186.158.238.108]) by smtp.gmail.com with ESMTPSA id 956f58d0204a3-6606970912fsm2399747d50.4.2026.05.31.11.23.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 31 May 2026 11:23:39 -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 v3] i2c: rust: fix I2cAdapter refcounts double increment Date: Sun, 31 May 2026 15:23:07 -0300 Message-ID: <20260531182319.21777-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 increment to the reference counts. 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 Reviewed-by: Igor Korotin --- v3: - Remove unnecessary `.as_ptr()` and `NonNull` rewrap. - Add SAFETY comment for `ARef::from_raw`. - Small change in commit message to make it clearer. v2: - Add missing Link tag to commit message. - https://lore.kernel.org/rust-for-linux/20260526194734.14378-1-nico.antin= ori.7@gmail.com v1: https://lore.kernel.org/rust-for-linux/20260524181151.24988-1-nico.anti= nori.7@gmail.com rust/kernel/i2c.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/rust/kernel/i2c.rs b/rust/kernel/i2c.rs index 7b908f0c5a58..c084a45b1916 100644 --- a/rust/kernel/i2c.rs +++ b/rust/kernel/i2c.rs @@ -405,7 +405,9 @@ 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() }) + // `i2c_get_adapter` returned the adapter with an incremented refc= ount, which we pass to + // the `ARef`. + Ok(unsafe { ARef::from_raw(adapter.cast::>()) }) } } -- 2.53.0