From 8156a23a6eb4ce279729bf47f7de9799f1b61914 Mon Sep 17 00:00:00 2001 From: Sagar Kamble Date: Tue, 16 Jun 2020 02:13:07 +0530 Subject: [PATCH] gpu: nvgpu: support userspace Read Only mappings Until now, all userspace buffers were mapped in the GMMU as Read & Write (RW) by default. In order to enable the use cases which require the GPU to only read the SYSMEM buffers and not inadvertently write to those, map buffer ioctls need to provide interface to set the mapping access type from the userspace. Some of the use cases are: 1. A third party server process exposes shared memory that is read-only to the client process, which does the GPU processing. Registering this memory using cudaHostRegister API as read-only in the client process will restict the access to Read Only type from the GPU. 2. IO devices exposing streaming read-only data for processing by the GPU. 3. For marking semantically read-only data as actually read-only for the purposes of debugging data corruption. This patch introduces new AS buffer mapping bitmask flag and corresponding core VM mapping bitmask flag for representing Read Only (RO) access type. By default, the access is set as Read Write (RW). Bug 200621157 Change-Id: I5ec9dec3ce089e577b86c43003d92b61eee4a90b Signed-off-by: Sagar Kamble Reviewed-on: https://git-master.nvidia.com/r/c/linux-nvgpu/+/2361750 Reviewed-by: automaticguardword Reviewed-by: Automatic_Commit_Validation_User Reviewed-by: Deepak Nibade Reviewed-by: mobile promotions Tested-by: mobile promotions GVS: Gerrit_Virtual_Submit --- drivers/gpu/nvgpu/include/nvgpu/linux/vm.h | 3 +-- drivers/gpu/nvgpu/include/nvgpu/vm.h | 3 ++- drivers/gpu/nvgpu/os/linux/cde.c | 1 - drivers/gpu/nvgpu/os/linux/vm.c | 9 +++++++-- include/uapi/linux/nvgpu.h | 1 + 5 files changed, 11 insertions(+), 6 deletions(-) diff --git a/drivers/gpu/nvgpu/include/nvgpu/linux/vm.h b/drivers/gpu/nvgpu/include/nvgpu/linux/vm.h index b807d87e3..9587bbb20 100644 --- a/drivers/gpu/nvgpu/include/nvgpu/linux/vm.h +++ b/drivers/gpu/nvgpu/include/nvgpu/linux/vm.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019, NVIDIA CORPORATION. All rights reserved. + * Copyright (c) 2017-2020, NVIDIA CORPORATION. All rights reserved. * * This program is free software; you can redistribute it and/or modify it * under the terms and conditions of the GNU General Public License, @@ -60,7 +60,6 @@ int nvgpu_vm_map_linux(struct vm_gk20a *vm, u32 page_size, s16 compr_kind, s16 incompr_kind, - enum gk20a_mem_rw_flag rw_flag, u64 buffer_offset, u64 mapping_size, struct vm_gk20a_mapping_batch *mapping_batch, diff --git a/drivers/gpu/nvgpu/include/nvgpu/vm.h b/drivers/gpu/nvgpu/include/nvgpu/vm.h index 7fd9d3cbe..d5c13f6b7 100644 --- a/drivers/gpu/nvgpu/include/nvgpu/vm.h +++ b/drivers/gpu/nvgpu/include/nvgpu/vm.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019, NVIDIA CORPORATION. All rights reserved. + * Copyright (c) 2017-2020, NVIDIA CORPORATION. All rights reserved. * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), @@ -339,6 +339,7 @@ struct vm_gk20a { #define NVGPU_VM_MAP_DIRECT_KIND_CTRL BIT32(4) #define NVGPU_VM_MAP_L3_ALLOC BIT32(5) #define NVGPU_VM_MAP_PLATFORM_ATOMIC BIT32(6) +#define NVGPU_VM_MAP_ACCESS_NO_WRITE BIT32(7) #define NVGPU_KIND_INVALID S16(-1) diff --git a/drivers/gpu/nvgpu/os/linux/cde.c b/drivers/gpu/nvgpu/os/linux/cde.c index 40d9904fc..db0363284 100644 --- a/drivers/gpu/nvgpu/os/linux/cde.c +++ b/drivers/gpu/nvgpu/os/linux/cde.c @@ -1111,7 +1111,6 @@ __releases(&l->cde_app->mutex) map_size), NV_KIND_INVALID, compbits_kind, /* incompressible kind */ - gk20a_mem_flag_none, map_offset, map_size, NULL, &map_vaddr); diff --git a/drivers/gpu/nvgpu/os/linux/vm.c b/drivers/gpu/nvgpu/os/linux/vm.c index 830b40f64..4e96ca869 100644 --- a/drivers/gpu/nvgpu/os/linux/vm.c +++ b/drivers/gpu/nvgpu/os/linux/vm.c @@ -58,6 +58,8 @@ static u32 nvgpu_vm_translate_linux_flags(struct gk20a *g, u32 flags) core_flags |= NVGPU_VM_MAP_DIRECT_KIND_CTRL; if (flags & NVGPU_AS_MAP_BUFFER_FLAGS_PLATFORM_ATOMIC) core_flags |= NVGPU_VM_MAP_PLATFORM_ATOMIC; + if (flags & NVGPU_AS_MAP_BUFFER_FLAGS_ACCESS_NO_WRITE) + core_flags |= NVGPU_VM_MAP_ACCESS_NO_WRITE; if (flags & NVGPU_AS_MAP_BUFFER_FLAGS_MAPPABLE_COMPBITS) nvgpu_warn(g, "Ignoring deprecated flag: " @@ -188,12 +190,12 @@ int nvgpu_vm_map_linux(struct vm_gk20a *vm, u32 page_size, s16 compr_kind, s16 incompr_kind, - enum gk20a_mem_rw_flag rw_flag, u64 buffer_offset, u64 mapping_size, struct vm_gk20a_mapping_batch *batch, u64 *gpu_va) { + enum gk20a_mem_rw_flag rw_flag = gk20a_mem_flag_none; struct gk20a *g = gk20a_from_vm(vm); struct device *dev = dev_from_gk20a(g); struct nvgpu_os_buffer os_buf; @@ -223,6 +225,10 @@ int nvgpu_vm_map_linux(struct vm_gk20a *vm, goto clean_up; } + if (flags & NVGPU_VM_MAP_ACCESS_NO_WRITE) { + rw_flag = gk20a_mem_flag_read_only; + } + err = nvgpu_vm_map(vm, &os_buf, nvgpu_sgt, @@ -328,7 +334,6 @@ int nvgpu_vm_map_buffer(struct vm_gk20a *vm, nvgpu_vm_translate_linux_flags(g, flags), page_size, compr_kind, incompr_kind, - gk20a_mem_flag_none, buffer_offset, mapping_size, batch, diff --git a/include/uapi/linux/nvgpu.h b/include/uapi/linux/nvgpu.h index 0bbebe972..16ee496eb 100644 --- a/include/uapi/linux/nvgpu.h +++ b/include/uapi/linux/nvgpu.h @@ -1918,6 +1918,7 @@ struct nvgpu_as_bind_channel_args { #define NVGPU_AS_MAP_BUFFER_FLAGS_L3_ALLOC (1 << 7) #define NVGPU_AS_MAP_BUFFER_FLAGS_DIRECT_KIND_CTRL (1 << 8) #define NVGPU_AS_MAP_BUFFER_FLAGS_PLATFORM_ATOMIC (1 << 9) +#define NVGPU_AS_MAP_BUFFER_FLAGS_ACCESS_NO_WRITE (1 << 10) /* * VM map buffer IOCTL