From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-lf1-f45.google.com (mail-lf1-f45.google.com [209.85.167.45]) (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 D180418733D for ; Fri, 7 Jun 2024 11:24:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.45 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717759482; cv=none; b=CRBExj07Yaywhdl+Fa0L7dzKgMR5/evB5Bd365mkZKj1CsiJd4BXZiLb9nCA8z5w5fIIyFmCCqF/GNPDNRf9LuX8h3XyCF74NWu6cLwBeMIs91v4wxGYRvtbgzFk7MkW3SFtL5S9ijup8t4xPPmjRuXhco3VbWYYtFUtWwdRq8s= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717759482; c=relaxed/simple; bh=QDB7wOedg633ySJnJZZj65PiGCmpyS2TDfWcEfEhYxM=; h=Date:From:To:Cc:Subject:References:In-Reply-To:Message-ID: MIME-Version:Content-Type; b=YTS87tgQlQrUi7Q71qUYtWaOVvKnIbV45pVbtfFv0KQz/PoFXgQJ+LUdlCeuLP/qaED8HIa8lKnl33pG6crHT0kNHRdXmOLxEsZvxuJOYYtxqHp1YTqnWbo0ldZPYdZyK2RooOS8ji7VGiFR9FWCr+p5naUDB5gLA8dS5AWHvN8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=VLYqQIjb; arc=none smtp.client-ip=209.85.167.45 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="VLYqQIjb" Received: by mail-lf1-f45.google.com with SMTP id 2adb3069b0e04-52b950aa47bso3008612e87.1 for ; Fri, 07 Jun 2024 04:24:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1717759478; x=1718364278; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:in-reply-to :references:user-agent:subject:cc:to:from:date:from:to:cc:subject :date:message-id:reply-to; bh=TxKqtp38KhT9aMHnsIeC+TX8dWAC4DZuSMvsQh7h8Sc=; b=VLYqQIjbfl2wPFGZk2xA8XlK0+V39hR5wdPBCyTESDrnh3YR30F8bI4q/SNm47jJNe o08BMPN+GNyTgFimyII+lrhnkC4Uf1FEikintb3U3gDEASsfWcXlegSE88S+ImBqNEFP 9F7RZGDc178x3utWSuvRTTWc/mj2ek9oT4Cl0VpP7BYQiDCMTBYcB5PzrUB+BW5LEX4I +tXkTrTxVl+P1ByffSeVnyaVa9ErMgczBm7Jwnf5EUJBfR2fxIHPhwNyVFPTrVm/8g4T CkY1imGKX6k4PAdjUA9Ma+I/9VcXHPiPwdeo/2g+xpewxISLuVQAX4c8kl1LJkrdtfXz /aUQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717759478; x=1718364278; h=content-transfer-encoding:mime-version:message-id:in-reply-to :references:user-agent:subject:cc:to:from:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=TxKqtp38KhT9aMHnsIeC+TX8dWAC4DZuSMvsQh7h8Sc=; b=sBJ4pQBO3aB1HUxQ5kg+Xr4OCnr5KXXdP5MUfYTN3TaGIqszOlXclzWrinp+CZKxmO PuCP8ZYdI4ZDVhag1TqbYz/+Wc4PAP3fwaUI3CP2vPVaR0FdljoFhvLfdgBIbwm/g9MZ 5pz8/GtBTuM7TQ/ujUVSYCD6yyAYfY0Dwl2fUyrzDi3IdAisbpvkucKTmbzC3XzXJw5+ SR3WKPcgKJe5BPRCxdT4jdUhGBwxOXLZyyWRpAvh36dnEIZ4Dqc86mmlqfS7CnOuGaTu 6OFm/YKpfQnldJ7STrjRrOTOKdEJF4MJwx02AT2/t7QzCzZH2mFq4e+Tsk0xnMO/wANl w1sw== X-Forwarded-Encrypted: i=1; AJvYcCVKJoEiEZxTAP0i54QtHmgbSE0lMkraHLC1cOqzh+/h76E+7jgN47EoT1rv4ObGX/Nt8jSJsmUilXcInml9KOYA7za8vVHJ24iGn3k2/Zg= X-Gm-Message-State: AOJu0Ywg5bdD2G0VHFbU2vLgNt7pjYWTezCOTYkRTv4AK+/2wRo7BMxn xUwhx2ZxYMCDULTSfbIKhvdir12TcoA0BpN6v0X5R3PGsdiSWRaycXy9F2DcVdE= X-Google-Smtp-Source: AGHT+IGn858uClRXM981tpsaA+vmiD0FQcCFjuKV9/5cUuIuy0VYS5bgZRqyLlCPVkDX655Cz+J0Tw== X-Received: by 2002:a05:6512:ac4:b0:52b:7bde:896b with SMTP id 2adb3069b0e04-52bb9f5d3b5mr1689086e87.14.1717759477758; Fri, 07 Jun 2024 04:24:37 -0700 (PDT) Received: from meli-email.org (adsl-105.37.6.1.tellas.gr. [37.6.1.105]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4215c19e572sm50531555e9.10.2024.06.07.04.24.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Jun 2024 04:24:37 -0700 (PDT) Date: Fri, 07 Jun 2024 14:18:54 +0300 From: Manos Pitsidianakis To: Alice Ryhl , Viresh Kumar Cc: "Rafael J. Wysocki" , Miguel Ojeda , Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , Bj=?UTF-8?B?w7Y=?= rn Roy Baron , Benno Lossin , Andreas Hindborg , linux-pm@vger.kernel.org, Vincent Guittot , Stephen Boyd , Nishanth Menon , rust-for-linux@vger.kernel.org, Manos Pitsidianakis , Erik Schilling , Alex Benn=?UTF-8?B?w6k=?= e , Joakim Bech , Rob Herring , linux-kernel@vger.kernel.org Subject: Re: [RFC PATCH V2 1/8] rust: Add initial bindings for OPP framework User-Agent: meli 0.8.5 References: In-Reply-To: Message-ID: Precedence: bulk X-Mailing-List: rust-for-linux@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset=utf-8; format=flowed On Fri, 07 Jun 2024 13:51, Alice Ryhl wrote: >On Fri, Jun 7, 2024 at 11:12 AM Viresh Kumar wrote: >> >> This commit adds initial Rust bindings for the Operating performance >> points (OPP) core. This adds bindings for `struct dev_pm_opp` and >> `struct dev_pm_opp_data` to begin with. >> >> Signed-off-by: Viresh Kumar > >> +//! Operating performance points. >> +//! >> +//! This module provides bindings for interacting with the OPP subsystem. >> +//! >> +//! C header: [`include/linux/pm_opp.h`](../../../../../../include/linux/pm_opp.h) > >Please use srctree links instead. > >C header: [`include/linux/pm_opp.h`](srctree/include/linux/pm_opp.h) > >> +impl OPP { >> + /// Creates a reference to a [`OPP`] from a valid pointer. >> + /// >> + /// # Safety >> + /// >> + /// The caller must ensure that `ptr` is valid and remains valid for the lifetime of the >> + /// returned [`OPP`] reference. >> + pub unsafe fn from_ptr_owned(ptr: *mut bindings::dev_pm_opp) -> Result> { >> + let ptr = ptr::NonNull::new(ptr).ok_or(ENODEV)?; >> + >> + // SAFETY: The safety requirements guarantee the validity of the pointer. >> + // >> + // INVARIANT: The refcount is already incremented by the C API that returned the pointer, >> + // and we pass ownership of the refcount to the new `ARef`. >> + Ok(unsafe { ARef::from_raw(ptr.cast()) }) >> + } >> + >> + /// Creates a reference to a [`OPP`] from a valid pointer. >> + /// >> + /// # Safety >> + /// >> + /// The caller must ensure that `ptr` is valid and remains valid for the lifetime of the >> + /// returned [`OPP`] reference. >> + pub unsafe fn from_ptr(ptr: *mut bindings::dev_pm_opp) -> Result> { >> + let opp = unsafe { Self::from_ptr_owned(ptr) }?; >> + >> + // Take an extra reference to the OPP since the caller didn't take it. >> + opp.inc_ref(); >> + >> + Ok(opp) >> + } > >I would recommend a slightly different approach here. You can provide >a method called `from_raw_opp` that takes a *mut bindings::dev_pm_opp >and returns a &Self. The ARef type provides a method that converts >&Self to ARef by taking a refcount. This way, users would also >be able to call OPP methods without giving Rust any refcounts. You can Wouldn't this allow for use-after-free? What if the refcount drops to 0 before the method is called? >As for `from_ptr_owned`, I would probably rename it to >`from_raw_opp_owned` or similar. It's often nice to use a more >descriptive name than just "ptr". >I think most existing examples call this `as_raw` and mark it >`#[inline]`. I think `ptr` is more idiomatic to Rust users, not that your suggestion is wrong. from_ptr_owned also implies the function signature. > >> + /// Adds an OPP dynamically. >> + pub fn add(dev: ARef, mut data: Data) -> Result<()> { >> + // SAFETY: The requirements are satisfied by the existence of `Device` and its safety >> + // requirements. >> + to_result(unsafe { bindings::dev_pm_opp_add_dynamic(dev.as_raw(), &mut data.0) }) >> + } >> + >> + /// Removes a dynamically added OPP. >> + pub fn remove(dev: ARef, freq: u64) { >> + // SAFETY: The requirements are satisfied by the existence of `Device` and its safety >> + // requirements. >> + unsafe { bindings::dev_pm_opp_remove(dev.as_raw(), freq) }; >> + } > >Is it intentional that these methods take ownership of a refcount to >the device that it then drops after calling the C function? use-after-free again? Though I'm suggesting this without actually examining if it can happen.