From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-yw1-f176.google.com (mail-yw1-f176.google.com [209.85.128.176]) (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 4F177448B82 for ; Tue, 11 Aug 2026 14:43:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.176 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786459419; cv=none; b=I+jc1vkF4XkIUC/i67bDMHFD0wjEHDisOkgo43ErY/TWC5IdTBQ200J3V1OdG1r7xHLtN8j8z6m/G3SZID2oZGidBOanSODyEhRm6KbW3eUR4eil7WEk1wCKss7oSwaTs1QQSLpEYTFfpH0G5tW+NEEr3ZgPNp6xg8b4C2na3q4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786459419; c=relaxed/simple; bh=1P9l62yx7Nzv3bIICI1uduDZAxBz+2R2qseDQnTY1Dc=; h=Mime-Version:Content-Type:Date:Message-Id:Cc:Subject:From:To: References:In-Reply-To; b=IQu6XKW3df9g/7pzYO9TTZaIuriRhbV0E3DOniCl5O4e1Kb0MM+xXRwFT45xGj4lttSu/e1be2xEqf62Cfpy7OzpWJX4jGOdxnA594pVweshxZj3T9obzpm+rGZdNMptcI9xorxtiA0dLG3+D1uDFBtWJuTJZ9DNecPzCsvImT8= 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=YySpyG+1; arc=none smtp.client-ip=209.85.128.176 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="YySpyG+1" Received: by mail-yw1-f176.google.com with SMTP id 00721157ae682-80c5cb9a888so34535407b3.3 for ; Tue, 11 Aug 2026 07:43:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1786459416; x=1787064216; darn=vger.kernel.org; h=in-reply-to:references:to:from:subject:cc:message-id:date :content-type:content-transfer-encoding:mime-version:from:to:cc :subject:date:message-id:reply-to:content-type; bh=lhr48ORyh4YoiC1XBSoZDdeizVPdbIlhjfg7vaMIrKA=; b=YySpyG+1QEcz7DbOX2nr+KoWpfWCCS6OAJbRNdsQHuv4/Oz3WDj94RiZy+p/m1sXcn q+jrE+ly83G8SGTyvFIy8sxd8OeTd+RyKh7i9bZwFTbRUaW57miGC+lukgMtZWRDBQhL zzLTaWRB/ZoShZY+9ax5tMHPwdDiC8BQlLmP67KDp+igawnDeofcRhOP/bCeORrfyx1A QWK0VAKCv43pMAr1lvjZcWnQGbJ+Cvlu+gWatULUcVp+i/5Y+HYy1q6tjj4iDFOh2+uw 1MGJASJTqm1DvvoV14wlEl1sNsehccWQaYDNkHT7VC2vuZdNX7LqYyUnfbf2St0dckWs AD0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786459416; x=1787064216; h=in-reply-to:references:to:from:subject:cc:message-id:date :content-type:content-transfer-encoding:mime-version:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=lhr48ORyh4YoiC1XBSoZDdeizVPdbIlhjfg7vaMIrKA=; b=gJd7H7nZX9YkrXXOUSlHO4dqMmVjxKEY019++jyGpxgxesM1O3ict+N6ROc223DXCm dTZZjifKpwq8dtl3daxroDdJtYeH6gELP9aqgVLATFQ/BSCqXzx1C0769XJgZCbeMAPY TfqbSS0+GoN/CoiW+neGxBUzhxzFloGoWB6uJTacdbwI05vCo9vBxxt1X5KAYpEK4Ucv voh+GmDdDkX9Keu81VKu8LmGzb0k3UYm4HDZZJ55cgkb+u3KWeu0jsbLtMmBPD7EmmVz J8TWh6+pKpIEbw9tj18apuB29kZ7FqRwlllGNMrRFsZ15+g/LGgZzaOWgX9ZOqdE9yri d/3w== X-Forwarded-Encrypted: i=1; AHgh+RrXZJNvW25goJV0a9/V38z9YKZAe49AgzcmMs5U9TQGQFjciL8w0dY8iojGldouvL//pTsE48CGJT6QXsE=@vger.kernel.org X-Gm-Message-State: AOJu0YxSAA/MEG0Lk9ntIRlihFeFUW7DkrJd/AsQ4fPmeHXkD6GiymyS 7prURNKIypPUzsykGiTZnyZzLNxY7GgEbMoRkAsTJeokwsmyiSen6xY= X-Gm-Gg: AR+sD13iL2PCCmh+QGHcOI1GMPtPx+100lXZNL9ZA0gooCXMwVuCcjo5CUHSetD7Gc1 J+Mcbb8uExnRqStWp/cPA6ZBKJVlK7C4J6CKIX6I5U7TyJOXw9OLFBATS9zugcXKSwg+qrLHiVT 0U/ov+OKlUqUhesIbTqdtSV988rurunTisYpRMugFx/4H9ntnTTRz9JlDAEmSOG8bWkbIC9mV6X jsrMZw7q9QybRSJzIaKdwK8/jK3A/kkZeg0bvmpztDEBAZMoW62Z49JAN7WmtCVXSp8l3O224c6 Ng6comV0NVBK6809+oxVugqM/05HOIS1uLxc8a3hTwvPPefQ5YdkfXU7Cxt1XgtFPBDqHTd2qQi iyMoBH0muCf7AogV6oqxqEDG5bd6dQeJNFpX2bDbiiaETColY6DvhSHmPw/5Bp4crtOeBuALG6e 2GXDHpRQ3IJELWzWanAkdRJHgPIzNdFqS2XLlyWS2nMDwTsuVw8rwlcxPwaoiHejOUvWy2mazlv H4fTL8OjK5DI4ntbvqii3DY X-Received: by 2002:a05:690c:9e:b0:7bd:cf35:e33b with SMTP id 00721157ae682-82f2cd0550cmr25224097b3.17.1786459416049; Tue, 11 Aug 2026 07:43:36 -0700 (PDT) Received: from localhost ([186.158.238.108]) by smtp.gmail.com with ESMTPSA id 00721157ae682-82eb5fb7a22sm11871067b3.0.2026.08.11.07.43.30 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 11 Aug 2026 07:43:35 -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: Tue, 11 Aug 2026 11:43:29 -0300 Message-Id: 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" Subject: Re: [PATCH] rust: i2c: avoid locking when calling I2cAdapter::inc_ref From: =?utf-8?q?Nicol=C3=A1s_Antinori?= To: "Igor Korotin" , "Gary Guo" X-Mailer: aerc 0.20.0 References: <20260615201141.8920-1-nico.antinori.7@gmail.com> <178628144572.5226.7072490581990266256@linux.dev> In-Reply-To: <178628144572.5226.7072490581990266256@linux.dev> On Sun Aug 9, 2026 at 10:17 AM -03, Igor Korotin wrote: > Hello Nicol=C3=A1s > > Sorry for the delay. > > Yes, worth fixing regardless of real users -- inc_ref taking a lock it > doesn't need is a real bug, not speculative work. > > Instead of adding a separate Rust-only wrapper, please consider splitting > i2c_get_adapter itself: pull the increment into a helper, export it, and > call it directly from inc_ref -- no lock, no lookup: > > +bool __i2c_adapter_get(struct i2c_adapter *adapter) > +{ > + if (try_module_get(adapter->owner)) { > + get_device(&adapter->dev); > + return true; > + } > + return false; > +} > +EXPORT_SYMBOL(__i2c_adapter_get); > + > struct i2c_adapter *i2c_get_adapter(int nr) > { > struct i2c_adapter *adapter; > > mutex_lock(&core_lock); > adapter =3D idr_find(&i2c_adapter_idr, nr); > - if (!adapter) > - goto exit; > - > - if (try_module_get(adapter->owner)) > - get_device(&adapter->dev); > - else > + if (adapter && !__i2c_adapter_get(adapter)) > adapter =3D NULL; > - > - exit: > mutex_unlock(&core_lock); > return adapter; > } > EXPORT_SYMBOL(i2c_get_adapter); Excellent! Thank you for this suggestion and the explanation! > > Heads up for v2: Trevor Chan has a patch changing AlwaysRefCounted::inc_r= ef > to an associated function (fn inc_ref(obj: &Self)). Not merged yet -- > rebase onto it if it lands first. I think it is better to wait until Trevor Chan's patch gets merged since it is in v7 [1] and already has been reviewed by some mantainers. If I send= =20 this fix and gets merged first he will probably have conflicts and have to send a v8. I'll keep an eye on the progress of that patch and once it is merged I am going to send this fix. Thank you, Nicol=C3=A1s [1] https://lore.kernel.org/rust-for-linux/20260628100731.64885-1-trev@trev= rosa.dev/T/#u