Skip to contentSkip to conversation

drivers: hv: dxgkrnl: Driver for Hyper-V virtual compute device

PATCH New Eric Curtin 1 participant 56 messages Mar 19

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v4 cover drivers: hv: dxgkrnl: Driver for Hyper-V virtual compute device

This patch series introduces the dxgkrnl driver, which provides Linux support for virtual compute devices (vGPUs) paravirtualized by a Windows Hyper-V host. The primary use case is Windows Subsystem for Linux (WSL2), where the driver enables GPU-accelerated workloads inside Linux containers running on Windows.

The driver creates /dev/dxg, a miscdevice that user-space API libraries (such as the open-source libdxg) use to communicate with virtual GPU adapters via IOCTLs implementing a subset of the WDDM/MCDM D3DKMT interface. Physical GPU access is performed entirely on the Windows host; the guest driver communicates over Hyper-V VMBus channels.

Key characteristics:
- Self-contained under drivers/hv/dxgkrnl/
- Depends only on CONFIG_HYPERV, DMA_SHARED_BUFFER, SYNC_FILE
- Supports multiple vGPU adapters per VM
- DMA fence integration via dxgsyncfile (SYNC_FILE)
- Supports compute-only accelerators (AI/ML workloads) as well as full graphics adapters

Changes since v3 (posted March 2022):
- Replace deprecated one-element arrays [1] with C99 flexible arrays []
- Replace %px with %p in trace macros
- Remove unnecessary braces from single-statement if blocks
- Remove LINUX_VERSION_CODE guard for max_pkt_size (added in 5.15, well before any target kernel for this submission)
- Remove linux/version.h include (no longer needed)
- Fix whitespace issues flagged by checkpatch
- Replace non-debug DXG_ERR do{}while(0) macro with direct dev_err call
- Change -EBADE to -ENODEV for global channel duplicate detection (as requested by Greg KH in v3 review)
- Remove MODULE_VERSION (not recommended for in-tree drivers)
- Add explanatory comment to guid_to_luid() cast
- Additional features and fixes developed in the WSL2 fork:
* dxgsyncfile: DMA fence / sync file integration
* D3DKMTEnumProcesses, D3DDKMTIsFeatureEnabled, D3DKMTInvalidateCache
* Compute-only adapter support
* pin_user_pages for DMA-accessible memory
* Retry logic for VMBus ring buffer full condition
* Various synchronization and memory safety fixes

Regarding the dxgglobal singleton raised in v3 review:
The design reflects a host architecture constraint: each Hyper-V VM has exactly one global VMBus channel offered by the host, regardless of how many vGPU adapters are present. The dxgglobal structure encapsulates this VM-level state (global channel, adapter list, process list, host event tracking). Per-adapter state is separately managed in dxgadapter objects. This design was previously explained in the v3 thread; the architecture matches the Hyper-V GPU-PV protocol which is fixed by the host side.

The patches apply on top of v6.6-lts. The user-space library (libdxg) that communicates with this driver is available at:
https://github.com/microsoft/libdxg

The full WDDM compute stack (OpenCL, oneAPI, OpenVINO) is available open-source via Intel's compute-runtime project.

Iouri Tarassov ([email protected]) is the primary author and maintainer of this driver.

Eric Curtin (1):
drivers: hv: dxgkrnl: Fix checkpatch issues and address reviewer
feedback

Hideyuki Nagase (1):
drivers: hv: dxgkrnl: Fix crash at hmgrtable_free_handle

Iouri Tarassov (53):
drivers: hv: dxgkrnl: Driver initialization and loading
drivers: hv: dxgkrnl: Add VMBus message support, initialize VMBus
channels.
drivers: hv: dxgkrnl: Creation of dxgadapter object
drivers: hv: dxgkrnl: Opening of /dev/dxg device and dxgprocess
creation
drivers: hv: dxgkrnl: Enumerate and open dxgadapter objects
drivers: hv: dxgkrnl: Creation of dxgdevice objects
drivers: hv: dxgkrnl: Creation of dxgcontext objects
drivers: hv: dxgkrnl: Creation of compute device allocations and
resources
drivers: hv: dxgkrnl: Creation of compute device sync objects
drivers: hv: dxgkrnl: Operations using sync objects
drivers: hv: dxgkrnl: Sharing of dxgresource objects
drivers: hv: dxgkrnl: Sharing of sync objects
drivers: hv: dxgkrnl: Creation of paging queue objects.
drivers: hv: dxgkrnl: Submit execution commands to the compute device
drivers: hv: dxgkrnl: Share objects with the host
drivers: hv: dxgkrnl: Query the dxgdevice state
drivers: hv: dxgkrnl: Map(unmap) CPU address to device allocation
drivers: hv: dxgkrnl: Manage device allocation properties
drivers: hv: dxgkrnl: Flush heap transitions
drivers: hv: dxgkrnl: Query video memory information
drivers: hv: dxgkrnl: The escape ioctl
drivers: hv: dxgkrnl: Ioctl to put device to error state
drivers: hv: dxgkrnl: Ioctls to query statistics and clock calibration
drivers: hv: dxgkrnl: Offer and reclaim allocations
drivers: hv: dxgkrnl: Ioctls to manage scheduling priority
drivers: hv: dxgkrnl: Manage residency of allocations
drivers: hv: dxgkrnl: Manage compute device virtual addresses
drivers: hv: dxgkrnl: Add support to map guest pages by host
drivers: hv: dxgkrnl: Removed struct vmbus_gpadl, which was defined in
the main linux branch
drivers: hv: dxgkrnl: Remove dxgk_init_ioctls
drivers: hv: dxgkrnl: Creation of dxgsyncfile objects
drivers: hv: dxgkrnl: Use tracing instead of dev_dbg
drivers: hv: dxgkrnl: Implement D3DKMTWaitSyncFile
drivers: hv: dxgkrnl: Improve tracing and return values from copy from
user
drivers: hv: dxgkrnl: Fix synchronization locks
drivers: hv: dxgkrnl: Close shared file objects in case of a failure
drivers: hv: dxgkrnl: Added missed NULL check for resource object
drivers: hv: dxgkrnl: Fixed dxgkrnl to build for the 6.1 kernel
drivers: hv: dxgkrnl: Added support for compute only adapters
drivers: hv: dxgkrnl: Added implementation for D3DKMTInvalidateCache
drivers: hv: dxgkrnl: Handle process ID in D3DKMTQueryStatistics
drivers: hv: dxgkrnl: Implement the D3DKMTEnumProcesses API
drivers: hv: dxgkrnl: Implement D3DDKMTIsFeatureEnabled API
drivers: hv: dxgkrnl: Implement known escapes
drivers: hv: dxgkrnl: Fixed coding style issues
drivers: hv: dxgkrnl: Fixed the implementation of
D3DKMTQueryClockCalibration
drivers: hv: dxgkrnl: Retry sending a VM bus packet when there is no
place in the ring buffer
drivers: hv: dxgkrnl: Add support for locking a shared allocation by
not the owner
drivers: hv: dxgkrnl: Fix build breaks when switching to 6.6 kernel
due to hv_driver remove callback change.
drivers: hv: dxgkrnl: Fix build breaks when switching to 6.6 kernel
due to removed uuid_le_cmp
drivers: hv: dxgkrnl: Implement D3DKMTEnumProcesses to match the
Windows implementation
drivers: hv: dxgkrnl: Use pin_user_pages instead of get_user_pages for
DMA accessible memory
drivers: hv: dxgkrnl: Do not print error messages when virtual GPU is
not present

 MAINTAINERS                      |    7 +
 drivers/hv/Kconfig               |    2 +
 drivers/hv/Makefile              |    1 +
 drivers/hv/dxgkrnl/Kconfig       |   28 +
 drivers/hv/dxgkrnl/Makefile      |    5 +
 drivers/hv/dxgkrnl/dxgadapter.c  | 1367 ++++++++
 drivers/hv/dxgkrnl/dxgkrnl.h     | 1042 ++++++
 drivers/hv/dxgkrnl/dxgmodule.c   |  971 +++++
 drivers/hv/dxgkrnl/dxgprocess.c  |  348 ++
 drivers/hv/dxgkrnl/dxgsyncfile.c |  481 +++
 drivers/hv/dxgkrnl/dxgsyncfile.h |   33 +
 drivers/hv/dxgkrnl/dxgvmbus.c    | 3992 +++++++++++++++++++++
 drivers/hv/dxgkrnl/dxgvmbus.h    |  910 +++++
 drivers/hv/dxgkrnl/hmgr.c        |  567 +++
 drivers/hv/dxgkrnl/hmgr.h        |  112 +
 drivers/hv/dxgkrnl/ioctl.c       | 5648 ++++++++++++++++++++++++++++++
 drivers/hv/dxgkrnl/misc.c        |   38 +
 drivers/hv/dxgkrnl/misc.h        |   96 +
 include/uapi/misc/d3dkmthk.h     | 1794 ++++++++++
 19 files changed, 17442 insertions(+)
 create mode 100644 drivers/hv/dxgkrnl/Kconfig
 create mode 100644 drivers/hv/dxgkrnl/Makefile
 create mode 100644 drivers/hv/dxgkrnl/dxgadapter.c
 create mode 100644 drivers/hv/dxgkrnl/dxgkrnl.h
 create mode 100644 drivers/hv/dxgkrnl/dxgmodule.c
 create mode 100644 drivers/hv/dxgkrnl/dxgprocess.c
 create mode 100644 drivers/hv/dxgkrnl/dxgsyncfile.c
 create mode 100644 drivers/hv/dxgkrnl/dxgsyncfile.h
 create mode 100644 drivers/hv/dxgkrnl/dxgvmbus.c
 create mode 100644 drivers/hv/dxgkrnl/dxgvmbus.h
 create mode 100644 drivers/hv/dxgkrnl/hmgr.c
 create mode 100644 drivers/hv/dxgkrnl/hmgr.h
 create mode 100644 drivers/hv/dxgkrnl/ioctl.c
 create mode 100644 drivers/hv/dxgkrnl/misc.c
 create mode 100644 drivers/hv/dxgkrnl/misc.h
 create mode 100644 include/uapi/misc/d3dkmthk.h
#1 lore raw

PATCH v4 1/55 drivers: hv: dxgkrnl: Driver initialization and loading

From: Iouri Tarassov <[email protected]>

- Create skeleton and add basic functionality for the Hyper-V compute device driver (dxgkrnl).

- Register for PCI and VMBus driver notifications and handle initialization of VMBus channels.

- Connect the dxgkrnl module to the drivers/hv/ Makefile and Kconfig

- Create a MAINTAINERS entry

A VMBus channel is a communication interface between the Hyper-V guest and the host. The are two type of VMBus channels, used in the driver:
- the global channel
- per virtual compute device channel

A PCI device is created for each virtual compute device, projected by the host. The device vendor is PCI_VENDOR_ID_MICROSOFT and device id is PCI_DEVICE_ID_VIRTUAL_RENDER. dxg_pci_probe_device handles arrival of such devices. The PCI config space of the virtual compute device has luid of the corresponding virtual compute device VM bus channel. This is how the compute device adapter objects are linked to VMBus channels.

VMBus interface version is exchanged by reading/writing the PCI config space of the virtual compute device.

The IO space is used to handle CPU accessible compute device allocations. Hyper-V allocates IO space for the global VMBus channel.

[kms: forward port to 6.6 from 6.1. No code changes made.]
---
 MAINTAINERS                    |   7 +
 drivers/hv/Kconfig             |   2 +
 drivers/hv/Makefile            |   1 +
 drivers/hv/dxgkrnl/Kconfig     |  26 ++
 drivers/hv/dxgkrnl/Makefile    |   5 +
 drivers/hv/dxgkrnl/dxgkrnl.h   | 155 ++++++++++
 drivers/hv/dxgkrnl/dxgmodule.c | 506 +++++++++++++++++++++++++++++++++
 drivers/hv/dxgkrnl/dxgvmbus.c  |  92 ++++++
 drivers/hv/dxgkrnl/dxgvmbus.h  |  19 ++
 include/uapi/misc/d3dkmthk.h   |  27 ++
 10 files changed, 840 insertions(+)
 create mode 100644 drivers/hv/dxgkrnl/Kconfig
 create mode 100644 drivers/hv/dxgkrnl/Makefile
 create mode 100644 drivers/hv/dxgkrnl/dxgkrnl.h
 create mode 100644 drivers/hv/dxgkrnl/dxgmodule.c
 create mode 100644 drivers/hv/dxgkrnl/dxgvmbus.c
 create mode 100644 drivers/hv/dxgkrnl/dxgvmbus.h
 create mode 100644 include/uapi/misc/d3dkmthk.h
MAINTAINERS +7 -0
index ae4c0cec5073..4fe0b3501931 100644
@@ -9771,6 +9771,13 @@ F:	Documentation/devicetree/bindings/mtd/ti,am654-hbmc.yaml
 F:	drivers/mtd/hyperbus/
 F:	include/linux/mtd/hyperbus.h
 
+Hyper-V vGPU DRIVER
+M:	Iouri Tarassov <[email protected]>
+L:	[email protected]
+S:	Supported
+F:	drivers/hv/dxgkrnl/
+F:	include/uapi/misc/d3dkmthk.h
+
 HYPERVISOR VIRTUAL CONSOLE DRIVER
 L:	[email protected]
 S:	Odd Fixes
drivers/hv/Kconfig +2 -0
index 862c47b191af..b16c7701da19 100644
@@ -55,4 +55,6 @@ config HYPERV_BALLOON
 	help
 	  Select this option to enable Hyper-V Balloon driver.
 
+source "drivers/hv/dxgkrnl/Kconfig"
+
 endmenu
drivers/hv/Makefile +1 -0
index d76df5c8c2a9..aa1cbdb5d0d2 100644
@@ -2,6 +2,7 @@
 obj-$(CONFIG_HYPERV)		+= hv_vmbus.o
 obj-$(CONFIG_HYPERV_UTILS)	+= hv_utils.o
 obj-$(CONFIG_HYPERV_BALLOON)	+= hv_balloon.o
+obj-$(CONFIG_DXGKRNL)		+= dxgkrnl/
 
 CFLAGS_hv_trace.o = -I$(src)
 CFLAGS_hv_balloon.o = -I$(src)
drivers/hv/dxgkrnl/Kconfig new file +26 -0
new file mode 100644
index 000000000000..bcd92bbff939
@@ -0,0 +1,26 @@
+# SPDX-License-Identifier: GPL-2.0
+# Configuration for the hyper-v virtual compute driver (dxgkrnl)
+#
+
+config DXGKRNL
+	tristate "Microsoft Paravirtualized GPU support"
+	depends on HYPERV
+	depends on 64BIT || COMPILE_TEST
+	help
+	  This driver supports paravirtualized virtual compute devices, exposed
+	  by Microsoft Hyper-V when Linux is running inside of a virtual machine
+	  hosted by Windows. The virtual machines needs to be configured to use
+	  host compute adapters. The driver name is dxgkrnl.
+
+	  An example of such virtual machine is a  Windows Subsystem for
+	  Linux container. When such container is instantiated, the Windows host
+	  assigns compatible host GPU adapters to the container. The corresponding
+	  virtual GPU devices appear on the PCI bus in the container. These
+	  devices are enumerated and accessed by this driver.
+
+	  Communications with the driver are done by using the Microsoft libdxcore
+	  library, which translates the D3DKMT interface
+	  <https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/d3dkmthk/>
+	  to the driver IOCTLs. The virtual GPU devices are paravirtualized,
+	  which means that access to the hardware is done in the host. The driver
+	  communicates with the host using Hyper-V VM bus communication channels.
drivers/hv/dxgkrnl/Makefile new file +5 -0
new file mode 100644
index 000000000000..76349064b60a
@@ -0,0 +1,5 @@
+# SPDX-License-Identifier: GPL-2.0
+# Makefile for the hyper-v compute device driver (dxgkrnl).
+
+obj-$(CONFIG_DXGKRNL)	+= dxgkrnl.o
+dxgkrnl-y		:= dxgmodule.o dxgvmbus.o
drivers/hv/dxgkrnl/dxgkrnl.h new file +155 -0

155 lines not on this page · raw message

drivers/hv/dxgkrnl/dxgmodule.c new file +506 -0

506 lines not on this page · raw message

drivers/hv/dxgkrnl/dxgvmbus.c new file +92 -0

92 lines not on this page · raw message

drivers/hv/dxgkrnl/dxgvmbus.h new file +19 -0
new file mode 100644
index 000000000000..6cdca5e03d1f
@@ -0,0 +1,19 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+/*
+ * Copyright (c) 2022, Microsoft Corporation.
+ *
+ * Author:
+ *   Iouri Tarassov <[email protected]>
+ *
+ * Dxgkrnl Graphics Driver
+ * VM bus interface with the host definitions
+ *
+ */
+
+#ifndef _DXGVMBUS_H
+#define _DXGVMBUS_H
+
+#define DXG_MAX_VM_BUS_PACKET_SIZE	(1024 * 128)
+
+#endif /* _DXGVMBUS_H */
include/uapi/misc/d3dkmthk.h new file +27 -0
new file mode 100644
index 000000000000..5d973604400c
@@ -0,0 +1,27 @@
+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
+
+/*
+ * Copyright (c) 2019, Microsoft Corporation.
+ *
+ * Author:
+ *   Iouri Tarassov <[email protected]>
+ *
+ * Dxgkrnl Graphics Driver
+ * User mode WDDM interface definitions
+ *
+ */
+
+#ifndef _D3DKMTHK_H
+#define _D3DKMTHK_H
+
+/*
+ * Matches the Windows LUID definition.
+ * LUID is a locally unique identifier (similar to GUID, but not global),
+ * which is guaranteed to be unique intil the computer is rebooted.
+ */
+struct winluid {
+	__u32 a;
+	__u32 b;
+};
+
+#endif /* _D3DKMTHK_H */
#2 lore raw

PATCH v4 2/55 drivers: hv: dxgkrnl: Add VMBus message support, initialize VMBus channels.

In reply to Eric Curtin

From: Iouri Tarassov <[email protected]>

Implement support for sending/receiving VMBus messages between the host and the guest.

Initialize the VMBus channels and notify the host about IO space settings of the VMBus global channel.

[kms: forward port to 6.6 from 6.1. No code changes made.]
---
 drivers/hv/dxgkrnl/dxgkrnl.h   |  14 ++
 drivers/hv/dxgkrnl/dxgmodule.c |   9 +-
 drivers/hv/dxgkrnl/dxgvmbus.c  | 318 +++++++++++++++++++++++++++++++++
 drivers/hv/dxgkrnl/dxgvmbus.h  |  67 +++++++
 drivers/hv/dxgkrnl/ioctl.c     |  24 +++
 drivers/hv/dxgkrnl/misc.h      |  72 ++++++++
 include/uapi/misc/d3dkmthk.h   |  34 ++++
 7 files changed, 536 insertions(+), 2 deletions(-)
 create mode 100644 drivers/hv/dxgkrnl/ioctl.c
 create mode 100644 drivers/hv/dxgkrnl/misc.h
drivers/hv/dxgkrnl/dxgkrnl.h +14 -0
index f7900840d1ed..52b9e82c51e6 100644
@@ -28,6 +28,8 @@
 #include <linux/hyperv.h>
 #include <uapi/misc/d3dkmthk.h>
 #include <linux/version.h>
+#include "misc.h"
+#include <uapi/misc/d3dkmthk.h>
 
 struct dxgadapter;
 
@@ -100,6 +102,13 @@ static inline struct dxgglobal *dxggbl(void)
 	return dxgdrv.dxgglobal;
 }
 
+int dxgglobal_init_global_channel(void);
+void dxgglobal_destroy_global_channel(void);
+struct vmbus_channel *dxgglobal_get_vmbus(void);
+struct dxgvmbuschannel *dxgglobal_get_dxgvmbuschannel(void);
+int dxgglobal_acquire_channel_lock(void);
+void dxgglobal_release_channel_lock(void);
+
 struct dxgprocess {
 	/* Placeholder */
 };
@@ -130,6 +139,11 @@ static inline void guid_to_luid(guid_t *guid, struct winluid *luid)
 #define DXGK_VMBUS_INTERFACE_VERSION			40
 #define DXGK_VMBUS_LAST_COMPATIBLE_INTERFACE_VERSION	16
 
+void dxgvmb_initialize(void);
+int dxgvmb_send_set_iospace_region(u64 start, u64 len);
+
+int ntstatus2int(struct ntstatus status);
+
 #ifdef DEBUG
 
 void dxgk_validate_ioctls(void);
drivers/hv/dxgkrnl/dxgmodule.c +7 -2
index de02edc4d023..e55639dc0adc 100644
@@ -260,6 +260,13 @@ int dxgglobal_init_global_channel(void)
 		goto error;
 	}
 
+	ret = dxgvmb_send_set_iospace_region(dxgglobal->mmiospace_base,
+					     dxgglobal->mmiospace_size);
+	if (ret < 0) {
+		DXG_ERR("send_set_iospace_region failed");
+		goto error;
+	}
+
 	hv_set_drvdata(dxgglobal->hdev, dxgglobal);
 
 error:
@@ -429,8 +436,6 @@ static void dxgglobal_destroy(struct dxgglobal *dxgglobal)
 		if (dxgglobal->vmbus_registered)
 			vmbus_driver_unregister(&dxgdrv.vmbus_drv);
 
-		dxgglobal_destroy_global_channel();
-
 		if (dxgglobal->pci_registered)
 			pci_unregister_driver(&dxgdrv.pci_drv);
 
drivers/hv/dxgkrnl/dxgvmbus.c +318 -0

331 lines not on this page · raw message

drivers/hv/dxgkrnl/dxgvmbus.h +67 -0

71 lines not on this page · raw message

drivers/hv/dxgkrnl/ioctl.c new file +24 -0
new file mode 100644
index 000000000000..23ecd15b0cd7
@@ -0,0 +1,24 @@
+// SPDX-License-Identifier: GPL-2.0
+
+/*
+ * Copyright (c) 2022, Microsoft Corporation.
+ *
+ * Author:
+ *   Iouri Tarassov <[email protected]>
+ *
+ * Dxgkrnl Graphics Driver
+ * Ioctl implementation
+ *
+ */
+
+#include <linux/eventfd.h>
+#include <linux/file.h>
+#include <linux/fs.h>
+#include <linux/anon_inodes.h>
+#include <linux/mman.h>
+
+#include "dxgkrnl.h"
+#include "dxgvmbus.h"
+
+#undef pr_fmt
+#define pr_fmt(fmt)	"dxgk: " fmt
drivers/hv/dxgkrnl/misc.h new file +72 -0

72 lines not on this page · raw message

include/uapi/misc/d3dkmthk.h +34 -0
index 5d973604400c..2ea04cc02a1f 100644
@@ -14,6 +14,40 @@
 #ifndef _D3DKMTHK_H
 #define _D3DKMTHK_H
 
+/*
+ * This structure matches the definition of D3DKMTHANDLE in Windows.
+ * The handle is opaque in user mode. It is used by user mode applications to
+ * represent kernel mode objects, created by dxgkrnl.
+ */
+struct d3dkmthandle {
+	union {
+		struct {
+			__u32 instance	:  6;
+			__u32 index	: 24;
+			__u32 unique	: 2;
+		};
+		__u32 v;
+	};
+};
+
+/*
+ * VM bus messages return Windows' NTSTATUS, which is integer and only negative
+ * value indicates a failure. A positive number is a success and needs to be
+ * returned to user mode as the IOCTL return code. Negative status codes are
+ * converted to Linux error codes.
+ */
+struct ntstatus {
+	union {
+		struct {
+			int code	: 16;
+			int facility	: 13;
+			int customer	: 1;
+			int severity	: 2;
+		};
+		int v;
+	};
+};
+
 /*
  * Matches the Windows LUID definition.
  * LUID is a locally unique identifier (similar to GUID, but not global),
#3 lore raw

PATCH v4 3/55 drivers: hv: dxgkrnl: Creation of dxgadapter object

In reply to Eric Curtin

From: Iouri Tarassov <[email protected]>

Handle creation and destruction of dxgadapter object, which represents a virtual compute device, projected to the VM by the host. The dxgadapter object is created when the corresponding VMBus channel is offered by Hyper-V.

There could be multiple virtual compute device objects, projected by the host to VM. They are enumerated by issuing IOCTLs to the /dev/dxg device.

The adapter object can start functioning only when the global VMBus channel and the corresponding per device VMBus channel are initialized. Notifications about arrival of a virtual compute PCI device and VMBus channels can happen in any order. Therefore, the initial dxgadapter object state is DXGADAPTER_STATE_WAITING_VMBUS. A list of VMBus channels and a list of waiting dxgadapter objects are maintained. When dxgkrnl is notified about a VMBus channel arrival, if tries to start all adapters, which are not started yet.

Properties of the adapter object are determined by sending VMBus messages to the host to the corresponding VMBus channel.

When the per virtual compute device VMBus channel or the global channel are destroyed, the adapter object is destroyed.

[kms: forward port to 6.6 from 6.1. No code changes made.]
---
 drivers/hv/dxgkrnl/Makefile     |   2 +-
 drivers/hv/dxgkrnl/dxgadapter.c | 170 +++++++++++++++++++++++++
 drivers/hv/dxgkrnl/dxgkrnl.h    |  85 +++++++++++++
 drivers/hv/dxgkrnl/dxgmodule.c  | 204 +++++++++++++++++++++++++++++-
 drivers/hv/dxgkrnl/dxgvmbus.c   | 217 +++++++++++++++++++++++++++++---
 drivers/hv/dxgkrnl/dxgvmbus.h   | 128 +++++++++++++++++++
 drivers/hv/dxgkrnl/misc.c       |  37 ++++++
 drivers/hv/dxgkrnl/misc.h       |  24 +++-
 8 files changed, 844 insertions(+), 23 deletions(-)
 create mode 100644 drivers/hv/dxgkrnl/dxgadapter.c
 create mode 100644 drivers/hv/dxgkrnl/misc.c
drivers/hv/dxgkrnl/Makefile +1 -1
index 76349064b60a..2ed07d877c91 100644
@@ -2,4 +2,4 @@
 # Makefile for the hyper-v compute device driver (dxgkrnl).
 
 obj-$(CONFIG_DXGKRNL)	+= dxgkrnl.o
-dxgkrnl-y		:= dxgmodule.o dxgvmbus.o
+dxgkrnl-y		:= dxgmodule.o misc.o dxgadapter.o ioctl.o dxgvmbus.o
drivers/hv/dxgkrnl/dxgadapter.c new file +170 -0

170 lines not on this page · raw message

drivers/hv/dxgkrnl/dxgkrnl.h +85 -0

118 lines not on this page · raw message

drivers/hv/dxgkrnl/dxgmodule.c +203 -1

258 lines not on this page · raw message

drivers/hv/dxgkrnl/dxgvmbus.c +200 -17

279 lines not on this page · raw message

drivers/hv/dxgkrnl/dxgvmbus.h +128 -0

144 lines not on this page · raw message

drivers/hv/dxgkrnl/misc.c new file +37 -0
new file mode 100644
index 000000000000..cb1e0635bebc
@@ -0,0 +1,37 @@
+// SPDX-License-Identifier: GPL-2.0
+
+/*
+ * Copyright (c) 2019, Microsoft Corporation.
+ *
+ * Author:
+ *   Iouri Tarassov <[email protected]>
+ *
+ * Dxgkrnl Graphics Driver
+ * Helper functions
+ *
+ */
+
+#include <linux/slab.h>
+#include <linux/vmalloc.h>
+#include <linux/uaccess.h>
+
+#include "dxgkrnl.h"
+#include "misc.h"
+
+#undef pr_fmt
+#define pr_fmt(fmt)	"dxgk: " fmt
+
+u16 *wcsncpy(u16 *dest, const u16 *src, size_t n)
+{
+	int i;
+
+	for (i = 0; i < n; i++) {
+		dest[i] = src[i];
+		if (src[i] == 0) {
+			i++;
+			break;
+		}
+	}
+	dest[i - 1] = 0;
+	return dest;
+}
drivers/hv/dxgkrnl/misc.h +20 -4
index 4c6047c32a20..d292e9a9bb7f 100644
@@ -14,18 +14,34 @@
 #ifndef _MISC_H_
 #define _MISC_H_
 
+/* Max characters in Windows path */
+#define WIN_MAX_PATH		260
+
 extern const struct d3dkmthandle zerohandle;
 
 /*
  * Synchronization lock hierarchy.
  *
- * The higher enum value, the higher is the lock order.
- * When a lower lock ois held, the higher lock should not be acquired.
+ * The locks here are in the order from lowest to highest.
+ * When a lower lock is held, the higher lock should not be acquired.
  *
- * channel_lock
- * device_mutex
+ * channel_lock (VMBus channel lock)
+ * fd_mutex
+ * plistmutex (process list mutex)
+ * table_lock (handle table lock)
+ * core_lock (dxgadapter lock)
+ * device_lock (dxgdevice lock)
+ * adapter_list_lock
+ * device_mutex (dxgglobal mutex)
  */
 
+u16 *wcsncpy(u16 *dest, const u16 *src, size_t n);
+
+enum dxglockstate {
+	DXGLOCK_SHARED,
+	DXGLOCK_EXCL
+};
+
 /*
  * Some of the Windows return codes, which needs to be translated to Linux
  * IOCTL return codes. Positive values are success codes and need to be
#4 lore raw

PATCH v4 4/55 drivers: hv: dxgkrnl: Opening of /dev/dxg device and dxgprocess creation

In reply to Eric Curtin

From: Iouri Tarassov <[email protected]>

- Implement opening of the device (/dev/dxg) file object and creation of dxgprocess objects.

- Add VM bus messages to create and destroy the host side of a dxgprocess object.

- Implement the handle manager, which manages d3dkmthandle handles for the internal process objects. The handles are used by a user mode client to reference dxgkrnl objects.

dxgprocess is created for each process, which opens /dev/dxg. dxgprocess is ref counted, so the existing dxgprocess objects is used for a process, which opens the device object multiple time. dxgprocess is destroyed when the file object is released.

A corresponding dxgprocess object is created on the host for every dxgprocess object in the guest.

When a dxgkrnl object is created, in most cases the corresponding object is created in the host. The VM references the host objects by handles (d3dkmthandle). d3dkmthandle values for a host object and the corresponding VM object are the same. A host handle is allocated first and its value is assigned to the guest object.

[kms: forward port to 6.6 from 6.1. No code changes made.]
---
 drivers/hv/dxgkrnl/Makefile     |   2 +-
 drivers/hv/dxgkrnl/dxgadapter.c |  72 ++++
 drivers/hv/dxgkrnl/dxgkrnl.h    |  95 +++++-
 drivers/hv/dxgkrnl/dxgmodule.c  |  97 ++++++
 drivers/hv/dxgkrnl/dxgprocess.c | 262 +++++++++++++++
 drivers/hv/dxgkrnl/dxgvmbus.c   | 164 ++++++++++
 drivers/hv/dxgkrnl/dxgvmbus.h   |  36 ++
 drivers/hv/dxgkrnl/hmgr.c       | 563 ++++++++++++++++++++++++++++++++
 drivers/hv/dxgkrnl/hmgr.h       | 112 +++++++
 drivers/hv/dxgkrnl/ioctl.c      |  60 ++++
 drivers/hv/dxgkrnl/misc.h       |   9 +-
 include/uapi/misc/d3dkmthk.h    | 103 ++++++
 12 files changed, 1569 insertions(+), 6 deletions(-)
 create mode 100644 drivers/hv/dxgkrnl/dxgprocess.c
 create mode 100644 drivers/hv/dxgkrnl/hmgr.c
 create mode 100644 drivers/hv/dxgkrnl/hmgr.h
drivers/hv/dxgkrnl/Makefile +1 -1
index 2ed07d877c91..9d821e83448a 100644
@@ -2,4 +2,4 @@
 # Makefile for the hyper-v compute device driver (dxgkrnl).
 
 obj-$(CONFIG_DXGKRNL)	+= dxgkrnl.o
-dxgkrnl-y		:= dxgmodule.o misc.o dxgadapter.o ioctl.o dxgvmbus.o
+dxgkrnl-y		:= dxgmodule.o hmgr.o misc.o dxgadapter.o ioctl.o dxgvmbus.o dxgprocess.o
drivers/hv/dxgkrnl/dxgadapter.c +72 -0

93 lines not on this page · raw message

drivers/hv/dxgkrnl/dxgkrnl.h +94 -1

148 lines not on this page · raw message

drivers/hv/dxgkrnl/dxgmodule.c +97 -0

124 lines not on this page · raw message

drivers/hv/dxgkrnl/dxgprocess.c new file +262 -0

262 lines not on this page · raw message

drivers/hv/dxgkrnl/dxgvmbus.c +164 -0

173 lines not on this page · raw message

drivers/hv/dxgkrnl/dxgvmbus.h +36 -0
index 584cdd3db6c0..a805a396e083 100644
@@ -14,7 +14,11 @@
 #ifndef _DXGVMBUS_H
 #define _DXGVMBUS_H
 
+struct dxgprocess;
+struct dxgadapter;
+
 #define DXG_MAX_VM_BUS_PACKET_SIZE	(1024 * 128)
+#define DXG_VM_PROCESS_NAME_LENGTH	260
 
 enum dxgkvmb_commandchanneltype {
 	DXGKVMB_VGPU_TO_HOST,
@@ -169,6 +173,26 @@ struct dxgkvmb_command_setiospaceregion {
 	u32				shared_page_gpadl;
 };
 
+struct dxgkvmb_command_createprocess {
+	struct dxgkvmb_command_vm_to_host hdr;
+	void			*process;
+	u64			process_id;
+	u16			process_name[DXG_VM_PROCESS_NAME_LENGTH + 1];
+	u8			csrss_process:1;
+	u8			dwm_process:1;
+	u8			wow64_process:1;
+	u8			linux_process:1;
+};
+
+struct dxgkvmb_command_createprocess_return {
+	struct d3dkmthandle	hprocess;
+};
+
+// The command returns ntstatus
+struct dxgkvmb_command_destroyprocess {
+	struct dxgkvmb_command_vm_to_host hdr;
+};
+
 struct dxgkvmb_command_openadapter {
 	struct dxgkvmb_command_vgpu_to_host hdr;
 	u32				vmbus_interface_version;
@@ -211,4 +235,16 @@ struct dxgkvmb_command_getinternaladapterinfo_return {
 	struct winluid			host_vgpu_luid;
 };
 
+struct dxgkvmb_command_queryadapterinfo {
+	struct dxgkvmb_command_vgpu_to_host hdr;
+	enum kmtqueryadapterinfotype	query_type;
+	u32				private_data_size;
+	u8				private_data[1];
+};
+
+struct dxgkvmb_command_queryadapterinfo_return {
+	struct ntstatus			status;
+	u8				private_data[1];
+};
+
 #endif /* _DXGVMBUS_H */
drivers/hv/dxgkrnl/hmgr.c new file +563 -0

563 lines not on this page · raw message

drivers/hv/dxgkrnl/hmgr.h new file +112 -0

112 lines not on this page · raw message

drivers/hv/dxgkrnl/ioctl.c +60 -0

63 lines not on this page · raw message

drivers/hv/dxgkrnl/misc.h +5 -4
index d292e9a9bb7f..dc849a8ed3f2 100644
@@ -27,10 +27,11 @@ extern const struct d3dkmthandle zerohandle;
  *
  * channel_lock (VMBus channel lock)
  * fd_mutex
- * plistmutex (process list mutex)
- * table_lock (handle table lock)
- * core_lock (dxgadapter lock)
- * device_lock (dxgdevice lock)
+ * plistmutex
+ * table_lock
+ * core_lock
+ * device_lock
+ * process_adapter_mutex
  * adapter_list_lock
  * device_mutex (dxgglobal mutex)
  */
include/uapi/misc/d3dkmthk.h +103 -0

107 lines not on this page · raw message

#5 lore raw

PATCH v4 5/55 drivers: hv: dxgkrnl: Enumerate and open dxgadapter objects

In reply to Eric Curtin

From: Iouri Tarassov <[email protected]>

Implement ioctls to enumerate dxgadapter objects:
- The LX_DXENUMADAPTERS2 ioctl
- The LX_DXENUMADAPTERS3 ioctl.

Implement ioctls to open adapter by LUID and to close adapter handle:
- The LX_DXOPENADAPTERFROMLUID ioctl
- the LX_DXCLOSEADAPTER ioctl

Impllement the ioctl to query dxgadapter information:
- The LX_DXQUERYADAPTERINFO ioctl

When a dxgadapter is enumerated, it is implicitely opened and a handle (d3dkmthandle) is created in the current process handle table. The handle is returned to the caller and can be used by user mode to reference the VGPU adapter in other ioctls.

The caller is responsible to close the adapter when it is not longer used by sending the LX_DXCLOSEADAPTER ioctl.

A dxgprocess has a list of opened dxgadapter objects (dxgprocess_adapter is used to represent the entry in the list). A dxgadapter also has a list of dxgprocess_adapter objects. This is needed for cleanup because either a process or an adapter could be destroyed first.

[kms: forward port to 6.6 from 6.1. No code changes made.]
---
 drivers/hv/dxgkrnl/dxgmodule.c |   3 +
 drivers/hv/dxgkrnl/ioctl.c     | 482 ++++++++++++++++++++++++++++++++-
 2 files changed, 484 insertions(+), 1 deletion(-)
drivers/hv/dxgkrnl/dxgmodule.c +3 -0
index 17c22001ca6c..fbe1c58ecb46 100644
@@ -721,6 +721,9 @@ static struct dxgglobal *dxgglobal_create(void)
 
 	init_rwsem(&dxgglobal->channel_lock);
 
+#ifdef DEBUG
+	dxgk_validate_ioctls();
+#endif
 	return dxgglobal;
 }
 
drivers/hv/dxgkrnl/ioctl.c +481 -1

492 lines not on this page · raw message

#6 lore raw

PATCH v4 6/55 drivers: hv: dxgkrnl: Creation of dxgdevice objects

In reply to Eric Curtin

From: Iouri Tarassov <[email protected]>

Implement ioctls for creation and destruction of dxgdevice objects:
- the LX_DXCREATEDEVICE ioctl
- the LX_DXDESTROYDEVICE ioctl

A dxgdevice object represents a container of other virtual compute device objects (allocations, sync objects, contexts, etc.). It belongs to a dxgadapter object.

[kms: forward port to 6.6 from 6.1. No code changes made.]
---
 drivers/hv/dxgkrnl/dxgadapter.c | 187 ++++++++++++++++++++++++++++++++
 drivers/hv/dxgkrnl/dxgkrnl.h    |  58 ++++++++++
 drivers/hv/dxgkrnl/dxgprocess.c |  43 ++++++++
 drivers/hv/dxgkrnl/dxgvmbus.c   |  80 ++++++++++++++
 drivers/hv/dxgkrnl/dxgvmbus.h   |  22 ++++
 drivers/hv/dxgkrnl/ioctl.c      | 130 +++++++++++++++++++++-
 drivers/hv/dxgkrnl/misc.h       |   8 +-
 include/uapi/misc/d3dkmthk.h    |  82 ++++++++++++++
 8 files changed, 604 insertions(+), 6 deletions(-)
drivers/hv/dxgkrnl/dxgadapter.c +187 -0
index fa0d6beca157..a9a341716eba 100644
@@ -194,6 +194,122 @@ void dxgadapter_release_lock_shared(struct dxgadapter *adapter)
 	up_read(&adapter->core_lock);
 }
 
+struct dxgdevice *dxgdevice_create(struct dxgadapter *adapter,
+				   struct dxgprocess *process)
+{
+	struct dxgdevice *device;
+	int ret;
+
+	device = kzalloc(sizeof(struct dxgdevice), GFP_KERNEL);
+	if (device) {
+		kref_init(&device->device_kref);
+		device->adapter = adapter;
+		device->process = process;
+		kref_get(&adapter->adapter_kref);
+		init_rwsem(&device->device_lock);
+		INIT_LIST_HEAD(&device->pqueue_list_head);
+		device->object_state = DXGOBJECTSTATE_CREATED;
+		device->execution_state = _D3DKMT_DEVICEEXECUTION_ACTIVE;
+
+		ret = dxgprocess_adapter_add_device(process, adapter, device);
+		if (ret < 0) {
+			kref_put(&device->device_kref, dxgdevice_release);
+			device = NULL;
+		}
+	}
+	return device;
+}
+
+void dxgdevice_stop(struct dxgdevice *device)
+{
+}
+
+void dxgdevice_mark_destroyed(struct dxgdevice *device)
+{
+	down_write(&device->device_lock);
+	device->object_state = DXGOBJECTSTATE_DESTROYED;
+	up_write(&device->device_lock);
+}
+
+void dxgdevice_destroy(struct dxgdevice *device)
+{
+	struct dxgprocess *process = device->process;
+	struct dxgadapter *adapter = device->adapter;
+	struct d3dkmthandle device_handle = {};
+
+	DXG_TRACE("Destroying device: %p", device);
+
+	down_write(&device->device_lock);
+
+	if (device->object_state != DXGOBJECTSTATE_ACTIVE)
+		goto cleanup;
+
+	device->object_state = DXGOBJECTSTATE_DESTROYED;
+
+	dxgdevice_stop(device);
+
+	/* Guest handles need to be released before the host handles */
+	hmgrtable_lock(&process->handle_table, DXGLOCK_EXCL);
+	if (device->handle_valid) {
+		hmgrtable_free_handle(&process->handle_table,
+				      HMGRENTRY_TYPE_DXGDEVICE, device->handle);
+		device_handle = device->handle;
+		device->handle_valid = 0;
+	}
+	hmgrtable_unlock(&process->handle_table, DXGLOCK_EXCL);
+
+	if (device_handle.v) {
+		up_write(&device->device_lock);
+		if (dxgadapter_acquire_lock_shared(adapter) == 0) {
+			dxgvmb_send_destroy_device(adapter, process,
+						   device_handle);
+			dxgadapter_release_lock_shared(adapter);
+		}
+		down_write(&device->device_lock);
+	}
+
+cleanup:
+
+	if (device->adapter) {
+		dxgprocess_adapter_remove_device(device);
+		kref_put(&device->adapter->adapter_kref, dxgadapter_release);
+		device->adapter = NULL;
+	}
+
+	up_write(&device->device_lock);
+
+	kref_put(&device->device_kref, dxgdevice_release);
+	DXG_TRACE("Device destroyed");
+}
+
+int dxgdevice_acquire_lock_shared(struct dxgdevice *device)
+{
+	down_read(&device->device_lock);
+	if (!dxgdevice_is_active(device)) {
+		up_read(&device->device_lock);
+		return -ENODEV;
+	}
+	return 0;
+}
+
+void dxgdevice_release_lock_shared(struct dxgdevice *device)
+{
+	up_read(&device->device_lock);
+}
+
+bool dxgdevice_is_active(struct dxgdevice *device)
+{
+	return device->object_state == DXGOBJECTSTATE_ACTIVE;
+}
+
+void dxgdevice_release(struct kref *refcount)
+{
+	struct dxgdevice *device;
+
+	device = container_of(refcount, struct dxgdevice, device_kref);
+	kfree(device);
+}
+
 struct dxgprocess_adapter *dxgprocess_adapter_create(struct dxgprocess *process,
 						     struct dxgadapter *adapter)
 {
@@ -208,6 +324,8 @@ struct dxgprocess_adapter *dxgprocess_adapter_create(struct dxgprocess *process,
 		adapter_info->adapter = adapter;
 		adapter_info->process = process;
 		adapter_info->refcount = 1;
+		mutex_init(&adapter_info->device_list_mutex);
+		INIT_LIST_HEAD(&adapter_info->device_list_head);
 		list_add_tail(&adapter_info->process_adapter_list_entry,
 			      &process->process_adapter_list_head);
 		dxgadapter_add_process(adapter, adapter_info);
@@ -221,10 +339,34 @@ struct dxgprocess_adapter *dxgprocess_adapter_create(struct dxgprocess *process,
 
 void dxgprocess_adapter_stop(struct dxgprocess_adapter *adapter_info)
 {
+	struct dxgdevice *device;
+
+	mutex_lock(&adapter_info->device_list_mutex);
+	list_for_each_entry(device, &adapter_info->device_list_head,
+			    device_list_entry) {
+		dxgdevice_stop(device);
+	}
+	mutex_unlock(&adapter_info->device_list_mutex);
 }
 
 void dxgprocess_adapter_destroy(struct dxgprocess_adapter *adapter_info)
 {
+	struct dxgdevice *device;
+
+	mutex_lock(&adapter_info->device_list_mutex);
+	while (!list_empty(&adapter_info->device_list_head)) {
+		device = list_first_entry(&adapter_info->device_list_head,
+					  struct dxgdevice, device_list_entry);
+		list_del(&device->device_list_entry);
+		device->device_list_entry.next = NULL;
+		mutex_unlock(&adapter_info->device_list_mutex);
+		dxgvmb_send_flush_device(device,
+			DXGDEVICE_FLUSHSCHEDULER_DEVICE_TERMINATE);
+		dxgdevice_destroy(device);
+		mutex_lock(&adapter_info->device_list_mutex);
+	}
+	mutex_unlock(&adapter_info->device_list_mutex);
+
 	dxgadapter_remove_process(adapter_info);
 	kref_put(&adapter_info->adapter->adapter_kref, dxgadapter_release);
 	list_del(&adapter_info->process_adapter_list_entry);
@@ -240,3 +382,48 @@ void dxgprocess_adapter_release(struct dxgprocess_adapter *adapter_info)
 	if (adapter_info->refcount == 0)
 		dxgprocess_adapter_destroy(adapter_info);
 }
+
+int dxgprocess_adapter_add_device(struct dxgprocess *process,
+				  struct dxgadapter *adapter,
+				  struct dxgdevice *device)
+{
+	struct dxgprocess_adapter *entry;
+	struct dxgprocess_adapter *adapter_info = NULL;
+	int ret = 0;
+
+	dxgglobal_acquire_process_adapter_lock();
+
+	list_for_each_entry(entry, &process->process_adapter_list_head,
+			    process_adapter_list_entry) {
+		if (entry->adapter == adapter) {
+			adapter_info = entry;
+			break;
+		}
+	}
+	if (adapter_info == NULL) {
+		DXG_ERR("failed to find process adapter info");
+		ret = -EINVAL;
+		goto cleanup;
+	}
+	mutex_lock(&adapter_info->device_list_mutex);
+	list_add_tail(&device->device_list_entry,
+		      &adapter_info->device_list_head);
+	device->adapter_info = adapter_info;
+	mutex_unlock(&adapter_info->device_list_mutex);
+
+cleanup:
+
+	dxgglobal_release_process_adapter_lock();
+	return ret;
+}
+
+void dxgprocess_adapter_remove_device(struct dxgdevice *device)
+{
+	DXG_TRACE("Removing device: %p", device);
+	mutex_lock(&device->adapter_info->device_list_mutex);
+	if (device->device_list_entry.next) {
+		list_del(&device->device_list_entry);
+		device->device_list_entry.next = NULL;
+	}
+	mutex_unlock(&device->adapter_info->device_list_mutex);
+}
drivers/hv/dxgkrnl/dxgkrnl.h +58 -0

106 lines not on this page · raw message

drivers/hv/dxgkrnl/dxgprocess.c +43 -0

49 lines not on this page · raw message

drivers/hv/dxgkrnl/dxgvmbus.c +80 -0

86 lines not on this page · raw message

drivers/hv/dxgkrnl/dxgvmbus.h +22 -0
index a805a396e083..4ccf45765954 100644
@@ -247,4 +247,26 @@ struct dxgkvmb_command_queryadapterinfo_return {
 	u8				private_data[1];
 };
 
+struct dxgkvmb_command_createdevice {
+	struct dxgkvmb_command_vgpu_to_host hdr;
+	struct d3dkmt_createdeviceflags	flags;
+	bool				cdd_device;
+	void				*error_code;
+};
+
+struct dxgkvmb_command_createdevice_return {
+	struct d3dkmthandle		device;
+};
+
+struct dxgkvmb_command_destroydevice {
+	struct dxgkvmb_command_vgpu_to_host hdr;
+	struct d3dkmthandle		device;
+};
+
+struct dxgkvmb_command_flushdevice {
+	struct dxgkvmb_command_vgpu_to_host	hdr;
+	struct d3dkmthandle			device;
+	enum dxgdevice_flushschedulerreason	reason;
+};
+
 #endif /* _DXGVMBUS_H */
drivers/hv/dxgkrnl/ioctl.c +128 -2

145 lines not on this page · raw message

drivers/hv/dxgkrnl/misc.h +4 -4
index dc849a8ed3f2..e0bd33b365b0 100644
@@ -27,10 +27,10 @@ extern const struct d3dkmthandle zerohandle;
  *
  * channel_lock (VMBus channel lock)
  * fd_mutex
- * plistmutex
- * table_lock
- * core_lock
- * device_lock
+ * plistmutex (process list mutex)
+ * table_lock (handle table lock)
+ * core_lock (dxgadapter lock)
+ * device_lock (dxgdevice lock)
  * process_adapter_mutex
  * adapter_list_lock
  * device_mutex (dxgglobal mutex)
include/uapi/misc/d3dkmthk.h +82 -0

106 lines not on this page · raw message

#7 lore raw

PATCH v4 7/55 drivers: hv: dxgkrnl: Creation of dxgcontext objects

In reply to Eric Curtin

From: Iouri Tarassov <[email protected]>

Implement ioctls for creation/destruction of dxgcontext objects:
- the LX_DXCREATECONTEXTVIRTUAL ioctl
- the LX_DXDESTROYCONTEXT ioctl.

A dxgcontext object represents a compute device execution thread. Ccompute device DMA buffers and synchronization operations are submitted for execution to a dxgcontext. dxgcontexts objects belong to a dxgdevice object.

[kms: forward port to 6.6 from 6.1. No code changes made.]
---
 drivers/hv/dxgkrnl/dxgadapter.c | 103 ++++++++++++++++++++
 drivers/hv/dxgkrnl/dxgkrnl.h    |  38 ++++++++
 drivers/hv/dxgkrnl/dxgprocess.c |   4 +
 drivers/hv/dxgkrnl/dxgvmbus.c   | 101 ++++++++++++++++++-
 drivers/hv/dxgkrnl/dxgvmbus.h   |  18 ++++
 drivers/hv/dxgkrnl/ioctl.c      | 168 +++++++++++++++++++++++++++++++-
 drivers/hv/dxgkrnl/misc.h       |   1 +
 include/uapi/misc/d3dkmthk.h    |  47 +++++++++
 8 files changed, 477 insertions(+), 3 deletions(-)
drivers/hv/dxgkrnl/dxgadapter.c +103 -0
index a9a341716eba..cd103e092ac2 100644
@@ -206,7 +206,9 @@ struct dxgdevice *dxgdevice_create(struct dxgadapter *adapter,
 		device->adapter = adapter;
 		device->process = process;
 		kref_get(&adapter->adapter_kref);
+		INIT_LIST_HEAD(&device->context_list_head);
 		init_rwsem(&device->device_lock);
+		init_rwsem(&device->context_list_lock);
 		INIT_LIST_HEAD(&device->pqueue_list_head);
 		device->object_state = DXGOBJECTSTATE_CREATED;
 		device->execution_state = _D3DKMT_DEVICEEXECUTION_ACTIVE;
@@ -248,6 +250,20 @@ void dxgdevice_destroy(struct dxgdevice *device)
 
 	dxgdevice_stop(device);
 
+	{
+		struct dxgcontext *context;
+		struct dxgcontext *tmp;
+
+		DXG_TRACE("destroying contexts");
+		dxgdevice_acquire_context_list_lock(device);
+		list_for_each_entry_safe(context, tmp,
+					 &device->context_list_head,
+					 context_list_entry) {
+			dxgcontext_destroy(process, context);
+		}
+		dxgdevice_release_context_list_lock(device);
+	}
+
 	/* Guest handles need to be released before the host handles */
 	hmgrtable_lock(&process->handle_table, DXGLOCK_EXCL);
 	if (device->handle_valid) {
@@ -302,6 +318,32 @@ bool dxgdevice_is_active(struct dxgdevice *device)
 	return device->object_state == DXGOBJECTSTATE_ACTIVE;
 }
 
+void dxgdevice_acquire_context_list_lock(struct dxgdevice *device)
+{
+	down_write(&device->context_list_lock);
+}
+
+void dxgdevice_release_context_list_lock(struct dxgdevice *device)
+{
+	up_write(&device->context_list_lock);
+}
+
+void dxgdevice_add_context(struct dxgdevice *device, struct dxgcontext *context)
+{
+	down_write(&device->context_list_lock);
+	list_add_tail(&context->context_list_entry, &device->context_list_head);
+	up_write(&device->context_list_lock);
+}
+
+void dxgdevice_remove_context(struct dxgdevice *device,
+			      struct dxgcontext *context)
+{
+	if (context->context_list_entry.next) {
+		list_del(&context->context_list_entry);
+		context->context_list_entry.next = NULL;
+	}
+}
+
 void dxgdevice_release(struct kref *refcount)
 {
 	struct dxgdevice *device;
@@ -310,6 +352,67 @@ void dxgdevice_release(struct kref *refcount)
 	kfree(device);
 }
 
+struct dxgcontext *dxgcontext_create(struct dxgdevice *device)
+{
+	struct dxgcontext *context;
+
+	context = kzalloc(sizeof(struct dxgcontext), GFP_KERNEL);
+	if (context) {
+		kref_init(&context->context_kref);
+		context->device = device;
+		context->process = device->process;
+		context->device_handle = device->handle;
+		kref_get(&device->device_kref);
+		INIT_LIST_HEAD(&context->hwqueue_list_head);
+		init_rwsem(&context->hwqueue_list_lock);
+		dxgdevice_add_context(device, context);
+		context->object_state = DXGOBJECTSTATE_ACTIVE;
+	}
+	return context;
+}
+
+/*
+ * Called when the device context list lock is held
+ */
+void dxgcontext_destroy(struct dxgprocess *process, struct dxgcontext *context)
+{
+	DXG_TRACE("Destroying context %p", context);
+	context->object_state = DXGOBJECTSTATE_DESTROYED;
+	if (context->device) {
+		if (context->handle.v) {
+			hmgrtable_free_handle_safe(&process->handle_table,
+						   HMGRENTRY_TYPE_DXGCONTEXT,
+						   context->handle);
+		}
+		dxgdevice_remove_context(context->device, context);
+		kref_put(&context->device->device_kref, dxgdevice_release);
+	}
+	kref_put(&context->context_kref, dxgcontext_release);
+}
+
+void dxgcontext_destroy_safe(struct dxgprocess *process,
+			     struct dxgcontext *context)
+{
+	struct dxgdevice *device = context->device;
+
+	dxgdevice_acquire_context_list_lock(device);
+	dxgcontext_destroy(process, context);
+	dxgdevice_release_context_list_lock(device);
+}
+
+bool dxgcontext_is_active(struct dxgcontext *context)
+{
+	return context->object_state == DXGOBJECTSTATE_ACTIVE;
+}
+
+void dxgcontext_release(struct kref *refcount)
+{
+	struct dxgcontext *context;
+
+	context = container_of(refcount, struct dxgcontext, context_kref);
+	kfree(context);
+}
+
 struct dxgprocess_adapter *dxgprocess_adapter_create(struct dxgprocess *process,
 						     struct dxgadapter *adapter)
 {
drivers/hv/dxgkrnl/dxgkrnl.h +38 -0
index 45ac1f25cc5e..a3d8d3c9f37d 100644
@@ -35,6 +35,7 @@
 struct dxgprocess;
 struct dxgadapter;
 struct dxgdevice;
+struct dxgcontext;
 
 /*
  * Driver private data.
@@ -298,6 +299,7 @@ void dxgadapter_remove_process(struct dxgprocess_adapter *process_info);
 /*
  * The object represent the device object.
  * The following objects take reference on the device
+ * - dxgcontext
  * - device handle (struct d3dkmthandle)
  */
 struct dxgdevice {
@@ -311,6 +313,8 @@ struct dxgdevice {
 	struct kref		device_kref;
 	/* Protects destcruction of the device object */
 	struct rw_semaphore	device_lock;
+	struct rw_semaphore	context_list_lock;
+	struct list_head	context_list_head;
 	/* List of paging queues. Protected by process handle table lock. */
 	struct list_head	pqueue_list_head;
 	struct d3dkmthandle	handle;
@@ -325,7 +329,33 @@ void dxgdevice_mark_destroyed(struct dxgdevice *device);
 int dxgdevice_acquire_lock_shared(struct dxgdevice *dev);
 void dxgdevice_release_lock_shared(struct dxgdevice *dev);
 void dxgdevice_release(struct kref *refcount);
+void dxgdevice_add_context(struct dxgdevice *dev, struct dxgcontext *ctx);
+void dxgdevice_remove_context(struct dxgdevice *dev, struct dxgcontext *ctx);
 bool dxgdevice_is_active(struct dxgdevice *dev);
+void dxgdevice_acquire_context_list_lock(struct dxgdevice *dev);
+void dxgdevice_release_context_list_lock(struct dxgdevice *dev);
+
+/*
+ * The object represent the execution context of a device.
+ */
+struct dxgcontext {
+	enum dxgobjectstate	object_state;
+	struct dxgdevice	*device;
+	struct dxgprocess	*process;
+	/* entry in the device context list */
+	struct list_head	context_list_entry;
+	struct list_head	hwqueue_list_head;
+	struct rw_semaphore	hwqueue_list_lock;
+	struct kref		context_kref;
+	struct d3dkmthandle	handle;
+	struct d3dkmthandle	device_handle;
+};
+
+struct dxgcontext *dxgcontext_create(struct dxgdevice *dev);
+void dxgcontext_destroy(struct dxgprocess *pr, struct dxgcontext *ctx);
+void dxgcontext_destroy_safe(struct dxgprocess *pr, struct dxgcontext *ctx);
+void dxgcontext_release(struct kref *refcount);
+bool dxgcontext_is_active(struct dxgcontext *ctx);
 
 long dxgk_compat_ioctl(struct file *f, unsigned int p1, unsigned long p2);
 long dxgk_unlocked_ioctl(struct file *f, unsigned int p1, unsigned long p2);
@@ -371,6 +401,14 @@ int dxgvmb_send_destroy_device(struct dxgadapter *adapter,
 			       struct d3dkmthandle h);
 int dxgvmb_send_flush_device(struct dxgdevice *device,
 			     enum dxgdevice_flushschedulerreason reason);
+struct d3dkmthandle
+dxgvmb_send_create_context(struct dxgadapter *adapter,
+			   struct dxgprocess *process,
+			   struct d3dkmt_createcontextvirtual
+			   *args);
+int dxgvmb_send_destroy_context(struct dxgadapter *adapter,
+				struct dxgprocess *process,
+				struct d3dkmthandle h);
 int dxgvmb_send_query_adapter_info(struct dxgprocess *process,
 				   struct dxgadapter *adapter,
 				   struct d3dkmt_queryadapterinfo *args);
drivers/hv/dxgkrnl/dxgprocess.c +4 -0
index 8373f681e822..ca307beb9a9a 100644
@@ -257,6 +257,10 @@ struct dxgdevice *dxgprocess_device_by_object_handle(struct dxgprocess *process,
 		case HMGRENTRY_TYPE_DXGDEVICE:
 			device = obj;
 			break;
+		case HMGRENTRY_TYPE_DXGCONTEXT:
+			device_handle =
+			    ((struct dxgcontext *)obj)->device_handle;
+			break;
 		default:
 			DXG_ERR("invalid handle type: %d", t);
 			break;
drivers/hv/dxgkrnl/dxgvmbus.c +100 -1

113 lines not on this page · raw message

drivers/hv/dxgkrnl/dxgvmbus.h +18 -0
index 4ccf45765954..ebcb7b0f62c1 100644
@@ -269,4 +269,22 @@ struct dxgkvmb_command_flushdevice {
 	enum dxgdevice_flushschedulerreason	reason;
 };
 
+struct dxgkvmb_command_createcontextvirtual {
+	struct dxgkvmb_command_vgpu_to_host hdr;
+	struct d3dkmthandle		context;
+	struct d3dkmthandle		device;
+	u32				node_ordinal;
+	u32				engine_affinity;
+	struct d3dddi_createcontextflags flags;
+	enum d3dkmt_clienthint		client_hint;
+	u32				priv_drv_data_size;
+	u8				priv_drv_data[1];
+};
+
+/* The command returns ntstatus */
+struct dxgkvmb_command_destroycontext {
+	struct dxgkvmb_command_vgpu_to_host hdr;
+	struct d3dkmthandle	context;
+};
+
 #endif /* _DXGVMBUS_H */
drivers/hv/dxgkrnl/ioctl.c +166 -2

179 lines not on this page · raw message

drivers/hv/dxgkrnl/misc.h +1 -0
index e0bd33b365b0..3a9637f0b5e2 100644
@@ -29,6 +29,7 @@ extern const struct d3dkmthandle zerohandle;
  * fd_mutex
  * plistmutex (process list mutex)
  * table_lock (handle table lock)
+ * context_list_lock
  * core_lock (dxgadapter lock)
  * device_lock (dxgdevice lock)
  * process_adapter_mutex
include/uapi/misc/d3dkmthk.h +47 -0

59 lines not on this page · raw message

#8 lore raw

PATCH v4 8/55 drivers: hv: dxgkrnl: Creation of compute device allocations and resources

In reply to Eric Curtin

From: Iouri Tarassov <[email protected]>

Implemented ioctls to create and destroy virtual compute device allocations (dxgallocation) and resources (dxgresource):
- the LX_DXCREATEALLOCATION ioctl,
- the LX_DXDESTROYALLOCATION2 ioctl.

Compute device allocations (dxgallocation objects) represent memory allocation, which could be accessible by the device. Allocations can be created around existing system memory (provided by an application) or memory, allocated by dxgkrnl on the host.

Compute device resources (dxgresource objects) represent containers of compute device allocations. Allocations could be dynamically added, removed from a resource.

Each allocation/resource has associated driver private data, which is provided during creation.

Each created resource or allocation have a handle (d3dkmthandle), which is used to reference the corresponding object in other ioctls.

A dxgallocation can be resident (meaning that it is accessible by the compute device) or evicted. When an allocation is evicted, its content is stored in the backing store in system memory.

[kms: forward port to 6.6 from 6.1. No code changes made.]
---
 drivers/hv/dxgkrnl/dxgadapter.c | 282 ++++++++++++++
 drivers/hv/dxgkrnl/dxgkrnl.h    | 113 ++++++
 drivers/hv/dxgkrnl/dxgmodule.c  |   1 +
 drivers/hv/dxgkrnl/dxgvmbus.c   | 649 ++++++++++++++++++++++++++++++++
 drivers/hv/dxgkrnl/dxgvmbus.h   | 123 ++++++
 drivers/hv/dxgkrnl/ioctl.c      | 631 ++++++++++++++++++++++++++++++-
 drivers/hv/dxgkrnl/misc.h       |   3 +
 include/uapi/misc/d3dkmthk.h    | 204 ++++++++++
 8 files changed, 2004 insertions(+), 2 deletions(-)
drivers/hv/dxgkrnl/dxgadapter.c +282 -0
index cd103e092ac2..402caa81a5db 100644
@@ -207,8 +207,11 @@ struct dxgdevice *dxgdevice_create(struct dxgadapter *adapter,
 		device->process = process;
 		kref_get(&adapter->adapter_kref);
 		INIT_LIST_HEAD(&device->context_list_head);
+		INIT_LIST_HEAD(&device->alloc_list_head);
+		INIT_LIST_HEAD(&device->resource_list_head);
 		init_rwsem(&device->device_lock);
 		init_rwsem(&device->context_list_lock);
+		init_rwsem(&device->alloc_list_lock);
 		INIT_LIST_HEAD(&device->pqueue_list_head);
 		device->object_state = DXGOBJECTSTATE_CREATED;
 		device->execution_state = _D3DKMT_DEVICEEXECUTION_ACTIVE;
@@ -224,6 +227,14 @@ struct dxgdevice *dxgdevice_create(struct dxgadapter *adapter,
 
 void dxgdevice_stop(struct dxgdevice *device)
 {
+	struct dxgallocation *alloc;
+
+	DXG_TRACE("Destroying device: %p", device);
+	dxgdevice_acquire_alloc_list_lock(device);
+	list_for_each_entry(alloc, &device->alloc_list_head, alloc_list_entry) {
+		dxgallocation_stop(alloc);
+	}
+	dxgdevice_release_alloc_list_lock(device);
 }
 
 void dxgdevice_mark_destroyed(struct dxgdevice *device)
@@ -250,6 +261,33 @@ void dxgdevice_destroy(struct dxgdevice *device)
 
 	dxgdevice_stop(device);
 
+	dxgdevice_acquire_alloc_list_lock(device);
+
+	{
+		struct dxgallocation *alloc;
+		struct dxgallocation *tmp;
+
+		DXG_TRACE("destroying allocations");
+		list_for_each_entry_safe(alloc, tmp, &device->alloc_list_head,
+					 alloc_list_entry) {
+			dxgallocation_destroy(alloc);
+		}
+	}
+
+	{
+		struct dxgresource *resource;
+		struct dxgresource *tmp;
+
+		DXG_TRACE("destroying resources");
+		list_for_each_entry_safe(resource, tmp,
+					 &device->resource_list_head,
+					 resource_list_entry) {
+			dxgresource_destroy(resource);
+		}
+	}
+
+	dxgdevice_release_alloc_list_lock(device);
+
 	{
 		struct dxgcontext *context;
 		struct dxgcontext *tmp;
@@ -328,6 +366,26 @@ void dxgdevice_release_context_list_lock(struct dxgdevice *device)
 	up_write(&device->context_list_lock);
 }
 
+void dxgdevice_acquire_alloc_list_lock(struct dxgdevice *device)
+{
+	down_write(&device->alloc_list_lock);
+}
+
+void dxgdevice_release_alloc_list_lock(struct dxgdevice *device)
+{
+	up_write(&device->alloc_list_lock);
+}
+
+void dxgdevice_acquire_alloc_list_lock_shared(struct dxgdevice *device)
+{
+	down_read(&device->alloc_list_lock);
+}
+
+void dxgdevice_release_alloc_list_lock_shared(struct dxgdevice *device)
+{
+	up_read(&device->alloc_list_lock);
+}
+
 void dxgdevice_add_context(struct dxgdevice *device, struct dxgcontext *context)
 {
 	down_write(&device->context_list_lock);
@@ -344,6 +402,161 @@ void dxgdevice_remove_context(struct dxgdevice *device,
 	}
 }
 
+void dxgdevice_add_alloc(struct dxgdevice *device, struct dxgallocation *alloc)
+{
+	dxgdevice_acquire_alloc_list_lock(device);
+	list_add_tail(&alloc->alloc_list_entry, &device->alloc_list_head);
+	kref_get(&device->device_kref);
+	alloc->owner.device = device;
+	dxgdevice_release_alloc_list_lock(device);
+}
+
+void dxgdevice_remove_alloc(struct dxgdevice *device,
+			    struct dxgallocation *alloc)
+{
+	if (alloc->alloc_list_entry.next) {
+		list_del(&alloc->alloc_list_entry);
+		alloc->alloc_list_entry.next = NULL;
+		kref_put(&device->device_kref, dxgdevice_release);
+	}
+}
+
+void dxgdevice_remove_alloc_safe(struct dxgdevice *device,
+				 struct dxgallocation *alloc)
+{
+	dxgdevice_acquire_alloc_list_lock(device);
+	dxgdevice_remove_alloc(device, alloc);
+	dxgdevice_release_alloc_list_lock(device);
+}
+
+void dxgdevice_add_resource(struct dxgdevice *device, struct dxgresource *res)
+{
+	dxgdevice_acquire_alloc_list_lock(device);
+	list_add_tail(&res->resource_list_entry, &device->resource_list_head);
+	kref_get(&device->device_kref);
+	dxgdevice_release_alloc_list_lock(device);
+}
+
+void dxgdevice_remove_resource(struct dxgdevice *device,
+			       struct dxgresource *res)
+{
+	if (res->resource_list_entry.next) {
+		list_del(&res->resource_list_entry);
+		res->resource_list_entry.next = NULL;
+		kref_put(&device->device_kref, dxgdevice_release);
+	}
+}
+
+struct dxgresource *dxgresource_create(struct dxgdevice *device)
+{
+	struct dxgresource *resource;
+
+	resource = kzalloc(sizeof(struct dxgresource), GFP_KERNEL);
+	if (resource) {
+		kref_init(&resource->resource_kref);
+		resource->device = device;
+		resource->process = device->process;
+		resource->object_state = DXGOBJECTSTATE_ACTIVE;
+		mutex_init(&resource->resource_mutex);
+		INIT_LIST_HEAD(&resource->alloc_list_head);
+		dxgdevice_add_resource(device, resource);
+	}
+	return resource;
+}
+
+void dxgresource_free_handle(struct dxgresource *resource)
+{
+	struct dxgallocation *alloc;
+	struct dxgprocess *process;
+
+	if (resource->handle_valid) {
+		process = resource->device->process;
+		hmgrtable_free_handle_safe(&process->handle_table,
+					   HMGRENTRY_TYPE_DXGRESOURCE,
+					   resource->handle);
+		resource->handle_valid = 0;
+	}
+	list_for_each_entry(alloc, &resource->alloc_list_head,
+			    alloc_list_entry) {
+		dxgallocation_free_handle(alloc);
+	}
+}
+
+void dxgresource_destroy(struct dxgresource *resource)
+{
+	/* device->alloc_list_lock is held */
+	struct dxgallocation *alloc;
+	struct dxgallocation *tmp;
+	struct d3dkmt_destroyallocation2 args = { };
+	int destroyed = test_and_set_bit(0, &resource->flags);
+	struct dxgdevice *device = resource->device;
+
+	if (!destroyed) {
+		dxgresource_free_handle(resource);
+		if (resource->handle.v) {
+			args.device = device->handle;
+			args.resource = resource->handle;
+			dxgvmb_send_destroy_allocation(device->process,
+						       device, &args, NULL);
+			resource->handle.v = 0;
+		}
+		list_for_each_entry_safe(alloc, tmp, &resource->alloc_list_head,
+					 alloc_list_entry) {
+			dxgallocation_destroy(alloc);
+		}
+		dxgdevice_remove_resource(device, resource);
+	}
+	kref_put(&resource->resource_kref, dxgresource_release);
+}
+
+void dxgresource_release(struct kref *refcount)
+{
+	struct dxgresource *resource;
+
+	resource = container_of(refcount, struct dxgresource, resource_kref);
+	kfree(resource);
+}
+
+bool dxgresource_is_active(struct dxgresource *resource)
+{
+	return resource->object_state == DXGOBJECTSTATE_ACTIVE;
+}
+
+int dxgresource_add_alloc(struct dxgresource *resource,
+				      struct dxgallocation *alloc)
+{
+	int ret = -ENODEV;
+	struct dxgdevice *device = resource->device;
+
+	dxgdevice_acquire_alloc_list_lock(device);
+	if (dxgresource_is_active(resource)) {
+		list_add_tail(&alloc->alloc_list_entry,
+			      &resource->alloc_list_head);
+		alloc->owner.resource = resource;
+		ret = 0;
+	}
+	alloc->resource_owner = 1;
+	dxgdevice_release_alloc_list_lock(device);
+	return ret;
+}
+
+void dxgresource_remove_alloc(struct dxgresource *resource,
+			      struct dxgallocation *alloc)
+{
+	if (alloc->alloc_list_entry.next) {
+		list_del(&alloc->alloc_list_entry);
+		alloc->alloc_list_entry.next = NULL;
+	}
+}
+
+void dxgresource_remove_alloc_safe(struct dxgresource *resource,
+				   struct dxgallocation *alloc)
+{
+	dxgdevice_acquire_alloc_list_lock(resource->device);
+	dxgresource_remove_alloc(resource, alloc);
+	dxgdevice_release_alloc_list_lock(resource->device);
+}
+
 void dxgdevice_release(struct kref *refcount)
 {
 	struct dxgdevice *device;
@@ -413,6 +626,75 @@ void dxgcontext_release(struct kref *refcount)
 	kfree(context);
 }
 
+struct dxgallocation *dxgallocation_create(struct dxgprocess *process)
+{
+	struct dxgallocation *alloc;
+
+	alloc = kzalloc(sizeof(struct dxgallocation), GFP_KERNEL);
+	if (alloc)
+		alloc->process = process;
+	return alloc;
+}
+
+void dxgallocation_stop(struct dxgallocation *alloc)
+{
+	if (alloc->pages) {
+		release_pages(alloc->pages, alloc->num_pages);
+		vfree(alloc->pages);
+		alloc->pages = NULL;
+	}
+}
+
+void dxgallocation_free_handle(struct dxgallocation *alloc)
+{
+	dxgprocess_ht_lock_exclusive_down(alloc->process);
+	if (alloc->handle_valid) {
+		hmgrtable_free_handle(&alloc->process->handle_table,
+				      HMGRENTRY_TYPE_DXGALLOCATION,
+				      alloc->alloc_handle);
+		alloc->handle_valid = 0;
+	}
+	dxgprocess_ht_lock_exclusive_up(alloc->process);
+}
+
+void dxgallocation_destroy(struct dxgallocation *alloc)
+{
+	struct dxgprocess *process = alloc->process;
+	struct d3dkmt_destroyallocation2 args = { };
+
+	dxgallocation_stop(alloc);
+	if (alloc->resource_owner)
+		dxgresource_remove_alloc(alloc->owner.resource, alloc);
+	else if (alloc->owner.device)
+		dxgdevice_remove_alloc(alloc->owner.device, alloc);
+	dxgallocation_free_handle(alloc);
+	if (alloc->alloc_handle.v && !alloc->resource_owner) {
+		args.device = alloc->owner.device->handle;
+		args.alloc_count = 1;
+		dxgvmb_send_destroy_allocation(process,
+					       alloc->owner.device,
+					       &args, &alloc->alloc_handle);
+	}
+#ifdef _MAIN_KERNEL_
+	if (alloc->gpadl.gpadl_handle) {
+		DXG_TRACE("Teardown gpadl %d",
+			alloc->gpadl.gpadl_handle);
+		vmbus_teardown_gpadl(dxgglobal_get_vmbus(), &alloc->gpadl);
+		alloc->gpadl.gpadl_handle = 0;
+	}
+else
+	if (alloc->gpadl) {
+		DXG_TRACE("Teardown gpadl %d",
+			alloc->gpadl);
+		vmbus_teardown_gpadl(dxgglobal_get_vmbus(), alloc->gpadl);
+		alloc->gpadl = 0;
+	}
+#endif
+	if (alloc->priv_drv_data)
+		vfree(alloc->priv_drv_data);
+	kfree(alloc);
+}
+
 struct dxgprocess_adapter *dxgprocess_adapter_create(struct dxgprocess *process,
 						     struct dxgadapter *adapter)
 {
drivers/hv/dxgkrnl/dxgkrnl.h +113 -0

155 lines not on this page · raw message

drivers/hv/dxgkrnl/dxgmodule.c +1 -0
index fbe1c58ecb46..053ce6f3e083 100644
@@ -162,6 +162,7 @@ int dxgglobal_create_adapter(struct pci_dev *dev, guid_t *guid,
 	init_rwsem(&adapter->core_lock);
 
 	INIT_LIST_HEAD(&adapter->adapter_process_list_head);
+	init_rwsem(&adapter->shared_resource_list_lock);
 	adapter->pci_dev = dev;
 	guid_to_luid(guid, &adapter->luid);
 
drivers/hv/dxgkrnl/dxgvmbus.c +649 -0

661 lines not on this page · raw message

drivers/hv/dxgkrnl/dxgvmbus.h +123 -0

135 lines not on this page · raw message

drivers/hv/dxgkrnl/ioctl.c +629 -2

649 lines not on this page · raw message

drivers/hv/dxgkrnl/misc.h +3 -0
index 3a9637f0b5e2..a51b29a6a68f 100644
@@ -30,6 +30,9 @@ extern const struct d3dkmthandle zerohandle;
  * plistmutex (process list mutex)
  * table_lock (handle table lock)
  * context_list_lock
+ * alloc_list_lock
+ * resource_mutex
+ * shared_resource_list_lock
  * core_lock (dxgadapter lock)
  * device_lock (dxgdevice lock)
  * process_adapter_mutex
include/uapi/misc/d3dkmthk.h +204 -0

224 lines not on this page · raw message

#9 lore raw

PATCH v4 9/55 drivers: hv: dxgkrnl: Creation of compute device sync objects

In reply to Eric Curtin

From: Iouri Tarassov <[email protected]>

Implement ioctls to create and destroy compute devicesync objects:
- the LX_DXCREATESYNCHRONIZATIONOBJECT ioctl,
- the LX_DXDESTROYSYNCHRONIZATIONOBJECT ioctl.

Compute device synchronization objects are used to synchronize execution of compute device commands, which are queued to different execution contexts (dxgcontext objects).

There are several types of sync objects (mutex, monitored fence, CPU event, fence). A "signal" or a "wait" operation could be queued to an execution context.

Monitored fence sync objects are particular important.
A monitored fence object has a fence value, which could be monitored by the compute device or by CPU. Therefore, a CPU virtual address is allocated during object creation to allow an application to read the fence value. dxg_map_iospace and dxg_unmap_iospace implement creation of the CPU virtual address. This is done as follow:
- The host allocates a portion of the guest IO space, which is mapped to the actual fence value memory on the host
- The host returns the guest IO space address to the guest
- The guest allocates a CPU virtual address and updates page tables to point to the IO space address

[kms: forward port to 6.6 from 6.1. No code changes made.]
---
 drivers/hv/dxgkrnl/dxgadapter.c | 184 ++++++++++++++++++++++++++++
 drivers/hv/dxgkrnl/dxgkrnl.h    |  80 +++++++++++++
 drivers/hv/dxgkrnl/dxgmodule.c  |   1 +
 drivers/hv/dxgkrnl/dxgprocess.c |  16 +++
 drivers/hv/dxgkrnl/dxgvmbus.c   | 205 ++++++++++++++++++++++++++++++++
 drivers/hv/dxgkrnl/dxgvmbus.h   |  20 ++++
 drivers/hv/dxgkrnl/ioctl.c      | 130 +++++++++++++++++++-
 include/uapi/misc/d3dkmthk.h    |  95 +++++++++++++++
 8 files changed, 729 insertions(+), 2 deletions(-)
drivers/hv/dxgkrnl/dxgadapter.c +184 -0
index 402caa81a5db..d2f2b96527e6 100644
@@ -160,6 +160,24 @@ void dxgadapter_remove_process(struct dxgprocess_adapter *process_info)
 	list_del(&process_info->adapter_process_list_entry);
 }
 
+void dxgadapter_add_syncobj(struct dxgadapter *adapter,
+			    struct dxgsyncobject *object)
+{
+	down_write(&adapter->shared_resource_list_lock);
+	list_add_tail(&object->syncobj_list_entry, &adapter->syncobj_list_head);
+	up_write(&adapter->shared_resource_list_lock);
+}
+
+void dxgadapter_remove_syncobj(struct dxgsyncobject *object)
+{
+	down_write(&object->adapter->shared_resource_list_lock);
+	if (object->syncobj_list_entry.next) {
+		list_del(&object->syncobj_list_entry);
+		object->syncobj_list_entry.next = NULL;
+	}
+	up_write(&object->adapter->shared_resource_list_lock);
+}
+
 int dxgadapter_acquire_lock_exclusive(struct dxgadapter *adapter)
 {
 	down_write(&adapter->core_lock);
@@ -213,6 +231,7 @@ struct dxgdevice *dxgdevice_create(struct dxgadapter *adapter,
 		init_rwsem(&device->context_list_lock);
 		init_rwsem(&device->alloc_list_lock);
 		INIT_LIST_HEAD(&device->pqueue_list_head);
+		INIT_LIST_HEAD(&device->syncobj_list_head);
 		device->object_state = DXGOBJECTSTATE_CREATED;
 		device->execution_state = _D3DKMT_DEVICEEXECUTION_ACTIVE;
 
@@ -228,6 +247,7 @@ struct dxgdevice *dxgdevice_create(struct dxgadapter *adapter,
 void dxgdevice_stop(struct dxgdevice *device)
 {
 	struct dxgallocation *alloc;
+	struct dxgsyncobject *syncobj;
 
 	DXG_TRACE("Destroying device: %p", device);
 	dxgdevice_acquire_alloc_list_lock(device);
@@ -235,6 +255,14 @@ void dxgdevice_stop(struct dxgdevice *device)
 		dxgallocation_stop(alloc);
 	}
 	dxgdevice_release_alloc_list_lock(device);
+
+	hmgrtable_lock(&device->process->handle_table, DXGLOCK_EXCL);
+	list_for_each_entry(syncobj, &device->syncobj_list_head,
+			    syncobj_list_entry) {
+		dxgsyncobject_stop(syncobj);
+	}
+	hmgrtable_unlock(&device->process->handle_table, DXGLOCK_EXCL);
+	DXG_TRACE("Device stopped: %p", device);
 }
 
 void dxgdevice_mark_destroyed(struct dxgdevice *device)
@@ -263,6 +291,20 @@ void dxgdevice_destroy(struct dxgdevice *device)
 
 	dxgdevice_acquire_alloc_list_lock(device);
 
+	while (!list_empty(&device->syncobj_list_head)) {
+		struct dxgsyncobject *syncobj =
+		    list_first_entry(&device->syncobj_list_head,
+				     struct dxgsyncobject,
+				     syncobj_list_entry);
+		list_del(&syncobj->syncobj_list_entry);
+		syncobj->syncobj_list_entry.next = NULL;
+		dxgdevice_release_alloc_list_lock(device);
+
+		dxgsyncobject_destroy(process, syncobj);
+
+		dxgdevice_acquire_alloc_list_lock(device);
+	}
+
 	{
 		struct dxgallocation *alloc;
 		struct dxgallocation *tmp;
@@ -565,6 +607,30 @@ void dxgdevice_release(struct kref *refcount)
 	kfree(device);
 }
 
+void dxgdevice_add_syncobj(struct dxgdevice *device,
+			   struct dxgsyncobject *syncobj)
+{
+	dxgdevice_acquire_alloc_list_lock(device);
+	list_add_tail(&syncobj->syncobj_list_entry, &device->syncobj_list_head);
+	kref_get(&syncobj->syncobj_kref);
+	dxgdevice_release_alloc_list_lock(device);
+}
+
+void dxgdevice_remove_syncobj(struct dxgsyncobject *entry)
+{
+	struct dxgdevice *device = entry->device;
+
+	dxgdevice_acquire_alloc_list_lock(device);
+	if (entry->syncobj_list_entry.next) {
+		list_del(&entry->syncobj_list_entry);
+		entry->syncobj_list_entry.next = NULL;
+		kref_put(&entry->syncobj_kref, dxgsyncobject_release);
+	}
+	dxgdevice_release_alloc_list_lock(device);
+	kref_put(&device->device_kref, dxgdevice_release);
+	entry->device = NULL;
+}
+
 struct dxgcontext *dxgcontext_create(struct dxgdevice *device)
 {
 	struct dxgcontext *context;
@@ -812,3 +878,121 @@ void dxgprocess_adapter_remove_device(struct dxgdevice *device)
 	}
 	mutex_unlock(&device->adapter_info->device_list_mutex);
 }
+
+struct dxgsyncobject *dxgsyncobject_create(struct dxgprocess *process,
+					   struct dxgdevice *device,
+					   struct dxgadapter *adapter,
+					   enum
+					   d3dddi_synchronizationobject_type
+					   type,
+					   struct
+					   d3dddi_synchronizationobject_flags
+					   flags)
+{
+	struct dxgsyncobject *syncobj;
+
+	syncobj = kzalloc(sizeof(*syncobj), GFP_KERNEL);
+	if (syncobj == NULL)
+		goto cleanup;
+	syncobj->type = type;
+	syncobj->process = process;
+	switch (type) {
+	case _D3DDDI_MONITORED_FENCE:
+	case _D3DDDI_PERIODIC_MONITORED_FENCE:
+		syncobj->monitored_fence = 1;
+		break;
+	default:
+		break;
+	}
+	if (flags.shared) {
+		syncobj->shared = 1;
+		if (!flags.nt_security_sharing) {
+			DXG_ERR("nt_security_sharing must be set");
+			goto cleanup;
+		}
+	}
+
+	kref_init(&syncobj->syncobj_kref);
+
+	if (syncobj->monitored_fence) {
+		syncobj->device = device;
+		syncobj->device_handle = device->handle;
+		kref_get(&device->device_kref);
+		dxgdevice_add_syncobj(device, syncobj);
+	} else {
+		dxgadapter_add_syncobj(adapter, syncobj);
+	}
+	syncobj->adapter = adapter;
+	kref_get(&adapter->adapter_kref);
+
+	DXG_TRACE("Syncobj created: %p", syncobj);
+	return syncobj;
+cleanup:
+	if (syncobj)
+		kfree(syncobj);
+	return NULL;
+}
+
+void dxgsyncobject_destroy(struct dxgprocess *process,
+			   struct dxgsyncobject *syncobj)
+{
+	int destroyed;
+
+	DXG_TRACE("Destroying syncobj: %p", syncobj);
+
+	dxgsyncobject_stop(syncobj);
+
+	destroyed = test_and_set_bit(0, &syncobj->flags);
+	if (!destroyed) {
+		DXG_TRACE("Deleting handle: %x", syncobj->handle.v);
+		hmgrtable_lock(&process->handle_table, DXGLOCK_EXCL);
+		if (syncobj->handle.v) {
+			hmgrtable_free_handle(&process->handle_table,
+					      HMGRENTRY_TYPE_DXGSYNCOBJECT,
+					      syncobj->handle);
+			syncobj->handle.v = 0;
+			kref_put(&syncobj->syncobj_kref, dxgsyncobject_release);
+		}
+		hmgrtable_unlock(&process->handle_table, DXGLOCK_EXCL);
+
+		if (syncobj->monitored_fence)
+			dxgdevice_remove_syncobj(syncobj);
+		else
+			dxgadapter_remove_syncobj(syncobj);
+		if (syncobj->adapter) {
+			kref_put(&syncobj->adapter->adapter_kref,
+				 dxgadapter_release);
+			syncobj->adapter = NULL;
+		}
+	}
+	kref_put(&syncobj->syncobj_kref, dxgsyncobject_release);
+}
+
+void dxgsyncobject_stop(struct dxgsyncobject *syncobj)
+{
+	int stopped = test_and_set_bit(1, &syncobj->flags);
+
+	if (!stopped) {
+		DXG_TRACE("Stopping syncobj");
+		if (syncobj->monitored_fence) {
+			if (syncobj->mapped_address) {
+				int ret =
+				    dxg_unmap_iospace(syncobj->mapped_address,
+						      PAGE_SIZE);
+
+				(void)ret;
+				DXG_TRACE("unmap fence %d %p",
+					ret, syncobj->mapped_address);
+				syncobj->mapped_address = NULL;
+			}
+		}
+	}
+}
+
+void dxgsyncobject_release(struct kref *refcount)
+{
+	struct dxgsyncobject *syncobj;
+
+	syncobj = container_of(refcount, struct dxgsyncobject, syncobj_kref);
+	kfree(syncobj);
+}
drivers/hv/dxgkrnl/dxgkrnl.h +80 -0

134 lines not on this page · raw message

drivers/hv/dxgkrnl/dxgmodule.c +1 -0
index 053ce6f3e083..9bc8931c5043 100644
@@ -162,6 +162,7 @@ int dxgglobal_create_adapter(struct pci_dev *dev, guid_t *guid,
 	init_rwsem(&adapter->core_lock);
 
 	INIT_LIST_HEAD(&adapter->adapter_process_list_head);
+	INIT_LIST_HEAD(&adapter->syncobj_list_head);
 	init_rwsem(&adapter->shared_resource_list_lock);
 	adapter->pci_dev = dev;
 	guid_to_luid(guid, &adapter->luid);
drivers/hv/dxgkrnl/dxgprocess.c +16 -0
index ca307beb9a9a..a41985ef438d 100644
@@ -59,6 +59,7 @@ void dxgprocess_destroy(struct dxgprocess *process)
 	enum hmgrentry_type t;
 	struct d3dkmthandle h;
 	void *o;
+	struct dxgsyncobject *syncobj;
 	struct dxgprocess_adapter *entry;
 	struct dxgprocess_adapter *tmp;
 
@@ -84,6 +85,21 @@ void dxgprocess_destroy(struct dxgprocess *process)
 		}
 	}
 
+	i = 0;
+	while (hmgrtable_next_entry(&process->handle_table, &i, &t, &h, &o)) {
+		switch (t) {
+		case HMGRENTRY_TYPE_DXGSYNCOBJECT:
+			DXG_TRACE("Destroy syncobj: %p %d", o, i);
+			syncobj = o;
+			syncobj->handle.v = 0;
+			dxgsyncobject_destroy(process, syncobj);
+			break;
+		default:
+			DXG_ERR("invalid entry in handle table %d", t);
+			break;
+		}
+	}
+
 	hmgrtable_destroy(&process->handle_table);
 	hmgrtable_destroy(&process->local_handle_table);
 }
drivers/hv/dxgkrnl/dxgvmbus.c +205 -0

230 lines not on this page · raw message

drivers/hv/dxgkrnl/dxgvmbus.h +20 -0
index 4b7466d1b9f2..bbf5f31cdf81 100644
@@ -410,4 +410,24 @@ struct dxgkvmb_command_destroycontext {
 	struct d3dkmthandle	context;
 };
 
+struct dxgkvmb_command_createsyncobject {
+	struct dxgkvmb_command_vgpu_to_host hdr;
+	struct d3dkmt_createsynchronizationobject2 args;
+	u32				client_hint;
+};
+
+struct dxgkvmb_command_createsyncobject_return {
+	struct d3dkmthandle	sync_object;
+	struct d3dkmthandle	global_sync_object;
+	u64			fence_gpu_va;
+	u64			fence_storage_address;
+	u32			fence_storage_offset;
+};
+
+/* The command returns ntstatus */
+struct dxgkvmb_command_destroysyncobject {
+	struct dxgkvmb_command_vm_to_host hdr;
+	struct d3dkmthandle	sync_object;
+};
+
 #endif /* _DXGVMBUS_H */
drivers/hv/dxgkrnl/ioctl.c +128 -2

148 lines not on this page · raw message

include/uapi/misc/d3dkmthk.h +95 -0

113 lines not on this page · raw message

#10 lore raw

PATCH v4 10/55 drivers: hv: dxgkrnl: Operations using sync objects

In reply to Eric Curtin

From: Iouri Tarassov <[email protected]>

Implement ioctls to submit operations with compute device sync objects:
- the LX_DXSIGNALSYNCHRONIZATIONOBJECT ioctl.
The ioctl is used to submit a signal to a sync object.
- the LX_DXWAITFORSYNCHRONIZATIONOBJECT ioctl.
The ioctl is used to submit a wait for a sync object
- the LX_DXSIGNALSYNCHRONIZATIONOBJECTFROMCPU ioctl The ioctl is used to signal to a monitored fence sync object from a CPU thread.
- the LX_DXSIGNALSYNCHRONIZATIONOBJECTFROMGPU ioctl. The ioctl is used to submit a signal to a monitored fence sync object..
- the LX_DXSIGNALSYNCHRONIZATIONOBJECTFROMGPU2 ioctl. The ioctl is used to submit a signal to a monitored fence sync object.
- the LX_DXWAITFORSYNCHRONIZATIONOBJECTFROMGPU ioctl. The ioctl is used to submit a wait for a monitored fence sync object.

Compute device synchronization objects are used to synchronize execution of DMA buffers between different execution contexts. Operations with sync objects include "signal" and "wait". A wait for a sync object is satisfied when the sync object is signaled.

A signal operation could be submitted to a compute device context or the sync object could be signaled by a CPU thread.

To improve performance, submitting operations to the host is done asynchronously when the host supports it.

[kms: forward port to 6.6 from 6.1. No code changes made.]
---
 drivers/hv/dxgkrnl/dxgadapter.c |  38 +-
 drivers/hv/dxgkrnl/dxgkrnl.h    |  62 +++
 drivers/hv/dxgkrnl/dxgmodule.c  | 102 ++++-
 drivers/hv/dxgkrnl/dxgvmbus.c   | 219 +++++++++-
 drivers/hv/dxgkrnl/dxgvmbus.h   |  48 +++
 drivers/hv/dxgkrnl/ioctl.c      | 702 +++++++++++++++++++++++++++++++-
 drivers/hv/dxgkrnl/misc.h       |   2 +
 include/uapi/misc/d3dkmthk.h    | 159 ++++++++
 8 files changed, 1311 insertions(+), 21 deletions(-)
drivers/hv/dxgkrnl/dxgadapter.c +28 -10
index d2f2b96527e6..04d827a15c54 100644
@@ -249,7 +249,7 @@ void dxgdevice_stop(struct dxgdevice *device)
 	struct dxgallocation *alloc;
 	struct dxgsyncobject *syncobj;
 
-	DXG_TRACE("Destroying device: %p", device);
+	DXG_TRACE("Stopping device: %p", device);
 	dxgdevice_acquire_alloc_list_lock(device);
 	list_for_each_entry(alloc, &device->alloc_list_head, alloc_list_entry) {
 		dxgallocation_stop(alloc);
@@ -743,15 +743,13 @@ void dxgallocation_destroy(struct dxgallocation *alloc)
 	}
 #ifdef _MAIN_KERNEL_
 	if (alloc->gpadl.gpadl_handle) {
-		DXG_TRACE("Teardown gpadl %d",
-			alloc->gpadl.gpadl_handle);
+		DXG_TRACE("Teardown gpadl %d", alloc->gpadl.gpadl_handle);
 		vmbus_teardown_gpadl(dxgglobal_get_vmbus(), &alloc->gpadl);
 		alloc->gpadl.gpadl_handle = 0;
 	}
 else
 	if (alloc->gpadl) {
-		DXG_TRACE("Teardown gpadl %d",
-			alloc->gpadl);
+		DXG_TRACE("Teardown gpadl %d", alloc->gpadl);
 		vmbus_teardown_gpadl(dxgglobal_get_vmbus(), alloc->gpadl);
 		alloc->gpadl = 0;
 	}
@@ -901,6 +899,13 @@ struct dxgsyncobject *dxgsyncobject_create(struct dxgprocess *process,
 	case _D3DDDI_PERIODIC_MONITORED_FENCE:
 		syncobj->monitored_fence = 1;
 		break;
+	case _D3DDDI_CPU_NOTIFICATION:
+		syncobj->cpu_event = 1;
+		syncobj->host_event = kzalloc(sizeof(*syncobj->host_event),
+					GFP_KERNEL);
+		if (syncobj->host_event == NULL)
+			goto cleanup;
+		break;
 	default:
 		break;
 	}
@@ -928,6 +933,8 @@ struct dxgsyncobject *dxgsyncobject_create(struct dxgprocess *process,
 	DXG_TRACE("Syncobj created: %p", syncobj);
 	return syncobj;
 cleanup:
+	if (syncobj->host_event)
+		kfree(syncobj->host_event);
 	if (syncobj)
 		kfree(syncobj);
 	return NULL;
@@ -937,6 +944,7 @@ void dxgsyncobject_destroy(struct dxgprocess *process,
 			   struct dxgsyncobject *syncobj)
 {
 	int destroyed;
+	struct dxghosteventcpu *host_event;
 
 	DXG_TRACE("Destroying syncobj: %p", syncobj);
 
@@ -955,6 +963,16 @@ void dxgsyncobject_destroy(struct dxgprocess *process,
 		}
 		hmgrtable_unlock(&process->handle_table, DXGLOCK_EXCL);
 
+		if (syncobj->cpu_event) {
+			host_event = syncobj->host_event;
+			if (host_event->cpu_event) {
+				eventfd_ctx_put(host_event->cpu_event);
+				if (host_event->hdr.event_id)
+					dxgglobal_remove_host_event(
+						&host_event->hdr);
+				host_event->cpu_event = NULL;
+			}
+		}
 		if (syncobj->monitored_fence)
 			dxgdevice_remove_syncobj(syncobj);
 		else
@@ -971,16 +989,14 @@ void dxgsyncobject_destroy(struct dxgprocess *process,
 void dxgsyncobject_stop(struct dxgsyncobject *syncobj)
 {
 	int stopped = test_and_set_bit(1, &syncobj->flags);
+	int ret;
 
 	if (!stopped) {
 		DXG_TRACE("Stopping syncobj");
 		if (syncobj->monitored_fence) {
 			if (syncobj->mapped_address) {
-				int ret =
-				    dxg_unmap_iospace(syncobj->mapped_address,
-						      PAGE_SIZE);
-
-				(void)ret;
+				ret = dxg_unmap_iospace(syncobj->mapped_address,
+							PAGE_SIZE);
 				DXG_TRACE("unmap fence %d %p",
 					ret, syncobj->mapped_address);
 				syncobj->mapped_address = NULL;
@@ -994,5 +1010,7 @@ void dxgsyncobject_release(struct kref *refcount)
 	struct dxgsyncobject *syncobj;
 
 	syncobj = container_of(refcount, struct dxgsyncobject, syncobj_kref);
+	if (syncobj->host_event)
+		kfree(syncobj->host_event);
 	kfree(syncobj);
 }
drivers/hv/dxgkrnl/dxgkrnl.h +62 -0

104 lines not on this page · raw message

drivers/hv/dxgkrnl/dxgmodule.c +101 -1

119 lines not on this page · raw message

drivers/hv/dxgkrnl/dxgvmbus.c +218 -1

243 lines not on this page · raw message

drivers/hv/dxgkrnl/dxgvmbus.h +48 -0

58 lines not on this page · raw message

drivers/hv/dxgkrnl/ioctl.c +693 -9

759 lines not on this page · raw message

drivers/hv/dxgkrnl/misc.h +2 -0
index a51b29a6a68f..ee2ebfdd1c13 100644
@@ -25,6 +25,8 @@ extern const struct d3dkmthandle zerohandle;
  * The locks here are in the order from lowest to highest.
  * When a lower lock is held, the higher lock should not be acquired.
  *
+ * device_list_mutex
+ * host_event_list_mutex
  * channel_lock (VMBus channel lock)
  * fd_mutex
  * plistmutex (process list mutex)
include/uapi/misc/d3dkmthk.h +159 -0

183 lines not on this page · raw message

#11 lore raw

PATCH v4 11/55 drivers: hv: dxgkrnl: Sharing of dxgresource objects

In reply to Eric Curtin

From: Iouri Tarassov <[email protected]>

Implement creation of shared resources and ioctls for sharing dxgresource objects between processes in the virtual machine.

A dxgresource object is a collection of dxgallocation objects. The driver API allows addition/removal of allocations to a resource, but has limitations on addition/removal of allocations to a shared resource. When a resource is "sealed", addition/removal of allocations is not allowed.

Resources are shared using file descriptor (FD) handles. The name "NT handle" is used to be compatible with Windows implementation.

An FD handle is created by the LX_DXSHAREOBJECTS ioctl. The given FD handle could be sent to another process using any Linux API.

To use a shared resource object in other ioctls the object needs to be opened using its FD handle. An resource object is opened by the LX_DXOPENRESOURCEFROMNTHANDLE ioctl. This ioctl returns a d3dkmthandle value, which can be used to reference the resource object.

The LX_DXQUERYRESOURCEINFOFROMNTHANDLE ioctl is used to query private driver data of a shared resource object. This private data needs to be used to actually open the object using the LX_DXOPENRESOURCEFROMNTHANDLE ioctl.

[kms: forward port to 6.6 from 6.1. No code changes made.]
---
 drivers/hv/dxgkrnl/dxgadapter.c |  81 ++++
 drivers/hv/dxgkrnl/dxgkrnl.h    |  77 ++++
 drivers/hv/dxgkrnl/dxgmodule.c  |   1 +
 drivers/hv/dxgkrnl/dxgvmbus.c   | 127 +++++
 drivers/hv/dxgkrnl/dxgvmbus.h   |  30 ++
 drivers/hv/dxgkrnl/ioctl.c      | 792 +++++++++++++++++++++++++++++++-
 include/uapi/misc/d3dkmthk.h    |  96 ++++
 7 files changed, 1200 insertions(+), 4 deletions(-)
drivers/hv/dxgkrnl/dxgadapter.c +81 -0
index 04d827a15c54..26fce9aba4f3 100644
@@ -160,6 +160,17 @@ void dxgadapter_remove_process(struct dxgprocess_adapter *process_info)
 	list_del(&process_info->adapter_process_list_entry);
 }
 
+void dxgadapter_remove_shared_resource(struct dxgadapter *adapter,
+				       struct dxgsharedresource *object)
+{
+	down_write(&adapter->shared_resource_list_lock);
+	if (object->shared_resource_list_entry.next) {
+		list_del(&object->shared_resource_list_entry);
+		object->shared_resource_list_entry.next = NULL;
+	}
+	up_write(&adapter->shared_resource_list_lock);
+}
+
 void dxgadapter_add_syncobj(struct dxgadapter *adapter,
 			    struct dxgsyncobject *object)
 {
@@ -489,6 +500,69 @@ void dxgdevice_remove_resource(struct dxgdevice *device,
 	}
 }
 
+struct dxgsharedresource *dxgsharedresource_create(struct dxgadapter *adapter)
+{
+	struct dxgsharedresource *resource;
+
+	resource = kzalloc(sizeof(*resource), GFP_KERNEL);
+	if (resource) {
+		INIT_LIST_HEAD(&resource->resource_list_head);
+		kref_init(&resource->sresource_kref);
+		mutex_init(&resource->fd_mutex);
+		resource->adapter = adapter;
+	}
+	return resource;
+}
+
+void dxgsharedresource_destroy(struct kref *refcount)
+{
+	struct dxgsharedresource *resource;
+
+	resource = container_of(refcount, struct dxgsharedresource,
+				sresource_kref);
+	if (resource->runtime_private_data)
+		vfree(resource->runtime_private_data);
+	if (resource->resource_private_data)
+		vfree(resource->resource_private_data);
+	if (resource->alloc_private_data_sizes)
+		vfree(resource->alloc_private_data_sizes);
+	if (resource->alloc_private_data)
+		vfree(resource->alloc_private_data);
+	kfree(resource);
+}
+
+void dxgsharedresource_add_resource(struct dxgsharedresource *shared_resource,
+				    struct dxgresource *resource)
+{
+	down_write(&shared_resource->adapter->shared_resource_list_lock);
+	DXG_TRACE("Adding resource: %p %p", shared_resource, resource);
+	list_add_tail(&resource->shared_resource_list_entry,
+		      &shared_resource->resource_list_head);
+	kref_get(&shared_resource->sresource_kref);
+	kref_get(&resource->resource_kref);
+	resource->shared_owner = shared_resource;
+	up_write(&shared_resource->adapter->shared_resource_list_lock);
+}
+
+void dxgsharedresource_remove_resource(struct dxgsharedresource
+				       *shared_resource,
+				       struct dxgresource *resource)
+{
+	struct dxgadapter *adapter = shared_resource->adapter;
+
+	down_write(&adapter->shared_resource_list_lock);
+	DXG_TRACE("Removing resource: %p %p", shared_resource, resource);
+	if (resource->shared_resource_list_entry.next) {
+		list_del(&resource->shared_resource_list_entry);
+		resource->shared_resource_list_entry.next = NULL;
+		kref_put(&shared_resource->sresource_kref,
+			 dxgsharedresource_destroy);
+		resource->shared_owner = NULL;
+		kref_put(&resource->resource_kref, dxgresource_release);
+	}
+	up_write(&adapter->shared_resource_list_lock);
+}
+
 struct dxgresource *dxgresource_create(struct dxgdevice *device)
 {
 	struct dxgresource *resource;
@@ -532,6 +606,7 @@ void dxgresource_destroy(struct dxgresource *resource)
 	struct d3dkmt_destroyallocation2 args = { };
 	int destroyed = test_and_set_bit(0, &resource->flags);
 	struct dxgdevice *device = resource->device;
+	struct dxgsharedresource *shared_resource;
 
 	if (!destroyed) {
 		dxgresource_free_handle(resource);
@@ -547,6 +622,12 @@ void dxgresource_destroy(struct dxgresource *resource)
 			dxgallocation_destroy(alloc);
 		}
 		dxgdevice_remove_resource(device, resource);
+		shared_resource = resource->shared_owner;
+		if (shared_resource) {
+			dxgsharedresource_remove_resource(shared_resource,
+							  resource);
+			resource->shared_owner = NULL;
+		}
 	}
 	kref_put(&resource->resource_kref, dxgresource_release);
 }
drivers/hv/dxgkrnl/dxgkrnl.h +77 -0

113 lines not on this page · raw message

drivers/hv/dxgkrnl/dxgmodule.c +1 -0
index 5a5ca8791d27..69e221613af9 100644
@@ -258,6 +258,7 @@ int dxgglobal_create_adapter(struct pci_dev *dev, guid_t *guid,
 	init_rwsem(&adapter->core_lock);
 
 	INIT_LIST_HEAD(&adapter->adapter_process_list_head);
+	INIT_LIST_HEAD(&adapter->shared_resource_list_head);
 	INIT_LIST_HEAD(&adapter->syncobj_list_head);
 	init_rwsem(&adapter->shared_resource_list_lock);
 	adapter->pci_dev = dev;
drivers/hv/dxgkrnl/dxgvmbus.c +127 -0

139 lines not on this page · raw message

drivers/hv/dxgkrnl/dxgvmbus.h +30 -0
index 89fecbcefbc8..73d7adac60a1 100644
@@ -172,6 +172,21 @@ struct dxgkvmb_command_signalguestevent {
 	bool				dereference_event;
 };
 
+/*
+ * The command returns struct d3dkmthandle of a shared object for the
+ * given pre-process object
+ */
+struct dxgkvmb_command_createntsharedobject {
+	struct dxgkvmb_command_vm_to_host hdr;
+	struct d3dkmthandle		object;
+};
+
+/* The command returns ntstatus */
+struct dxgkvmb_command_destroyntsharedobject {
+	struct dxgkvmb_command_vm_to_host hdr;
+	struct d3dkmthandle		shared_handle;
+};
+
 /* Returns ntstatus */
 struct dxgkvmb_command_setiospaceregion {
 	struct dxgkvmb_command_vm_to_host hdr;
@@ -305,6 +320,21 @@ struct dxgkvmb_command_createallocation {
 /* u8 priv_drv_data[] for each alloc_info */
 };
 
+struct dxgkvmb_command_openresource {
+	struct dxgkvmb_command_vgpu_to_host hdr;
+	struct d3dkmthandle		device;
+	bool				nt_security_sharing;
+	struct d3dkmthandle		global_share;
+	u32				allocation_count;
+	u32				total_priv_drv_data_size;
+};
+
+struct dxgkvmb_command_openresource_return {
+	struct d3dkmthandle		resource;
+	struct ntstatus			status;
+/* struct d3dkmthandle   allocation[allocation_count]; */
+};
+
 struct dxgkvmb_command_getstandardallocprivdata {
 	struct dxgkvmb_command_vgpu_to_host hdr;
 	enum d3dkmdt_standardallocationtype alloc_type;
drivers/hv/dxgkrnl/ioctl.c +788 -4

851 lines not on this page · raw message

include/uapi/misc/d3dkmthk.h +96 -0

107 lines not on this page · raw message

#12 lore raw

PATCH v4 12/55 drivers: hv: dxgkrnl: Sharing of sync objects

In reply to Eric Curtin

From: Iouri Tarassov <[email protected]>

Implement creation of a shared sync objects and the ioctl for sharing dxgsyncobject objects between processes in the virtual machine.

Sync objects are shared using file descriptor (FD) handles. The name "NT handle" is used to be compatible with Windows implementation.

An FD handle is created by the LX_DXSHAREOBJECTS ioctl. The created FD handle could be sent to another process using any Linux API.

To use a shared sync object in other ioctls, the object needs to be opened using its FD handle. A sync object is opened by the LX_DXOPENSYNCOBJECTFROMNTHANDLE2 ioctl, which returns a d3dkmthandle value.

[kms: forward port to 6.6 from 6.1. No code changes made.]
---
 drivers/hv/dxgkrnl/dxgadapter.c | 181 ++++++++++-
 drivers/hv/dxgkrnl/dxgkrnl.h    |  96 ++++++
 drivers/hv/dxgkrnl/dxgmodule.c  |   1 +
 drivers/hv/dxgkrnl/dxgprocess.c |   4 +
 drivers/hv/dxgkrnl/dxgvmbus.c   | 221 +++++++++++++
 drivers/hv/dxgkrnl/dxgvmbus.h   |  35 ++
 drivers/hv/dxgkrnl/ioctl.c      | 556 +++++++++++++++++++++++++++++++-
 include/uapi/misc/d3dkmthk.h    |  93 ++++++
 8 files changed, 1181 insertions(+), 6 deletions(-)
drivers/hv/dxgkrnl/dxgadapter.c +180 -1
index 26fce9aba4f3..f59173f13559 100644
@@ -171,6 +171,26 @@ void dxgadapter_remove_shared_resource(struct dxgadapter *adapter,
 	up_write(&adapter->shared_resource_list_lock);
 }
 
+void dxgadapter_add_shared_syncobj(struct dxgadapter *adapter,
+				   struct dxgsharedsyncobject *object)
+{
+	down_write(&adapter->shared_resource_list_lock);
+	list_add_tail(&object->adapter_shared_syncobj_list_entry,
+		      &adapter->adapter_shared_syncobj_list_head);
+	up_write(&adapter->shared_resource_list_lock);
+}
+
+void dxgadapter_remove_shared_syncobj(struct dxgadapter *adapter,
+				      struct dxgsharedsyncobject *object)
+{
+	down_write(&adapter->shared_resource_list_lock);
+	if (object->adapter_shared_syncobj_list_entry.next) {
+		list_del(&object->adapter_shared_syncobj_list_entry);
+		object->adapter_shared_syncobj_list_entry.next = NULL;
+	}
+	up_write(&adapter->shared_resource_list_lock);
+}
+
 void dxgadapter_add_syncobj(struct dxgadapter *adapter,
 			    struct dxgsyncobject *object)
 {
@@ -622,7 +642,7 @@ void dxgresource_destroy(struct dxgresource *resource)
 			dxgallocation_destroy(alloc);
 		}
 		dxgdevice_remove_resource(device, resource);
-		shared_resource = resource->shared_owner;
+				shared_resource = resource->shared_owner;
 		if (shared_resource) {
 			dxgsharedresource_remove_resource(shared_resource,
 							  resource);
@@ -736,6 +756,9 @@ struct dxgcontext *dxgcontext_create(struct dxgdevice *device)
  */
 void dxgcontext_destroy(struct dxgprocess *process, struct dxgcontext *context)
 {
+	struct dxghwqueue *hwqueue;
+	struct dxghwqueue *tmp;
+
 	DXG_TRACE("Destroying context %p", context);
 	context->object_state = DXGOBJECTSTATE_DESTROYED;
 	if (context->device) {
@@ -747,6 +770,10 @@ void dxgcontext_destroy(struct dxgprocess *process, struct dxgcontext *context)
 		dxgdevice_remove_context(context->device, context);
 		kref_put(&context->device->device_kref, dxgdevice_release);
 	}
+	list_for_each_entry_safe(hwqueue, tmp, &context->hwqueue_list_head,
+				 hwqueue_list_entry) {
+		dxghwqueue_destroy(process, hwqueue);
+	}
 	kref_put(&context->context_kref, dxgcontext_release);
 }
 
@@ -773,6 +800,38 @@ void dxgcontext_release(struct kref *refcount)
 	kfree(context);
 }
 
+int dxgcontext_add_hwqueue(struct dxgcontext *context,
+			   struct dxghwqueue *hwqueue)
+{
+	int ret = 0;
+
+	down_write(&context->hwqueue_list_lock);
+	if (dxgcontext_is_active(context))
+		list_add_tail(&hwqueue->hwqueue_list_entry,
+			      &context->hwqueue_list_head);
+	else
+		ret = -ENODEV;
+	up_write(&context->hwqueue_list_lock);
+	return ret;
+}
+
+void dxgcontext_remove_hwqueue(struct dxgcontext *context,
+			       struct dxghwqueue *hwqueue)
+{
+	if (hwqueue->hwqueue_list_entry.next) {
+		list_del(&hwqueue->hwqueue_list_entry);
+		hwqueue->hwqueue_list_entry.next = NULL;
+	}
+}
+
+void dxgcontext_remove_hwqueue_safe(struct dxgcontext *context,
+				    struct dxghwqueue *hwqueue)
+{
+	down_write(&context->hwqueue_list_lock);
+	dxgcontext_remove_hwqueue(context, hwqueue);
+	up_write(&context->hwqueue_list_lock);
+}
+
 struct dxgallocation *dxgallocation_create(struct dxgprocess *process)
 {
 	struct dxgallocation *alloc;
@@ -958,6 +1017,63 @@ void dxgprocess_adapter_remove_device(struct dxgdevice *device)
 	mutex_unlock(&device->adapter_info->device_list_mutex);
 }
 
+struct dxgsharedsyncobject *dxgsharedsyncobj_create(struct dxgadapter *adapter,
+						    struct dxgsyncobject *so)
+{
+	struct dxgsharedsyncobject *syncobj;
+
+	syncobj = kzalloc(sizeof(*syncobj), GFP_KERNEL);
+	if (syncobj) {
+		kref_init(&syncobj->ssyncobj_kref);
+		INIT_LIST_HEAD(&syncobj->shared_syncobj_list_head);
+		syncobj->adapter = adapter;
+		syncobj->type = so->type;
+		syncobj->monitored_fence = so->monitored_fence;
+		dxgadapter_add_shared_syncobj(adapter, syncobj);
+		kref_get(&adapter->adapter_kref);
+		init_rwsem(&syncobj->syncobj_list_lock);
+		mutex_init(&syncobj->fd_mutex);
+	}
+	return syncobj;
+}
+
+void dxgsharedsyncobj_release(struct kref *refcount)
+{
+	struct dxgsharedsyncobject *syncobj;
+
+	syncobj = container_of(refcount, struct dxgsharedsyncobject,
+			       ssyncobj_kref);
+	DXG_TRACE("Destroying shared sync object %p", syncobj);
+	if (syncobj->adapter) {
+		dxgadapter_remove_shared_syncobj(syncobj->adapter,
+							syncobj);
+		kref_put(&syncobj->adapter->adapter_kref,
+				dxgadapter_release);
+	}
+	kfree(syncobj);
+}
+
+void dxgsharedsyncobj_add_syncobj(struct dxgsharedsyncobject *shared,
+				  struct dxgsyncobject *syncobj)
+{
+	DXG_TRACE("Add syncobj 0x%p 0x%p", shared, syncobj);
+	kref_get(&shared->ssyncobj_kref);
+	down_write(&shared->syncobj_list_lock);
+	list_add(&syncobj->shared_syncobj_list_entry,
+		 &shared->shared_syncobj_list_head);
+	syncobj->shared_owner = shared;
+	up_write(&shared->syncobj_list_lock);
+}
+
+void dxgsharedsyncobj_remove_syncobj(struct dxgsharedsyncobject *shared,
+				     struct dxgsyncobject *syncobj)
+{
+	DXG_TRACE("Remove syncobj 0x%p", shared);
+	down_write(&shared->syncobj_list_lock);
+	list_del(&syncobj->shared_syncobj_list_entry);
+	up_write(&shared->syncobj_list_lock);
+}
+
 struct dxgsyncobject *dxgsyncobject_create(struct dxgprocess *process,
 					   struct dxgdevice *device,
 					   struct dxgadapter *adapter,
@@ -1091,7 +1207,70 @@ void dxgsyncobject_release(struct kref *refcount)
 	struct dxgsyncobject *syncobj;
 
 	syncobj = container_of(refcount, struct dxgsyncobject, syncobj_kref);
+	if (syncobj->shared_owner) {
+		dxgsharedsyncobj_remove_syncobj(syncobj->shared_owner,
+						syncobj);
+		kref_put(&syncobj->shared_owner->ssyncobj_kref,
+			 dxgsharedsyncobj_release);
+	}
 	if (syncobj->host_event)
 		kfree(syncobj->host_event);
 	kfree(syncobj);
 }
+
+struct dxghwqueue *dxghwqueue_create(struct dxgcontext *context)
+{
+	struct dxgprocess *process = context->device->process;
+	struct dxghwqueue *hwqueue = kzalloc(sizeof(*hwqueue), GFP_KERNEL);
+
+	if (hwqueue) {
+		kref_init(&hwqueue->hwqueue_kref);
+		hwqueue->context = context;
+		hwqueue->process = process;
+		hwqueue->device_handle = context->device->handle;
+		if (dxgcontext_add_hwqueue(context, hwqueue) < 0) {
+			kref_put(&hwqueue->hwqueue_kref, dxghwqueue_release);
+			hwqueue = NULL;
+		} else {
+			kref_get(&context->context_kref);
+		}
+	}
+	return hwqueue;
+}
+
+void dxghwqueue_destroy(struct dxgprocess *process, struct dxghwqueue *hwqueue)
+{
+	DXG_TRACE("Destroyng hwqueue %p", hwqueue);
+	hmgrtable_lock(&process->handle_table, DXGLOCK_EXCL);
+	if (hwqueue->handle.v) {
+		hmgrtable_free_handle(&process->handle_table,
+				      HMGRENTRY_TYPE_DXGHWQUEUE,
+				      hwqueue->handle);
+		hwqueue->handle.v = 0;
+	}
+	if (hwqueue->progress_fence_sync_object.v) {
+		hmgrtable_free_handle(&process->handle_table,
+				      HMGRENTRY_TYPE_MONITOREDFENCE,
+				      hwqueue->progress_fence_sync_object);
+		hwqueue->progress_fence_sync_object.v = 0;
+	}
+	hmgrtable_unlock(&process->handle_table, DXGLOCK_EXCL);
+
+	if (hwqueue->progress_fence_mapped_address) {
+		dxg_unmap_iospace(hwqueue->progress_fence_mapped_address,
+				  PAGE_SIZE);
+		hwqueue->progress_fence_mapped_address = NULL;
+	}
+	dxgcontext_remove_hwqueue_safe(hwqueue->context, hwqueue);
+
+	kref_put(&hwqueue->context->context_kref, dxgcontext_release);
+	kref_put(&hwqueue->hwqueue_kref, dxghwqueue_release);
+}
+
+void dxghwqueue_release(struct kref *refcount)
+{
+	struct dxghwqueue *hwqueue;
+
+	hwqueue = container_of(refcount, struct dxghwqueue, hwqueue_kref);
+	kfree(hwqueue);
+}
drivers/hv/dxgkrnl/dxgkrnl.h +96 -0

155 lines not on this page · raw message

drivers/hv/dxgkrnl/dxgmodule.c +1 -0
index 69e221613af9..8cbe1095599f 100644
@@ -259,6 +259,7 @@ int dxgglobal_create_adapter(struct pci_dev *dev, guid_t *guid,
 
 	INIT_LIST_HEAD(&adapter->adapter_process_list_head);
 	INIT_LIST_HEAD(&adapter->shared_resource_list_head);
+	INIT_LIST_HEAD(&adapter->adapter_shared_syncobj_list_head);
 	INIT_LIST_HEAD(&adapter->syncobj_list_head);
 	init_rwsem(&adapter->shared_resource_list_lock);
 	adapter->pci_dev = dev;
drivers/hv/dxgkrnl/dxgprocess.c +4 -0
index a41985ef438d..4021084ebd78 100644
@@ -277,6 +277,10 @@ struct dxgdevice *dxgprocess_device_by_object_handle(struct dxgprocess *process,
 			device_handle =
 			    ((struct dxgcontext *)obj)->device_handle;
 			break;
+		case HMGRENTRY_TYPE_DXGHWQUEUE:
+			device_handle =
+			    ((struct dxghwqueue *)obj)->device_handle;
+			break;
 		default:
 			DXG_ERR("invalid handle type: %d", t);
 			break;
drivers/hv/dxgkrnl/dxgvmbus.c +221 -0

233 lines not on this page · raw message

drivers/hv/dxgkrnl/dxgvmbus.h +35 -0
index 73d7adac60a1..2e2fd1ae5ec2 100644
@@ -172,6 +172,21 @@ struct dxgkvmb_command_signalguestevent {
 	bool				dereference_event;
 };
 
+struct dxgkvmb_command_opensyncobject {
+	struct dxgkvmb_command_vm_to_host hdr;
+	struct d3dkmthandle		device;
+	struct d3dkmthandle		global_sync_object;
+	u32				engine_affinity;
+	struct d3dddi_synchronizationobject_flags flags;
+};
+
+struct dxgkvmb_command_opensyncobject_return {
+	struct d3dkmthandle		sync_object;
+	struct ntstatus			status;
+	u64				gpu_virtual_address;
+	u64				guest_cpu_physical_address;
+};
+
 /*
  * The command returns struct d3dkmthandle of a shared object for the
  * given pre-process object
@@ -508,4 +523,24 @@ struct dxgkvmb_command_waitforsyncobjectfromgpu {
 	/* struct d3dkmthandle ObjectHandles[object_count] */
 };
 
+/* Returns the same structure */
+struct dxgkvmb_command_createhwqueue {
+	struct dxgkvmb_command_vgpu_to_host hdr;
+	struct ntstatus			status;
+	struct d3dkmthandle		hwqueue;
+	struct d3dkmthandle		hwqueue_progress_fence;
+	void				*hwqueue_progress_fence_cpuva;
+	u64				hwqueue_progress_fence_gpuva;
+	struct d3dkmthandle		context;
+	struct d3dddi_createhwqueueflags flags;
+	u32				priv_drv_data_size;
+	char				priv_drv_data[1];
+};
+
+/* The command returns ntstatus */
+struct dxgkvmb_command_destroyhwqueue {
+	struct dxgkvmb_command_vgpu_to_host hdr;
+	struct d3dkmthandle		hwqueue;
+};
+
 #endif /* _DXGVMBUS_H */
drivers/hv/dxgkrnl/ioctl.c +551 -5

654 lines not on this page · raw message

include/uapi/misc/d3dkmthk.h +93 -0

117 lines not on this page · raw message

#13 lore raw

PATCH v4 13/55 drivers: hv: dxgkrnl: Creation of paging queue objects.

In reply to Eric Curtin

From: Iouri Tarassov <[email protected]>

Implement ioctls for creation/destruction of the paging queue objects:
- LX_DXCREATEPAGINGQUEUE,
- LX_DXDESTROYPAGINGQUEUE

Paging queue objects (dxgpagingqueue) contain operations, which handle residency of device accessible allocations. An allocation is resident, when the device has access to it. For example, the allocation resides in local device memory or device page tables point to system memory which is made non-pageable.

Each paging queue has an associated monitored fence sync object, which is used to detect when a paging operation is completed.

[kms: forward port to 6.6 from 6.1. No code changes made.]
---
 drivers/hv/dxgkrnl/dxgadapter.c |  89 +++++++++++++++
 drivers/hv/dxgkrnl/dxgkrnl.h    |  24 ++++
 drivers/hv/dxgkrnl/dxgprocess.c |   4 +
 drivers/hv/dxgkrnl/dxgvmbus.c   |  74 +++++++++++++
 drivers/hv/dxgkrnl/dxgvmbus.h   |  17 +++
 drivers/hv/dxgkrnl/ioctl.c      | 189 +++++++++++++++++++++++++++++++-
 include/uapi/misc/d3dkmthk.h    |  27 +++++
 7 files changed, 418 insertions(+), 6 deletions(-)
drivers/hv/dxgkrnl/dxgadapter.c +89 -0
index f59173f13559..410f08768bad 100644
@@ -278,6 +278,7 @@ struct dxgdevice *dxgdevice_create(struct dxgadapter *adapter,
 void dxgdevice_stop(struct dxgdevice *device)
 {
 	struct dxgallocation *alloc;
+	struct dxgpagingqueue *pqueue;
 	struct dxgsyncobject *syncobj;
 
 	DXG_TRACE("Stopping device: %p", device);
@@ -288,6 +289,10 @@ void dxgdevice_stop(struct dxgdevice *device)
 	dxgdevice_release_alloc_list_lock(device);
 
 	hmgrtable_lock(&device->process->handle_table, DXGLOCK_EXCL);
+	list_for_each_entry(pqueue, &device->pqueue_list_head,
+			    pqueue_list_entry) {
+		dxgpagingqueue_stop(pqueue);
+	}
 	list_for_each_entry(syncobj, &device->syncobj_list_head,
 			    syncobj_list_entry) {
 		dxgsyncobject_stop(syncobj);
@@ -375,6 +380,17 @@ void dxgdevice_destroy(struct dxgdevice *device)
 		dxgdevice_release_context_list_lock(device);
 	}
 
+	{
+		struct dxgpagingqueue *tmp;
+		struct dxgpagingqueue *pqueue;
+
+		DXG_TRACE("destroying paging queues");
+		list_for_each_entry_safe(pqueue, tmp, &device->pqueue_list_head,
+					 pqueue_list_entry) {
+			dxgpagingqueue_destroy(pqueue);
+		}
+	}
+
 	/* Guest handles need to be released before the host handles */
 	hmgrtable_lock(&process->handle_table, DXGLOCK_EXCL);
 	if (device->handle_valid) {
@@ -708,6 +724,26 @@ void dxgdevice_release(struct kref *refcount)
 	kfree(device);
 }
 
+void dxgdevice_add_paging_queue(struct dxgdevice *device,
+				struct dxgpagingqueue *entry)
+{
+	dxgdevice_acquire_alloc_list_lock(device);
+	list_add_tail(&entry->pqueue_list_entry, &device->pqueue_list_head);
+	dxgdevice_release_alloc_list_lock(device);
+}
+
+void dxgdevice_remove_paging_queue(struct dxgpagingqueue *pqueue)
+{
+	struct dxgdevice *device = pqueue->device;
+
+	dxgdevice_acquire_alloc_list_lock(device);
+	if (pqueue->pqueue_list_entry.next) {
+		list_del(&pqueue->pqueue_list_entry);
+		pqueue->pqueue_list_entry.next = NULL;
+	}
+	dxgdevice_release_alloc_list_lock(device);
+}
+
 void dxgdevice_add_syncobj(struct dxgdevice *device,
 			   struct dxgsyncobject *syncobj)
 {
@@ -899,6 +935,59 @@ else
 	kfree(alloc);
 }
 
+struct dxgpagingqueue *dxgpagingqueue_create(struct dxgdevice *device)
+{
+	struct dxgpagingqueue *pqueue;
+
+	pqueue = kzalloc(sizeof(*pqueue), GFP_KERNEL);
+	if (pqueue) {
+		pqueue->device = device;
+		pqueue->process = device->process;
+		pqueue->device_handle = device->handle;
+		dxgdevice_add_paging_queue(device, pqueue);
+	}
+	return pqueue;
+}
+
+void dxgpagingqueue_stop(struct dxgpagingqueue *pqueue)
+{
+	int ret;
+
+	if (pqueue->mapped_address) {
+		ret = dxg_unmap_iospace(pqueue->mapped_address, PAGE_SIZE);
+		DXG_TRACE("fence is unmapped %d %p",
+			ret, pqueue->mapped_address);
+		pqueue->mapped_address = NULL;
+	}
+}
+
+void dxgpagingqueue_destroy(struct dxgpagingqueue *pqueue)
+{
+	struct dxgprocess *process = pqueue->process;
+
+	DXG_TRACE("Destroying pqueue %p %x", pqueue, pqueue->handle.v);
+
+	dxgpagingqueue_stop(pqueue);
+
+	hmgrtable_lock(&process->handle_table, DXGLOCK_EXCL);
+	if (pqueue->handle.v) {
+		hmgrtable_free_handle(&process->handle_table,
+				      HMGRENTRY_TYPE_DXGPAGINGQUEUE,
+				      pqueue->handle);
+		pqueue->handle.v = 0;
+	}
+	if (pqueue->syncobj_handle.v) {
+		hmgrtable_free_handle(&process->handle_table,
+				      HMGRENTRY_TYPE_MONITOREDFENCE,
+				      pqueue->syncobj_handle);
+		pqueue->syncobj_handle.v = 0;
+	}
+	hmgrtable_unlock(&process->handle_table, DXGLOCK_EXCL);
+	if (pqueue->device)
+		dxgdevice_remove_paging_queue(pqueue);
+	kfree(pqueue);
+}
+
 struct dxgprocess_adapter *dxgprocess_adapter_create(struct dxgprocess *process,
 						     struct dxgadapter *adapter)
 {
drivers/hv/dxgkrnl/dxgkrnl.h +24 -0
index 0330352b9c06..440d1f9b8882 100644
@@ -104,6 +104,16 @@ int dxgvmbuschannel_init(struct dxgvmbuschannel *ch, struct hv_device *hdev);
 void dxgvmbuschannel_destroy(struct dxgvmbuschannel *ch);
 void dxgvmbuschannel_receive(void *ctx);
 
+struct dxgpagingqueue {
+	struct dxgdevice	*device;
+	struct dxgprocess	*process;
+	struct list_head	pqueue_list_entry;
+	struct d3dkmthandle	device_handle;
+	struct d3dkmthandle	handle;
+	struct d3dkmthandle	syncobj_handle;
+	void			*mapped_address;
+};
+
 /*
  * The structure describes an event, which will be signaled by
  * a message from host.
@@ -127,6 +137,10 @@ struct dxghosteventcpu {
 	bool			remove_from_list;
 };
 
+struct dxgpagingqueue *dxgpagingqueue_create(struct dxgdevice *device);
+void dxgpagingqueue_destroy(struct dxgpagingqueue *pqueue);
+void dxgpagingqueue_stop(struct dxgpagingqueue *pqueue);
+
 /*
  * This is GPU synchronization object, which is used to synchronize execution
  * between GPU contextx/hardware queues or for tracking GPU execution progress.
@@ -516,6 +530,9 @@ void dxgdevice_remove_alloc_safe(struct dxgdevice *dev,
 				 struct dxgallocation *a);
 void dxgdevice_add_resource(struct dxgdevice *dev, struct dxgresource *res);
 void dxgdevice_remove_resource(struct dxgdevice *dev, struct dxgresource *res);
+void dxgdevice_add_paging_queue(struct dxgdevice *dev,
+				struct dxgpagingqueue *pqueue);
+void dxgdevice_remove_paging_queue(struct dxgpagingqueue *pqueue);
 void dxgdevice_add_syncobj(struct dxgdevice *dev, struct dxgsyncobject *so);
 void dxgdevice_remove_syncobj(struct dxgsyncobject *so);
 bool dxgdevice_is_active(struct dxgdevice *dev);
@@ -762,6 +779,13 @@ dxgvmb_send_create_context(struct dxgadapter *adapter,
 int dxgvmb_send_destroy_context(struct dxgadapter *adapter,
 				struct dxgprocess *process,
 				struct d3dkmthandle h);
+int dxgvmb_send_create_paging_queue(struct dxgprocess *pr,
+				    struct dxgdevice *dev,
+				    struct d3dkmt_createpagingqueue *args,
+				    struct dxgpagingqueue *pq);
+int dxgvmb_send_destroy_paging_queue(struct dxgprocess *process,
+				     struct dxgadapter *adapter,
+				     struct d3dkmthandle h);
 int dxgvmb_send_create_allocation(struct dxgprocess *pr, struct dxgdevice *dev,
 				  struct d3dkmt_createallocation *args,
 				  struct d3dkmt_createallocation *__user inargs,
drivers/hv/dxgkrnl/dxgprocess.c +4 -0
index 4021084ebd78..5de3f8ccb448 100644
@@ -277,6 +277,10 @@ struct dxgdevice *dxgprocess_device_by_object_handle(struct dxgprocess *process,
 			device_handle =
 			    ((struct dxgcontext *)obj)->device_handle;
 			break;
+		case HMGRENTRY_TYPE_DXGPAGINGQUEUE:
+			device_handle =
+			    ((struct dxgpagingqueue *)obj)->device_handle;
+			break;
 		case HMGRENTRY_TYPE_DXGHWQUEUE:
 			device_handle =
 			    ((struct dxghwqueue *)obj)->device_handle;
drivers/hv/dxgkrnl/dxgvmbus.c +74 -0

80 lines not on this page · raw message

drivers/hv/dxgkrnl/dxgvmbus.h +17 -0
index 2e2fd1ae5ec2..aba075d374c9 100644
@@ -462,6 +462,23 @@ struct dxgkvmb_command_destroycontext {
 	struct d3dkmthandle	context;
 };
 
+struct dxgkvmb_command_createpagingqueue {
+	struct dxgkvmb_command_vgpu_to_host hdr;
+	struct d3dkmt_createpagingqueue	args;
+};
+
+struct dxgkvmb_command_createpagingqueue_return {
+	struct d3dkmthandle	paging_queue;
+	struct d3dkmthandle	sync_object;
+	u64			fence_storage_physical_address;
+	u64			fence_storage_offset;
+};
+
+struct dxgkvmb_command_destroypagingqueue {
+	struct dxgkvmb_command_vgpu_to_host hdr;
+	struct d3dkmthandle	paging_queue;
+};
+
 struct dxgkvmb_command_createsyncobject {
 	struct dxgkvmb_command_vgpu_to_host hdr;
 	struct d3dkmt_createsynchronizationobject2 args;
drivers/hv/dxgkrnl/ioctl.c +183 -6

231 lines not on this page · raw message

include/uapi/misc/d3dkmthk.h +27 -0
index a78252901c8d..6ec70852de6e 100644
@@ -211,6 +211,29 @@ struct d3dddi_createhwqueueflags {
 	};
 };
 
+enum d3dddi_pagingqueue_priority {
+	_D3DDDI_PAGINGQUEUE_PRIORITY_BELOW_NORMAL	= -1,
+	_D3DDDI_PAGINGQUEUE_PRIORITY_NORMAL		= 0,
+	_D3DDDI_PAGINGQUEUE_PRIORITY_ABOVE_NORMAL	= 1,
+};
+
+struct d3dkmt_createpagingqueue {
+	struct d3dkmthandle		device;
+	enum d3dddi_pagingqueue_priority priority;
+	struct d3dkmthandle		paging_queue;
+	struct d3dkmthandle		sync_object;
+#ifdef __KERNEL__
+	void				*fence_cpu_virtual_address;
+#else
+	__u64				fence_cpu_virtual_address;
+#endif
+	__u32				physical_adapter_index;
+};
+
+struct d3dddi_destroypagingqueue {
+	struct d3dkmthandle		paging_queue;
+};
+
 enum d3dkmdt_gdisurfacetype {
 	_D3DKMDT_GDISURFACE_INVALID				= 0,
 	_D3DKMDT_GDISURFACE_TEXTURE				= 1,
@@ -890,6 +913,8 @@ struct d3dkmt_enumadapters3 {
 	_IOWR(0x47, 0x05, struct d3dkmt_destroycontext)
 #define LX_DXCREATEALLOCATION		\
 	_IOWR(0x47, 0x06, struct d3dkmt_createallocation)
+#define LX_DXCREATEPAGINGQUEUE		\
+	_IOWR(0x47, 0x07, struct d3dkmt_createpagingqueue)
 #define LX_DXQUERYADAPTERINFO		\
 	_IOWR(0x47, 0x09, struct d3dkmt_queryadapterinfo)
 #define LX_DXCREATESYNCHRONIZATIONOBJECT \
@@ -908,6 +933,8 @@ struct d3dkmt_enumadapters3 {
 	_IOWR(0x47, 0x18, struct d3dkmt_createhwqueue)
 #define LX_DXDESTROYHWQUEUE		\
 	_IOWR(0x47, 0x1b, struct d3dkmt_destroyhwqueue)
+#define LX_DXDESTROYPAGINGQUEUE		\
+	_IOWR(0x47, 0x1c, struct d3dddi_destroypagingqueue)
 #define LX_DXDESTROYDEVICE		\
 	_IOWR(0x47, 0x19, struct d3dkmt_destroydevice)
 #define LX_DXDESTROYSYNCHRONIZATIONOBJECT \
#14 lore raw

PATCH v4 14/55 drivers: hv: dxgkrnl: Submit execution commands to the compute device

In reply to Eric Curtin

From: Iouri Tarassov <[email protected]>

Implements ioctls for submission of compute device buffers for execution:
- LX_DXSUBMITCOMMAND
The ioctl is used to submit a command buffer to the device, working in the "packet scheduling" mode.

- LX_DXSUBMITCOMMANDTOHWQUEUE
The ioctl is used to submit a command buffer to the device, working in the "hardware scheduling" mode.

To improve performance both ioctls use asynchronous VM bus messages to communicate with the host as these are high frequency operations.

[kms: forward port to 6.6 from 6.1. No code changes made.]
---
 drivers/hv/dxgkrnl/dxgkrnl.h  |   6 ++
 drivers/hv/dxgkrnl/dxgvmbus.c | 113 ++++++++++++++++++++++++++++++
 drivers/hv/dxgkrnl/dxgvmbus.h |  14 ++++
 drivers/hv/dxgkrnl/ioctl.c    | 127 +++++++++++++++++++++++++++++++++-
 include/uapi/misc/d3dkmthk.h  |  58 ++++++++++++++++
 5 files changed, 316 insertions(+), 2 deletions(-)
drivers/hv/dxgkrnl/dxgkrnl.h +6 -0
index 440d1f9b8882..ab97bc53b124 100644
@@ -796,6 +796,9 @@ int dxgvmb_send_create_allocation(struct dxgprocess *pr, struct dxgdevice *dev,
 int dxgvmb_send_destroy_allocation(struct dxgprocess *pr, struct dxgdevice *dev,
 				   struct d3dkmt_destroyallocation2 *args,
 				   struct d3dkmthandle *alloc_handles);
+int dxgvmb_send_submit_command(struct dxgprocess *pr,
+			       struct dxgadapter *adapter,
+			       struct d3dkmt_submitcommand *args);
 int dxgvmb_send_create_sync_object(struct dxgprocess *pr,
 				   struct dxgadapter *adapter,
 				   struct d3dkmt_createsynchronizationobject2
@@ -838,6 +841,9 @@ int dxgvmb_send_destroy_hwqueue(struct dxgprocess *process,
 int dxgvmb_send_query_adapter_info(struct dxgprocess *process,
 				   struct dxgadapter *adapter,
 				   struct d3dkmt_queryadapterinfo *args);
+int dxgvmb_send_submit_command_hwqueue(struct dxgprocess *process,
+				       struct dxgadapter *adapter,
+				       struct d3dkmt_submitcommandtohwqueue *a);
 int dxgvmb_send_open_sync_object_nt(struct dxgprocess *process,
 				    struct dxgvmbuschannel *channel,
 				    struct d3dkmt_opensyncobjectfromnthandle2
drivers/hv/dxgkrnl/dxgvmbus.c +113 -0

122 lines not on this page · raw message

drivers/hv/dxgkrnl/dxgvmbus.h +14 -0
index aba075d374c9..acfdbde09e82 100644
@@ -314,6 +314,20 @@ struct dxgkvmb_command_flushdevice {
 	enum dxgdevice_flushschedulerreason	reason;
 };
 
+struct dxgkvmb_command_submitcommand {
+	struct dxgkvmb_command_vgpu_to_host hdr;
+	struct d3dkmt_submitcommand	args;
+	/* HistoryBufferHandles */
+	/* PrivateDriverData    */
+};
+
+struct dxgkvmb_command_submitcommandtohwqueue {
+	struct dxgkvmb_command_vgpu_to_host hdr;
+	struct d3dkmt_submitcommandtohwqueue args;
+	/* Written primaries */
+	/* PrivateDriverData */
+};
+
 struct dxgkvmb_command_createallocation_allocinfo {
 	u32				flags;
 	u32				priv_drv_data_size;
drivers/hv/dxgkrnl/ioctl.c +125 -2

145 lines not on this page · raw message

include/uapi/misc/d3dkmthk.h +58 -0

82 lines not on this page · raw message

#15 lore raw

PATCH v4 15/55 drivers: hv: dxgkrnl: Share objects with the host

In reply to Eric Curtin

From: Iouri Tarassov <[email protected]>

Implement the LX_DXSHAREOBJECTWITHHOST ioctl.
This ioctl is used to create a Windows NT handle on the host for the given shared object (resource or sync object). The NT handle is returned to the caller. The caller could share the NT handle with a host application, which needs to access the object. The host application can open the shared resource using the NT handle. This way the guest and the host have access to the same object.

Fix incorrect handling of error results from copy_from_user().

[kms: forward port to 6.6 from 6.1. No code changes made.]
---
 drivers/hv/dxgkrnl/dxgkrnl.h  |  2 ++
 drivers/hv/dxgkrnl/dxgvmbus.c | 60 ++++++++++++++++++++++++++++++++---
 drivers/hv/dxgkrnl/dxgvmbus.h | 18 +++++++++++
 drivers/hv/dxgkrnl/ioctl.c    | 38 ++++++++++++++++++++--
 include/uapi/misc/d3dkmthk.h  |  9 ++++++
 5 files changed, 120 insertions(+), 7 deletions(-)
drivers/hv/dxgkrnl/dxgkrnl.h +2 -0
index ab97bc53b124..a39d11d76e41 100644
@@ -872,6 +872,8 @@ int dxgvmb_send_get_stdalloc_data(struct dxgdevice *device,
 int dxgvmb_send_async_msg(struct dxgvmbuschannel *channel,
 			  void *command,
 			  u32 cmd_size);
+int dxgvmb_send_share_object_with_host(struct dxgprocess *process,
+				struct d3dkmt_shareobjectwithhost *args);
 
 void signal_host_cpu_event(struct dxghostevent *eventhdr);
 int ntstatus2int(struct ntstatus status);
drivers/hv/dxgkrnl/dxgvmbus.c +55 -5

98 lines not on this page · raw message

drivers/hv/dxgkrnl/dxgvmbus.h +18 -0
index acfdbde09e82..c1f693917d99 100644
@@ -574,4 +574,22 @@ struct dxgkvmb_command_destroyhwqueue {
 	struct d3dkmthandle		hwqueue;
 };
 
+struct dxgkvmb_command_shareobjectwithhost {
+	struct dxgkvmb_command_vm_to_host hdr;
+	struct d3dkmthandle	device_handle;
+	struct d3dkmthandle	object_handle;
+	u64			reserved;
+};
+
+struct dxgkvmb_command_shareobjectwithhost_return {
+	struct ntstatus	status;
+	u32		alignment;
+	u64		vail_nt_handle;
+};
+
+int
+dxgvmb_send_sync_msg(struct dxgvmbuschannel *channel,
+		     void *command, u32 command_size, void *result,
+		     u32 result_size);
+
 #endif /* _DXGVMBUS_H */
drivers/hv/dxgkrnl/ioctl.c +36 -2
index 9128694c8e78..ac052836ce27 100644
@@ -2460,6 +2460,7 @@ dxgkio_open_sync_object_nt(struct dxgprocess *process, void *__user inargs)
 	if (ret == 0)
 		goto success;
 	DXG_ERR("failed to copy output args");
+	ret = -EINVAL;
 
 cleanup:
 
@@ -3364,8 +3365,10 @@ dxgkio_share_objects(struct dxgprocess *process, void *__user inargs)
 	tmp = (u64) object_fd;
 
 	ret = copy_to_user(args.shared_handle, &tmp, sizeof(u64));
-	if (ret < 0)
+	if (ret) {
 		DXG_ERR("failed to copy shared handle");
+		ret = -EINVAL;
+	}
 
 cleanup:
 	if (ret < 0) {
@@ -3773,6 +3776,37 @@ dxgkio_open_resource_nt(struct dxgprocess *process,
 	return ret;
 }
 
+static int
+dxgkio_share_object_with_host(struct dxgprocess *process, void *__user inargs)
+{
+	struct d3dkmt_shareobjectwithhost args;
+	int ret;
+
+	ret = copy_from_user(&args, inargs, sizeof(args));
+	if (ret) {
+		DXG_ERR("failed to copy input args");
+		ret = -EINVAL;
+		goto cleanup;
+	}
+
+	ret = dxgvmb_send_share_object_with_host(process, &args);
+	if (ret) {
+		DXG_ERR("dxgvmb_send_share_object_with_host dailed");
+		goto cleanup;
+	}
+
+	ret = copy_to_user(inargs, &args, sizeof(args));
+	if (ret) {
+		DXG_ERR("failed to copy data to user");
+		ret = -EINVAL;
+	}
+
+cleanup:
+
+	DXG_TRACE("ioctl:%s %d", errorstr(ret), ret);
+	return ret;
+}
+
 static struct ioctl_desc ioctls[] = {
 /* 0x00 */	{},
 /* 0x01 */	{dxgkio_open_adapter_from_luid, LX_DXOPENADAPTERFROMLUID},
@@ -3850,7 +3884,7 @@ static struct ioctl_desc ioctls[] = {
 		 LX_DXQUERYRESOURCEINFOFROMNTHANDLE},
 /* 0x42 */	{dxgkio_open_resource_nt, LX_DXOPENRESOURCEFROMNTHANDLE},
 /* 0x43 */	{},
-/* 0x44 */	{},
+/* 0x44 */	{dxgkio_share_object_with_host, LX_DXSHAREOBJECTWITHHOST},
 /* 0x45 */	{},
 };
 
include/uapi/misc/d3dkmthk.h +9 -0
index 9238115d165d..895861505e6e 100644
@@ -952,6 +952,13 @@ struct d3dkmt_enumadapters3 {
 #endif
 };
 
+struct d3dkmt_shareobjectwithhost {
+	struct d3dkmthandle	device_handle;
+	struct d3dkmthandle	object_handle;
+	__u64			reserved;
+	__u64			object_vail_nt_handle;
+};
+
 /*
  * Dxgkrnl Graphics Port Driver ioctl definitions
  *
@@ -1021,5 +1028,7 @@ struct d3dkmt_enumadapters3 {
 	_IOWR(0x47, 0x41, struct d3dkmt_queryresourceinfofromnthandle)
 #define LX_DXOPENRESOURCEFROMNTHANDLE	\
 	_IOWR(0x47, 0x42, struct d3dkmt_openresourcefromnthandle)
+#define LX_DXSHAREOBJECTWITHHOST	\
+	_IOWR(0x47, 0x44, struct d3dkmt_shareobjectwithhost)
 
 #endif /* _D3DKMTHK_H */
#16 lore raw

PATCH v4 16/55 drivers: hv: dxgkrnl: Query the dxgdevice state

In reply to Eric Curtin

From: Iouri Tarassov <[email protected]>

Implement the ioctl to query the dxgdevice state - LX_DXGETDEVICESTATE. The IOCTL is used to query the state of the given dxgdevice object (active, error, etc.).

A call to the dxgdevice execution state could be high frequency. The following method is used to avoid sending a synchronous VM bus message to the host for every call:
- When a dxgdevice is created, a pointer to dxgglobal->device_state_counter is sent to the host
- Every time the device state on the host is changed, the host will send an asynchronous message to the guest (DXGK_VMBCOMMAND_SETGUESTDATA) and the guest will increment the device_state_counter value.
- the dxgdevice object has execution_state_counter member, which is equal to dxgglobal->device_state_counter value at the time when LX_DXGETDEVICESTATE was last processed..
- if execution_state_counter is different from device_state_counter, the dxgk_vmbcommand_getdevicestate VM bus message is sent to the host. Otherwise, the cached value is returned to the caller.

[kms: forward port to 6.6 from 6.1. No code changes made.]
---
 drivers/hv/dxgkrnl/dxgkrnl.h   |  11 ++++
 drivers/hv/dxgkrnl/dxgmodule.c |   1 -
 drivers/hv/dxgkrnl/dxgvmbus.c  |  68 ++++++++++++++++++++++
 drivers/hv/dxgkrnl/dxgvmbus.h  |  26 +++++++++
 drivers/hv/dxgkrnl/ioctl.c     |  66 ++++++++++++++++++++-
 include/uapi/misc/d3dkmthk.h   | 101 +++++++++++++++++++++++++++++----
 6 files changed, 261 insertions(+), 12 deletions(-)
drivers/hv/dxgkrnl/dxgkrnl.h +11 -0
index a39d11d76e41..b131c3b43838 100644
@@ -268,12 +268,18 @@ void dxgsyncobject_destroy(struct dxgprocess *process,
 void dxgsyncobject_stop(struct dxgsyncobject *syncobj);
 void dxgsyncobject_release(struct kref *refcount);
 
+/*
+ * device_state_counter - incremented every time the execition state of
+ *	a DXGDEVICE is changed in the host. Used to optimize access to the
+ *	device execution state.
+ */
 struct dxgglobal {
 	struct dxgdriver	*drvdata;
 	struct dxgvmbuschannel	channel;
 	struct hv_device	*hdev;
 	u32			num_adapters;
 	u32			vmbus_ver;	/* Interface version */
+	atomic_t		device_state_counter;
 	struct resource		*mem;
 	u64			mmiospace_base;
 	u64			mmiospace_size;
@@ -512,6 +518,7 @@ struct dxgdevice {
 	struct list_head	syncobj_list_head;
 	struct d3dkmthandle	handle;
 	enum d3dkmt_deviceexecution_state execution_state;
+	int			execution_state_counter;
 	u32			handle_valid;
 };
 
@@ -849,6 +856,10 @@ int dxgvmb_send_open_sync_object_nt(struct dxgprocess *process,
 				    struct d3dkmt_opensyncobjectfromnthandle2
 				    *args,
 				    struct dxgsyncobject *syncobj);
+int dxgvmb_send_get_device_state(struct dxgprocess *process,
+				 struct dxgadapter *adapter,
+				 struct d3dkmt_getdevicestate *args,
+				 struct d3dkmt_getdevicestate *__user inargs);
 int dxgvmb_send_create_nt_shared_object(struct dxgprocess *process,
 					struct d3dkmthandle object,
 					struct d3dkmthandle *shared_handle);
drivers/hv/dxgkrnl/dxgmodule.c +0 -1
index 8cbe1095599f..5c364a46b65f 100644
@@ -827,7 +827,6 @@ static struct dxgglobal *dxgglobal_create(void)
 #ifdef DEBUG
 	dxgk_validate_ioctls();
 #endif
-
 	return dxgglobal;
 }
 
drivers/hv/dxgkrnl/dxgvmbus.c +68 -0

98 lines not on this page · raw message

drivers/hv/dxgkrnl/dxgvmbus.h +26 -0
index c1f693917d99..6ca1068b0d4c 100644
@@ -172,6 +172,22 @@ struct dxgkvmb_command_signalguestevent {
 	bool				dereference_event;
 };
 
+enum set_guestdata_type {
+	SETGUESTDATA_DATATYPE_DWORD	= 0,
+	SETGUESTDATA_DATATYPE_UINT64	= 1
+};
+
+struct dxgkvmb_command_setguestdata {
+	struct dxgkvmb_command_host_to_vm hdr;
+	void *guest_pointer;
+	union {
+		u64	data64;
+		u32	data32;
+	};
+	u32	dereference	: 1;
+	u32	data_type	: 4;
+};
+
 struct dxgkvmb_command_opensyncobject {
 	struct dxgkvmb_command_vm_to_host hdr;
 	struct d3dkmthandle		device;
@@ -574,6 +590,16 @@ struct dxgkvmb_command_destroyhwqueue {
 	struct d3dkmthandle		hwqueue;
 };
 
+struct dxgkvmb_command_getdevicestate {
+	struct dxgkvmb_command_vgpu_to_host hdr;
+	struct d3dkmt_getdevicestate	args;
+};
+
+struct dxgkvmb_command_getdevicestate_return {
+	struct d3dkmt_getdevicestate	args;
+	struct ntstatus			status;
+};
+
 struct dxgkvmb_command_shareobjectwithhost {
 	struct dxgkvmb_command_vm_to_host hdr;
 	struct d3dkmthandle	device_handle;
drivers/hv/dxgkrnl/ioctl.c +65 -1

78 lines not on this page · raw message

include/uapi/misc/d3dkmthk.h +91 -10

119 lines not on this page · raw message

#17 lore raw

PATCH v4 17/55 drivers: hv: dxgkrnl: Map(unmap) CPU address to device allocation

In reply to Eric Curtin

From: Iouri Tarassov <[email protected]>

Implement ioctls to map/unmap CPU virtual addresses to compute device allocations - LX_DXLOCK2 and LX_DXUNLOCK2.

The LX_DXLOCK2 ioctl maps a CPU virtual address to a compute device allocation. The allocation could be located in system memory or local device memory on the host. When the device allocation is created from the guest system memory (existing sysmem allocation), the allocation CPU address is known and is returned to the caller. For other CPU visible allocations the code flow is the following:
1. A VM bus message is sent to the host to map the allocation
2. The host allocates a portion of the guest IO space and maps it to the allocation backing store. The IO space address of the allocation is returned back to the guest.
3. The guest allocates a CPU virtual address and maps it to the IO space (see the dxg_map_iospace function).
4. The CPU VA is returned back to the caller cpu_address_mapped and cpu_address_refcount are used to track how many times an allocation was mapped.

The LX_DXUNLOCK2 ioctl unmaps a CPU virtual address from a compute device allocation.

[kms: forward port to 6.6 from 6.1. No code changes made.]
---
 drivers/hv/dxgkrnl/dxgadapter.c |  11 +++
 drivers/hv/dxgkrnl/dxgkrnl.h    |  14 +++
 drivers/hv/dxgkrnl/dxgvmbus.c   | 107 +++++++++++++++++++++
 drivers/hv/dxgkrnl/dxgvmbus.h   |  19 ++++
 drivers/hv/dxgkrnl/ioctl.c      | 160 +++++++++++++++++++++++++++++++-
 include/uapi/misc/d3dkmthk.h    |  30 ++++++
 6 files changed, 339 insertions(+), 2 deletions(-)
drivers/hv/dxgkrnl/dxgadapter.c +11 -0
index 410f08768bad..23f00db7637e 100644
@@ -885,6 +885,15 @@ void dxgallocation_stop(struct dxgallocation *alloc)
 		vfree(alloc->pages);
 		alloc->pages = NULL;
 	}
+	dxgprocess_ht_lock_exclusive_down(alloc->process);
+	if (alloc->cpu_address_mapped) {
+		dxg_unmap_iospace(alloc->cpu_address,
+				  alloc->num_pages << PAGE_SHIFT);
+		alloc->cpu_address_mapped = false;
+		alloc->cpu_address = NULL;
+		alloc->cpu_address_refcount = 0;
+	}
+	dxgprocess_ht_lock_exclusive_up(alloc->process);
 }
 
 void dxgallocation_free_handle(struct dxgallocation *alloc)
@@ -932,6 +941,8 @@ else
 #endif
 	if (alloc->priv_drv_data)
 		vfree(alloc->priv_drv_data);
+	if (alloc->cpu_address_mapped)
+		pr_err("Alloc IO space is mapped: %p", alloc);
 	kfree(alloc);
 }
 
drivers/hv/dxgkrnl/dxgkrnl.h +14 -0
index b131c3b43838..1d6b552f1c1a 100644
@@ -708,6 +708,8 @@ struct dxgallocation {
 	struct d3dkmthandle		alloc_handle;
 	/* Set to 1 when allocation belongs to resource. */
 	u32				resource_owner:1;
+	/* Set to 1 when 'cpu_address' is mapped to the IO space. */
+	u32				cpu_address_mapped:1;
 	/* Set to 1 when the allocatio is mapped as cached */
 	u32				cached:1;
 	u32				handle_valid:1;
@@ -719,6 +721,11 @@ struct dxgallocation {
 #endif
 	/* Number of pages in the 'pages' array */
 	u32				num_pages;
+	/*
+	 * How many times dxgk_lock2 is called to allocation, which is mapped
+	 * to IO space.
+	 */
+	u32				cpu_address_refcount;
 	/*
 	 * CPU address from the existing sysmem allocation, or
 	 * mapped to the CPU visible backing store in the IO space
@@ -837,6 +844,13 @@ int dxgvmb_send_wait_sync_object_cpu(struct dxgprocess *process,
 				     d3dkmt_waitforsynchronizationobjectfromcpu
 				     *args,
 				     u64 cpu_event);
+int dxgvmb_send_lock2(struct dxgprocess *process,
+		      struct dxgadapter *adapter,
+		      struct d3dkmt_lock2 *args,
+		      struct d3dkmt_lock2 *__user outargs);
+int dxgvmb_send_unlock2(struct dxgprocess *process,
+			struct dxgadapter *adapter,
+			struct d3dkmt_unlock2 *args);
 int dxgvmb_send_create_hwqueue(struct dxgprocess *process,
 			       struct dxgadapter *adapter,
 			       struct d3dkmt_createhwqueue *args,
drivers/hv/dxgkrnl/dxgvmbus.c +107 -0

113 lines not on this page · raw message

drivers/hv/dxgkrnl/dxgvmbus.h +19 -0
index 6ca1068b0d4c..447bb1ba391b 100644
@@ -570,6 +570,25 @@ struct dxgkvmb_command_waitforsyncobjectfromgpu {
 	/* struct d3dkmthandle ObjectHandles[object_count] */
 };
 
+struct dxgkvmb_command_lock2 {
+	struct dxgkvmb_command_vgpu_to_host hdr;
+	struct d3dkmt_lock2		args;
+	bool				use_legacy_lock;
+	u32				flags;
+	u32				priv_drv_data;
+};
+
+struct dxgkvmb_command_lock2_return {
+	struct ntstatus			status;
+	void				*cpu_visible_buffer_offset;
+};
+
+struct dxgkvmb_command_unlock2 {
+	struct dxgkvmb_command_vgpu_to_host hdr;
+	struct d3dkmt_unlock2		args;
+	bool				use_legacy_unlock;
+};
+
 /* Returns the same structure */
 struct dxgkvmb_command_createhwqueue {
 	struct dxgkvmb_command_vgpu_to_host hdr;
drivers/hv/dxgkrnl/ioctl.c +158 -2

178 lines not on this page · raw message

include/uapi/misc/d3dkmthk.h +30 -0
index 8a013b07e88a..b498f09e694d 100644
@@ -668,6 +668,32 @@ struct d3dkmt_submitcommandtohwqueue {
 #endif
 };
 
+struct d3dddicb_lock2flags {
+	union {
+		struct {
+			__u32	reserved:32;
+		};
+		__u32		value;
+	};
+};
+
+struct d3dkmt_lock2 {
+	struct d3dkmthandle		device;
+	struct d3dkmthandle		allocation;
+	struct d3dddicb_lock2flags	flags;
+	__u32				reserved;
+#ifdef __KERNEL__
+	void				*data;
+#else
+	__u64				data;
+#endif
+};
+
+struct d3dkmt_unlock2 {
+	struct d3dkmthandle			device;
+	struct d3dkmthandle			allocation;
+};
+
 enum d3dkmt_standardallocationtype {
 	_D3DKMT_STANDARDALLOCATIONTYPE_EXISTINGHEAP	= 1,
 	_D3DKMT_STANDARDALLOCATIONTYPE_CROSSADAPTER	= 2,
@@ -1083,6 +1109,8 @@ struct d3dkmt_shareobjectwithhost {
 	_IOWR(0x47, 0x19, struct d3dkmt_destroydevice)
 #define LX_DXDESTROYSYNCHRONIZATIONOBJECT \
 	_IOWR(0x47, 0x1d, struct d3dkmt_destroysynchronizationobject)
+#define LX_DXLOCK2			\
+	_IOWR(0x47, 0x25, struct d3dkmt_lock2)
 #define LX_DXSIGNALSYNCHRONIZATIONOBJECTFROMCPU \
 	_IOWR(0x47, 0x31, struct d3dkmt_signalsynchronizationobjectfromcpu)
 #define LX_DXSIGNALSYNCHRONIZATIONOBJECTFROMGPU \
@@ -1095,6 +1123,8 @@ struct d3dkmt_shareobjectwithhost {
 	_IOWR(0x47, 0x35, struct d3dkmt_submitsignalsyncobjectstohwqueue)
 #define LX_DXSUBMITWAITFORSYNCOBJECTSTOHWQUEUE \
 	_IOWR(0x47, 0x36, struct d3dkmt_submitwaitforsyncobjectstohwqueue)
+#define LX_DXUNLOCK2			\
+	_IOWR(0x47, 0x37, struct d3dkmt_unlock2)
 #define LX_DXWAITFORSYNCHRONIZATIONOBJECTFROMCPU \
 	_IOWR(0x47, 0x3a, struct d3dkmt_waitforsynchronizationobjectfromcpu)
 #define LX_DXWAITFORSYNCHRONIZATIONOBJECTFROMGPU \
#18 lore raw

PATCH v4 18/55 drivers: hv: dxgkrnl: Manage device allocation properties

In reply to Eric Curtin

From: Iouri Tarassov <[email protected]>

Implement ioctls to manage properties of a compute device allocation:
- LX_DXUPDATEALLOCPROPERTY,
- LX_DXSETALLOCATIONPRIORITY,
- LX_DXGETALLOCATIONPRIORITY,
- LX_DXQUERYALLOCATIONRESIDENCY.
- LX_DXCHANGEVIDEOMEMORYRESERVATION,

The LX_DXUPDATEALLOCPROPERTY ioctl requests the host to update various properties of a compute devoce allocation.

The LX_DXSETALLOCATIONPRIORITY and LX_DXGETALLOCATIONPRIORITY ioctls are used to set/get allocation priority, which defines the importance of the allocation to be in the local device memory.

The LX_DXQUERYALLOCATIONRESIDENCY ioctl queries if the allocation is located in the compute device accessible memory.

The LX_DXCHANGEVIDEOMEMORYRESERVATION ioctl changes compute device memory reservation of an allocation.

[kms: forward port to 6.6 from 6.1. No code changes made.]
---
 drivers/hv/dxgkrnl/dxgkrnl.h  |  21 +++
 drivers/hv/dxgkrnl/dxgvmbus.c | 300 ++++++++++++++++++++++++++++++++++
 drivers/hv/dxgkrnl/dxgvmbus.h |  50 ++++++
 drivers/hv/dxgkrnl/ioctl.c    | 217 +++++++++++++++++++++++-
 include/uapi/misc/d3dkmthk.h  | 127 ++++++++++++++
 5 files changed, 708 insertions(+), 7 deletions(-)
drivers/hv/dxgkrnl/dxgkrnl.h +21 -0
index 1d6b552f1c1a..7fefe4617488 100644
@@ -851,6 +851,23 @@ int dxgvmb_send_lock2(struct dxgprocess *process,
 int dxgvmb_send_unlock2(struct dxgprocess *process,
 			struct dxgadapter *adapter,
 			struct d3dkmt_unlock2 *args);
+int dxgvmb_send_update_alloc_property(struct dxgprocess *process,
+				      struct dxgadapter *adapter,
+				      struct d3dddi_updateallocproperty *args,
+				      struct d3dddi_updateallocproperty *__user
+				      inargs);
+int dxgvmb_send_set_allocation_priority(struct dxgprocess *process,
+					struct dxgadapter *adapter,
+					struct d3dkmt_setallocationpriority *a);
+int dxgvmb_send_get_allocation_priority(struct dxgprocess *process,
+					struct dxgadapter *adapter,
+					struct d3dkmt_getallocationpriority *a);
+int dxgvmb_send_change_vidmem_reservation(struct dxgprocess *process,
+					  struct dxgadapter *adapter,
+					  struct d3dkmthandle other_process,
+					  struct
+					  d3dkmt_changevideomemoryreservation
+					  *args);
 int dxgvmb_send_create_hwqueue(struct dxgprocess *process,
 			       struct dxgadapter *adapter,
 			       struct d3dkmt_createhwqueue *args,
@@ -870,6 +887,10 @@ int dxgvmb_send_open_sync_object_nt(struct dxgprocess *process,
 				    struct d3dkmt_opensyncobjectfromnthandle2
 				    *args,
 				    struct dxgsyncobject *syncobj);
+int dxgvmb_send_query_alloc_residency(struct dxgprocess *process,
+				      struct dxgadapter *adapter,
+				      struct d3dkmt_queryallocationresidency
+				      *args);
 int dxgvmb_send_get_device_state(struct dxgprocess *process,
 				 struct dxgadapter *adapter,
 				 struct d3dkmt_getdevicestate *args,
drivers/hv/dxgkrnl/dxgvmbus.c +300 -0

312 lines not on this page · raw message

drivers/hv/dxgkrnl/dxgvmbus.h +50 -0

68 lines not on this page · raw message

drivers/hv/dxgkrnl/ioctl.c +210 -7

255 lines not on this page · raw message

include/uapi/misc/d3dkmthk.h +127 -0

167 lines not on this page · raw message

#19 lore raw

PATCH v4 19/55 drivers: hv: dxgkrnl: Flush heap transitions

In reply to Eric Curtin

From: Iouri Tarassov <[email protected]>

Implement the ioctl to flush heap transitions
(LX_DXFLUSHHEAPTRANSITIONS).

The ioctl is used to ensure that the video memory manager on the host flushes all internal operations.

[kms: forward port to 6.6 from 6.1. No code changes made.]
---
 drivers/hv/dxgkrnl/dxgadapter.c |  2 +-
 drivers/hv/dxgkrnl/dxgkrnl.h    |  3 ++
 drivers/hv/dxgkrnl/dxgvmbus.c   | 23 ++++++++++++++++
 drivers/hv/dxgkrnl/dxgvmbus.h   |  5 ++++
 drivers/hv/dxgkrnl/ioctl.c      | 49 ++++++++++++++++++++++++++++++++-
 include/uapi/misc/d3dkmthk.h    |  6 ++++
 6 files changed, 86 insertions(+), 2 deletions(-)
drivers/hv/dxgkrnl/dxgadapter.c +1 -1
index 23f00db7637e..6f763e326a65 100644
@@ -942,7 +942,7 @@ else
 	if (alloc->priv_drv_data)
 		vfree(alloc->priv_drv_data);
 	if (alloc->cpu_address_mapped)
-		pr_err("Alloc IO space is mapped: %p", alloc);
+		DXG_ERR("Alloc IO space is mapped: %p", alloc);
 	kfree(alloc);
 }
 
drivers/hv/dxgkrnl/dxgkrnl.h +3 -0
index 7fefe4617488..ced9dd294f5f 100644
@@ -882,6 +882,9 @@ int dxgvmb_send_query_adapter_info(struct dxgprocess *process,
 int dxgvmb_send_submit_command_hwqueue(struct dxgprocess *process,
 				       struct dxgadapter *adapter,
 				       struct d3dkmt_submitcommandtohwqueue *a);
+int dxgvmb_send_flush_heap_transitions(struct dxgprocess *process,
+				       struct dxgadapter *adapter,
+				       struct d3dkmt_flushheaptransitions *arg);
 int dxgvmb_send_open_sync_object_nt(struct dxgprocess *process,
 				    struct dxgvmbuschannel *channel,
 				    struct d3dkmt_opensyncobjectfromnthandle2
drivers/hv/dxgkrnl/dxgvmbus.c +23 -0
index dd2c97fee27b..928fad5f133b 100644
@@ -1829,6 +1829,29 @@ int dxgvmb_send_destroy_allocation(struct dxgprocess *process,
 	return ret;
 }
 
+int dxgvmb_send_flush_heap_transitions(struct dxgprocess *process,
+				       struct dxgadapter *adapter,
+				       struct d3dkmt_flushheaptransitions *args)
+{
+	struct dxgkvmb_command_flushheaptransitions *command;
+	int ret;
+	struct dxgvmbusmsg msg = {.hdr = NULL};
+
+	ret = init_message(&msg, adapter, process, sizeof(*command));
+	if (ret)
+		goto cleanup;
+	command = (void *)msg.msg;
+	command_vgpu_to_host_init2(&command->hdr,
+				   DXGK_VMBCOMMAND_FLUSHHEAPTRANSITIONS,
+				   process->host_handle);
+	ret = dxgvmb_send_sync_msg_ntstatus(msg.channel, msg.hdr, msg.size);
+cleanup:
+	free_message(&msg, process);
+	if (ret)
+		DXG_TRACE("err: %d", ret);
+	return ret;
+}
+
 int dxgvmb_send_query_alloc_residency(struct dxgprocess *process,
 				      struct dxgadapter *adapter,
 				      struct d3dkmt_queryallocationresidency
drivers/hv/dxgkrnl/dxgvmbus.h +5 -0
index dbb01b9ab066..d232eb234e2c 100644
@@ -367,6 +367,11 @@ struct dxgkvmb_command_submitcommandtohwqueue {
 	/* PrivateDriverData */
 };
 
+/* Returns  ntstatus */
+struct dxgkvmb_command_flushheaptransitions {
+	struct dxgkvmb_command_vgpu_to_host hdr;
+};
+
 struct dxgkvmb_command_createallocation_allocinfo {
 	u32				flags;
 	u32				priv_drv_data_size;
drivers/hv/dxgkrnl/ioctl.c +48 -1

61 lines not on this page · raw message

include/uapi/misc/d3dkmthk.h +6 -0
index af381101fd90..873feb951129 100644
@@ -936,6 +936,10 @@ struct d3dkmt_queryadapterinfo {
 	__u32				private_data_size;
 };
 
+struct d3dkmt_flushheaptransitions {
+	struct d3dkmthandle	adapter;
+};
+
 struct d3dddi_openallocationinfo2 {
 	struct d3dkmthandle	allocation;
 #ifdef __KERNEL__
@@ -1228,6 +1232,8 @@ struct d3dkmt_shareobjectwithhost {
 	_IOWR(0x47, 0x19, struct d3dkmt_destroydevice)
 #define LX_DXDESTROYSYNCHRONIZATIONOBJECT \
 	_IOWR(0x47, 0x1d, struct d3dkmt_destroysynchronizationobject)
+#define LX_DXFLUSHHEAPTRANSITIONS	\
+	_IOWR(0x47, 0x1f, struct d3dkmt_flushheaptransitions)
 #define LX_DXLOCK2			\
 	_IOWR(0x47, 0x25, struct d3dkmt_lock2)
 #define LX_DXQUERYALLOCATIONRESIDENCY	\
#20 lore raw

PATCH v4 20/55 drivers: hv: dxgkrnl: Query video memory information

In reply to Eric Curtin

From: Iouri Tarassov <[email protected]>

Implement the ioctl to query video memory information from the host (LX_DXQUERYVIDEOMEMORYINFO).

[kms: forward port to 6.6 from 6.1. No code changes made.]
---
 drivers/hv/dxgkrnl/dxgkrnl.h  |  5 +++
 drivers/hv/dxgkrnl/dxgvmbus.c | 64 +++++++++++++++++++++++++++++++++++
 drivers/hv/dxgkrnl/dxgvmbus.h | 14 ++++++++
 drivers/hv/dxgkrnl/ioctl.c    | 50 ++++++++++++++++++++++++++-
 include/uapi/misc/d3dkmthk.h  | 13 +++++++
 5 files changed, 145 insertions(+), 1 deletion(-)
drivers/hv/dxgkrnl/dxgkrnl.h +5 -0
index ced9dd294f5f..b6a7288a4177 100644
@@ -894,6 +894,11 @@ int dxgvmb_send_query_alloc_residency(struct dxgprocess *process,
 				      struct dxgadapter *adapter,
 				      struct d3dkmt_queryallocationresidency
 				      *args);
+int dxgvmb_send_query_vidmem_info(struct dxgprocess *process,
+				  struct dxgadapter *adapter,
+				  struct d3dkmt_queryvideomemoryinfo *args,
+				  struct d3dkmt_queryvideomemoryinfo
+				  *__user iargs);
 int dxgvmb_send_get_device_state(struct dxgprocess *process,
 				 struct dxgadapter *adapter,
 				 struct d3dkmt_getdevicestate *args,
drivers/hv/dxgkrnl/dxgvmbus.c +64 -0

70 lines not on this page · raw message

drivers/hv/dxgkrnl/dxgvmbus.h +14 -0
index d232eb234e2c..a1549983d50f 100644
@@ -664,6 +664,20 @@ struct dxgkvmb_command_queryallocationresidency_return {
 	/* d3dkmt_allocationresidencystatus[NumAllocations] */
 };
 
+struct dxgkvmb_command_queryvideomemoryinfo {
+	struct dxgkvmb_command_vgpu_to_host hdr;
+	struct d3dkmthandle		adapter;
+	enum d3dkmt_memory_segment_group memory_segment_group;
+	u32				physical_adapter_index;
+};
+
+struct dxgkvmb_command_queryvideomemoryinfo_return {
+	u64			budget;
+	u64			current_usage;
+	u64			current_reservation;
+	u64			available_for_reservation;
+};
+
 struct dxgkvmb_command_getdevicestate {
 	struct dxgkvmb_command_vgpu_to_host hdr;
 	struct d3dkmt_getdevicestate	args;
drivers/hv/dxgkrnl/ioctl.c +49 -1

62 lines not on this page · raw message

include/uapi/misc/d3dkmthk.h +13 -0
index 873feb951129..b7d8b1d91cfc 100644
@@ -897,6 +897,17 @@ enum d3dkmt_memory_segment_group {
 	_D3DKMT_MEMORY_SEGMENT_GROUP_NON_LOCAL	= 1
 };
 
+struct d3dkmt_queryvideomemoryinfo {
+	__u64					process;
+	struct d3dkmthandle			adapter;
+	enum d3dkmt_memory_segment_group	memory_segment_group;
+	__u64					budget;
+	__u64					current_usage;
+	__u64					current_reservation;
+	__u64					available_for_reservation;
+	__u32					physical_adapter_index;
+};
+
 struct d3dkmt_adaptertype {
 	union {
 		struct {
@@ -1204,6 +1215,8 @@ struct d3dkmt_shareobjectwithhost {
 	_IOWR(0x47, 0x07, struct d3dkmt_createpagingqueue)
 #define LX_DXQUERYADAPTERINFO		\
 	_IOWR(0x47, 0x09, struct d3dkmt_queryadapterinfo)
+#define LX_DXQUERYVIDEOMEMORYINFO	\
+	_IOWR(0x47, 0x0a, struct d3dkmt_queryvideomemoryinfo)
 #define LX_DXGETDEVICESTATE		\
 	_IOWR(0x47, 0x0e, struct d3dkmt_getdevicestate)
 #define LX_DXSUBMITCOMMAND		\
#21 lore raw

PATCH v4 21/55 drivers: hv: dxgkrnl: The escape ioctl

In reply to Eric Curtin

From: Iouri Tarassov <[email protected]>

Implement the escape ioctl (LX_DXESCAPE).

This ioctl is used to send/receive private data between user mode compute device driver (guest) and kernel mode compute device driver (host). It allows the user mode driver to extend the virtual compute device API.

[kms: forward port to 6.6 from 6.1. No code changes made.]
---
 drivers/hv/dxgkrnl/dxgkrnl.h  |  3 ++
 drivers/hv/dxgkrnl/dxgvmbus.c | 75 ++++++++++++++++++++++++++++++++---
 drivers/hv/dxgkrnl/dxgvmbus.h | 12 ++++++
 drivers/hv/dxgkrnl/ioctl.c    | 42 +++++++++++++++++++-
 include/uapi/misc/d3dkmthk.h  | 41 +++++++++++++++++++
 5 files changed, 167 insertions(+), 6 deletions(-)
drivers/hv/dxgkrnl/dxgkrnl.h +3 -0
index b6a7288a4177..dafc721ed6cf 100644
@@ -894,6 +894,9 @@ int dxgvmb_send_query_alloc_residency(struct dxgprocess *process,
 				      struct dxgadapter *adapter,
 				      struct d3dkmt_queryallocationresidency
 				      *args);
+int dxgvmb_send_escape(struct dxgprocess *process,
+		       struct dxgadapter *adapter,
+		       struct d3dkmt_escape *args);
 int dxgvmb_send_query_vidmem_info(struct dxgprocess *process,
 				  struct dxgadapter *adapter,
 				  struct d3dkmt_queryvideomemoryinfo *args,
drivers/hv/dxgkrnl/dxgvmbus.c +70 -5

114 lines not on this page · raw message

drivers/hv/dxgkrnl/dxgvmbus.h +12 -0
index a1549983d50f..e1c2ed7b1580 100644
@@ -664,6 +664,18 @@ struct dxgkvmb_command_queryallocationresidency_return {
 	/* d3dkmt_allocationresidencystatus[NumAllocations] */
 };
 
+/* Returns only private data */
+struct dxgkvmb_command_escape {
+	struct dxgkvmb_command_vgpu_to_host hdr;
+	struct d3dkmthandle		adapter;
+	struct d3dkmthandle		device;
+	enum d3dkmt_escapetype		type;
+	struct d3dddi_escapeflags	flags;
+	u32				priv_drv_data_size;
+	struct d3dkmthandle		context;
+	u8				priv_drv_data[1];
+};
+
 struct dxgkvmb_command_queryvideomemoryinfo {
 	struct dxgkvmb_command_vgpu_to_host hdr;
 	struct d3dkmthandle		adapter;
drivers/hv/dxgkrnl/ioctl.c +41 -1

54 lines not on this page · raw message

include/uapi/misc/d3dkmthk.h +41 -0

53 lines not on this page · raw message

#22 lore raw

PATCH v4 22/55 drivers: hv: dxgkrnl: Ioctl to put device to error state

In reply to Eric Curtin

From: Iouri Tarassov <[email protected]>

Implement the ioctl to put the virtual compute device to the error state (LX_DXMARKDEVICEASERROR).

This ioctl is used by the user mode driver when it detects an unrecoverable error condition.

When a compute device is put to the error state, all subsequent ioctl calls to the device will fail.

[kms: forward port to 6.6 from 6.1. No code changes made.]
---
 drivers/hv/dxgkrnl/dxgkrnl.h  |  3 +++
 drivers/hv/dxgkrnl/dxgvmbus.c | 25 +++++++++++++++++++++++
 drivers/hv/dxgkrnl/dxgvmbus.h |  5 +++++
 drivers/hv/dxgkrnl/ioctl.c    | 38 ++++++++++++++++++++++++++++++++++-
 include/uapi/misc/d3dkmthk.h  | 12 +++++++++++
 5 files changed, 82 insertions(+), 1 deletion(-)
drivers/hv/dxgkrnl/dxgkrnl.h +3 -0
index dafc721ed6cf..b454c7430f06 100644
@@ -856,6 +856,9 @@ int dxgvmb_send_update_alloc_property(struct dxgprocess *process,
 				      struct d3dddi_updateallocproperty *args,
 				      struct d3dddi_updateallocproperty *__user
 				      inargs);
+int dxgvmb_send_mark_device_as_error(struct dxgprocess *process,
+				     struct dxgadapter *adapter,
+				     struct d3dkmt_markdeviceaserror *args);
 int dxgvmb_send_set_allocation_priority(struct dxgprocess *process,
 					struct dxgadapter *adapter,
 					struct d3dkmt_setallocationpriority *a);
drivers/hv/dxgkrnl/dxgvmbus.c +25 -0
index 8bdd49bc7aa6..f7264b12a477 100644
@@ -2730,6 +2730,31 @@ int dxgvmb_send_update_alloc_property(struct dxgprocess *process,
 	return ret;
 }
 
+int dxgvmb_send_mark_device_as_error(struct dxgprocess *process,
+				     struct dxgadapter *adapter,
+				     struct d3dkmt_markdeviceaserror *args)
+{
+	struct dxgkvmb_command_markdeviceaserror *command;
+	int ret;
+	struct dxgvmbusmsg msg = {.hdr = NULL};
+
+	ret = init_message(&msg, adapter, process, sizeof(*command));
+	if (ret)
+		goto cleanup;
+	command = (void *)msg.msg;
+
+	command_vgpu_to_host_init2(&command->hdr,
+				   DXGK_VMBCOMMAND_MARKDEVICEASERROR,
+				   process->host_handle);
+	command->args = *args;
+	ret = dxgvmb_send_sync_msg_ntstatus(msg.channel, msg.hdr, msg.size);
+cleanup:
+	free_message(&msg, process);
+	if (ret)
+		DXG_TRACE("err: %d", ret);
+	return ret;
+}
+
 int dxgvmb_send_set_allocation_priority(struct dxgprocess *process,
 				struct dxgadapter *adapter,
 				struct d3dkmt_setallocationpriority *args)
drivers/hv/dxgkrnl/dxgvmbus.h +5 -0
index e1c2ed7b1580..a66e11097bb2 100644
@@ -627,6 +627,11 @@ struct dxgkvmb_command_updateallocationproperty_return {
 	struct ntstatus			status;
 };
 
+struct dxgkvmb_command_markdeviceaserror {
+	struct dxgkvmb_command_vgpu_to_host hdr;
+	struct d3dkmt_markdeviceaserror args;
+};
+
 /* Returns ntstatus */
 struct dxgkvmb_command_changevideomemoryreservation {
 	struct dxgkvmb_command_vgpu_to_host hdr;
drivers/hv/dxgkrnl/ioctl.c +37 -1
index 78de76abce2d..ce4af610ada7 100644
@@ -3341,6 +3341,42 @@ dxgkio_update_alloc_property(struct dxgprocess *process, void *__user inargs)
 	return ret;
 }
 
+static int
+dxgkio_mark_device_as_error(struct dxgprocess *process, void *__user inargs)
+{
+	struct d3dkmt_markdeviceaserror args;
+	struct dxgadapter *adapter = NULL;
+	struct dxgdevice *device = NULL;
+	int ret;
+
+	ret = copy_from_user(&args, inargs, sizeof(args));
+	if (ret) {
+		DXG_ERR("failed to copy input args");
+		ret = -EINVAL;
+		goto cleanup;
+	}
+	device = dxgprocess_device_by_handle(process, args.device);
+	if (device == NULL) {
+		ret = -EINVAL;
+		goto cleanup;
+	}
+	adapter = device->adapter;
+	ret = dxgadapter_acquire_lock_shared(adapter);
+	if (ret < 0) {
+		adapter = NULL;
+		goto cleanup;
+	}
+	device->execution_state = _D3DKMT_DEVICEEXECUTION_RESET;
+	ret = dxgvmb_send_mark_device_as_error(process, adapter, &args);
+cleanup:
+	if (adapter)
+		dxgadapter_release_lock_shared(adapter);
+	if (device)
+		kref_put(&device->device_kref, dxgdevice_release);
+	DXG_TRACE("ioctl:%s %d", errorstr(ret), ret);
+	return ret;
+}
+
 static int
 dxgkio_query_alloc_residency(struct dxgprocess *process, void *__user inargs)
 {
@@ -4404,7 +4440,7 @@ static struct ioctl_desc ioctls[] = {
 /* 0x23 */	{},
 /* 0x24 */	{},
 /* 0x25 */	{dxgkio_lock2, LX_DXLOCK2},
-/* 0x26 */	{},
+/* 0x26 */	{dxgkio_mark_device_as_error, LX_DXMARKDEVICEASERROR},
 /* 0x27 */	{},
 /* 0x28 */	{},
 /* 0x29 */	{},
include/uapi/misc/d3dkmthk.h +12 -0
index 749edf28bd43..ce5a638a886d 100644
@@ -790,6 +790,16 @@ struct d3dkmt_unlock2 {
 	struct d3dkmthandle			allocation;
 };
 
+enum d3dkmt_device_error_reason {
+	_D3DKMT_DEVICE_ERROR_REASON_GENERIC		= 0x80000000,
+	_D3DKMT_DEVICE_ERROR_REASON_DRIVER_ERROR	= 0x80000006,
+};
+
+struct d3dkmt_markdeviceaserror {
+	struct d3dkmthandle			device;
+	enum d3dkmt_device_error_reason		reason;
+};
+
 enum d3dkmt_standardallocationtype {
 	_D3DKMT_STANDARDALLOCATIONTYPE_EXISTINGHEAP	= 1,
 	_D3DKMT_STANDARDALLOCATIONTYPE_CROSSADAPTER	= 2,
@@ -1290,6 +1300,8 @@ struct d3dkmt_shareobjectwithhost {
 	_IOWR(0x47, 0x1f, struct d3dkmt_flushheaptransitions)
 #define LX_DXLOCK2			\
 	_IOWR(0x47, 0x25, struct d3dkmt_lock2)
+#define LX_DXMARKDEVICEASERROR		\
+	_IOWR(0x47, 0x26, struct d3dkmt_markdeviceaserror)
 #define LX_DXQUERYALLOCATIONRESIDENCY	\
 	_IOWR(0x47, 0x2a, struct d3dkmt_queryallocationresidency)
 #define LX_DXSETALLOCATIONPRIORITY	\
#23 lore raw

PATCH v4 23/55 drivers: hv: dxgkrnl: Ioctls to query statistics and clock calibration

In reply to Eric Curtin

From: Iouri Tarassov <[email protected]>

Implement ioctls to query statistics from the VGPU device (LX_DXQUERYSTATISTICS) and to query clock calibration (LX_DXQUERYCLOCKCALIBRATION).

The LX_DXQUERYSTATISTICS ioctl is used to query various statistics from the compute device on the host.

The LX_DXQUERYCLOCKCALIBRATION ioctl queries the compute device clock and is used for performance monitoring.

[kms: forward port to 6.6 from 6.1. No code changes made.]
---
 drivers/hv/dxgkrnl/dxgkrnl.h  |   8 +++
 drivers/hv/dxgkrnl/dxgvmbus.c |  77 +++++++++++++++++++++++
 drivers/hv/dxgkrnl/dxgvmbus.h |  21 +++++++
 drivers/hv/dxgkrnl/ioctl.c    | 111 +++++++++++++++++++++++++++++++++-
 include/uapi/misc/d3dkmthk.h  |  62 +++++++++++++++++++
 5 files changed, 277 insertions(+), 2 deletions(-)
drivers/hv/dxgkrnl/dxgkrnl.h +8 -0
index b454c7430f06..a55873bdd9a6 100644
@@ -885,6 +885,11 @@ int dxgvmb_send_query_adapter_info(struct dxgprocess *process,
 int dxgvmb_send_submit_command_hwqueue(struct dxgprocess *process,
 				       struct dxgadapter *adapter,
 				       struct d3dkmt_submitcommandtohwqueue *a);
+int dxgvmb_send_query_clock_calibration(struct dxgprocess *process,
+					struct dxgadapter *adapter,
+					struct d3dkmt_queryclockcalibration *a,
+					struct d3dkmt_queryclockcalibration
+					*__user inargs);
 int dxgvmb_send_flush_heap_transitions(struct dxgprocess *process,
 				       struct dxgadapter *adapter,
 				       struct d3dkmt_flushheaptransitions *arg);
@@ -929,6 +934,9 @@ int dxgvmb_send_get_stdalloc_data(struct dxgdevice *device,
 				  void *prive_alloc_data,
 				  u32 *res_priv_data_size,
 				  void *priv_res_data);
+int dxgvmb_send_query_statistics(struct dxgprocess *process,
+				 struct dxgadapter *adapter,
+				 struct d3dkmt_querystatistics *args);
 int dxgvmb_send_async_msg(struct dxgvmbuschannel *channel,
 			  void *command,
 			  u32 cmd_size);
drivers/hv/dxgkrnl/dxgvmbus.c +77 -0

86 lines not on this page · raw message

drivers/hv/dxgkrnl/dxgvmbus.h +21 -0
index a66e11097bb2..17768ed0e68d 100644
@@ -372,6 +372,16 @@ struct dxgkvmb_command_flushheaptransitions {
 	struct dxgkvmb_command_vgpu_to_host hdr;
 };
 
+struct dxgkvmb_command_queryclockcalibration {
+	struct dxgkvmb_command_vgpu_to_host hdr;
+	struct d3dkmt_queryclockcalibration args;
+};
+
+struct dxgkvmb_command_queryclockcalibration_return {
+	struct ntstatus			status;
+	struct dxgk_gpuclockdata	clock_data;
+};
+
 struct dxgkvmb_command_createallocation_allocinfo {
 	u32				flags;
 	u32				priv_drv_data_size;
@@ -408,6 +418,17 @@ struct dxgkvmb_command_openresource_return {
 /* struct d3dkmthandle   allocation[allocation_count]; */
 };
 
+struct dxgkvmb_command_querystatistics {
+	struct dxgkvmb_command_vgpu_to_host	hdr;
+	struct d3dkmt_querystatistics		args;
+};
+
+struct dxgkvmb_command_querystatistics_return {
+	struct ntstatus				status;
+	u32					reserved;
+	struct d3dkmt_querystatistics_result	result;
+};
+
 struct dxgkvmb_command_getstandardallocprivdata {
 	struct dxgkvmb_command_vgpu_to_host hdr;
 	enum d3dkmdt_standardallocationtype alloc_type;
drivers/hv/dxgkrnl/ioctl.c +109 -2

135 lines not on this page · raw message

include/uapi/misc/d3dkmthk.h +62 -0

86 lines not on this page · raw message

#24 lore raw

PATCH v4 24/55 drivers: hv: dxgkrnl: Offer and reclaim allocations

In reply to Eric Curtin

From: Iouri Tarassov <[email protected]>

Implement ioctls to offer and reclaim compute device allocations:
- LX_DXOFFERALLOCATIONS,
- LX_DXRECLAIMALLOCATIONS2

When a user mode driver (UMD) does not need to access an allocation, it can "offer" it by issuing the LX_DXOFFERALLOCATIONS ioctl. This means that the allocation is not in use and its local device memory could be evicted. The freed space could be given to another allocation. When the allocation is again needed, the UMD can attempt to"reclaim" the allocation by issuing the LX_DXRECLAIMALLOCATIONS2 ioctl. If the allocation is still not evicted, the reclaim operation succeeds and no other action is required. If the reclaim operation fails, the caller must restore the content of the allocation before it can be used by the device.

[kms: forward port to 6.6 from 6.1. No code changes made.]
---
 drivers/hv/dxgkrnl/dxgkrnl.h  |   8 +++
 drivers/hv/dxgkrnl/dxgvmbus.c | 124 +++++++++++++++++++++++++++++++++-
 drivers/hv/dxgkrnl/dxgvmbus.h |  27 ++++++++
 drivers/hv/dxgkrnl/ioctl.c    | 117 +++++++++++++++++++++++++++++++-
 include/uapi/misc/d3dkmthk.h  |  67 ++++++++++++++++++
 5 files changed, 340 insertions(+), 3 deletions(-)
drivers/hv/dxgkrnl/dxgkrnl.h +8 -0
index a55873bdd9a6..494ea8fb0bb3 100644
@@ -865,6 +865,14 @@ int dxgvmb_send_set_allocation_priority(struct dxgprocess *process,
 int dxgvmb_send_get_allocation_priority(struct dxgprocess *process,
 					struct dxgadapter *adapter,
 					struct d3dkmt_getallocationpriority *a);
+int dxgvmb_send_offer_allocations(struct dxgprocess *process,
+				  struct dxgadapter *adapter,
+				  struct d3dkmt_offerallocations *args);
+int dxgvmb_send_reclaim_allocations(struct dxgprocess *process,
+				    struct dxgadapter *adapter,
+				    struct d3dkmthandle device,
+				    struct d3dkmt_reclaimallocations2 *args,
+				    u64 __user *paging_fence_value);
 int dxgvmb_send_change_vidmem_reservation(struct dxgprocess *process,
 					  struct dxgadapter *adapter,
 					  struct d3dkmthandle other_process,
drivers/hv/dxgkrnl/dxgvmbus.c +123 -1

136 lines not on this page · raw message

drivers/hv/dxgkrnl/dxgvmbus.h +27 -0
index 17768ed0e68d..558c6576a262 100644
@@ -653,6 +653,33 @@ struct dxgkvmb_command_markdeviceaserror {
 	struct d3dkmt_markdeviceaserror args;
 };
 
+/* Returns ntstatus */
+struct dxgkvmb_command_offerallocations {
+	struct dxgkvmb_command_vgpu_to_host hdr;
+	struct d3dkmthandle		device;
+	u32				allocation_count;
+	enum d3dkmt_offer_priority	priority;
+	struct d3dkmt_offer_flags	flags;
+	bool				resources;
+	struct d3dkmthandle		allocations[1];
+};
+
+struct dxgkvmb_command_reclaimallocations {
+	struct dxgkvmb_command_vgpu_to_host hdr;
+	struct d3dkmthandle		device;
+	struct d3dkmthandle		paging_queue;
+	u32				allocation_count;
+	bool				resources;
+	bool				write_results;
+	struct d3dkmthandle		allocations[1];
+};
+
+struct dxgkvmb_command_reclaimallocations_return {
+	u64				paging_fence_value;
+	struct ntstatus			status;
+	enum d3dddi_reclaim_result	discarded[1];
+};
+
 /* Returns ntstatus */
 struct dxgkvmb_command_changevideomemoryreservation {
 	struct dxgkvmb_command_vgpu_to_host hdr;
drivers/hv/dxgkrnl/ioctl.c +115 -2

133 lines not on this page · raw message

include/uapi/misc/d3dkmthk.h +67 -0

87 lines not on this page · raw message

#25 lore raw

PATCH v4 25/55 drivers: hv: dxgkrnl: Ioctls to manage scheduling priority

In reply to Eric Curtin

From: Iouri Tarassov <[email protected]>

Implement iocts to manage compute device scheduling priority:
- LX_DXGETCONTEXTINPROCESSSCHEDULINGPRIORITY
- LX_DXGETCONTEXTSCHEDULINGPRIORITY
- LX_DXSETCONTEXTINPROCESSSCHEDULINGPRIORITY
- LX_DXSETCONTEXTSCHEDULINGPRIORITY

Each compute device execution context has an assigned scheduling priority. It is used by the compute device scheduler on the host to pick contexts for execution. There is a global priority and a priority within a process.

[kms: forward port to 6.6 from 6.1. No code changes made.]
---
 drivers/hv/dxgkrnl/dxgkrnl.h  |   9 ++
 drivers/hv/dxgkrnl/dxgvmbus.c |  67 ++++++++++++-
 drivers/hv/dxgkrnl/dxgvmbus.h |  19 ++++
 drivers/hv/dxgkrnl/ioctl.c    | 177 +++++++++++++++++++++++++++++++++-
 include/uapi/misc/d3dkmthk.h  |  28 ++++++
 5 files changed, 294 insertions(+), 6 deletions(-)
drivers/hv/dxgkrnl/dxgkrnl.h +9 -0
index 494ea8fb0bb3..02d10bdcc820 100644
@@ -865,6 +865,15 @@ int dxgvmb_send_set_allocation_priority(struct dxgprocess *process,
 int dxgvmb_send_get_allocation_priority(struct dxgprocess *process,
 					struct dxgadapter *adapter,
 					struct d3dkmt_getallocationpriority *a);
+int dxgvmb_send_set_context_sch_priority(struct dxgprocess *process,
+					 struct dxgadapter *adapter,
+					 struct d3dkmthandle context,
+					 int priority, bool in_process);
+int dxgvmb_send_get_context_sch_priority(struct dxgprocess *process,
+					 struct dxgadapter *adapter,
+					 struct d3dkmthandle context,
+					 int *priority,
+					 bool in_process);
 int dxgvmb_send_offer_allocations(struct dxgprocess *process,
 				  struct dxgadapter *adapter,
 				  struct d3dkmt_offerallocations *args);
drivers/hv/dxgkrnl/dxgvmbus.c +65 -2

85 lines not on this page · raw message

drivers/hv/dxgkrnl/dxgvmbus.h +19 -0
index 558c6576a262..509482e1f870 100644
@@ -331,6 +331,25 @@ struct dxgkvmb_command_getallocationpriority_return {
 	/* u32 priorities[allocation_count or 1]; */
 };
 
+/* Returns ntstatus */
+struct dxgkvmb_command_setcontextschedulingpriority2 {
+	struct dxgkvmb_command_vgpu_to_host hdr;
+	struct d3dkmthandle		context;
+	int				priority;
+	bool				in_process;
+};
+
+struct dxgkvmb_command_getcontextschedulingpriority {
+	struct dxgkvmb_command_vgpu_to_host hdr;
+	struct d3dkmthandle		context;
+	bool				in_process;
+};
+
+struct dxgkvmb_command_getcontextschedulingpriority_return {
+	struct ntstatus			status;
+	int				priority;
+};
+
 struct dxgkvmb_command_createdevice {
 	struct dxgkvmb_command_vgpu_to_host hdr;
 	struct d3dkmt_createdeviceflags	flags;
drivers/hv/dxgkrnl/ioctl.c +173 -4

195 lines not on this page · raw message

include/uapi/misc/d3dkmthk.h +28 -0
index 46b9f6d303bf..a9bafab97c18 100644
@@ -708,6 +708,26 @@ struct d3dkmt_submitcommandtohwqueue {
 #endif
 };
 
+struct d3dkmt_setcontextschedulingpriority {
+	struct d3dkmthandle			context;
+	int					priority;
+};
+
+struct d3dkmt_setcontextinprocessschedulingpriority {
+	struct d3dkmthandle			context;
+	int					priority;
+};
+
+struct d3dkmt_getcontextschedulingpriority {
+	struct d3dkmthandle			context;
+	int					priority;
+};
+
+struct d3dkmt_getcontextinprocessschedulingpriority {
+	struct d3dkmthandle			context;
+	int					priority;
+};
+
 struct d3dkmt_setallocationpriority {
 	struct d3dkmthandle		device;
 	struct d3dkmthandle		resource;
@@ -1419,6 +1439,10 @@ struct d3dkmt_shareobjectwithhost {
 	_IOWR(0x47, 0x1d, struct d3dkmt_destroysynchronizationobject)
 #define LX_DXFLUSHHEAPTRANSITIONS	\
 	_IOWR(0x47, 0x1f, struct d3dkmt_flushheaptransitions)
+#define LX_DXGETCONTEXTINPROCESSSCHEDULINGPRIORITY \
+	_IOWR(0x47, 0x21, struct d3dkmt_getcontextinprocessschedulingpriority)
+#define LX_DXGETCONTEXTSCHEDULINGPRIORITY \
+	_IOWR(0x47, 0x22, struct d3dkmt_getcontextschedulingpriority)
 #define LX_DXLOCK2			\
 	_IOWR(0x47, 0x25, struct d3dkmt_lock2)
 #define LX_DXMARKDEVICEASERROR		\
@@ -1431,6 +1455,10 @@ struct d3dkmt_shareobjectwithhost {
 	_IOWR(0x47, 0x2c, struct d3dkmt_reclaimallocations2)
 #define LX_DXSETALLOCATIONPRIORITY	\
 	_IOWR(0x47, 0x2e, struct d3dkmt_setallocationpriority)
+#define LX_DXSETCONTEXTINPROCESSSCHEDULINGPRIORITY \
+	_IOWR(0x47, 0x2f, struct d3dkmt_setcontextinprocessschedulingpriority)
+#define LX_DXSETCONTEXTSCHEDULINGPRIORITY \
+	_IOWR(0x47, 0x30, struct d3dkmt_setcontextschedulingpriority)
 #define LX_DXSIGNALSYNCHRONIZATIONOBJECTFROMCPU \
 	_IOWR(0x47, 0x31, struct d3dkmt_signalsynchronizationobjectfromcpu)
 #define LX_DXSIGNALSYNCHRONIZATIONOBJECTFROMGPU \
#26 lore raw

PATCH v4 26/55 drivers: hv: dxgkrnl: Manage residency of allocations

In reply to Eric Curtin

From: Iouri Tarassov <[email protected]>

Implement ioctls to manage residency of compute device allocations:
- LX_DXMAKERESIDENT,
- LX_DXEVICT.

An allocation is "resident" when the compute devoce is setup to access it. It means that the allocation is in the local device memory or in non-pageable system memory.

The current design does not support on demand compute device page faulting. An allocation must be resident before the compute device is allowed to access it.

The LX_DXMAKERESIDENT ioctl instructs the video memory manager to make the given allocations resident. The operation is submitted to a paging queue (dxgpagingqueue). When the ioctl returns a "pending" status, a monitored fence sync object can be used to synchronize with the completion of the operation.

The LX_DXEVICT ioctl istructs the video memory manager to evict the given allocations from device accessible memory.

[kms: forward port to 6.6 from 6.1. No code changes made.]
---
 drivers/hv/dxgkrnl/dxgkrnl.h  |   4 +
 drivers/hv/dxgkrnl/dxgvmbus.c |  98 +++++++++++++++++++++++
 drivers/hv/dxgkrnl/dxgvmbus.h |  27 +++++++
 drivers/hv/dxgkrnl/ioctl.c    | 141 +++++++++++++++++++++++++++++++++-
 include/uapi/misc/d3dkmthk.h  |  54 +++++++++++++
 5 files changed, 322 insertions(+), 2 deletions(-)
drivers/hv/dxgkrnl/dxgkrnl.h +4 -0
index 02d10bdcc820..93c3ceb23865 100644
@@ -810,6 +810,10 @@ int dxgvmb_send_create_allocation(struct dxgprocess *pr, struct dxgdevice *dev,
 int dxgvmb_send_destroy_allocation(struct dxgprocess *pr, struct dxgdevice *dev,
 				   struct d3dkmt_destroyallocation2 *args,
 				   struct d3dkmthandle *alloc_handles);
+int dxgvmb_send_make_resident(struct dxgprocess *pr, struct dxgadapter *adapter,
+			      struct d3dddi_makeresident *args);
+int dxgvmb_send_evict(struct dxgprocess *pr, struct dxgadapter *adapter,
+		      struct d3dkmt_evict *args);
 int dxgvmb_send_submit_command(struct dxgprocess *pr,
 			       struct dxgadapter *adapter,
 			       struct d3dkmt_submitcommand *args);
drivers/hv/dxgkrnl/dxgvmbus.c +98 -0

104 lines not on this page · raw message

drivers/hv/dxgkrnl/dxgvmbus.h +27 -0
index 509482e1f870..23f92ab9f8ad 100644
@@ -372,6 +372,33 @@ struct dxgkvmb_command_flushdevice {
 	enum dxgdevice_flushschedulerreason	reason;
 };
 
+struct dxgkvmb_command_makeresident {
+	struct dxgkvmb_command_vgpu_to_host hdr;
+	struct d3dkmthandle		device;
+	struct d3dkmthandle		paging_queue;
+	struct d3dddi_makeresident_flags flags;
+	u32				alloc_count;
+	struct d3dkmthandle		allocations[1];
+};
+
+struct dxgkvmb_command_makeresident_return {
+	u64			paging_fence_value;
+	u64			num_bytes_to_trim;
+	struct ntstatus		status;
+};
+
+struct dxgkvmb_command_evict {
+	struct dxgkvmb_command_vgpu_to_host hdr;
+	struct d3dkmthandle		device;
+	struct d3dddi_evict_flags	flags;
+	u32				alloc_count;
+	struct d3dkmthandle		allocations[1];
+};
+
+struct dxgkvmb_command_evict_return {
+	u64			num_bytes_to_trim;
+};
+
 struct dxgkvmb_command_submitcommand {
 	struct dxgkvmb_command_vgpu_to_host hdr;
 	struct d3dkmt_submitcommand	args;
drivers/hv/dxgkrnl/ioctl.c +139 -2

159 lines not on this page · raw message

include/uapi/misc/d3dkmthk.h +54 -0

72 lines not on this page · raw message

#27 lore raw

PATCH v4 27/55 drivers: hv: dxgkrnl: Manage compute device virtual addresses

In reply to Eric Curtin

From: Iouri Tarassov <[email protected]>

Implement ioctls to manage compute device virtual addresses (VA):
- LX_DXRESERVEGPUVIRTUALADDRESS,
- LX_DXFREEGPUVIRTUALADDRESS,
- LX_DXMAPGPUVIRTUALADDRESS,
- LX_DXUPDATEGPUVIRTUALADDRESS.

Compute devices access memory by using virtual addressses. Each process has a dedicated VA space. The video memory manager on the host is responsible with updating device page tables before submitting a DMA buffer for execution.

The LX_DXRESERVEGPUVIRTUALADDRESS ioctl reserves a portion of the process compute device VA space.

The LX_DXMAPGPUVIRTUALADDRESS ioctl reserves a portion of the process compute device VA space and maps it to the given compute device allocation.

The LX_DXFREEGPUVIRTUALADDRESS frees the previously reserved portion of the compute device VA space.

The LX_DXUPDATEGPUVIRTUALADDRESS ioctl adds operations to modify the compute device VA space to a compute device execution context. It allows the operations to be queued and synchronized with execution of other compute device DMA buffers..

[kms: forward port to 6.6 from 6.1. No code changes made.]
---
 drivers/hv/dxgkrnl/dxgkrnl.h  |  10 ++
 drivers/hv/dxgkrnl/dxgvmbus.c | 150 ++++++++++++++++++++++
 drivers/hv/dxgkrnl/dxgvmbus.h |  38 ++++++
 drivers/hv/dxgkrnl/ioctl.c    | 228 +++++++++++++++++++++++++++++++++-
 include/uapi/misc/d3dkmthk.h  | 126 +++++++++++++++++++
 5 files changed, 548 insertions(+), 4 deletions(-)
drivers/hv/dxgkrnl/dxgkrnl.h +10 -0
index 93c3ceb23865..93bc9b41aa41 100644
@@ -817,6 +817,16 @@ int dxgvmb_send_evict(struct dxgprocess *pr, struct dxgadapter *adapter,
 int dxgvmb_send_submit_command(struct dxgprocess *pr,
 			       struct dxgadapter *adapter,
 			       struct d3dkmt_submitcommand *args);
+int dxgvmb_send_map_gpu_va(struct dxgprocess *pr, struct d3dkmthandle h,
+			   struct dxgadapter *adapter,
+			   struct d3dddi_mapgpuvirtualaddress *args);
+int dxgvmb_send_reserve_gpu_va(struct dxgprocess *pr,
+			       struct dxgadapter *adapter,
+			       struct d3dddi_reservegpuvirtualaddress *args);
+int dxgvmb_send_free_gpu_va(struct dxgprocess *pr, struct dxgadapter *adapter,
+			    struct d3dkmt_freegpuvirtualaddress *args);
+int dxgvmb_send_update_gpu_va(struct dxgprocess *pr, struct dxgadapter *adapter,
+			      struct d3dkmt_updategpuvirtualaddress *args);
 int dxgvmb_send_create_sync_object(struct dxgprocess *pr,
 				   struct dxgadapter *adapter,
 				   struct d3dkmt_createsynchronizationobject2
drivers/hv/dxgkrnl/dxgvmbus.c +150 -0

156 lines not on this page · raw message

drivers/hv/dxgkrnl/dxgvmbus.h +38 -0
index 23f92ab9f8ad..88967ff6a505 100644
@@ -418,6 +418,44 @@ struct dxgkvmb_command_flushheaptransitions {
 	struct dxgkvmb_command_vgpu_to_host hdr;
 };
 
+struct dxgkvmb_command_freegpuvirtualaddress {
+	struct dxgkvmb_command_vgpu_to_host hdr;
+	struct d3dkmt_freegpuvirtualaddress args;
+};
+
+struct dxgkvmb_command_mapgpuvirtualaddress {
+	struct dxgkvmb_command_vgpu_to_host hdr;
+	struct d3dddi_mapgpuvirtualaddress args;
+	struct d3dkmthandle		device;
+};
+
+struct dxgkvmb_command_mapgpuvirtualaddress_return {
+	u64		virtual_address;
+	u64		paging_fence_value;
+	struct ntstatus	status;
+};
+
+struct dxgkvmb_command_reservegpuvirtualaddress {
+	struct dxgkvmb_command_vgpu_to_host hdr;
+	struct d3dddi_reservegpuvirtualaddress args;
+};
+
+struct dxgkvmb_command_reservegpuvirtualaddress_return {
+	u64	virtual_address;
+	u64	paging_fence_value;
+};
+
+struct dxgkvmb_command_updategpuvirtualaddress {
+	struct dxgkvmb_command_vgpu_to_host hdr;
+	u64				fence_value;
+	struct d3dkmthandle		device;
+	struct d3dkmthandle		context;
+	struct d3dkmthandle		fence_object;
+	u32				num_operations;
+	u32				flags;
+	struct d3dddi_updategpuvirtualaddress_operation operations[1];
+};
+
 struct dxgkvmb_command_queryclockcalibration {
 	struct dxgkvmb_command_vgpu_to_host hdr;
 	struct d3dkmt_queryclockcalibration args;
drivers/hv/dxgkrnl/ioctl.c +224 -4

255 lines not on this page · raw message

include/uapi/misc/d3dkmthk.h +126 -0

156 lines not on this page · raw message

#28 lore raw

PATCH v4 28/55 drivers: hv: dxgkrnl: Add support to map guest pages by host

In reply to Eric Curtin

From: Iouri Tarassov <[email protected]>

Implement support for mapping guest memory pages by the host. This removes hyper-v limitations of using GPADL (guest physical address list).

Dxgkrnl uses hyper-v GPADLs to share guest system memory with the host. This method has limitations:
- a single GPADL can represent only ~32MB of memory
- there is a limit of how much memory the total size of GPADLs in a VM can represent.
To avoid these limitations the host implemented mapping guest memory pages. Presence of this support is determined by reading PCI config space. When the support is enabled, dxgkrnl does not use GPADLs and instead uses the following code flow:
- memory pages of an existing system memory buffer are pinned
- PFNs of the pages are sent to the host via a VM bus message
- the host maps the PFNs to get access to the memory

[kms: forward port to 6.6 from 6.1. No code changes made.]
---
 drivers/hv/dxgkrnl/Makefile    |   2 +-
 drivers/hv/dxgkrnl/dxgkrnl.h   |   1 +
 drivers/hv/dxgkrnl/dxgmodule.c |  33 +++++++++-
 drivers/hv/dxgkrnl/dxgvmbus.c  | 117 ++++++++++++++++++++++++---------
 drivers/hv/dxgkrnl/dxgvmbus.h  |  10 +++
 drivers/hv/dxgkrnl/misc.c      |   1 +
 6 files changed, 129 insertions(+), 35 deletions(-)
drivers/hv/dxgkrnl/Makefile +1 -1
index 9d821e83448a..fc85a47a6ad5 100644
@@ -2,4 +2,4 @@
 # Makefile for the hyper-v compute device driver (dxgkrnl).
 
 obj-$(CONFIG_DXGKRNL)	+= dxgkrnl.o
-dxgkrnl-y		:= dxgmodule.o hmgr.o misc.o dxgadapter.o ioctl.o dxgvmbus.o dxgprocess.o
+dxgkrnl-y := dxgmodule.o hmgr.o misc.o dxgadapter.o ioctl.o dxgvmbus.o dxgprocess.o
drivers/hv/dxgkrnl/dxgkrnl.h +1 -0
index 93bc9b41aa41..091dbe999d33 100644
@@ -316,6 +316,7 @@ struct dxgglobal {
 	bool			misc_registered;
 	bool			pci_registered;
 	bool			vmbus_registered;
+	bool			map_guest_pages_enabled;
 };
 
 static inline struct dxgglobal *dxggbl(void)
drivers/hv/dxgkrnl/dxgmodule.c +31 -2
index 5c364a46b65f..b1b612b90fc1 100644
@@ -147,7 +147,7 @@ void dxgglobal_remove_host_event(struct dxghostevent *event)
 
 void signal_host_cpu_event(struct dxghostevent *eventhdr)
 {
-	struct  dxghosteventcpu *event = (struct  dxghosteventcpu *)eventhdr;
+	struct dxghosteventcpu *event = (struct dxghosteventcpu *)eventhdr;
 
 	if (event->remove_from_list ||
 		event->destroy_after_signal) {
@@ -426,7 +426,11 @@ const struct file_operations dxgk_fops = {
 #define DXGK_VMBUS_VGPU_LUID_OFFSET	(DXGK_VMBUS_VERSION_OFFSET + \
 					sizeof(u32))
 
-/* The guest writes its capabilities to this address */
+/* The host caps (dxgk_vmbus_hostcaps) */
+#define DXGK_VMBUS_HOSTCAPS_OFFSET	(DXGK_VMBUS_VGPU_LUID_OFFSET + \
+					sizeof(struct winluid))
+
+/* The guest writes its capavilities to this adderss */
 #define DXGK_VMBUS_GUESTCAPS_OFFSET	(DXGK_VMBUS_VERSION_OFFSET + \
 					sizeof(u32))
 
@@ -441,6 +445,23 @@ struct dxgk_vmbus_guestcaps {
 	};
 };
 
+/*
+ * The structure defines features, supported by the host.
+ *
+ * map_guest_memory
+ *   Host can map guest memory pages, so the guest can avoid using GPADLs
+ *   to represent existing system memory allocations.
+ */
+struct dxgk_vmbus_hostcaps {
+	union {
+		struct {
+			u32	map_guest_memory	: 1;
+			u32	reserved		: 31;
+		};
+		u32 host_caps;
+	};
+};
+
 /*
  * A helper function to read PCI config space.
  */
@@ -475,6 +496,7 @@ static int dxg_pci_probe_device(struct pci_dev *dev,
 	struct winluid vgpu_luid = {};
 	struct dxgk_vmbus_guestcaps guest_caps = {.wsl2 = 1};
 	struct dxgglobal *dxgglobal = dxggbl();
+	struct dxgk_vmbus_hostcaps host_caps = {};
 
 	mutex_lock(&dxgglobal->device_mutex);
 
@@ -503,6 +525,13 @@ static int dxg_pci_probe_device(struct pci_dev *dev,
 		if (ret)
 			goto cleanup;
 
+		ret = pci_read_config_dword(dev, DXGK_VMBUS_HOSTCAPS_OFFSET,
+					&host_caps.host_caps);
+		if (ret == 0) {
+			if (host_caps.map_guest_memory)
+				dxgglobal->map_guest_pages_enabled = true;
+		}
+
 		if (dxgglobal->vmbus_ver > DXGK_VMBUS_INTERFACE_VERSION)
 			dxgglobal->vmbus_ver = DXGK_VMBUS_INTERFACE_VERSION;
 	}
drivers/hv/dxgkrnl/dxgvmbus.c +85 -32

145 lines not on this page · raw message

drivers/hv/dxgkrnl/dxgvmbus.h +10 -0
index 88967ff6a505..b4a98f7c2522 100644
@@ -234,6 +234,16 @@ struct dxgkvmb_command_setexistingsysmemstore {
 	u32				gpadl;
 };
 
+/* Returns ntstatus */
+struct dxgkvmb_command_setexistingsysmempages {
+	struct dxgkvmb_command_vgpu_to_host hdr;
+	struct d3dkmthandle		device;
+	struct d3dkmthandle		allocation;
+	u32				num_pages;
+	u32				alloc_offset_in_pages;
+	/* u64 pfn_array[num_pages] */
+};
+
 struct dxgkvmb_command_createprocess {
 	struct dxgkvmb_command_vm_to_host hdr;
 	void			*process;
drivers/hv/dxgkrnl/misc.c +1 -0
index cb1e0635bebc..4a1309d80ee5 100644
@@ -35,3 +35,4 @@ u16 *wcsncpy(u16 *dest, const u16 *src, size_t n)
 	dest[i - 1] = 0;
 	return dest;
 }
+
#29 lore raw

PATCH v4 29/55 drivers: hv: dxgkrnl: Removed struct vmbus_gpadl, which was defined in the main linux branch

In reply to Eric Curtin

From: Iouri Tarassov <[email protected]>

[kms: forward port to 6.6 from 6.1. No code changes made.]
---
 drivers/hv/dxgkrnl/dxgadapter.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/hv/dxgkrnl/dxgadapter.c +1 -1
index 6f763e326a65..236febbc6fca 100644
@@ -932,7 +932,7 @@ void dxgallocation_destroy(struct dxgallocation *alloc)
 		vmbus_teardown_gpadl(dxgglobal_get_vmbus(), &alloc->gpadl);
 		alloc->gpadl.gpadl_handle = 0;
 	}
-else
+#else
 	if (alloc->gpadl) {
 		DXG_TRACE("Teardown gpadl %d", alloc->gpadl);
 		vmbus_teardown_gpadl(dxgglobal_get_vmbus(), alloc->gpadl);
#30 lore raw

PATCH v4 30/55 drivers: hv: dxgkrnl: Remove dxgk_init_ioctls

In reply to Eric Curtin

From: Iouri Tarassov <[email protected]>

The array of ioctls is initialized statically to remove the unnecessary function.

[kms: forward port to 6.6 from 6.1. No code changes made.]
---
 drivers/hv/dxgkrnl/dxgmodule.c |  2 +-
 drivers/hv/dxgkrnl/ioctl.c     | 15 +++++++--------
 2 files changed, 8 insertions(+), 9 deletions(-)
drivers/hv/dxgkrnl/dxgmodule.c +1 -1
index b1b612b90fc1..f1245a9d8826 100644
@@ -300,7 +300,7 @@ static void dxgglobal_start_adapters(void)
 }
 
 /*
- * Stopsthe active dxgadapter objects.
+ * Stop the active dxgadapter objects.
  */
 static void dxgglobal_stop_adapters(void)
 {
drivers/hv/dxgkrnl/ioctl.c +7 -8
index f6700e974f25..8732a66040a0 100644
@@ -26,7 +26,6 @@
 struct ioctl_desc {
 	int (*ioctl_callback)(struct dxgprocess *p, void __user *arg);
 	u32 ioctl;
-	u32 arg_size;
 };
 
 #ifdef DEBUG
@@ -91,7 +90,7 @@ static const struct file_operations dxg_resource_fops = {
 };
 
 static int dxgkio_open_adapter_from_luid(struct dxgprocess *process,
-						   void *__user inargs)
+					void *__user inargs)
 {
 	struct d3dkmt_openadapterfromluid args;
 	int ret;
@@ -1002,7 +1001,7 @@ dxgkio_create_hwqueue(struct dxgprocess *process, void *__user inargs)
 }
 
 static int dxgkio_destroy_hwqueue(struct dxgprocess *process,
-					    void *__user inargs)
+				void *__user inargs)
 {
 	struct d3dkmt_destroyhwqueue args;
 	int ret;
@@ -2280,7 +2279,8 @@ dxgkio_submit_command(struct dxgprocess *process, void *__user inargs)
 }
 
 static int
-dxgkio_submit_command_to_hwqueue(struct dxgprocess *process, void *__user inargs)
+dxgkio_submit_command_to_hwqueue(struct dxgprocess *process,
+				void *__user inargs)
 {
 	int ret;
 	struct d3dkmt_submitcommandtohwqueue args;
@@ -5087,8 +5087,7 @@ open_resource(struct dxgprocess *process,
 }
 
 static int
-dxgkio_open_resource_nt(struct dxgprocess *process,
-				      void *__user inargs)
+dxgkio_open_resource_nt(struct dxgprocess *process, void *__user inargs)
 {
 	struct d3dkmt_openresourcefromnthandle args;
 	struct d3dkmt_openresourcefromnthandle *__user args_user = inargs;
@@ -5166,7 +5165,7 @@ static struct ioctl_desc ioctls[] = {
 /* 0x14 */	{dxgkio_enum_adapters, LX_DXENUMADAPTERS2},
 /* 0x15 */	{dxgkio_close_adapter, LX_DXCLOSEADAPTER},
 /* 0x16 */	{dxgkio_change_vidmem_reservation,
-		  LX_DXCHANGEVIDEOMEMORYRESERVATION},
+		 LX_DXCHANGEVIDEOMEMORYRESERVATION},
 /* 0x17 */	{},
 /* 0x18 */	{dxgkio_create_hwqueue, LX_DXCREATEHWQUEUE},
 /* 0x19 */	{dxgkio_destroy_device, LX_DXDESTROYDEVICE},
@@ -5205,7 +5204,7 @@ static struct ioctl_desc ioctls[] = {
 		 LX_DXSIGNALSYNCHRONIZATIONOBJECTFROMGPU2},
 /* 0x34 */	{dxgkio_submit_command_to_hwqueue, LX_DXSUBMITCOMMANDTOHWQUEUE},
 /* 0x35 */	{dxgkio_submit_signal_to_hwqueue,
-		  LX_DXSUBMITSIGNALSYNCOBJECTSTOHWQUEUE},
+		 LX_DXSUBMITSIGNALSYNCOBJECTSTOHWQUEUE},
 /* 0x36 */	{dxgkio_submit_wait_to_hwqueue,
 		 LX_DXSUBMITWAITFORSYNCOBJECTSTOHWQUEUE},
 /* 0x37 */	{dxgkio_unlock2, LX_DXUNLOCK2},
#31 lore raw

PATCH v4 31/55 drivers: hv: dxgkrnl: Creation of dxgsyncfile objects

In reply to Eric Curtin

From: Iouri Tarassov <[email protected]>

Implement the ioctl to create a dxgsyncfile object (LX_DXCREATESYNCFILE). This object is a wrapper around a monitored fence sync object and a fence value.

dxgsyncfile is built on top of the Linux sync_file object and provides a way for the user mode to synchronize with the execution of the device DMA packets.

The ioctl creates a dxgsyncfile object for the given GPU synchronization object and a fence value. A file descriptor of the sync_file object is returned to the caller. The caller could wait for the object by using poll(). When the underlying GPU synchronization object is signaled on the host, the host sends a message to the virtual machine and the sync_file object is signaled.

[kms: forward port to 6.6 from 6.1. No code changes made.]
---
 drivers/hv/dxgkrnl/Kconfig       |   2 +
 drivers/hv/dxgkrnl/Makefile      |   2 +-
 drivers/hv/dxgkrnl/dxgkrnl.h     |   2 +
 drivers/hv/dxgkrnl/dxgmodule.c   |  12 ++
 drivers/hv/dxgkrnl/dxgsyncfile.c | 215 +++++++++++++++++++++++++++++++
 drivers/hv/dxgkrnl/dxgsyncfile.h |  30 +++++
 drivers/hv/dxgkrnl/dxgvmbus.c    |  33 +++--
 drivers/hv/dxgkrnl/ioctl.c       |   5 +-
 include/uapi/misc/d3dkmthk.h     |   9 ++
 9 files changed, 294 insertions(+), 16 deletions(-)
 create mode 100644 drivers/hv/dxgkrnl/dxgsyncfile.c
 create mode 100644 drivers/hv/dxgkrnl/dxgsyncfile.h
drivers/hv/dxgkrnl/Kconfig +2 -0
index bcd92bbff939..782692610887 100644
@@ -6,6 +6,8 @@ config DXGKRNL
 	tristate "Microsoft Paravirtualized GPU support"
 	depends on HYPERV
 	depends on 64BIT || COMPILE_TEST
+	select DMA_SHARED_BUFFER
+	select SYNC_FILE
 	help
 	  This driver supports paravirtualized virtual compute devices, exposed
 	  by Microsoft Hyper-V when Linux is running inside of a virtual machine
drivers/hv/dxgkrnl/Makefile +1 -1
index fc85a47a6ad5..89824cda670a 100644
@@ -2,4 +2,4 @@
 # Makefile for the hyper-v compute device driver (dxgkrnl).
 
 obj-$(CONFIG_DXGKRNL)	+= dxgkrnl.o
-dxgkrnl-y := dxgmodule.o hmgr.o misc.o dxgadapter.o ioctl.o dxgvmbus.o dxgprocess.o
+dxgkrnl-y := dxgmodule.o hmgr.o misc.o dxgadapter.o ioctl.o dxgvmbus.o dxgprocess.o  dxgsyncfile.o
drivers/hv/dxgkrnl/dxgkrnl.h +2 -0
index 091dbe999d33..3a69e3b34e1c 100644
@@ -120,6 +120,7 @@ struct dxgpagingqueue {
  */
 enum dxghosteventtype {
 	dxghostevent_cpu_event = 1,
+	dxghostevent_dma_fence = 2,
 };
 
 struct dxghostevent {
@@ -858,6 +859,7 @@ int dxgvmb_send_wait_sync_object_cpu(struct dxgprocess *process,
 				     struct
 				     d3dkmt_waitforsynchronizationobjectfromcpu
 				     *args,
+				     bool user_address,
 				     u64 cpu_event);
 int dxgvmb_send_lock2(struct dxgprocess *process,
 		      struct dxgadapter *adapter,
drivers/hv/dxgkrnl/dxgmodule.c +12 -0
index f1245a9d8826..af51fcd35697 100644
@@ -16,6 +16,7 @@
 #include <linux/hyperv.h>
 #include <linux/pci.h>
 #include "dxgkrnl.h"
+#include "dxgsyncfile.h"
 
 #define PCI_VENDOR_ID_MICROSOFT		0x1414
 #define PCI_DEVICE_ID_VIRTUAL_RENDER	0x008E
@@ -145,6 +146,15 @@ void dxgglobal_remove_host_event(struct dxghostevent *event)
 	spin_unlock_irq(&dxgglobal->host_event_list_mutex);
 }
 
+static void signal_dma_fence(struct dxghostevent *eventhdr)
+{
+	struct dxgsyncpoint *event = (struct dxgsyncpoint *)eventhdr;
+
+	event->fence_value++;
+	list_del(&eventhdr->host_event_list_entry);
+	dma_fence_signal(&event->base);
+}
+
 void signal_host_cpu_event(struct dxghostevent *eventhdr)
 {
 	struct dxghosteventcpu *event = (struct dxghosteventcpu *)eventhdr;
@@ -184,6 +194,8 @@ void dxgglobal_signal_host_event(u64 event_id)
 			DXG_TRACE("found event to signal");
 			if (event->event_type == dxghostevent_cpu_event)
 				signal_host_cpu_event(event);
+			else if (event->event_type == dxghostevent_dma_fence)
+				signal_dma_fence(event);
 			else
 				DXG_ERR("Unknown host event type");
 			break;
drivers/hv/dxgkrnl/dxgsyncfile.c new file +215 -0

215 lines not on this page · raw message

drivers/hv/dxgkrnl/dxgsyncfile.h new file +30 -0
new file mode 100644
index 000000000000..207ef9b30f67
@@ -0,0 +1,30 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+/*
+ * Copyright (c) 2022, Microsoft Corporation.
+ *
+ * Author:
+ *   Iouri Tarassov <[email protected]>
+ *
+ * Dxgkrnl Graphics Driver
+ * Headers for sync file objects
+ *
+ */
+
+#ifndef _DXGSYNCFILE_H
+#define _DXGSYNCFILE_H
+
+#include <linux/sync_file.h>
+
+int dxgkio_create_sync_file(struct dxgprocess *process, void *__user inargs);
+
+struct dxgsyncpoint {
+	struct dxghostevent	hdr;
+	struct dma_fence	base;
+	u64			fence_value;
+	u64			context;
+	spinlock_t		lock;
+	u64			u64;
+};
+
+#endif	 /* _DXGSYNCFILE_H */
drivers/hv/dxgkrnl/dxgvmbus.c +20 -13
index 4d7807909284..913ea3cabb31 100644
@@ -2820,6 +2820,7 @@ int dxgvmb_send_wait_sync_object_cpu(struct dxgprocess *process,
 				     struct
 				     d3dkmt_waitforsynchronizationobjectfromcpu
 				     *args,
+				     bool user_address,
 				     u64 cpu_event)
 {
 	int ret = -EINVAL;
@@ -2844,19 +2845,25 @@ int dxgvmb_send_wait_sync_object_cpu(struct dxgprocess *process,
 	command->guest_event_pointer = (u64) cpu_event;
 	current_pos = (u8 *) &command[1];
 
-	ret = copy_from_user(current_pos, args->objects, object_size);
-	if (ret) {
-		DXG_ERR("failed to copy objects");
-		ret = -EINVAL;
-		goto cleanup;
-	}
-	current_pos += object_size;
-	ret = copy_from_user(current_pos, args->fence_values,
-				fence_size);
-	if (ret) {
-		DXG_ERR("failed to copy fences");
-		ret = -EINVAL;
-		goto cleanup;
+	if (user_address) {
+		ret = copy_from_user(current_pos, args->objects, object_size);
+		if (ret) {
+			DXG_ERR("failed to copy objects");
+			ret = -EINVAL;
+			goto cleanup;
+		}
+		current_pos += object_size;
+		ret = copy_from_user(current_pos, args->fence_values,
+					fence_size);
+		if (ret) {
+			DXG_ERR("failed to copy fences");
+			ret = -EINVAL;
+			goto cleanup;
+		}
+	} else {
+		memcpy(current_pos, args->objects, object_size);
+		current_pos += object_size;
+		memcpy(current_pos, args->fence_values, fence_size);
 	}
 
 	ret = dxgvmb_send_sync_msg_ntstatus(msg.channel, msg.hdr, msg.size);
drivers/hv/dxgkrnl/ioctl.c +3 -2
index 8732a66040a0..6c26aafb0619 100644
@@ -19,6 +19,7 @@
 
 #include "dxgkrnl.h"
 #include "dxgvmbus.h"
+#include "dxgsyncfile.h"
 
 #undef pr_fmt
 #define pr_fmt(fmt)	"dxgk: " fmt
@@ -3488,7 +3489,7 @@ dxgkio_wait_sync_object_cpu(struct dxgprocess *process, void *__user inargs)
 	}
 
 	ret = dxgvmb_send_wait_sync_object_cpu(process, adapter,
-					       &args, event_id);
+					       &args, true, event_id);
 	if (ret < 0)
 		goto cleanup;
 
@@ -5224,7 +5225,7 @@ static struct ioctl_desc ioctls[] = {
 /* 0x42 */	{dxgkio_open_resource_nt, LX_DXOPENRESOURCEFROMNTHANDLE},
 /* 0x43 */	{dxgkio_query_statistics, LX_DXQUERYSTATISTICS},
 /* 0x44 */	{dxgkio_share_object_with_host, LX_DXSHAREOBJECTWITHHOST},
-/* 0x45 */	{},
+/* 0x45 */	{dxgkio_create_sync_file, LX_DXCREATESYNCFILE},
 };
 
 /*
include/uapi/misc/d3dkmthk.h +9 -0
index 1f60f5120e1d..c7f168425dc7 100644
@@ -1554,6 +1554,13 @@ struct d3dkmt_shareobjectwithhost {
 	__u64			object_vail_nt_handle;
 };
 
+struct d3dkmt_createsyncfile {
+	struct d3dkmthandle	device;
+	struct d3dkmthandle	monitored_fence;
+	__u64			fence_value;
+	__u64			sync_file_handle;	/* out */
+};
+
 /*
  * Dxgkrnl Graphics Port Driver ioctl definitions
  *
@@ -1677,5 +1684,7 @@ struct d3dkmt_shareobjectwithhost {
 	_IOWR(0x47, 0x43, struct d3dkmt_querystatistics)
 #define LX_DXSHAREOBJECTWITHHOST	\
 	_IOWR(0x47, 0x44, struct d3dkmt_shareobjectwithhost)
+#define LX_DXCREATESYNCFILE	\
+	_IOWR(0x47, 0x45, struct d3dkmt_createsyncfile)
 
 #endif /* _D3DKMTHK_H */
#32 lore raw

PATCH v4 32/55 drivers: hv: dxgkrnl: Use tracing instead of dev_dbg

In reply to Eric Curtin

From: Iouri Tarassov <[email protected]>

[kms: forward port to 6.6 from 6.1. No code changes made.]
---
 drivers/hv/dxgkrnl/dxgadapter.c |  4 ++--
 drivers/hv/dxgkrnl/dxgmodule.c  |  5 ++++-
 drivers/hv/dxgkrnl/dxgprocess.c |  6 +++---
 drivers/hv/dxgkrnl/dxgvmbus.c   |  4 ++--
 drivers/hv/dxgkrnl/hmgr.c       | 16 ++++++++--------
 drivers/hv/dxgkrnl/ioctl.c      |  8 ++++----
 drivers/hv/dxgkrnl/misc.c       |  4 ++--
 7 files changed, 25 insertions(+), 22 deletions(-)
drivers/hv/dxgkrnl/dxgadapter.c +2 -2
index 236febbc6fca..3d8bec295b87 100644
@@ -18,8 +18,8 @@
 
 #include "dxgkrnl.h"
 
-#undef pr_fmt
-#define pr_fmt(fmt)	"dxgk: " fmt
+#undef dev_fmt
+#define dev_fmt(fmt)	"dxgk: " fmt
 
 int dxgadapter_set_vmbus(struct dxgadapter *adapter, struct hv_device *hdev)
 {
drivers/hv/dxgkrnl/dxgmodule.c +4 -1
index af51fcd35697..08feae97e845 100644
@@ -24,6 +24,9 @@
 #undef pr_fmt
 #define pr_fmt(fmt)	"dxgk: " fmt
 
+#undef dev_fmt
+#define dev_fmt(fmt)	"dxgk: " fmt
+
 /*
  * Interface from dxgglobal
  */
@@ -442,7 +445,7 @@ const struct file_operations dxgk_fops = {
 #define DXGK_VMBUS_HOSTCAPS_OFFSET	(DXGK_VMBUS_VGPU_LUID_OFFSET + \
 					sizeof(struct winluid))
 
-/* The guest writes its capavilities to this adderss */
+/* The guest writes its capabilities to this address */
 #define DXGK_VMBUS_GUESTCAPS_OFFSET	(DXGK_VMBUS_VERSION_OFFSET + \
 					sizeof(u32))
 
drivers/hv/dxgkrnl/dxgprocess.c +3 -3
index 5de3f8ccb448..afef196c0588 100644
@@ -13,8 +13,8 @@
 
 #include "dxgkrnl.h"
 
-#undef pr_fmt
-#define pr_fmt(fmt)	"dxgk: " fmt
+#undef dev_fmt
+#define dev_fmt(fmt)	"dxgk: " fmt
 
 /*
  * Creates a new dxgprocess object
@@ -248,7 +248,7 @@ struct dxgadapter *dxgprocess_adapter_by_handle(struct dxgprocess *process,
 					       HMGRENTRY_TYPE_DXGADAPTER,
 					       handle);
 	if (adapter == NULL)
-		DXG_ERR("adapter_by_handle failed %x", handle.v);
+		DXG_TRACE("adapter_by_handle failed %x", handle.v);
 	else if (kref_get_unless_zero(&adapter->adapter_kref) == 0) {
 		DXG_ERR("failed to acquire adapter reference");
 		adapter = NULL;
drivers/hv/dxgkrnl/dxgvmbus.c +2 -2
index 913ea3cabb31..d53d4254be63 100644
@@ -22,8 +22,8 @@
 #include "dxgkrnl.h"
 #include "dxgvmbus.h"
 
-#undef pr_fmt
-#define pr_fmt(fmt)	"dxgk: " fmt
+#undef dev_fmt
+#define dev_fmt(fmt)	"dxgk: " fmt
 
 #define RING_BUFSIZE (256 * 1024)
 
drivers/hv/dxgkrnl/hmgr.c +8 -8
index 526b50f46d96..24101d0091ab 100644
@@ -19,8 +19,8 @@
 #include "dxgkrnl.h"
 #include "hmgr.h"
 
-#undef pr_fmt
-#define pr_fmt(fmt)	"dxgk: " fmt
+#undef dev_fmt
+#define dev_fmt(fmt)	"dxgk: " fmt
 
 const struct d3dkmthandle zerohandle;
 
@@ -90,29 +90,29 @@ static bool is_handle_valid(struct hmgrtable *table, struct d3dkmthandle h,
 	struct hmgrentry *entry;
 
 	if (index >= table->table_size) {
-		DXG_ERR("Invalid index %x %d", h.v, index);
+		DXG_TRACE("Invalid index %x %d", h.v, index);
 		return false;
 	}
 
 	entry = &table->entry_table[index];
 	if (unique != entry->unique) {
-		DXG_ERR("Invalid unique %x %d %d %d %p",
+		DXG_TRACE("Invalid unique %x %d %d %d %p",
 			h.v, unique, entry->unique, index, entry->object);
 		return false;
 	}
 
 	if (entry->destroyed && !ignore_destroyed) {
-		DXG_ERR("Invalid destroyed value");
+		DXG_TRACE("Invalid destroyed value");
 		return false;
 	}
 
 	if (entry->type == HMGRENTRY_TYPE_FREE) {
-		DXG_ERR("Entry is freed %x %d", h.v, index);
+		DXG_TRACE("Entry is freed %x %d", h.v, index);
 		return false;
 	}
 
 	if (t != HMGRENTRY_TYPE_FREE && t != entry->type) {
-		DXG_ERR("type mismatch %x %d %d", h.v, t, entry->type);
+		DXG_TRACE("type mismatch %x %d %d", h.v, t, entry->type);
 		return false;
 	}
 
@@ -500,7 +500,7 @@ void *hmgrtable_get_object_by_type(struct hmgrtable *table,
 				   struct d3dkmthandle h)
 {
 	if (!is_handle_valid(table, h, false, type)) {
-		DXG_ERR("Invalid handle %x", h.v);
+		DXG_TRACE("Invalid handle %x", h.v);
 		return NULL;
 	}
 	return table->entry_table[get_index(h)].object;
drivers/hv/dxgkrnl/ioctl.c +4 -4
index 6c26aafb0619..4db23cd55b24 100644
@@ -21,8 +21,8 @@
 #include "dxgvmbus.h"
 #include "dxgsyncfile.h"
 
-#undef pr_fmt
-#define pr_fmt(fmt)	"dxgk: " fmt
+#undef dev_fmt
+#define dev_fmt(fmt)	"dxgk: " fmt
 
 struct ioctl_desc {
 	int (*ioctl_callback)(struct dxgprocess *p, void __user *arg);
@@ -556,7 +556,7 @@ dxgkio_enum_adapters3(struct dxgprocess *process, void *__user inargs)
 
 cleanup:
 
-	DXG_TRACE("ioctl: %s %d", errorstr(ret), ret);
+	DXG_TRACE("ioctl:%s %d", errorstr(ret), ret);
 	return ret;
 }
 
@@ -5242,7 +5242,7 @@ static int dxgk_ioctl(struct file *f, unsigned int p1, unsigned long p2)
 	int status;
 	struct dxgprocess *process;
 
-	if (code < 1 ||  code >= ARRAY_SIZE(ioctls)) {
+	if (code < 1 || code >= ARRAY_SIZE(ioctls)) {
 		DXG_ERR("bad ioctl %x %x %x %x",
 			code, _IOC_TYPE(p1), _IOC_SIZE(p1), _IOC_DIR(p1));
 		return -ENOTTY;
drivers/hv/dxgkrnl/misc.c +2 -2
index 4a1309d80ee5..4bf6fe80d22a 100644
@@ -18,8 +18,8 @@
 #include "dxgkrnl.h"
 #include "misc.h"
 
-#undef pr_fmt
-#define pr_fmt(fmt)	"dxgk: " fmt
+#undef dev_fmt
+#define dev_fmt(fmt)	"dxgk: " fmt
 
 u16 *wcsncpy(u16 *dest, const u16 *src, size_t n)
 {
#33 lore raw

PATCH v4 33/55 drivers: hv: dxgkrnl: Implement D3DKMTWaitSyncFile

In reply to Eric Curtin

From: Iouri Tarassov <[email protected]>

[kms: forward port to 6.6 from 6.1. No code changes made.]
---
 drivers/hv/dxgkrnl/dxgkrnl.h     |  11 ++
 drivers/hv/dxgkrnl/dxgmodule.c   |   7 +-
 drivers/hv/dxgkrnl/dxgprocess.c  |  12 +-
 drivers/hv/dxgkrnl/dxgsyncfile.c | 291 ++++++++++++++++++++++++++++++-
 drivers/hv/dxgkrnl/dxgsyncfile.h |   3 +
 drivers/hv/dxgkrnl/dxgvmbus.c    |  49 ++++++
 drivers/hv/dxgkrnl/ioctl.c       |  16 +-
 include/uapi/misc/d3dkmthk.h     |  23 +++
 8 files changed, 396 insertions(+), 16 deletions(-)
drivers/hv/dxgkrnl/dxgkrnl.h +11 -0
index 3a69e3b34e1c..d92e1348ccfb 100644
@@ -254,6 +254,10 @@ void dxgsharedsyncobj_add_syncobj(struct dxgsharedsyncobject *sharedsyncobj,
 				  struct dxgsyncobject *syncobj);
 void dxgsharedsyncobj_remove_syncobj(struct dxgsharedsyncobject *sharedsyncobj,
 				     struct dxgsyncobject *syncobj);
+int dxgsharedsyncobj_get_host_nt_handle(struct dxgsharedsyncobject *syncobj,
+					struct dxgprocess *process,
+					struct d3dkmthandle objecthandle);
+void dxgsharedsyncobj_put(struct dxgsharedsyncobject *syncobj);
 
 struct dxgsyncobject *dxgsyncobject_create(struct dxgprocess *process,
 					   struct dxgdevice *device,
@@ -384,6 +388,8 @@ struct dxgprocess {
 	pid_t			tgid;
 	/* how many time the process was opened */
 	struct kref		process_kref;
+	/* protects the object memory */
+	struct kref		process_mem_kref;
 	/*
 	 * This handle table is used for all objects except dxgadapter
 	 * The handle table lock order is higher than the local_handle_table
@@ -405,6 +411,7 @@ struct dxgprocess {
 struct dxgprocess *dxgprocess_create(void);
 void dxgprocess_destroy(struct dxgprocess *process);
 void dxgprocess_release(struct kref *refcount);
+void dxgprocess_mem_release(struct kref *refcount);
 int dxgprocess_open_adapter(struct dxgprocess *process,
 					struct dxgadapter *adapter,
 					struct d3dkmthandle *handle);
@@ -932,6 +939,10 @@ int dxgvmb_send_open_sync_object_nt(struct dxgprocess *process,
 				    struct d3dkmt_opensyncobjectfromnthandle2
 				    *args,
 				    struct dxgsyncobject *syncobj);
+int dxgvmb_send_open_sync_object(struct dxgprocess *process,
+				struct d3dkmthandle device,
+				struct d3dkmthandle host_shared_syncobj,
+				struct d3dkmthandle *syncobj);
 int dxgvmb_send_query_alloc_residency(struct dxgprocess *process,
 				      struct dxgadapter *adapter,
 				      struct d3dkmt_queryallocationresidency
drivers/hv/dxgkrnl/dxgmodule.c +5 -2
index 08feae97e845..5570f35954d4 100644
@@ -149,10 +149,11 @@ void dxgglobal_remove_host_event(struct dxghostevent *event)
 	spin_unlock_irq(&dxgglobal->host_event_list_mutex);
 }
 
-static void signal_dma_fence(struct dxghostevent *eventhdr)
+static void dxg_signal_dma_fence(struct dxghostevent *eventhdr)
 {
 	struct dxgsyncpoint *event = (struct dxgsyncpoint *)eventhdr;
 
+	DXG_TRACE("syncpoint: %px, fence: %lld", event, event->fence_value);
 	event->fence_value++;
 	list_del(&eventhdr->host_event_list_entry);
 	dma_fence_signal(&event->base);
@@ -198,7 +199,7 @@ void dxgglobal_signal_host_event(u64 event_id)
 			if (event->event_type == dxghostevent_cpu_event)
 				signal_host_cpu_event(event);
 			else if (event->event_type == dxghostevent_dma_fence)
-				signal_dma_fence(event);
+				dxg_signal_dma_fence(event);
 			else
 				DXG_ERR("Unknown host event type");
 			break;
@@ -355,6 +356,7 @@ static struct dxgprocess *dxgglobal_get_current_process(void)
 		if (entry->tgid == current->tgid) {
 			if (kref_get_unless_zero(&entry->process_kref)) {
 				process = entry;
+				kref_get(&entry->process_mem_kref);
 				DXG_TRACE("found dxgprocess");
 			} else {
 				DXG_TRACE("process is destroyed");
@@ -405,6 +407,7 @@ static int dxgk_release(struct inode *n, struct file *f)
 		return -EINVAL;
 
 	kref_put(&process->process_kref, dxgprocess_release);
+	kref_put(&process->process_mem_kref, dxgprocess_mem_release);
 
 	f->private_data = NULL;
 	return 0;
drivers/hv/dxgkrnl/dxgprocess.c +11 -1
index afef196c0588..e77e3a4983f8 100644
@@ -39,6 +39,7 @@ struct dxgprocess *dxgprocess_create(void)
 		} else {
 			INIT_LIST_HEAD(&process->plistentry);
 			kref_init(&process->process_kref);
+			kref_init(&process->process_mem_kref);
 
 			mutex_lock(&dxgglobal->plistmutex);
 			list_add_tail(&process->plistentry,
@@ -117,8 +118,17 @@ void dxgprocess_release(struct kref *refcount)
 
 	dxgprocess_destroy(process);
 
-	if (process->host_handle.v)
+	if (process->host_handle.v) {
 		dxgvmb_send_destroy_process(process->host_handle);
+		process->host_handle.v = 0;
+	}
+}
+
+void dxgprocess_mem_release(struct kref *refcount)
+{
+	struct dxgprocess *process;
+
+	process = container_of(refcount, struct dxgprocess, process_mem_kref);
 	kfree(process);
 }
 
drivers/hv/dxgkrnl/dxgsyncfile.c +282 -9

342 lines not on this page · raw message

drivers/hv/dxgkrnl/dxgsyncfile.h +3 -0
index 207ef9b30f67..292b7f718987 100644
@@ -17,10 +17,13 @@
 #include <linux/sync_file.h>
 
 int dxgkio_create_sync_file(struct dxgprocess *process, void *__user inargs);
+int dxgkio_wait_sync_file(struct dxgprocess *process, void *__user inargs);
+int dxgkio_open_syncobj_from_syncfile(struct dxgprocess *p, void *__user args);
 
 struct dxgsyncpoint {
 	struct dxghostevent	hdr;
 	struct dma_fence	base;
+	struct dxgsharedsyncobject *shared_syncobj;
 	u64			fence_value;
 	u64			context;
 	spinlock_t		lock;
drivers/hv/dxgkrnl/dxgvmbus.c +49 -0

55 lines not on this page · raw message

drivers/hv/dxgkrnl/ioctl.c +12 -4
index 4db23cd55b24..622904d5c3a9 100644
@@ -36,10 +36,8 @@ static char *errorstr(int ret)
 }
 #endif
 
-static int dxgsyncobj_release(struct inode *inode, struct file *file)
+void dxgsharedsyncobj_put(struct dxgsharedsyncobject *syncobj)
 {
-	struct dxgsharedsyncobject *syncobj = file->private_data;
-
 	DXG_TRACE("Release syncobj: %p", syncobj);
 	mutex_lock(&syncobj->fd_mutex);
 	kref_get(&syncobj->ssyncobj_kref);
@@ -56,6 +54,13 @@ static int dxgsyncobj_release(struct inode *inode, struct file *file)
 	}
 	mutex_unlock(&syncobj->fd_mutex);
 	kref_put(&syncobj->ssyncobj_kref, dxgsharedsyncobj_release);
+}
+
+static int dxgsyncobj_release(struct inode *inode, struct file *file)
+{
+	struct dxgsharedsyncobject *syncobj = file->private_data;
+
+	dxgsharedsyncobj_put(syncobj);
 	return 0;
 }
 
@@ -4478,7 +4483,7 @@ dxgkio_get_device_state(struct dxgprocess *process, void *__user inargs)
 	return ret;
 }
 
-static int
+int
 dxgsharedsyncobj_get_host_nt_handle(struct dxgsharedsyncobject *syncobj,
 				    struct dxgprocess *process,
 				    struct d3dkmthandle objecthandle)
@@ -5226,6 +5231,9 @@ static struct ioctl_desc ioctls[] = {
 /* 0x43 */	{dxgkio_query_statistics, LX_DXQUERYSTATISTICS},
 /* 0x44 */	{dxgkio_share_object_with_host, LX_DXSHAREOBJECTWITHHOST},
 /* 0x45 */	{dxgkio_create_sync_file, LX_DXCREATESYNCFILE},
+/* 0x46 */	{dxgkio_wait_sync_file, LX_DXWAITSYNCFILE},
+/* 0x46 */	{dxgkio_open_syncobj_from_syncfile,
+		 LX_DXOPENSYNCOBJECTFROMSYNCFILE},
 };
 
 /*
include/uapi/misc/d3dkmthk.h +23 -0
index c7f168425dc7..1eaa3f038322 100644
@@ -1561,6 +1561,25 @@ struct d3dkmt_createsyncfile {
 	__u64			sync_file_handle;	/* out */
 };
 
+struct d3dkmt_waitsyncfile {
+	__u64			sync_file_handle;
+	struct d3dkmthandle	context;
+	__u32			reserved;
+};
+
+struct d3dkmt_opensyncobjectfromsyncfile {
+	__u64			sync_file_handle;
+	struct d3dkmthandle	device;
+	struct d3dkmthandle	syncobj;	/* out */
+	__u64			fence_value;	/* out */
+#ifdef __KERNEL__
+	void			*fence_value_cpu_va;	/* out */
+#else
+	__u64			fence_value_cpu_va;	/* out */
+#endif
+	__u64			fence_value_gpu_va;	/* out */
+};
+
 /*
  * Dxgkrnl Graphics Port Driver ioctl definitions
  *
@@ -1686,5 +1705,9 @@ struct d3dkmt_createsyncfile {
 	_IOWR(0x47, 0x44, struct d3dkmt_shareobjectwithhost)
 #define LX_DXCREATESYNCFILE	\
 	_IOWR(0x47, 0x45, struct d3dkmt_createsyncfile)
+#define LX_DXWAITSYNCFILE	\
+	_IOWR(0x47, 0x46, struct d3dkmt_waitsyncfile)
+#define LX_DXOPENSYNCOBJECTFROMSYNCFILE	\
+	_IOWR(0x47, 0x47, struct d3dkmt_opensyncobjectfromsyncfile)
 
 #endif /* _D3DKMTHK_H */
#34 lore raw

PATCH v4 34/55 drivers: hv: dxgkrnl: Improve tracing and return values from copy from user

In reply to Eric Curtin

From: Iouri Tarassov <[email protected]>

[kms: forward port to 6.6 from 6.1. No code changes made.]
---
 drivers/hv/dxgkrnl/dxgkrnl.h     |  17 +-
 drivers/hv/dxgkrnl/dxgmodule.c   |   1 +
 drivers/hv/dxgkrnl/dxgsyncfile.c |  13 +-
 drivers/hv/dxgkrnl/dxgvmbus.c    |  98 ++++-----
 drivers/hv/dxgkrnl/ioctl.c       | 327 +++++++++++++++----------------
 5 files changed, 225 insertions(+), 231 deletions(-)
drivers/hv/dxgkrnl/dxgkrnl.h +12 -5
index d92e1348ccfb..f63aa6f7a9dc 100644
@@ -999,18 +999,25 @@ void dxgk_validate_ioctls(void);
 	trace_printk(dev_fmt(fmt) "\n", ##__VA_ARGS__);	\
 }  while (0)
 
-#define DXG_ERR(fmt, ...) do {				\
-	dev_err(DXGDEV, fmt, ##__VA_ARGS__);		\
-	trace_printk("*** dxgkerror *** " dev_fmt(fmt) "\n", ##__VA_ARGS__);	\
+#define DXG_ERR(fmt, ...) do {					\
+	dev_err(DXGDEV, "%s: " fmt, __func__, ##__VA_ARGS__);	\
+	trace_printk("*** dxgkerror *** " dev_fmt(fmt) "\n", ##__VA_ARGS__); \
 } while (0)
 
 #else
 
 #define DXG_TRACE(...)
-#define DXG_ERR(fmt, ...) do {			\
-	dev_err(DXGDEV, fmt, ##__VA_ARGS__);	\
+#define DXG_ERR(fmt, ...) do {					\
+	dev_err(DXGDEV, "%s: " fmt, __func__, ##__VA_ARGS__);	\
 } while (0)
 
 #endif /* DEBUG */
 
+#define DXG_TRACE_IOCTL_END(ret)  do {			\
+	if (ret < 0)					\
+		DXG_ERR("Ioctl failed: %d", ret);	\
+	else						\
+		DXG_TRACE("Ioctl returned: %d", ret);	\
+} while (0)
+
 #endif
drivers/hv/dxgkrnl/dxgmodule.c +1 -0
index 5570f35954d4..aa27931a3447 100644
@@ -961,3 +961,4 @@ module_exit(dxg_drv_exit);
 
 MODULE_LICENSE("GPL");
 MODULE_DESCRIPTION("Microsoft Dxgkrnl virtual compute device Driver");
+MODULE_VERSION("2.0.0");
drivers/hv/dxgkrnl/dxgsyncfile.c +3 -10
index 9d5832c90ad7..f3b3e8dd4568 100644
@@ -38,13 +38,6 @@
 #undef dev_fmt
 #define dev_fmt(fmt)	"dxgk: " fmt
 
-#ifdef DEBUG
-static char *errorstr(int ret)
-{
-	return ret < 0 ? "err" : "";
-}
-#endif
-
 static const struct dma_fence_ops dxgdmafence_ops;
 
 static struct dxgsyncpoint *to_syncpoint(struct dma_fence *fence)
@@ -193,7 +186,7 @@ int dxgkio_create_sync_file(struct dxgprocess *process, void *__user inargs)
 		if (fd >= 0)
 			put_unused_fd(fd);
 	}
-	DXG_TRACE("ioctl:%s %d", errorstr(ret), ret);
+	DXG_TRACE_IOCTL_END(ret);
 	return ret;
 }
 
@@ -317,7 +310,7 @@ int dxgkio_open_syncobj_from_syncfile(struct dxgprocess *process,
 		kref_put(&device->device_kref, dxgdevice_release);
 	}
 
-	DXG_TRACE("ioctl:%s %d", errorstr(ret), ret);
+	DXG_TRACE_IOCTL_END(ret);
 	return ret;
 }
 
@@ -415,7 +408,7 @@ int dxgkio_wait_sync_file(struct dxgprocess *process, void *__user inargs)
 	if (dmafence)
 		dma_fence_put(dmafence);
 
-	DXG_TRACE("ioctl:%s %d", errorstr(ret), ret);
+	DXG_TRACE_IOCTL_END(ret);
 	return ret;
 }
 
drivers/hv/dxgkrnl/dxgvmbus.c +49 -49

392 lines not on this page · raw message

drivers/hv/dxgkrnl/ioctl.c +160 -167

1,290 lines not on this page · raw message

#35 lore raw

PATCH v4 35/55 drivers: hv: dxgkrnl: Fix synchronization locks

In reply to Eric Curtin

From: Iouri Tarassov <[email protected]>

[kms: forward port to 6.6 from 6.1. No code changes made.]
---
 drivers/hv/dxgkrnl/dxgadapter.c | 19 ++++----
 drivers/hv/dxgkrnl/dxgkrnl.h    |  8 +++-
 drivers/hv/dxgkrnl/dxgmodule.c  |  3 +-
 drivers/hv/dxgkrnl/dxgprocess.c | 11 +++--
 drivers/hv/dxgkrnl/dxgvmbus.c   | 85 +++++++++++++++++++++++----------
 drivers/hv/dxgkrnl/ioctl.c      | 24 ++++++----
 drivers/hv/dxgkrnl/misc.h       |  1 +
 7 files changed, 101 insertions(+), 50 deletions(-)
drivers/hv/dxgkrnl/dxgadapter.c +11 -8
index 3d8bec295b87..d9d45bd4a31e 100644
@@ -136,7 +136,7 @@ void dxgadapter_release(struct kref *refcount)
 	struct dxgadapter *adapter;
 
 	adapter = container_of(refcount, struct dxgadapter, adapter_kref);
-	DXG_TRACE("%p", adapter);
+	DXG_TRACE("Destroying adapter: %px", adapter);
 	kfree(adapter);
 }
 
@@ -270,6 +270,8 @@ struct dxgdevice *dxgdevice_create(struct dxgadapter *adapter,
 		if (ret < 0) {
 			kref_put(&device->device_kref, dxgdevice_release);
 			device = NULL;
+		} else {
+			DXG_TRACE("dxgdevice created: %px", device);
 		}
 	}
 	return device;
@@ -413,11 +415,8 @@ void dxgdevice_destroy(struct dxgdevice *device)
 
 cleanup:
 
-	if (device->adapter) {
+	if (device->adapter)
 		dxgprocess_adapter_remove_device(device);
-		kref_put(&device->adapter->adapter_kref, dxgadapter_release);
-		device->adapter = NULL;
-	}
 
 	up_write(&device->device_lock);
 
@@ -721,6 +720,8 @@ void dxgdevice_release(struct kref *refcount)
 	struct dxgdevice *device;
 
 	device = container_of(refcount, struct dxgdevice, device_kref);
+	DXG_TRACE("Destroying device: %px", device);
+	kref_put(&device->adapter->adapter_kref, dxgadapter_release);
 	kfree(device);
 }
 
@@ -999,6 +1000,9 @@ void dxgpagingqueue_destroy(struct dxgpagingqueue *pqueue)
 	kfree(pqueue);
 }
 
+/*
+ * Process_adapter_mutex is held.
+ */
 struct dxgprocess_adapter *dxgprocess_adapter_create(struct dxgprocess *process,
 						     struct dxgadapter *adapter)
 {
@@ -1108,7 +1112,7 @@ int dxgprocess_adapter_add_device(struct dxgprocess *process,
 
 void dxgprocess_adapter_remove_device(struct dxgdevice *device)
 {
-	DXG_TRACE("Removing device: %p", device);
+	DXG_TRACE("Removing device: %px", device);
 	mutex_lock(&device->adapter_info->device_list_mutex);
 	if (device->device_list_entry.next) {
 		list_del(&device->device_list_entry);
@@ -1147,8 +1151,7 @@ void dxgsharedsyncobj_release(struct kref *refcount)
 	if (syncobj->adapter) {
 		dxgadapter_remove_shared_syncobj(syncobj->adapter,
 							syncobj);
-		kref_put(&syncobj->adapter->adapter_kref,
-				dxgadapter_release);
+		kref_put(&syncobj->adapter->adapter_kref, dxgadapter_release);
 	}
 	kfree(syncobj);
 }
drivers/hv/dxgkrnl/dxgkrnl.h +7 -1
index f63aa6f7a9dc..1b40d6e39085 100644
@@ -404,7 +404,10 @@ struct dxgprocess {
 	/* Handle of the corresponding objec on the host */
 	struct d3dkmthandle	host_handle;
 
-	/* List of opened adapters (dxgprocess_adapter) */
+	/*
+	 * List of opened adapters (dxgprocess_adapter).
+	 * Protected by process_adapter_mutex.
+	 */
 	struct list_head	process_adapter_list_head;
 };
 
@@ -451,6 +454,8 @@ enum dxgadapter_state {
 struct dxgadapter {
 	struct rw_semaphore	core_lock;
 	struct kref		adapter_kref;
+	/* Protects creation and destruction of dxgdevice objects */
+	struct mutex		device_creation_lock;
 	/* Entry in the list of adapters in dxgglobal */
 	struct list_head	adapter_list_entry;
 	/* The list of dxgprocess_adapter entries */
@@ -997,6 +1002,7 @@ void dxgk_validate_ioctls(void);
 
 #define DXG_TRACE(fmt, ...)  do {			\
 	trace_printk(dev_fmt(fmt) "\n", ##__VA_ARGS__);	\
+	dev_dbg(DXGDEV, "%s: " fmt, __func__, ##__VA_ARGS__);	\
 }  while (0)
 
 #define DXG_ERR(fmt, ...) do {					\
drivers/hv/dxgkrnl/dxgmodule.c +2 -1
index aa27931a3447..f419597f711a 100644
@@ -272,6 +272,7 @@ int dxgglobal_create_adapter(struct pci_dev *dev, guid_t *guid,
 	adapter->host_vgpu_luid = host_vgpu_luid;
 	kref_init(&adapter->adapter_kref);
 	init_rwsem(&adapter->core_lock);
+	mutex_init(&adapter->device_creation_lock);
 
 	INIT_LIST_HEAD(&adapter->adapter_process_list_head);
 	INIT_LIST_HEAD(&adapter->shared_resource_list_head);
@@ -961,4 +962,4 @@ module_exit(dxg_drv_exit);
 
 MODULE_LICENSE("GPL");
 MODULE_DESCRIPTION("Microsoft Dxgkrnl virtual compute device Driver");
-MODULE_VERSION("2.0.0");
+MODULE_VERSION("2.0.1");
drivers/hv/dxgkrnl/dxgprocess.c +6 -5
index e77e3a4983f8..fd51fd968049 100644
@@ -214,14 +214,15 @@ int dxgprocess_close_adapter(struct dxgprocess *process,
 	hmgrtable_unlock(&process->local_handle_table, DXGLOCK_EXCL);
 
 	if (adapter) {
+		mutex_lock(&adapter->device_creation_lock);
+		dxgglobal_acquire_process_adapter_lock();
 		adapter_info = dxgprocess_get_adapter_info(process, adapter);
-		if (adapter_info) {
-			dxgglobal_acquire_process_adapter_lock();
+		if (adapter_info)
 			dxgprocess_adapter_release(adapter_info);
-			dxgglobal_release_process_adapter_lock();
-		} else {
+		else
 			ret = -EINVAL;
-		}
+		dxgglobal_release_process_adapter_lock();
+		mutex_unlock(&adapter->device_creation_lock);
 	} else {
 		DXG_ERR("Adapter not found %x", handle.v);
 		ret = -EINVAL;
drivers/hv/dxgkrnl/dxgvmbus.c +59 -26

136 lines not on this page · raw message

drivers/hv/dxgkrnl/ioctl.c +15 -9
index 3dc9e76f4f3d..7c72790f917f 100644
@@ -636,6 +636,7 @@ dxgkio_create_device(struct dxgprocess *process, void *__user inargs)
 	struct dxgdevice *device = NULL;
 	struct d3dkmthandle host_device_handle = {};
 	bool adapter_locked = false;
+	bool device_creation_locked = false;
 
 	ret = copy_from_user(&args, inargs, sizeof(args));
 	if (ret) {
@@ -651,6 +652,9 @@ dxgkio_create_device(struct dxgprocess *process, void *__user inargs)
 		goto cleanup;
 	}
 
+	mutex_lock(&adapter->device_creation_lock);
+	device_creation_locked = true;
+
 	device = dxgdevice_create(adapter, process);
 	if (device == NULL) {
 		ret = -ENOMEM;
@@ -699,6 +703,9 @@ dxgkio_create_device(struct dxgprocess *process, void *__user inargs)
 	if (adapter_locked)
 		dxgadapter_release_lock_shared(adapter);
 
+	if (device_creation_locked)
+		mutex_unlock(&adapter->device_creation_lock);
+
 	if (adapter)
 		kref_put(&adapter->adapter_kref, dxgadapter_release);
 
@@ -803,22 +810,21 @@ dxgkio_create_context_virtual(struct dxgprocess *process, void *__user inargs)
 	host_context_handle = dxgvmb_send_create_context(adapter,
 							 process, &args);
 	if (host_context_handle.v) {
-		hmgrtable_lock(&process->handle_table, DXGLOCK_EXCL);
-		ret = hmgrtable_assign_handle(&process->handle_table, context,
-					      HMGRENTRY_TYPE_DXGCONTEXT,
-					      host_context_handle);
-		if (ret >= 0)
-			context->handle = host_context_handle;
-		hmgrtable_unlock(&process->handle_table, DXGLOCK_EXCL);
-		if (ret < 0)
-			goto cleanup;
 		ret = copy_to_user(&((struct d3dkmt_createcontextvirtual *)
 				   inargs)->context, &host_context_handle,
 				   sizeof(struct d3dkmthandle));
 		if (ret) {
 			DXG_ERR("failed to copy context handle");
 			ret = -EFAULT;
+			goto cleanup;
 		}
+		hmgrtable_lock(&process->handle_table, DXGLOCK_EXCL);
+		ret = hmgrtable_assign_handle(&process->handle_table, context,
+					      HMGRENTRY_TYPE_DXGCONTEXT,
+					      host_context_handle);
+		if (ret >= 0)
+			context->handle = host_context_handle;
+		hmgrtable_unlock(&process->handle_table, DXGLOCK_EXCL);
 	} else {
 		DXG_ERR("invalid host handle");
 		ret = -EINVAL;
drivers/hv/dxgkrnl/misc.h +1 -0
index ee2ebfdd1c13..9fcab4ae2c0c 100644
@@ -38,6 +38,7 @@ extern const struct d3dkmthandle zerohandle;
  * core_lock (dxgadapter lock)
  * device_lock (dxgdevice lock)
  * process_adapter_mutex
+ * device_creation_lock in dxgadapter
  * adapter_list_lock
  * device_mutex (dxgglobal mutex)
  */
#36 lore raw

PATCH v4 36/55 drivers: hv: dxgkrnl: Close shared file objects in case of a failure

In reply to Eric Curtin

From: Iouri Tarassov <[email protected]>

[kms: forward port to 6.6 from 6.1. No code changes made.]
---
 drivers/hv/dxgkrnl/ioctl.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
drivers/hv/dxgkrnl/ioctl.c +10 -4
index 7c72790f917f..69324510c9e2 100644
@@ -4536,7 +4536,7 @@ enum dxg_sharedobject_type {
 };
 
 static int get_object_fd(enum dxg_sharedobject_type type,
-				     void *object, int *fdout)
+			void *object, int *fdout, struct file **filp)
 {
 	struct file *file;
 	int fd;
@@ -4565,8 +4565,8 @@ static int get_object_fd(enum dxg_sharedobject_type type,
 		return -ENOTRECOVERABLE;
 	}
 
-	fd_install(fd, file);
 	*fdout = fd;
+	*filp = file;
 	return 0;
 }
 
@@ -4581,6 +4581,7 @@ dxgkio_share_objects(struct dxgprocess *process, void *__user inargs)
 	struct dxgsharedresource *shared_resource = NULL;
 	struct d3dkmthandle *handles = NULL;
 	int object_fd = -1;
+	struct file *filp = NULL;
 	void *obj = NULL;
 	u32 handle_size;
 	int ret;
@@ -4660,7 +4661,7 @@ dxgkio_share_objects(struct dxgprocess *process, void *__user inargs)
 	switch (object_type) {
 	case HMGRENTRY_TYPE_DXGSYNCOBJECT:
 		ret = get_object_fd(DXG_SHARED_SYNCOBJECT, shared_syncobj,
-				    &object_fd);
+				    &object_fd, &filp);
 		if (ret < 0) {
 			DXG_ERR("get_object_fd failed for sync object");
 			goto cleanup;
@@ -4675,7 +4676,7 @@ dxgkio_share_objects(struct dxgprocess *process, void *__user inargs)
 		break;
 	case HMGRENTRY_TYPE_DXGRESOURCE:
 		ret = get_object_fd(DXG_SHARED_RESOURCE, shared_resource,
-				    &object_fd);
+				    &object_fd, &filp);
 		if (ret < 0) {
 			DXG_ERR("get_object_fd failed for resource");
 			goto cleanup;
@@ -4708,10 +4709,15 @@ dxgkio_share_objects(struct dxgprocess *process, void *__user inargs)
 	if (ret) {
 		DXG_ERR("failed to copy shared handle");
 		ret = -EFAULT;
+		goto cleanup;
 	}
 
+	fd_install(object_fd, filp);
+
 cleanup:
 	if (ret < 0) {
+		if (filp)
+			fput(filp);
 		if (object_fd >= 0)
 			put_unused_fd(object_fd);
 	}
#37 lore raw

PATCH v4 37/55 drivers: hv: dxgkrnl: Added missed NULL check for resource object

In reply to Eric Curtin

From: Iouri Tarassov <[email protected]>

[kms: forward port to 6.6 from 6.1. No code changes made.]
---
 drivers/hv/dxgkrnl/ioctl.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
drivers/hv/dxgkrnl/ioctl.c +6 -4
index 69324510c9e2..98350583943e 100644
@@ -1589,7 +1589,8 @@ dxgkio_create_allocation(struct dxgprocess *process, void *__user inargs)
 				&process->handle_table,
 				HMGRENTRY_TYPE_DXGRESOURCE,
 				args.resource);
-			kref_get(&resource->resource_kref);
+			if (resource != NULL)
+				kref_get(&resource->resource_kref);
 			dxgprocess_ht_lock_shared_up(process);
 
 			if (resource == NULL || resource->device != device) {
@@ -1693,10 +1694,8 @@ dxgkio_create_allocation(struct dxgprocess *process, void *__user inargs)
 					    &standard_alloc);
 cleanup:
 
-	if (resource_mutex_acquired) {
+	if (resource_mutex_acquired)
 		mutex_unlock(&resource->resource_mutex);
-		kref_put(&resource->resource_kref, dxgresource_release);
-	}
 	if (ret < 0) {
 		if (dxgalloc) {
 			for (i = 0; i < args.alloc_count; i++) {
@@ -1727,6 +1726,9 @@ dxgkio_create_allocation(struct dxgprocess *process, void *__user inargs)
 	if (adapter)
 		dxgadapter_release_lock_shared(adapter);
 
+	if (resource && !args.flags.create_resource)
+		kref_put(&resource->resource_kref, dxgresource_release);
+
 	if (device) {
 		dxgdevice_release_lock_shared(device);
 		kref_put(&device->device_kref, dxgdevice_release);
#38 lore raw

PATCH v4 38/55 drivers: hv: dxgkrnl: Fixed dxgkrnl to build for the 6.1 kernel

In reply to Eric Curtin

From: Iouri Tarassov <[email protected]>

Definition for GPADL was changed from u32 to struct vmbus_gpadl.

[kms: forward port to 6.6 from 6.1. No code changes made.]
---
 drivers/hv/dxgkrnl/dxgadapter.c | 8 --------
 drivers/hv/dxgkrnl/dxgkrnl.h    | 4 ----
 drivers/hv/dxgkrnl/dxgvmbus.c   | 8 --------
 3 files changed, 20 deletions(-)
drivers/hv/dxgkrnl/dxgadapter.c +0 -8
index d9d45bd4a31e..bcd19b7267d1 100644
@@ -927,19 +927,11 @@ void dxgallocation_destroy(struct dxgallocation *alloc)
 					       alloc->owner.device,
 					       &args, &alloc->alloc_handle);
 	}
-#ifdef _MAIN_KERNEL_
 	if (alloc->gpadl.gpadl_handle) {
 		DXG_TRACE("Teardown gpadl %d", alloc->gpadl.gpadl_handle);
 		vmbus_teardown_gpadl(dxgglobal_get_vmbus(), &alloc->gpadl);
 		alloc->gpadl.gpadl_handle = 0;
 	}
-#else
-	if (alloc->gpadl) {
-		DXG_TRACE("Teardown gpadl %d", alloc->gpadl);
-		vmbus_teardown_gpadl(dxgglobal_get_vmbus(), alloc->gpadl);
-		alloc->gpadl = 0;
-	}
-#endif
 	if (alloc->priv_drv_data)
 		vfree(alloc->priv_drv_data);
 	if (alloc->cpu_address_mapped)
drivers/hv/dxgkrnl/dxgkrnl.h +0 -4
index 1b40d6e39085..c5ed23cb90df 100644
@@ -728,11 +728,7 @@ struct dxgallocation {
 	u32				cached:1;
 	u32				handle_valid:1;
 	/* GPADL address list for existing sysmem allocations */
-#ifdef _MAIN_KERNEL_
 	struct vmbus_gpadl		gpadl;
-#else
-	u32				gpadl;
-#endif
 	/* Number of pages in the 'pages' array */
 	u32				num_pages;
 	/*
drivers/hv/dxgkrnl/dxgvmbus.c +0 -8
index 8c99f141482e..eb3f4c5153a6 100644
@@ -1493,22 +1493,14 @@ int create_existing_sysmem(struct dxgdevice *device,
 			ret = -ENOMEM;
 			goto cleanup;
 		}
-#ifdef _MAIN_KERNEL_
 		DXG_TRACE("New gpadl %d", dxgalloc->gpadl.gpadl_handle);
-#else
-		DXG_TRACE("New gpadl %d", dxgalloc->gpadl);
-#endif
 
 		command_vgpu_to_host_init2(&set_store_command->hdr,
 					DXGK_VMBCOMMAND_SETEXISTINGSYSMEMSTORE,
 					device->process->host_handle);
 		set_store_command->device = device->handle;
 		set_store_command->allocation = host_alloc->allocation;
-#ifdef _MAIN_KERNEL_
 		set_store_command->gpadl = dxgalloc->gpadl.gpadl_handle;
-#else
-		set_store_command->gpadl = dxgalloc->gpadl;
-#endif
 		ret = dxgvmb_send_sync_msg_ntstatus(msg.channel, msg.hdr,
 						    msg.size);
 		if (ret < 0)
#39 lore raw

PATCH v4 39/55 drivers: hv: dxgkrnl: Added support for compute only adapters

In reply to Eric Curtin

From: Iouri Tarassov <[email protected]>

[kms: forward port to 6.6 from 6.1. No code changes made.]
---
 drivers/hv/dxgkrnl/dxgkrnl.h   |  1 +
 drivers/hv/dxgkrnl/dxgmodule.c | 11 ++++++++++-
 drivers/hv/dxgkrnl/dxgvmbus.c  |  1 +
 drivers/hv/dxgkrnl/ioctl.c     |  4 ++++
 4 files changed, 16 insertions(+), 1 deletion(-)
drivers/hv/dxgkrnl/dxgkrnl.h +1 -0
index c5ed23cb90df..d20489317c0b 100644
@@ -478,6 +478,7 @@ struct dxgadapter {
 	struct winluid		luid;	/* VM bus channel luid */
 	u16			device_description[80];
 	u16			device_instance_id[WIN_MAX_PATH];
+	bool			compute_only;
 	bool			stopping_adapter;
 };
 
drivers/hv/dxgkrnl/dxgmodule.c +10 -1
index f419597f711a..0fafb6167229 100644
@@ -20,6 +20,7 @@
 
 #define PCI_VENDOR_ID_MICROSOFT		0x1414
 #define PCI_DEVICE_ID_VIRTUAL_RENDER	0x008E
+#define PCI_DEVICE_ID_COMPUTE_ACCELERATOR	0x008A
 
 #undef pr_fmt
 #define pr_fmt(fmt)	"dxgk: " fmt
@@ -270,6 +271,8 @@ int dxgglobal_create_adapter(struct pci_dev *dev, guid_t *guid,
 
 	adapter->adapter_state = DXGADAPTER_STATE_WAITING_VMBUS;
 	adapter->host_vgpu_luid = host_vgpu_luid;
+	if (dev->device == PCI_DEVICE_ID_COMPUTE_ACCELERATOR)
+		adapter->compute_only = true;
 	kref_init(&adapter->adapter_kref);
 	init_rwsem(&adapter->core_lock);
 	mutex_init(&adapter->device_creation_lock);
@@ -622,6 +625,12 @@ static struct pci_device_id dxg_pci_id_table[] = {
 		.subvendor = PCI_ANY_ID,
 		.subdevice = PCI_ANY_ID
 	},
+	{
+		.vendor = PCI_VENDOR_ID_MICROSOFT,
+		.device = PCI_DEVICE_ID_COMPUTE_ACCELERATOR,
+		.subvendor = PCI_ANY_ID,
+		.subdevice = PCI_ANY_ID
+	},
 	{ 0 }
 };
 
@@ -962,4 +971,4 @@ module_exit(dxg_drv_exit);
 
 MODULE_LICENSE("GPL");
 MODULE_DESCRIPTION("Microsoft Dxgkrnl virtual compute device Driver");
-MODULE_VERSION("2.0.1");
+MODULE_VERSION("2.0.2");
drivers/hv/dxgkrnl/dxgvmbus.c +1 -0
index eb3f4c5153a6..5f17efc937c3 100644
@@ -3774,6 +3774,7 @@ int dxgvmb_send_query_adapter_info(struct dxgprocess *process,
 			adapter_type->indirect_display_device = 0;
 			adapter_type->acg_supported = 0;
 			adapter_type->support_set_timings_from_vidpn = 0;
+			adapter_type->compute_only = !!adapter->compute_only;
 			break;
 		}
 	default:
drivers/hv/dxgkrnl/ioctl.c +4 -0
index 98350583943e..f735b18fcc14 100644
@@ -254,6 +254,8 @@ dxgkp_enum_adapters(struct dxgprocess *process,
 
 	list_for_each_entry(entry, &dxgglobal->adapter_list_head,
 			    adapter_list_entry) {
+		if (entry->compute_only && !filter.include_compute_only)
+			continue;
 		if (dxgadapter_acquire_lock_shared(entry) == 0) {
 			struct d3dkmt_adapterinfo *inf = &info[adapter_count];
 
@@ -474,6 +476,8 @@ dxgkio_enum_adapters(struct dxgprocess *process, void *__user inargs)
 
 	list_for_each_entry(entry, &dxgglobal->adapter_list_head,
 			    adapter_list_entry) {
+		if (entry->compute_only)
+			continue;
 		if (dxgadapter_acquire_lock_shared(entry) == 0) {
 			struct d3dkmt_adapterinfo *inf = &info[adapter_count];
 
#40 lore raw

PATCH v4 40/55 drivers: hv: dxgkrnl: Added implementation for D3DKMTInvalidateCache

In reply to Eric Curtin

From: Iouri Tarassov <[email protected]>

D3DKMTInvalidateCache is called by user mode drivers when the device doesn't support cache coherent access to compute device allocations. It needs to be called after an allocation was accessed by CPU and now needs to be accessed by the device. And vice versa.

[kms: forward port to 6.6 from 6.1. No code changes made.]
---
 drivers/hv/dxgkrnl/dxgkrnl.h  |  3 +++
 drivers/hv/dxgkrnl/dxgvmbus.c | 27 +++++++++++++++++++
 drivers/hv/dxgkrnl/dxgvmbus.h | 11 ++++++++
 drivers/hv/dxgkrnl/ioctl.c    | 49 +++++++++++++++++++++++++++++++++--
 include/uapi/misc/d3dkmthk.h  |  9 +++++++
 5 files changed, 97 insertions(+), 2 deletions(-)
drivers/hv/dxgkrnl/dxgkrnl.h +3 -0
index d20489317c0b..e7d8919b3c01 100644
@@ -989,6 +989,9 @@ int dxgvmb_send_async_msg(struct dxgvmbuschannel *channel,
 			  u32 cmd_size);
 int dxgvmb_send_share_object_with_host(struct dxgprocess *process,
 				struct d3dkmt_shareobjectwithhost *args);
+int dxgvmb_send_invalidate_cache(struct dxgprocess *process,
+				struct dxgadapter *adapter,
+				struct d3dkmt_invalidatecache *args);
 
 void signal_host_cpu_event(struct dxghostevent *eventhdr);
 int ntstatus2int(struct ntstatus status);
drivers/hv/dxgkrnl/dxgvmbus.c +27 -0
index 5f17efc937c3..487804ca731a 100644
@@ -2021,6 +2021,33 @@ int dxgvmb_send_flush_heap_transitions(struct dxgprocess *process,
 	return ret;
 }
 
+int dxgvmb_send_invalidate_cache(struct dxgprocess *process,
+				struct dxgadapter *adapter,
+				struct d3dkmt_invalidatecache *args)
+{
+	struct dxgkvmb_command_invalidatecache *command;
+	int ret;
+	struct dxgvmbusmsg msg = {.hdr = NULL};
+
+	ret = init_message(&msg, adapter, process, sizeof(*command));
+	if (ret)
+		goto cleanup;
+	command = (void *)msg.msg;
+	command_vgpu_to_host_init2(&command->hdr,
+				   DXGK_VMBCOMMAND_INVALIDATECACHE,
+				   process->host_handle);
+	command->device = args->device;
+	command->allocation = args->allocation;
+	command->offset = args->offset;
+	command->length = args->length;
+	ret = dxgvmb_send_sync_msg_ntstatus(msg.channel, msg.hdr, msg.size);
+cleanup:
+	free_message(&msg, process);
+	if (ret)
+		DXG_TRACE("err: %d", ret);
+	return ret;
+}
+
 int dxgvmb_send_query_alloc_residency(struct dxgprocess *process,
 				      struct dxgadapter *adapter,
 				      struct d3dkmt_queryallocationresidency
drivers/hv/dxgkrnl/dxgvmbus.h +11 -0
index b4a98f7c2522..20c562b485de 100644
@@ -125,6 +125,7 @@ enum dxgkvmb_commandtype {
 	DXGK_VMBCOMMAND_QUERYRESOURCEINFO	= 64,
 	DXGK_VMBCOMMAND_LOGEVENT		= 65,
 	DXGK_VMBCOMMAND_SETEXISTINGSYSMEMPAGES	= 66,
+	DXGK_VMBCOMMAND_INVALIDATECACHE		= 67,
 	DXGK_VMBCOMMAND_INVALID
 };
 
@@ -428,6 +429,16 @@ struct dxgkvmb_command_flushheaptransitions {
 	struct dxgkvmb_command_vgpu_to_host hdr;
 };
 
+/* Returns  ntstatus */
+struct dxgkvmb_command_invalidatecache {
+	struct dxgkvmb_command_vgpu_to_host hdr;
+	struct d3dkmthandle device;
+	struct d3dkmthandle allocation;
+	u64 offset;
+	u64 length;
+	u64 reserved;
+};
+
 struct dxgkvmb_command_freegpuvirtualaddress {
 	struct dxgkvmb_command_vgpu_to_host hdr;
 	struct d3dkmt_freegpuvirtualaddress args;
drivers/hv/dxgkrnl/ioctl.c +47 -2

73 lines not on this page · raw message

include/uapi/misc/d3dkmthk.h +9 -0
index 1eaa3f038322..84fa07a46d3c 100644
@@ -1580,6 +1580,13 @@ struct d3dkmt_opensyncobjectfromsyncfile {
 	__u64			fence_value_gpu_va;	/* out */
 };
 
+struct d3dkmt_invalidatecache {
+	struct d3dkmthandle	device;
+	struct d3dkmthandle	allocation;
+	__u64			offset;
+	__u64			length;
+};
+
 /*
  * Dxgkrnl Graphics Port Driver ioctl definitions
  *
@@ -1647,6 +1654,8 @@ struct d3dkmt_opensyncobjectfromsyncfile {
 	_IOWR(0x47, 0x21, struct d3dkmt_getcontextinprocessschedulingpriority)
 #define LX_DXGETCONTEXTSCHEDULINGPRIORITY \
 	_IOWR(0x47, 0x22, struct d3dkmt_getcontextschedulingpriority)
+#define LX_DXINVALIDATECACHE \
+	_IOWR(0x47, 0x24, struct d3dkmt_invalidatecache)
 #define LX_DXLOCK2			\
 	_IOWR(0x47, 0x25, struct d3dkmt_lock2)
 #define LX_DXMARKDEVICEASERROR		\
#41 lore raw

PATCH v4 41/55 drivers: hv: dxgkrnl: Handle process ID in D3DKMTQueryStatistics

In reply to Eric Curtin

From: Iouri Tarassov <[email protected]>

When D3DKMTQueryStatistics specifies a non-zero process ID, it needs to be translated to the host process handle before sending a message to the host.

[kms: forward port to 6.6 from 6.1. No code changes made.]
---
 drivers/hv/dxgkrnl/dxgkrnl.h    |   3 +-
 drivers/hv/dxgkrnl/dxgprocess.c |   2 +
 drivers/hv/dxgkrnl/dxgvmbus.c   | 140 ++++++++++++++++----------------
 drivers/hv/dxgkrnl/ioctl.c      |  39 ++++++++-
 4 files changed, 111 insertions(+), 73 deletions(-)
drivers/hv/dxgkrnl/dxgkrnl.h +2 -1
index e7d8919b3c01..6af1e59b0a31 100644
@@ -386,6 +386,7 @@ struct dxgprocess {
 	struct list_head	plistentry;
 	pid_t			pid;
 	pid_t			tgid;
+	pid_t			vpid; /* pdi from the current namespace */
 	/* how many time the process was opened */
 	struct kref		process_kref;
 	/* protects the object memory */
@@ -981,7 +982,7 @@ int dxgvmb_send_get_stdalloc_data(struct dxgdevice *device,
 				  void *prive_alloc_data,
 				  u32 *res_priv_data_size,
 				  void *priv_res_data);
-int dxgvmb_send_query_statistics(struct dxgprocess *process,
+int dxgvmb_send_query_statistics(struct d3dkmthandle host_process_handle,
 				 struct dxgadapter *adapter,
 				 struct d3dkmt_querystatistics *args);
 int dxgvmb_send_async_msg(struct dxgvmbuschannel *channel,
drivers/hv/dxgkrnl/dxgprocess.c +2 -0
index fd51fd968049..5a4c4cb0c2e8 100644
@@ -12,6 +12,7 @@
  */
 
 #include "dxgkrnl.h"
+#include "linux/sched.h"
 
 #undef dev_fmt
 #define dev_fmt(fmt)	"dxgk: " fmt
@@ -31,6 +32,7 @@ struct dxgprocess *dxgprocess_create(void)
 		DXG_TRACE("new dxgprocess created");
 		process->pid = current->pid;
 		process->tgid = current->tgid;
+		process->vpid = task_pid_vnr(current);
 		ret = dxgvmb_send_create_process(process);
 		if (ret < 0) {
 			DXG_TRACE("send_create_process failed");
drivers/hv/dxgkrnl/dxgvmbus.c +70 -70

559 lines not on this page · raw message

drivers/hv/dxgkrnl/ioctl.c +37 -2
index 56b838a87f09..466bef6c14b3 100644
@@ -147,6 +147,23 @@ static int dxgkio_open_adapter_from_luid(struct dxgprocess *process,
 	return ret;
 }
 
+static struct d3dkmthandle find_dxgprocess_handle(u64 pid)
+{
+	struct dxgglobal *dxgglobal = dxggbl();
+	struct dxgprocess *entry;
+	struct d3dkmthandle host_handle = {};
+
+	mutex_lock(&dxgglobal->plistmutex);
+	list_for_each_entry(entry, &dxgglobal->plisthead, plistentry) {
+		if (entry->vpid == pid) {
+			host_handle.v = entry->host_handle.v;
+			break;
+		}
+	}
+	mutex_unlock(&dxgglobal->plistmutex);
+	return host_handle;
+}
+
 static int dxgkio_query_statistics(struct dxgprocess *process,
 				void __user *inargs)
 {
@@ -156,6 +173,8 @@ static int dxgkio_query_statistics(struct dxgprocess *process,
 	struct dxgadapter *adapter = NULL;
 	struct winluid tmp;
 	struct dxgglobal *dxgglobal = dxggbl();
+	struct d3dkmthandle host_process_handle = process->host_handle;
+	u64 pid;
 
 	args = vzalloc(sizeof(struct d3dkmt_querystatistics));
 	if (args == NULL) {
@@ -170,6 +189,18 @@ static int dxgkio_query_statistics(struct dxgprocess *process,
 		goto cleanup;
 	}
 
+	/* Find the host process handle when needed */
+	pid = args->process;
+	if (pid) {
+		host_process_handle = find_dxgprocess_handle(pid);
+		if (host_process_handle.v == 0) {
+			DXG_ERR("Invalid process ID is specified: %lld", pid);
+			ret = -EINVAL;
+			goto cleanup;
+		}
+		args->process = 0;
+	}
+
 	dxgglobal_acquire_adapter_list_lock(DXGLOCK_SHARED);
 	list_for_each_entry(entry, &dxgglobal->adapter_list_head,
 			    adapter_list_entry) {
@@ -186,7 +217,8 @@ static int dxgkio_query_statistics(struct dxgprocess *process,
 	if (adapter) {
 		tmp = args->adapter_luid;
 		args->adapter_luid = adapter->host_adapter_luid;
-		ret = dxgvmb_send_query_statistics(process, adapter, args);
+		ret = dxgvmb_send_query_statistics(host_process_handle, adapter,
+						   args);
 		if (ret >= 0) {
 			args->adapter_luid = tmp;
 			ret = copy_to_user(inargs, args, sizeof(*args));
@@ -280,7 +312,10 @@ dxgkp_enum_adapters(struct dxgprocess *process,
 	dxgglobal_release_adapter_list_lock(DXGLOCK_SHARED);
 
 	if (adapter_count > adapter_count_max) {
-		ret = STATUS_BUFFER_TOO_SMALL;
+		struct ntstatus status;
+
+		status.v = STATUS_BUFFER_TOO_SMALL;
+		ret = ntstatus2int(status);
 		DXG_TRACE("Too many adapters");
 		ret = copy_to_user(adapter_count_out,
 				   &dxgglobal->num_adapters, sizeof(u32));
#42 lore raw

PATCH v4 42/55 drivers: hv: dxgkrnl: Implement the D3DKMTEnumProcesses API

In reply to Eric Curtin

From: Iouri Tarassov <[email protected]>

D3DKMTEnumProcesses is used to enumerate PIDs for all processes, which opened the /dev/dxg device.

[kms: forward port to 6.6 from 6.1. No code changes made.]
---
 drivers/hv/dxgkrnl/dxgkrnl.h    |  1 +
 drivers/hv/dxgkrnl/dxgprocess.c |  2 +
 drivers/hv/dxgkrnl/ioctl.c      | 81 +++++++++++++++++++++++++++++++++
 include/uapi/misc/d3dkmthk.h    | 12 +++++
 4 files changed, 96 insertions(+)
drivers/hv/dxgkrnl/dxgkrnl.h +1 -0
index 6af1e59b0a31..90bcd5377744 100644
@@ -387,6 +387,7 @@ struct dxgprocess {
 	pid_t			pid;
 	pid_t			tgid;
 	pid_t			vpid; /* pdi from the current namespace */
+	struct pid_namespace	*nspid; /* namespace id */
 	/* how many time the process was opened */
 	struct kref		process_kref;
 	/* protects the object memory */
drivers/hv/dxgkrnl/dxgprocess.c +2 -0
index 5a4c4cb0c2e8..9bfd53df1a54 100644
@@ -13,6 +13,7 @@
 
 #include "dxgkrnl.h"
 #include "linux/sched.h"
+#include <linux/pid_namespace.h>
 
 #undef dev_fmt
 #define dev_fmt(fmt)	"dxgk: " fmt
@@ -33,6 +34,7 @@ struct dxgprocess *dxgprocess_create(void)
 		process->pid = current->pid;
 		process->tgid = current->tgid;
 		process->vpid = task_pid_vnr(current);
+		process->nspid = task_active_pid_ns(current);
 		ret = dxgvmb_send_create_process(process);
 		if (ret < 0) {
 			DXG_TRACE("send_create_process failed");
drivers/hv/dxgkrnl/ioctl.c +81 -0

99 lines not on this page · raw message

include/uapi/misc/d3dkmthk.h +12 -0
index 84fa07a46d3c..f9f817060fa9 100644
@@ -1580,6 +1580,16 @@ struct d3dkmt_opensyncobjectfromsyncfile {
 	__u64			fence_value_gpu_va;	/* out */
 };
 
+ struct d3dkmt_enumprocesses {
+	struct winluid 		adapter_luid;
+#ifdef __KERNEL__
+	__u32			*buffer;
+#else
+	__u64			buffer;
+#endif
+	__u64			buffer_count;
+};
+
 struct d3dkmt_invalidatecache {
 	struct d3dkmthandle	device;
 	struct d3dkmthandle	allocation;
@@ -1718,5 +1728,7 @@ struct d3dkmt_invalidatecache {
 	_IOWR(0x47, 0x46, struct d3dkmt_waitsyncfile)
 #define LX_DXOPENSYNCOBJECTFROMSYNCFILE	\
 	_IOWR(0x47, 0x47, struct d3dkmt_opensyncobjectfromsyncfile)
+#define LX_DXENUMPROCESSES	\
+	_IOWR(0x47, 0x48, struct d3dkmt_enumprocesses)
 
 #endif /* _D3DKMTHK_H */
#43 lore raw

PATCH v4 43/55 drivers: hv: dxgkrnl: Implement D3DDKMTIsFeatureEnabled API

In reply to Eric Curtin

From: Iouri Tarassov <[email protected]>

D3DKMTIsFeatureEnabled is used to query if a particular feature is supported by the given adapter.

[kms: forward port to 6.6 from 6.1. No code changes made.]
---
 drivers/hv/dxgkrnl/dxgkrnl.h  |  2 ++
 drivers/hv/dxgkrnl/dxgvmbus.c | 58 ++++++++++++++++++++++++++++++++---
 drivers/hv/dxgkrnl/dxgvmbus.h | 31 +++++++++++++++++++
 drivers/hv/dxgkrnl/ioctl.c    | 46 +++++++++++++++++++++++++++
 include/uapi/misc/d3dkmthk.h  | 31 ++++++++++++++++++-
 5 files changed, 163 insertions(+), 5 deletions(-)
drivers/hv/dxgkrnl/dxgkrnl.h +2 -0
index 90bcd5377744..ebf81cffa289 100644
@@ -994,6 +994,8 @@ int dxgvmb_send_share_object_with_host(struct dxgprocess *process,
 int dxgvmb_send_invalidate_cache(struct dxgprocess *process,
 				struct dxgadapter *adapter,
 				struct d3dkmt_invalidatecache *args);
+int dxgvmb_send_is_feature_enabled(struct dxgadapter *adapter,
+				   struct d3dkmt_isfeatureenabled *args);
 
 void signal_host_cpu_event(struct dxghostevent *eventhdr);
 int ntstatus2int(struct ntstatus status);
drivers/hv/dxgkrnl/dxgvmbus.c +54 -4

75 lines not on this page · raw message

drivers/hv/dxgkrnl/dxgvmbus.h +31 -0
index 20c562b485de..a7e625b2f896 100644
@@ -48,6 +48,7 @@ enum dxgkvmb_commandtype_global {
 	DXGK_VMBCOMMAND_SETIOSPACEREGION	= 1010,
 	DXGK_VMBCOMMAND_COMPLETETRANSACTION	= 1011,
 	DXGK_VMBCOMMAND_SHAREOBJECTWITHHOST	= 1021,
+	DXGK_VMBCOMMAND_ISFEATUREENABLED_GLOBAL	= 1022,
 	DXGK_VMBCOMMAND_INVALID_VM_TO_HOST
 };
 
@@ -126,6 +127,7 @@ enum dxgkvmb_commandtype {
 	DXGK_VMBCOMMAND_LOGEVENT		= 65,
 	DXGK_VMBCOMMAND_SETEXISTINGSYSMEMPAGES	= 66,
 	DXGK_VMBCOMMAND_INVALIDATECACHE		= 67,
+	DXGK_VMBCOMMAND_ISFEATUREENABLED	= 68,
 	DXGK_VMBCOMMAND_INVALID
 };
 
@@ -871,6 +873,35 @@ struct dxgkvmb_command_shareobjectwithhost_return {
 	u64		vail_nt_handle;
 };
 
+struct dxgk_feature_desc {
+	u16 min_supported_version;
+	u16 max_supported_version;
+	struct {
+		u16 supported		: 1;
+		u16 virtualization_mode : 3;
+		u16 global 		: 1;
+		u16 driver_feature	: 1;
+		u16 internal		: 1;
+		u16 reserved		: 9;
+	};
+};
+
+struct  dxgkvmb_command_isfeatureenabled {
+	struct dxgkvmb_command_vgpu_to_host	hdr;
+	enum dxgk_feature_id			feature_id;
+};
+
+struct  dxgkvmb_command_isfeatureenabled_gbl {
+	struct dxgkvmb_command_vm_to_host	hdr;
+	enum dxgk_feature_id			feature_id;
+};
+
+struct dxgkvmb_command_isfeatureenabled_return {
+	struct ntstatus				status;
+	struct dxgk_feature_desc		descriptor;
+	struct dxgk_isfeatureenabled_result	result;
+};
+
 int
 dxgvmb_send_sync_msg(struct dxgvmbuschannel *channel,
 		     void *command, u32 command_size, void *result,
drivers/hv/dxgkrnl/ioctl.c +46 -0

58 lines not on this page · raw message

include/uapi/misc/d3dkmthk.h +30 -1
index f9f817060fa9..5b345ddaf66e 100644
@@ -1580,7 +1580,7 @@ struct d3dkmt_opensyncobjectfromsyncfile {
 	__u64			fence_value_gpu_va;	/* out */
 };
 
- struct d3dkmt_enumprocesses {
+struct d3dkmt_enumprocesses {
 	struct winluid 		adapter_luid;
 #ifdef __KERNEL__
 	__u32			*buffer;
@@ -1590,6 +1590,33 @@ struct d3dkmt_opensyncobjectfromsyncfile {
 	__u64			buffer_count;
 };
 
+enum dxgk_feature_id {
+	_DXGK_FEATURE_HWSCH				= 0,
+	_DXGK_FEATURE_PAGE_BASED_MEMORY_MANAGER		= 32,
+	_DXGK_FEATURE_KERNEL_MODE_TESTING		= 33,
+	_DXGK_FEATURE_MAX
+};
+
+struct dxgk_isfeatureenabled_result {
+	__u16	version;
+	union {
+		struct {
+			__u16 enabled			:  1;
+			__u16 known_feature		:  1;
+			__u16 supported_by_driver	:  1;
+			__u16 supported_on_config	:  1;
+			__u16 reserved			: 12;
+		};
+		__u16 value;
+	};
+};
+
+struct d3dkmt_isfeatureenabled {
+	struct d3dkmthandle			adapter;
+	enum dxgk_feature_id			feature_id;
+	struct dxgk_isfeatureenabled_result	result;
+};
+
 struct d3dkmt_invalidatecache {
 	struct d3dkmthandle	device;
 	struct d3dkmthandle	allocation;
@@ -1730,5 +1757,7 @@ struct d3dkmt_invalidatecache {
 	_IOWR(0x47, 0x47, struct d3dkmt_opensyncobjectfromsyncfile)
 #define LX_DXENUMPROCESSES	\
 	_IOWR(0x47, 0x48, struct d3dkmt_enumprocesses)
+#define LX_ISFEATUREENABLED	\
+	_IOWR(0x47, 0x49, struct d3dkmt_isfeatureenabled)
 
 #endif /* _D3DKMTHK_H */
#44 lore raw

PATCH v4 44/55 drivers: hv: dxgkrnl: Implement known escapes

In reply to Eric Curtin

From: Iouri Tarassov <[email protected]>

Implement an escape to build test command buffer. Implement other known escapes.

[kms: forward port to 6.6 from 6.1. No code changes made.]
---
 drivers/hv/dxgkrnl/dxgkrnl.h  |   3 +-
 drivers/hv/dxgkrnl/dxgvmbus.c |  40 +++++---
 drivers/hv/dxgkrnl/ioctl.c    | 170 +++++++++++++++++++++++++++++-----
 include/uapi/misc/d3dkmthk.h  |  31 +++++++
 4 files changed, 205 insertions(+), 39 deletions(-)
drivers/hv/dxgkrnl/dxgkrnl.h +2 -1
index ebf81cffa289..9599ec8e0f1d 100644
@@ -953,7 +953,8 @@ int dxgvmb_send_query_alloc_residency(struct dxgprocess *process,
 				      *args);
 int dxgvmb_send_escape(struct dxgprocess *process,
 		       struct dxgadapter *adapter,
-		       struct d3dkmt_escape *args);
+		       struct d3dkmt_escape *args,
+		       bool user_mode);
 int dxgvmb_send_query_vidmem_info(struct dxgprocess *process,
 				  struct dxgadapter *adapter,
 				  struct d3dkmt_queryvideomemoryinfo *args,
drivers/hv/dxgkrnl/dxgvmbus.c +26 -14
index 2436e1a7bc73..de28c6162a70 100644
@@ -2174,7 +2174,8 @@ int dxgvmb_send_query_alloc_residency(struct dxgprocess *process,
 
 int dxgvmb_send_escape(struct dxgprocess *process,
 		       struct dxgadapter *adapter,
-		       struct d3dkmt_escape *args)
+		       struct d3dkmt_escape *args,
+		       bool user_mode)
 {
 	int ret;
 	struct dxgkvmb_command_escape *command = NULL;
@@ -2203,13 +2204,18 @@ int dxgvmb_send_escape(struct dxgprocess *process,
 	command->priv_drv_data_size = args->priv_drv_data_size;
 	command->context = args->context;
 	if (args->priv_drv_data_size) {
-		ret = copy_from_user(command->priv_drv_data,
-				     args->priv_drv_data,
-				     args->priv_drv_data_size);
-		if (ret) {
-			DXG_ERR("failed to copy priv data");
-			ret = -EFAULT;
-			goto cleanup;
+		if (user_mode) {
+			ret = copy_from_user(command->priv_drv_data,
+					args->priv_drv_data,
+					args->priv_drv_data_size);
+			if (ret) {
+				DXG_ERR("failed to copy priv data");
+				ret = -EFAULT;
+				goto cleanup;
+			}
+		} else {
+			memcpy(command->priv_drv_data, args->priv_drv_data,
+				args->priv_drv_data_size);
 		}
 	}
 
@@ -2220,12 +2226,18 @@ int dxgvmb_send_escape(struct dxgprocess *process,
 		goto cleanup;
 
 	if (args->priv_drv_data_size) {
-		ret = copy_to_user(args->priv_drv_data,
-				   command->priv_drv_data,
-				   args->priv_drv_data_size);
-		if (ret) {
-			DXG_ERR("failed to copy priv data");
-			ret = -EINVAL;
+		if (user_mode) {
+			ret = copy_to_user(args->priv_drv_data,
+					command->priv_drv_data,
+					args->priv_drv_data_size);
+			if (ret) {
+				DXG_ERR("failed to copy priv data");
+				ret = -EINVAL;
+			}
+		} else {
+			memcpy(args->priv_drv_data,
+				command->priv_drv_data,
+				args->priv_drv_data_size);
 		}
 	}
 
drivers/hv/dxgkrnl/ioctl.c +146 -24

236 lines not on this page · raw message

include/uapi/misc/d3dkmthk.h +31 -0
index 5b345ddaf66e..db40e8ff40b0 100644
@@ -237,6 +237,37 @@ struct d3dddi_destroypagingqueue {
 	struct d3dkmthandle		paging_queue;
 };
 
+enum d3dddi_knownescapetype {
+	_D3DDDI_DRIVERESCAPETYPE_TRANSLATEALLOCATIONHANDLE	= 0,
+	_D3DDDI_DRIVERESCAPETYPE_TRANSLATERESOURCEHANDLE	= 1,
+	_D3DDDI_DRIVERESCAPETYPE_CPUEVENTUSAGE			= 2,
+	_D3DDDI_DRIVERESCAPETYPE_BUILDTESTCOMMANDBUFFER		= 3,
+};
+
+struct d3dddi_translate_allocation_handle {
+	enum d3dddi_knownescapetype	escape_type;
+	struct d3dkmthandle		allocation;
+};
+
+struct d3dddi_testcommand {
+	char buffer[72];
+};
+
+#define D3DDDI_MAXTESTBUFFERSIZE 4096
+#define D3DDDI_MAXTESTBUFFERPRIVATEDRIVERDATASIZE 1024
+
+struct d3dddi_buildtestcommandbuffer {
+	enum d3dddi_knownescapetype	escape_type;
+	struct d3dkmthandle		device;
+	struct d3dkmthandle		context;
+	__u32				flags;
+	struct d3dddi_testcommand	command;
+	void				*dma_buffer;
+	void				*dma_buffer_priv_data;
+	__u32				dma_buffer_size;
+	__u32				dma_buffer_priv_data_size;
+};
+
 enum d3dkmt_escapetype {
 	_D3DKMT_ESCAPE_DRIVERPRIVATE	= 0,
 	_D3DKMT_ESCAPE_VIDMM		= 1,
#45 lore raw

PATCH v4 45/55 drivers: hv: dxgkrnl: Fixed coding style issues

In reply to Eric Curtin

From: Iouri Tarassov <[email protected]>

[kms: forward port to 6.6 from 6.1. No code changes made.]
---
 drivers/hv/dxgkrnl/dxgadapter.c | 12 ++++--------
 drivers/hv/dxgkrnl/dxgkrnl.h    |  6 +++---
 drivers/hv/dxgkrnl/dxgvmbus.c   |  2 +-
 drivers/hv/dxgkrnl/ioctl.c      | 20 +++++++-------------
 4 files changed, 15 insertions(+), 25 deletions(-)
drivers/hv/dxgkrnl/dxgadapter.c +4 -8
index bcd19b7267d1..b8ae8099847b 100644
@@ -1017,8 +1017,7 @@ struct dxgprocess_adapter *dxgprocess_adapter_create(struct dxgprocess *process,
 	}
 	return adapter_info;
 cleanup:
-	if (adapter_info)
-		kfree(adapter_info);
+	kfree(adapter_info);
 	return NULL;
 }
 
@@ -1225,10 +1224,8 @@ struct dxgsyncobject *dxgsyncobject_create(struct dxgprocess *process,
 	DXG_TRACE("Syncobj created: %p", syncobj);
 	return syncobj;
 cleanup:
-	if (syncobj->host_event)
-		kfree(syncobj->host_event);
-	if (syncobj)
-		kfree(syncobj);
+	kfree(syncobj->host_event);
+	kfree(syncobj);
 	return NULL;
 }
 
@@ -1308,8 +1305,7 @@ void dxgsyncobject_release(struct kref *refcount)
 		kref_put(&syncobj->shared_owner->ssyncobj_kref,
 			 dxgsharedsyncobj_release);
 	}
-	if (syncobj->host_event)
-		kfree(syncobj->host_event);
+	kfree(syncobj->host_event);
 	kfree(syncobj);
 }
 
drivers/hv/dxgkrnl/dxgkrnl.h +3 -3
index 9599ec8e0f1d..a4d0c504668b 100644
@@ -47,10 +47,10 @@ struct dxghwqueue;
  * Driver private data.
  * A single /dev/dxg device is created per virtual machine.
  */
-struct dxgdriver{
+struct dxgdriver {
 	struct dxgglobal	*dxgglobal;
-	struct device 		*dxgdev;
-	struct pci_driver 	pci_drv;
+	struct device		*dxgdev;
+	struct pci_driver	pci_drv;
 	struct hv_driver	vmbus_drv;
 };
 extern struct dxgdriver dxgdrv;
drivers/hv/dxgkrnl/dxgvmbus.c +1 -1
index de28c6162a70..215e2f6648e2 100644
@@ -246,7 +246,7 @@ int dxgvmbuschannel_init(struct dxgvmbuschannel *ch, struct hv_device *hdev)
 		goto cleanup;
 	}
 
-#if LINUX_VERSION_CODE >= KERNEL_VERSION(5,15,0)
+#if KERNEL_VERSION(5, 15, 0) <= LINUX_VERSION_CODE
 	hdev->channel->max_pkt_size = DXG_MAX_VM_BUS_PACKET_SIZE;
 #endif
 	ret = vmbus_open(hdev->channel, RING_BUFSIZE, RING_BUFSIZE,
drivers/hv/dxgkrnl/ioctl.c +7 -13
index f8ca79d098f3..5ac6dd1f09b9 100644
@@ -3162,8 +3162,7 @@ dxgkio_signal_sync_object(struct dxgprocess *process, void *__user inargs)
 		}
 		if (event)
 			eventfd_ctx_put(event);
-		if (host_event)
-			kfree(host_event);
+		kfree(host_event);
 	}
 	if (adapter)
 		dxgadapter_release_lock_shared(adapter);
@@ -3398,8 +3397,7 @@ dxgkio_signal_sync_object_gpu2(struct dxgprocess *process, void *__user inargs)
 		}
 		if (event)
 			eventfd_ctx_put(event);
-		if (host_event)
-			kfree(host_event);
+		kfree(host_event);
 	}
 	if (adapter)
 		dxgadapter_release_lock_shared(adapter);
@@ -3577,8 +3575,7 @@ dxgkio_wait_sync_object_cpu(struct dxgprocess *process, void *__user inargs)
 		}
 		if (event)
 			eventfd_ctx_put(event);
-		if (async_host_event)
-			kfree(async_host_event);
+		kfree(async_host_event);
 	}
 
 	DXG_TRACE_IOCTL_END(ret);
@@ -4438,7 +4435,7 @@ build_test_command_buffer(struct dxgprocess *process,
 	if (cmd.dma_buffer_size < sizeof(u32) ||
 	    cmd.dma_buffer_size > D3DDDI_MAXTESTBUFFERSIZE ||
 	    cmd.dma_buffer_priv_data_size >
-	    	D3DDDI_MAXTESTBUFFERPRIVATEDRIVERDATASIZE) {
+		D3DDDI_MAXTESTBUFFERPRIVATEDRIVERDATASIZE) {
 		DXG_ERR("Invalid DMA buffer or private data size");
 		return -EINVAL;
 	}
@@ -4511,8 +4508,7 @@ driver_known_escape(struct dxgprocess *process,
 	enum d3dkmt_escapetype escape_type;
 	int ret = 0;
 
-	if (args->priv_drv_data_size < sizeof(enum d3dddi_knownescapetype))
-	{
+	if (args->priv_drv_data_size < sizeof(enum d3dddi_knownescapetype)) {
 		DXG_ERR("Invalid private data size");
 		return -EINVAL;
 	}
@@ -5631,10 +5627,8 @@ void dxgk_validate_ioctls(void)
 {
 	int i;
 
-	for (i=0; i < ARRAY_SIZE(ioctls); i++)
-	{
-		if (ioctls[i].ioctl && _IOC_NR(ioctls[i].ioctl) != i)
-		{
+	for (i = 0; i < ARRAY_SIZE(ioctls); i++) {
+		if (ioctls[i].ioctl && _IOC_NR(ioctls[i].ioctl) != i) {
 			DXG_ERR("Invalid ioctl");
 			DXGKRNL_ASSERT(0);
 		}
#46 lore raw

PATCH v4 46/55 drivers: hv: dxgkrnl: Fixed the implementation of D3DKMTQueryClockCalibration

In reply to Eric Curtin

From: Iouri Tarassov <[email protected]>

The result of a VM bus call was not copied to the user output structure.

[kms: forward port to 6.6 from 6.1. No code changes made.]
---
 drivers/hv/dxgkrnl/dxgvmbus.c | 18 ++++++++++--------
 drivers/hv/dxgkrnl/ioctl.c    |  5 -----
 2 files changed, 10 insertions(+), 13 deletions(-)
drivers/hv/dxgkrnl/dxgvmbus.c +10 -8
index 215e2f6648e2..67f55f4bf41d 100644
@@ -1966,14 +1966,16 @@ int dxgvmb_send_query_clock_calibration(struct dxgprocess *process,
 					*__user inargs)
 {
 	struct dxgkvmb_command_queryclockcalibration *command;
-	struct dxgkvmb_command_queryclockcalibration_return result;
+	struct dxgkvmb_command_queryclockcalibration_return *result;
 	int ret;
-	struct dxgvmbusmsg msg = {.hdr = NULL};
+	struct dxgvmbusmsgres msg = {.hdr = NULL};
 
-	ret = init_message(&msg, adapter, process, sizeof(*command));
+	ret = init_message_res(&msg, adapter, sizeof(*command),
+				sizeof(*result));
 	if (ret)
 		goto cleanup;
 	command = (void *)msg.msg;
+	result = msg.res;
 
 	command_vgpu_to_host_init2(&command->hdr,
 				   DXGK_VMBCOMMAND_QUERYCLOCKCALIBRATION,
@@ -1981,20 +1983,20 @@ int dxgvmb_send_query_clock_calibration(struct dxgprocess *process,
 	command->args = *args;
 
 	ret = dxgvmb_send_sync_msg(msg.channel, msg.hdr, msg.size,
-				   &result, sizeof(result));
+				   result, sizeof(*result));
 	if (ret < 0)
 		goto cleanup;
-	ret = copy_to_user(&inargs->clock_data, &result.clock_data,
-			   sizeof(result.clock_data));
+	ret = copy_to_user(&inargs->clock_data, &result->clock_data,
+			   sizeof(result->clock_data));
 	if (ret) {
 		DXG_ERR("failed to copy clock data");
 		ret = -EFAULT;
 		goto cleanup;
 	}
-	ret = ntstatus2int(result.status);
+	ret = ntstatus2int(result->status);
 
 cleanup:
-	free_message(&msg);
+	free_message((struct dxgvmbusmsg *)&msg);
 	if (ret)
 		DXG_TRACE("err: %d", ret);
 	return ret;
drivers/hv/dxgkrnl/ioctl.c +0 -5
index 5ac6dd1f09b9..d91af2e176e9 100644
@@ -4303,11 +4303,6 @@ dxgkio_query_clock_calibration(struct dxgprocess *process, void *__user inargs)
 						  &args, inargs);
 	if (ret < 0)
 		goto cleanup;
-	ret = copy_to_user(inargs, &args, sizeof(args));
-	if (ret) {
-		DXG_ERR("failed to copy output args");
-		ret = -EFAULT;
-	}
 
 cleanup:
 
#47 lore raw

PATCH v4 47/55 drivers: hv: dxgkrnl: Retry sending a VM bus packet when there is no place in the ring buffer

In reply to Eric Curtin

From: Iouri Tarassov <[email protected]>

When D3DKMT requests are sent too quickly, the VM bus ring buffer could be full when a message is submitted. The change adds sleep and re-try count to handle this condition.

[kms: forward port to 6.6 from 6.1. No code changes made.]
---
 drivers/hv/dxgkrnl/dxgvmbus.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)
drivers/hv/dxgkrnl/dxgvmbus.c +14 -3
index 67f55f4bf41d..467e7707c8c7 100644
@@ -420,6 +420,7 @@ int dxgvmb_send_sync_msg(struct dxgvmbuschannel *channel,
 	struct dxgvmbuspacket *packet = NULL;
 	struct dxgkvmb_command_vm_to_host *cmd1;
 	struct dxgkvmb_command_vgpu_to_host *cmd2;
+	int try_count = 0;
 
 	if (cmd_size > DXG_MAX_VM_BUS_PACKET_SIZE ||
 	    result_size > DXG_MAX_VM_BUS_PACKET_SIZE) {
@@ -453,9 +454,19 @@ int dxgvmb_send_sync_msg(struct dxgvmbuschannel *channel,
 	list_add_tail(&packet->packet_list_entry, &channel->packet_list_head);
 	spin_unlock_irq(&channel->packet_list_mutex);
 
-	ret = vmbus_sendpacket(channel->channel, command, cmd_size,
-			       packet->request_id, VM_PKT_DATA_INBAND,
-			       VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
+	do {
+		ret = vmbus_sendpacket(channel->channel, command, cmd_size,
+				packet->request_id, VM_PKT_DATA_INBAND,
+				VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
+		/*
+		 * -EAGAIN is returned when the VM bus ring buffer if full.
+		 * Wait 2ms to allow the host to process messages and try again.
+		 */
+		if (ret == -EAGAIN) {
+			usleep_range(1000, 2000);
+			try_count++;
+		}
+	} while (ret == -EAGAIN && try_count < 50);
 	if (ret) {
 		DXG_ERR("vmbus_sendpacket failed: %x", ret);
 		spin_lock_irq(&channel->packet_list_mutex);
#48 lore raw

PATCH v4 48/55 drivers: hv: dxgkrnl: Add support for locking a shared allocation by not the owner

In reply to Eric Curtin

From: Iouri Tarassov <[email protected]>

WDDM has a restriction that an allocation of a shared resource can be locked for CPU access only by the resource creator (the owner). This restriction is removed for system memory only allocations. This change adds support for this feature.

[kms: forward port to 6.6 from 6.1. No code changes made.]
---
 drivers/hv/dxgkrnl/dxgadapter.c |  4 ++--
 drivers/hv/dxgkrnl/dxgkrnl.h    | 13 ++++++++++++-
 drivers/hv/dxgkrnl/ioctl.c      | 25 +++++++++++++++++--------
 3 files changed, 31 insertions(+), 11 deletions(-)
drivers/hv/dxgkrnl/dxgadapter.c +2 -2
index b8ae8099847b..cf946e476411 100644
@@ -559,8 +559,8 @@ void dxgsharedresource_destroy(struct kref *refcount)
 		vfree(resource->runtime_private_data);
 	if (resource->resource_private_data)
 		vfree(resource->resource_private_data);
-	if (resource->alloc_private_data_sizes)
-		vfree(resource->alloc_private_data_sizes);
+	if (resource->alloc_info)
+		vfree(resource->alloc_info);
 	if (resource->alloc_private_data)
 		vfree(resource->alloc_private_data);
 	kfree(resource);
drivers/hv/dxgkrnl/dxgkrnl.h +12 -1
index a4d0c504668b..d816a875d5ab 100644
@@ -613,6 +613,17 @@ struct dxghwqueue *dxghwqueue_create(struct dxgcontext *ctx);
 void dxghwqueue_destroy(struct dxgprocess *pr, struct dxghwqueue *hq);
 void dxghwqueue_release(struct kref *refcount);
 
+/*
+ * When a shared resource is created this structure provides information
+ * about every allocation in the resource. It is used when someone opens the
+ * resource and locks its allocation.
+ */
+struct dxgsharedallocdata {
+	u32	private_data_size;	/* Size of private data */
+	u32	num_pages;	/* Allocation size in pages */
+	bool	cached;		/* True is the alloc memory is cached */
+};
+
 /*
  * A shared resource object is created to track the list of dxgresource objects,
  * which are opened for the same underlying shared resource.
@@ -658,7 +669,7 @@ struct dxgsharedresource {
 		};
 		long		flags;
 	};
-	u32			*alloc_private_data_sizes;
+	struct dxgsharedallocdata *alloc_info;
 	u8			*alloc_private_data;
 	u8			*runtime_private_data;
 	u8			*resource_private_data;
drivers/hv/dxgkrnl/ioctl.c +17 -8
index d91af2e176e9..f8f116a7f87f 100644
@@ -369,6 +369,7 @@ static int dxgsharedresource_seal(struct dxgsharedresource *shared_resource)
 	u32 data_size;
 	struct dxgresource *resource;
 	struct dxgallocation *alloc;
+	struct dxgsharedallocdata *alloc_info;
 
 	DXG_TRACE("Sealing resource: %p", shared_resource);
 
@@ -409,9 +410,10 @@ static int dxgsharedresource_seal(struct dxgsharedresource *shared_resource)
 			ret = -EINVAL;
 			goto cleanup1;
 		}
-		shared_resource->alloc_private_data_sizes =
-			vzalloc(sizeof(u32)*shared_resource->allocation_count);
-		if (shared_resource->alloc_private_data_sizes == NULL) {
+		shared_resource->alloc_info =
+			vzalloc(sizeof(struct dxgsharedallocdata) *
+				shared_resource->allocation_count);
+		if (shared_resource->alloc_info == NULL) {
 			ret = -EINVAL;
 			goto cleanup1;
 		}
@@ -429,8 +431,10 @@ static int dxgsharedresource_seal(struct dxgsharedresource *shared_resource)
 					ret = -EINVAL;
 					goto cleanup1;
 				}
-				shared_resource->alloc_private_data_sizes[i] =
-				    alloc_data_size;
+				alloc_info = &shared_resource->alloc_info[i];
+				alloc_info->private_data_size = alloc_data_size;
+				alloc_info->num_pages = alloc->num_pages;
+				alloc_info->cached = alloc->cached;
 				memcpy(private_data,
 				       alloc->priv_drv_data->data,
 				       alloc_data_size);
@@ -5031,6 +5035,7 @@ assign_resource_handles(struct dxgprocess *process,
 	u8 *cur_priv_data;
 	u32 total_priv_data_size = 0;
 	struct d3dddi_openallocationinfo2 open_alloc_info = { };
+	struct dxgsharedallocdata *alloc_info;
 
 	hmgrtable_lock(&process->handle_table, DXGLOCK_EXCL);
 	ret = hmgrtable_assign_handle(&process->handle_table, resource,
@@ -5050,11 +5055,15 @@ assign_resource_handles(struct dxgprocess *process,
 		allocs[i]->alloc_handle = handles[i];
 		allocs[i]->handle_valid = 1;
 		open_alloc_info.allocation = handles[i];
-		if (shared_resource->alloc_private_data_sizes)
+		if (shared_resource->alloc_info) {
+			alloc_info = &shared_resource->alloc_info[i];
 			open_alloc_info.priv_drv_data_size =
-			    shared_resource->alloc_private_data_sizes[i];
-		else
+			    alloc_info->private_data_size;
+			allocs[i]->num_pages = alloc_info->num_pages;
+			allocs[i]->cached  = alloc_info->cached;
+		} else {
 			open_alloc_info.priv_drv_data_size = 0;
+		}
 
 		total_priv_data_size += open_alloc_info.priv_drv_data_size;
 		open_alloc_info.priv_drv_data = cur_priv_data;
#49 lore raw

PATCH v4 49/55 drivers: hv: dxgkrnl: Fix build breaks when switching to 6.6 kernel due to hv_driver remove callback change.

In reply to Eric Curtin

From: Iouri Tarassov <[email protected]>

The hv_driver remove callback has been updated to return void instead of int. dxg_remove_vmbus() in dxgmodule.c needs to be updated to match. See this commit for more context:
96ec2939620c "Drivers: hv: Make remove callback of hyperv driver void returned"

---
 drivers/hv/dxgkrnl/dxgmodule.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
drivers/hv/dxgkrnl/dxgmodule.c +1 -5
index 0fafb6167229..5459bd9b82fb 100644
@@ -803,9 +803,8 @@ static int dxg_probe_vmbus(struct hv_device *hdev,
 	return ret;
 }
 
-static int dxg_remove_vmbus(struct hv_device *hdev)
+static void dxg_remove_vmbus(struct hv_device *hdev)
 {
-	int ret = 0;
 	struct dxgvgpuchannel *vgpu_channel;
 	struct dxgglobal *dxgglobal = dxggbl();
 
@@ -830,12 +829,9 @@ static int dxg_remove_vmbus(struct hv_device *hdev)
 	} else {
 		/* Unknown device type */
 		DXG_ERR("Unknown device type");
-		ret = -ENODEV;
 	}
 
 	mutex_unlock(&dxgglobal->device_mutex);
-
-	return ret;
 }
 
 MODULE_DEVICE_TABLE(vmbus, dxg_vmbus_id_table);
#50 lore raw

PATCH v4 50/55 drivers: hv: dxgkrnl: Fix build breaks when switching to 6.6 kernel due to removed uuid_le_cmp

In reply to Eric Curtin

From: Iouri Tarassov <[email protected]>

uuid_le_cmp was removed and needs to be replaced by guid_equal. The relevant upstream commits are:
1fb1ea0d9cb8 "mei: Move uuid.h to the MEI namespace" f5b3c341a46e "mei: Move uuid_le_cmp() to its only user" 5e6a51787fef "uuid: Decouple guid_t and uuid_le types and respective macros"

---
 drivers/hv/dxgkrnl/dxgmodule.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
drivers/hv/dxgkrnl/dxgmodule.c +4 -6
index 5459bd9b82fb..e3ac70df1b6f 100644
@@ -762,7 +762,7 @@ static int dxg_probe_vmbus(struct hv_device *hdev,
 
 	mutex_lock(&dxgglobal->device_mutex);
 
-	if (uuid_le_cmp(hdev->dev_type, dxg_vmbus_id_table[0].guid) == 0) {
+	if (guid_equal(&hdev->dev_type, &dxg_vmbus_id_table[0].guid)) {
 		/* This is a new virtual GPU channel */
 		guid_to_luid(&hdev->channel->offermsg.offer.if_instance, &luid);
 		DXG_TRACE("vGPU channel: %pUb",
@@ -777,8 +777,7 @@ static int dxg_probe_vmbus(struct hv_device *hdev,
 		list_add_tail(&vgpuch->vgpu_ch_list_entry,
 			      &dxgglobal->vgpu_ch_list_head);
 		dxgglobal_start_adapters();
-	} else if (uuid_le_cmp(hdev->dev_type,
-		   dxg_vmbus_id_table[1].guid) == 0) {
+	} else if (guid_equal(&hdev->dev_type, &dxg_vmbus_id_table[1].guid)) {
 		/* This is the global Dxgkgnl channel */
 		DXG_TRACE("Global channel: %pUb",
 			 &hdev->channel->offermsg.offer.if_instance);
@@ -810,7 +809,7 @@ static void dxg_remove_vmbus(struct hv_device *hdev)
 
 	mutex_lock(&dxgglobal->device_mutex);
 
-	if (uuid_le_cmp(hdev->dev_type, dxg_vmbus_id_table[0].guid) == 0) {
+	if (guid_equal(&hdev->dev_type, &dxg_vmbus_id_table[0].guid)) {
 		DXG_TRACE("Remove virtual GPU channel");
 		dxgglobal_stop_adapter_vmbus(hdev);
 		list_for_each_entry(vgpu_channel,
@@ -822,8 +821,7 @@ static void dxg_remove_vmbus(struct hv_device *hdev)
 				break;
 			}
 		}
-	} else if (uuid_le_cmp(hdev->dev_type,
-		   dxg_vmbus_id_table[1].guid) == 0) {
+	} else if (guid_equal(&hdev->dev_type, &dxg_vmbus_id_table[1].guid)) {
 		DXG_TRACE("Remove global channel device");
 		dxgglobal_destroy_global_channel();
 	} else {
#51 lore raw

PATCH v4 51/55 drivers: hv: dxgkrnl: Implement D3DKMTEnumProcesses to match the Windows implementation

In reply to Eric Curtin

From: Iouri Tarassov <[email protected]>

The behavior of D3DKMTEnumProcesses on Windows is that when buffer_count is 0 or input buffer is NULL, the number of active processes is returned. The Linux implemenation is updated to match this.

---
 drivers/hv/dxgkrnl/dxgmodule.c |  2 +-
 drivers/hv/dxgkrnl/ioctl.c     | 29 ++++++++++++++++++-----------
 2 files changed, 19 insertions(+), 12 deletions(-)
drivers/hv/dxgkrnl/dxgmodule.c +1 -1
index e3ac70df1b6f..8f5d6db256a3 100644
@@ -965,4 +965,4 @@ module_exit(dxg_drv_exit);
 
 MODULE_LICENSE("GPL");
 MODULE_DESCRIPTION("Microsoft Dxgkrnl virtual compute device Driver");
-MODULE_VERSION("2.0.2");
+MODULE_VERSION("2.0.3");
drivers/hv/dxgkrnl/ioctl.c +18 -11
index f8f116a7f87f..42f3de31a63c 100644
@@ -5373,7 +5373,7 @@ dxgkio_enum_processes(struct dxgprocess *process, void *__user inargs)
 	struct dxgprocess_adapter *pentry;
 	int nump = 0;	/* Current number of processes*/
 	struct ntstatus status;
-	int ret;
+	int ret, ret1;
 
 	ret = copy_from_user(&args, inargs, sizeof(args));
 	if (ret) {
@@ -5382,12 +5382,6 @@ dxgkio_enum_processes(struct dxgprocess *process, void *__user inargs)
 		goto cleanup;
 	}
 
-	if (args.buffer_count == 0) {
-		DXG_ERR("Invalid buffer count");
-		ret = -EINVAL;
-		goto cleanup;
-	}
-
 	dxgglobal_acquire_adapter_list_lock(DXGLOCK_SHARED);
 	dxgglobal_acquire_process_adapter_lock();
 
@@ -5405,6 +5399,19 @@ dxgkio_enum_processes(struct dxgprocess *process, void *__user inargs)
 		goto cleanup_locks;
 	}
 
+	list_for_each_entry(pentry, &adapter->adapter_process_list_head,
+			    adapter_process_list_entry) {
+		if (pentry->process->nspid == task_active_pid_ns(current))
+			nump++;
+	}
+
+	if (nump > args.buffer_count || args.buffer == NULL) {
+		status.v = STATUS_BUFFER_TOO_SMALL;
+		ret = ntstatus2int(status);
+		goto cleanup_locks;
+	}
+
+	nump = 0;
 	list_for_each_entry(pentry, &adapter->adapter_process_list_head,
 			    adapter_process_list_entry) {
 		if (pentry->process->nspid != task_active_pid_ns(current))
@@ -5429,10 +5436,10 @@ dxgkio_enum_processes(struct dxgprocess *process, void *__user inargs)
 	dxgglobal_release_process_adapter_lock();
 	dxgglobal_release_adapter_list_lock(DXGLOCK_SHARED);
 
-	if (ret == 0) {
-		ret = copy_to_user(&input->buffer_count, &nump, sizeof(u32));
-		if (ret)
-			DXG_ERR("failed to copy buffer count to user");
+	ret1 = copy_to_user(&input->buffer_count, &nump, sizeof(u32));
+	if (ret1) {
+		DXG_ERR("failed to copy buffer count to user");
+		ret = -EFAULT;
 	}
 
 cleanup:
#52 lore raw

PATCH v4 52/55 drivers: hv: dxgkrnl: Use pin_user_pages instead of get_user_pages for DMA accessible memory

In reply to Eric Curtin

From: Iouri Tarassov <[email protected]>

Pages, which are obtained by calling get_user_pages(), can be evicted from memory. pin_user_pages() should be used for memory, which is accessed by DMA.

---
 drivers/hv/dxgkrnl/dxgadapter.c |  2 +-
 drivers/hv/dxgkrnl/dxgvmbus.c   | 12 ++++++++----
 2 files changed, 9 insertions(+), 5 deletions(-)
drivers/hv/dxgkrnl/dxgadapter.c +1 -1
index cf946e476411..c94283b09fa1 100644
@@ -882,7 +882,7 @@ struct dxgallocation *dxgallocation_create(struct dxgprocess *process)
 void dxgallocation_stop(struct dxgallocation *alloc)
 {
 	if (alloc->pages) {
-		release_pages(alloc->pages, alloc->num_pages);
+		unpin_user_pages(alloc->pages, alloc->num_pages);
 		vfree(alloc->pages);
 		alloc->pages = NULL;
 	}
drivers/hv/dxgkrnl/dxgvmbus.c +8 -4
index 467e7707c8c7..abb6d2af89ac 100644
@@ -1457,6 +1457,7 @@ int create_existing_sysmem(struct dxgdevice *device,
 	u64 *pfn;
 	u32 pages_to_send;
 	u32 i;
+	u32 gup_flags = FOLL_LONGTERM;
 	struct dxgglobal *dxgglobal = dxggbl();
 
 	/*
@@ -1475,12 +1476,15 @@ int create_existing_sysmem(struct dxgdevice *device,
 		ret = -ENOMEM;
 		goto cleanup;
 	}
-	ret1 = get_user_pages_fast((unsigned long)sysmem, npages, !read_only,
-				  dxgalloc->pages);
+	if (!read_only)
+		gup_flags |= FOLL_WRITE;
+	ret1 = pin_user_pages_fast((unsigned long)sysmem, npages, gup_flags,
+				   dxgalloc->pages);
 	if (ret1 != npages) {
 		DXG_ERR("get_user_pages_fast failed: %d", ret1);
-		if (ret1 > 0 && ret1 < npages)
-			release_pages(dxgalloc->pages, ret1);
+		if (ret1 > 0 && ret1 < npages) {
+			unpin_user_pages(dxgalloc->pages, ret1);
+		}
 		vfree(dxgalloc->pages);
 		dxgalloc->pages = NULL;
 		ret = -ENOMEM;
#53 lore raw

PATCH v4 53/55 drivers: hv: dxgkrnl: Do not print error messages when virtual GPU is not present

In reply to Eric Curtin

From: Iouri Tarassov <[email protected]>

Dxgkrnl prints the error message "Failed to acquire global channel lock" when a process tries to open the /dev/dxg device and there is no virtual GPU. This message should not be printed in this scenario.

---
 drivers/hv/dxgkrnl/dxgadapter.c | 2 +-
 drivers/hv/dxgkrnl/dxgmodule.c  | 4 ++++
 2 files changed, 5 insertions(+), 1 deletion(-)
drivers/hv/dxgkrnl/dxgadapter.c +1 -1
index c94283b09fa1..6d3cabb24e6f 100644
@@ -78,12 +78,12 @@ void dxgadapter_start(struct dxgadapter *adapter)
 
 	/* The global channel is initialized when the first adapter starts */
 	if (!dxgglobal->global_channel_initialized) {
+		dxgglobal->global_channel_initialized = true;
 		ret = dxgglobal_init_global_channel();
 		if (ret) {
 			dxgglobal_destroy_global_channel();
 			return;
 		}
-		dxgglobal->global_channel_initialized = true;
 	}
 
 	/* Initialize vGPU vm bus channel */
drivers/hv/dxgkrnl/dxgmodule.c +4 -0
index 8f5d6db256a3..c2a4a2a2136f 100644
@@ -46,9 +46,13 @@ int dxgglobal_acquire_channel_lock(void)
 {
 	struct dxgglobal *dxgglobal = dxggbl();
 
+	if (!dxgglobal->global_channel_initialized)
+		return -ENODEV;
+
 	down_read(&dxgglobal->channel_lock);
 	if (dxgglobal->channel.channel == NULL) {
 		DXG_ERR("Failed to acquire global channel lock");
+		up_read(&dxgglobal->channel_lock);
 		return -ENODEV;
 	} else {
 		return 0;
#54 lore raw

PATCH v4 54/55 drivers: hv: dxgkrnl: Fix crash at hmgrtable_free_handle

In reply to Eric Curtin

From: Hideyuki Nagase <[email protected]>

Fix a potential NULL pointer crash in hmgrtable_free_handle() when free_handle_list_tail is HMGRTABLE_INVALID_INDEX. Guard the entry dereference with a bounds check before writing the next_free_index.

---
 drivers/hv/dxgkrnl/hmgr.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/hv/dxgkrnl/hmgr.c +7 -2
index 24101d0091ab..059f94307a0e 100644
@@ -462,9 +462,14 @@ void hmgrtable_free_handle(struct hmgrtable *table, enum hmgrentry_type t,
 		 */
 		entry->next_free_index = HMGRTABLE_INVALID_INDEX;
 		entry->prev_free_index = table->free_handle_list_tail;
-		entry = &table->entry_table[table->free_handle_list_tail];
-		entry->next_free_index = i;
+		if (table->free_handle_list_tail != HMGRTABLE_INVALID_INDEX) {
+			entry = &table->entry_table[table->free_handle_list_tail];
+			entry->next_free_index = i;
+		}
 		table->free_handle_list_tail = i;
+		if (table->free_handle_list_head == HMGRTABLE_INVALID_INDEX) {
+			table->free_handle_list_head = i;
+		}
 	} else {
 		DXG_ERR("Invalid handle to free: %d %x", i, h.v);
 	}
#55 lore raw

PATCH v4 55/55 drivers: hv: dxgkrnl: Code cleanup for upstream submission

In reply to Eric Curtin

Address issues raised in previous LKML submission attempts (v1-v3):

- Replace deprecated one-element arrays [1] with C99 flexible arrays [] in dxgvmbus.h and dxgkrnl.h
- Replace %px with %p in DXG_TRACE calls (avoids exposing kernel layout)
- Remove unnecessary braces from single-statement if blocks
- Remove LINUX_VERSION_CODE guard: max_pkt_size exists in all supported kernels
- Remove unused linux/version.h include from dxgkrnl.h
- Fix whitespace (space before tab) in dxgvmbus.h and d3dkmthk.h
- Replace DXG_ERR non-debug macro do{}while(0) with direct dev_err call
- Change -EBADE to -ENODEV for global channel duplicate detection
- Remove MODULE_VERSION as it is not recommended for in-tree drivers
- Add explanatory comment to guid_to_luid() cast
- Update MAINTAINERS email to [email protected]

---
 MAINTAINERS                     |  2 +-
 drivers/hv/dxgkrnl/dxgadapter.c |  8 ++++----
 drivers/hv/dxgkrnl/dxgkrnl.h    | 13 +++++--------
 drivers/hv/dxgkrnl/dxgmodule.c  |  5 ++---
 drivers/hv/dxgkrnl/dxgvmbus.c   |  5 +----
 drivers/hv/dxgkrnl/dxgvmbus.h   | 26 +++++++++++++-------------
 drivers/hv/dxgkrnl/hmgr.c       |  3 +--
 include/uapi/misc/d3dkmthk.h    |  2 +-
 8 files changed, 28 insertions(+), 36 deletions(-)
MAINTAINERS +1 -1
index 4fe0b3501931..493c65a02b80 100644
@@ -9772,7 +9772,7 @@ F:	drivers/mtd/hyperbus/
 F:	include/linux/mtd/hyperbus.h
 
 Hyper-V vGPU DRIVER
-M:	Iouri Tarassov <[email protected]>
+M:	Iouri Tarassov <[email protected]>
 L:	[email protected]
 S:	Supported
 F:	drivers/hv/dxgkrnl/
drivers/hv/dxgkrnl/dxgadapter.c +4 -4
index 6d3cabb24e6f..d395fdcb63fa 100644
@@ -136,7 +136,7 @@ void dxgadapter_release(struct kref *refcount)
 	struct dxgadapter *adapter;
 
 	adapter = container_of(refcount, struct dxgadapter, adapter_kref);
-	DXG_TRACE("Destroying adapter: %px", adapter);
+	DXG_TRACE("Destroying adapter: %p", adapter);
 	kfree(adapter);
 }
 
@@ -271,7 +271,7 @@ struct dxgdevice *dxgdevice_create(struct dxgadapter *adapter,
 			kref_put(&device->device_kref, dxgdevice_release);
 			device = NULL;
 		} else {
-			DXG_TRACE("dxgdevice created: %px", device);
+			DXG_TRACE("dxgdevice created: %p", device);
 		}
 	}
 	return device;
@@ -720,7 +720,7 @@ void dxgdevice_release(struct kref *refcount)
 	struct dxgdevice *device;
 
 	device = container_of(refcount, struct dxgdevice, device_kref);
-	DXG_TRACE("Destroying device: %px", device);
+	DXG_TRACE("Destroying device: %p", device);
 	kref_put(&device->adapter->adapter_kref, dxgadapter_release);
 	kfree(device);
 }
@@ -1103,7 +1103,7 @@ int dxgprocess_adapter_add_device(struct dxgprocess *process,
 
 void dxgprocess_adapter_remove_device(struct dxgdevice *device)
 {
-	DXG_TRACE("Removing device: %px", device);
+	DXG_TRACE("Removing device: %p", device);
 	mutex_lock(&device->adapter_info->device_list_mutex);
 	if (device->device_list_entry.next) {
 		list_del(&device->device_list_entry);
drivers/hv/dxgkrnl/dxgkrnl.h +5 -8
index d816a875d5ab..4a4605f45736 100644
@@ -27,7 +27,6 @@
 #include <linux/pci.h>
 #include <linux/hyperv.h>
 #include <uapi/misc/d3dkmthk.h>
-#include <linux/version.h>
 #include "misc.h"
 #include "hmgr.h"
 #include <uapi/misc/d3dkmthk.h>
@@ -719,7 +718,7 @@ bool dxgresource_is_active(struct dxgresource *res);
 
 struct privdata {
 	u32 data_size;
-	u8 data[1];
+	u8 data[];
 };
 
 struct dxgallocation {
@@ -769,9 +768,9 @@ long dxgk_unlocked_ioctl(struct file *f, unsigned int p1, unsigned long p2);
 
 int dxg_unmap_iospace(void *va, u32 size);
 /*
- * The convention is that VNBus instance id is a GUID, but the host sets
- * the lower part of the value to the host adapter LUID. The function
- * provides the necessary conversion.
+ * The convention is that VMBus instance id is a GUID, but the host sets
+ * the lower part of the value to the host adapter LUID. The cast reads
+ * the first sizeof(winluid) bytes of the GUID as a winluid value.
  */
 static inline void guid_to_luid(guid_t *guid, struct winluid *luid)
 {
@@ -1029,9 +1028,7 @@ void dxgk_validate_ioctls(void);
 #else
 
 #define DXG_TRACE(...)
-#define DXG_ERR(fmt, ...) do {					\
-	dev_err(DXGDEV, "%s: " fmt, __func__, ##__VA_ARGS__);	\
-} while (0)
+#define DXG_ERR(fmt, ...)	dev_err(DXGDEV, "%s: " fmt, __func__, ##__VA_ARGS__)
 
 #endif /* DEBUG */
 
drivers/hv/dxgkrnl/dxgmodule.c +2 -3
index c2a4a2a2136f..435dc60511b8 100644
@@ -158,7 +158,7 @@ static void dxg_signal_dma_fence(struct dxghostevent *eventhdr)
 {
 	struct dxgsyncpoint *event = (struct dxgsyncpoint *)eventhdr;
 
-	DXG_TRACE("syncpoint: %px, fence: %lld", event, event->fence_value);
+	DXG_TRACE("syncpoint: %p, fence: %lld", event, event->fence_value);
 	event->fence_value++;
 	list_del(&eventhdr->host_event_list_entry);
 	dma_fence_signal(&event->base);
@@ -788,7 +788,7 @@ static int dxg_probe_vmbus(struct hv_device *hdev,
 		if (dxgglobal->hdev) {
 			/* This device should appear only once */
 			DXG_ERR("global channel already exists");
-			ret = -EBADE;
+			ret = -ENODEV;
 			goto error;
 		}
 		dxgglobal->hdev = hdev;
@@ -969,4 +969,3 @@ module_exit(dxg_drv_exit);
 
 MODULE_LICENSE("GPL");
 MODULE_DESCRIPTION("Microsoft Dxgkrnl virtual compute device Driver");
-MODULE_VERSION("2.0.3");
drivers/hv/dxgkrnl/dxgvmbus.c +1 -4
index abb6d2af89ac..4b1ccaac440c 100644
@@ -246,9 +246,7 @@ int dxgvmbuschannel_init(struct dxgvmbuschannel *ch, struct hv_device *hdev)
 		goto cleanup;
 	}
 
-#if KERNEL_VERSION(5, 15, 0) <= LINUX_VERSION_CODE
 	hdev->channel->max_pkt_size = DXG_MAX_VM_BUS_PACKET_SIZE;
-#endif
 	ret = vmbus_open(hdev->channel, RING_BUFSIZE, RING_BUFSIZE,
 			 NULL, 0, dxgvmbuschannel_receive, ch);
 	if (ret) {
@@ -1482,9 +1480,8 @@ int create_existing_sysmem(struct dxgdevice *device,
 				   dxgalloc->pages);
 	if (ret1 != npages) {
 		DXG_ERR("get_user_pages_fast failed: %d", ret1);
-		if (ret1 > 0 && ret1 < npages) {
+		if (ret1 > 0 && ret1 < npages)
 			unpin_user_pages(dxgalloc->pages, ret1);
-		}
 		vfree(dxgalloc->pages);
 		dxgalloc->pages = NULL;
 		ret = -ENOMEM;
drivers/hv/dxgkrnl/dxgvmbus.h +13 -13
index a7e625b2f896..22246826d2f1 100644
@@ -313,12 +313,12 @@ struct dxgkvmb_command_queryadapterinfo {
 	struct dxgkvmb_command_vgpu_to_host hdr;
 	enum kmtqueryadapterinfotype	query_type;
 	u32				private_data_size;
-	u8				private_data[1];
+	u8				private_data[];
 };
 
 struct dxgkvmb_command_queryadapterinfo_return {
 	struct ntstatus			status;
-	u8				private_data[1];
+	u8				private_data[];
 };
 
 /* Returns ntstatus */
@@ -391,7 +391,7 @@ struct dxgkvmb_command_makeresident {
 	struct d3dkmthandle		paging_queue;
 	struct d3dddi_makeresident_flags flags;
 	u32				alloc_count;
-	struct d3dkmthandle		allocations[1];
+	struct d3dkmthandle		allocations[];
 };
 
 struct dxgkvmb_command_makeresident_return {
@@ -405,7 +405,7 @@ struct dxgkvmb_command_evict {
 	struct d3dkmthandle		device;
 	struct d3dddi_evict_flags	flags;
 	u32				alloc_count;
-	struct d3dkmthandle		allocations[1];
+	struct d3dkmthandle		allocations[];
 };
 
 struct dxgkvmb_command_evict_return {
@@ -476,7 +476,7 @@ struct dxgkvmb_command_updategpuvirtualaddress {
 	struct d3dkmthandle		fence_object;
 	u32				num_operations;
 	u32				flags;
-	struct d3dddi_updategpuvirtualaddress_operation operations[1];
+	struct d3dddi_updategpuvirtualaddress_operation operations[];
 };
 
 struct dxgkvmb_command_queryclockcalibration {
@@ -627,7 +627,7 @@ struct dxgkvmb_command_destroyallocation {
 	struct d3dkmthandle		resource;
 	u32				alloc_count;
 	struct d3dddicb_destroyallocation2flags flags;
-	struct d3dkmthandle		allocations[1];
+	struct d3dkmthandle		allocations[];
 };
 
 struct dxgkvmb_command_createcontextvirtual {
@@ -639,7 +639,7 @@ struct dxgkvmb_command_createcontextvirtual {
 	struct d3dddi_createcontextflags flags;
 	enum d3dkmt_clienthint		client_hint;
 	u32				priv_drv_data_size;
-	u8				priv_drv_data[1];
+	u8				priv_drv_data[];
 };
 
 /* The command returns ntstatus */
@@ -768,7 +768,7 @@ struct dxgkvmb_command_offerallocations {
 	enum d3dkmt_offer_priority	priority;
 	struct d3dkmt_offer_flags	flags;
 	bool				resources;
-	struct d3dkmthandle		allocations[1];
+	struct d3dkmthandle		allocations[];
 };
 
 struct dxgkvmb_command_reclaimallocations {
@@ -778,13 +778,13 @@ struct dxgkvmb_command_reclaimallocations {
 	u32				allocation_count;
 	bool				resources;
 	bool				write_results;
-	struct d3dkmthandle		allocations[1];
+	struct d3dkmthandle		allocations[];
 };
 
 struct dxgkvmb_command_reclaimallocations_return {
 	u64				paging_fence_value;
 	struct ntstatus			status;
-	enum d3dddi_reclaim_result	discarded[1];
+	enum d3dddi_reclaim_result	discarded[];
 };
 
 /* Returns ntstatus */
@@ -804,7 +804,7 @@ struct dxgkvmb_command_createhwqueue {
 	struct d3dkmthandle		context;
 	struct d3dddi_createhwqueueflags flags;
 	u32				priv_drv_data_size;
-	char				priv_drv_data[1];
+	char				priv_drv_data[];
 };
 
 /* The command returns ntstatus */
@@ -833,7 +833,7 @@ struct dxgkvmb_command_escape {
 	struct d3dddi_escapeflags	flags;
 	u32				priv_drv_data_size;
 	struct d3dkmthandle		context;
-	u8				priv_drv_data[1];
+	u8				priv_drv_data[];
 };
 
 struct dxgkvmb_command_queryvideomemoryinfo {
@@ -879,7 +879,7 @@ struct dxgk_feature_desc {
 	struct {
 		u16 supported		: 1;
 		u16 virtualization_mode : 3;
-		u16 global 		: 1;
+		u16 global		: 1;
 		u16 driver_feature	: 1;
 		u16 internal		: 1;
 		u16 reserved		: 9;
drivers/hv/dxgkrnl/hmgr.c +1 -2
index 059f94307a0e..95879f59133e 100644
@@ -467,9 +467,8 @@ void hmgrtable_free_handle(struct hmgrtable *table, enum hmgrentry_type t,
 			entry->next_free_index = i;
 		}
 		table->free_handle_list_tail = i;
-		if (table->free_handle_list_head == HMGRTABLE_INVALID_INDEX) {
+		if (table->free_handle_list_head == HMGRTABLE_INVALID_INDEX)
 			table->free_handle_list_head = i;
-		}
 	} else {
 		DXG_ERR("Invalid handle to free: %d %x", i, h.v);
 	}
include/uapi/misc/d3dkmthk.h +1 -1
index db40e8ff40b0..a58b2513dfd3 100644
@@ -1612,7 +1612,7 @@ struct d3dkmt_opensyncobjectfromsyncfile {
 };
 
 struct d3dkmt_enumprocesses {
-	struct winluid 		adapter_luid;
+	struct winluid		adapter_luid;
 #ifdef __KERNEL__
 	__u32			*buffer;
 #else