From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-qt1-f181.google.com (mail-qt1-f181.google.com [209.85.160.181]) (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 1DBE91E1DFE; Mon, 23 Jun 2025 19:18:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.181 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750706300; cv=none; b=XanOP3sCpEjVz3vTdEoBsFL09+vKAp4LqjGtslxaUCMeUgYuGvUNzFwLEK/NtDlJIPlzmNEDH+6/EaWpuM5QklOhZ16MLGoYGUiiSoXF0B2jqSS1WbTFtGV30KlokWXJiCAD/EGjsV/zjxh/PesYxrkHH7aveCY7eEKmtq5xuIE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750706300; c=relaxed/simple; bh=CCa+qFwoB/4SE/mJsQWrLXlV9mWW5ISzIjMEdBOmqMY=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=S2aBzLotOe194UGtEfVvoIS224bvEA6G1X+P9afQ4VhbtDq/UQbzbaZqU5qBYst/1ji0AGgdqmajlDI6APY1kC+E0hm8m394Gk4YKYVjJbM575I7qvl1MxDorJgYHn2BEnlU+b3s1h8oEK0PDA1JER/tNRFuz6EjCz/Z1XvsL6k= 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=XWyxxhT+; arc=none smtp.client-ip=209.85.160.181 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="XWyxxhT+" Received: by mail-qt1-f181.google.com with SMTP id d75a77b69052e-4a440a72584so50865911cf.2; Mon, 23 Jun 2025 12:18:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1750706297; x=1751311097; darn=vger.kernel.org; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :feedback-id:from:to:cc:subject:date:message-id:reply-to; bh=jpuQIftcNsqHfftfV777iY5f4fxqvsRYD0zJxsdnOrY=; b=XWyxxhT+AySUgbO6ukKov3HjUilYgiM5ACML45d7/JGyTwsjB2EuLLCX/90Qg11qjg pnQ6kk7lqk03hkp5uOgLbZyOqpVC3OSQfZXXnEvgIfhR9yWIIfp4DHlYSYPNZHSxjSr6 m7ufwD8I2vvbbdQjnITivRr+J5OZ09b+xK5aUTmak7CjhvdfMsflUDCehjxL9yL8AF9C GGgBRvR3bGMN28ol63MNxoVm6hk5YYLTZjdYYg2jr41fxmWIN1Be2NImt88ooGUDHLCB covyFBbAxcSR0cvfEU1FvgUeDPEw+kveb6++sSn4t2uierLs6dIuOi/rGRwg3MlYX3jy ssbA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1750706297; x=1751311097; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :feedback-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=jpuQIftcNsqHfftfV777iY5f4fxqvsRYD0zJxsdnOrY=; b=IgYj0v+1hwH02FcN0fQiNdZTiQ9/MEqRxv209xlC9+/vgyTGFRHGUgw/awyFCd+unK dcDuDD8C9bj9cO4A3ezS/flejQosapnRqZLXEEFtpo6cmvRjJpP1YhTxKDqDSRDEQF7c qyQGy39o32ZMYK0WNnWADJJg/1exTA+rpSYcWtfF+hEEA4Ncub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aqm/7AYzXA/NCwVetvA+PYk1LTyOkW68YxjyQbu07rbo67Ap4gWE/PQS9AvQbcpCw== X-Received: by 2002:a05:622a:246:b0:4a4:31a0:68d1 with SMTP id d75a77b69052e-4a77a24c1b1mr203720071cf.21.1750706296698; Mon, 23 Jun 2025 12:18:16 -0700 (PDT) Received: from fauth-a1-smtp.messagingengine.com (fauth-a1-smtp.messagingengine.com. [103.168.172.200]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-4a779d6635fsm42084201cf.20.2025.06.23.12.18.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Jun 2025 12:18:16 -0700 (PDT) Received: from phl-compute-11.internal (phl-compute-11.phl.internal [10.202.2.51]) by mailfauth.phl.internal (Postfix) with ESMTP id BBDE21200043; Mon, 23 Jun 2025 15:18:15 -0400 (EDT) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-11.internal (MEProxy); Mon, 23 Jun 2025 15:18:15 -0400 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeffedrtddvgddujeekgecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpuffrtefokffrpgfnqfghnecuuegr ihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjug hrpeffhffvvefukfhfgggtugfgjgesthekredttddtjeenucfhrhhomhepuehoqhhunhcu hfgvnhhguceosghoqhhunhdrfhgvnhhgsehgmhgrihhlrdgtohhmqeenucggtffrrghtth gvrhhnpeevgffhueevkedutefgveduuedujeefledthffgheegkeekiefgudekhffggeel feenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegsoh hquhhnodhmvghsmhhtphgruhhthhhpvghrshhonhgrlhhithihqdeiledvgeehtdeigedq udejjeekheehhedvqdgsohhquhhnrdhfvghngheppehgmhgrihhlrdgtohhmsehfihigmh gvrdhnrghmvgdpnhgspghrtghpthhtohepudelpdhmohguvgepshhmthhpohhuthdprhgt phhtthhopehlohhsshhinheskhgvrhhnvghlrdhorhhgpdhrtghpthhtoheprghlihgtvg hrhihhlhesghhoohhglhgvrdgtohhmpdhrtghpthhtohepuggrkhhrsehkvghrnhgvlhdr ohhrghdprhgtphhtthhopegurghnihgvlhdrrghlmhgvihgurgestgholhhlrggsohhrrg drtghomhdprhgtphhtthhopehojhgvuggrsehkvghrnhgvlhdrohhrghdprhgtphhtthho pegrlhgvgidrghgrhihnohhrsehgmhgrihhlrdgtohhmpdhrtghpthhtohepghgrrhihse hgrghrhihguhhordhnvghtpdhrtghpthhtohepsghjohhrnhefpghghhesphhrohhtohhn mhgrihhlrdgtohhmpdhrtghpthhtoheprgdrhhhinhgusghorhhgsehkvghrnhgvlhdroh hrgh X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 23 Jun 2025 15:18:15 -0400 (EDT) Date: Mon, 23 Jun 2025 12:18:14 -0700 From: Boqun Feng To: Benno Lossin Cc: Alice Ryhl , Danilo Krummrich , Daniel Almeida , Miguel Ojeda , Alex Gaynor , Gary Guo , =?iso-8859-1?Q?Bj=F6rn?= Roy Baron , Andreas Hindborg , Trevor Gross , Greg Kroah-Hartman , "Rafael J. Wysocki" , Thomas Gleixner , Bjorn Helgaas , Krzysztof =?iso-8859-1?Q?Wilczy=B4nski?= , linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-pci@vger.kernel.org Subject: Re: [PATCH v4 3/6] rust: irq: add support for non-threaded IRQs and handlers Message-ID: References: <20250608-topics-tyr-request_irq-v4-0-81cb81fb8073@collabora.com> <20250608-topics-tyr-request_irq-v4-3-81cb81fb8073@collabora.com> Precedence: bulk X-Mailing-List: rust-for-linux@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: On Mon, Jun 23, 2025 at 10:31:16AM -0700, Boqun Feng wrote: > On Mon, Jun 23, 2025 at 05:26:14PM +0200, Benno Lossin wrote: > > On Mon Jun 23, 2025 at 5:10 PM CEST, Alice Ryhl wrote: > > > On Mon, Jun 9, 2025 at 12:47 PM Danilo Krummrich wrote: > > >> On Sun, Jun 08, 2025 at 07:51:08PM -0300, Daniel Almeida wrote: > > >> > + dev: &'a Device, > > >> > + irq: u32, > > >> > + flags: Flags, > > >> > + name: &'static CStr, > > >> > + handler: T, > > >> > + ) -> impl PinInit + 'a { > > >> > + let closure = move |slot: *mut Self| { > > >> > + // SAFETY: The slot passed to pin initializer is valid for writing. > > >> > + unsafe { > > >> > + slot.write(Self { > > >> > + inner: Devres::new( > > >> > + dev, > > >> > + RegistrationInner { > > >> > + irq, > > >> > + cookie: slot.cast(), > > >> > + }, > > >> > + GFP_KERNEL, > > >> > + )?, > > >> > + handler, > > >> > + _pin: PhantomPinned, > > >> > + }) > > >> > + }; > > >> > + > > >> > + // SAFETY: > > >> > + // - The callbacks are valid for use with request_irq. > > >> > + // - If this succeeds, the slot is guaranteed to be valid until the > > >> > + // destructor of Self runs, which will deregister the callbacks > > >> > + // before the memory location becomes invalid. > > >> > + let res = to_result(unsafe { > > >> > + bindings::request_irq( > > >> > + irq, > > >> > + Some(handle_irq_callback::), > > >> > + flags.into_inner() as usize, > > >> > + name.as_char_ptr(), > > >> > + slot.cast(), > > >> > + ) > > >> > + }); > > >> > + > > >> > + if res.is_err() { > > >> > + // SAFETY: We are returning an error, so we can destroy the slot. > > >> > + unsafe { core::ptr::drop_in_place(&raw mut (*slot).handler) }; > > >> > + } > > >> > + > > >> > + res > > >> > + }; > > >> > + > > >> > + // SAFETY: > > >> > + // - if this returns Ok, then every field of `slot` is fully > > >> > + // initialized. > > >> > + // - if this returns an error, then the slot does not need to remain > > >> > + // valid. > > >> > + unsafe { pin_init_from_closure(closure) } > > >> > > >> Can't we use try_pin_init!() instead, move request_irq() into the initializer of > > >> RegistrationInner and initialize inner last? > > > > > > We need a pointer to the entire struct when calling > > > bindings::request_irq. I'm not sure this allows you to easily get one? > > > I don't think using container_of! here is worth it. > > > > There is the `&this in` syntax (`this` is of type `NonNull`): > > > > try_pin_init!(&this in Self { > > inner: Devres::new( > > dev, > > RegistrationInner { > > irq, > > cookie: this.as_ptr().cast(), > > }, > > GFP_KERNEL, > > )?, > > handler, > > _pin: { > > to_result(unsafe { > > bindings::request_irq( > > irq, > > Some(handle_irq_callback::), > > flags.into_inner() as usize, > > name.as_char_ptr(), > > slot.as_ptr().cast(), > > this is "this" instead of "slot", right? > > > ) > > })?; > > PhantomPinned > > }, > > }) > > > > Last time around, I also asked this question and you replied with that > > we need to abort the initializer when `request_irq` returns false and > > avoid running `Self::drop` (thus we can't do it using `pin_chain`). > > > > I asked what we could do instead and you mentioned the `_: {}` > > initializers and those would indeed solve it, but we can abuse the > > `_pin` field for that :) > > > > Hmm.. but if request_irq() fails, aren't we going to call `drop` on > `inner`, which drops the `Devres` which will eventually call > `RegistrationInner::drop()`? And that's a `free_irq()` without > `request_irq()` succeeded. > This may however work ;-) Because at `request_irq()` time, all it needs is ready, and if it fails, `RegistrationInner` won't construct. try_pin_init!(&this in Self { handler, inner: Devres::new( dev, RegistrationInner { // Needs to use `handler` address as cookie, same for // request_irq(). cookie: &raw (*(this.as_ptr().cast()).handler), irq: { to_result(unsafe { bindings::request_irq(...) })?; irq } }, GFP_KERNEL, )?, _pin: PhantomPinned }) Regards, Boqun > Regards, > Boqun > > > --- > > Cheers, > > Benno