From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C25A1606A31A for ; Wed, 2 Dec 2026 12:22:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=amd.com header.i=@amd.com header.a=rsa-sha256 header.s=selector1 header.b=Oq7ai+Sz Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 60EC310E3EB; Wed, 2 Dec 2026 12:22:23 +0000 (UTC) Received: from PH7PR06CU001.outbound.protection.outlook.com (mail-westus3azon11010034.outbound.protection.outlook.com [52.101.201.34]) by gabe.freedesktop.org (Postfix) with ESMTPS id 4A5CC10E3EB for ; Wed, 2 Dec 2026 12:22:17 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=uSphu+9D4G/ZyRvWNGzMwjn3RzmePdMzVYMpDWhP0GgxinMpx1f3Fd4OGyl/er1PiEy4WfGmGZE7JgETfBFIbM7QcYw3KJR3xpirBY2UaNhMDiIuGRs2nr/QnnoSieFjcZlmk0G+kBKxfJK5I9Ag0Eox7vf+tNkDUqooyTPrc/IUjoXVZMSttCikpnePxCfUjEOJNx2sQeB1bnMbZkFzHqk/q17k7N5ouRUWgb+YL+peiT6yidhMdwYkhw2YZdOWZps1Du93ImZIa2UdWIM/AAYURqsXu1a+Ey9FF7CIJ3xc68bcdJtsZP2l9BUGq/yzT8Kvp4taJ17fbA0R77v+Yw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=xo7kFGX+QXmU6GSfCb/DYGmn9K4fszKQMAfPwtUd0a0=; b=p35YSEbWyP7U/Q4Bg9CpkKI1nrpmGqIcY0rgWpJeH2/4V2SjynA6oF9nZXHgO7Td7M96E4n1UebizNcGaia9C1RRrjQHWrzmxQtPe1uNIczZV0Bi8TRin3yti9w+sthDliLYBVS93JB8AjXXK8dk5KDHpcAn3qw5zbE6Hdt5ixYvlVH1e0CD8MiOTSTCV6rvSOYz7MVcOCyu6XltT+fRRkFUag4GEeYIw2e5sDU+iquAFmw8Yc2o0k0lDW7FiDNhtySTDY+f7vVjBwoavt1oZmbwrJvQYW69AuQQCRVb+qScPHqcWM6vfjOioQnNF4zPMUeE+ZluUwlpRobkcbsv2w== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=xo7kFGX+QXmU6GSfCb/DYGmn9K4fszKQMAfPwtUd0a0=; b=Oq7ai+Sz5bzmHmS9vtBshZqrpP+RLzZ/fEOPk3FvfaaXPVph+FRlu8hKEwhWoCLaaqaVrApvgU9S0RPsCtsLgToO/DOqOSzFZMYcsp2YtLwXFJ+ynI6pdPzHtOGcCgXAc7XtvArVfrOTFNuh1v0OwSl90fdHG0lz/Bh52Qs1Xow= Authentication-Results: mx.microsoft.com 1; dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=amd.com; Received: from PH7PR12MB5685.namprd12.prod.outlook.com (2603:10b6:510:13c::22) by CY5PR12MB6204.namprd12.prod.outlook.com (2603:10b6:930:23::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.688.10; Wed, 2 Dec 2026 12:22:07 +0000 Received: from PH7PR12MB5685.namprd12.prod.outlook.com ([fe80::ce69:cfae:774d:a65c]) by PH7PR12MB5685.namprd12.prod.outlook.com ([fe80::ce69:cfae:774d:a65c%3]) with mapi id 15.21.0662.007; Wed, 2 Dec 2026 12:22:07 +0000 Message-ID: Date: Wed, 2 Dec 2026 13:22:02 +0100 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] drm/amdkfd: add in-place userptr write access upgrade To: Junhua Shen , Felix Kuehling Cc: amd-gfx@lists.freedesktop.org, Philip Yang , Alex Deucher , Chris Freehill , Kent Russell , Maneesh Gupta , Denny Iriawan , David Yat Sin , Huang Rui , Jenny Liu , Zhu Lingshan , Honglei Huang , Yiru Ma References: <20261201112332.44784-1-Junhua.Shen@amd.com> <98d1d9f9-7e34-4d53-b419-7ef534093d1c@amd.com> <6e6abe45-b6d6-4647-8de2-3401800c5f30@amd.com> <20261202095040.94859-1-Junhua.Shen@amd.com> Content-Language: en-US From: =?UTF-8?Q?Christian_K=C3=B6nig?= In-Reply-To: <20261202095040.94859-1-Junhua.Shen@amd.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-ClientProxiedBy: FR4P281CA0090.DEUP281.PROD.OUTLOOK.COM (2603:10a6:d10:cd::16) To PH7PR12MB5685.namprd12.prod.outlook.com (2603:10b6:510:13c::22) X-Archived-By: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: PH7PR12MB5685:EE_|CY5PR12MB6204:EE_ X-MS-Office365-Filtering-Correlation-Id: f811ad01-ec71-4640-f19e-08df506ee1bb X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|366016|23010399003|1800799024|376014|22082099003|18002099003|10067099003|56012099006|261028144727099003|261021123027099003|261102123227099003|261021121427099003|261022075727099003|261022105027099003|261026121527099003|261026140227099003|261009221227099003|261009221727099003|261009220027099003|261009223327099003|261009223027099003|4143699003|5023799004|11063799006|6133799003|3023799007; X-Microsoft-Antispam-Message-Info: FkT/Uo7KZm5j38trakPNaV413vled8KpBNt1SptmzaYzSvR4v1Rse6KlTlupfO+nYDL2N9DODkEZifC3NLqGiokM8vPJFSTDfvtG+Y2xX+mdE/yZ/igUm99Qh+7rRHiYvDhT3T9INmW5vSanpJKUKGpk3po3UpAA50n1xsEtflDimyMJN5vg/zXY3Yrf5N8phofVooQHcD27juPDvuM5GrsEQCodUEFlaSnGeHS1KSRmJ43j+wRrE2u/B8LtbjnYmdC7CqkWMkNeZBoPSZ7gw1VHn8nJ50NYT8nYd/+Y/oObhAojdigNFjEkaY7EMcUYsrxnicUfMb3obsLD1TyK6hQXT0AqrMipHbB8ME5J2b4Y+uESwckurn3nR/fPCO7sNO5EWoB48/Zw5f8aOzE8qL/dI/CO8ZllCF3a1jOS4vZhFuo1HWI5uUwMovPD8Vf8PECch3LrNTSnW7IsmWffJlt2INR7E0jLf67WmfmhYygVs3QKutCKZ9JJAwYCx41SBJxpMuyLuw5/zuZjlPxVhZw+cem8ZIY0uD8EL5y20SUzaDfgP3qVBNvETOqRWGHuQNboiDHR/S2k4BUHuCGXtdy80j2y8/khZtlMsaIlVJFw0hdrck0Z9hYVjs+bHo/w X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:PH7PR12MB5685.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230040)(366016)(23010399003)(1800799024)(376014)(22082099003)(18002099003)(10067099003)(56012099006)(261028144727099003)(261021123027099003)(261102123227099003)(261021121427099003)(261022075727099003)(261022105027099003)(261026121527099003)(261026140227099003)(261009221227099003)(261009221727099003)(261009220027099003)(261009223327099003)(261009223027099003)(4143699003)(5023799004)(11063799006)(6133799003)(3023799007); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-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2MCtCcEhVOGhRQVNlRUowRnpUZkhOWlhlN2lNUTdUR2g5?= =?utf-8?B?OUU3SW0ycFFFT05WQ0diYWRlQkhQQmJMMUR5RWFZc3RjMFFzWjkyVTlWRUxH?= =?utf-8?B?b0VRTjJkUmUwM3FIQWtyZ3VYRlJoMHByVDhNOTNpZ2Y2R29pRWJta2laa1g3?= =?utf-8?B?bVlTRzVXdnp1RzJRL0t1OXlrc0l2WTFpNktFZUVsTENwdlZpUmdWZEdEaXVN?= =?utf-8?B?N3FYY25CSUZRMTlwdEtrL2c2aVVwTHlpODFtQjJ2K2ZOampQWThTUFM5djZs?= =?utf-8?B?WmlZYnJWdWlMNXEzbDI4VFZiZkFHLzFGOWtsUEVHakhjRUxuUjVJMUtSSVJz?= =?utf-8?B?dTJGRjRSRktJcXY4cG5sNmJnVno3NFg1d2pCQVU4T3ovQ05rVXpoYmlyYk4y?= =?utf-8?B?cnpTamFmTnFRTFltN3hSRE1RbStudXNnMDhzZWZVOWdQQmdWNW44b1pWWXc2?= =?utf-8?B?RGhBeGpKcmUybCtzUGFzdU5yTUlJcVg4MDFIM3Q2WmpvODNSL3VKVFJpU3Js?= =?utf-8?Q?j66876Bz6w6JhnldQA?= X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: f811ad01-ec71-4640-f19e-08df506ee1bb X-MS-Exchange-CrossTenant-AuthSource: PH7PR12MB5685.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Dec 2026 12:22:07.5447 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: wWf4tkx74oFEKo0EBH24Na0kw+jL793SHjXzcyXoNIEQuc2t1jnGCOmHcU0gF7NF X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY5PR12MB6204 X-BeenThere: amd-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Discussion list for AMD gfx List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "amd-gfx" X-Spam-Status: No, score=-22.3 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI, MENTIONS_GIT_HOSTING,SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_1 autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 On 12/2/26 10:50, Junhua Shen wrote: > On Tue, 01 Dec 2026 14:04:02 -0500, Felix Kuehling wrote: >> >> On 2026-12-01 09:16, Christian König wrote: >>> On 12/1/26 12:23, Junhua Shen wrote: >>>> Userptr BOs can be registered read-only so that pinning host memory for >>>> device access does not request write permission, which would, for >>>> example, break the copy-on-write sharing of a private file mapping. >>>> When the same host range later needs write access, the only option today >>>> is to unregister and re-register it, which recreates the BO and can move >>>> its GPU virtual address. >>>> >>>> Add AMDKFD_IOC_UPGRADE_USERPTR_WRITABLE to upgrade an existing read-only >>>> userptr BO to read/write in place, without changing its VA or recreating >>>> the BO. On failure the BO keeps its original read-only state. >>> Yeah that is clearly not somethign we can do. >>> >>>> Signed-off-by: Junhua Shen >>>> --- >>>> Background: >>>> In ROCm rocm-systems PR #15545 [1], the runtime changes host-to-device >>>> source pins from writable to read-only. Previously, those writable pins >>>> requested write permission even though the device only reads the buffer. >>>> On a MAP_PRIVATE file mapping, e.g. mmapped model weights, this breaks >>>> copy-on-write: every page is copied into anonymous memory, and for >>>> userptr the resulting MMU notifier evicts the process queues, repeatedly >>>> during a single model load. >>>> >>>> These read-only pins will be made internally by the runtime and avoid >>>> the COW copies, their memory overhead and the resulting queue evictions. >>>> However, the same host range can concurrently be registered writable by >>>> another user, such as hipHostRegister() or a device-to-host staging pin. >>> That is an illegal operation and needs to be rejected somehow. >>> >>>> User space shares one BO per range and reference-counts it. With SVM, a >>>> writable request just clears GPU_RO on the existing range. With >>>> userptr, the read-only BO can only be freed and re-allocated, which >>>> recreates the BO and can move its GPU VA while other registrants still >>>> use it, so user space has to fail the writable request. This ioctl lets >>>> userptr behave like SVM. >>> That this doesn't work is perfectly intentional behavior, mappings must be made either readonly or read/write but not mixed by the application. >>> >>> I think we rather need to fix the SVM side to return an error when a read-only range is made read-write without unmapping it in between. >> >> AFAICT this has nothing to do with SVM. This is about the legacy userptr >> API. ROCr shouldn't even use this on GPU+kernel combinations that >> support SVM. Therefore I strongly suspect that this kind of hack isn't >> needed in the upstream driver in the first place. >> >> >>> >>> So clear NAK from my side to this approach here. >> >> I agree. >> > Thanks Christian and Felix. > > Understood, I will focus on SVM path instead. Well what I wanted to point out is that the SVM path is most likely incorrect as well. When the application says something should be accessed only read-only and then later says that the same VA region should be accessed read/write without unmapping it in between then that is an application bug. Regards, Christian. > > Regards, > Junhua > >> Regards, >>   Felix >> >> >>> >>> Regards, >>> Christian. >>> >>>> This builds on the read-only userptr registration series: >>>> https://lore.kernel.org/all/20261117042051.768201-1-Junhua.Shen@amd.com/ >>>> Without it no KFD userptr is registered read-only and this ioctl is a no-op. >>>> >>>> Upgrade flow: >>>> - Reject non-writable or unmapped VMAs up front, before touching queues. >>>> - Evict the process queues, unmap the BO on every GPU where it is mapped, >>>> sync, flush TLBs and drop peer DMA mappings (still DMA_TO_DEVICE). >>>> - Re-fault the pages for write, unbind the old read-only GTT binding and >>>> rebind with DMA_BIDIRECTIONAL, then publish WRITABLE and refresh the >>>> cached attachment PTE flags. >>>> - Remap on the same GPUs, sync, flush TLBs and restore the queues. >>>> On any failure the BO keeps its read-only state and its original mappings >>>> are restored. -EAGAIN is propagated and retried by libhsakmt. >>>> >>>> User space and tests (ROCm rocm-systems [1]): >>>> - libhsakmt: use the ioctl when a read-only registered range is >>>> re-registered writable (fmm.c). >>>> - kfdtest: KFDMemoryTest.MemoryRegisterReadOnly and >>>> MemoryRegisterUserPtrUpgrade{,Unmapped,ProtReadFail,CowZeroPage, >>>> RemapAfterUnmap,ActiveQueue,MultiGPU}. >>>> >>>> [1] https://github.com/ROCm/rocm-systems/pull/15545 >>>> >>>> Tested on gfx1151 (single GPU): all of the above pass with HSA_USE_SVM=0; >>>> MultiGPU is skipped on single-GPU systems, so the multi-GPU path has not >>>> been exercised on hardware. >>>> >>>> drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h | 3 + >>>> .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 200 ++++++++++++++++++ >>>> drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 130 ++++++++++++ >>>> include/uapi/linux/kfd_ioctl.h | 17 +- >>>> 4 files changed, 348 insertions(+), 2 deletions(-) >>>> >>>> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h >>>> index 74dc307e5140..f1b899c18505 100644 >>>> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h >>>> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h >>>> @@ -337,6 +337,9 @@ int amdgpu_amdkfd_gpuvm_map_memory_to_gpu(struct amdgpu_device *adev, >>>> struct kgd_mem *mem, void *drm_priv); >>>> int amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu( >>>> struct amdgpu_device *adev, struct kgd_mem *mem, void *drm_priv); >>>> +int amdgpu_amdkfd_gpuvm_check_userptr_writable(struct kgd_mem *mem); >>>> +int amdgpu_amdkfd_gpuvm_upgrade_userptr_writable(struct amdgpu_device *adev, >>>> + struct kgd_mem *mem); >>>> int amdgpu_amdkfd_gpuvm_dmaunmap_mem(struct kgd_mem *mem, void *drm_priv); >>>> int amdgpu_amdkfd_gpuvm_sync_memory( >>>> struct amdgpu_device *adev, struct kgd_mem *mem, bool intr); >>>> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c >>>> index 342a7cefaacd..51de1ba19d60 100644 >>>> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c >>>> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c >>>> @@ -1937,6 +1937,206 @@ int amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu( >>>> return ret; >>>> } >>>> >>>> +/** >>>> + * amdgpu_amdkfd_gpuvm_check_userptr_writable - Fast pre-check that all VMAs >>>> + * backing a userptr BO are currently mapped and writable. >>>> + * @mem: KFD userptr BO to check >>>> + * >>>> + * Allows callers to reject non-writable (e.g. PROT_READ) or unmapped ranges >>>> + * before evicting user queues and tearing down GPU mappings. >>>> + * >>>> + * Return: 0 if the whole range is writable, -EPERM if a VMA is not writable, >>>> + * -EFAULT if part of the range is unmapped, or another negative error code. >>>> + */ >>>> +int amdgpu_amdkfd_gpuvm_check_userptr_writable(struct kgd_mem *mem) >>>> +{ >>>> + struct amdgpu_bo *bo = mem->bo; >>>> + struct vm_area_struct *vma; >>>> + struct mm_struct *mm; >>>> + unsigned long start, end, addr; >>>> + u64 user_addr = 0; >>>> + int ret; >>>> + >>>> + if (!bo || !amdgpu_ttm_tt_is_userptr(bo->tbo.ttm)) >>>> + return -EINVAL; >>>> + >>>> + ret = amdgpu_ttm_tt_get_userptr(&bo->tbo, &user_addr); >>>> + if (ret) >>>> + return ret; >>>> + >>>> + mm = bo->notifier.mm; >>>> + if (unlikely(!mm)) >>>> + return -EFAULT; >>>> + >>>> + if (!mmget_not_zero(mm)) >>>> + return -ESRCH; >>>> + >>>> + start = user_addr; >>>> + if (check_add_overflow(start, >>>> + (unsigned long)bo->tbo.ttm->num_pages << PAGE_SHIFT, >>>> + &end)) { >>>> + mmput(mm); >>>> + return -EFAULT; >>>> + } >>>> + >>>> + mmap_read_lock(mm); >>>> + addr = start; >>>> + do { >>>> + vma = vma_lookup(mm, addr); >>>> + if (!vma) { >>>> + ret = -EFAULT; >>>> + break; >>>> + } >>>> + if (!(vma->vm_flags & VM_WRITE)) { >>>> + ret = -EPERM; >>>> + break; >>>> + } >>>> + addr = min(end, vma->vm_end); >>>> + } while (addr < end); >>>> + mmap_read_unlock(mm); >>>> + mmput(mm); >>>> + >>>> + return ret; >>>> +} >>>> + >>>> +/** >>>> + * amdgpu_amdkfd_gpuvm_upgrade_userptr_writable - In-place read-only -> read/write >>>> + * upgrade of a userptr BO. >>>> + * @adev: amdgpu device >>>> + * @mem: KFD BO to upgrade (must be an unmapped userptr BO) >>>> + * >>>> + * Caller must have quiesced user queues, unmapped and DMA-unmapped all GPU >>>> + * mappings of @mem, and will remap them after this returns. On failure, the >>>> + * BO's read-only state is preserved so the caller can restore the original >>>> + * read-only mappings. >>>> + * >>>> + * Return: 0 on success or if the BO is already writable, negative error code >>>> + * on failure. >>>> + */ >>>> +int amdgpu_amdkfd_gpuvm_upgrade_userptr_writable(struct amdgpu_device *adev, >>>> + struct kgd_mem *mem) >>>> +{ >>>> + struct amdkfd_process_info *process_info = mem->process_info; >>>> + struct amdgpu_bo *bo = mem->bo; >>>> + struct ttm_operation_ctx ctx = { >>>> + .interruptible = false, >>>> + .no_wait_gpu = false, >>>> + }; >>>> + struct kfd_mem_attachment *entry; >>>> + struct amdgpu_hmm_range *range; >>>> + u64 user_addr = 0; >>>> + bool unbound = false; >>>> + int ret = 0; >>>> + >>>> + if (!bo || !amdgpu_ttm_tt_is_userptr(bo->tbo.ttm)) >>>> + return -EINVAL; >>>> + >>>> + /* Already writable: nothing to do. */ >>>> + if (!amdgpu_ttm_tt_is_readonly(bo->tbo.ttm)) >>>> + return 0; >>>> + >>>> + ret = amdgpu_ttm_tt_get_userptr(&bo->tbo, &user_addr); >>>> + if (ret) >>>> + return ret; >>>> + >>>> + mutex_lock(&process_info->lock); >>>> + >>>> + /* Clear the read-only flag and re-fault the user pages for write >>>> + * before touching the BO's placement, so a failure (e.g. -EPERM on a >>>> + * PROT_READ VMA or -EFAULT) leaves the existing read-only BO intact. >>>> + */ >>>> + ret = amdgpu_ttm_tt_set_userptr(&bo->tbo, user_addr, 0); >>>> + if (ret) >>>> + goto out_unlock; >>>> + >>>> + range = amdgpu_hmm_range_alloc(NULL); >>>> + if (unlikely(!range)) { >>>> + ret = -ENOMEM; >>>> + goto out_restore_ro; >>>> + } >>>> + >>>> + ret = amdgpu_ttm_tt_get_user_pages(bo, range); >>>> + if (ret) { >>>> + if (ret == -EAGAIN || ret == -EPERM || ret == -EFAULT) >>>> + pr_debug("%s: Failed to get user pages: %d\n", >>>> + __func__, ret); >>>> + else >>>> + pr_err("%s: Failed to get user pages: %d\n", >>>> + __func__, ret); >>>> + goto out_free_range; >>>> + } >>>> + >>>> + ret = amdgpu_bo_reserve(bo, true); >>>> + if (ret) { >>>> + pr_err("%s: Failed to reserve BO\n", __func__); >>>> + goto out_free_range; >>>> + } >>>> + >>>> + /* Unbind the old GTT mapping with READONLY still set so TTM unmaps >>>> + * the SG table with its original DMA_TO_DEVICE direction, then rebind >>>> + * the writable pages with DMA_BIDIRECTIONAL. >>>> + */ >>>> + if (bo->tbo.resource->mem_type != TTM_PL_SYSTEM) { >>>> + amdgpu_ttm_tt_set_userptr(&bo->tbo, user_addr, >>>> + AMDGPU_GEM_USERPTR_READONLY); >>>> + amdgpu_bo_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_CPU); >>>> + ret = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx); >>>> + if (ret) { >>>> + pr_err("%s: failed to unbind RO userptr BO\n", __func__); >>>> + amdgpu_bo_unreserve(bo); >>>> + amdgpu_hmm_range_free(range); >>>> + goto out_unlock; >>>> + } >>>> + unbound = true; >>>> + amdgpu_ttm_tt_set_userptr(&bo->tbo, user_addr, 0); >>>> + } >>>> + >>>> + amdgpu_ttm_tt_set_user_pages(bo->tbo.ttm, range); >>>> + amdgpu_bo_placement_from_domain(bo, mem->domain); >>>> + ret = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx); >>>> + if (ret) { >>>> + pr_err("%s: failed to validate BO: %d\n", __func__, ret); >>>> + amdgpu_ttm_tt_set_userptr(&bo->tbo, user_addr, >>>> + AMDGPU_GEM_USERPTR_READONLY); >>>> + if (unbound) { >>>> + /* Re-bind read-only so the BO is not stranded in >>>> + * TTM_PL_SYSTEM when the caller remaps it. >>>> + */ >>>> + amdgpu_bo_placement_from_domain(bo, mem->domain); >>>> + if (ttm_bo_validate(&bo->tbo, &bo->placement, &ctx)) >>>> + pr_err("%s: failed to restore RO userptr BO\n", >>>> + __func__); >>>> + } >>>> + amdgpu_bo_unreserve(bo); >>>> + amdgpu_hmm_range_free(range); >>>> + goto out_unlock; >>>> + } >>>> + amdgpu_bo_unreserve(bo); >>>> + amdgpu_hmm_range_free(range); >>>> + >>>> + /* Publish the writable state and refresh cached attachment PTE flags >>>> + * so the caller's remap installs writable PTEs. >>>> + */ >>>> + mutex_lock(&mem->lock); >>>> + mem->alloc_flags |= KFD_IOC_ALLOC_MEM_FLAGS_WRITABLE; >>>> + list_for_each_entry(entry, &mem->attachments, list) >>>> + entry->pte_flags = get_pte_flags(entry->adev, >>>> + entry->bo_va->base.vm, mem); >>>> + mutex_unlock(&mem->lock); >>>> + >>>> + mutex_unlock(&process_info->lock); >>>> + return 0; >>>> + >>>> +out_free_range: >>>> + amdgpu_hmm_range_free(range); >>>> +out_restore_ro: >>>> + amdgpu_ttm_tt_set_userptr(&bo->tbo, user_addr, >>>> + AMDGPU_GEM_USERPTR_READONLY); >>>> +out_unlock: >>>> + mutex_unlock(&process_info->lock); >>>> + return ret; >>>> +} >>>> + >>>> int amdgpu_amdkfd_gpuvm_free_memory_of_gpu( >>>> struct amdgpu_device *adev, struct kgd_mem *mem, void *drm_priv, >>>> uint64_t *size) >>>> diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c >>>> index 3d2136adb62b..05898e8b5064 100644 >>>> --- a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c >>>> +++ b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c >>>> @@ -1277,6 +1277,133 @@ static int kfd_ioctl_alloc_memory_of_gpu(struct file *filep, >>>> return err; >>>> } >>>> >>>> +/* Upgrade an already registered userptr BO from read-only to read/write in >>>> + * place. >>>> + */ >>>> +static int kfd_ioctl_upgrade_userptr_writable(struct file *filep, >>>> + struct kfd_process *p, void *data) >>>> +{ >>>> + struct kfd_ioctl_upgrade_userptr_writable_args *args = data; >>>> + struct kfd_process_device *pdd, *peer_pdd; >>>> + DECLARE_BITMAP(bitmap_mapped, MAX_GPU_INSTANCE); >>>> + DECLARE_BITMAP(bitmap_unmapped, MAX_GPU_INSTANCE); >>>> + bool has_mapped = false; >>>> + struct kgd_mem *mem; >>>> + long err = 0; >>>> + int i; >>>> + >>>> + mutex_lock(&p->mutex); >>>> + >>>> + pdd = kfd_process_device_data_by_id(p, GET_GPU_ID(args->handle)); >>>> + if (!pdd) { >>>> + err = -EINVAL; >>>> + goto err_unlock; >>>> + } >>>> + >>>> + mem = kfd_process_device_translate_handle(pdd, GET_IDR_HANDLE(args->handle)); >>>> + if (!mem) { >>>> + err = -EINVAL; >>>> + goto err_unlock; >>>> + } >>>> + >>>> + if (!(mem->alloc_flags & KFD_IOC_ALLOC_MEM_FLAGS_USERPTR)) { >>>> + err = -EINVAL; >>>> + goto err_unlock; >>>> + } >>>> + >>>> + if (mem->alloc_flags & KFD_IOC_ALLOC_MEM_FLAGS_WRITABLE) >>>> + goto err_unlock; >>>> + >>>> + /* Fast pre-check: reject non-writable or unmapped VMAs before >>>> + * evicting user queues and tearing down GPU mappings. >>>> + */ >>>> + err = amdgpu_amdkfd_gpuvm_check_userptr_writable(mem); >>>> + if (err) >>>> + goto err_unlock; >>>> + >>>> + bitmap_zero(bitmap_mapped, MAX_GPU_INSTANCE); >>>> + bitmap_zero(bitmap_unmapped, MAX_GPU_INSTANCE); >>>> + if (mem->mapped_to_gpu_memory) { >>>> + for (i = 0; i < p->n_pdds; i++) { >>>> + if (p->pdds[i]->drm_priv && >>>> + amdgpu_amdkfd_bo_mapped_to_dev(p->pdds[i]->drm_priv, >>>> + mem)) >>>> + set_bit(i, bitmap_mapped); >>>> + } >>>> + has_mapped = !bitmap_empty(bitmap_mapped, p->n_pdds); >>>> + } >>>> + >>>> + if (has_mapped) { >>>> + /* Stop all user queues before tearing down the read-only >>>> + * mappings so no in-flight GPU access faults on unmapped PTEs. >>>> + */ >>>> + err = kfd_process_evict_queues(p, KFD_QUEUE_EVICTION_TRIGGER_USERPTR); >>>> + if (err) >>>> + goto err_unlock; >>>> + >>>> + for_each_set_bit(i, bitmap_mapped, p->n_pdds) { >>>> + peer_pdd = p->pdds[i]; >>>> + err = amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu(peer_pdd->dev->adev, >>>> + mem, peer_pdd->drm_priv); >>>> + if (err) >>>> + break; >>>> + set_bit(i, bitmap_unmapped); >>>> + } >>>> + >>>> + /* Sync, flush TLBs, and drop peer DMA mappings for every GPU >>>> + * actually unmapped above before either upgrading or remapping, >>>> + * so dmaunmap uses the original DMA_TO_DEVICE direction and >>>> + * remap will not leak an existing peer SG table. >>>> + */ >>>> + if (!bitmap_empty(bitmap_unmapped, p->n_pdds)) { >>>> + long sync_err; >>>> + >>>> + sync_err = amdgpu_amdkfd_gpuvm_sync_memory(pdd->dev->adev, mem, false); >>>> + if (sync_err && !err) >>>> + err = sync_err; >>>> + >>>> + for_each_set_bit(i, bitmap_unmapped, p->n_pdds) { >>>> + peer_pdd = p->pdds[i]; >>>> + kfd_flush_tlb(peer_pdd); >>>> + amdgpu_amdkfd_gpuvm_dmaunmap_mem(mem, peer_pdd->drm_priv); >>>> + } >>>> + } >>>> + } >>>> + >>>> + if (!err) >>>> + err = amdgpu_amdkfd_gpuvm_upgrade_userptr_writable(pdd->dev->adev, mem); >>>> + >>>> + if (has_mapped) { >>>> + long map_err; >>>> + >>>> + /* Restore mappings on every GPU that was unmapped: writable >>>> + * PTEs on success, or the original read-only PTEs on rollback. >>>> + */ >>>> + for_each_set_bit(i, bitmap_unmapped, p->n_pdds) { >>>> + peer_pdd = p->pdds[i]; >>>> + map_err = amdgpu_amdkfd_gpuvm_map_memory_to_gpu(peer_pdd->dev->adev, >>>> + mem, peer_pdd->drm_priv); >>>> + if (map_err && !err) >>>> + err = map_err; >>>> + } >>>> + >>>> + if (!bitmap_empty(bitmap_unmapped, p->n_pdds)) { >>>> + map_err = amdgpu_amdkfd_gpuvm_sync_memory(pdd->dev->adev, mem, false); >>>> + if (map_err && !err) >>>> + err = map_err; >>>> + >>>> + for_each_set_bit(i, bitmap_unmapped, p->n_pdds) >>>> + kfd_flush_tlb(p->pdds[i]); >>>> + } >>>> + >>>> + kfd_process_restore_queues(p); >>>> + } >>>> + >>>> +err_unlock: >>>> + mutex_unlock(&p->mutex); >>>> + return err; >>>> +} >>>> + >>>> static int kfd_ioctl_free_memory_of_gpu(struct file *filep, >>>> struct kfd_process *p, void *data) >>>> { >>>> @@ -3682,6 +3809,9 @@ static const struct amdkfd_ioctl_desc amdkfd_ioctls[] = { >>>> >>>> AMDKFD_IOCTL_DEF(AMDKFD_IOC_PROFILER, >>>> kfd_ioctl_profiler, 0), >>>> + >>>> + AMDKFD_IOCTL_DEF(AMDKFD_IOC_UPGRADE_USERPTR_WRITABLE, >>>> + kfd_ioctl_upgrade_userptr_writable, 0), >>>> }; >>>> >>>> #define AMDKFD_CORE_IOCTL_COUNT ARRAY_SIZE(amdkfd_ioctls) >>>> diff --git a/include/uapi/linux/kfd_ioctl.h b/include/uapi/linux/kfd_ioctl.h >>>> index 3d608d6b0ab8..d08166fe98fd 100644 >>>> --- a/include/uapi/linux/kfd_ioctl.h >>>> +++ b/include/uapi/linux/kfd_ioctl.h >>>> @@ -50,9 +50,10 @@ >>>> * - 1.22 - Add queue creation with metadata ring base address >>>> * - 1.23 - Add profiler control ioctl to enable/disable profiler on a process >>>> * - 1.24 - Enable SVM no-access attributes >>>> + * - 1.25 - Add userptr read-only -> read/write upgrade ioctl >>>> */ >>>> #define KFD_IOCTL_MAJOR_VERSION 1 >>>> -#define KFD_IOCTL_MINOR_VERSION 24 >>>> +#define KFD_IOCTL_MINOR_VERSION 25 >>>> >>>> struct kfd_ioctl_get_version_args { >>>> __u32 major_version; /* from KFD */ >>>> @@ -449,6 +450,15 @@ struct kfd_ioctl_alloc_memory_of_gpu_args { >>>> __u32 flags; >>>> }; >>>> >>>> +/* Upgrade an already registered userptr BO from read-only to read/write in place >>>> + * >>>> + * Rebuilds the GPU mapping with write permission without a free/re-alloc; user >>>> + * space does not unmap. Only the read-only -> read/write direction is supported. >>>> + */ >>>> +struct kfd_ioctl_upgrade_userptr_writable_args { >>>> + __u64 handle; /* to KFD */ >>>> +}; >>>> + >>>> /* Free memory allocated with kfd_ioctl_alloc_memory_of_gpu >>>> * >>>> * @handle: memory handle returned by alloc >>>> @@ -1723,7 +1733,10 @@ struct kfd_ioctl_profiler_args { >>>> #define AMDKFD_IOC_PROFILER \ >>>> AMDKFD_IOWR(0x28, struct kfd_ioctl_profiler_args) >>>> >>>> +#define AMDKFD_IOC_UPGRADE_USERPTR_WRITABLE \ >>>> + AMDKFD_IOW(0x29, struct kfd_ioctl_upgrade_userptr_writable_args) >>>> + >>>> #define AMDKFD_COMMAND_START 0x01 >>>> -#define AMDKFD_COMMAND_END 0x29 >>>> +#define AMDKFD_COMMAND_END 0x2A >>>> >>>> #endif