From patchwork Wed Jul 10 12:42:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Christian_K=C3=B6nig?= X-Patchwork-Id: 13729248 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 0C645C3DA42 for ; Wed, 10 Jul 2024 12:43:08 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 7D34A10E784; Wed, 10 Jul 2024 12:43:07 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="dpH3Gp8x"; dkim-atps=neutral Received: from mail-ed1-f53.google.com (mail-ed1-f53.google.com [209.85.208.53]) by gabe.freedesktop.org (Postfix) with ESMTPS id 025ED10E782 for ; Wed, 10 Jul 2024 12:43:06 +0000 (UTC) Received: by mail-ed1-f53.google.com with SMTP id 4fb4d7f45d1cf-58bac81f40bso8484190a12.1 for ; Wed, 10 Jul 2024 05:43:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1720615385; x=1721220185; darn=lists.freedesktop.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=4CxJJEdicDCb6IZeT3uMULK0SV5Cncp7AGUzQdXGY/s=; b=dpH3Gp8xXg27rBuT9ZO6fwUy1h+L/zlqW3cCzzrrNOJHuw0M7yUWk3xBplFD7+FL3f eMCH3VidrtvLqmq7rszs9hZh+81gHHgCVCQvn4Zgi5c7KJe2f5h2Nzu1TVcxc/fr67dr lWoJNK3s2FtqjjJqLH5MhtE7KFs6yviy9mTfSLUVXsiweYBYzcQyj9v6R3LUV+SvgyDv uV0hV/TTu06bG25yhBoMhM9/MJcdxtTNsmE9W0hXrdl395hC7PMzfcdT3gzOwM86Q0AC lvAxnU49wF/qKx6cpX2H9n31LxWTuKmPoitX8ED7qb5+Rm6dvyW5xlup+xSeRS1NLzrO DWeQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720615385; x=1721220185; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=4CxJJEdicDCb6IZeT3uMULK0SV5Cncp7AGUzQdXGY/s=; b=Qctxu5El8axvfBFcyBeZ7khDnsgJ2njiZrIUhTY0xJotH2xP/JB5IAqe8jc0NtM2KW vu3DlXW17H+BBG/CYstwOljkpIFvCu03tqr7+RuB/qhOVcNCS8EPSl0aMJ7/ZculnJTY 7gtPjdl5cAwhbY1BLn4CBWkcYVlQb/ID6boNycpHQrXiy7wqeDbJW1FjHdQkOp2be9Vm KMgb2Aw7VY/cQcNNTMN3Y46o/xSpnkibhHBFzB6MoQb5rbMpfafFGlFQngro8t2bd7W2 xBHfA+rsfpFuB96c+gNZ2p0V5VuRncp0FWUmQHzXoeE4C7jFBQ7PfuBzx9040jK48ApT 6fbA== X-Forwarded-Encrypted: i=1; AJvYcCU7eOuWSMLSEAythkRWTd0icIK1cmGhdhZRHfdtwTVFWuDtHWg43HVSfmXe+TW1up28QeDvitY5+s4x/X2fxzA/abpYbFKLG5u9D3PpXN/M X-Gm-Message-State: AOJu0Ywo96Nir3XmfqIB4JCxj+4phBMYPPNZvSxw17qlMx9X95XAMbvE 0zy908Zo69NSA2XrvdZVn49E8pcFikRR14ee63zxc1iWvk2QA6+h X-Google-Smtp-Source: AGHT+IE5etIXweaqq62z3jPiUqvyp41KQBN7iFif7bz5yhiAxsuI7UxbgemKOCShvKEwTUpd0fwezw== X-Received: by 2002:aa7:c35a:0:b0:57a:858d:20ca with SMTP id 4fb4d7f45d1cf-594bc7c7e77mr3355790a12.28.1720615384803; Wed, 10 Jul 2024 05:43:04 -0700 (PDT) Received: from able.fritz.box ([2a00:e180:1504:6f00:b310:1e44:6e5e:7646]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-594bb9604f7sm2171868a12.15.2024.07.10.05.43.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Jul 2024 05:43:04 -0700 (PDT) From: " =?utf-8?q?Christian_K=C3=B6nig?= " X-Google-Original-From: =?utf-8?q?Christian_K=C3=B6nig?= To: thomas.hellstrom@linux.intel.com, matthew.brost@intel.com, dri-devel@lists.freedesktop.org Subject: [PATCH 1/7] dma-buf/dma-resv: Introduce dma_resv_trylock_ctx() Date: Wed, 10 Jul 2024 14:42:55 +0200 Message-Id: <20240710124301.1628-2-christian.koenig@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240710124301.1628-1-christian.koenig@amd.com> References: <20240710124301.1628-1-christian.koenig@amd.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" From: Thomas Hellström For the drm_exec_trylock() functionality, there is a need to be able to trylock a dma-resv object as part of a drm_exec transaction. Therefore expose a variant of dma_resv_trylock that also takes a struct ww_acquire_ctx parameter. Cc: Christian König Cc: Somalapuram Amaranath Cc: Matthew Brost Cc: Cc: Signed-off-by: Thomas Hellström --- include/linux/dma-resv.h | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/include/linux/dma-resv.h b/include/linux/dma-resv.h index 8d0e34dad446..68dae8f2a22c 100644 --- a/include/linux/dma-resv.h +++ b/include/linux/dma-resv.h @@ -405,6 +405,27 @@ static inline int dma_resv_lock_slow_interruptible(struct dma_resv *obj, return ww_mutex_lock_slow_interruptible(&obj->lock, ctx); } +/** + * dma_resv_trylock_ctx - trylock the reservation object + * @obj: the reservation object + * @ctx: The ww acquire context or NULL. + * + * Tries to lock the reservation object for exclusive access and modification. + * Note, that the lock is only against other writers, readers will run + * concurrently with a writer under RCU. The seqlock is used to notify readers + * if they overlap with a writer. The context parameter ensures that other + * ww transactions can perform deadlock backoff if necessary, and that + * subsequent attempts to dma_resv_lock() @obj for @ctx will return + * -EALREADY. + * + * Return: true if the lock was acquired, false otherwise. + */ +static inline bool __must_check +dma_resv_trylock_ctx(struct dma_resv *obj, struct ww_acquire_ctx *ctx) +{ + return ww_mutex_trylock(&obj->lock, ctx); +} + /** * dma_resv_trylock - trylock the reservation object * @obj: the reservation object @@ -421,7 +442,7 @@ static inline int dma_resv_lock_slow_interruptible(struct dma_resv *obj, */ static inline bool __must_check dma_resv_trylock(struct dma_resv *obj) { - return ww_mutex_trylock(&obj->lock, NULL); + return dma_resv_trylock_ctx(obj, NULL); } /** From patchwork Wed Jul 10 12:42:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Christian_K=C3=B6nig?= X-Patchwork-Id: 13729249 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 28DDBC3DA42 for ; Wed, 10 Jul 2024 12:43:11 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 8664610E782; Wed, 10 Jul 2024 12:43:10 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="KU0hLPJV"; dkim-atps=neutral Received: from mail-ed1-f42.google.com (mail-ed1-f42.google.com [209.85.208.42]) by gabe.freedesktop.org (Postfix) with ESMTPS id 7700510E782 for ; Wed, 10 Jul 2024 12:43:07 +0000 (UTC) Received: by mail-ed1-f42.google.com with SMTP id 4fb4d7f45d1cf-585e774fd3dso8205330a12.0 for ; Wed, 10 Jul 2024 05:43:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1720615386; x=1721220186; darn=lists.freedesktop.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=zrGWK9ix3Z5llLRfYODIcW20ljsKJIUMR1M7aT6SnbA=; b=KU0hLPJV4fwNqxa6QkE9k1Ct+Ij7Mscg1r4ZuVOO0HJGoZwkRKUKslT2aXmw2LmOEV FijbsrDu8JVA6zI0Vg8iCK9J0jd0Zxt9e/IvZ4kMio/E3APSp/FvhxhxocnPVXdsSDwk gznGWSj2pLFuyDHf++I80gfN+9ri3uuZhHbeWmAjI6xQa+jAscA/ceIG5hWvHWRCOvGQ XM1yB1MC6cxyzeSkuCDs/PZuoiXSKJK0guEv8J36gQ6nqq8JXs9q4N09YI9v7+F0u7qy 6uVtITJNFnzuu9SctHI9ljiHooi9IOC+d3lUPNuDRHRvcOMVsJu1MJse18ek1RRtK8T0 PrUQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720615386; x=1721220186; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=zrGWK9ix3Z5llLRfYODIcW20ljsKJIUMR1M7aT6SnbA=; b=i53SyUrYorkezmOcOkoJomeAVMijtFjwm4+no7AyrlpZ92IPuVSEhsInOXKRMYgryj 8X1nvmCa+SSx7zjnf1eI0PfHMMUF4+EGnMZTzWleQ+9e7tyU/nmdXrPGEl6X2YZJymHW wxB2HAhxWDlZ0Pq/TvIzhMxb3ms0r2KKCXePClqicMyMRbshFZfb1WpiguuM/MYJP2kB hFry80hg69BD05T4RtMKFTIlEGblz39GJ7eyUL1Ljt6/n24CR5B5O8x00Ha2M7U34Wr1 KXsrCqIzjWHnhCXkxCH6E69MoQ8Vj0+g60ja9nzEGkeSgkmmgf7YABVtzsoUJbNxen7z wEqQ== X-Forwarded-Encrypted: i=1; AJvYcCXtKJh5/tKtJA9zus+cympdszTjhtbMzrq8+Z5m5xXzcwNz4tEVbgtXCl73qI1mspV9ViZ1/JXglHDWP3BuEtBwPgWRZOpkflEdE3IEINVb X-Gm-Message-State: AOJu0YxDURSQmQ1ag2eiVyqGg1fJ4Ae3jvFamxuu2T3C8hX0WWQbaMLy KNcrXjqMLaz7vFw40/ZpQnUJfvmqSZLvaBN9lbL99KTp4+JsScrM X-Google-Smtp-Source: AGHT+IH8XhK8C4D8UNL2dRYDg6OeHix/CwPk0Qzd2XGs8ECRNe09UZ5Xr7l6DMxHl8gdoWDktBhaYw== X-Received: by 2002:a05:6402:1742:b0:57c:9ccd:c626 with SMTP id 4fb4d7f45d1cf-594bcab136cmr3329639a12.39.1720615385601; Wed, 10 Jul 2024 05:43:05 -0700 (PDT) Received: from able.fritz.box ([2a00:e180:1504:6f00:b310:1e44:6e5e:7646]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-594bb9604f7sm2171868a12.15.2024.07.10.05.43.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Jul 2024 05:43:05 -0700 (PDT) From: " =?utf-8?q?Christian_K=C3=B6nig?= " X-Google-Original-From: =?utf-8?q?Christian_K=C3=B6nig?= To: thomas.hellstrom@linux.intel.com, matthew.brost@intel.com, dri-devel@lists.freedesktop.org Subject: [PATCH 2/7] drm/exec: don't immediately add the prelocked obj Date: Wed, 10 Jul 2024 14:42:56 +0200 Message-Id: <20240710124301.1628-3-christian.koenig@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240710124301.1628-1-christian.koenig@amd.com> References: <20240710124301.1628-1-christian.koenig@amd.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" Some contended objects might never be locked again in the case of eviction handling for example. Make sure that those doesn't show up in the list of locked objects until they are explicitely mentioned. Signed-off-by: Christian König --- drivers/gpu/drm/drm_exec.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/drivers/gpu/drm/drm_exec.c b/drivers/gpu/drm/drm_exec.c index 2da094bdf8a4..220df336fbd9 100644 --- a/drivers/gpu/drm/drm_exec.c +++ b/drivers/gpu/drm/drm_exec.c @@ -61,8 +61,11 @@ static void drm_exec_unlock_all(struct drm_exec *exec) drm_gem_object_put(obj); } - drm_gem_object_put(exec->prelocked); - exec->prelocked = NULL; + if (exec->prelocked) { + dma_resv_unlock(exec->prelocked->resv); + drm_gem_object_put(exec->prelocked); + exec->prelocked = NULL; + } } /** @@ -179,16 +182,9 @@ static int drm_exec_lock_contended(struct drm_exec *exec) dma_resv_lock_slow(obj->resv, &exec->ticket); } - ret = drm_exec_obj_locked(exec, obj); - if (unlikely(ret)) - goto error_unlock; - exec->prelocked = obj; return 0; -error_unlock: - dma_resv_unlock(obj->resv); - error_dropref: drm_gem_object_put(obj); return ret; @@ -214,6 +210,10 @@ int drm_exec_lock_obj(struct drm_exec *exec, struct drm_gem_object *obj) return ret; if (exec->prelocked == obj) { + ret = drm_exec_obj_locked(exec, obj); + if (unlikely(ret)) + return ret; + drm_gem_object_put(exec->prelocked); exec->prelocked = NULL; return 0; From patchwork Wed Jul 10 12:42:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Christian_K=C3=B6nig?= X-Patchwork-Id: 13729252 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 4AAF1C3DA41 for ; Wed, 10 Jul 2024 12:43:15 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 886BB10E78C; Wed, 10 Jul 2024 12:43:14 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="nPE8q17X"; dkim-atps=neutral Received: from mail-ed1-f41.google.com (mail-ed1-f41.google.com [209.85.208.41]) by gabe.freedesktop.org (Postfix) with ESMTPS id 590EE10E782 for ; Wed, 10 Jul 2024 12:43:08 +0000 (UTC) Received: by mail-ed1-f41.google.com with SMTP id 4fb4d7f45d1cf-57cf8880f95so8233155a12.3 for ; Wed, 10 Jul 2024 05:43:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1720615387; x=1721220187; darn=lists.freedesktop.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=wqDOtib2IWFavjXl6U2wSV2USEnbi4NLN6orparHipU=; b=nPE8q17XQqFqmOPuw/Xq3Zix7h+W2AZwpf1fdyMOyK/LZg0rYwPqEqjH+LYClVvQyW v6I2kLpXcAU+rJd+WjObq/1++M/08SoNzSav7s/KGc17Gr5bvpTV9FEfnnCTXqIGU1qe 1FUp3nJaJ/B6WJlI38HI7yqtoGNxe7AQS0TwLLeNy0Eko3QQYwdPH/6geRjZ4/F0Pkyr gCaJOpjJOwIPXUcR4Vi30xCjGD20nnoNQSTiNw+j3PzDc9NZtw0obrAeboSoOAoe2ZaP x3ERM7qAcnHheOPMboLPr8E3NECAvIY1vpG5t1vGNVxHcPd3raKqbGGSwXWCdw3whcL4 5hzg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720615387; x=1721220187; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=wqDOtib2IWFavjXl6U2wSV2USEnbi4NLN6orparHipU=; b=Omz9p5wlY+55QIjakIvJo/uHHx2OIaZFCLqQVhslgVQiOY+lPxP8x3mv2UKIsxXu6E sokKYip3o3fNoGQNJ8HP3vPhepqomtUUo0/geupfIqm27D14pZInhMb2oDrCZR9owyoE 1CMHdGTvqsOv6NSfRWtwmdoTTkfXB0H5cWkqOhqf6XnkgYvwC26k22/MGHVP3X3oBoZz XxPzPk1qtKzf0JTO0SslvnrigbjJmZvFfiMP5wsCVQtMW9XeB0eTDaerpGQIo80xiqxD MhkQvMDvRk8SodeXWyUYapA/SZn1J8AfXRm3jMEMzYRfu2RtjiZvcrf1O/XmxS7tUOzc gtnA== X-Forwarded-Encrypted: i=1; AJvYcCVdfbcAY9B9gKkrG0WMNkkO01qg8HYp1IFsow1hN6UzzAo5hzI8arnYYV78X5kTEPyMHwQxg+Siifz8dsaNvUcuoUQJohb8B3HZjMjpPlgz X-Gm-Message-State: AOJu0YzzANBe2mSSjhKjGTM8NNxT1MiA7Q/5mLB04atCSCL66zNMAzpr 0MwuwwYY6rHjDx1dWTUQXhF7T3CuGy5u0475GlkIYNJ2nKiozsY3 X-Google-Smtp-Source: AGHT+IGcx69p4kp6QNhNGYGbH73egDV++LmAtMPL1ckgtdLk9nkGBW/Fed8DLcwIk6L3HRFlvOxTrw== X-Received: by 2002:a05:6402:2106:b0:57c:6d1c:3cee with SMTP id 4fb4d7f45d1cf-594baf91661mr4275662a12.14.1720615386322; Wed, 10 Jul 2024 05:43:06 -0700 (PDT) Received: from able.fritz.box ([2a00:e180:1504:6f00:b310:1e44:6e5e:7646]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-594bb9604f7sm2171868a12.15.2024.07.10.05.43.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Jul 2024 05:43:05 -0700 (PDT) From: " =?utf-8?q?Christian_K=C3=B6nig?= " X-Google-Original-From: =?utf-8?q?Christian_K=C3=B6nig?= To: thomas.hellstrom@linux.intel.com, matthew.brost@intel.com, dri-devel@lists.freedesktop.org Subject: [PATCH 3/7] drm/exec: provide trylock interface for eviction Date: Wed, 10 Jul 2024 14:42:57 +0200 Message-Id: <20240710124301.1628-4-christian.koenig@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240710124301.1628-1-christian.koenig@amd.com> References: <20240710124301.1628-1-christian.koenig@amd.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" The TTM eviction path has some additional requirements which make it necessary to trylock an object and then eventually keep or drop the lock again. Signed-off-by: Christian König --- drivers/gpu/drm/drm_exec.c | 77 ++++++++++++++++++++++++++++++++++++++ include/drm/drm_exec.h | 5 +++ 2 files changed, 82 insertions(+) diff --git a/drivers/gpu/drm/drm_exec.c b/drivers/gpu/drm/drm_exec.c index 220df336fbd9..b81bf5a92d97 100644 --- a/drivers/gpu/drm/drm_exec.c +++ b/drivers/gpu/drm/drm_exec.c @@ -336,5 +336,82 @@ int drm_exec_prepare_array(struct drm_exec *exec, } EXPORT_SYMBOL(drm_exec_prepare_array); +/** + * drm_exec_trylock_obj - try to lock a GEM object + * @exec: the drm_exec object with the state + * @obj: the GEM object to trylock + * + * Try to lock a GEM object but don't grab a reference yet. + * + * Since we can't handle contention here it's illegal to trylock the first + * object. + * + * This function is suposed to be used from atomic context and we don't + * know if the GEM object will actually be used or not. So we don't grab a + * reference yet. + * + * Returns: True if the object could be locked, false otherwise. + */ +bool drm_exec_trylock_obj(struct drm_exec *exec, struct drm_gem_object *obj) +{ + if (WARN_ON(!exec->num_objects)) + return false; + + if (exec->prelocked == obj) + return true; + + return dma_resv_trylock_ctx(obj->resv, &exec->ticket); +} +EXPORT_SYMBOL(drm_exec_trylock_obj); + +/** + * drm_exec_keep_trylocked_obj - keep the trylocked obj + * @exec: the drm_exec object with the state + * @obj: the GEM object to trylock + * + * Keep a trylocked object in the drm_exec state object. Grabs a reference to + * the object and adds it to the container of locked objects. + */ +int drm_exec_keep_trylocked_obj(struct drm_exec *exec, + struct drm_gem_object *obj) +{ + int ret; + + ret = drm_exec_obj_locked(exec, obj); + if (ret) { + dma_resv_unlock(obj->resv); + return ret; + } + + if (exec->prelocked == obj) { + drm_gem_object_put(exec->prelocked); + exec->prelocked = NULL; + } + + return ret; +} +EXPORT_SYMBOL(drm_exec_keep_trylocked_obj); + +/** + * drm_exec_drop_trylocked_obj - drop the trylocked obj + * @exec: the drm_exec object with the state + * @obj: the GEM object to trylock + * + * Used to drop a trylocked object in the drm_exec state object, drop the + * reservation lock again and cleanup all references. + */ +void drm_exec_drop_trylocked_obj(struct drm_exec *exec, + struct drm_gem_object *obj) +{ + /* + * We can't drop the reference of prelocked objects since we might still + * be in atomic context. Additionally it makes sense to keep the + * prelocked object around since we might need it again later on. + */ + if (exec->prelocked != obj) + dma_resv_unlock(obj->resv); +} +EXPORT_SYMBOL(drm_exec_drop_trylocked_obj); + MODULE_DESCRIPTION("DRM execution context"); MODULE_LICENSE("Dual MIT/GPL"); diff --git a/include/drm/drm_exec.h b/include/drm/drm_exec.h index aa786b828a0a..a3943057a3e8 100644 --- a/include/drm/drm_exec.h +++ b/include/drm/drm_exec.h @@ -146,5 +146,10 @@ int drm_exec_prepare_array(struct drm_exec *exec, struct drm_gem_object **objects, unsigned int num_objects, unsigned int num_fences); +bool drm_exec_trylock_obj(struct drm_exec *exec, struct drm_gem_object *obj); +int drm_exec_keep_trylocked_obj(struct drm_exec *exec, + struct drm_gem_object *obj); +void drm_exec_drop_trylocked_obj(struct drm_exec *exec, + struct drm_gem_object *obj); #endif From patchwork Wed Jul 10 12:42:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Christian_K=C3=B6nig?= X-Patchwork-Id: 13729250 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 3DFF7C3DA41 for ; Wed, 10 Jul 2024 12:43:12 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 8AC3E10E786; Wed, 10 Jul 2024 12:43:10 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="m/PGfq5q"; dkim-atps=neutral Received: from mail-ed1-f41.google.com (mail-ed1-f41.google.com [209.85.208.41]) by gabe.freedesktop.org (Postfix) with ESMTPS id 3EC4410E782 for ; Wed, 10 Jul 2024 12:43:09 +0000 (UTC) Received: by mail-ed1-f41.google.com with SMTP id 4fb4d7f45d1cf-58b447c519eso7940701a12.3 for ; Wed, 10 Jul 2024 05:43:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1720615388; x=1721220188; darn=lists.freedesktop.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=1DzdNUe/Ry+da5QYCBOVa0efIY5B5o+i0CSMZqA5OQg=; b=m/PGfq5qxwvhjEJIdhbcD/ShNnC9tQH/+FosrRCO7YWsOeHeHaIgcw8J5MylaqxE0m DwG8MVLEUJhIXCF75SUTEthkbKotDwOChH/FXIn82HwLi6QZM8yeQUXs5/gMeCKzz7uR Yez/2z46fc2FF2akXIUyd4SZJW/cnx8W24+K8AZeDiTqgzQLe8bHd0LePJuvo2eSu/YU dFiFZCOSljco0TJOfMMIbVRi9fNRoHKzyFpqmSFmE1cQQs68GNlq03jy7vKqxcIVhNk3 Y/xvZKj5hyKnLz9KxK13uAVZB+nxj8Xl/cHcQKqMhYjDfmuVIvJt6HKNsBSG/4WOoZF+ RWsw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720615388; x=1721220188; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=1DzdNUe/Ry+da5QYCBOVa0efIY5B5o+i0CSMZqA5OQg=; b=bnvB0o+a9aeDQKuyuT0EuR0Wyhy4jkD19Gy7IHq3V6L7HSAjVkoCrJUY84Y1B27qKq gR32OLOisT8CdsunAlq1mdKSEtzWW2RPZPsPngXRPyUGJgoTEmK+v3kqq1e9NnPMfaWw VvJPYqloAQ394092mAvXmu9yt5GMVvNok2nRgJUnkzBicyiw/znEoCiQW5Q19AbXBEq0 PTekZckhM5O5P3ArkWxNIhzl7wSpY6OJQZ6m/wV+9m4Tjy2q9TJ52k0wRv4+yc13FkzB pu/XEaHf3snUUlNsv8tvVJSpyEVZZSLKiabukgTT3ZpFCCSzHAIKWcPGV9IwU8i2TqzV jbhw== X-Forwarded-Encrypted: i=1; AJvYcCX9WxgTsDXYuoWHtcSg9WCG6KYaHz9+SIKG8kW0uyNSjTqLYrA++gORJSbA+11qLkD6qp6sfroSjSaPJVt2N8rnLOvzhb4owTlhN0dvWfJW X-Gm-Message-State: AOJu0YyShxKcLgWh7xZ1yrGWQUSMdPFF8403ndV+0BnVj9ThOEkD/hr4 YFUIjGXFKK25UuP+Gqorg+XzFcywboqutpScphfmjkumfItIBmp2xXD0+ykIIHE= X-Google-Smtp-Source: AGHT+IEO/7Rs+ElVcB2Dn2I6s+rMvLeAoJT1biIVqHe5lojvLlj2OKUE9Xh7iebQn1CiZ76NdMolng== X-Received: by 2002:a05:6402:3551:b0:586:57c:2373 with SMTP id 4fb4d7f45d1cf-594b6eda294mr4697588a12.0.1720615387101; Wed, 10 Jul 2024 05:43:07 -0700 (PDT) Received: from able.fritz.box ([2a00:e180:1504:6f00:b310:1e44:6e5e:7646]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-594bb9604f7sm2171868a12.15.2024.07.10.05.43.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Jul 2024 05:43:06 -0700 (PDT) From: " =?utf-8?q?Christian_K=C3=B6nig?= " X-Google-Original-From: =?utf-8?q?Christian_K=C3=B6nig?= To: thomas.hellstrom@linux.intel.com, matthew.brost@intel.com, dri-devel@lists.freedesktop.org Subject: [PATCH 4/7] drm/ttm: move LRU walk defines into new internal header Date: Wed, 10 Jul 2024 14:42:58 +0200 Message-Id: <20240710124301.1628-5-christian.koenig@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240710124301.1628-1-christian.koenig@amd.com> References: <20240710124301.1628-1-christian.koenig@amd.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" That is something drivers really shouldn't mess with. Signed-off-by: Christian König --- drivers/gpu/drm/ttm/ttm_bo.c | 1 + drivers/gpu/drm/ttm/ttm_bo_util.c | 2 + drivers/gpu/drm/ttm/ttm_bo_util.h | 67 +++++++++++++++++++++++++++++++ include/drm/ttm/ttm_bo.h | 35 ---------------- 4 files changed, 70 insertions(+), 35 deletions(-) create mode 100644 drivers/gpu/drm/ttm/ttm_bo_util.h diff --git a/drivers/gpu/drm/ttm/ttm_bo.c b/drivers/gpu/drm/ttm/ttm_bo.c index 0131ec802066..41bee8696e69 100644 --- a/drivers/gpu/drm/ttm/ttm_bo.c +++ b/drivers/gpu/drm/ttm/ttm_bo.c @@ -45,6 +45,7 @@ #include #include "ttm_module.h" +#include "ttm_bo_util.h" static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo, struct ttm_placement *placement) diff --git a/drivers/gpu/drm/ttm/ttm_bo_util.c b/drivers/gpu/drm/ttm/ttm_bo_util.c index 3c07f4712d5c..03e28e3d0d03 100644 --- a/drivers/gpu/drm/ttm/ttm_bo_util.c +++ b/drivers/gpu/drm/ttm/ttm_bo_util.c @@ -37,6 +37,8 @@ #include +#include "ttm_bo_util.h" + struct ttm_transfer_obj { struct ttm_buffer_object base; struct ttm_buffer_object *bo; diff --git a/drivers/gpu/drm/ttm/ttm_bo_util.h b/drivers/gpu/drm/ttm/ttm_bo_util.h new file mode 100644 index 000000000000..c19b50809208 --- /dev/null +++ b/drivers/gpu/drm/ttm/ttm_bo_util.h @@ -0,0 +1,67 @@ +/* SPDX-License-Identifier: GPL-2.0 OR MIT */ +/************************************************************************** + * Copyright 2024 Advanced Micro Devices, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR + * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + **************************************************************************/ +#ifndef _TTM_BO_UTIL_H_ +#define _TTM_BO_UTIL_H_ + +struct ww_acquire_ctx; + +struct ttm_buffer_object; +struct ttm_operation_ctx; +struct ttm_lru_walk; + +/** struct ttm_lru_walk_ops - Operations for a LRU walk. */ +struct ttm_lru_walk_ops { + /** + * process_bo - Process this bo. + * @walk: struct ttm_lru_walk describing the walk. + * @bo: A locked and referenced buffer object. + * + * Return: Negative error code on error, User-defined positive value + * (typically, but not always, size of the processed bo) on success. + * On success, the returned values are summed by the walk and the + * walk exits when its target is met. + * 0 also indicates success, -EBUSY means this bo was skipped. + */ + s64 (*process_bo)(struct ttm_lru_walk *walk, + struct ttm_buffer_object *bo); +}; + +/** + * struct ttm_lru_walk - Structure describing a LRU walk. + */ +struct ttm_lru_walk { + /** @ops: Pointer to the ops structure. */ + const struct ttm_lru_walk_ops *ops; + /** @ctx: Pointer to the struct ttm_operation_ctx. */ + struct ttm_operation_ctx *ctx; + /** @ticket: The struct ww_acquire_ctx if any. */ + struct ww_acquire_ctx *ticket; + /** @tryock_only: Only use trylock for locking. */ + bool trylock_only; +}; + +s64 ttm_lru_walk_for_evict(struct ttm_lru_walk *walk, struct ttm_device *bdev, + struct ttm_resource_manager *man, s64 target); + +#endif diff --git a/include/drm/ttm/ttm_bo.h b/include/drm/ttm/ttm_bo.h index d1a732d56259..5f7c967222a2 100644 --- a/include/drm/ttm/ttm_bo.h +++ b/include/drm/ttm/ttm_bo.h @@ -194,41 +194,6 @@ struct ttm_operation_ctx { uint64_t bytes_moved; }; -struct ttm_lru_walk; - -/** struct ttm_lru_walk_ops - Operations for a LRU walk. */ -struct ttm_lru_walk_ops { - /** - * process_bo - Process this bo. - * @walk: struct ttm_lru_walk describing the walk. - * @bo: A locked and referenced buffer object. - * - * Return: Negative error code on error, User-defined positive value - * (typically, but not always, size of the processed bo) on success. - * On success, the returned values are summed by the walk and the - * walk exits when its target is met. - * 0 also indicates success, -EBUSY means this bo was skipped. - */ - s64 (*process_bo)(struct ttm_lru_walk *walk, struct ttm_buffer_object *bo); -}; - -/** - * struct ttm_lru_walk - Structure describing a LRU walk. - */ -struct ttm_lru_walk { - /** @ops: Pointer to the ops structure. */ - const struct ttm_lru_walk_ops *ops; - /** @ctx: Pointer to the struct ttm_operation_ctx. */ - struct ttm_operation_ctx *ctx; - /** @ticket: The struct ww_acquire_ctx if any. */ - struct ww_acquire_ctx *ticket; - /** @tryock_only: Only use trylock for locking. */ - bool trylock_only; -}; - -s64 ttm_lru_walk_for_evict(struct ttm_lru_walk *walk, struct ttm_device *bdev, - struct ttm_resource_manager *man, s64 target); - /** * ttm_bo_get - reference a struct ttm_buffer_object * From patchwork Wed Jul 10 12:42:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Christian_K=C3=B6nig?= X-Patchwork-Id: 13729251 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 4DE3FC3DA42 for ; Wed, 10 Jul 2024 12:43:13 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 0327E10E787; Wed, 10 Jul 2024 12:43:11 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="OsyrS5w3"; dkim-atps=neutral Received: from mail-ed1-f41.google.com (mail-ed1-f41.google.com [209.85.208.41]) by gabe.freedesktop.org (Postfix) with ESMTPS id D38EF10E782 for ; Wed, 10 Jul 2024 12:43:09 +0000 (UTC) Received: by mail-ed1-f41.google.com with SMTP id 4fb4d7f45d1cf-58b447c513aso7635118a12.2 for ; Wed, 10 Jul 2024 05:43:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1720615388; x=1721220188; darn=lists.freedesktop.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=40acJs2fLXR0Yl1TXrRI/vXyr4P4RhGTEqeA9wFSz/w=; b=OsyrS5w3Xn6Ho2BJ62NE7Qbj61QoMl/7RpP9wYNPEqFhrltKJtKh8VVjszpf4/cg+Z cbTZhHHYFpUl+6HJHHDGOsh84+ohhbAH6CC2YNjsBJULGd2YT2rUMejFgBFutbeTSTst CRp0wLSTOIPkkHJclivxeYwp/YiP/+xBsw/XfHy68Z0VdjUwWgrvu/cv0oajVD/5h3U2 aGplz74zZV4kLeCAQpdWayTfB1pE1n8UsaUD64v6+ye9eMa2hHX6LxLJaCvulUdxzPIg spaNilgWtMDpPwtgXOTahWLEUfy74wJ+8Z4Bmg1jMHlTIxJdFXe+HbreNDdm+h9Ox6Cb KZcg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720615388; x=1721220188; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=40acJs2fLXR0Yl1TXrRI/vXyr4P4RhGTEqeA9wFSz/w=; b=A7bcF/MaZ06ZO4LLwGZVE2tpVOtRa12rXMeE3nN4CAI3r3pmLhw6hJJU4/EpbxuC9K gZ6eYjUMS7nPiLqrh61vg80FGuP8XDVtByZJHuZ/9rda1dBquAPFFaZR3822jtOzx5fO b5zVy11jPaMrO/wIog95Lw8YQ5lQvQg7I6pMluRwm3vflRUTlFd0ft2GtyOTIaZmXqLa 1zy4P9pAF3VxHP8LBZjqRdco96/RBF9y1uthtCSYeVRR7SVgJTO7LrmUi3yIHTBA+X6P 4nqKbl9eODRQPAV6dB350qLjjD+iFuTAQ4VOR7ze8haTKDCPzQApZbv1Oc7/0e0q2Hqx 3jpg== X-Forwarded-Encrypted: i=1; AJvYcCUhuf/1qoS4M13LGQrY7u3W+d/XbInWByiwZzNhl4rXjUWtwHqLOYFPosB4/+q1ii9eAbSRcRlDo8HpPAgUrdOzq2sLXXoVTxUEDFkeuwkC X-Gm-Message-State: AOJu0YwizhzLGK3JOBZ/RDrM7B1uJVAAreYXMsI3whOBaYvR5SzpiiBf KAsD7AAClzgB2Wxr/dfv4M2opoKRaPd0vSlrgp80B/6gf+mL/VIzfpOdznhx0eg= X-Google-Smtp-Source: AGHT+IEtJgrCgEuJUf7vfL5SnFbsP/SYj65cd+WrgGDL1gePi6WLKUNEyXtCa2JOhVtRDeH0VZzivQ== X-Received: by 2002:a05:6402:120f:b0:58b:9077:2bd4 with SMTP id 4fb4d7f45d1cf-594bbe2ba76mr2558982a12.41.1720615387901; Wed, 10 Jul 2024 05:43:07 -0700 (PDT) Received: from able.fritz.box ([2a00:e180:1504:6f00:b310:1e44:6e5e:7646]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-594bb9604f7sm2171868a12.15.2024.07.10.05.43.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Jul 2024 05:43:07 -0700 (PDT) From: " =?utf-8?q?Christian_K=C3=B6nig?= " X-Google-Original-From: =?utf-8?q?Christian_K=C3=B6nig?= To: thomas.hellstrom@linux.intel.com, matthew.brost@intel.com, dri-devel@lists.freedesktop.org Subject: [PATCH 5/7] drm/ttm: move needs_unlock into the walk Date: Wed, 10 Jul 2024 14:42:59 +0200 Message-Id: <20240710124301.1628-6-christian.koenig@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240710124301.1628-1-christian.koenig@amd.com> References: <20240710124301.1628-1-christian.koenig@amd.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" Not a walk parameter but important to have that status around. Signed-off-by: Christian König --- drivers/gpu/drm/ttm/ttm_bo_util.c | 26 ++++++++++++-------------- drivers/gpu/drm/ttm/ttm_bo_util.h | 2 ++ 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/drivers/gpu/drm/ttm/ttm_bo_util.c b/drivers/gpu/drm/ttm/ttm_bo_util.c index 03e28e3d0d03..7a4bc7e9950b 100644 --- a/drivers/gpu/drm/ttm/ttm_bo_util.c +++ b/drivers/gpu/drm/ttm/ttm_bo_util.c @@ -772,15 +772,14 @@ int ttm_bo_pipeline_gutting(struct ttm_buffer_object *bo) } static bool ttm_lru_walk_trylock(struct ttm_lru_walk *walk, - struct ttm_buffer_object *bo, - bool *needs_unlock) + struct ttm_buffer_object *bo) { struct ttm_operation_ctx *ctx = walk->ctx; - *needs_unlock = false; + walk->needs_unlock = false; if (dma_resv_trylock(bo->base.resv)) { - *needs_unlock = true; + walk->needs_unlock = true; return true; } @@ -793,8 +792,7 @@ static bool ttm_lru_walk_trylock(struct ttm_lru_walk *walk, } static int ttm_lru_walk_ticketlock(struct ttm_lru_walk *walk, - struct ttm_buffer_object *bo, - bool *needs_unlock) + struct ttm_buffer_object *bo) { struct dma_resv *resv = bo->base.resv; int ret; @@ -805,7 +803,7 @@ static int ttm_lru_walk_ticketlock(struct ttm_lru_walk *walk, ret = dma_resv_lock(resv, walk->ticket); if (!ret) { - *needs_unlock = true; + walk->needs_unlock = true; /* * Only a single ticketlock per loop. Ticketlocks are prone * to return -EDEADLK causing the eviction to fail, so @@ -821,9 +819,10 @@ static int ttm_lru_walk_ticketlock(struct ttm_lru_walk *walk, return ret; } -static void ttm_lru_walk_unlock(struct ttm_buffer_object *bo, bool locked) +static void ttm_lru_walk_unlock(struct ttm_lru_walk *walk, + struct ttm_buffer_object *bo) { - if (locked) + if (walk->needs_unlock) dma_resv_unlock(bo->base.resv); } @@ -869,7 +868,6 @@ s64 ttm_lru_walk_for_evict(struct ttm_lru_walk *walk, struct ttm_device *bdev, spin_lock(&bdev->lru_lock); ttm_resource_manager_for_each_res(man, &cursor, res) { struct ttm_buffer_object *bo = res->bo; - bool bo_needs_unlock = false; bool bo_locked = false; int mem_type; @@ -878,14 +876,14 @@ s64 ttm_lru_walk_for_evict(struct ttm_lru_walk *walk, struct ttm_device *bdev, * since if we do it the other way around, and the trylock fails, * we need to drop the lru lock to put the bo. */ - if (ttm_lru_walk_trylock(walk, bo, &bo_needs_unlock)) + if (ttm_lru_walk_trylock(walk, bo)) bo_locked = true; else if (!walk->ticket || walk->ctx->no_wait_gpu || walk->trylock_only) continue; if (!ttm_bo_get_unless_zero(bo)) { - ttm_lru_walk_unlock(bo, bo_needs_unlock); + ttm_lru_walk_unlock(walk, bo); continue; } @@ -894,7 +892,7 @@ s64 ttm_lru_walk_for_evict(struct ttm_lru_walk *walk, struct ttm_device *bdev, lret = 0; if (!bo_locked) - lret = ttm_lru_walk_ticketlock(walk, bo, &bo_needs_unlock); + lret = ttm_lru_walk_ticketlock(walk, bo); /* * Note that in between the release of the lru lock and the @@ -906,7 +904,7 @@ s64 ttm_lru_walk_for_evict(struct ttm_lru_walk *walk, struct ttm_device *bdev, if (!lret && bo->resource && bo->resource->mem_type == mem_type) lret = walk->ops->process_bo(walk, bo); - ttm_lru_walk_unlock(bo, bo_needs_unlock); + ttm_lru_walk_unlock(walk, bo); ttm_bo_put(bo); if (lret == -EBUSY || lret == -EALREADY) lret = 0; diff --git a/drivers/gpu/drm/ttm/ttm_bo_util.h b/drivers/gpu/drm/ttm/ttm_bo_util.h index c19b50809208..c653e16ccb76 100644 --- a/drivers/gpu/drm/ttm/ttm_bo_util.h +++ b/drivers/gpu/drm/ttm/ttm_bo_util.h @@ -59,6 +59,8 @@ struct ttm_lru_walk { struct ww_acquire_ctx *ticket; /** @tryock_only: Only use trylock for locking. */ bool trylock_only; + /** @needs_unlock: If the current BO needs unlocking */ + bool needs_unlock; }; s64 ttm_lru_walk_for_evict(struct ttm_lru_walk *walk, struct ttm_device *bdev, From patchwork Wed Jul 10 12:43:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Christian_K=C3=B6nig?= X-Patchwork-Id: 13729253 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 64354C3DA4A for ; Wed, 10 Jul 2024 12:43:15 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 8F8CE10E78D; Wed, 10 Jul 2024 12:43:14 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="jkNgdLNJ"; dkim-atps=neutral Received: from mail-ed1-f43.google.com (mail-ed1-f43.google.com [209.85.208.43]) by gabe.freedesktop.org (Postfix) with ESMTPS id A56F610E787 for ; Wed, 10 Jul 2024 12:43:10 +0000 (UTC) Received: by mail-ed1-f43.google.com with SMTP id 4fb4d7f45d1cf-58be2b8b6b2so7950749a12.3 for ; Wed, 10 Jul 2024 05:43:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1720615389; x=1721220189; darn=lists.freedesktop.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=3IudXMkmOGnBAKAh9aEHKOVaR8ZFNG/6gQf1mC18+Yo=; b=jkNgdLNJ5a0JGrFpbWrBiRIeZzbxcA6P7rcj+2ciRBJ0DJgBgibCBC4gFy0TUvRLU3 tHXj7vB54o/FFbqbWYqJwwH+4n1DC6beqhnaEV5TwNTycMkINdspB883sgAJYBaUQudZ loazwlyMoDN1U4+qnndt/70tmWK1DCMO4PD028dwvALiNkkQAeYicPCyYAqzYhJyiXuA JOhYGRu+tnt5q0bUdXpUt8BHgtDPfxfzhfzRva0CytEmnspdDxABaiUCw18cEM4nVeQ7 jHvzjpk+VMKDQV2p0Efp8os1tGfaA8IvBp/t4FN1YiuMQ21ET/mNOFCLza8GfHcP+ljK V6UA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720615389; x=1721220189; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=3IudXMkmOGnBAKAh9aEHKOVaR8ZFNG/6gQf1mC18+Yo=; b=cCTEj6I0wyd+DfpXlg8OezWVLgsjq6yxF1JMaEFvnwcI2LRPgwfd6WzmTE7qwkfSB6 Q99D7TcQcI6kbkhG13e6DRwA1aOVGU9l3Ak9sv5M+Fz1q3+at+JESca/79B7kyUAR6NJ gfzlGY9lW7lvcT2hKhqT84LngLNvd/wIIfT+Mbn/Dd/7M4S9dZpZg7NJpvnPWYhB9K86 q1qMCKySv7ywUG3dW0DUSGFyskb5uFK8jA6m6q1uGaI1O8h97SQcuPU4xp3wm88U2+UG 7230jx7i9W3zmHV/fqiPp2UISXHf7iwcKLucsvOEEpFXp3dL4zP6MZuJph69klmlrMKg Kxng== X-Forwarded-Encrypted: i=1; AJvYcCV56WGR+09djsLrJiVhX1JC5dL5FiaRtFsRkBB+Vusn9rvhz9aXwmpp6oapG1AeafMKITGoem7tU+fuGLjQ1sGWOzeTggd3iuaRrK13tkMR X-Gm-Message-State: AOJu0YybjLY4h0Iwj8wJOFSdVQtVT4UqsmGq1u2AhZvx+CUPZxMDFPe2 0wumggrkGUNk22FVHOKQvpXc7oPLkoliSVVfXsgozHBN4eH9of8k6fM4CoylfX4= X-Google-Smtp-Source: AGHT+IE3UVenv0a09ve8iTEwAtYZ0/8SIcRXOoG1MT2xdezugAc9QE8reTPFy0jTVJNuQTLOfUsi/Q== X-Received: by 2002:a05:6402:5cd:b0:57c:fd20:352c with SMTP id 4fb4d7f45d1cf-594bc7c8337mr4106938a12.34.1720615388666; Wed, 10 Jul 2024 05:43:08 -0700 (PDT) Received: from able.fritz.box ([2a00:e180:1504:6f00:b310:1e44:6e5e:7646]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-594bb9604f7sm2171868a12.15.2024.07.10.05.43.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Jul 2024 05:43:08 -0700 (PDT) From: " =?utf-8?q?Christian_K=C3=B6nig?= " X-Google-Original-From: =?utf-8?q?Christian_K=C3=B6nig?= To: thomas.hellstrom@linux.intel.com, matthew.brost@intel.com, dri-devel@lists.freedesktop.org Subject: [PATCH 6/7] drm/ttm: support using drm_exec during eviction v2 Date: Wed, 10 Jul 2024 14:43:00 +0200 Message-Id: <20240710124301.1628-7-christian.koenig@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240710124301.1628-1-christian.koenig@amd.com> References: <20240710124301.1628-1-christian.koenig@amd.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" Allow specifying a drm_exec object in TTMs operation context which is used to lock objects during eviction. This allows to handle deadlocks much more gracefully and with that avoid returning -ENOMEM on heavily contended domains. v2: rebased on top of Thomas work TODO: This still doesn't handle BOs which are about to be torn down correctly. Signed-off-by: Christian König --- drivers/gpu/drm/ttm/ttm_bo_util.c | 45 +++++++++++++++++++++++++------ drivers/gpu/drm/ttm/ttm_bo_util.h | 2 ++ include/drm/ttm/ttm_bo.h | 3 +++ 3 files changed, 42 insertions(+), 8 deletions(-) diff --git a/drivers/gpu/drm/ttm/ttm_bo_util.c b/drivers/gpu/drm/ttm/ttm_bo_util.c index 7a4bc7e9950b..850e329ab5a5 100644 --- a/drivers/gpu/drm/ttm/ttm_bo_util.c +++ b/drivers/gpu/drm/ttm/ttm_bo_util.c @@ -36,6 +36,7 @@ #include #include +#include #include "ttm_bo_util.h" @@ -776,15 +777,22 @@ static bool ttm_lru_walk_trylock(struct ttm_lru_walk *walk, { struct ttm_operation_ctx *ctx = walk->ctx; + walk->needs_drop = false; walk->needs_unlock = false; - if (dma_resv_trylock(bo->base.resv)) { - walk->needs_unlock = true; + if (bo->base.resv == ctx->resv && ctx->allow_res_evict) { + dma_resv_assert_held(bo->base.resv); return true; } - if (bo->base.resv == ctx->resv && ctx->allow_res_evict) { - dma_resv_assert_held(bo->base.resv); + if (walk->ctx->exec) { + if (drm_exec_trylock_obj(walk->ctx->exec, &bo->base)) { + walk->needs_drop = true; + return true; + } + + } else if (dma_resv_trylock(bo->base.resv)) { + walk->needs_unlock = true; return true; } @@ -797,7 +805,9 @@ static int ttm_lru_walk_ticketlock(struct ttm_lru_walk *walk, struct dma_resv *resv = bo->base.resv; int ret; - if (walk->ctx->interruptible) + if (walk->ctx->exec) + ret = drm_exec_lock_obj(walk->ctx->exec, &bo->base); + else if (walk->ctx->interruptible) ret = dma_resv_lock_interruptible(resv, walk->ticket); else ret = dma_resv_lock(resv, walk->ticket); @@ -811,7 +821,8 @@ static int ttm_lru_walk_ticketlock(struct ttm_lru_walk *walk, * trylocking for this walk. */ walk->ticket = NULL; - } else if (ret == -EDEADLK) { + + } else if (!walk->ctx->exec && ret == -EDEADLK) { /* Caller needs to exit the ww transaction. */ ret = -ENOSPC; } @@ -822,7 +833,15 @@ static int ttm_lru_walk_ticketlock(struct ttm_lru_walk *walk, static void ttm_lru_walk_unlock(struct ttm_lru_walk *walk, struct ttm_buffer_object *bo) { - if (walk->needs_unlock) + if (walk->needs_drop) + drm_exec_drop_trylocked_obj(walk->ctx->exec, &bo->base); + + if (!walk->needs_unlock) + return; + + if (walk->ctx->exec) + drm_exec_unlock_obj(walk->ctx->exec, &bo->base); + else dma_resv_unlock(bo->base.resv); } @@ -891,8 +910,18 @@ s64 ttm_lru_walk_for_evict(struct ttm_lru_walk *walk, struct ttm_device *bdev, spin_unlock(&bdev->lru_lock); lret = 0; - if (!bo_locked) + if (!bo_locked) { lret = ttm_lru_walk_ticketlock(walk, bo); + } else if (walk->ctx->exec && !bo->deleted) { + lret = drm_exec_keep_trylocked_obj(walk->ctx->exec, + &bo->base); + if (!lret) { + walk->needs_drop = false; + walk->needs_unlock = true; + } + } else { + lret = 0; + } /* * Note that in between the release of the lru lock and the diff --git a/drivers/gpu/drm/ttm/ttm_bo_util.h b/drivers/gpu/drm/ttm/ttm_bo_util.h index c653e16ccb76..5e1bb156837f 100644 --- a/drivers/gpu/drm/ttm/ttm_bo_util.h +++ b/drivers/gpu/drm/ttm/ttm_bo_util.h @@ -59,6 +59,8 @@ struct ttm_lru_walk { struct ww_acquire_ctx *ticket; /** @tryock_only: Only use trylock for locking. */ bool trylock_only; + /** @needs_drop: If the current BO needs a drm_exec trylock drop */ + bool needs_drop; /** @needs_unlock: If the current BO needs unlocking */ bool needs_unlock; }; diff --git a/include/drm/ttm/ttm_bo.h b/include/drm/ttm/ttm_bo.h index 5f7c967222a2..5bee917e01e2 100644 --- a/include/drm/ttm/ttm_bo.h +++ b/include/drm/ttm/ttm_bo.h @@ -180,6 +180,8 @@ struct ttm_bo_kmap_obj { * faults. Should only be used by TTM internally. * @resv: Reservation object to allow reserved evictions with. * @bytes_moved: Statistics on how many bytes have been moved. + * @exec: optional drm_exec object to use for locking BOs and tracking which are + * locked. * * Context for TTM operations like changing buffer placement or general memory * allocation. @@ -192,6 +194,7 @@ struct ttm_operation_ctx { bool force_alloc; struct dma_resv *resv; uint64_t bytes_moved; + struct drm_exec *exec; }; /** From patchwork Wed Jul 10 12:43:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Christian_K=C3=B6nig?= X-Patchwork-Id: 13729254 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 4F21EC3DA47 for ; Wed, 10 Jul 2024 12:43:16 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 97E9110E78F; Wed, 10 Jul 2024 12:43:14 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="bAsubeF+"; dkim-atps=neutral Received: from mail-lj1-f172.google.com (mail-lj1-f172.google.com [209.85.208.172]) by gabe.freedesktop.org (Postfix) with ESMTPS id 1D17E10E78C for ; Wed, 10 Jul 2024 12:43:12 +0000 (UTC) Received: by mail-lj1-f172.google.com with SMTP id 38308e7fff4ca-2ee88c4443eso66423491fa.3 for ; Wed, 10 Jul 2024 05:43:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1720615390; x=1721220190; darn=lists.freedesktop.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=umrby2MQnpwBnZqHdFG3/JwLGYUDmvuInXq96jEbSz4=; b=bAsubeF+jHY1nJSoFD68Yb5SnObzLNfTGSTpTDlPOY/piK7K0yPYnfnuK6Q9WQKIId z3q0slyOVBkS8GiDQeKuRHfWbiY7w002A30zFcqWZKwxmbbGp4mMq4oMMsK3Ef7roFG6 qUK5Fprect+TlE9pJj4qZ0sCi2xtIACcEEWRcpr32FLmK/HdWaUnx2jwuM5v+1hUq2In f+ThxpK/SxxlizTo/wi10a85CfEFJXVSWZtKaQnJH7B36SMMs6APZ1uzigeb0KWUhkJY WFmseSSziWUSOS1LpgyBN+Ko/9KR7OQhmJk+DWNePcM+HDxaawoKMhp2V14gWZPuQsGu p+FA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720615390; x=1721220190; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=umrby2MQnpwBnZqHdFG3/JwLGYUDmvuInXq96jEbSz4=; b=w/3c3OeL6yjq4Rbg+QeVmvAmKKTiQgVI21Bqw+0ux1l3O7dkjHEz28VrnU0RxHRVu3 hqInXPO2wXNMPkOmdnTQaiq0ffM/NYtnSS3B+fa+s5bBe+2k+nQlSPsrgaIXYdmy7WMw UgV78GgK2SuoZbInTpAimLpqKCj8Er9a/cXpp2LDmxCWWpll9/gBr1wEjmbdRKdEkGcs j5zYpXHWCNlaRDoc9OwdH87IGdnvQ+PxGjZ5EsCE6IGgX07RcNWj0mOD0Xq1y95J/itX 00f7db3VqqXgk6jT9LzGEwPgs8Sx98pkfUzQTFQ+cShURNC6CAqHAPkbFFCWLF9jWyLZ vbRg== X-Forwarded-Encrypted: i=1; AJvYcCWl/aWG+Owt2yv+XrLGWY+57G+fdhVWPnVnewlbhDFXMGZOyW9MouDIehdPf3rr2whxjwJlpgT/GVnfJF8rIjNhM43Rs8XKfdZAJ4CRNUP2 X-Gm-Message-State: AOJu0YyrS79ti0MLjBp2dbx/BDV9iVX+NcgejzN74NtsiHJED/01VuGY RTuZ152iMNC0ov/yKdSXNycAPJ4ZRzmU38lj5p55CUvvp1ypbewV X-Google-Smtp-Source: AGHT+IHShUw8PJmudP1U5KmobFkNtnD0gXpYoSxeWfQTX4ax8yvc34JKVIuBOJzXpqDk/10/YBTnPw== X-Received: by 2002:a05:651c:11c7:b0:2ee:4623:93e with SMTP id 38308e7fff4ca-2eeb30e398cmr36527401fa.20.1720615389492; Wed, 10 Jul 2024 05:43:09 -0700 (PDT) Received: from able.fritz.box ([2a00:e180:1504:6f00:b310:1e44:6e5e:7646]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-594bb9604f7sm2171868a12.15.2024.07.10.05.43.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Jul 2024 05:43:09 -0700 (PDT) From: " =?utf-8?q?Christian_K=C3=B6nig?= " X-Google-Original-From: =?utf-8?q?Christian_K=C3=B6nig?= To: thomas.hellstrom@linux.intel.com, matthew.brost@intel.com, dri-devel@lists.freedesktop.org Subject: [PATCH 7/7] drm/amdgpu: use drm_exec during BO validation Date: Wed, 10 Jul 2024 14:43:01 +0200 Message-Id: <20240710124301.1628-8-christian.koenig@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240710124301.1628-1-christian.koenig@amd.com> References: <20240710124301.1628-1-christian.koenig@amd.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" This allows to detect deadlocks happening because of resource constraints. Especially submissions which want to use all of GDS doesn't result in sporadic -ENOMEM any more. Signed-off-by: Christian König --- drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c | 86 ++++++++++++++------------ 1 file changed, 46 insertions(+), 40 deletions(-) diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c index ec888fc6ead8..ff532c8b7a62 100644 --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c @@ -782,7 +782,7 @@ static int amdgpu_cs_bo_validate(void *param, struct amdgpu_bo *bo) struct ttm_operation_ctx ctx = { .interruptible = true, .no_wait_gpu = false, - .resv = bo->tbo.base.resv + .exec = &p->exec, }; uint32_t domain; int r; @@ -834,7 +834,10 @@ static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p, union drm_amdgpu_cs *cs) { struct amdgpu_fpriv *fpriv = p->filp->driver_priv; - struct ttm_operation_ctx ctx = { true, false }; + struct ttm_operation_ctx ctx = { + .interruptible =true, + .exec = &p->exec + }; struct amdgpu_vm *vm = &fpriv->vm; struct amdgpu_bo_list_entry *e; struct drm_gem_object *obj; @@ -919,50 +922,56 @@ static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p, if (unlikely(r)) goto out_free_user_pages; } - } - - amdgpu_bo_list_for_each_userptr_entry(e, p->bo_list) { - struct mm_struct *usermm; - usermm = amdgpu_ttm_tt_get_usermm(e->bo->tbo.ttm); - if (usermm && usermm != current->mm) { - r = -EPERM; - goto out_free_user_pages; - } + amdgpu_bo_list_for_each_userptr_entry(e, p->bo_list) { + struct mm_struct *usermm; - if (amdgpu_ttm_tt_is_userptr(e->bo->tbo.ttm) && - e->user_invalidated && e->user_pages) { - amdgpu_bo_placement_from_domain(e->bo, - AMDGPU_GEM_DOMAIN_CPU); - r = ttm_bo_validate(&e->bo->tbo, &e->bo->placement, - &ctx); - if (r) + usermm = amdgpu_ttm_tt_get_usermm(e->bo->tbo.ttm); + if (usermm && usermm != current->mm) { + r = -EPERM; goto out_free_user_pages; + } + + if (amdgpu_ttm_tt_is_userptr(e->bo->tbo.ttm) && + e->user_invalidated && e->user_pages) { + amdgpu_bo_placement_from_domain(e->bo, + AMDGPU_GEM_DOMAIN_CPU); + r = ttm_bo_validate(&e->bo->tbo, &e->bo->placement, + &ctx); + drm_exec_retry_on_contention(&p->exec); + if (r) + goto out_free_user_pages; + + amdgpu_ttm_tt_set_user_pages(e->bo->tbo.ttm, + e->user_pages); + } - amdgpu_ttm_tt_set_user_pages(e->bo->tbo.ttm, - e->user_pages); + kvfree(e->user_pages); + e->user_pages = NULL; } - kvfree(e->user_pages); - e->user_pages = NULL; - } + amdgpu_cs_get_threshold_for_moves(p->adev, &p->bytes_moved_threshold, + &p->bytes_moved_vis_threshold); + p->bytes_moved = 0; + p->bytes_moved_vis = 0; - amdgpu_cs_get_threshold_for_moves(p->adev, &p->bytes_moved_threshold, - &p->bytes_moved_vis_threshold); - p->bytes_moved = 0; - p->bytes_moved_vis = 0; + r = amdgpu_vm_validate(p->adev, &fpriv->vm, NULL, + amdgpu_cs_bo_validate, p); + drm_exec_retry_on_contention(&p->exec); + if (r) { + DRM_ERROR("amdgpu_vm_validate() failed.\n"); + goto out_free_user_pages; + } - r = amdgpu_vm_validate(p->adev, &fpriv->vm, NULL, - amdgpu_cs_bo_validate, p); - if (r) { - DRM_ERROR("amdgpu_vm_validate() failed.\n"); - goto out_free_user_pages; - } + drm_exec_for_each_locked_object(&p->exec, index, obj) { + r = amdgpu_cs_bo_validate(p, gem_to_amdgpu_bo(obj)); + drm_exec_retry_on_contention(&p->exec); + if (unlikely(r)) + goto out_free_user_pages; + } - drm_exec_for_each_locked_object(&p->exec, index, obj) { - r = amdgpu_cs_bo_validate(p, gem_to_amdgpu_bo(obj)); - if (unlikely(r)) - goto out_free_user_pages; + amdgpu_cs_report_moved_bytes(p->adev, p->bytes_moved, + p->bytes_moved_vis); } if (p->uf_bo) { @@ -973,9 +982,6 @@ static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p, p->gang_leader->uf_addr += amdgpu_bo_gpu_offset(p->uf_bo); } - amdgpu_cs_report_moved_bytes(p->adev, p->bytes_moved, - p->bytes_moved_vis); - for (i = 0; i < p->gang_size; ++i) amdgpu_job_set_resources(p->jobs[i], p->bo_list->gds_obj, p->bo_list->gws_obj,