From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-qk1-f178.google.com (mail-qk1-f178.google.com [209.85.222.178]) (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 9E5A22D23AB; Tue, 24 Jun 2025 14:42:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.178 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750776130; cv=none; b=gjke3TGD4pCd2y3GL986Af2eKNW++2IDHGkGDhUa714Lfyy9yPvTM0/Bt/v7UvcH8FkRDiQgc+xVjt+b6opF+rKwtf1QoOjzsYX750b8CLddEB9m1MM0k9ZggbtdJjdRDQp8iP9dMLWCvQA4UEgtTf3pLn/BxC6iDaJQu4dJ+3M= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750776130; c=relaxed/simple; bh=V1M4ORKLJLIjG57LATbk5936tLU6u1xz5jO3Mt7+sNI=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=dKHWagWPL1pccvyY25iAQ0OJyhdoyNtIPb6o6uuMe2FREIXDDP1KNPI20s+2pqR5p0Pj5irIj7a5zrSM60n2//Dl57HGs5KROIUnnY5sTkPKVml+lGngfU8imO3SmdnoSkH++d4RMZZOkSk5jMPvVSpEchSeLbJlEroeZZQ4kmw= 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=bfLCv5z6; arc=none smtp.client-ip=209.85.222.178 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="bfLCv5z6" Received: by mail-qk1-f178.google.com with SMTP id af79cd13be357-7d402c901cbso40839885a.3; Tue, 24 Jun 2025 07:42:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1750776127; x=1751380927; 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=GtB/3XhJ46B/NpEKgpZNrT4UMCjsycvYug2oSPbfSqo=; b=bfLCv5z6p9h9cd4vlfyOSHf9Y7C5GbbuzRMnmn8AHq13lY8BnZTQtvBkKKIFcXWZSX IpybrLjVsQd/XT7VpEPThd4w3HQDygtLF5L1K9Z1I5pdRGzyxYlKvQu1UMRB7BYHtnjC SLRyBED0EJkNo+9x48OdkmDL+W8rmuVQOLNFTsrdWX87iaHH6IQzYAtm6CZF6I+jD6Cs Q4HL3WkqlAskorqTISUOmadnppOGH32LN56NRHiiGpJlA0menrXO2xyLzBkhok6dmHK+ RvKNlShuC1pyf9J1LDCwdKrKo0n7O2a/Of6dImVdMelmn5EqslZQRzpFD8cxvDi1O3vq g1NQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1750776127; x=1751380927; 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=GtB/3XhJ46B/NpEKgpZNrT4UMCjsycvYug2oSPbfSqo=; b=BT7WVBYjvI+zCRaraP1utQbih17HtwEXAYjYcdBnqw56zJgRkTe/DGCFyRNnOycsCt 7eMFrCTJHLkeL4aBqpBPB8q/prMKWjcBWH6lmcqDCXjPIwKyrrZqpnMCDtrptGhHDv4K GeSbT1ntrjUeAik0msF6LiWeokMc9sDYOaGZmkRWC0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r07VLKEUP7DR9SwamJYuczpKcnyFMNqjHFRpwoNy5lg3m6iRp1rsXysqsgRKw== X-Received: by 2002:a05:620a:1a06:b0:7d3:abd6:3cab with SMTP id af79cd13be357-7d3f9931d6fmr2391463585a.41.1750776127335; Tue, 24 Jun 2025 07:42:07 -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 af79cd13be357-7d3f99a6415sm507451085a.40.2025.06.24.07.42.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Jun 2025 07:42:06 -0700 (PDT) Received: from phl-compute-12.internal (phl-compute-12.phl.internal [10.202.2.52]) by mailfauth.phl.internal (Postfix) with ESMTP id 544901200043; Tue, 24 Jun 2025 10:42:06 -0400 (EDT) Received: from phl-mailfrontend-02 ([10.202.2.163]) by phl-compute-12.internal (MEProxy); Tue, 24 Jun 2025 10:42:06 -0400 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeffedrtddvgddvtdduiecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpuffrtefokffrpgfnqfghnecuuegr ihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjug hrpeffhffvvefukfhfgggtugfgjgesthekredttddtjeenucfhrhhomhepuehoqhhunhcu hfgvnhhguceosghoqhhunhdrfhgvnhhgsehgmhgrihhlrdgtohhmqeenucggtffrrghtth gvrhhnpeevgffhueevkedutefgveduuedujeefledthffgheegkeekiefgudekhffggeel feenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegsoh hquhhnodhmvghsmhhtphgruhhthhhpvghrshhonhgrlhhithihqdeiledvgeehtdeigedq udejjeekheehhedvqdgsohhquhhnrdhfvghngheppehgmhgrihhlrdgtohhmsehfihigmh gvrdhnrghmvgdpnhgspghrtghpthhtohepudelpdhmohguvgepshhmthhpohhuthdprhgt phhtthhopegrlhhitggvrhihhhhlsehgohhoghhlvgdrtghomhdprhgtphhtthhopehloh hsshhinheskhgvrhhnvghlrdhorhhgpdhrtghpthhtohepuggrnhhivghlrdgrlhhmvghi uggrsegtohhllhgrsghorhgrrdgtohhmpdhrtghpthhtohepuggrkhhrsehkvghrnhgvlh drohhrghdprhgtphhtthhopehojhgvuggrsehkvghrnhgvlhdrohhrghdprhgtphhtthho pegrlhgvgidrghgrhihnohhrsehgmhgrihhlrdgtohhmpdhrtghpthhtohepghgrrhihse hgrghrhihguhhordhnvghtpdhrtghpthhtohepsghjohhrnhefpghghhesphhrohhtohhn mhgrihhlrdgtohhmpdhrtghpthhtoheprgdrhhhinhgusghorhhgsehkvghrnhgvlhdroh hrgh X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 24 Jun 2025 10:42:05 -0400 (EDT) Date: Tue, 24 Jun 2025 07:42:05 -0700 From: Boqun Feng To: Alice Ryhl Cc: Benno Lossin , Daniel Almeida , Danilo Krummrich , 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: Precedence: bulk X-Mailing-List: linux-pci@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 Tue, Jun 24, 2025 at 07:33:35AM -0700, Boqun Feng wrote: > On Tue, Jun 24, 2025 at 02:50:23PM +0100, Alice Ryhl wrote: > > On Tue, Jun 24, 2025 at 1:46 PM Benno Lossin wrote: > > > > > > On Tue Jun 24, 2025 at 2:31 PM CEST, Daniel Almeida wrote: > > > > On 23 Jun 2025, at 16:28, Benno Lossin wrote: > > > >> On Mon Jun 23, 2025 at 9:18 PM CEST, Boqun Feng wrote: > > > >>> 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 > > > >>> }) > > > >> > > > >> Well yes and no, with the Devres changes, the `cookie` can just be the > > > >> address of the `RegistrationInner` & we can do it this way :) > > > >> > > > >> --- > > > >> Cheers, > > > >> Benno > > > > > > > > > > > > No, we need this to be the address of the the whole thing (i.e. > > > > Registration), otherwise you can’t access the handler in the irq > > > > callback. > > You only need the access of `handler` in the irq callback, right? I.e. > passing the address of `handler` would suffice (of course you need > to change the irq callback as well). > > > > > > > Gotcha, so you keep the cookie field, but you should still be able to > > > use `try_pin_init` & the devres improvements to avoid the use of > > > `pin_init_from_closure`. > > > > It sounds like this is getting too complicated and that > > `pin_init_from_closure` is the simpler way to go. > > Even if we use `pin_init_from_closure`, we still need the other > `try_pin_init` anyway for `Devres::new()` (or alternatively we can > implement a `RegistrationInner::new()`). > > Below is what would look like with the Devres changes in mind: > > > try_pin_init!(&this in Self { > handler, > inner: <- Devres::new( > dev, > try_pin_init!( RegistrationInner { > // Needs to use `handler` address as cookie, same for > // request_irq(). > cookie: &raw (*(this.as_ptr().cast()).handler), > // @Benno, would this "this" work here? > irq: { > to_result(unsafe { bindings::request_irq(...) })?; > irq > } > }), > )?, > _pin: PhantomPinned > }) > > Never mind, `dev` is a `Device` so it cannot be unbounded during the call ;-) Regards, Boqun > Besides, working on this made me realize that we have to request_irq() > before `Devres::new()`, otherwise we may leak the irq resource, > considering the follow code from the current `pin_init_from_closure` > approach: > > 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, > }) > }; > > `dev` can be unbound at here, right? If so, the devm callback will > revoke the `RegistrationInner`, `RegistrationInner::drop()` will then > call `free_irq()` before `request_irq()`, the best case is that we would > request_irq() with no one going to free it. > > // 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(), > ) > }); > ... > } > > So seems to me the order of initialization has to be: > > 1. Initialize the `handler`. > 2. `request_irq()`, i.e initialize the `RegistrationInner`. > 3. `Devres::new()`, i.e initialize the `Devres`. > > Regards, > Boqun