From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailout2.w1.samsung.com (mailout2.w1.samsung.com [210.118.77.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C31B42BE62A for ; Fri, 11 Jul 2025 12:36:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=210.118.77.12 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752237403; cv=none; b=PrglwSFgxUhDKwGRtcb0G4fjSktfz1kjA0TXXYXIOvrQlnVgYNSe4Or0dT9UK57FEW8l7kVlFQ1eciC8SJm364kPYYkgTIleT4wgKHWrhq1Y02RYBN7voajrfbx3HccZQezhPPGO9FGFLe8VlXgRTOW1/EhEsUr/cpNP5LgZaZg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752237403; c=relaxed/simple; bh=3k7zeyqd7SmF+I9Quix2UZLh1NAmpJ08rR0/1iKfNfc=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:From:In-Reply-To: Content-Type:References; b=UXxoYM/egU5zIqOZwnUcuTAZwFO0pL3JXv+3VAjjWj5FWm9DFbG4qi0lfx8PzQgjuca9P/uPNEHGqZYduQeVO/WJRivdbW6bdRLaPd18x5FaDmT1s/sf85ucwYM5aZTlaFqisQrFX//PYHWLst/VFQKWHawwxJ2Apd18dEn9fXU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=samsung.com; spf=pass smtp.mailfrom=samsung.com; dkim=pass (1024-bit key) header.d=samsung.com header.i=@samsung.com header.b=C1irTKJK; arc=none smtp.client-ip=210.118.77.12 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=samsung.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=samsung.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=samsung.com header.i=@samsung.com header.b="C1irTKJK" Received: from eucas1p1.samsung.com (unknown [182.198.249.206]) by mailout2.w1.samsung.com (KnoxPortal) with ESMTP id 20250711123640euoutp023a62e32419e1df22099af935cc97bf7a~RMqGwzFmc0786907869euoutp02r for ; Fri, 11 Jul 2025 12:36:40 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 mailout2.w1.samsung.com 20250711123640euoutp023a62e32419e1df22099af935cc97bf7a~RMqGwzFmc0786907869euoutp02r DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=samsung.com; s=mail20170921; t=1752237400; bh=K0K/KtJbTXHNw0pmcTrwWsLkly4FuPuuoVVC7z5NRQU=; h=Date:Subject:To:Cc:From:In-Reply-To:References:From; b=C1irTKJKR6dh7Zyb0hzv5ZIWVDUNbUFWxCqT3Z09Oirx/34YrJ7RKqeNSd3wCC3Ji IJHkR0vLQvRNN+rNzQJgYvbmdLrcjiJiV5ZFOgtOudkwD5LrcsUnataJLfTIPiwJIC vNXQ5zUwT/XPaG02QKWIcb1NAG85KB7+rU74SHTY= Received: from eusmtip2.samsung.com (unknown [203.254.199.222]) by eucas1p1.samsung.com (KnoxPortal) with ESMTPA id 20250711123639eucas1p1941aef7a8b0e49493b430463f97136db~RMqF-42Wd2355023550eucas1p1H; Fri, 11 Jul 2025 12:36:39 +0000 (GMT) Received: from [192.168.1.44] (unknown [106.210.136.40]) by eusmtip2.samsung.com (KnoxPortal) with ESMTPA id 20250711123637eusmtip2a6e5e3c4f720153c14660001342b34a8~RMqEx1kL50333203332eusmtip2h; Fri, 11 Jul 2025 12:36:37 +0000 (GMT) Message-ID: <020b8036-8959-4733-a5ab-ce8c963ce869@samsung.com> Date: Fri, 11 Jul 2025 14:36:37 +0200 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v10 0/7] Rust Abstractions for PWM subsystem with TH1520 PWM driver To: Danilo Krummrich , =?UTF-8?Q?Uwe_Kleine-K=C3=B6nig?= Cc: Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?UTF-8?Q?Bj=C3=B6rn_Roy_Baron?= , Andreas Hindborg , Alice Ryhl , Trevor Gross , Guo Ren , Fu Wei , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Paul Walmsley , Palmer Dabbelt , Albert Ou , Alexandre Ghiti , Marek Szyprowski , Benno Lossin , Michael Turquette , Drew Fustini , linux-kernel@vger.kernel.org, linux-pwm@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-riscv@lists.infradead.org, devicetree@vger.kernel.org, Krzysztof Kozlowski Content-Language: en-US From: Michal Wilczynski In-Reply-To: Content-Transfer-Encoding: 8bit X-CMS-MailID: 20250711123639eucas1p1941aef7a8b0e49493b430463f97136db X-Msg-Generator: CA Content-Type: text/plain; charset="utf-8" X-RootMTR: 20250707094926eucas1p155bd967b6986c4a999776839b1aa1fc6 X-EPHeader: CA X-CMS-RootMailID: 20250707094926eucas1p155bd967b6986c4a999776839b1aa1fc6 References: <20250707-rust-next-pwm-working-fan-for-sending-v10-0-d0c5cf342004@samsung.com> <4hmb3di5x2iei43nmrykrj5wzlltrf3vrnqvexiablonbscn57@4bbsz5c76t63> On 7/10/25 23:19, Danilo Krummrich wrote: > On Thu Jul 10, 2025 at 10:57 PM CEST, Uwe Kleine-König wrote: >> On Thu, Jul 10, 2025 at 06:06:26PM +0200, Danilo Krummrich wrote: >>> On Thu Jul 10, 2025 at 5:25 PM CEST, Uwe Kleine-König wrote: >>>> Hello Michal, >>>> >>>> On Thu, Jul 10, 2025 at 03:48:08PM +0200, Michal Wilczynski wrote: >>>>> On 7/10/25 15:10, Uwe Kleine-König wrote: >>>>>> On Thu, Jul 10, 2025 at 10:42:07AM +0200, Michal Wilczynski wrote: >>>>>>> On 7/7/25 11:48, Michal Wilczynski wrote: >>>>>>>> The series is structured as follows: >>>>>>>> - Expose static function pwmchip_release. >>>>>> >>>>>> Is this really necessary? I didn't try to understand the requirements >>>>>> yet, but I wonder about that. If you get the pwmchip from >>>>>> __pwmchip_add() the right thing to do to release it is to call >>>>>> pwmchip_remove(). Feels like a layer violation. >>>>> >>>>> It's required to prevent a memory leak in a specific, critical failure >>>>> scenario. The sequence of events is as follows: >>>>> >>>>> pwm::Chip::new() succeeds, allocating both the C struct pwm_chip and >>>>> the Rust drvdata. >>>>> >>>>> pwm::Registration::register() (which calls pwmchip_add()) fails for >>>>> some reason. >>>> >>> >>> (Just trying to help clear up the confusion.) >> >> Very appreciated! >> >>>> If you called pwmchip_alloc() but not yet pwmchip_add(), the right >>>> function to call for cleanup is pwmchip_put(). >>> >>> That is exactly what is happening when ARef is dropped. If the reference >>> count drops to zero, pwmchip_release() is called, which frees the chip. However, >>> this would leave the driver's private data allocation behind, which is owned by >>> the Chip instance. >> >> I don't understand that. The chip and the driver private data both are >> located in the same allocation. How is this a problem of the driver >> private data only then? The kfree() in pwmchip_release() is good enough >> for both?! > > Not in the current abstractions, there are two allocations, one for the Chip and > one for the driver's private data, or in other words the abstraction uses > pwmchip_set_drvdata() and pwmchip_get_drvdata(). > > Having a brief look at pwmchip_alloc(), it seems to me that PWM supports the > subclassing pattern with pwmchip_priv(). > > We should probably take advantage of that. Assuming we do that, the Rust > abstraction still needs a release() callback because we still need to call > drop_in_place() in order to get the destructor of the driver's private data > type called. We actually missed this in DRM and I fixed it up recently [1]. > > @Michal: With the subclassing pattern the Chip structure would look like this: > > #[repr(C)] > #[pin_data] > pub struct Chip { > inner: Opaque, > #[pin] > data: T, > } } Hello Thank you both for the detailed feedback and suggestions. Danilo, you are right, we should absolutely use the subclassing pattern to have a single allocation for the chip and driver data. This is a much cleaner design. As I looked into this, the main difference is that the C struct pwm_chip doesn't have a fixed size because of the pwms[] array at the end. This prevents us from using the exact struct layout you suggested. pub pwms: __IncompleteArrayField, Therefore, to correctly implement the subclassing pattern, it would be sufficient to leave the current struct as is and use pwmchip_get_drvdata to acquire pointers to the allocated drvdata. pub struct Chip(Opaque, PhantomData); This will still achieve the goal of a single allocation via pwmchip_alloc's sizeof_priv argument, while working around the DST limitation. > > And in the release() callback would look like this: > > extern "C" fn release(ptr: *mut bindings::pwm_chip) { > // CAST: Casting `ptr` to `Chip` is valid, since [...]. > let this = ptr.cast>(); I think this would use pwmchip_get_drvdata instead. > > // SAFETY: > // - When `release` runs it is guaranteed that there is no further access to `this`. > // - `this` is valid for dropping. > unsafe { core::ptr::drop_in_place(this) }; > } > > This is exactly what we're doing in DRM as well, I would have recommended this > to begin with, but I didn't recognize that PWM supports subclassing. :) > > I recommend having a look at [2]. > > [1] https://lore.kernel.org/all/20250629153747.72536-1-dakr@kernel.org/ > [2] https://gitlab.freedesktop.org/drm/misc/kernel/-/blob/drm-misc-fixes/rust/kernel/drm/device.rs > Best regards, -- Michal Wilczynski