From patchwork Mon Jun 3 15:53:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13683960 Received: from NAM11-CO1-obe.outbound.protection.outlook.com (mail-co1nam11on2068.outbound.protection.outlook.com [40.107.220.68]) (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 42E892576F; Mon, 3 Jun 2024 15:53:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.220.68 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717430014; cv=fail; b=LIQMOKihFfH9Key2q7dM3Vw7O2tsPahv8ztXcc12gXM/wUnnkU/JVjb5SoKmkXeSlBlYOJuyb8oHszqRO5ItNmUGap1CKiz32COLfO2GzlGSwbrt7jvMhgaDPIpOSf9nV3kCws10OgD6P15wOxxtq3RvE/4EtIKWmwg4AzvDPEI= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717430014; c=relaxed/simple; bh=AEYCTJ65gy0+wqFPeTtMg5SoCFWMgKAi9rCsp2EmSAo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=CMPmR8+1Ftt/yWQgeWoM8mHBzzs7fJbHTHl35rwt5itD9Vq+u59nPY2Ky3zvIAH9CNOwxn2M2PElNj77hg4+IfBBOtyeXsmQBbmNs4dXXF9NPxW4S3O/2szPmOq42ek4jASRxwiLUd3xlpDXhWm/0EXe7Ky/tA4COIMBTK/yUiM= 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=Z0a1JyPk; arc=fail smtp.client-ip=40.107.220.68 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="Z0a1JyPk" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=HYZ0BC338H3J+9jZsXWLYSasdixwGKeeXV4SigvRTJ9jlgilrdabVmxGkRizPyEe81uHLWo01zbmR0AQyseEgdC3zBj/M4ceCp9IgPqctRsQ/b+pRTc/lIQ56b4K0d/rLazwkaWCv2422AhZjkXdhti2Yc6qEc967FDn/CXbEt7iGH4S5FYLsk9zIe9esTM2PTeUN+nyxyWf9HFHevGLjwDT20Y7gTGo/IfMpMzOOMYPiLTb7O5JC9LUc9x5d8hk5ZiybTsPvm12pieqBBfERLQCcXusAID3n4vIk9RfbIDZ+xPMB1yPO7Cve7y8+DI3Ry/gFCHzAlTx7niQ15mkUw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; 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=8a66Ch5a6DnRuLMQiJuqgluaOzuZl4aSIf4u2RZsEhw=; b=D0V9qMtPTyxfar+9ix2tHg/jJ/7XY1XS+o/uDC8YNE7+UMu/CaJ7Fb9FDYONkZVkzwM/LH2Y4YDG8g8JHdsTqjSwhxmtHUru/K05C+YUaPfu//63W9akanhtVdp0wJzraCbimZ3qG0WJhAf+/yn2OlArzgVcmfUwGsqcV5cRKE2QZkIevx+ewriM0AEBmAFwGM0Ge8EogmQ8mTfW6MghHawqiqBisxp99kqVwgXmu/O7OC2KA9q3r3JD+bzjyVwXaVslbTKQIkqiYJ5tMgEqOGfNua5DGrD2q0+FsvOxrWSh1p1i8Ukbx3TZi3oViNkeg8d3JwbXq3eEyp3gh3O1Tg== 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=8a66Ch5a6DnRuLMQiJuqgluaOzuZl4aSIf4u2RZsEhw=; b=Z0a1JyPk4JNK+U4FldqMbgn9B8PWuk3uzx/l/R22G8UAphkex1IhFDSeBDViyzmwCj7FK+/I4bjtC0Ca5vYt2EonC2dZHi37f5fmsUX5cWIunyAsuNYd/p1c9ATjXyrTKTR9AvlgjeOsD2dvH/uHwbnpiTPhpjCcljaTxHyKtrkXDguQ2LV+QSViBMkVFjBVZX5wc7Zlbs3WzM6NlHOrOoc8bb3QGgjOCfB0uTfsbthfHDeubhZQPHKNOdnJLXQmQBmn++vjJjw3JChyhCy9W7kFpztkhkU0PYBrRV9RaaeAcNTTndvY6DxCk/Bpz3PuYIeMCmm0v8t5GnPxZlhiGA== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from DM6PR12MB3849.namprd12.prod.outlook.com (2603:10b6:5:1c7::26) by MN0PR12MB6197.namprd12.prod.outlook.com (2603:10b6:208:3c6::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7633.21; Mon, 3 Jun 2024 15:53:26 +0000 Received: from DM6PR12MB3849.namprd12.prod.outlook.com ([fe80::c296:774b:a5fc:965e]) by DM6PR12MB3849.namprd12.prod.outlook.com ([fe80::c296:774b:a5fc:965e%4]) with mapi id 15.20.7633.021; Mon, 3 Jun 2024 15:53:26 +0000 From: Jason Gunthorpe To: Jonathan Corbet , Itay Avraham , Jakub Kicinski , Leon Romanovsky , linux-doc@vger.kernel.org, linux-rdma@vger.kernel.org, netdev@vger.kernel.org, Paolo Abeni , Saeed Mahameed , Tariq Toukan Cc: Andy Gospodarek , Aron Silverton , Dan Williams , David Ahern , Christoph Hellwig , Jiri Pirko , Leonid Bloch , Leon Romanovsky , linux-cxl@vger.kernel.org, patches@lists.linux.dev Subject: [PATCH 3/8] fwctl: FWCTL_INFO to return basic information about the device Date: Mon, 3 Jun 2024 12:53:19 -0300 Message-ID: <3-v1-9912f1a11620+2a-fwctl_jgg@nvidia.com> In-Reply-To: <0-v1-9912f1a11620+2a-fwctl_jgg@nvidia.com> References: X-ClientProxiedBy: YT3PR01CA0009.CANPRD01.PROD.OUTLOOK.COM (2603:10b6:b01:86::21) To DM6PR12MB3849.namprd12.prod.outlook.com (2603:10b6:5:1c7::26) Precedence: bulk X-Mailing-List: linux-cxl@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6PR12MB3849:EE_|MN0PR12MB6197:EE_ X-MS-Office365-Filtering-Correlation-Id: c0280e73-9959-4ffb-d015-08dc83e54e17 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230031|1800799015|7416005|366007|376005|921011; X-Microsoft-Antispam-Message-Info: nIghvLiuVPhYq/o4sOz9W1WPwHsxoMHp7L7KtI70mavoXU39bGC/f6nbItxdVGZgmVoEO1jNN6q/XcLqcami5hbKOoCb89SjPW59rVAZj+EyQeGJ2xAUpOEShMylTfRSjfVjm5xHeYpmfUJZTled4BWlq8+xjqWOsv++nOu2O/IbHSIvx3L23keeSY1UbzAnSzrfsls4iHtnGEVq9dBcAFXRRYyIVJ4HpnwLm1G4bPtqmtDRJhgSVvNDBL40efVqIWVDyxuQsBkUl/LRLLVBZVcHX/sYlvVRC04KmyWYxt2PcRMd/pY9vEF2AU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;SRV:;IPV:NLI;SFV:NSPM;H:DM6PR12MB3849.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230031)(1800799015)(7416005)(366007)(376005)(921011);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: hknLPk9z35a6QPTymck7wWebUmeybQhztn+AWMzGd9+IkKHtC2t5CQvCZ90bppxIAzj0YdbYazvTx0LfZYo1TLBBJCsDCof4Hygh86QMzo5HULpnzPgmTdaxrnaZQmCaeZgdXzyX7cMrKyAri/ZDNOKIHVfdigiJOAL1b64/Z39jIhVbBWmZ5NK47cDqXxrEdC1jk4siijMI2eeg4Y5tUnp9DlavBhiqVcGZKT+Rt7oQ+EItndn2HhGSFUycLBhsNtkgvn2dZMq7HAubEGLfiKNdaabsYvQDD2AxipePFluRYgrmerSDJWSupjLdZNLaMeCKyizwrKJThzgSz5SRDLO/dS5wLltsZidF3REm71RVTUivlXpW0SYRMcP3s23lJoUoXdAw6a08RfvCNF0Dadi1lktXypKjibRaG1AQkJRBUMKwIFmZH9H8EErZBINmtTvSlVOaOcV9PNyp8a96m3FqwxXcBvDMaI9VYesGMbk1h/mUaEjoTO3lh83UxFsBmvHJA1Dz8+pOQnRUDX/LS6wmu5/KQV6p/lJKhGVcsbFtyvEX3NS9R9bHO3tYFxBhSXm5fKFrj0yGMEXZYnLo+40rH65+wMOy+TbDc4lcgysd3QOFkS3LPtc7Vmxy37leS3h3qYtuvIuaGVEOTaMur7LDTYiRERntlRQ9Gl2dw3KTG1PHib/Hq3/XkBWwuXXpXq0Y0zn74XMa6chaVIUuvILjhJjzxBW5ZqeaV9yvxV/azO1ITWH8ytUdK0tZPHLouUTCWIdoPXh6LJUra+UVoZIndycZspS+xMvx4ALQTRgfOwyZJXGRx0/E7kbvZoUT5cm42A8bptelKsi1EuO7NV4O0i6Ug6ZfXd9guiAWZcGE4HFnZtz8yw+oH1eTno10/UzVnYl7COQMW4n/xbFBReVNalofqDbS1d/0PN33fMThw9EgaVrV6dpNaajGw+TIXOro7RGHUK8Q8VZu01m+2TRTO5pmJm+qsq7u7Ad6Q7DqWDxWI8K74G3eOEKyF4iDvtWL8kRbFHwifY68YySk8ei02hP2AUAl7HYAFPs0aWz73oW99Nj3WMYhIvpLqc+QAzLaj3mn+rqVpXEGlVrNQLfGjh3gkwL+JOTyrNt0OuUh7DEQHLfcRuhSane1nw3saaX7J+n1E3Fmkfx6tqeCsxG1dOmS+4QiqWsuxbkNOe8dZQlzNGmu9o7aa3N3OE8nFsG9bZvefXJG6VE2BLde05Y0d7w4BO71iSkt21ad1Mozvhlusxg8qXLCIPx4xdYiLLsjRVD5ZiwJ67dTKx1Af4tCriC7fH16Fyf3v3KlARUauCLpNwJMWLKty1bA4iMMPcskpe/6xhFERFPMJF0frGVB+2a5mQgP0CROOgBH0+dy7m/Hg8m4HfDPrzjm1APEo7iG152YbN4omkvRJpxnqI2h+V0s5pP9ErKWPAX214DBxGwOtNoA+5Xpc++qF3NGcdY1pQWI30stjF1UsAn4JY19B0pv2IkqvwFkz2dfKiv1+ss0jFQolqyJtRL8x6hPOxTlvTzgLMpOHSzQ9twxab6SfG+ZJgJLCOpKniCvEK2KC/wBQsRw4bU/+8v64wN6 X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: c0280e73-9959-4ffb-d015-08dc83e54e17 X-MS-Exchange-CrossTenant-AuthSource: DM6PR12MB3849.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Jun 2024 15:53:26.3005 (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: T+hksErZcJH0PrWJ8hqTyvQ2+xh5UswJNkFY4PIb6Hn6JAWmduKecTbgMaeujcAk X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN0PR12MB6197 Userspace will need to know some details about the fwctl interface being used to locate the correct userspace code to communicate with the kernel. Provide a simple device_type enum indicating what the kernel driver is. Allow the device to provide a device specific info struct that contains any additional information that the driver may need to provide to userspace. Signed-off-by: Jason Gunthorpe --- drivers/fwctl/main.c | 54 ++++++++++++++++++++++++++++++++++++++ include/linux/fwctl.h | 8 ++++++ include/uapi/fwctl/fwctl.h | 29 ++++++++++++++++++++ 3 files changed, 91 insertions(+) diff --git a/drivers/fwctl/main.c b/drivers/fwctl/main.c index 7ecdabdd9dcb1e..10e3f504893892 100644 --- a/drivers/fwctl/main.c +++ b/drivers/fwctl/main.c @@ -17,6 +17,8 @@ enum { static dev_t fwctl_dev; static DEFINE_IDA(fwctl_ida); +DEFINE_FREE(kfree_errptr, void *, if (!IS_ERR_OR_NULL(_T)) kfree(_T)); + struct fwctl_ucmd { struct fwctl_uctx *uctx; void __user *ubuffer; @@ -24,8 +26,59 @@ struct fwctl_ucmd { u32 user_size; }; +static int ucmd_respond(struct fwctl_ucmd *ucmd, size_t cmd_len) +{ + if (copy_to_user(ucmd->ubuffer, ucmd->cmd, + min_t(size_t, ucmd->user_size, cmd_len))) + return -EFAULT; + return 0; +} + +static int copy_to_user_zero_pad(void __user *to, const void *from, + size_t from_len, size_t user_len) +{ + size_t copy_len; + + copy_len = min(from_len, user_len); + if (copy_to_user(to, from, copy_len)) + return -EFAULT; + if (copy_len < user_len) { + if (clear_user(to + copy_len, user_len - copy_len)) + return -EFAULT; + } + return 0; +} + +static int fwctl_cmd_info(struct fwctl_ucmd *ucmd) +{ + struct fwctl_device *fwctl = ucmd->uctx->fwctl; + struct fwctl_info *cmd = ucmd->cmd; + size_t driver_info_len = 0; + + if (cmd->flags) + return -EOPNOTSUPP; + + if (cmd->device_data_len) { + void *driver_info __free(kfree_errptr) = NULL; + + driver_info = fwctl->ops->info(ucmd->uctx, &driver_info_len); + if (IS_ERR(driver_info)) + return PTR_ERR(driver_info); + + if (copy_to_user_zero_pad(u64_to_user_ptr(cmd->out_device_data), + driver_info, driver_info_len, + cmd->device_data_len)) + return -EFAULT; + } + + cmd->out_device_type = fwctl->ops->device_type; + cmd->device_data_len = driver_info_len; + return ucmd_respond(ucmd, sizeof(*cmd)); +} + /* On stack memory for the ioctl structs */ union ucmd_buffer { + struct fwctl_info info; }; struct fwctl_ioctl_op { @@ -45,6 +98,7 @@ struct fwctl_ioctl_op { .execute = _fn, \ } static const struct fwctl_ioctl_op fwctl_ioctl_ops[] = { + IOCTL_OP(FWCTL_INFO, fwctl_cmd_info, struct fwctl_info, out_device_data), }; static long fwctl_fops_ioctl(struct file *filp, unsigned int cmd, diff --git a/include/linux/fwctl.h b/include/linux/fwctl.h index 1d9651de92fc19..9a906b861acf3a 100644 --- a/include/linux/fwctl.h +++ b/include/linux/fwctl.h @@ -7,12 +7,14 @@ #include #include #include +#include struct fwctl_device; struct fwctl_uctx; /** * struct fwctl_ops - Driver provided operations + * @device_type: The drivers assigned device_type number. This is uABI * @uctx_size: The size of the fwctl_uctx struct to allocate. The first * bytes of this memory will be a fwctl_uctx. The driver can use the * remaining bytes as its private memory. @@ -20,11 +22,17 @@ struct fwctl_uctx; * used. * @close_uctx: Called when the uctx is destroyed, usually when the FD is * closed. + * @info: Implement FWCTL_INFO. Return a kmalloc() memory that is copied to + * out_device_data. On input length indicates the size of the user buffer + * on output it indicates the size of the memory. The driver can ignore + * length on input, the core code will handle everything. */ struct fwctl_ops { + enum fwctl_device_type device_type; size_t uctx_size; int (*open_uctx)(struct fwctl_uctx *uctx); void (*close_uctx)(struct fwctl_uctx *uctx); + void *(*info)(struct fwctl_uctx *uctx, size_t *length); }; /** diff --git a/include/uapi/fwctl/fwctl.h b/include/uapi/fwctl/fwctl.h index 0bdce95b6d69d9..39db9f09f8068e 100644 --- a/include/uapi/fwctl/fwctl.h +++ b/include/uapi/fwctl/fwctl.h @@ -36,6 +36,35 @@ */ enum { FWCTL_CMD_BASE = 0, + FWCTL_CMD_INFO = 0, + FWCTL_CMD_RPC = 1, }; +enum fwctl_device_type { + FWCTL_DEVICE_TYPE_ERROR = 0, +}; + +/** + * struct fwctl_info - ioctl(FWCTL_INFO) + * @size: sizeof(struct fwctl_info) + * @flags: Must be 0 + * @out_device_type: Returns the type of the device from enum fwctl_device_type + * @device_data_len: On input the length of the out_device_data memory. On + * output the size of the kernel's device_data which may be larger or + * smaller than the input. Maybe 0 on input. + * @out_device_data: Pointer to a memory of device_data_len bytes. Kernel will + * fill the entire memory, zeroing as required. + * + * Returns basic information about this fwctl instance, particularly what driver + * is being used to define the device_data format. + */ +struct fwctl_info { + __u32 size; + __u32 flags; + __u32 out_device_type; + __u32 device_data_len; + __aligned_u64 out_device_data; +}; +#define FWCTL_INFO _IO(FWCTL_TYPE, FWCTL_CMD_INFO) + #endif