From patchwork Fri Nov 24 23:36:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 13468233 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 CF321C61D97 for ; Fri, 24 Nov 2023 23:37:08 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 2BF8410E0EC; Fri, 24 Nov 2023 23:37:07 +0000 (UTC) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by gabe.freedesktop.org (Postfix) with ESMTPS id 1D9C210E0EC for ; Fri, 24 Nov 2023 23:37:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1700869023; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=auqPkghBUASB9YQ336TJ1HIAZATGxqMLyF8qUrxjjf4=; b=gFfkzFnd9iFoSR2C9QOqNYjj0UGHKHjWm2pnbD0t78sbc8M6J+hFXPk/PxASE5nhnwrQMM hHagAqDic/spNkpMIW4KioQKSJMD21eEm8IBQxesmefLR45bunciZ6aCzZNXv+xcSe5zAk jbqc7tICr9fX9gSo+aMLrvfCXqnVxcU= Received: from mail-lj1-f197.google.com (mail-lj1-f197.google.com [209.85.208.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-464-QSdxVnbsNYSWNsnbYhHjmw-1; Fri, 24 Nov 2023 18:37:02 -0500 X-MC-Unique: QSdxVnbsNYSWNsnbYhHjmw-1 Received: by mail-lj1-f197.google.com with SMTP id 38308e7fff4ca-2c53ea92642so21217581fa.2 for ; Fri, 24 Nov 2023 15:37:01 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700869020; x=1701473820; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=auqPkghBUASB9YQ336TJ1HIAZATGxqMLyF8qUrxjjf4=; b=UAsOU7mPji7dbO2VENhgQcICD1cCLgePNRhoCaOYrkkSpiGfFHWr5PGVpp9I9u5Ql5 0tpb0pKhX77OI9ruiXcOcZUqbj7DhzdOOcqzsjB1Lz9Jn8NLYyR/FBWQPXQxG3ikc8Lr kRpHglQP4asuJ2LWeYm5gxrd9Y0ualLcTHCpBv0sSz7dliaX+m1aRsml7i8+4NQBVwaN wKOpcL9ZytGUE9nulf7y7euKYhhPgj+HXdHh3bTaNVIa7Qao/avgcLFe0cqgcOayt11F 7JoG/5REBIK0UbG7+3ItzAdvy0bVqp858yWlXN49HchQ1f/PtiC0ywjFx95wr+T7Y4eM ZJAA== X-Gm-Message-State: AOJu0YxNzKT8zPy2Tofk81gA+AjM93gWkylWWOd/gXKblEaoIEL9gzhb aK2T3MQFhIutdHDsge8dRCYl1xcOCG3GRiR69ssw/KOi3mbhUqASBPJRJErDNqdVPhY4tBEPKD5 dtjwElk+9hivxaJbFhp09hZADlDKZ X-Received: by 2002:a2e:9dd9:0:b0:2c5:183d:42bf with SMTP id x25-20020a2e9dd9000000b002c5183d42bfmr3147722ljj.45.1700869020659; Fri, 24 Nov 2023 15:37:00 -0800 (PST) X-Google-Smtp-Source: AGHT+IEylly0/xjh3bd979MfjJPHjqJh2nTHcO1bCjYpiXsPXTwaA+GMEpL5vvLU6l2RLJIGMPK6VA== X-Received: by 2002:a2e:9dd9:0:b0:2c5:183d:42bf with SMTP id x25-20020a2e9dd9000000b002c5183d42bfmr3147720ljj.45.1700869020366; Fri, 24 Nov 2023 15:37:00 -0800 (PST) Received: from pollux.. ([2a02:810d:4b3f:de9c:abf:b8ff:feee:998b]) by smtp.gmail.com with ESMTPSA id q22-20020a1709064c9600b009ae69c303aasm2631313eju.137.2023.11.24.15.36.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Nov 2023 15:36:59 -0800 (PST) From: Danilo Krummrich To: mripard@kernel.org, airlied@gmail.com, daniel@ffwll.ch, frank.binns@imgtec.com, donald.robson@imgtec.com, matt.coster@imgtec.com, sarah.walker@imgtec.com Subject: [PATCH drm-misc-next 1/5] drm/imagination: vm: prevent duplicate drm_gpuvm_bo instances Date: Sat, 25 Nov 2023 00:36:36 +0100 Message-ID: <20231124233650.152653-2-dakr@redhat.com> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20231124233650.152653-1-dakr@redhat.com> References: <20231124233650.152653-1-dakr@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com 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: , Cc: Danilo Krummrich , linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" Use drm_gpuvm_bo_obtain() instead of drm_gpuvm_bo_create(). The latter should only be used in conjunction with drm_gpuvm_bo_obtain_prealloc(). drm_gpuvm_bo_obtain() re-uses existing instances of a given VM and BO combination, whereas drm_gpuvm_bo_create() would always create a new instance of struct drm_gpuvm_bo and hence leave us with duplicates. Fixes: ff5f643de0bf ("drm/imagination: Add GEM and VM related code") Signed-off-by: Danilo Krummrich Reviewed-by: Donald Robson --- drivers/gpu/drm/imagination/pvr_vm.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/drivers/gpu/drm/imagination/pvr_vm.c b/drivers/gpu/drm/imagination/pvr_vm.c index 3ad1366294b9..09d481c575b0 100644 --- a/drivers/gpu/drm/imagination/pvr_vm.c +++ b/drivers/gpu/drm/imagination/pvr_vm.c @@ -224,6 +224,7 @@ pvr_vm_bind_op_map_init(struct pvr_vm_bind_op *bind_op, struct pvr_gem_object *pvr_obj, u64 offset, u64 device_addr, u64 size) { + struct drm_gem_object *obj = gem_from_pvr_gem(pvr_obj); const bool is_user = vm_ctx == vm_ctx->pvr_dev->kernel_vm_ctx; const u64 pvr_obj_size = pvr_gem_object_size(pvr_obj); struct sg_table *sgt; @@ -245,10 +246,11 @@ pvr_vm_bind_op_map_init(struct pvr_vm_bind_op *bind_op, bind_op->type = PVR_VM_BIND_TYPE_MAP; - bind_op->gpuvm_bo = drm_gpuvm_bo_create(&vm_ctx->gpuvm_mgr, - gem_from_pvr_gem(pvr_obj)); - if (!bind_op->gpuvm_bo) - return -ENOMEM; + dma_resv_lock(obj->resv, NULL); + bind_op->gpuvm_bo = drm_gpuvm_bo_obtain(&vm_ctx->gpuvm_mgr, obj); + dma_resv_unlock(obj->resv); + if (IS_ERR(bind_op->gpuvm_bo)) + return PTR_ERR(bind_op->gpuvm_bo); bind_op->new_va = kzalloc(sizeof(*bind_op->new_va), GFP_KERNEL); bind_op->prev_va = kzalloc(sizeof(*bind_op->prev_va), GFP_KERNEL); From patchwork Fri Nov 24 23:36:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 13468234 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 C9240C61DF4 for ; Fri, 24 Nov 2023 23:37:14 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 61B6410E21A; Fri, 24 Nov 2023 23:37:11 +0000 (UTC) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by gabe.freedesktop.org (Postfix) with ESMTPS id 81D6E10E21A for ; Fri, 24 Nov 2023 23:37:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1700869028; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=PxcVKcnovZ3WmT6VXlVQva+pEVKvpiQ4FcmwXIH6X8E=; b=L0uX4FWDJ/rLTrxOThydMQ7SAdUcJRL9lMcxKXl9ZVD4vq+b9LnaIEmI4my9F6JLbqmW7X 9FLfauWAufjxD6e+brmdyjymj3j+EolWu4t9ySItQUkoXN4KQrYVeDKFeHrv1MKjWR4V/e n/waO77EMWtak3nZrM5vLr+2EMi2msI= Received: from mail-ej1-f69.google.com (mail-ej1-f69.google.com [209.85.218.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-84-ZOToJ3okNMWQm3tx9vcEJQ-1; Fri, 24 Nov 2023 18:37:05 -0500 X-MC-Unique: ZOToJ3okNMWQm3tx9vcEJQ-1 Received: by mail-ej1-f69.google.com with SMTP id a640c23a62f3a-a02b9c28a08so160750166b.0 for ; Fri, 24 Nov 2023 15:37:05 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700869024; x=1701473824; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=PxcVKcnovZ3WmT6VXlVQva+pEVKvpiQ4FcmwXIH6X8E=; b=qJ4fqWK9IzQNfhRAv8KPqARRXOlAgDFAP31JRTOD+SchjX0eeQ4jeRmbS8gt7WLVch ZAmLyeHGwT/EzzZGW/3BZlOeysbuc87mXZMafCJXq30gxlO33zMd4snyAE9uwdUrux4F eXwd1jFki9XLxMPl6sUbo8RtRU/qectEEqLRr+4QgFb4wTn49admUdhOJZAOmz+4vbMi 9P1Ko+NvGSoA+uTo45fq0vfXNWpcY8WgHaoXJ05WqGYWnbqp1sWsY1EMfqAVdyzpQ3K/ cB8KKfiy9MknU932j51ScBTmc5F3oJgBKjbbxQCvwez1o9ePGVcUqb32q3TyVm61BDQV vHkQ== X-Gm-Message-State: AOJu0YxmmMLatoraOkNqjMXHc0lnubkSmBrH70cZGIKLspS46xJiQyXf ROLgHBm+FTlXSIsj31Xuiei5uFdBtgpvVNjJMfDJuFZJEfPXpLik+nqi0550cETVzl4ch7LZhfa uoJiPFQzzfgdKHBL7T+Fla5pK2nrh X-Received: by 2002:a17:906:51c8:b0:a00:2947:409d with SMTP id v8-20020a17090651c800b00a002947409dmr3186799ejk.12.1700869024358; Fri, 24 Nov 2023 15:37:04 -0800 (PST) X-Google-Smtp-Source: AGHT+IHi3IqHfjuue65RKfXtgnC8RUrgAB9/KKEn/ifKMbimZnKv4/th/PQwNIcKNEiUQH3jh7UVig== X-Received: by 2002:a17:906:51c8:b0:a00:2947:409d with SMTP id v8-20020a17090651c800b00a002947409dmr3186782ejk.12.1700869023988; Fri, 24 Nov 2023 15:37:03 -0800 (PST) Received: from pollux.. ([2a02:810d:4b3f:de9c:abf:b8ff:feee:998b]) by smtp.gmail.com with ESMTPSA id w1-20020a170906b18100b00a046a773175sm2636639ejy.122.2023.11.24.15.37.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Nov 2023 15:37:03 -0800 (PST) From: Danilo Krummrich To: mripard@kernel.org, airlied@gmail.com, daniel@ffwll.ch, frank.binns@imgtec.com, donald.robson@imgtec.com, matt.coster@imgtec.com, sarah.walker@imgtec.com Subject: [PATCH drm-misc-next 2/5] drm/imagination: vm: check for drm_gpuvm_range_valid() Date: Sat, 25 Nov 2023 00:36:37 +0100 Message-ID: <20231124233650.152653-3-dakr@redhat.com> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20231124233650.152653-1-dakr@redhat.com> References: <20231124233650.152653-1-dakr@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com 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: , Cc: Danilo Krummrich , linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" Extend pvr_device_addr_and_size_are_valid() by the corresponding GPUVM sanity checks. This includes a, previously missing, overflow check for the base address and size of the requested mapping. Fixes: ff5f643de0bf ("drm/imagination: Add GEM and VM related code") Signed-off-by: Danilo Krummrich Reviewed-by: Donald Robson --- drivers/gpu/drm/imagination/pvr_vm.c | 9 ++++++--- drivers/gpu/drm/imagination/pvr_vm.h | 3 ++- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/drivers/gpu/drm/imagination/pvr_vm.c b/drivers/gpu/drm/imagination/pvr_vm.c index 09d481c575b0..1e89092c3dcc 100644 --- a/drivers/gpu/drm/imagination/pvr_vm.c +++ b/drivers/gpu/drm/imagination/pvr_vm.c @@ -239,7 +239,7 @@ pvr_vm_bind_op_map_init(struct pvr_vm_bind_op *bind_op, return -EINVAL; } - if (!pvr_device_addr_and_size_are_valid(device_addr, size) || + if (!pvr_device_addr_and_size_are_valid(vm_ctx, device_addr, size) || offset & ~PAGE_MASK || size & ~PAGE_MASK || offset >= pvr_obj_size || offset_plus_size > pvr_obj_size) return -EINVAL; @@ -295,7 +295,7 @@ pvr_vm_bind_op_unmap_init(struct pvr_vm_bind_op *bind_op, { int err; - if (!pvr_device_addr_and_size_are_valid(device_addr, size)) + if (!pvr_device_addr_and_size_are_valid(vm_ctx, device_addr, size)) return -EINVAL; bind_op->type = PVR_VM_BIND_TYPE_UNMAP; @@ -505,6 +505,7 @@ pvr_device_addr_is_valid(u64 device_addr) /** * pvr_device_addr_and_size_are_valid() - Tests whether a device-virtual * address and associated size are both valid. + * @vm_ctx: Target VM context. * @device_addr: Virtual device address to test. * @size: Size of the range based at @device_addr to test. * @@ -523,9 +524,11 @@ pvr_device_addr_is_valid(u64 device_addr) * * %false otherwise. */ bool -pvr_device_addr_and_size_are_valid(u64 device_addr, u64 size) +pvr_device_addr_and_size_are_valid(struct pvr_vm_context *vm_ctx, + u64 device_addr, u64 size) { return pvr_device_addr_is_valid(device_addr) && + drm_gpuvm_range_valid(&vm_ctx->gpuvm_mgr, device_addr, size) && size != 0 && (size & ~PVR_DEVICE_PAGE_MASK) == 0 && (device_addr + size <= PVR_PAGE_TABLE_ADDR_SPACE_SIZE); } diff --git a/drivers/gpu/drm/imagination/pvr_vm.h b/drivers/gpu/drm/imagination/pvr_vm.h index cf8b97553dc8..f2a6463f2b05 100644 --- a/drivers/gpu/drm/imagination/pvr_vm.h +++ b/drivers/gpu/drm/imagination/pvr_vm.h @@ -29,7 +29,8 @@ struct drm_exec; /* Functions defined in pvr_vm.c */ bool pvr_device_addr_is_valid(u64 device_addr); -bool pvr_device_addr_and_size_are_valid(u64 device_addr, u64 size); +bool pvr_device_addr_and_size_are_valid(struct pvr_vm_context *vm_ctx, + u64 device_addr, u64 size); struct pvr_vm_context *pvr_vm_create_context(struct pvr_device *pvr_dev, bool is_userspace_context); From patchwork Fri Nov 24 23:36:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 13468236 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 363B9C61D97 for ; Fri, 24 Nov 2023 23:37:22 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 4D38710E37C; Fri, 24 Nov 2023 23:37:21 +0000 (UTC) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by gabe.freedesktop.org (Postfix) with ESMTPS id A91F910E380 for ; Fri, 24 Nov 2023 23:37:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1700869035; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=o9d7VDWKIT6kRskPMwA56/YhEUxpKcUgE6jJTCtxE4g=; b=NdamtGkI+DxIDL1kCI3u/HbdAlrXKgDlgB/iPLITKNBMFG1DYQgrwXCxrFdNBbU6s5vsFl rvX/x8GLnrT/iX6hiJ2SzN8QsqNAC3/8vo5tyufri4ksCaDHxMIzESn+GqO/ZpHucX9aH3 Ddv7GRzsOOJilYp6TPDjOTDBcHy8K8I= Received: from mail-lf1-f71.google.com (mail-lf1-f71.google.com [209.85.167.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-130-Wb_AfZR4MxiUz6L1sHjUhQ-1; Fri, 24 Nov 2023 18:37:09 -0500 X-MC-Unique: Wb_AfZR4MxiUz6L1sHjUhQ-1 Received: by mail-lf1-f71.google.com with SMTP id 2adb3069b0e04-50aa822cb33so2277781e87.0 for ; Fri, 24 Nov 2023 15:37:08 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700869028; x=1701473828; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=o9d7VDWKIT6kRskPMwA56/YhEUxpKcUgE6jJTCtxE4g=; b=eBkAKepbgBmkLP9qCqBkCIRJKWIgIijLzbS0++Ybt4CLKEIGwv7OxUBMjGa6K/0Eh7 faEnGu9HYh0Iee4/2FZagGrO9cM4n5JmZDF9KjSSdQmrjgWvWM7CbASiDy93fxoijyC+ 3348XKMtEOyeN2LCMXrdAQ89oZjFLyXMu4yqS6RYwDxul+dW2481LUAold4npnFh5wcm Uc/6jbxILuR4hrjNFsfvt64S5VZYRYAHCZ2OQM8ohU5adrJHvCF5hL6snBGzxE3vRJXV +J9OLbDDhFEe1MQ3FlrJMFIxyBcVcTqcaOT8NtaqYEFMKKDPLgWWo8Un4JlHL4CW7G8U wVXQ== X-Gm-Message-State: AOJu0YxEM9oGF06iUWRtRoTstQeH2hjx7SKwB0HhQ5r0WHZOhFufPBAW QScnS7hW1OKK1Max9ls5Ay4u1skrHCNpm8Lqknkv5GUOOZC1TzhwXrhMb9+K+NAP3U6cVRFh0rH dUGHsCwg87Ada6AltJLL0tzbYkk3Z X-Received: by 2002:a05:6512:a92:b0:500:99a9:bc40 with SMTP id m18-20020a0565120a9200b0050099a9bc40mr3990401lfu.69.1700869027863; Fri, 24 Nov 2023 15:37:07 -0800 (PST) X-Google-Smtp-Source: AGHT+IHASf51P/stzy6MO1wClOrbLB0m2h5TDmbZM6s6hMIvpjkKqyVWebotr5L22G9Z2BXEHBKA2A== X-Received: by 2002:a05:6512:a92:b0:500:99a9:bc40 with SMTP id m18-20020a0565120a9200b0050099a9bc40mr3990385lfu.69.1700869027648; Fri, 24 Nov 2023 15:37:07 -0800 (PST) Received: from pollux.. ([2a02:810d:4b3f:de9c:abf:b8ff:feee:998b]) by smtp.gmail.com with ESMTPSA id n18-20020a17090625d200b009fe1d575365sm2664262ejb.55.2023.11.24.15.37.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Nov 2023 15:37:07 -0800 (PST) From: Danilo Krummrich To: mripard@kernel.org, airlied@gmail.com, daniel@ffwll.ch, frank.binns@imgtec.com, donald.robson@imgtec.com, matt.coster@imgtec.com, sarah.walker@imgtec.com Subject: [PATCH drm-misc-next 3/5] drm/imagination: vm: fix drm_gpuvm reference count Date: Sat, 25 Nov 2023 00:36:38 +0100 Message-ID: <20231124233650.152653-4-dakr@redhat.com> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20231124233650.152653-1-dakr@redhat.com> References: <20231124233650.152653-1-dakr@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com 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: , Cc: Danilo Krummrich , linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" The driver specific reference count indicates whether the VM should be teared down, whereas GPUVM's reference count indicates whether the VM structure can finally be freed. Hence, free the VM structure in pvr_gpuvm_free() and drop the last GPUVM reference after tearing down the VM. Generally, this prevents lifetime issues such as the VM being freed as long as drm_gpuvm_bo structures still hold references to the VM. Fixes: ff5f643de0bf ("drm/imagination: Add GEM and VM related code") Signed-off-by: Danilo Krummrich Reviewed-by: Donald Robson --- drivers/gpu/drm/imagination/pvr_vm.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/drivers/gpu/drm/imagination/pvr_vm.c b/drivers/gpu/drm/imagination/pvr_vm.c index 1e89092c3dcc..e0d74d9a6190 100644 --- a/drivers/gpu/drm/imagination/pvr_vm.c +++ b/drivers/gpu/drm/imagination/pvr_vm.c @@ -64,6 +64,12 @@ struct pvr_vm_context { struct drm_gem_object dummy_gem; }; +static inline +struct pvr_vm_context *to_pvr_vm_context(struct drm_gpuvm *gpuvm) +{ + return container_of(gpuvm, struct pvr_vm_context, gpuvm_mgr); +} + struct pvr_vm_context *pvr_vm_context_get(struct pvr_vm_context *vm_ctx) { if (vm_ctx) @@ -535,7 +541,7 @@ pvr_device_addr_and_size_are_valid(struct pvr_vm_context *vm_ctx, void pvr_gpuvm_free(struct drm_gpuvm *gpuvm) { - + kfree(to_pvr_vm_context(gpuvm)); } static const struct drm_gpuvm_ops pvr_vm_gpuva_ops = { @@ -655,12 +661,11 @@ pvr_vm_context_release(struct kref *ref_count) WARN_ON(pvr_vm_unmap(vm_ctx, vm_ctx->gpuvm_mgr.mm_start, vm_ctx->gpuvm_mgr.mm_range)); - drm_gpuvm_put(&vm_ctx->gpuvm_mgr); pvr_mmu_context_destroy(vm_ctx->mmu_ctx); drm_gem_private_object_fini(&vm_ctx->dummy_gem); mutex_destroy(&vm_ctx->lock); - kfree(vm_ctx); + drm_gpuvm_put(&vm_ctx->gpuvm_mgr); } /** From patchwork Fri Nov 24 23:36:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 13468237 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 CD532C61DF4 for ; Fri, 24 Nov 2023 23:37:23 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 44C9410E380; Fri, 24 Nov 2023 23:37:22 +0000 (UTC) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by gabe.freedesktop.org (Postfix) with ESMTPS id 637EC10E37C for ; Fri, 24 Nov 2023 23:37:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1700869036; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=izHSVRiDPLqloSEgKNfnuq4BC7zmY7Slqcod5ZMSAJI=; b=DntNbxp29IGn24HuhJ5FqL3AqvX4po0NhSkVqo0I98gIAJabIPLtrOfGjVXevoQkeUiXeE KTPjw2nNVTrjKnR/gNQ21UGt984KvO6mBMAc3iCJzrpPWSdnna9sw0CMZNQzjRsY5wspQt 11y49TSkIIsM+X98BO0G5jC+g1PLFHE= Received: from mail-ej1-f72.google.com (mail-ej1-f72.google.com [209.85.218.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-696-kO2NyIwEPu-iD_CKjTghcg-1; Fri, 24 Nov 2023 18:37:12 -0500 X-MC-Unique: kO2NyIwEPu-iD_CKjTghcg-1 Received: by mail-ej1-f72.google.com with SMTP id a640c23a62f3a-a03337f7851so172527566b.2 for ; Fri, 24 Nov 2023 15:37:12 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700869031; x=1701473831; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=izHSVRiDPLqloSEgKNfnuq4BC7zmY7Slqcod5ZMSAJI=; b=kGrGxFv/W/pATUjx682XnypV5JyBbN+XiilNGmtHx3611tzbR4bVXZJyAtetX7Rro0 vgrRmtrBDAIsdFOX/luDBtEy3kmkk8qNno4ELlIzWCLdHbBkmGGTwrtIIeWp5YsVgReZ V1irk+EweKplQ4Q2zYPmJTYd9L9KHNDLrVmFZsLOBBXeu26leai7YJak69POj9aaOwX4 /b3/NgVrUTWzFaF78tfeFw0ahvS5LclloDWz2AV2DQf94e2eSxaJXz13RBfzVkf/1AA3 2X0bjJFLsUurHTGYadPCrALUpYj2dRE3rg1BZat0AJLs5pSW7Ky807EC1ccF8vgBzPvS QVVw== X-Gm-Message-State: AOJu0YyCFdD6ltH5FVddMwUtSk/HXee85dfQWGOFeTqx03M5RY01GMzX j/2zLI8u1pvQkXev0N1O7KambzXQMLEbdYkFvSAVYwnyR2csk99eQ9hMBMMXLViSStcqYT2kKq6 jniMXFyTsFk7PpTcf7OILJwK1o/HT X-Received: by 2002:a17:906:2ad7:b0:9b7:37de:601a with SMTP id m23-20020a1709062ad700b009b737de601amr3498398eje.49.1700869031736; Fri, 24 Nov 2023 15:37:11 -0800 (PST) X-Google-Smtp-Source: AGHT+IG7AM/j8VK9NPe0eYzewGeBE2LWUQe7Blb8qYhXMJvXKt25UPVDkshQAwVtb5iSlMHYw7d5Ug== X-Received: by 2002:a17:906:2ad7:b0:9b7:37de:601a with SMTP id m23-20020a1709062ad700b009b737de601amr3498388eje.49.1700869031532; Fri, 24 Nov 2023 15:37:11 -0800 (PST) Received: from pollux.. ([2a02:810d:4b3f:de9c:abf:b8ff:feee:998b]) by smtp.gmail.com with ESMTPSA id lb16-20020a170907785000b009fda665860csm2641510ejc.22.2023.11.24.15.37.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Nov 2023 15:37:10 -0800 (PST) From: Danilo Krummrich To: mripard@kernel.org, airlied@gmail.com, daniel@ffwll.ch, frank.binns@imgtec.com, donald.robson@imgtec.com, matt.coster@imgtec.com, sarah.walker@imgtec.com Subject: [PATCH drm-misc-next 4/5] drm/gpuvm: fall back to drm_exec_lock_obj() Date: Sat, 25 Nov 2023 00:36:39 +0100 Message-ID: <20231124233650.152653-5-dakr@redhat.com> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20231124233650.152653-1-dakr@redhat.com> References: <20231124233650.152653-1-dakr@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com 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: , Cc: Danilo Krummrich , linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org, =?utf-8?q?Christian_K=C3=B6nig?= Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" Fall back to drm_exec_lock_obj() if num_fences is zero for the drm_gpuvm_prepare_* function family. Otherwise dma_resv_reserve_fences() would actually allocate slots even though num_fences is zero. Cc: Christian König Signed-off-by: Danilo Krummrich --- drivers/gpu/drm/drm_gpuvm.c | 36 +++++++++++++++++++++++++++++++++--- include/drm/drm_gpuvm.h | 23 +++-------------------- 2 files changed, 36 insertions(+), 23 deletions(-) diff --git a/drivers/gpu/drm/drm_gpuvm.c b/drivers/gpu/drm/drm_gpuvm.c index 54f5e8851de5..d1d1c2379e44 100644 --- a/drivers/gpu/drm/drm_gpuvm.c +++ b/drivers/gpu/drm/drm_gpuvm.c @@ -1085,6 +1085,36 @@ drm_gpuvm_put(struct drm_gpuvm *gpuvm) } EXPORT_SYMBOL_GPL(drm_gpuvm_put); +static int +exec_prepare_obj(struct drm_exec *exec, struct drm_gem_object *obj, + unsigned int num_fences) +{ + return num_fences ? drm_exec_prepare_obj(exec, obj, num_fences) : + drm_exec_lock_obj(exec, obj); +} + +/** + * drm_gpuvm_prepare_vm() - prepare the GPUVMs common dma-resv + * @gpuvm: the &drm_gpuvm + * @exec: the &drm_exec context + * @num_fences: the amount of &dma_fences to reserve + * + * Calls drm_exec_prepare_obj() for the GPUVMs dummy &drm_gem_object. + * + * Using this function directly, it is the drivers responsibility to call + * drm_exec_init() and drm_exec_fini() accordingly. + * + * Returns: 0 on success, negative error code on failure. + */ +int +drm_gpuvm_prepare_vm(struct drm_gpuvm *gpuvm, + struct drm_exec *exec, + unsigned int num_fences) +{ + return exec_prepare_obj(exec, gpuvm->r_obj, num_fences); +} +EXPORT_SYMBOL_GPL(drm_gpuvm_prepare_vm); + static int __drm_gpuvm_prepare_objects(struct drm_gpuvm *gpuvm, struct drm_exec *exec, @@ -1095,7 +1125,7 @@ __drm_gpuvm_prepare_objects(struct drm_gpuvm *gpuvm, int ret = 0; for_each_vm_bo_in_list(gpuvm, extobj, &extobjs, vm_bo) { - ret = drm_exec_prepare_obj(exec, vm_bo->obj, num_fences); + ret = exec_prepare_obj(exec, vm_bo->obj, num_fences); if (ret) break; } @@ -1116,7 +1146,7 @@ drm_gpuvm_prepare_objects_locked(struct drm_gpuvm *gpuvm, drm_gpuvm_resv_assert_held(gpuvm); list_for_each_entry(vm_bo, &gpuvm->extobj.list, list.entry.extobj) { - ret = drm_exec_prepare_obj(exec, vm_bo->obj, num_fences); + ret = exec_prepare_obj(exec, vm_bo->obj, num_fences); if (ret) break; @@ -1186,7 +1216,7 @@ drm_gpuvm_prepare_range(struct drm_gpuvm *gpuvm, struct drm_exec *exec, drm_gpuvm_for_each_va_range(va, gpuvm, addr, end) { struct drm_gem_object *obj = va->gem.obj; - ret = drm_exec_prepare_obj(exec, obj, num_fences); + ret = exec_prepare_obj(exec, obj, num_fences); if (ret) return ret; } diff --git a/include/drm/drm_gpuvm.h b/include/drm/drm_gpuvm.h index f94fec9a8517..b3f82ec7fb17 100644 --- a/include/drm/drm_gpuvm.h +++ b/include/drm/drm_gpuvm.h @@ -544,26 +544,9 @@ struct drm_gpuvm_exec { } extra; }; -/** - * drm_gpuvm_prepare_vm() - prepare the GPUVMs common dma-resv - * @gpuvm: the &drm_gpuvm - * @exec: the &drm_exec context - * @num_fences: the amount of &dma_fences to reserve - * - * Calls drm_exec_prepare_obj() for the GPUVMs dummy &drm_gem_object. - * - * Using this function directly, it is the drivers responsibility to call - * drm_exec_init() and drm_exec_fini() accordingly. - * - * Returns: 0 on success, negative error code on failure. - */ -static inline int -drm_gpuvm_prepare_vm(struct drm_gpuvm *gpuvm, - struct drm_exec *exec, - unsigned int num_fences) -{ - return drm_exec_prepare_obj(exec, gpuvm->r_obj, num_fences); -} +int drm_gpuvm_prepare_vm(struct drm_gpuvm *gpuvm, + struct drm_exec *exec, + unsigned int num_fences); int drm_gpuvm_prepare_objects(struct drm_gpuvm *gpuvm, struct drm_exec *exec, From patchwork Fri Nov 24 23:36:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 13468238 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 BD8E6C61D97 for ; Fri, 24 Nov 2023 23:37:25 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 409DF10E387; Fri, 24 Nov 2023 23:37:23 +0000 (UTC) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by gabe.freedesktop.org (Postfix) with ESMTPS id C0A4810E37C for ; Fri, 24 Nov 2023 23:37:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1700869038; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=RTwNQN1v75eY2Dvgc8MjegaphCjYwXgyBTZJw1w/hwE=; b=Q97PneDatilpnco8YrHiPtEiDVxdsVvSLRYJrvJig0rZTk1YdGCB7AaUyZbW2ehiwdhjDq sGmAGZKl050qR8VuQRbJt8L2amdPhm791UikrH4BhQKwR2U7G10KfAVrAK36MHnV0LpVvd BTNfDniV7ZAbwLtXer1gLHWiJRoqaG0= Received: from mail-ej1-f69.google.com (mail-ej1-f69.google.com [209.85.218.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-311-h-yQ2WeGPBaY9OxKYB51Tg-1; Fri, 24 Nov 2023 18:37:16 -0500 X-MC-Unique: h-yQ2WeGPBaY9OxKYB51Tg-1 Received: by mail-ej1-f69.google.com with SMTP id a640c23a62f3a-9fffa4c4f28so156402066b.3 for ; Fri, 24 Nov 2023 15:37:16 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700869035; x=1701473835; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=RTwNQN1v75eY2Dvgc8MjegaphCjYwXgyBTZJw1w/hwE=; b=MCiogIaXL8mzGU/v7RYbVuVg1thFYCfFJbJBrI/+Uq3GpSvgPbhTVh3Zq/gEcKx6Ht eWK0jCijaAoQyvxenMQufup271d11DAyhGOWd1w6tmlW1j6FUA3WjeSRsv6pSTWORW33 KnToU07OQdGbwdaOYtcd3U2XtpNgoU7ZdTuSKeOHdsE5nLnQBqSeer8Um+4Y0qtKLsZW U17LwP8Zbzvot70T+wanPSk31226Oz43K5698u5PnAwG1xMRKIg9UgUmg7ogd49Auonj i3MVUZdiikze4yAnLtrBBjyVvmULVYSE7uo6Bq+kzJU9/6gezTwBt1wWBYkaB5sdFTZX j0BA== X-Gm-Message-State: AOJu0YwvTvvlK07KBXLPVDoCkujqXxE6w5KRCL61sYGHC02+8Q8PpGVC CkUR7IYoSCjLZrOn+28J9Gd1Zlri0DrjN6s9ZRwOvZThxm0tpxXeG4vOzkBm+LADCojWAbxmnqj wFRlUvoRq5j5FhTJdHmybHkaIJO4L X-Received: by 2002:a17:906:74ca:b0:9dd:87ec:9455 with SMTP id z10-20020a17090674ca00b009dd87ec9455mr3754025ejl.48.1700869035416; Fri, 24 Nov 2023 15:37:15 -0800 (PST) X-Google-Smtp-Source: AGHT+IGPpFhYhbOjtcDOebmW6OKpEMEhuiIuahmko3GKgrpRAjDGHm36lGmiO0pqGG9IYw4PVtIdjw== X-Received: by 2002:a17:906:74ca:b0:9dd:87ec:9455 with SMTP id z10-20020a17090674ca00b009dd87ec9455mr3754014ejl.48.1700869035224; Fri, 24 Nov 2023 15:37:15 -0800 (PST) Received: from pollux.. ([2a02:810d:4b3f:de9c:abf:b8ff:feee:998b]) by smtp.gmail.com with ESMTPSA id w24-20020a170906481800b009920e9a3a73sm2651101ejq.115.2023.11.24.15.37.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Nov 2023 15:37:14 -0800 (PST) From: Danilo Krummrich To: mripard@kernel.org, airlied@gmail.com, daniel@ffwll.ch, frank.binns@imgtec.com, donald.robson@imgtec.com, matt.coster@imgtec.com, sarah.walker@imgtec.com Subject: [PATCH drm-misc-next 5/5] drm/imagination: vm: make use of GPUVM's drm_exec helper Date: Sat, 25 Nov 2023 00:36:40 +0100 Message-ID: <20231124233650.152653-6-dakr@redhat.com> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20231124233650.152653-1-dakr@redhat.com> References: <20231124233650.152653-1-dakr@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com 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: , Cc: Danilo Krummrich , linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" Make use of GPUVM's drm_exec helper functions preventing direct access to GPUVM internal data structures, such as the external object list. This is especially important to ensure following the locking rules around the GPUVM external object list. Fixes: ff5f643de0bf ("drm/imagination: Add GEM and VM related code") Signed-off-by: Danilo Krummrich --- drivers/gpu/drm/imagination/pvr_vm.c | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/drivers/gpu/drm/imagination/pvr_vm.c b/drivers/gpu/drm/imagination/pvr_vm.c index e0d74d9a6190..3f7888f5cc53 100644 --- a/drivers/gpu/drm/imagination/pvr_vm.c +++ b/drivers/gpu/drm/imagination/pvr_vm.c @@ -337,27 +337,21 @@ static int pvr_vm_bind_op_lock_resvs(struct drm_exec *exec, struct pvr_vm_bind_op *bind_op) { drm_exec_until_all_locked(exec) { - struct drm_gem_object *r_obj = &bind_op->vm_ctx->dummy_gem; struct drm_gpuvm *gpuvm = &bind_op->vm_ctx->gpuvm_mgr; struct pvr_gem_object *pvr_obj = bind_op->pvr_obj; - struct drm_gpuvm_bo *gpuvm_bo; /* Acquire lock on the vm_context's reserve object. */ - int err = drm_exec_lock_obj(exec, r_obj); + int err = drm_gpuvm_prepare_vm(gpuvm, exec, 0); drm_exec_retry_on_contention(exec); if (err) return err; /* Acquire lock on all BOs in the context. */ - list_for_each_entry(gpuvm_bo, &gpuvm->extobj.list, - list.entry.extobj) { - err = drm_exec_lock_obj(exec, gpuvm_bo->obj); - - drm_exec_retry_on_contention(exec); - if (err) - return err; - } + err = drm_gpuvm_prepare_objects(gpuvm, exec, 0); + drm_exec_retry_on_contention(exec); + if (err) + return err; /* Unmap operations don't have an object to lock. */ if (!pvr_obj)