From nobody Wed Dec 17 22:40:55 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9F30CC54FB9 for ; Tue, 21 Nov 2023 07:06:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229985AbjKUHGf (ORCPT ); Tue, 21 Nov 2023 02:06:35 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48436 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229470AbjKUHGb (ORCPT ); Tue, 21 Nov 2023 02:06:31 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CE203F5 for ; Mon, 20 Nov 2023 23:06:27 -0800 (PST) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5F2DFC433C8; Tue, 21 Nov 2023 07:06:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700550387; bh=fRBJjZFPTR6OGb7k2gGjja7sHzz884j46t7cYBYv3r8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=DoxnnLt2h5sAHOExSrhg3cR5/5BXtrkiCv0ZiwrqokF+RY1/ZVSCXFSXEipTWeVDU CAReITvBovf9yexbC2/k6T/KVelOR2M4ZBunk3OWncJsqOUYMbHJSM1mUS17xGBsVC XzsUsT7eqkbSj935jcVGBo5T4i4r9r5tTqtxaVsQRCwZi1GvD+ZuEaqqLgx6vnyaO1 dGcdw8jho1cA589iON0MXnXHMVP6hn3nLFn//ASvI7T3w7KToSi3AbT6a8XSR2qFFE HucyxZECGiG1SI2F97NjolLsj192AOtolxBv3kB4hQJhZ0iHLoaArh7R5aDVEmqIz/ AyOP/+R8a1Ngw== From: Saeed Mahameed To: Arnd Bergmann , Greg Kroah-Hartman Cc: Jason Gunthorpe , Leon Romanovsky , Jiri Pirko , Leonid Bloch , Itay Avraham , Jakub Kicinski , linux-kernel@vger.kernel.org, Saeed Mahameed Subject: [PATCH V3 1/5] mlx5: Add aux dev for ctl interface Date: Mon, 20 Nov 2023 23:06:15 -0800 Message-ID: <20231121070619.9836-2-saeed@kernel.org> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20231121070619.9836-1-saeed@kernel.org> References: <20231121070619.9836-1-saeed@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Saeed Mahameed Allow ctl protocol interface auxiliary driver in mlx5. Signed-off-by: Saeed Mahameed --- drivers/net/ethernet/mellanox/mlx5/core/dev.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/drivers/net/ethernet/mellanox/mlx5/core/dev.c b/drivers/net/et= hernet/mellanox/mlx5/core/dev.c index cf0477f53dc4..753c11f7050d 100644 --- a/drivers/net/ethernet/mellanox/mlx5/core/dev.c +++ b/drivers/net/ethernet/mellanox/mlx5/core/dev.c @@ -228,8 +228,14 @@ enum { MLX5_INTERFACE_PROTOCOL_VNET, =20 MLX5_INTERFACE_PROTOCOL_DPLL, + MLX5_INTERFACE_PROTOCOL_CTL, }; =20 +static bool is_ctl_supported(struct mlx5_core_dev *dev) +{ + return MLX5_CAP_GEN(dev, uctx_cap); +} + static const struct mlx5_adev_device { const char *suffix; bool (*is_supported)(struct mlx5_core_dev *dev); @@ -252,6 +258,8 @@ static const struct mlx5_adev_device { .is_supported =3D &is_mp_supported }, [MLX5_INTERFACE_PROTOCOL_DPLL] =3D { .suffix =3D "dpll", .is_supported =3D &is_dpll_supported }, + [MLX5_INTERFACE_PROTOCOL_CTL] =3D { .suffix =3D "ctl", + .is_supported =3D &is_ctl_supported }, }; =20 int mlx5_adev_idx_alloc(void) --=20 2.42.0 From nobody Wed Dec 17 22:40:55 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 23781C5ACB3 for ; Tue, 21 Nov 2023 07:06:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231201AbjKUHGl (ORCPT ); Tue, 21 Nov 2023 02:06:41 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35362 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229754AbjKUHGe (ORCPT ); Tue, 21 Nov 2023 02:06:34 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DF92710C for ; Mon, 20 Nov 2023 23:06:28 -0800 (PST) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 77D5AC433C8; Tue, 21 Nov 2023 07:06:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700550388; bh=isJE+4To73RbzPw8SDE1Zz0T/qfFE6bocOEKEudicH0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=mi+Pgl1wvwnpVnwtnnEbnNS/+QC1P52sQYHZkYBuCnDBJwAArVD/Kq7fWQomA5Pdu 2CxDukFP8BH6Y0blRhzcNotoC6HuBo+y9S8iTokbJKbELKo14vbyibKX6sF+oKGYBN C/ib9SfRFWM0tf1I50O1GEwR50d8jtOya92JpLYwtyPYv8HoJeBBcMOTTgIrfItWQA nxPmFhEhuTpp670oN2OsvROjbiRfFkFthNa5WInBz+HZPoZ6PdOFr48nyNwhZInwva cH/VfJsmXyg6zLkLmQ/VYmymrjMeYc2Kh0D3ljFR0Nu0Z49kM5tt7hHtkC6hi2qc7o LZz+dTpbWHVlQ== From: Saeed Mahameed To: Arnd Bergmann , Greg Kroah-Hartman Cc: Jason Gunthorpe , Leon Romanovsky , Jiri Pirko , Leonid Bloch , Itay Avraham , Jakub Kicinski , linux-kernel@vger.kernel.org, Saeed Mahameed Subject: [PATCH V3 2/5] misc: mlx5ctl: Add mlx5ctl misc driver Date: Mon, 20 Nov 2023 23:06:16 -0800 Message-ID: <20231121070619.9836-3-saeed@kernel.org> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20231121070619.9836-1-saeed@kernel.org> References: <20231121070619.9836-1-saeed@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Saeed Mahameed The ConnectX HW family supported by the mlx5 drivers uses an architecture where a FW component executes "mailbox RPCs" issued by the driver to make changes to the device. This results in a complex debugging environment where the FW component has information and low level configuration that needs to be accessed to userspace for debugging purposes. Historically a userspace program was used that accessed the PCI register and config space directly through /sys/bus/pci/.../XXX and could operate these debugging interfaces in parallel with the running driver. This approach is incompatible with secure boot and kernel lockdown so this driver provides a secure and restricted interface to that same data. On open the driver would allocate a special FW UID (user context ID) restrected to debug RPCs only, later in this series all user RPCs will be stamped with this UID. Reviewed-by: Jiri Pirko Reviewed-by: Leon Romanovsky Signed-off-by: Saeed Mahameed --- MAINTAINERS | 8 + drivers/misc/Kconfig | 1 + drivers/misc/Makefile | 1 + drivers/misc/mlx5ctl/Kconfig | 14 ++ drivers/misc/mlx5ctl/Makefile | 4 + drivers/misc/mlx5ctl/main.c | 314 ++++++++++++++++++++++++++++++++++ 6 files changed, 342 insertions(+) create mode 100644 drivers/misc/mlx5ctl/Kconfig create mode 100644 drivers/misc/mlx5ctl/Makefile create mode 100644 drivers/misc/mlx5ctl/main.c diff --git a/MAINTAINERS b/MAINTAINERS index 97f51d5ec1cf..4b532df16b19 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -13835,6 +13835,14 @@ L: virtualization@lists.linux-foundation.org S: Supported F: drivers/vdpa/mlx5/ =20 +MELLANOX MLX5 ConnectX Diag DRIVER +M: Saeed Mahameed +R: Itay Avraham +L: linux-kernel@vger.kernel.org +S: Supported +F: drivers/misc/mlx5ctl/ +F: include/uapi/misc/mlx5ctl.h + MELLANOX MLXCPLD I2C AND MUX DRIVER M: Vadim Pasternak M: Michael Shych diff --git a/drivers/misc/Kconfig b/drivers/misc/Kconfig index f37c4b8380ae..c0e4823648ed 100644 --- a/drivers/misc/Kconfig +++ b/drivers/misc/Kconfig @@ -579,4 +579,5 @@ source "drivers/misc/cardreader/Kconfig" source "drivers/misc/uacce/Kconfig" source "drivers/misc/pvpanic/Kconfig" source "drivers/misc/mchp_pci1xxxx/Kconfig" +source "drivers/misc/mlx5ctl/Kconfig" endmenu diff --git a/drivers/misc/Makefile b/drivers/misc/Makefile index f2a4d1ff65d4..49bc4697f498 100644 --- a/drivers/misc/Makefile +++ b/drivers/misc/Makefile @@ -67,3 +67,4 @@ obj-$(CONFIG_TMR_MANAGER) +=3D xilinx_tmr_manager.o obj-$(CONFIG_TMR_INJECT) +=3D xilinx_tmr_inject.o obj-$(CONFIG_TPS6594_ESM) +=3D tps6594-esm.o obj-$(CONFIG_TPS6594_PFSM) +=3D tps6594-pfsm.o +obj-$(CONFIG_MLX5CTL) +=3D mlx5ctl/ diff --git a/drivers/misc/mlx5ctl/Kconfig b/drivers/misc/mlx5ctl/Kconfig new file mode 100644 index 000000000000..faaa1dba2cc2 --- /dev/null +++ b/drivers/misc/mlx5ctl/Kconfig @@ -0,0 +1,14 @@ +# SPDX-License-Identifier: GPL-2.0 +# + +config MLX5CTL + tristate "mlx5 ConnectX control misc driver" + depends on MLX5_CORE + help + MLX5CTL provides interface for the user process to access the debug and + configuration registers of the ConnectX hardware family + (NICs, PCI switches and SmartNIC SoCs). + This will allow configuration and debug tools to work out of the= box on + mainstream kernel. + + If you don't know what to do here, say N. diff --git a/drivers/misc/mlx5ctl/Makefile b/drivers/misc/mlx5ctl/Makefile new file mode 100644 index 000000000000..b5c7f99e0ab6 --- /dev/null +++ b/drivers/misc/mlx5ctl/Makefile @@ -0,0 +1,4 @@ +# SPDX-License-Identifier: GPL-2.0 + +obj-$(CONFIG_MLX5CTL) +=3D mlx5ctl.o +mlx5ctl-y :=3D main.o diff --git a/drivers/misc/mlx5ctl/main.c b/drivers/misc/mlx5ctl/main.c new file mode 100644 index 000000000000..8eb150461b80 --- /dev/null +++ b/drivers/misc/mlx5ctl/main.c @@ -0,0 +1,314 @@ +// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0 +/* Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserve= d. */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +MODULE_DESCRIPTION("mlx5 ConnectX control misc driver"); +MODULE_AUTHOR("Saeed Mahameed "); +MODULE_LICENSE("Dual BSD/GPL"); + +struct mlx5ctl_dev { + struct mlx5_core_dev *mdev; + struct miscdevice miscdev; + struct auxiliary_device *adev; + struct list_head fd_list; + spinlock_t fd_list_lock; /* protect list add/del */ + struct rw_semaphore rw_lock; + struct kref refcount; +}; + +struct mlx5ctl_fd { + u16 uctx_uid; + u32 uctx_cap; + u32 ucap; /* user cap */ + struct mlx5ctl_dev *mcdev; + struct list_head list; +}; + +#define mlx5ctl_err(mcdev, format, ...) \ + dev_err(mcdev->miscdev.parent, format, ##__VA_ARGS__) + +#define mlx5ctl_dbg(mcdev, format, ...) \ + dev_dbg(mcdev->miscdev.parent, "PID %d: " format, \ + current->pid, ##__VA_ARGS__) + +enum { + MLX5_UCTX_OBJECT_CAP_RAW_TX =3D 0x1, + MLX5_UCTX_OBJECT_CAP_INTERNAL_DEVICE_RESOURCES =3D 0x2, + MLX5_UCTX_OBJECT_CAP_TOOLS_RESOURCES =3D 0x4, +}; + +static int mlx5ctl_alloc_uid(struct mlx5ctl_dev *mcdev, u32 cap) +{ + u32 out[MLX5_ST_SZ_DW(create_uctx_out)] =3D {}; + u32 in[MLX5_ST_SZ_DW(create_uctx_in)] =3D {}; + void *uctx; + int err; + u16 uid; + + uctx =3D MLX5_ADDR_OF(create_uctx_in, in, uctx); + + mlx5ctl_dbg(mcdev, "MLX5_CMD_OP_CREATE_UCTX: caps 0x%x\n", cap); + MLX5_SET(create_uctx_in, in, opcode, MLX5_CMD_OP_CREATE_UCTX); + MLX5_SET(uctx, uctx, cap, cap); + + err =3D mlx5_cmd_exec(mcdev->mdev, in, sizeof(in), out, sizeof(out)); + if (err) + return err; + + uid =3D MLX5_GET(create_uctx_out, out, uid); + mlx5ctl_dbg(mcdev, "allocated uid %d with caps 0x%x\n", uid, cap); + return uid; +} + +static void mlx5ctl_release_uid(struct mlx5ctl_dev *mcdev, u16 uid) +{ + u32 in[MLX5_ST_SZ_DW(destroy_uctx_in)] =3D {}; + struct mlx5_core_dev *mdev =3D mcdev->mdev; + int err; + + MLX5_SET(destroy_uctx_in, in, opcode, MLX5_CMD_OP_DESTROY_UCTX); + MLX5_SET(destroy_uctx_in, in, uid, uid); + + err =3D mlx5_cmd_exec_in(mdev, destroy_uctx, in); + mlx5ctl_dbg(mcdev, "released uid %d err(%d)\n", uid, err); +} + +static void mcdev_get(struct mlx5ctl_dev *mcdev); +static void mcdev_put(struct mlx5ctl_dev *mcdev); + +static int mlx5ctl_open_mfd(struct mlx5ctl_fd *mfd) +{ + struct mlx5_core_dev *mdev =3D mfd->mcdev->mdev; + struct mlx5ctl_dev *mcdev =3D mfd->mcdev; + u32 ucap =3D 0, cap =3D 0; + int uid; + +#define MLX5_UCTX_CAP(mdev, cap) \ + (MLX5_CAP_GEN(mdev, uctx_cap) & MLX5_UCTX_OBJECT_CAP_##cap) + + if (capable(CAP_NET_RAW) && MLX5_UCTX_CAP(mdev, RAW_TX)) { + ucap |=3D CAP_NET_RAW; + cap |=3D MLX5_UCTX_OBJECT_CAP_RAW_TX; + } + + if (capable(CAP_SYS_RAWIO) && MLX5_UCTX_CAP(mdev, INTERNAL_DEVICE_RESOURC= ES)) { + ucap |=3D CAP_SYS_RAWIO; + cap |=3D MLX5_UCTX_OBJECT_CAP_INTERNAL_DEVICE_RESOURCES; + } + + if (capable(CAP_SYS_ADMIN) && MLX5_UCTX_CAP(mdev, TOOLS_RESOURCES)) { + ucap |=3D CAP_SYS_ADMIN; + cap |=3D MLX5_UCTX_OBJECT_CAP_TOOLS_RESOURCES; + } + + uid =3D mlx5ctl_alloc_uid(mcdev, cap); + if (uid < 0) + return uid; + + mfd->uctx_uid =3D uid; + mfd->uctx_cap =3D cap; + mfd->ucap =3D ucap; + mfd->mcdev =3D mcdev; + + mlx5ctl_dbg(mcdev, "allocated uid %d with uctx caps 0x%x, user cap 0x%x\n= ", + uid, cap, ucap); + return 0; +} + +static void mlx5ctl_release_mfd(struct mlx5ctl_fd *mfd) +{ + struct mlx5ctl_dev *mcdev =3D mfd->mcdev; + + mlx5ctl_release_uid(mcdev, mfd->uctx_uid); +} + +static int mlx5ctl_open(struct inode *inode, struct file *file) +{ + struct mlx5_core_dev *mdev; + struct mlx5ctl_dev *mcdev; + struct mlx5ctl_fd *mfd; + int err =3D 0; + + mcdev =3D container_of(file->private_data, struct mlx5ctl_dev, miscdev); + mcdev_get(mcdev); + down_read(&mcdev->rw_lock); + mdev =3D mcdev->mdev; + if (!mdev) { + err =3D -ENODEV; + goto unlock; + } + + mfd =3D kzalloc(sizeof(*mfd), GFP_KERNEL_ACCOUNT); + if (!mfd) + return -ENOMEM; + + mfd->mcdev =3D mcdev; + err =3D mlx5ctl_open_mfd(mfd); + if (err) + goto unlock; + + spin_lock(&mcdev->fd_list_lock); + list_add_tail(&mfd->list, &mcdev->fd_list); + spin_unlock(&mcdev->fd_list_lock); + + file->private_data =3D mfd; + +unlock: + up_read(&mcdev->rw_lock); + if (err) { + mcdev_put(mcdev); + kfree(mfd); + } + return err; +} + +static int mlx5ctl_release(struct inode *inode, struct file *file) +{ + struct mlx5ctl_fd *mfd =3D file->private_data; + struct mlx5ctl_dev *mcdev =3D mfd->mcdev; + + down_read(&mcdev->rw_lock); + if (!mcdev->mdev) { + pr_debug("[%d] UID %d mlx5ctl: mdev is already released\n", + current->pid, mfd->uctx_uid); + /* All mfds are already released, skip ... */ + goto unlock; + } + + spin_lock(&mcdev->fd_list_lock); + list_del(&mfd->list); + spin_unlock(&mcdev->fd_list_lock); + + mlx5ctl_release_mfd(mfd); + +unlock: + kfree(mfd); + up_read(&mcdev->rw_lock); + mcdev_put(mcdev); + file->private_data =3D NULL; + return 0; +} + +static const struct file_operations mlx5ctl_fops =3D { + .owner =3D THIS_MODULE, + .open =3D mlx5ctl_open, + .release =3D mlx5ctl_release, +}; + +static int mlx5ctl_probe(struct auxiliary_device *adev, + const struct auxiliary_device_id *id) + +{ + struct mlx5_adev *madev =3D container_of(adev, struct mlx5_adev, adev); + struct mlx5_core_dev *mdev =3D madev->mdev; + struct mlx5ctl_dev *mcdev; + char *devname =3D NULL; + int err; + + mcdev =3D kzalloc(sizeof(*mcdev), GFP_KERNEL_ACCOUNT); + if (!mcdev) + return -ENOMEM; + + kref_init(&mcdev->refcount); + INIT_LIST_HEAD(&mcdev->fd_list); + spin_lock_init(&mcdev->fd_list_lock); + init_rwsem(&mcdev->rw_lock); + mcdev->mdev =3D mdev; + mcdev->adev =3D adev; + devname =3D kasprintf(GFP_KERNEL_ACCOUNT, "mlx5ctl-%s", + dev_name(&adev->dev)); + if (!devname) { + err =3D -ENOMEM; + goto abort; + } + + mcdev->miscdev =3D (struct miscdevice) { + .minor =3D MISC_DYNAMIC_MINOR, + .name =3D devname, + .fops =3D &mlx5ctl_fops, + .parent =3D &adev->dev, + }; + + err =3D misc_register(&mcdev->miscdev); + if (err) { + mlx5ctl_err(mcdev, "mlx5ctl: failed to register misc device err %d\n", e= rr); + goto abort; + } + + mlx5ctl_dbg(mcdev, "probe mdev@%s %s\n", dev_driver_string(mdev->device),= dev_name(mdev->device)); + + auxiliary_set_drvdata(adev, mcdev); + + return 0; + +abort: + kfree(devname); + kfree(mcdev); + return err; +} + +static void mlx5ctl_remove(struct auxiliary_device *adev) +{ + struct mlx5ctl_dev *mcdev =3D auxiliary_get_drvdata(adev); + struct mlx5_core_dev *mdev =3D mcdev->mdev; + struct mlx5ctl_fd *mfd, *n; + + misc_deregister(&mcdev->miscdev); + down_write(&mcdev->rw_lock); + + list_for_each_entry_safe(mfd, n, &mcdev->fd_list, list) { + mlx5ctl_dbg(mcdev, "UID %d still has open FDs\n", mfd->uctx_uid); + list_del(&mfd->list); + mlx5ctl_release_mfd(mfd); + } + + mlx5ctl_dbg(mcdev, "removed mdev %s %s\n", + dev_driver_string(mdev->device), dev_name(mdev->device)); + + mcdev->mdev =3D NULL; /* prevent already open fds from accessing the devi= ce */ + up_write(&mcdev->rw_lock); + mcdev_put(mcdev); +} + +static void mcdev_free(struct kref *ref) +{ + struct mlx5ctl_dev *mcdev =3D container_of(ref, struct mlx5ctl_dev, refco= unt); + + kfree(mcdev->miscdev.name); + kfree(mcdev); +} + +static void mcdev_get(struct mlx5ctl_dev *mcdev) +{ + kref_get(&mcdev->refcount); +} + +static void mcdev_put(struct mlx5ctl_dev *mcdev) +{ + kref_put(&mcdev->refcount, mcdev_free); +} + +static const struct auxiliary_device_id mlx5ctl_id_table[] =3D { + { .name =3D MLX5_ADEV_NAME ".ctl", }, + {}, +}; + +MODULE_DEVICE_TABLE(auxiliary, mlx5ctl_id_table); + +static struct auxiliary_driver mlx5ctl_driver =3D { + .name =3D "ctl", + .probe =3D mlx5ctl_probe, + .remove =3D mlx5ctl_remove, + .id_table =3D mlx5ctl_id_table, +}; + +module_auxiliary_driver(mlx5ctl_driver); --=20 2.42.0 From nobody Wed Dec 17 22:40:55 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BF399C5ACB3 for ; Tue, 21 Nov 2023 07:06:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229612AbjKUHGo (ORCPT ); Tue, 21 Nov 2023 02:06:44 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35368 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229905AbjKUHGe (ORCPT ); Tue, 21 Nov 2023 02:06:34 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EE3DDBC for ; Mon, 20 Nov 2023 23:06:29 -0800 (PST) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8A9CAC433C9; Tue, 21 Nov 2023 07:06:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700550389; bh=p6LbyrzBaLsCjAoS9SsNRnPCnmltE2l6oztiZUD2C0A=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=A7U2fOEZsuWy1uv4zyWIq1VyTmJWxplU9kRS136V+4Io1AH3nNXv+CKmclItrLpfr 19yb5KBD9b7mh75Tn9Q+Eqzx8TKNi13r2247n/QqBk9qXIIJnqmKE/o3eUKM1ontUg V2WGHNh0s5FXNFaAIuH9hDcqlhsLh9Qd7eEYlHKHCfpI6arJvvgtuZb+Bhawso46IB dX80bOV6gXRQgX+IeribijtPDj1ME5ikTzNDqZDZK8uoPBfbAJog0WEfWooUfDqvs6 EjEFdKrMxq42fiODtzUiNfYwgw5u+TTa3pQz6QgIPQfuyBDiluCSx4mjRScCpGpfks eFK+CDpEUJW5w== From: Saeed Mahameed To: Arnd Bergmann , Greg Kroah-Hartman Cc: Jason Gunthorpe , Leon Romanovsky , Jiri Pirko , Leonid Bloch , Itay Avraham , Jakub Kicinski , linux-kernel@vger.kernel.org, Saeed Mahameed Subject: [PATCH V3 3/5] misc: mlx5ctl: Add info ioctl Date: Mon, 20 Nov 2023 23:06:17 -0800 Message-ID: <20231121070619.9836-4-saeed@kernel.org> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20231121070619.9836-1-saeed@kernel.org> References: <20231121070619.9836-1-saeed@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Saeed Mahameed Implement INFO ioctl to return the allocated UID and the capability flags and some other useful device information such as the underlying ConnectX device. Example: $ sudo ./mlx5ctlu mlx5_core.ctl.0 mlx5dev: 0000:00:04.0 UCTX UID: 1 UCTX CAP: 0x3 DEV UCTX CAP: 0x3 USER CAP: 0x1d Reviewed-by: Jiri Pirko Reviewed-by: Leon Romanovsky Signed-off-by: Saeed Mahameed --- .../userspace-api/ioctl/ioctl-number.rst | 1 + drivers/misc/mlx5ctl/main.c | 71 +++++++++++++++++++ include/uapi/misc/mlx5ctl.h | 24 +++++++ 3 files changed, 96 insertions(+) create mode 100644 include/uapi/misc/mlx5ctl.h diff --git a/Documentation/userspace-api/ioctl/ioctl-number.rst b/Documenta= tion/userspace-api/ioctl/ioctl-number.rst index 4ea5b837399a..9faf91ffefff 100644 --- a/Documentation/userspace-api/ioctl/ioctl-number.rst +++ b/Documentation/userspace-api/ioctl/ioctl-number.rst @@ -89,6 +89,7 @@ Code Seq# Include File = Comments 0x20 all drivers/cdrom/cm206.h 0x22 all scsi/sg.h 0x3E 00-0F linux/counter.h +0x5c all uapi/misc/mlx5ctl.h Nvidi= a ConnectX control '!' 00-1F uapi/linux/seccomp.h '#' 00-3F IEEE = 1394 Subsystem Block= for the entire subsystem diff --git a/drivers/misc/mlx5ctl/main.c b/drivers/misc/mlx5ctl/main.c index 8eb150461b80..6a98b40e4300 100644 --- a/drivers/misc/mlx5ctl/main.c +++ b/drivers/misc/mlx5ctl/main.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include =20 @@ -198,10 +199,80 @@ static int mlx5ctl_release(struct inode *inode, struc= t file *file) return 0; } =20 +static int mlx5ctl_info_ioctl(struct file *file, + struct mlx5ctl_info __user *arg, + size_t usize) +{ + struct mlx5ctl_fd *mfd =3D file->private_data; + struct mlx5ctl_dev *mcdev =3D mfd->mcdev; + struct mlx5_core_dev *mdev =3D mcdev->mdev; + struct mlx5ctl_info *info; + size_t ksize =3D 0; + int err =3D 0; + + ksize =3D max(sizeof(struct mlx5ctl_info), usize); + info =3D kzalloc(ksize, GFP_KERNEL_ACCOUNT); + if (!info) + return -ENOMEM; + + info->size =3D sizeof(struct mlx5ctl_info); + + info->dev_uctx_cap =3D MLX5_CAP_GEN(mdev, uctx_cap); + info->uctx_cap =3D mfd->uctx_cap; + info->uctx_uid =3D mfd->uctx_uid; + info->ucap =3D mfd->ucap; + + strscpy(info->devname, dev_name(&mdev->pdev->dev), + sizeof(info->devname)); + + if (copy_to_user(arg, info, usize)) + err =3D -EFAULT; + + kfree(info); + return err; +} + +static long mlx5ctl_ioctl(struct file *file, unsigned int cmd, unsigned lo= ng arg) +{ + struct mlx5ctl_fd *mfd =3D file->private_data; + struct mlx5ctl_dev *mcdev =3D mfd->mcdev; + void __user *argp =3D (void __user *)arg; + size_t size =3D _IOC_SIZE(cmd); + int err =3D 0; + + if (!capable(CAP_SYS_ADMIN)) + return -EPERM; + + mlx5ctl_dbg(mcdev, "ioctl 0x%x type/nr: %d/%d size: %d DIR:%d\n", cmd, + _IOC_TYPE(cmd), _IOC_NR(cmd), _IOC_SIZE(cmd), _IOC_DIR(cmd)); + + down_read(&mcdev->rw_lock); + if (!mcdev->mdev) { + err =3D -ENODEV; + goto unlock; + } + + switch (cmd) { + case MLX5CTL_IOCTL_INFO: + err =3D mlx5ctl_info_ioctl(file, argp, size); + break; + + default: + mlx5ctl_dbg(mcdev, "Unknown ioctl %x\n", cmd); + err =3D -ENOIOCTLCMD; + break; + } +unlock: + up_read(&mcdev->rw_lock); + return err; +} + static const struct file_operations mlx5ctl_fops =3D { .owner =3D THIS_MODULE, .open =3D mlx5ctl_open, .release =3D mlx5ctl_release, + .unlocked_ioctl =3D mlx5ctl_ioctl, + .compat_ioctl =3D compat_ptr_ioctl, }; =20 static int mlx5ctl_probe(struct auxiliary_device *adev, diff --git a/include/uapi/misc/mlx5ctl.h b/include/uapi/misc/mlx5ctl.h new file mode 100644 index 000000000000..37153cc0fc6e --- /dev/null +++ b/include/uapi/misc/mlx5ctl.h @@ -0,0 +1,24 @@ +/* SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0 WITH Linux-syscall-not= e */ +/* Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserve= d. */ + +#ifndef __MLX5CTL_IOCTL_H__ +#define __MLX5CTL_IOCTL_H__ + +struct mlx5ctl_info { + __aligned_u64 flags; + __u32 size; + __u8 devname[64]; /* underlaying ConnectX device */ + __u16 uctx_uid; /* current process allocated UCTX UID */ + __u16 reserved1; + __u32 uctx_cap; /* current process effective UCTX cap */ + __u32 dev_uctx_cap; /* device's UCTX capabilities */ + __u32 ucap; /* process user capability */ + __u32 reserved2; +}; + +#define MLX5CTL_IOCTL_MAGIC 0x5c + +#define MLX5CTL_IOCTL_INFO \ + _IOR(MLX5CTL_IOCTL_MAGIC, 0x0, struct mlx5ctl_info) + +#endif /* __MLX5CTL_IOCTL_H__ */ --=20 2.42.0 From nobody Wed Dec 17 22:40:55 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B2DFAC54FB9 for ; Tue, 21 Nov 2023 07:06:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233396AbjKUHGu (ORCPT ); Tue, 21 Nov 2023 02:06:50 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35356 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230080AbjKUHGj (ORCPT ); Tue, 21 Nov 2023 02:06:39 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 03C38C8 for ; Mon, 20 Nov 2023 23:06:30 -0800 (PST) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 899DFC433C7; Tue, 21 Nov 2023 07:06:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700550390; bh=U5lgYFwJwveZX6VvJjduX3a3pzJmocFwMI7ltZ6S+/E=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=mZgAfpbVELIbYq17PN8dBD+0bxFM89kOkTJqW+3cD/E8t4OLkYtT9mivstfh1CW63 WH7KYghFNd2FEoxu4w8PJpX20RJSvEQOBNrHJy9ldnm1zqpCnTXPBT2FIIzNDqsF7R dAEsoiX5ibla+vDtvHQQOjmhQsaeBYD+rYOQh8E5pgYnWJRmX9A840NXgKXtpdJcPE DFBW5HULSIXq8C67XduWuGbKMRT7mQ7MLUn6nZVToCRbyg5cCbbSFBHXq2j6jLiP3e vhJTUH1orFVUvdU/F2lIrmhcFmzSPbKmTnVdWjzONdmuwPOpbuIR8CARgIFToosYv6 pzS/rLTwM1I0A== From: Saeed Mahameed To: Arnd Bergmann , Greg Kroah-Hartman Cc: Jason Gunthorpe , Leon Romanovsky , Jiri Pirko , Leonid Bloch , Itay Avraham , Jakub Kicinski , linux-kernel@vger.kernel.org, Saeed Mahameed Subject: [PATCH V3 4/5] misc: mlx5ctl: Add command rpc ioctl Date: Mon, 20 Nov 2023 23:06:18 -0800 Message-ID: <20231121070619.9836-5-saeed@kernel.org> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20231121070619.9836-1-saeed@kernel.org> References: <20231121070619.9836-1-saeed@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Saeed Mahameed Add new IOCTL to allow user space to send device debug rpcs and attach the user's uctx UID to each rpc. In the mlx5 architecture the FW RPC commands are of the format of inbox and outbox buffers. The inbox buffer contains the command rpc layout as described in the ConnectX Programmers Reference Manual (PRM) document and as defined in include/linux/mlx5/mlx5_ifc.h. On success the user outbox buffer will be filled with the device's rpc response. For example to query device capabilities: a user fills out an inbox buffer with the inbox layout: struct mlx5_ifc_query_hca_cap_in_bits and expects an outbox buffer with the layout: struct mlx5_ifc_cmd_hca_cap_bits Reviewed-by: Jiri Pirko Reviewed-by: Leon Romanovsky Signed-off-by: Saeed Mahameed --- drivers/misc/mlx5ctl/main.c | 95 +++++++++++++++++++++++++++++++++++++ include/uapi/misc/mlx5ctl.h | 13 +++++ 2 files changed, 108 insertions(+) diff --git a/drivers/misc/mlx5ctl/main.c b/drivers/misc/mlx5ctl/main.c index 6a98b40e4300..e7776ea4bfca 100644 --- a/drivers/misc/mlx5ctl/main.c +++ b/drivers/misc/mlx5ctl/main.c @@ -232,6 +232,97 @@ static int mlx5ctl_info_ioctl(struct file *file, return err; } =20 +struct mlx5_ifc_mbox_in_hdr_bits { + u8 opcode[0x10]; + u8 uid[0x10]; + + u8 reserved_at_20[0x10]; + u8 op_mod[0x10]; + + u8 reserved_at_40[0x40]; +}; + +struct mlx5_ifc_mbox_out_hdr_bits { + u8 status[0x8]; + u8 reserved_at_8[0x18]; + + u8 syndrome[0x20]; + + u8 reserved_at_40[0x40]; +}; + +static int mlx5ctl_cmdrpc_ioctl(struct file *file, + struct mlx5ctl_cmdrpc __user *arg, + size_t usize) +{ + struct mlx5ctl_fd *mfd =3D file->private_data; + struct mlx5ctl_dev *mcdev =3D mfd->mcdev; + struct mlx5ctl_cmdrpc *rpc =3D NULL; + void *in =3D NULL, *out =3D NULL; + size_t ksize =3D 0; + int err; + + ksize =3D max(sizeof(struct mlx5ctl_cmdrpc), usize); + rpc =3D kzalloc(ksize, GFP_KERNEL_ACCOUNT); + if (!rpc) + return -ENOMEM; + + err =3D copy_from_user(rpc, arg, usize); + if (err) + goto out; + + mlx5ctl_dbg(mcdev, "[UID %d] cmdrpc: rpc->inlen %d rpc->outlen %d\n", + mfd->uctx_uid, rpc->inlen, rpc->outlen); + + if (rpc->inlen < MLX5_ST_SZ_BYTES(mbox_in_hdr) || + rpc->outlen < MLX5_ST_SZ_BYTES(mbox_out_hdr) || + rpc->inlen > MLX5CTL_MAX_RPC_SIZE || + rpc->outlen > MLX5CTL_MAX_RPC_SIZE) { + err =3D -EINVAL; + goto out; + } + + if (rpc->flags) { + err =3D -EOPNOTSUPP; + goto out; + } + + in =3D memdup_user(u64_to_user_ptr(rpc->in), rpc->inlen); + if (IS_ERR(in)) { + err =3D PTR_ERR(in); + goto out; + } + + out =3D kvzalloc(rpc->outlen, GFP_KERNEL_ACCOUNT); + if (!out) { + err =3D -ENOMEM; + goto out; + } + + mlx5ctl_dbg(mcdev, "[UID %d] cmdif: opcode 0x%x inlen %d outlen %d\n", + mfd->uctx_uid, + MLX5_GET(mbox_in_hdr, in, opcode), rpc->inlen, rpc->outlen); + + MLX5_SET(mbox_in_hdr, in, uid, mfd->uctx_uid); + err =3D mlx5_cmd_do(mcdev->mdev, in, rpc->inlen, out, rpc->outlen); + mlx5ctl_dbg(mcdev, "[UID %d] cmdif: opcode 0x%x retval %d\n", + mfd->uctx_uid, + MLX5_GET(mbox_in_hdr, in, opcode), err); + + /* -EREMOTEIO means outbox is valid, but out.status is not */ + if (!err || err =3D=3D -EREMOTEIO) { + err =3D 0; + if (copy_to_user(u64_to_user_ptr(rpc->out), out, rpc->outlen)) + err =3D -EFAULT; + } + +out: + kvfree(out); + kfree(in); + kfree(rpc); + return err; +} + static long mlx5ctl_ioctl(struct file *file, unsigned int cmd, unsigned lo= ng arg) { struct mlx5ctl_fd *mfd =3D file->private_data; @@ -257,6 +348,10 @@ static long mlx5ctl_ioctl(struct file *file, unsigned = int cmd, unsigned long arg err =3D mlx5ctl_info_ioctl(file, argp, size); break; =20 + case MLX5CTL_IOCTL_CMDRPC: + err =3D mlx5ctl_cmdrpc_ioctl(file, argp, size); + break; + default: mlx5ctl_dbg(mcdev, "Unknown ioctl %x\n", cmd); err =3D -ENOIOCTLCMD; diff --git a/include/uapi/misc/mlx5ctl.h b/include/uapi/misc/mlx5ctl.h index 37153cc0fc6e..3277eaf78a37 100644 --- a/include/uapi/misc/mlx5ctl.h +++ b/include/uapi/misc/mlx5ctl.h @@ -16,9 +16,22 @@ struct mlx5ctl_info { __u32 reserved2; }; =20 +struct mlx5ctl_cmdrpc { + __aligned_u64 in; /* RPC inbox buffer user address */ + __aligned_u64 out; /* RPC outbox buffer user address */ + __u32 inlen; /* inbox buffer length */ + __u32 outlen; /* outbox buffer length */ + __aligned_u64 flags; +}; + +#define MLX5CTL_MAX_RPC_SIZE 8192 + #define MLX5CTL_IOCTL_MAGIC 0x5c =20 #define MLX5CTL_IOCTL_INFO \ _IOR(MLX5CTL_IOCTL_MAGIC, 0x0, struct mlx5ctl_info) =20 +#define MLX5CTL_IOCTL_CMDRPC \ + _IOWR(MLX5CTL_IOCTL_MAGIC, 0x1, struct mlx5ctl_cmdrpc) + #endif /* __MLX5CTL_IOCTL_H__ */ --=20 2.42.0 From nobody Wed Dec 17 22:40:55 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3831BC54FB9 for ; Tue, 21 Nov 2023 07:06:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229470AbjKUHG4 (ORCPT ); Tue, 21 Nov 2023 02:06:56 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35400 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230220AbjKUHGj (ORCPT ); Tue, 21 Nov 2023 02:06:39 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 09B62113 for ; Mon, 20 Nov 2023 23:06:32 -0800 (PST) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 91E75C433CC; Tue, 21 Nov 2023 07:06:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700550391; bh=Jtlr6fv2Rmjy8F6VRINbFMiAGnkQMjW/RQtQL1aMuKs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Ry9CdzUBJtU6G4CEuZ3nBsxVjKKMBIYzoyu9o7SHBCx6/MuwE+H8R0t5v9gm0bbl3 eRSFgObAP4rmFU0CP1LOw6X6NC+1xKyZRifpZiizy0sQj8opB2FTc4wr+ILXfsEvRp qyM7nBmCpWR4tAphA1XmDNIkQdAfR4NRjoLNl/ORlxBdI15Xd0G1NGB9pUaeyzePgy IBZMtj6PYA2j2NvTXRv/BZsSRXgNDnzfL9r+vQIbDm4G91+G3BkXm8YCrHcdsBFA7L ZLl390nPk4otbUQspcXmrJ/bWYAp1LWUOvfh8XbZ9rIOFPakXG+lbEZEB6X+JvmJC9 f1VpFwspacfVw== From: Saeed Mahameed To: Arnd Bergmann , Greg Kroah-Hartman Cc: Jason Gunthorpe , Leon Romanovsky , Jiri Pirko , Leonid Bloch , Itay Avraham , Jakub Kicinski , linux-kernel@vger.kernel.org, Saeed Mahameed Subject: [PATCH V3 5/5] misc: mlx5ctl: Add umem reg/unreg ioctl Date: Mon, 20 Nov 2023 23:06:19 -0800 Message-ID: <20231121070619.9836-6-saeed@kernel.org> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20231121070619.9836-1-saeed@kernel.org> References: <20231121070619.9836-1-saeed@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Saeed Mahameed Command rpc outbox buffer is limited in size, which can be very annoying when trying to pull large traces out of the device. Many device rpcs offer the ability to scatter output traces, contexts and logs directly into user space buffers in a single shot. Allow user to register user memory space, so the device may dump information directly into user memory space. The registered memory will be described by a device UMEM object which has a unique umem_id, this umem_id can be later used in the rpc inbox to tell the device where to populate the response output, e.g HW traces and other debug object queries. To do so this patch introduces two ioctls: MLX5CTL_IOCTL_UMEM_REG(va_address, size): - calculate page fragments from the user provided virtual address - pin the pages, and allocate a sg list - dma map the sg list - create a UMEM device object that points to the dma addresses - add a driver umem object to an xarray data base for bookkeeping - return UMEM ID to user so it can be used in subsequent rpcs MLX5CTL_IOCTL_UMEM_UNREG(umem_id): - user provides a pre allocated umem ID - unwinds the above Example usecase, ConnectX device coredump can be as large as 2MB. Using inline rpcs will take thousands of rpcs to get the full coredump which can take multiple seconds. With UMEM, it can be done in a single rpc, using 2MB of umem user buffer. $ ./mlx5ctlu mlx5_core.ctl.0 coredump --umem_size=3D$(( 2 ** 20 )) 00 00 00 00 01 00 20 00 00 00 00 04 00 00 48 ec 00 00 00 08 00 00 00 00 00 00 00 0c 00 00 00 03 00 00 00 10 00 00 00 00 00 00 00 14 00 00 00 00 .... 00 50 0b 3c 00 00 00 00 00 50 0b 40 00 00 00 00 00 50 0b 44 00 00 00 00 00 50 0b 48 00 00 00 00 00 50 0c 00 00 00 00 00 INFO : Core dump done INFO : Core dump size 831304 INFO : Core dump address 0x0 INFO : Core dump cookie 0x500c04 INFO : More Dump 0 Other usecases are: dynamic HW and FW trace monitoring, high frequency diagnostic counters sampling and batched objects and resource dumps. Reviewed-by: Jiri Pirko Reviewed-by: Leon Romanovsky Signed-off-by: Saeed Mahameed --- drivers/misc/mlx5ctl/Makefile | 1 + drivers/misc/mlx5ctl/main.c | 99 +++++++++++ drivers/misc/mlx5ctl/umem.c | 322 ++++++++++++++++++++++++++++++++++ drivers/misc/mlx5ctl/umem.h | 17 ++ include/uapi/misc/mlx5ctl.h | 22 +++ 5 files changed, 461 insertions(+) create mode 100644 drivers/misc/mlx5ctl/umem.c create mode 100644 drivers/misc/mlx5ctl/umem.h diff --git a/drivers/misc/mlx5ctl/Makefile b/drivers/misc/mlx5ctl/Makefile index b5c7f99e0ab6..f35234e931a8 100644 --- a/drivers/misc/mlx5ctl/Makefile +++ b/drivers/misc/mlx5ctl/Makefile @@ -2,3 +2,4 @@ =20 obj-$(CONFIG_MLX5CTL) +=3D mlx5ctl.o mlx5ctl-y :=3D main.o +mlx5ctl-y +=3D umem.o diff --git a/drivers/misc/mlx5ctl/main.c b/drivers/misc/mlx5ctl/main.c index e7776ea4bfca..7e6d6da26a79 100644 --- a/drivers/misc/mlx5ctl/main.c +++ b/drivers/misc/mlx5ctl/main.c @@ -12,6 +12,8 @@ #include #include =20 +#include "umem.h" + MODULE_DESCRIPTION("mlx5 ConnectX control misc driver"); MODULE_AUTHOR("Saeed Mahameed "); MODULE_LICENSE("Dual BSD/GPL"); @@ -30,6 +32,8 @@ struct mlx5ctl_fd { u16 uctx_uid; u32 uctx_cap; u32 ucap; /* user cap */ + + struct mlx5ctl_umem_db *umem_db; struct mlx5ctl_dev *mcdev; struct list_head list; }; @@ -115,6 +119,12 @@ static int mlx5ctl_open_mfd(struct mlx5ctl_fd *mfd) if (uid < 0) return uid; =20 + mfd->umem_db =3D mlx5ctl_umem_db_create(mdev, uid); + if (IS_ERR(mfd->umem_db)) { + mlx5ctl_release_uid(mcdev, uid); + return PTR_ERR(mfd->umem_db); + } + mfd->uctx_uid =3D uid; mfd->uctx_cap =3D cap; mfd->ucap =3D ucap; @@ -129,6 +139,7 @@ static void mlx5ctl_release_mfd(struct mlx5ctl_fd *mfd) { struct mlx5ctl_dev *mcdev =3D mfd->mcdev; =20 + mlx5ctl_umem_db_destroy(mfd->umem_db); mlx5ctl_release_uid(mcdev, mfd->uctx_uid); } =20 @@ -323,6 +334,86 @@ static int mlx5ctl_cmdrpc_ioctl(struct file *file, return err; } =20 +static int mlx5ctl_ioctl_umem_reg(struct file *file, + struct mlx5ctl_umem_reg __user *arg, + size_t usize) +{ + struct mlx5ctl_fd *mfd =3D file->private_data; + struct mlx5ctl_umem_reg *umem_reg; + int umem_id, err =3D 0; + size_t ksize =3D 0; + + ksize =3D max(sizeof(struct mlx5ctl_umem_reg), usize); + umem_reg =3D kzalloc(ksize, GFP_KERNEL_ACCOUNT); + if (!umem_reg) + return -ENOMEM; + + umem_reg->size =3D sizeof(struct mlx5ctl_umem_reg); + + if (copy_from_user(umem_reg, arg, usize)) { + err =3D -EFAULT; + goto out; + } + + if (umem_reg->flags || umem_reg->reserved1 || umem_reg->reserved2) { + err =3D -EOPNOTSUPP; + goto out; + } + + umem_id =3D mlx5ctl_umem_reg(mfd->umem_db, + (unsigned long)umem_reg->addr, + umem_reg->len); + if (umem_id < 0) { + err =3D umem_id; + goto out; + } + + umem_reg->umem_id =3D umem_id; + + if (copy_to_user(arg, umem_reg, usize)) { + mlx5ctl_umem_unreg(mfd->umem_db, umem_id); + err =3D -EFAULT; + } +out: + kfree(umem_reg); + return err; +} + +static int mlx5ctl_ioctl_umem_unreg(struct file *file, + struct mlx5ctl_umem_unreg __user *arg, + size_t usize) +{ + struct mlx5ctl_fd *mfd =3D file->private_data; + struct mlx5ctl_umem_unreg *umem_unreg; + size_t ksize =3D 0; + int err =3D 0; + + ksize =3D max(sizeof(struct mlx5ctl_umem_unreg), usize); + umem_unreg =3D kzalloc(ksize, GFP_KERNEL_ACCOUNT); + if (!umem_unreg) + return -ENOMEM; + + umem_unreg->size =3D sizeof(struct mlx5ctl_umem_unreg); + + if (copy_from_user(umem_unreg, arg, usize)) { + err =3D -EFAULT; + goto out; + } + + if (umem_unreg->flags) { + err =3D -EOPNOTSUPP; + goto out; + } + + err =3D mlx5ctl_umem_unreg(mfd->umem_db, umem_unreg->umem_id); + + if (!err && copy_to_user(arg, umem_unreg, usize)) + err =3D -EFAULT; +out: + kfree(umem_unreg); + return err; +} + static long mlx5ctl_ioctl(struct file *file, unsigned int cmd, unsigned lo= ng arg) { struct mlx5ctl_fd *mfd =3D file->private_data; @@ -352,6 +443,14 @@ static long mlx5ctl_ioctl(struct file *file, unsigned = int cmd, unsigned long arg err =3D mlx5ctl_cmdrpc_ioctl(file, argp, size); break; =20 + case MLX5CTL_IOCTL_UMEM_REG: + err =3D mlx5ctl_ioctl_umem_reg(file, argp, size); + break; + + case MLX5CTL_IOCTL_UMEM_UNREG: + err =3D mlx5ctl_ioctl_umem_unreg(file, argp, size); + break; + default: mlx5ctl_dbg(mcdev, "Unknown ioctl %x\n", cmd); err =3D -ENOIOCTLCMD; diff --git a/drivers/misc/mlx5ctl/umem.c b/drivers/misc/mlx5ctl/umem.c new file mode 100644 index 000000000000..e62030dadf51 --- /dev/null +++ b/drivers/misc/mlx5ctl/umem.c @@ -0,0 +1,322 @@ +// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0 +/* Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserve= d. */ + +#include +#include +#include + +#include "umem.h" + +#define MLX5CTL_UMEM_MAX_MB 64 + +static unsigned long umem_num_pages(u64 addr, size_t len) +{ + return DIV_ROUND_UP(addr + len - PAGE_ALIGN_DOWN(addr), PAGE_SIZE); +} + +struct mlx5ctl_umem { + struct sg_table sgt; + unsigned long addr; + size_t size; + size_t offset; + size_t npages; + struct task_struct *source_task; + struct mm_struct *source_mm; + struct user_struct *source_user; + u32 umem_id; + struct page **page_list; +}; + +struct mlx5ctl_umem_db { + struct xarray xarray; + struct mlx5_core_dev *mdev; + u32 uctx_uid; +}; + +static int inc_user_locked_vm(struct mlx5ctl_umem *umem, unsigned long npa= ges) +{ + unsigned long lock_limit; + unsigned long cur_pages; + unsigned long new_pages; + + lock_limit =3D task_rlimit(umem->source_task, RLIMIT_MEMLOCK) >> + PAGE_SHIFT; + do { + cur_pages =3D atomic_long_read(&umem->source_user->locked_vm); + new_pages =3D cur_pages + npages; + if (new_pages > lock_limit) + return -ENOMEM; + } while (atomic_long_cmpxchg(&umem->source_user->locked_vm, cur_pages, + new_pages) !=3D cur_pages); + return 0; +} + +static void dec_user_locked_vm(struct mlx5ctl_umem *umem, unsigned long np= ages) +{ + if (WARN_ON(atomic_long_read(&umem->source_user->locked_vm) < npages)) + return; + atomic_long_sub(npages, &umem->source_user->locked_vm); +} + +#define PAGES_2_MB(pages) ((pages) >> (20 - PAGE_SHIFT)) + +static struct mlx5ctl_umem *mlx5ctl_umem_pin(struct mlx5ctl_umem_db *umem_= db, + unsigned long addr, size_t size) +{ + size_t npages =3D umem_num_pages(addr, size); + struct mlx5_core_dev *mdev =3D umem_db->mdev; + unsigned long endaddr =3D addr + size; + struct mlx5ctl_umem *umem; + struct page **page_list; + int err =3D -EINVAL; + int pinned =3D 0; + + dev_dbg(mdev->device, "%s: addr %p size %zu npages %zu\n", + __func__, (void __user *)addr, size, npages); + + /* Avoid integer overflow */ + if (endaddr < addr || PAGE_ALIGN(endaddr) < endaddr) + return ERR_PTR(-EINVAL); + + if (npages =3D=3D 0 || PAGES_2_MB(npages) > MLX5CTL_UMEM_MAX_MB) + return ERR_PTR(-EINVAL); + + page_list =3D kvmalloc_array(npages, sizeof(struct page *), GFP_KERNEL_AC= COUNT); + if (!page_list) + return ERR_PTR(-ENOMEM); + + umem =3D kzalloc(sizeof(*umem), GFP_KERNEL_ACCOUNT); + if (!umem) { + kvfree(page_list); + return ERR_PTR(-ENOMEM); + } + + umem->addr =3D addr; + umem->size =3D size; + umem->offset =3D addr & ~PAGE_MASK; + umem->npages =3D npages; + + umem->page_list =3D page_list; + umem->source_mm =3D current->mm; + umem->source_task =3D current->group_leader; + get_task_struct(current->group_leader); + umem->source_user =3D get_uid(current_user()); + + /* mm and RLIMIT_MEMLOCK user task accounting similar to what is + * being done in iopt_alloc_pages() and do_update_pinned() + * for IOPT_PAGES_ACCOUNT_USER @drivers/iommu/iommufd/pages.c + */ + mmgrab(umem->source_mm); + + pinned =3D pin_user_pages_fast(addr, npages, FOLL_WRITE, page_list); + if (pinned !=3D npages) { + dev_dbg(mdev->device, "pin_user_pages_fast failed %d\n", pinned); + err =3D pinned < 0 ? pinned : -ENOMEM; + goto pin_failed; + } + + err =3D inc_user_locked_vm(umem, npages); + if (err) + goto pin_failed; + + atomic64_add(npages, &umem->source_mm->pinned_vm); + + err =3D sg_alloc_table_from_pages(&umem->sgt, page_list, npages, 0, + npages << PAGE_SHIFT, GFP_KERNEL_ACCOUNT); + if (err) { + dev_dbg(mdev->device, "sg_alloc_table failed: %d\n", err); + goto sgt_failed; + } + + dev_dbg(mdev->device, "\tsgt: size %zu npages %zu sgt.nents (%d)\n", + size, npages, umem->sgt.nents); + + err =3D dma_map_sgtable(mdev->device, &umem->sgt, DMA_BIDIRECTIONAL, 0); + if (err) { + dev_dbg(mdev->device, "dma_map_sgtable failed: %d\n", err); + goto dma_failed; + } + + dev_dbg(mdev->device, "\tsgt: dma_nents %d\n", umem->sgt.nents); + return umem; + +dma_failed: +sgt_failed: + sg_free_table(&umem->sgt); + atomic64_sub(npages, &umem->source_mm->pinned_vm); + dec_user_locked_vm(umem, npages); +pin_failed: + if (pinned > 0) + unpin_user_pages(page_list, pinned); + mmdrop(umem->source_mm); + free_uid(umem->source_user); + put_task_struct(umem->source_task); + + kfree(umem); + kvfree(page_list); + return ERR_PTR(err); +} + +static void mlx5ctl_umem_unpin(struct mlx5ctl_umem_db *umem_db, + struct mlx5ctl_umem *umem) +{ + struct mlx5_core_dev *mdev =3D umem_db->mdev; + + dev_dbg(mdev->device, "%s: addr %p size %zu npages %zu dma_nents %d\n", + __func__, (void *)umem->addr, umem->size, umem->npages, + umem->sgt.nents); + + dma_unmap_sgtable(mdev->device, &umem->sgt, DMA_BIDIRECTIONAL, 0); + sg_free_table(&umem->sgt); + + atomic64_sub(umem->npages, &umem->source_mm->pinned_vm); + dec_user_locked_vm(umem, umem->npages); + unpin_user_pages(umem->page_list, umem->npages); + mmdrop(umem->source_mm); + free_uid(umem->source_user); + put_task_struct(umem->source_task); + + kvfree(umem->page_list); + kfree(umem); +} + +static int mlx5ctl_umem_create(struct mlx5_core_dev *mdev, + struct mlx5ctl_umem *umem, u32 uid) +{ + u32 out[MLX5_ST_SZ_DW(create_umem_out)] =3D {}; + int err, inlen, i, n =3D 0; + struct scatterlist *sg; + void *in, *umemptr; + __be64 *mtt; + + inlen =3D MLX5_ST_SZ_BYTES(create_umem_in) + + umem->npages * MLX5_ST_SZ_BYTES(mtt); + + in =3D kzalloc(inlen, GFP_KERNEL_ACCOUNT); + if (!in) + return -ENOMEM; + + MLX5_SET(create_umem_in, in, opcode, MLX5_CMD_OP_CREATE_UMEM); + MLX5_SET(create_umem_in, in, uid, uid); + + umemptr =3D MLX5_ADDR_OF(create_umem_in, in, umem); + + MLX5_SET(umem, umemptr, log_page_size, + PAGE_SHIFT - MLX5_ADAPTER_PAGE_SHIFT); + MLX5_SET64(umem, umemptr, num_of_mtt, umem->npages); + MLX5_SET(umem, umemptr, page_offset, umem->offset); + + dev_dbg(mdev->device, + "UMEM CREATE: log_page_size %d num_of_mtt %lld page_offset %d\n", + MLX5_GET(umem, umemptr, log_page_size), + MLX5_GET64(umem, umemptr, num_of_mtt), + MLX5_GET(umem, umemptr, page_offset)); + + mtt =3D MLX5_ADDR_OF(create_umem_in, in, umem.mtt); + for_each_sgtable_dma_sg(&umem->sgt, sg, i) { + u64 dma_addr =3D sg_dma_address(sg); + ssize_t len =3D sg_dma_len(sg); + + for (; n < umem->npages && len > 0; n++, mtt++) { + *mtt =3D cpu_to_be64(dma_addr); + MLX5_SET(mtt, mtt, wr_en, 1); + MLX5_SET(mtt, mtt, rd_en, 1); + dma_addr +=3D PAGE_SIZE; + len -=3D PAGE_SIZE; + } + WARN_ON_ONCE(n =3D=3D umem->npages && len > 0); + } + + err =3D mlx5_cmd_exec(mdev, in, inlen, out, sizeof(out)); + if (err) + goto out; + + umem->umem_id =3D MLX5_GET(create_umem_out, out, umem_id); + dev_dbg(mdev->device, "\tUMEM CREATED: umem_id %d\n", umem->umem_id); +out: + kfree(in); + return err; +} + +static void mlx5ctl_umem_destroy(struct mlx5_core_dev *mdev, + struct mlx5ctl_umem *umem) +{ + u32 in[MLX5_ST_SZ_DW(destroy_umem_in)] =3D {}; + + MLX5_SET(destroy_umem_in, in, opcode, MLX5_CMD_OP_DESTROY_UMEM); + MLX5_SET(destroy_umem_in, in, umem_id, umem->umem_id); + + dev_dbg(mdev->device, "UMEM DESTROY: umem_id %d\n", umem->umem_id); + mlx5_cmd_exec_in(mdev, destroy_umem, in); +} + +int mlx5ctl_umem_reg(struct mlx5ctl_umem_db *umem_db, unsigned long addr, + size_t size) +{ + struct mlx5ctl_umem *umem; + void *ret; + int err; + + umem =3D mlx5ctl_umem_pin(umem_db, addr, size); + if (IS_ERR(umem)) + return PTR_ERR(umem); + + err =3D mlx5ctl_umem_create(umem_db->mdev, umem, umem_db->uctx_uid); + if (err) + goto umem_create_err; + + ret =3D xa_store(&umem_db->xarray, umem->umem_id, umem, GFP_KERNEL_ACCOUN= T); + if (WARN(xa_is_err(ret), "Failed to store UMEM")) { + err =3D xa_err(ret); + goto xa_store_err; + } + + return umem->umem_id; + +xa_store_err: + mlx5ctl_umem_destroy(umem_db->mdev, umem); +umem_create_err: + mlx5ctl_umem_unpin(umem_db, umem); + return err; +} + +int mlx5ctl_umem_unreg(struct mlx5ctl_umem_db *umem_db, u32 umem_id) +{ + struct mlx5ctl_umem *umem; + + umem =3D xa_erase(&umem_db->xarray, umem_id); + if (!umem) + return -ENOENT; + + mlx5ctl_umem_destroy(umem_db->mdev, umem); + mlx5ctl_umem_unpin(umem_db, umem); + return 0; +} + +struct mlx5ctl_umem_db *mlx5ctl_umem_db_create(struct mlx5_core_dev *mdev, + u32 uctx_uid) +{ + struct mlx5ctl_umem_db *umem_db; + + umem_db =3D kzalloc(sizeof(*umem_db), GFP_KERNEL_ACCOUNT); + if (!umem_db) + return ERR_PTR(-ENOMEM); + + xa_init(&umem_db->xarray); + umem_db->mdev =3D mdev; + umem_db->uctx_uid =3D uctx_uid; + + return umem_db; +} + +void mlx5ctl_umem_db_destroy(struct mlx5ctl_umem_db *umem_db) +{ + struct mlx5ctl_umem *umem; + unsigned long index; + + xa_for_each(&umem_db->xarray, index, umem) + mlx5ctl_umem_unreg(umem_db, umem->umem_id); + + xa_destroy(&umem_db->xarray); + kfree(umem_db); +} diff --git a/drivers/misc/mlx5ctl/umem.h b/drivers/misc/mlx5ctl/umem.h new file mode 100644 index 000000000000..9cf62e5e775e --- /dev/null +++ b/drivers/misc/mlx5ctl/umem.h @@ -0,0 +1,17 @@ +/* SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0 */ +/* Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserve= d. */ + +#ifndef __MLX5CTL_UMEM_H__ +#define __MLX5CTL_UMEM_H__ + +#include +#include + +struct mlx5ctl_umem_db; + +struct mlx5ctl_umem_db *mlx5ctl_umem_db_create(struct mlx5_core_dev *mdev,= u32 uctx_uid); +void mlx5ctl_umem_db_destroy(struct mlx5ctl_umem_db *umem_db); +int mlx5ctl_umem_reg(struct mlx5ctl_umem_db *umem_db, unsigned long addr, = size_t size); +int mlx5ctl_umem_unreg(struct mlx5ctl_umem_db *umem_db, u32 umem_id); + +#endif /* __MLX5CTL_UMEM_H__ */ diff --git a/include/uapi/misc/mlx5ctl.h b/include/uapi/misc/mlx5ctl.h index 3277eaf78a37..506aa8db75b4 100644 --- a/include/uapi/misc/mlx5ctl.h +++ b/include/uapi/misc/mlx5ctl.h @@ -24,6 +24,22 @@ struct mlx5ctl_cmdrpc { __aligned_u64 flags; }; =20 +struct mlx5ctl_umem_reg { + __aligned_u64 flags; + __u32 size; + __u32 reserved1; + __aligned_u64 addr; /* user address */ + __aligned_u64 len; /* user buffer length */ + __u32 umem_id; /* returned device's umem ID */ + __u32 reserved2; +}; + +struct mlx5ctl_umem_unreg { + __aligned_u64 flags; + __u32 size; + __u32 umem_id; +}; + #define MLX5CTL_MAX_RPC_SIZE 8192 =20 #define MLX5CTL_IOCTL_MAGIC 0x5c @@ -34,4 +50,10 @@ struct mlx5ctl_cmdrpc { #define MLX5CTL_IOCTL_CMDRPC \ _IOWR(MLX5CTL_IOCTL_MAGIC, 0x1, struct mlx5ctl_cmdrpc) =20 +#define MLX5CTL_IOCTL_UMEM_REG \ + _IOWR(MLX5CTL_IOCTL_MAGIC, 0x2, struct mlx5ctl_umem_reg) + +#define MLX5CTL_IOCTL_UMEM_UNREG \ + _IOWR(MLX5CTL_IOCTL_MAGIC, 0x3, struct mlx5ctl_umem_unreg) + #endif /* __MLX5CTL_IOCTL_H__ */ --=20 2.42.0