From patchwork Wed Mar 19 14:18:55 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Courbot X-Patchwork-Id: 14022645 X-Patchwork-Delegate: bhelgaas@google.com Received: from NAM12-DM6-obe.outbound.protection.outlook.com (mail-dm6nam12on2074.outbound.protection.outlook.com [40.107.243.74]) (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 5A4D11C2DC8; Wed, 19 Mar 2025 14:19:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.243.74 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742393948; cv=fail; b=CLy2DoYMx/n9AHwoupeD1B7y+wal4KlByFBHs5+ZTaAbKsfkgichOCsaV7LxyrbuOoK0gx7JLq0E1jw24MZtsWfRJESvSGT5ZeOcLvMopqw0DT9Qi1Q2dCaEIZod6GIdMe/FAYdCzUx6j0bGNlfe6hPMSgd1bRZO5LboRURVqeo= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742393948; c=relaxed/simple; bh=RaDT9Lk/zd25JCoFXD8nq79AgReb8SFzRFFXoN5u2Go=; h=From:Date:Subject:Content-Type:Message-Id:References:In-Reply-To: To:Cc:MIME-Version; b=G9p00pjxbaANUdjkLydepcuTPwbYySidZPFgIF7ljyoC5DYYtMulksvn3Fm3g4LWJC2yfdabjqajq8AU3S/IBZgNS1LCw0UGPcjIechu9PbmwwmmqYu2gL/pHzsWG0odJw0IVPs99dDhTqTbJSNDQIdCVMOMsVJ8KKmSDKOQlKI= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=b9Xhx3td; arc=fail smtp.client-ip=40.107.243.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b="b9Xhx3td" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=UfASBVtSZqikUDzuNtg7OxJARK6G6AF8WXPE9j0g3blm4N44ctttZOMnAlkgpgZPlTDUTzmAi+g5RzPsLBlxfr4l7Vh7u3OPUAsNsiLDBKqULFlIez6lgWLlvmSRaghLj7FY/EajRA8KX5cfMv2toggqUYTnSEPRLjudYOY/djqqP+d8ScfowchsdmbsX7wZDs9Lx1bZulQdU3QSK2QwyvVjovE5LpvPPy+zC1Q7TYNnfMM6W90JzHBaKnYRei4f7343MlbmipICmlbInYYa0o4s5q/p2bCwIR+kBKPdDgLpcqtg6oiqVZnsziJMKKjESKvkq7JKo2lD8ayBt9Gc9w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=J8Oj/szeJStA1xEt/uzw4YL56XBlEZQEze9N67sFmw8=; b=jkW9V2HDYmPLvPeLZ+CIYy40/iOCn7/8zJiOFp1KdcHpfvX3MZxi2WYnDgg43vjsKScQJkjLThGSv7d+ZawiNAGI9Rdknsd3J7hOQJ5Be8l/e9Rzjn8Aqdq1JJGc8mN/Lk20e3iE2YClth12xZjP/5ri2aJIewN/T5doGYZTnOuchqmyHtvPQTxnxsH3Fvc+iB9Z3fRHm0qc3jqNBB2Anf19ALLrPZxUMF7Owp9LtuoA8MUKuSRbjlI+RjgOUDRo80nIvumfL2nGMhNJaeRxnKQ1X7WbcEr0MYIQLJL7a/a1E6btB9b6JLl3TMjEc92sFpeoZXjxgjvTYzbZ03lTSw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=J8Oj/szeJStA1xEt/uzw4YL56XBlEZQEze9N67sFmw8=; b=b9Xhx3tda96snIob5QEoWGukCs/mmXTr3/RrwZPp0EedH3ay/JjJsQPWUdz/kAFueqNk8EF0Py8AWa1VbJcUf561cn+Kc345IseOTu5z2Gm+dA1B645DRRX0NpY5QbrDeRmKtMFbreQAHjC7rx7JHG6GJxI1cJeflj0eX2xJiZfmvioGa06KH//iy5p1BzaTfk0zmoAwuV2NuEWKTJE9SkBtK3mzc9f/iwaEkgpvlZjydRprWIFT8sfRgU5zOPJ58lgBa6g6wIV7G3JLgOlEDsr/CYVZSmAMRhDkYgCEk6MAL8f2PV2v2Ipb6ACleuVtPnNxcVMQj30AA4c39sXzwQ== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from CH2PR12MB3990.namprd12.prod.outlook.com (2603:10b6:610:28::18) by MW4PR12MB5643.namprd12.prod.outlook.com (2603:10b6:303:188::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8534.34; Wed, 19 Mar 2025 14:19:02 +0000 Received: from CH2PR12MB3990.namprd12.prod.outlook.com ([fe80::6e37:569f:82ee:3f99]) by CH2PR12MB3990.namprd12.prod.outlook.com ([fe80::6e37:569f:82ee:3f99%6]) with mapi id 15.20.8534.034; Wed, 19 Mar 2025 14:19:02 +0000 From: Alexandre Courbot Date: Wed, 19 Mar 2025 23:18:55 +0900 Subject: [PATCH v2 1/2] rust/revocable: add try_access_with() convenience method Message-Id: <20250319-try_with-v2-1-822ec63c05fb@nvidia.com> References: <20250319-try_with-v2-0-822ec63c05fb@nvidia.com> In-Reply-To: <20250319-try_with-v2-0-822ec63c05fb@nvidia.com> To: Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , Danilo Krummrich , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Bjorn Helgaas Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org, Alexandre Courbot X-Mailer: b4 0.14.2 X-ClientProxiedBy: OS3P286CA0124.JPNP286.PROD.OUTLOOK.COM (2603:1096:604:1f7::20) To CH2PR12MB3990.namprd12.prod.outlook.com (2603:10b6:610:28::18) Precedence: bulk X-Mailing-List: linux-pci@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH2PR12MB3990:EE_|MW4PR12MB5643:EE_ X-MS-Office365-Filtering-Correlation-Id: ed278e9c-e1b0-4793-d634-08dd66f0ffd6 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|10070799003|1800799024|376014|7416014|366016|921020|7053199007; X-Microsoft-Antispam-Message-Info: =?utf-8?q?iigp9rdkRr4rQdYptScYY9GeEe7Hv18?= =?utf-8?q?qEkJHLLvDqEvWHtIUSEYILrMOlSzuN+ojoPzWj2JW8yZxo8SZweXNuhbZzD0800ST?= =?utf-8?q?6ZcG+peBDLzWdErRVp+M3TsP6LyJ4PuOhcm2ZwVt/RqkGpwa/0JD2aePEPkIa50YQ?= =?utf-8?q?X6T3MPaR4+tr1eQW/BCRIGC4DeQ7esnNV9bUIXfP+ox3iU6ezpObcdUkVGK3FOHOm?= =?utf-8?q?lTah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?q?HQ50LKTnbxBOX6m1GlFk1ypxECYsY6besr89/krf1bhfLuFE9Sggnc00VC6QIcQ/d?= =?utf-8?q?jpgILWuhPpG+TRpYR2k+ztnvm5oPIJb97h1ZUD1MZC9i2GK2L/HwZx5GVTD0gdPTZ?= =?utf-8?q?ORVW7qDvnSZbjA0KoBLS3t9OZNK3rfnGClQNZ7VxuBR1IChNb/YDsI0MVp2BeAslV?= =?utf-8?q?biwluWKECnnChyanZzzYbs1C8MUBfKDRqYd+6wMSYNNbg1K8Tq5juFujgVth4yzIH?= =?utf-8?q?HvhROGUyl4d7E5yWAqz03+aOOnOWLeX7sucQflBRtEwhoTV+LX74408=3D?= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:CH2PR12MB3990.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(10070799003)(1800799024)(376014)(7416014)(366016)(921020)(7053199007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?rptO8iQo8SLIT0bfc7xcdaRRKd9L?= =?utf-8?q?CNH2xspU3HuS1vLnMiGyFB8TLb3Y7+kuh9rgl8Lypfas1imvp6u2sncCYq9Oj++SI?= =?utf-8?q?mFUw9K29eUVtP0Ijx7k5iqiCHZD7miPsE5BZGT+RQWqi7i/AUAykaxLf4PkMwKC7p?= =?utf-8?q?gNnTZ91ASMWER0djavj1pzp71HuPU7ATfagJ8MKKOO0XBTm7ZZVqqSa4LBOuBG7DN?= =?utf-8?q?p3gPn0d4wRvHklsPmlLX2bpnTFiwbbfAnZQolvvPtP7XfJ0zSfrY8Tg4ql45Z5qge?= =?utf-8?q?olKFzb4D/cN8qZS5uoF1L+LQQDqFU3+C/9wlHQLEcEelCMc/k6AgU4nzz2yOruQJ+?= =?utf-8?q?/1o8hlTn2DqRAAOT+4m/O/mUqMB77bbkfMS6PyruYh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q4MGbzX9rj9/Nryfg9sMnng5XVmj3A?= =?utf-8?q?IB/lhZmKQzizVTMZ0Z7iYlJfGSQOhUVKkENHtKhsbeH0/J0f3EIVCXpXzzw4Ues1G?= =?utf-8?q?xvSa7pvZRi9Q4KEd7pALzqUwisLsus0jEhvSrC4LvP/5RH51lkzdlx8+8qqQ7Ljjb?= =?utf-8?q?iaOtqGl2lML0yjr0f4ZSbJY2h2bDtyL3KDohOVwOVibbKmdwgPOlnMDUwqpUfsmXR?= =?utf-8?q?lPjFyMvmVgQirsSjNPHWHLW0tANNDnOwykTcLhNKHb9xeL3I+JaraYSdVg2qOFmC2?= =?utf-8?q?aemjNaSS6j8RaqXneEPX47dCjstlObIgxExuCyQFyWPGv605xjgQH8fQ0Tk6cM8Mz?= =?utf-8?q?DxULMx9DlLO?= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: ed278e9c-e1b0-4793-d634-08dd66f0ffd6 X-MS-Exchange-CrossTenant-AuthSource: CH2PR12MB3990.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 Mar 2025 14:19:02.7954 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: 80jYlA6QQSZ8M0xG4wtBGATZbKOUwxz9wCeo0xGijO+/RQy4eKCNm4q8RBjTQ8IoFSvpurN5uVcgB7yPXfbVBg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW4PR12MB5643 Revocable::try_access() returns a guard through which the wrapped object can be accessed. Code that can sleep is not allowed while the guard is held; thus, it is common for the caller to explicitly drop it before running sleepable code, e.g: let b = bar.try_access()?; let reg = b.readl(...); // Don't forget this or things could go wrong! drop(b); something_that_might_sleep(); let b = bar.try_access()?; let reg2 = b.readl(...); This is arguably error-prone. try_access_with() provides an arguably safer alternative, by taking a closure that is run while the guard is held, and by dropping the guard automatically after the closure completes. This way, code can be organized more clearly around the critical sections and the risk of forgetting to release the guard when needed is considerably reduced: let reg = bar.try_access_with(|b| b.readl(...))?; something_that_might_sleep(); let reg2 = bar.try_access_with(|b| b.readl(...))?; The closure can return nothing, or any value including a Result which is then wrapped inside the Option returned by try_access_with. Error management is driver-specific, so users are encouraged to create their own macros that map and flatten the returned values to something appropriate for the code they are working on. Acked-by: Danilo Krummrich Suggested-by: Danilo Krummrich Signed-off-by: Alexandre Courbot --- rust/kernel/revocable.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/rust/kernel/revocable.rs b/rust/kernel/revocable.rs index 1e5a9d25c21b279b01f90b02997492aa4880d84f..b91e40e8160be0cc0ff8e0699e48e063c9dbce22 100644 --- a/rust/kernel/revocable.rs +++ b/rust/kernel/revocable.rs @@ -123,6 +123,22 @@ pub fn try_access_with_guard<'a>(&'a self, _guard: &'a rcu::Guard) -> Option<&'a } } + /// Tries to access the wrapped object and run a closure on it while the guard is held. + /// + /// This is a convenience method to run short non-sleepable code blocks while ensuring the + /// guard is dropped afterwards. [`Self::try_access`] carries the risk that the caller will + /// forget to explicitly drop that returned guard before calling sleepable code; this method + /// adds an extra safety to make sure it doesn't happen. + /// + /// Returns `None` if the object has been revoked and is therefore no longer accessible, or the + /// result of the closure wrapped in `Some`. If the closure returns a [`Result`] then the + /// return type becomes `Option>`, which can be inconvenient. Users are encouraged to + /// define their own macro that turns the `Option` into a proper error code and flattens the + /// inner result into it if it makes sense within their subsystem. + pub fn try_access_with R>(&self, f: F) -> Option { + self.try_access().map(|t| f(&*t)) + } + /// # Safety /// /// Callers must ensure that there are no more concurrent users of the revocable object. From patchwork Wed Mar 19 14:18:56 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Courbot X-Patchwork-Id: 14022646 X-Patchwork-Delegate: bhelgaas@google.com Received: from NAM12-DM6-obe.outbound.protection.outlook.com (mail-dm6nam12on2074.outbound.protection.outlook.com [40.107.243.74]) (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 3FD6D1C6FEE; Wed, 19 Mar 2025 14:19:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.243.74 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742393949; cv=fail; b=Rak/kCxBWUspGJbsYMWa4JosyBJF8PoNSEOuRxAds/WDcIqvx6eTsc2EgfPgyS0J2bNlc4uX5T4xOtY6eN3HyHybBIJ2aPO9iMk9TpEHhaRwvc8mw8GATjtSCk6EVo45ajMRoCz/SVaYBg4Sfftw5eh7pZ++pL8jRk0gMMTeOdI= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742393949; c=relaxed/simple; bh=sGuR/y+I/hfNrRHqw2C3aee/rQm2ahHYXY38fRTHYME=; h=From:Date:Subject:Content-Type:Message-Id:References:In-Reply-To: To:Cc:MIME-Version; b=m2z3/l2v+lAoNvt+Bc5E5urHnvB/4q2/085a5rxo7OhmJffBfrp7JZCtfJZSgQN+9MpI8Ce35kEu0ADR0+HjIyiaSwYN8xMRHatV5iQHbSBlQ92zITCQOUBlbsSW0zLLWEduTrdlOCmd9Z+5FFeiYDuHRmjpPL+BrQgvTnXfEgg= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=iN8S2byZ; arc=fail smtp.client-ip=40.107.243.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b="iN8S2byZ" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=r4SgmgjDc7Fj1amLDXUq1gq1fdU7DLV9mJbbETkVXRd+q2sB8UDozbGeWMTZPt2P1Ak76LRgOe9t2wrKZOuMUFTa0xO77rDRuTVFiDcN9OpUeLy2AFqrwgnyfhGgcAUWX2i+Nv9t6metTyGo1as6p7AvJ2zzTVRc4m2Xehuxfvzs+Ktt5NS4IJ2/RfT8fwdzoelDp1SH44wqXClvmelJyCoYD+fu4Zfr5Tt6z1QcFm+x11vFn/LNecPO+AoE0anaUeAnpRKvPLDR0F12ybejQEjB1AaPK1kYQyk/apTdmeJ1JHnSVBPUzO3EvkXeb+e9e0XyjkoSYX+zSFKtzasFtA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=g7SQmKroyePSTjssZFG1Ar1NnwprnQ5d7JfZrmaZ8qE=; b=Dbvz09I/7qKUPW3nJPUwKl360NWArmZ0ZI5OLCPGAkSz8RF8sABD247Az/tEw6YmV20uFhUj72GxLsnrqK192ZcoZro3DMJTH+Nz3We+lEsRIVXNc6y2k7ddSHxKmnfN466COBdBS2LxrOXyBYMGgFBdVJDklAgigII7ivGvsvzkT4DlMM/OuMgc27FAIA+EQT+B1dgMoBGfsAAraeggUefTkq0r+hbcFi8aGcT73PcVPAYJz4DPy6fLKsTomQAOs8wViacls9L1VRinNQCuPSCZXAesYmM9j9qBkbAAic3vG2Nq6AzsIocIUwQq1fabriLrOckfxp04WaLLT3yKfA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=g7SQmKroyePSTjssZFG1Ar1NnwprnQ5d7JfZrmaZ8qE=; b=iN8S2byZt1ckzV58gvvbpo+DSZInNCjpG5LRfLThIwmLhbEdUwUp5m8NSzN/rF5ecCLrXeMGlMcE7OHEBDuKZZo6iF5R8mrjvsQFIpj0q/INaIB7ACIImhYU33jsTyuH9cU+N+nKlCiC2lpUY3LJMhiYvQ6VTGS78r5nxpvwvT+NJmQ4EJS0nNqKjXtNcdgZctqOuOLC3w7+dQgDy3MqAzSlskUCJUvz45+aFFcyP2df/QkiWBIZoPUDoLXsehBy+E8Y8qtRO6mlKZk8mzpZPro3Co6zoixdbwbNEW7W64VueAPFEle0H2PBV60DkrScjsibljr758BVBk9In/4QOg== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from CH2PR12MB3990.namprd12.prod.outlook.com (2603:10b6:610:28::18) by MW4PR12MB5643.namprd12.prod.outlook.com (2603:10b6:303:188::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8534.34; Wed, 19 Mar 2025 14:19:06 +0000 Received: from CH2PR12MB3990.namprd12.prod.outlook.com ([fe80::6e37:569f:82ee:3f99]) by CH2PR12MB3990.namprd12.prod.outlook.com ([fe80::6e37:569f:82ee:3f99%6]) with mapi id 15.20.8534.034; Wed, 19 Mar 2025 14:19:06 +0000 From: Alexandre Courbot Date: Wed, 19 Mar 2025 23:18:56 +0900 Subject: [PATCH v2 2/2] samples: rust: convert PCI rust sample driver to use try_access_with() Message-Id: <20250319-try_with-v2-2-822ec63c05fb@nvidia.com> References: <20250319-try_with-v2-0-822ec63c05fb@nvidia.com> In-Reply-To: <20250319-try_with-v2-0-822ec63c05fb@nvidia.com> To: Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , Danilo Krummrich , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Bjorn Helgaas Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org, Alexandre Courbot X-Mailer: b4 0.14.2 X-ClientProxiedBy: TYCP301CA0087.JPNP301.PROD.OUTLOOK.COM (2603:1096:405:7b::6) To CH2PR12MB3990.namprd12.prod.outlook.com (2603:10b6:610:28::18) Precedence: bulk X-Mailing-List: linux-pci@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH2PR12MB3990:EE_|MW4PR12MB5643:EE_ X-MS-Office365-Filtering-Correlation-Id: 93339263-4de8-485c-0084-08dd66f101bd X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|10070799003|1800799024|376014|7416014|366016|921020; X-Microsoft-Antispam-Message-Info: =?utf-8?q?ApYaUuO0BV+n3MNHnjTB1FWgUoFXtnT?= =?utf-8?q?D6E/gqMMDtnTpqJb3IdAJ8DBREb5m3R9cPOvaZgb8URWelT957h+akP9OBkZAzfvI?= =?utf-8?q?oiFtL1a5rBu2xDxsuMpJHBM9bgtK4Try4aXEBJkxgzQA1txmby/2fRKWR2nF9JS6y?= =?utf-8?q?J7ZyzCjM9c1sGEKvBzlaZlOVIX0PHmKNi62JL68g3Cuk0MgeDd+rEgCFTgwzZ9pZP?= =?utf-8?q?VS/8VhDvol1SMEyI5HdCnFjr+OrqzQANVM5J2jTwGyC9rmsn5d53yoxvWO8Vu+faz?= =?utf-8?q?hS6gCnHfUztZOyYBEIEa0kMgpRdlO5bD2nIk8dmJgef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iV1c8Y2D4rtLEg6B+zbdu2?= =?utf-8?q?yBis0TKQH7JCZXS5ITqkgGBGIqEDbvQ6lw1lnZU2xfGlSwY1iNB3oqz1af4L6VTNE?= =?utf-8?q?3Od2cGV0Je51jlV/jqWNITkrtoNMYE6FtgedGNKhVrV/jfjk+dEYdL4G3VhvpSotl?= =?utf-8?q?5Dr7cLJAyFRgsb5pjuhQGUhmvHsjEftJgyXyGnBcZlxz9npmk3ghS6Q=3D?= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:CH2PR12MB3990.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(10070799003)(1800799024)(376014)(7416014)(366016)(921020);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?VxyKEHeWTUGYXnSdxXLQnrq9kVjv?= =?utf-8?q?PJacvXTj+khgQmhWe0u04S9sKBjtNRTvzBaktihW7tODJmSnL5rN+Icm5EB0rG1Yz?= =?utf-8?q?qbqcjpX8jKbVro4NfLip/S1ZzxtjQOvPTQv3hwBgp80rTZDAsX8JBPWYd63M2MKOz?= =?utf-8?q?qjr2xSJjzl1S/OS/3V7GO5D3nWIuoAimFwjBxGcaPlY1/qXzVArLk7XQr09kIn8AN?= =?utf-8?q?nx3GvfSTECzHqYmKaTDkjbSi/Px6wcmq02WHB1UEbDdyazITE7jjwZmN8cdX756Xp?= =?utf-8?q?d0FpJwye6kmFJJkIwG5QCPwXx0uEVZ+X7PqcRcQ9EUekcGK1oRUUZ1fjp8hIoeBhn?= =?utf-8?q?TjsCMkCeSUQs6iApy4O5M0MAjmy2s++wcVvPwDVwnrECYR/oI++ZCRukbmEcIugkK?= =?utf-8?q?oGreKlHL75324Sr3+4Yh3SSSTT+LqBWSBVpx8YCSffHsKeW7VwVOozf7qI+ts45uo?= =?utf-8?q?FRHScitkLLysnXk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FqsRYU7Jn8cOuqgWRAYfz3xYuYnOOT0KiybkFGKc4Qd87KvDzJ?= =?utf-8?q?VnTGD0bKv+Cg97h4/07Hjyei7+duGfbMaj7C5ck8QeJoSQfEJfUa7RuO8Lg6dT66E?= =?utf-8?q?A4mxhvhvNEWs2u2dKh/6KPAloFAnOTdGvdFx/fl21pP5z+dCk6hIkByAN/gYnDrdn?= =?utf-8?q?ccugSyNA4aK6OP0lc5G2lhhJmB0dO9MHoZHlBMvv58mEjBfnVVp/1P3mFIJLIQkLj?= =?utf-8?q?csDG7qb/BR4?= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 93339263-4de8-485c-0084-08dd66f101bd X-MS-Exchange-CrossTenant-AuthSource: CH2PR12MB3990.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 Mar 2025 14:19:06.1524 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: 5AiIDpvFstOgCKAoMXlgBwz/NKaG9VYqtHJOKUqxnfhlCkd79/BmeRsDa4xGlog+laDXFPIZoq8FfG+MPbcQwQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW4PR12MB5643 This method limits the scope of the revocable guard and is considered safer to use for most cases, so let's showcase it here. Signed-off-by: Alexandre Courbot Acked-by: Danilo Krummrich --- samples/rust/rust_driver_pci.rs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/samples/rust/rust_driver_pci.rs b/samples/rust/rust_driver_pci.rs index 1fb6e44f33951c521c8b086a7a3a012af911cf26..f2cb1c220bce42d161cf48664e8a5dd19770ba97 100644 --- a/samples/rust/rust_driver_pci.rs +++ b/samples/rust/rust_driver_pci.rs @@ -83,13 +83,12 @@ fn probe(pdev: &mut pci::Device, info: &Self::IdInfo) -> Result>> GFP_KERNEL, )?; - let bar = drvdata.bar.try_access().ok_or(ENXIO)?; + let res = drvdata + .bar + .try_access_with(|b| Self::testdev(info, b)) + .ok_or(ENXIO)??; - dev_info!( - pdev.as_ref(), - "pci-testdev data-match count: {}\n", - Self::testdev(info, &bar)? - ); + dev_info!(pdev.as_ref(), "pci-testdev data-match count: {}\n", res); Ok(drvdata.into()) }