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 E2F5A286D7C for ; Mon, 7 Jul 2025 07:30:20 +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=1751873423; cv=none; b=ODazqbgckYW1o2gWu6Q0E6yqBLJN+xbdbm4cfy+m+gLkt+ItK0sAHshZHuZPci46+3bo3hxZ+g+8j2LzGjFZ+FxGVKx/gDAXPEkSyXkPN1BILTWdlC79li6hQAUdtdIEUUTyXcIprdMaVjyT1OiQKbD2Qn9CRZetMWvUtbB5Ck4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751873423; c=relaxed/simple; bh=aSpnSLUsKoIud4TMn2Bqvcleyy0rBk7G0hRpyIo8X6c=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:From:In-Reply-To: Content-Type:References; b=XVvuwvooVApNTPe8c7bOBslhy7SnpfrwQPqQJh/+QJOINCCqBPoSqZpiIpVlNMFTav+q/xM/w9vOFU3EAAHo/JxqqxEiLp8hHnvw7sSYMZjO4y7Mlc1ZVHS7wDIwmDokhJf0P4sJyOOYEQCF/jaJoyE7EFFZkdWWFs+SOiv4t0w= 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=nTcmJlWE; 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="nTcmJlWE" Received: from eucas1p1.samsung.com (unknown [182.198.249.206]) by mailout2.w1.samsung.com (KnoxPortal) with ESMTP id 20250707073019euoutp02a91394c87378c123b4062a8c100ee5ee~P55fY7eU31518415184euoutp024 for ; Mon, 7 Jul 2025 07:30:19 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 mailout2.w1.samsung.com 20250707073019euoutp02a91394c87378c123b4062a8c100ee5ee~P55fY7eU31518415184euoutp024 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=samsung.com; s=mail20170921; t=1751873419; bh=Q5MiDO4kvDAjyRPyePkrhFPW14vb6d7EHum8/0WQ8v4=; h=Date:Subject:To:Cc:From:In-Reply-To:References:From; b=nTcmJlWE9ADFjtTB8/5dbhfElVYFVx1jEthmk7Ctq63wpWZA0y3SsvT87qm57mCtz rUjt3bDhvweBckPVDFw5OznBTOS+vV7+8SZ9nHBpYZQENs4/FWNurFGCw+oFHkPpQ1 XHWI3W7f3PQJvFFlwDzHrIxWLH38TC9yfJ59Mtd0= Received: from eusmtip2.samsung.com (unknown [203.254.199.222]) by eucas1p1.samsung.com (KnoxPortal) with ESMTPA id 20250707073018eucas1p1a0c2e714826a2571f8c5281b8ef0457c~P55eomzep1941219412eucas1p1u; Mon, 7 Jul 2025 07:30:18 +0000 (GMT) Received: from [192.168.1.44] (unknown [106.210.136.40]) by eusmtip2.samsung.com (KnoxPortal) with ESMTPA id 20250707073017eusmtip20a0a1f63bf89beef337da1edc73632c1~P55dgBGZm2165621656eusmtip2K; Mon, 7 Jul 2025 07:30:17 +0000 (GMT) Message-ID: <002c8275-f17d-4a4e-b6b8-4dc74c4d510d@samsung.com> Date: Mon, 7 Jul 2025 09:30:17 +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 v9 2/6] rust: pwm: Add complete abstraction layer To: Danilo Krummrich Cc: =?UTF-8?Q?Uwe_Kleine-K=C3=B6nig?= , 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 Content-Language: en-US From: Michal Wilczynski In-Reply-To: Content-Transfer-Encoding: 7bit X-CMS-MailID: 20250707073018eucas1p1a0c2e714826a2571f8c5281b8ef0457c X-Msg-Generator: CA Content-Type: text/plain; charset="utf-8" X-RootMTR: 20250706114605eucas1p17d1cbd035d14d95f4ad0588c2572b3e2 X-EPHeader: CA X-CMS-RootMailID: 20250706114605eucas1p17d1cbd035d14d95f4ad0588c2572b3e2 References: <20250706-rust-next-pwm-working-fan-for-sending-v9-0-42b5ac2101c7@samsung.com> <20250706-rust-next-pwm-working-fan-for-sending-v9-2-42b5ac2101c7@samsung.com> On 7/6/25 14:23, Danilo Krummrich wrote: > On Sun, Jul 06, 2025 at 01:45:13PM +0200, Michal Wilczynski wrote: >> +/// Trait defining the operations for a PWM driver. >> +pub trait PwmOps: 'static + Sized { >> + /// The type of the owned driver data (e.g., `Pin>`). >> + type DrvData: 'static + ForeignOwnable; >> + /// The driver-specific hardware representation of a waveform. >> + /// >> + /// This type must be [`Copy`], [`Default`], and fit within `PWM_WFHWSIZE`. >> + type WfHw: Copy + Default; >> + >> + /// Optional hook for when a PWM device is requested. >> + fn request( >> + _chip: &Chip, >> + _pwm: &Device, >> + _parent_dev: &device::Device, >> + ) -> Result { >> + Ok(()) >> + } >> + >> + /// Optional hook for when a PWM device is freed. >> + fn free(_chip: &Chip, _pwm: &Device, _parent_dev: &device::Device) {} > > NIT: I can't think of a case providing this callback in Rust is useful. Do you > have a clear use-case in mind? Otherwise, I'd not provide this callback until > you have one. Should be trivial to add later on. > >> +impl Adapter { > > > >> + /// # Safety >> + /// >> + /// `dev` must be a valid pointer to a `bindings::device` embedded within a >> + /// `bindings::pwm_chip`. This function is called by the device core when the >> + /// last reference to the device is dropped. >> + unsafe extern "C" fn release_callback(dev: *mut bindings::device) { >> + // SAFETY: The function's contract guarantees that `dev` points to a `device` >> + // field embedded within a valid `pwm_chip`. `container_of!` can therefore >> + // safely calculate the address of the containing struct. >> + let c_chip_ptr = unsafe { container_of!(dev, bindings::pwm_chip, dev) }; >> + >> + // SAFETY: `c_chip_ptr` is a valid pointer to a `pwm_chip` as established >> + // above. Calling this FFI function is safe. >> + let drvdata_ptr = unsafe { bindings::pwmchip_get_drvdata(c_chip_ptr) }; >> + >> + if !drvdata_ptr.is_null() { > > Is this check needed? I think one can't create a pwm::Chip instance without > providing a T, so this pointer can't be NULL I think. > >> + // SAFETY: `drvdata_ptr` was stored by `Chip::new` from an owned `T::DrvData` >> + // and is guaranteed to be valid if non-null. `from_foreign` can safely >> + // reclaim ownership to allow Rust to drop and free the data. >> + let _owned_drvdata = unsafe { T::DrvData::from_foreign(drvdata_ptr.cast()) }; >> + } >> + } > > If you overwrite this callback (as you do below) you're leaking the memory > allocated by pwmchip_alloc(). > > The simple way to solve this would be to call pwmchip_release() from here. Thanks, a pwmchip_release() is static though, so it's either expose the pwmchip_release in the header, or call kfree() here directly on pwmchip. > > > >> +impl Chip { >> + /// Allocates and wraps a PWM chip using `bindings::pwmchip_alloc`. >> + /// >> + /// Returns an [`ARef`] managing the chip's lifetime via refcounting >> + /// on its embedded `struct device`. >> + pub fn new>( >> + parent_dev: &device::Device, >> + npwm: u32, >> + sizeof_priv: usize, >> + drvdata: T, >> + ) -> Result> { >> + // SAFETY: `parent_device_for_dev_field.as_raw()` is valid. >> + // `bindings::pwmchip_alloc` returns a valid `*mut bindings::pwm_chip` (refcount 1) >> + // or an ERR_PTR. >> + let c_chip_ptr_raw = >> + unsafe { bindings::pwmchip_alloc(parent_dev.as_raw(), npwm, sizeof_priv) }; >> + >> + let c_chip_ptr: *mut bindings::pwm_chip = error::from_err_ptr(c_chip_ptr_raw)?; >> + >> + // Set the custom release function on the embedded device. This is the crucial step >> + // to ensure `drvdata` is freed when the chip's refcount reaches zero, regardless >> + // of whether `Registration::register` was called. >> + // SAFETY: `c_chip_ptr` points to a valid chip. >> + unsafe { (*c_chip_ptr).dev.release = Some(Adapter::::release_callback); } > > This overwrites [1]. > > [1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/pwm/core.c?h=v6.16-rc4#n1601 > Best regards, -- Michal Wilczynski