Received: by 2002:ab2:6309:0:b0:1fb:d597:ff75 with SMTP id s9csp950033lqt; Fri, 7 Jun 2024 03:52:16 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWU0+WPRsDyhbwDh0AcOxNFyYIXILwF6vV/YcVVsww5nqiFijM6gCCHuNmSX34IlLq+wNxJZyeDxS/IKKOzLR5lsapcqBGfvyfZf6P44Q== X-Google-Smtp-Source: AGHT+IGPmH5UeJALDwvZlFUCeumoFE6Doi9tdW7CFINagTw2ZFVQ94YtrpZMWl4awkKg6wu6eONO X-Received: by 2002:a05:6808:1416:b0:3d2:3ee:4d43 with SMTP id 5614622812f47-3d210f12fc1mr2097384b6e.36.1717757536221; Fri, 07 Jun 2024 03:52:16 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717757536; cv=pass; d=google.com; s=arc-20160816; b=BEALvm3IsZ6Vd9jadV7n4r50G6uBQ8eXiaAOQTjlWvD6YS13EcYi0Hx8t5OI6t2TuS JSUsgMvURpTyZ+p8LIrT8QTSss3DS3xUol5/dOhfuS7gF2SGVv414TNaMMBLwH4JbTti lLGGspEKpJFOFfLKlICKzYuZcomcuuvvHs+Oo8IJKlI93UV/Rd+lPhcyI9jrKN1cfpr6 R9bOJkgUrGrhEHMDeaaL0tByeQJ1BLgeIyfc3LFh1t3D+dCMiRCIPgT7mxDM+uD8sfLC q/jGfJD2dJx7ACFqITg1P23u2DBWOgsev8/c4aoLAjbVaUfOk/75/2PjTnDaM2usMjku GI+w== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=LmeXUhFF655AvDeF8viECIcXvU5zP377LCGo+5Zcuww=; fh=z9z2nTb/FQOjs4+89OEPhRoJGO0icQNdkPOvuBOfaJ8=; b=wBcRJ1fayuGMrZxBdeTJXgjowVFd1/KvwKBypTow9Q6hTw+YNvVE61Prcb4a16nwdG 0rnTOfp8M13yg6l++tUZyJzv0BpzlXBrRaOwxzWRCwJPKPX2A32T6B2K9AmA3hu4H6yF AQV6VZIQVWdqvPnA/0fTsoku2BHZjPkR0LAkhOgkizFtP8NPJksUhYwqIMDDGarB+9S9 Q6TeRuIUiPMub99cltqJA0cCPLq/uWmG5sxUVXiQVLWoP2jz+l3ZNv1utXMD2EXdoEKX iTYODudoYQItUwU6zL7+n+O9nPo0d/4LYIsELi5nupKqX+BdSZdc6N81e80Wgg9IFoyL pYZw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Ln5YiZc6; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-205848-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-205848-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id d2e1a72fcca58-703fd4c7366si489366b3a.211.2024.06.07.03.52.15 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Jun 2024 03:52:16 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-205848-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Ln5YiZc6; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-205848-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-205848-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 98A43B20A96 for ; Fri, 7 Jun 2024 10:52:03 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9A34B18629E; Fri, 7 Jun 2024 10:51:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Ln5YiZc6" Received: from mail-lj1-f181.google.com (mail-lj1-f181.google.com [209.85.208.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 CC2B2186293 for ; Fri, 7 Jun 2024 10:51:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.181 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717757509; cv=none; b=MCKSic7nrLcQBGLZZdzf3VoY+h/mpSLHbbj5zMLy3pPS6zV43YtPcd5zJAabFH8jTY3HJ7a7Yel7Hq2eHn38Em/uZKaqEw/ewqGzzE0FORtsNU4roulMy7mDyurcZFZezJRlTIb4tuee3ka9h2n77WTNLX5aSrwZejwQDNMDUeI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717757509; c=relaxed/simple; bh=FFbj21ozJQKf4i41vhO+He8GzAZG9POBGWSpHtgddTw=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=ckEi7DXUIAP7uqMy8jXJk1SnrT37Q/ZhBOngGK9yt2QVuy7YaHD3JUXyA5MO5bC1teFwxHYst3C2csuJ8gWfkYVy0AUGlRJSkSxBspR8SGhVBt2XaxzzI5eaFQAmq+oXxoljklaq8alDDzF6Fmzi/H9U5xP4eufoue1/UCe9KXY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Ln5YiZc6; arc=none smtp.client-ip=209.85.208.181 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=google.com Received: by mail-lj1-f181.google.com with SMTP id 38308e7fff4ca-2e73441edf7so21088961fa.1 for ; Fri, 07 Jun 2024 03:51:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1717757506; x=1718362306; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=LmeXUhFF655AvDeF8viECIcXvU5zP377LCGo+5Zcuww=; b=Ln5YiZc6E7N94HOps5slmYwc9HJA7Ba9+LauR5+WSsJFdeB1/iRMZEPKoD7NO8ajvv mGHMtcmji+J5fTo5hVYtcV6AzKYTJDluItgCGWyt7yGpxPrvmB/qX/uObZ4/AgRaf8fU W6Kxh+S4AaXzcl41LmkI+uVRGOV3J34hidxM/S5ym2zsHs45iWNpBpSxoYaJmqFA3lWX A6XGlNxINwrX5KBuKArVbh92vUuQVPMmesL+J/O7b7+cjlSYIF3p11dyV7l5jzE+w7Tw 3WQXijnJQbjl+X5hFbKzogmnfHPc4+wHjl5h1tBZsL9Boqu5t+oKqpm1iSRQ0vJ8iggM +FrQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717757506; x=1718362306; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=LmeXUhFF655AvDeF8viECIcXvU5zP377LCGo+5Zcuww=; b=wQhX07xTfLH2TnjOcTqI7DAfKTHGHWUAWglHfbPr/vT75F1iP7dDF/eaOcmAONxzBJ oWczWxTUPg2zktAuf8OWIT5/UhJLRdxM+0g8mi0JLtrbO6Oeha0exoIkSO9c2SIc5a96 hiebVQihwNcvLYixzojv30vpFpGOubN5/72+TR9EPdXbi+kZma0zDZpwrTSNKOQjKFoG rJOXw/kCbkOvHc9V8N+wggvn6arW96e2493okqsfLYxG4KnoLCQp+w5jKNHwqaVFYjJt d9cw9gbTaNOvEk5YQ+fidKK+TWgyTyC7AGO3GYJqGN5kTO02QxO9KaFNBeGkJkxwRZxY 1k6Q== X-Forwarded-Encrypted: i=1; AJvYcCWXnxCekiJs1zRPH3ZzHCnk8gw/77KXWo1nV7ONJiuT7KjCuXAue7D3Vbdz529Kxf1Y4HTLrmiSLyw7jlxsHYOl6c75H28/rhJMG/0W X-Gm-Message-State: AOJu0YwMpOBBlYT4fQPRq6wBBJAj4eptZrK/ef8X8CUCPCwY9qzSv/3J LKSvvqY+P1b4Rij84FaTaDXFNB+OH4NqUp1qp1U64nYFm1T+sRm2zO+F/vW2bkbslygNITFy651 T1xvMVPR1iE+bfUW6ToLprzolZ/aJ5wUtMK3M X-Received: by 2002:a2e:88c6:0:b0:2ea:8291:c667 with SMTP id 38308e7fff4ca-2eadce24b75mr13406881fa.13.1717757505728; Fri, 07 Jun 2024 03:51:45 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: In-Reply-To: From: Alice Ryhl Date: Fri, 7 Jun 2024 12:51:33 +0200 Message-ID: Subject: Re: [RFC PATCH V2 1/8] rust: Add initial bindings for OPP framework To: Viresh Kumar Cc: "Rafael J. Wysocki" , Miguel Ojeda , Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?UTF-8?Q?Bj=C3=B6rn_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 , =?UTF-8?B?QWxleCBCZW5uw6ll?= , Joakim Bech , Rob Herring , linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Fri, Jun 7, 2024 at 11:12=E2=80=AFAM 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) -> Resu= lt> { > + let ptr =3D 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 t= hat 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 =3D unsafe { Self::from_ptr_owned(ptr) }?; > + > + // Take an extra reference to the OPP since the caller didn't ta= ke 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 compare to my file patchset, where I am going to rename the equivalent method to `from_raw_file` in the next version. 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". > + fn as_mut_ptr(&self) -> *mut bindings::dev_pm_opp { > + self.0.get() > + } I think most existing examples call this `as_raw` and mark it `#[inline]`. > + /// Adds an OPP dynamically. > + pub fn add(dev: ARef, mut data: Data) -> Result<()> { > + // SAFETY: The requirements are satisfied by the existence of `D= evice` 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 `D= evice` 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? Also, why are these methods defined on OPP when they appear to be methods on Device and don't take any OPP argument? Alice