From patchwork Fri Feb 14 20:55:20 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Adri=C3=A1n_Larumbe?= X-Patchwork-Id: 13975625 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id E92CCC021A4 for ; Fri, 14 Feb 2025 21:00:35 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 4673710E037; Fri, 14 Feb 2025 21:00:35 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (1024-bit key; unprotected) header.d=collabora.com header.i=adrian.larumbe@collabora.com header.b="E5WKFmUI"; dkim-atps=neutral Received: from sender4-pp-f112.zoho.com (sender4-pp-f112.zoho.com [136.143.188.112]) by gabe.freedesktop.org (Postfix) with ESMTPS id 3A03810E037 for ; Fri, 14 Feb 2025 21:00:34 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; t=1739566822; cv=none; d=zohomail.com; s=zohoarc; b=TTH20RyKACG3RKP2ks2FvVdL1gk9qJMPOFAVzmfxi6fuFO04Rmtb7FGFsbtLBiHAwmmZeP/u1Dvmj3PKrdSilsZH0dwpvtqBUin6HiKlMVQjC6wBKTqaRbaZMMLB62S5PDvwGOMpKSt01teIaMRFpWtRY9NX8ZM+kv8Y6yuoDJQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1739566822; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:MIME-Version:Message-ID:Subject:Subject:To:To:Message-Id:Reply-To; bh=pGxqVtiyhh2WTau1AC7TbQOOxWOmbP85rJg2FnEKnpc=; b=Vxd5PowxPM6V0Z/Mf/0XzKLdpe+Q+7VXrvQVC6zc/aNXduF7qC9RHJguhs6eCiaHRR3HEKv7RYV9F4NTSYV7+RS3eFhg8wL1/wexkoB5/xwF4K9Eo1iW3VNZFTve+Vu7KsxmLCVtiqXSQmsXvtJnHjBIX2CilwN7OXJzM/SFXjM= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=collabora.com; spf=pass smtp.mailfrom=adrian.larumbe@collabora.com; dmarc=pass header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1739566822; s=zohomail; d=collabora.com; i=adrian.larumbe@collabora.com; h=From:From:To:To:Cc:Cc:Subject:Subject:Date:Date:Message-ID:MIME-Version:Content-Type:Content-Transfer-Encoding:Message-Id:Reply-To; bh=pGxqVtiyhh2WTau1AC7TbQOOxWOmbP85rJg2FnEKnpc=; b=E5WKFmUIEx3ndoXt0EX9dNciuLtGlBvTw+n2Hu3jQg3uhzyPqUfORLj5AyQnmwYC k13V1YpZqV8Og+S4Y6hf/kUsBawQqn7WQasmrwMMC7avWu+opJ6zgCVQ4tkeHGYFz2m h5uu/HIQ6ymqX4czr7SyI0p9u0QeaqqHvLVZ90UI= Received: by mx.zohomail.com with SMTPS id 1739566819232123.35089074976884; Fri, 14 Feb 2025 13:00:19 -0800 (PST) From: =?utf-8?q?Adri=C3=A1n_Larumbe?= To: Boris Brezillon , Steven Price , Liviu Dudau , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Simona Vetter Cc: kernel@collabora.com, =?utf-8?q?Adri=C3=A1n_Larumbe?= , dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 1/2] drm/panthor: Replace sleep locks with spinlocks in fdinfo path Date: Fri, 14 Feb 2025 20:55:20 +0000 Message-ID: <20250214210009.1994543-1-adrian.larumbe@collabora.com> X-Mailer: git-send-email 2.47.1 MIME-Version: 1.0 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" Commit 0590c94c3596 ("drm/panthor: Fix race condition when gathering fdinfo group samples") introduced an xarray lock to deal with potential use-after-free errors when accessing groups fdinfo figures. However, this toggles the kernel's atomic context status, so the next nested mutex lock will raise a warning when the kernel is compiled with mutex debug options: CONFIG_DEBUG_RT_MUTEXES=y CONFIG_DEBUG_MUTEXES=y Replace Panthor's group fdinfo data mutex with a guarded spinlock. Signed-off-by: Adrián Larumbe 0590c94c3596 ("drm/panthor: Fix race condition when gathering fdinfo group samples") Reviewed-by: Boris Brezillon --- drivers/gpu/drm/panthor/panthor_sched.c | 26 ++++++++++++------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/drivers/gpu/drm/panthor/panthor_sched.c b/drivers/gpu/drm/panthor/panthor_sched.c index 1a276db095ff..4d31d1967716 100644 --- a/drivers/gpu/drm/panthor/panthor_sched.c +++ b/drivers/gpu/drm/panthor/panthor_sched.c @@ -9,6 +9,7 @@ #include #include +#include #include #include #include @@ -631,10 +632,10 @@ struct panthor_group { struct panthor_gpu_usage data; /** - * @lock: Mutex to govern concurrent access from drm file's fdinfo callback - * and job post-completion processing function + * @fdinfo.lock: Spinlock to govern concurrent access from drm file's fdinfo + * callback and job post-completion processing function */ - struct mutex lock; + spinlock_t lock; /** @fdinfo.kbo_sizes: Aggregate size of private kernel BO's held by the group. */ size_t kbo_sizes; @@ -910,8 +911,6 @@ static void group_release_work(struct work_struct *work) release_work); u32 i; - mutex_destroy(&group->fdinfo.lock); - for (i = 0; i < group->queue_count; i++) group_free_queue(group, group->queues[i]); @@ -2861,12 +2860,12 @@ static void update_fdinfo_stats(struct panthor_job *job) struct panthor_job_profiling_data *slots = queue->profiling.slots->kmap; struct panthor_job_profiling_data *data = &slots[job->profiling.slot]; - mutex_lock(&group->fdinfo.lock); - if (job->profiling.mask & PANTHOR_DEVICE_PROFILING_CYCLES) - fdinfo->cycles += data->cycles.after - data->cycles.before; - if (job->profiling.mask & PANTHOR_DEVICE_PROFILING_TIMESTAMP) - fdinfo->time += data->time.after - data->time.before; - mutex_unlock(&group->fdinfo.lock); + scoped_guard(spinlock, &group->fdinfo.lock) { + if (job->profiling.mask & PANTHOR_DEVICE_PROFILING_CYCLES) + fdinfo->cycles += data->cycles.after - data->cycles.before; + if (job->profiling.mask & PANTHOR_DEVICE_PROFILING_TIMESTAMP) + fdinfo->time += data->time.after - data->time.before; + } } void panthor_fdinfo_gather_group_samples(struct panthor_file *pfile) @@ -2880,12 +2879,11 @@ void panthor_fdinfo_gather_group_samples(struct panthor_file *pfile) xa_lock(&gpool->xa); xa_for_each(&gpool->xa, i, group) { - mutex_lock(&group->fdinfo.lock); + guard(spinlock)(&group->fdinfo.lock); pfile->stats.cycles += group->fdinfo.data.cycles; pfile->stats.time += group->fdinfo.data.time; group->fdinfo.data.cycles = 0; group->fdinfo.data.time = 0; - mutex_unlock(&group->fdinfo.lock); } xa_unlock(&gpool->xa); } @@ -3537,7 +3535,7 @@ int panthor_group_create(struct panthor_file *pfile, mutex_unlock(&sched->reset.lock); add_group_kbo_sizes(group->ptdev, group); - mutex_init(&group->fdinfo.lock); + spin_lock_init(&group->fdinfo.lock); return gid; From patchwork Fri Feb 14 20:55:21 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Adri=C3=A1n_Larumbe?= X-Patchwork-Id: 13975626 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 27BEBC02198 for ; Fri, 14 Feb 2025 21:01:08 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 9BE4810E0B4; Fri, 14 Feb 2025 21:01:07 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (1024-bit key; unprotected) header.d=collabora.com header.i=adrian.larumbe@collabora.com header.b="V4+/2yiH"; dkim-atps=neutral Received: from sender4-pp-f112.zoho.com (sender4-pp-f112.zoho.com [136.143.188.112]) by gabe.freedesktop.org (Postfix) with ESMTPS id 9384110E0B4 for ; Fri, 14 Feb 2025 21:01:06 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; t=1739566824; cv=none; d=zohomail.com; s=zohoarc; b=WsX1oLnhjvUPVAMV6CPsMZWwHXSzCRoPUlcCbFkX1iVgiZFKaiYoGjNyqWGpS1XFF1pJCHRHPhNtI5Q2ZvQhua9YHfbbXKtsOIslON4VlGv63FKEn7ziLw5yF+nXEVeSfZq/KBU+vyUAHQP+hZtiCbxqeK8R80NZD6siO0NwH8k= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1739566824; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:Subject:To:To:Message-Id:Reply-To; bh=JWDjD2ykSNfdny1OGAbJaEOdyCiHP6cpZeQw+/uPvDY=; b=d7LxqseOVrrK5RJLWM4p15VVnw4/v+rW1z7ZoleA6q/pueCQ0SAlJMjRv8sbJapO9JslNl+U5owmn9a+llQGR7sHnm94mT2+sJOj2lXM5BgrEnp8zb2U67hMwL9m6X3keVLJAbj6ku1C1UWga63MJO0JU5Bp39/pRkOu8OflK/s= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=collabora.com; spf=pass smtp.mailfrom=adrian.larumbe@collabora.com; dmarc=pass header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1739566824; s=zohomail; d=collabora.com; i=adrian.larumbe@collabora.com; h=From:From:To:To:Cc:Cc:Subject:Subject:Date:Date:Message-ID:In-Reply-To:References:MIME-Version:Content-Type:Content-Transfer-Encoding:Message-Id:Reply-To; bh=JWDjD2ykSNfdny1OGAbJaEOdyCiHP6cpZeQw+/uPvDY=; b=V4+/2yiHXhSbtD3VXmryGatcLN4KjCtkZ0yOlgVp/g1Gpx+NvsCvGU/Ot5R0/qWE sBRYvlX7oiO5n90EAF10aOWwcT4eYoMMt8c2BzHWIHYNb+eX9vDS8DoNwHo9l34yb0W ZTQvq1eJJRPrNh9LrbtGw89LQe2YTiy7bmYB05vw= Received: by mx.zohomail.com with SMTPS id 1739566822837654.3062409366959; Fri, 14 Feb 2025 13:00:22 -0800 (PST) From: =?utf-8?q?Adri=C3=A1n_Larumbe?= To: Boris Brezillon , Steven Price , Liviu Dudau , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Simona Vetter , =?utf-8?q?Adri=C3=A1n_Larumbe?= , Mihail Atanassov Cc: kernel@collabora.com, dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 2/2] drm/panthor: Avoid sleep locking in the internal BO size path Date: Fri, 14 Feb 2025 20:55:21 +0000 Message-ID: <20250214210009.1994543-2-adrian.larumbe@collabora.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250214210009.1994543-1-adrian.larumbe@collabora.com> References: <20250214210009.1994543-1-adrian.larumbe@collabora.com> MIME-Version: 1.0 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" Commit 434e5ca5b5d7 ("drm/panthor: Expose size of driver internal BO's over fdinfo") locks the VMS xarray, to avoid UAF errors when the same VM is being concurrently destroyed by another thread. However, that puts the current thread in atomic context, which means taking the VMS' heap locks will trigger a warning as the thread is no longer allowed to sleep. Because in this case replacing the heap mutex with a spinlock isn't feasible, the fdinfo handler no longer traverses the list of heaps for every single VM associated with an open DRM file. Instead, when a new heap chunk is allocated, its size is accumulated into a VM-wide tally, which also makes the atomic context code path somewhat faster. Signed-off-by: Adrián Larumbe Fixes: 3e2c8c718567 ("drm/panthor: Expose size of driver internal BO's over fdinfo") --- drivers/gpu/drm/panthor/panthor_heap.c | 38 ++++++++------------------ drivers/gpu/drm/panthor/panthor_heap.h | 2 -- drivers/gpu/drm/panthor/panthor_mmu.c | 23 +++++++++++----- drivers/gpu/drm/panthor/panthor_mmu.h | 1 + 4 files changed, 28 insertions(+), 36 deletions(-) diff --git a/drivers/gpu/drm/panthor/panthor_heap.c b/drivers/gpu/drm/panthor/panthor_heap.c index db0285ce5812..e5e5953e4f87 100644 --- a/drivers/gpu/drm/panthor/panthor_heap.c +++ b/drivers/gpu/drm/panthor/panthor_heap.c @@ -127,6 +127,8 @@ static void panthor_free_heap_chunk(struct panthor_vm *vm, heap->chunk_count--; mutex_unlock(&heap->lock); + panthor_vm_heaps_size_accumulate(vm, -heap->chunk_size); + panthor_kernel_bo_destroy(chunk->bo); kfree(chunk); } @@ -180,6 +182,8 @@ static int panthor_alloc_heap_chunk(struct panthor_device *ptdev, heap->chunk_count++; mutex_unlock(&heap->lock); + panthor_vm_heaps_size_accumulate(vm, heap->chunk_size); + return 0; err_destroy_bo: @@ -389,6 +393,7 @@ int panthor_heap_return_chunk(struct panthor_heap_pool *pool, removed = chunk; list_del(&chunk->node); heap->chunk_count--; + panthor_vm_heaps_size_accumulate(chunk->bo->vm, -heap->chunk_size); break; } } @@ -560,6 +565,8 @@ panthor_heap_pool_create(struct panthor_device *ptdev, struct panthor_vm *vm) if (ret) goto err_destroy_pool; + panthor_vm_heaps_size_accumulate(vm, pool->gpu_contexts->obj->size); + return pool; err_destroy_pool: @@ -594,8 +601,11 @@ void panthor_heap_pool_destroy(struct panthor_heap_pool *pool) xa_for_each(&pool->xa, i, heap) drm_WARN_ON(&pool->ptdev->base, panthor_heap_destroy_locked(pool, i)); - if (!IS_ERR_OR_NULL(pool->gpu_contexts)) + if (!IS_ERR_OR_NULL(pool->gpu_contexts)) { + panthor_vm_heaps_size_accumulate(pool->gpu_contexts->vm, + -pool->gpu_contexts->obj->size); panthor_kernel_bo_destroy(pool->gpu_contexts); + } /* Reflects the fact the pool has been destroyed. */ pool->vm = NULL; @@ -603,29 +613,3 @@ void panthor_heap_pool_destroy(struct panthor_heap_pool *pool) panthor_heap_pool_put(pool); } - -/** - * panthor_heap_pool_size() - Calculate size of all chunks across all heaps in a pool - * @pool: Pool whose total chunk size to calculate. - * - * This function adds the size of all heap chunks across all heaps in the - * argument pool. It also adds the size of the gpu contexts kernel bo. - * It is meant to be used by fdinfo for displaying the size of internal - * driver BO's that aren't exposed to userspace through a GEM handle. - * - */ -size_t panthor_heap_pool_size(struct panthor_heap_pool *pool) -{ - struct panthor_heap *heap; - unsigned long i; - size_t size = 0; - - down_read(&pool->lock); - xa_for_each(&pool->xa, i, heap) - size += heap->chunk_size * heap->chunk_count; - up_read(&pool->lock); - - size += pool->gpu_contexts->obj->size; - - return size; -} diff --git a/drivers/gpu/drm/panthor/panthor_heap.h b/drivers/gpu/drm/panthor/panthor_heap.h index e3358d4e8edb..25a5f2bba445 100644 --- a/drivers/gpu/drm/panthor/panthor_heap.h +++ b/drivers/gpu/drm/panthor/panthor_heap.h @@ -27,8 +27,6 @@ struct panthor_heap_pool * panthor_heap_pool_get(struct panthor_heap_pool *pool); void panthor_heap_pool_put(struct panthor_heap_pool *pool); -size_t panthor_heap_pool_size(struct panthor_heap_pool *pool); - int panthor_heap_grow(struct panthor_heap_pool *pool, u64 heap_gpu_va, u32 renderpasses_in_flight, diff --git a/drivers/gpu/drm/panthor/panthor_mmu.c b/drivers/gpu/drm/panthor/panthor_mmu.c index 8c6fc587ddc3..9e48b34fcf80 100644 --- a/drivers/gpu/drm/panthor/panthor_mmu.c +++ b/drivers/gpu/drm/panthor/panthor_mmu.c @@ -347,6 +347,14 @@ struct panthor_vm { struct mutex lock; } heaps; + /** + * @fdinfo: VM-wide fdinfo fields. + */ + struct { + /** @fdinfo.heaps_size: Size of all chunks across all heaps in the pool. */ + atomic_t heaps_size; + } fdinfo; + /** @node: Used to insert the VM in the panthor_mmu::vm::list. */ struct list_head node; @@ -1541,6 +1549,8 @@ static void panthor_vm_destroy(struct panthor_vm *vm) vm->heaps.pool = NULL; mutex_unlock(&vm->heaps.lock); + atomic_set(&vm->fdinfo.heaps_size, 0); + drm_WARN_ON(&vm->ptdev->base, panthor_vm_unmap_range(vm, vm->base.mm_start, vm->base.mm_range)); panthor_vm_put(vm); @@ -1963,13 +1973,7 @@ void panthor_vm_heaps_sizes(struct panthor_file *pfile, struct drm_memory_stats xa_lock(&pfile->vms->xa); xa_for_each(&pfile->vms->xa, i, vm) { - size_t size = 0; - - mutex_lock(&vm->heaps.lock); - if (vm->heaps.pool) - size = panthor_heap_pool_size(vm->heaps.pool); - mutex_unlock(&vm->heaps.lock); - + size_t size = atomic_read(&vm->fdinfo.heaps_size); stats->resident += size; if (vm->as.id >= 0) stats->active += size; @@ -1977,6 +1981,11 @@ void panthor_vm_heaps_sizes(struct panthor_file *pfile, struct drm_memory_stats xa_unlock(&pfile->vms->xa); } +void panthor_vm_heaps_size_accumulate(struct panthor_vm *vm, ssize_t acc) +{ + atomic_add(acc, &vm->fdinfo.heaps_size); +} + static u64 mair_to_memattr(u64 mair, bool coherent) { u64 memattr = 0; diff --git a/drivers/gpu/drm/panthor/panthor_mmu.h b/drivers/gpu/drm/panthor/panthor_mmu.h index fc274637114e..29030384eafe 100644 --- a/drivers/gpu/drm/panthor/panthor_mmu.h +++ b/drivers/gpu/drm/panthor/panthor_mmu.h @@ -39,6 +39,7 @@ struct panthor_heap_pool * panthor_vm_get_heap_pool(struct panthor_vm *vm, bool create); void panthor_vm_heaps_sizes(struct panthor_file *pfile, struct drm_memory_stats *stats); +void panthor_vm_heaps_size_accumulate(struct panthor_vm *vm, ssize_t acc); struct panthor_vm *panthor_vm_get(struct panthor_vm *vm); void panthor_vm_put(struct panthor_vm *vm);