From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-ua1-f50.google.com (mail-ua1-f50.google.com [209.85.222.50]) (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 C80F42609DC for ; Mon, 6 Jul 2026 13:50:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.50 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783345857; cv=none; b=joraeslfiqj7slfd63I9hIhprUkNXRpiFRWcXv9AbKMInouyQaFenRkjwoW4DhjK4jCtUxoHwWaIY/IU12KhvaXS+aaCl4Pjhkd9NJIAMkCbQ18Rvdwpszf2rZoZVh/zYBuGh3KE+PlpuWugkCDU2OJ5WQySNXTJfW+3NhoyBzE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783345857; c=relaxed/simple; bh=IgUdviJEsSFTVxZilVLZxpOnx1T2poIBfRVSuMDMnfY=; h=Mime-Version:Content-Type:Date:Message-Id:Subject:From:To:Cc: References:In-Reply-To; b=JAuMajxu/uBwZF/IByCf7ehrmxkfuQQ6xRs2fIzRr9IMQvQsWs/FE4zQmGHDfFT9LOKEP/Mjau5kd4vq+hnZbW+yVPzw8KdNy/xkr/sPgtPAuZagNxzDt41UROkYT/KGrOLnhX6EN6R55lDPTXh38jrxPLPsAr5Z2mjf1ySGKV4= 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=LiSraGN/; arc=none smtp.client-ip=209.85.222.50 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="LiSraGN/" Received: by mail-ua1-f50.google.com with SMTP id a1e0cc1a2514c-9691bfc9f81so1741670241.0 for ; Mon, 06 Jul 2026 06:50:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1783345855; x=1783950655; darn=vger.kernel.org; h=in-reply-to:references:cc:to:from:subject:message-id:date :content-transfer-encoding:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=rokkLcuskiz++BokjvjyUp6gnS9lDDUYlv8J+paANt0=; b=LiSraGN/jG0wIJfrhMOF6A/W81qu012u6jLq1EBtw7fWbFvueJwWJiDV+y0Ova/Dzp SC0XJ4pkdZQEPay/M0aRBbThUR5F7OdXlxkiqHgtntnsY5qOcmsMCXHUeUWiH3BEp5vi TCcBBNhDb1vKAE80ZjQgThoUGMaDq3MLzWdVppn1LEGXQxnJMZmV47qjHgZuGrkYnVrr oOGnyJyPxzsUV/dBrgJvilPCkybeOPhwV9njV4dzlE161GawPtAmKSwDxV2ux03NMi+1 SyhRre5myV/XpkustcaBrA+j/7iTgvCaMZFLQ+YRuGNndp0B/rGp3QLHbCK+rY3SAnaV DNow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783345855; x=1783950655; h=in-reply-to:references:cc:to:from:subject:message-id:date :content-transfer-encoding:mime-version:x-gm-gg:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=rokkLcuskiz++BokjvjyUp6gnS9lDDUYlv8J+paANt0=; b=oP3S2gSMoBooCs/gL2WYQwN42l1DqJG/J82kui18PDtl402QFH7ViV3Ph0QBmHm/K1 wiiA9r6DMfAgpEpmYe1+PlN1HDlATzknsNTYqMM20SyNMM0Q/C1UfCgkx49CaOde3fg6 EOlEu4Ti1llTBKeAwImTUgZB3UiScExXP4xDLYrq8UoP674t6lGJo1m7G+uRP6qnniEJ zOFev7gVOrdNXmhVQ7FNFuydribD8nhX+afikl3zmI+7r6UKbcLBdrx5c6jXez/YXyDL kLyYm/rFlFmEUZpcJ04iZyqMZNKRjcODu47B13g2PJIQjDQXhK4fOyB+zwnR6q2SMGU+ zf9Q== X-Forwarded-Encrypted: i=1; AHgh+RoOhD+B/1ax+ivVW4eeLqKsw+NWO2EJlO+KvVv1bTOftgux23DPe3Eq+8h0ivmgJg87ae9uW7Q1EsR30Ec=@vger.kernel.org X-Gm-Message-State: AOJu0Yx2+a7EF5WAogRf5F7EOnrrbXEmhIKKK7vr+KeazRvVnJqImhtU PCn5tnlNSYSuSPi4EkGl7UcBu+uUUBMgobCCM5wCBCtJ+AGWHgf45sA= X-Gm-Gg: AfdE7cmmjk7mkM97abDC8Deiu8mgVdYk2x076RBXo19Z4CBvC/qDcJQCQL8p904jWvR vnxjlIbXem3mRT1+iQnsA7fQ52/4Cpbz+nWkGvjQj/0oFGrjyZbnsWeW3bb7/d+Brf8hNc7aOLA fb6C7pKmv02Toz/H6frEZoO37xkStjcRHDdJcuBU9FocL6XPfbQm5tqCD8XKCw2cD7NcEz5j2cJ En5mVOlIvmea1XMvlt7FoacM4TRwQZHoouwaRcKZH0LqdQOmFa7xATfTDpYbLjBD/W/mY6GvwyZ wFEoX08OkZiD2nHxDVKNQFe4VcOIC7RBwfLeaGBsrrx8vn59rwdEiMthkNVGSRdw9vxuWOUq8BV CQOlzpoqO0EOmu6BdMH3DTJP+RvS1vJK254z/d9wR3TE2hHVtVLaU4nMAh/LD8W0gSaMYramAyO LU9z2sF+Y6UEPXLPa1iRndERi8MNeGkDvl2djeiXN3crYvCM9d X-Received: by 2002:a05:6102:390c:b0:738:54e8:c101 with SMTP id ada2fe7eead31-744b7c8f42emr313140137.15.1783345854585; Mon, 06 Jul 2026 06:50:54 -0700 (PDT) Received: from localhost ([186.158.238.108]) by smtp.gmail.com with ESMTPSA id a1e0cc1a2514c-96b3890d5bfsm3509015241.11.2026.07.06.06.50.49 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 06 Jul 2026 06:50:54 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 Date: Mon, 06 Jul 2026 10:50:47 -0300 Message-Id: Subject: Re: [PATCH] rust: i2c: avoid locking when calling I2cAdapter::inc_ref From: =?utf-8?q?Nicol=C3=A1s_Antinori?= To: =?utf-8?q?Nicol=C3=A1s_Antinori?= , "Gary Guo" , "Igor Korotin" Cc: "Alexandre Courbot" , "Alice Ryhl" , "Andreas Hindborg" , "Benno Lossin" , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , "Boqun Feng" , "Daniel Almeida" , "Danilo Krummrich" , "Miguel Ojeda" , =?utf-8?q?Onur_=C3=96zkan?= , "Shuah Khan" , "Tamir Duberstein" , "Trevor Gross" , , , , "Sashiko" X-Mailer: aerc 0.20.0 References: <20260615201141.8920-1-nico.antinori.7@gmail.com> In-Reply-To: Hello Gary, On Thu Jun 18, 2026 at 12:32 PM -03, Nicol=C3=A1s Antinori wrote: > Hello Gary, > > On Wed Jun 17, 2026 at 11:12 AM -03, Gary Guo wrote: >> On Mon Jun 15, 2026 at 9:10 PM BST, Nicol=C3=A1s Antinori wrote: >>> diff --git a/rust/kernel/i2c.rs b/rust/kernel/i2c.rs >>> index 624b971ca8b0..d89c42691dfe 100644 >>> --- a/rust/kernel/i2c.rs >>> +++ b/rust/kernel/i2c.rs >>> @@ -426,8 +426,11 @@ pub fn get(index: i32) -> Result> { >>> // SAFETY: Instances of `I2cAdapter` are always reference-counted. >>> unsafe impl AlwaysRefCounted for I2cAdapter { >>> fn inc_ref(&self) { >>> - // SAFETY: The existence of a shared reference guarantees that= the refcount is non-zero. >>> - unsafe { bindings::i2c_get_adapter(self.index()) }; >>> + // SAFETY: The existence of a shared reference guarantees that= the refcounts are non-zero. >>> + unsafe { >>> + bindings::__module_get((*self.as_raw()).owner); >>> + bindings::get_device(&raw mut (*self.as_raw()).dev); >> >> Instead of open coding this sequence, it would be better to add a C API = that >> does exactly this (getting another reference from existing one). > > I based this solution on the logic used in I2cClient (where only > get_device is needed) and by verifying which counters the C > implementation (i2c_get_adapter) increments. > > The i2c_get_adapter function in i2c-core-base.c performs two increments > (module increment is done by calling try_module_get, but in this case, > because inc_ref operates on an already live instance, unconditionally=20 > incrementing the count with `__module_get` should be safe). > > If I understand correctly, the idea would be to introduce a helper=20 > function on the C side, for example: > void i2c_adapter_increment(struct i2c_adap *adap); > > We would perform the increments there and call it from Rust. Using this= =20 > in inc_ref would be safe because the existence of &self guarantees that= =20 > we already have a valid, live instance. > > Is there any concern regarding this function beign exposed to the C > side? To use it safely in C, callers would have to ensure that *adap > points to a valid instance. I tried implementing this with the following C helper: ... void i2c_adapter_inc_ref(struct i2c_adapter *adapter) { __module_get(adapter->owner); get_device(&adapter->dev); } ... Calling this from Rust results in much cleaner code. The function does=20 skips a NULL check for *adapter because the AlwaysRefCounted::inc_ref=20 signature suggest a non-fallibe operation. Did I understand your=20 suggestion correctly? Also, does it make sense to declare a C API wrapper whose sole purpose is to be called from the Rust side? > >> >> That said, is there an actual user that needs this, or are we just imple= menting >> AlwaysRefCounted preemptively? > > The only user I could find at the moment is the example driver > (impl platform::Driver for SampleDriver ..) in > samples/rust/rust_i2c_client.rs. Does it make sense to move forward with this patch since there are no real users yet? Does the Sashiko diagnostic makes sense? Maybe I misunderstood it and it does not apply. Thank you! Nicol=C3=A1s