Skip to content

Commit 89cbe4c

Browse files
Iouri Tarassovchessturo
authored andcommitted
drivers: hv: dxgkrnl: Share objects with the host
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(). Signed-off-by: Iouri Tarassov <iourit@linux.microsoft.com> [kms: forward port to 6.6 from 6.1. No code changes made.] Signed-off-by: Kelsey Steele <kelseysteele@microsoft.com>
1 parent 905eb40 commit 89cbe4c

5 files changed

Lines changed: 120 additions & 7 deletions

File tree

drivers/hv/dxgkrnl/dxgkrnl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -872,6 +872,8 @@ int dxgvmb_send_get_stdalloc_data(struct dxgdevice *device,
872872
int dxgvmb_send_async_msg(struct dxgvmbuschannel *channel,
873873
void *command,
874874
u32 cmd_size);
875+
int dxgvmb_send_share_object_with_host(struct dxgprocess *process,
876+
struct d3dkmt_shareobjectwithhost *args);
875877

876878
void signal_host_cpu_event(struct dxghostevent *eventhdr);
877879
int ntstatus2int(struct ntstatus status);

drivers/hv/dxgkrnl/dxgvmbus.c

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,50 @@ int dxgvmb_send_destroy_sync_object(struct dxgprocess *process,
881881
return ret;
882882
}
883883

884+
int dxgvmb_send_share_object_with_host(struct dxgprocess *process,
885+
struct d3dkmt_shareobjectwithhost *args)
886+
{
887+
struct dxgkvmb_command_shareobjectwithhost *command;
888+
struct dxgkvmb_command_shareobjectwithhost_return result = {};
889+
int ret;
890+
struct dxgvmbusmsg msg = {.hdr = NULL};
891+
892+
ret = init_message(&msg, NULL, process, sizeof(*command));
893+
if (ret)
894+
return ret;
895+
command = (void *)msg.msg;
896+
897+
ret = dxgglobal_acquire_channel_lock();
898+
if (ret < 0)
899+
goto cleanup;
900+
901+
command_vm_to_host_init2(&command->hdr,
902+
DXGK_VMBCOMMAND_SHAREOBJECTWITHHOST,
903+
process->host_handle);
904+
command->device_handle = args->device_handle;
905+
command->object_handle = args->object_handle;
906+
907+
ret = dxgvmb_send_sync_msg(dxgglobal_get_dxgvmbuschannel(),
908+
msg.hdr, msg.size, &result, sizeof(result));
909+
910+
dxgglobal_release_channel_lock();
911+
912+
if (ret || !NT_SUCCESS(result.status)) {
913+
if (ret == 0)
914+
ret = ntstatus2int(result.status);
915+
DXG_ERR("Host failed to share object with host: %d %x",
916+
ret, result.status.v);
917+
goto cleanup;
918+
}
919+
args->object_vail_nt_handle = result.vail_nt_handle;
920+
921+
cleanup:
922+
free_message(&msg, process);
923+
if (ret)
924+
DXG_ERR("err: %d", ret);
925+
return ret;
926+
}
927+
884928
/*
885929
* Virtual GPU messages to the host
886930
*/
@@ -2323,37 +2367,43 @@ int dxgvmb_send_create_hwqueue(struct dxgprocess *process,
23232367

23242368
ret = copy_to_user(&inargs->queue, &command->hwqueue,
23252369
sizeof(struct d3dkmthandle));
2326-
if (ret < 0) {
2370+
if (ret) {
23272371
DXG_ERR("failed to copy hwqueue handle");
2372+
ret = -EINVAL;
23282373
goto cleanup;
23292374
}
23302375
ret = copy_to_user(&inargs->queue_progress_fence,
23312376
&command->hwqueue_progress_fence,
23322377
sizeof(struct d3dkmthandle));
2333-
if (ret < 0) {
2378+
if (ret) {
23342379
DXG_ERR("failed to progress fence");
2380+
ret = -EINVAL;
23352381
goto cleanup;
23362382
}
23372383
ret = copy_to_user(&inargs->queue_progress_fence_cpu_va,
23382384
&hwqueue->progress_fence_mapped_address,
23392385
sizeof(inargs->queue_progress_fence_cpu_va));
2340-
if (ret < 0) {
2386+
if (ret) {
23412387
DXG_ERR("failed to copy fence cpu va");
2388+
ret = -EINVAL;
23422389
goto cleanup;
23432390
}
23442391
ret = copy_to_user(&inargs->queue_progress_fence_gpu_va,
23452392
&command->hwqueue_progress_fence_gpuva,
23462393
sizeof(u64));
2347-
if (ret < 0) {
2394+
if (ret) {
23482395
DXG_ERR("failed to copy fence gpu va");
2396+
ret = -EINVAL;
23492397
goto cleanup;
23502398
}
23512399
if (args->priv_drv_data_size) {
23522400
ret = copy_to_user(args->priv_drv_data,
23532401
command->priv_drv_data,
23542402
args->priv_drv_data_size);
2355-
if (ret < 0)
2403+
if (ret) {
23562404
DXG_ERR("failed to copy private data");
2405+
ret = -EINVAL;
2406+
}
23572407
}
23582408

23592409
cleanup:

drivers/hv/dxgkrnl/dxgvmbus.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,4 +574,22 @@ struct dxgkvmb_command_destroyhwqueue {
574574
struct d3dkmthandle hwqueue;
575575
};
576576

577+
struct dxgkvmb_command_shareobjectwithhost {
578+
struct dxgkvmb_command_vm_to_host hdr;
579+
struct d3dkmthandle device_handle;
580+
struct d3dkmthandle object_handle;
581+
u64 reserved;
582+
};
583+
584+
struct dxgkvmb_command_shareobjectwithhost_return {
585+
struct ntstatus status;
586+
u32 alignment;
587+
u64 vail_nt_handle;
588+
};
589+
590+
int
591+
dxgvmb_send_sync_msg(struct dxgvmbuschannel *channel,
592+
void *command, u32 command_size, void *result,
593+
u32 result_size);
594+
577595
#endif /* _DXGVMBUS_H */

drivers/hv/dxgkrnl/ioctl.c

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2460,6 +2460,7 @@ dxgkio_open_sync_object_nt(struct dxgprocess *process, void *__user inargs)
24602460
if (ret == 0)
24612461
goto success;
24622462
DXG_ERR("failed to copy output args");
2463+
ret = -EINVAL;
24632464

24642465
cleanup:
24652466

@@ -3364,8 +3365,10 @@ dxgkio_share_objects(struct dxgprocess *process, void *__user inargs)
33643365
tmp = (u64) object_fd;
33653366

33663367
ret = copy_to_user(args.shared_handle, &tmp, sizeof(u64));
3367-
if (ret < 0)
3368+
if (ret) {
33683369
DXG_ERR("failed to copy shared handle");
3370+
ret = -EINVAL;
3371+
}
33693372

33703373
cleanup:
33713374
if (ret < 0) {
@@ -3773,6 +3776,37 @@ dxgkio_open_resource_nt(struct dxgprocess *process,
37733776
return ret;
37743777
}
37753778

3779+
static int
3780+
dxgkio_share_object_with_host(struct dxgprocess *process, void *__user inargs)
3781+
{
3782+
struct d3dkmt_shareobjectwithhost args;
3783+
int ret;
3784+
3785+
ret = copy_from_user(&args, inargs, sizeof(args));
3786+
if (ret) {
3787+
DXG_ERR("failed to copy input args");
3788+
ret = -EINVAL;
3789+
goto cleanup;
3790+
}
3791+
3792+
ret = dxgvmb_send_share_object_with_host(process, &args);
3793+
if (ret) {
3794+
DXG_ERR("dxgvmb_send_share_object_with_host dailed");
3795+
goto cleanup;
3796+
}
3797+
3798+
ret = copy_to_user(inargs, &args, sizeof(args));
3799+
if (ret) {
3800+
DXG_ERR("failed to copy data to user");
3801+
ret = -EINVAL;
3802+
}
3803+
3804+
cleanup:
3805+
3806+
DXG_TRACE("ioctl:%s %d", errorstr(ret), ret);
3807+
return ret;
3808+
}
3809+
37763810
static struct ioctl_desc ioctls[] = {
37773811
/* 0x00 */ {},
37783812
/* 0x01 */ {dxgkio_open_adapter_from_luid, LX_DXOPENADAPTERFROMLUID},
@@ -3850,7 +3884,7 @@ static struct ioctl_desc ioctls[] = {
38503884
LX_DXQUERYRESOURCEINFOFROMNTHANDLE},
38513885
/* 0x42 */ {dxgkio_open_resource_nt, LX_DXOPENRESOURCEFROMNTHANDLE},
38523886
/* 0x43 */ {},
3853-
/* 0x44 */ {},
3887+
/* 0x44 */ {dxgkio_share_object_with_host, LX_DXSHAREOBJECTWITHHOST},
38543888
/* 0x45 */ {},
38553889
};
38563890

include/uapi/misc/d3dkmthk.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -952,6 +952,13 @@ struct d3dkmt_enumadapters3 {
952952
#endif
953953
};
954954

955+
struct d3dkmt_shareobjectwithhost {
956+
struct d3dkmthandle device_handle;
957+
struct d3dkmthandle object_handle;
958+
__u64 reserved;
959+
__u64 object_vail_nt_handle;
960+
};
961+
955962
/*
956963
* Dxgkrnl Graphics Port Driver ioctl definitions
957964
*
@@ -1021,5 +1028,7 @@ struct d3dkmt_enumadapters3 {
10211028
_IOWR(0x47, 0x41, struct d3dkmt_queryresourceinfofromnthandle)
10221029
#define LX_DXOPENRESOURCEFROMNTHANDLE \
10231030
_IOWR(0x47, 0x42, struct d3dkmt_openresourcefromnthandle)
1031+
#define LX_DXSHAREOBJECTWITHHOST \
1032+
_IOWR(0x47, 0x44, struct d3dkmt_shareobjectwithhost)
10241033

10251034
#endif /* _D3DKMTHK_H */

0 commit comments

Comments
 (0)