Received: by 2002:a05:6358:45e:b0:b5:b6eb:e1f9 with SMTP id 30csp4514428rwe; Tue, 30 Aug 2022 11:28:30 -0700 (PDT) X-Google-Smtp-Source: AA6agR7zOrnWBGaLiM874uD+sCkOKkKLzTs79JQPG7HkoRCNuGGexbSxCKtdI9P2iR7aAdRLjxfO X-Received: by 2002:a05:6a00:7c8:b0:537:a0f0:cacf with SMTP id n8-20020a056a0007c800b00537a0f0cacfmr22757645pfu.77.1661884110385; Tue, 30 Aug 2022 11:28:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661884110; cv=none; d=google.com; s=arc-20160816; b=dtJoNyqHScFJKbcAMSz6oteQgURdNZE75Lt2+JwmrmaXFwuceLlhy02vyNJjbJB+lG gX2DxRHXr65U8ZOWm4HdIgCtWBZG6b/tSdMsGT7zwQyJGCbO4+OWSF2C0WWBswBJo/6/ 2FpEUC27Vw/WqoGc5OMQZbxrkps2sx7W2KAG/q3ZIt/o6pgHbHQltTGLWv7kkk9tRi1H SWL03uko8vCi+BoFObhCxGImZQXuwegt+hY5NhKl/8mg5oC9a/z2q956Y9QW1+d1OVqr a57sxB5D8rAN9Iu2ufEckBx5vjb5BAPe+ionY9f1mjpkcmVvFYk2LzBmHZEeATa1l2rK 6okw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=T4XqepdsFu0ySoOpBxDDH5FEFg3YLP8J0OKTJODV5AA=; b=GzuS7kZF9EQeUoPCtLHqqxefK+l076RKbVQRi4dNDsoSe3UTeQoyRUPeQsOxkCm8/k EXqlnzkwvQSJ0DrZDLeYncI53eEik5RMViphKvO6IiDl2kHTcZ8lh5Pm0AK5mpeRHtbu +PGcHGQdQekkjun9qZcOL9EMz9j5hTWaKvVgogk6cPnsySGR9chlUPerMieyZ+Z6MwxW c0H9UrLq1wRStkviaA8rjE0hexxLRJo7cvNwpsRk49bhVpZ/CzOHwgET50zyNLlsIZn0 BYr/UfafW459BKmBUVeRakv28EC+GSxD/BTtXawOMFOIo28ZGtr7eSuismCH5Vk6twln 3jfg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=lWLD0a92; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x5-20020a1709027c0500b001731029cd79si7732003pll.208.2022.08.30.11.28.15; Tue, 30 Aug 2022 11:28:30 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=lWLD0a92; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230081AbiH3RTF (ORCPT + 99 others); Tue, 30 Aug 2022 13:19:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58872 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230233AbiH3RSn (ORCPT ); Tue, 30 Aug 2022 13:18:43 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 325FCD5DD2; Tue, 30 Aug 2022 10:18:40 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id C3DFA6172E; Tue, 30 Aug 2022 17:18:39 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B65F0C43140; Tue, 30 Aug 2022 17:18:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1661879919; bh=8WnNtHa6kyBxKAB9Qe4GYjamla7EnOs3bQQ3ef5zjTY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lWLD0a92QaOtqbkIfq23I902Bu3wJWbalp66xuPiDoWgFSBSEBvYHtCjewGndcAwR 35mk45e8F2KlQRD1luIK7zbWQPG1RBoLLZZZe4PCxnVyF+9Qxz1z4nsa82AKAszeag JG7ksiBkiCPs7WsN/Or79R2KpF01iNwhfSZ0SWJSK3uEEVpMVb7yXPYY1272RIUJy3 KQYwb4aiofT0E9CE8n8smXBdgjxzBVP1CaVdHgTbCcj8w8k6o7GcBFQKJjaoVffn8u KsnmeqSoH2vKenlEJS7PBHTi/7dL6Qj4o7VpXzNcqjPgzttgxzV+4qrphsEqRiQBYp K5M4iVHFlkWgQ== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Jeffy Chen , =?UTF-8?q?Christian=20K=C3=B6nig?= , Sasha Levin , maarten.lankhorst@linux.intel.com, mripard@kernel.org, tzimmermann@suse.de, airlied@linux.ie, daniel@ffwll.ch, sumit.semwal@linaro.org, dri-devel@lists.freedesktop.org, linux-media@vger.kernel.org, linaro-mm-sig@lists.linaro.org Subject: [PATCH AUTOSEL 5.19 07/33] drm/gem: Fix GEM handle release errors Date: Tue, 30 Aug 2022 13:17:58 -0400 Message-Id: <20220830171825.580603-7-sashal@kernel.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220830171825.580603-1-sashal@kernel.org> References: <20220830171825.580603-1-sashal@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Jeffy Chen [ Upstream commit ea2aa97ca37a9044ade001aef71dbc06318e8d44 ] Currently we are assuming a one to one mapping between dmabuf and GEM handle when releasing GEM handles. But that is not always true, since we would create extra handles for the GEM obj in cases like gem_open() and getfb{,2}(). A similar issue was reported at: https://lore.kernel.org/all/20211105083308.392156-1-jay.xu@rock-chips.com/ Another problem is that the imported dmabuf might not always have gem_obj->dma_buf set, which would cause leaks in drm_gem_remove_prime_handles(). Let's fix these for now by using handle to find the exact map to remove. Signed-off-by: Jeffy Chen Reviewed-by: Christian König Signed-off-by: Christian König Link: https://patchwork.freedesktop.org/patch/msgid/20220819072834.17888-1-jeffy.chen@rock-chips.com Signed-off-by: Sasha Levin --- drivers/gpu/drm/drm_gem.c | 17 +---------------- drivers/gpu/drm/drm_internal.h | 4 ++-- drivers/gpu/drm/drm_prime.c | 20 ++++++++++++-------- 3 files changed, 15 insertions(+), 26 deletions(-) diff --git a/drivers/gpu/drm/drm_gem.c b/drivers/gpu/drm/drm_gem.c index 86d670c712867..ad068865ba206 100644 --- a/drivers/gpu/drm/drm_gem.c +++ b/drivers/gpu/drm/drm_gem.c @@ -168,21 +168,6 @@ void drm_gem_private_object_init(struct drm_device *dev, } EXPORT_SYMBOL(drm_gem_private_object_init); -static void -drm_gem_remove_prime_handles(struct drm_gem_object *obj, struct drm_file *filp) -{ - /* - * Note: obj->dma_buf can't disappear as long as we still hold a - * handle reference in obj->handle_count. - */ - mutex_lock(&filp->prime.lock); - if (obj->dma_buf) { - drm_prime_remove_buf_handle_locked(&filp->prime, - obj->dma_buf); - } - mutex_unlock(&filp->prime.lock); -} - /** * drm_gem_object_handle_free - release resources bound to userspace handles * @obj: GEM object to clean up. @@ -253,7 +238,7 @@ drm_gem_object_release_handle(int id, void *ptr, void *data) if (obj->funcs->close) obj->funcs->close(obj, file_priv); - drm_gem_remove_prime_handles(obj, file_priv); + drm_prime_remove_buf_handle(&file_priv->prime, id); drm_vma_node_revoke(&obj->vma_node, file_priv); drm_gem_object_handle_put_unlocked(obj); diff --git a/drivers/gpu/drm/drm_internal.h b/drivers/gpu/drm/drm_internal.h index 1fbbc19f1ac09..7bb98e6a446d0 100644 --- a/drivers/gpu/drm/drm_internal.h +++ b/drivers/gpu/drm/drm_internal.h @@ -74,8 +74,8 @@ int drm_prime_fd_to_handle_ioctl(struct drm_device *dev, void *data, void drm_prime_init_file_private(struct drm_prime_file_private *prime_fpriv); void drm_prime_destroy_file_private(struct drm_prime_file_private *prime_fpriv); -void drm_prime_remove_buf_handle_locked(struct drm_prime_file_private *prime_fpriv, - struct dma_buf *dma_buf); +void drm_prime_remove_buf_handle(struct drm_prime_file_private *prime_fpriv, + uint32_t handle); /* drm_drv.c */ struct drm_minor *drm_minor_acquire(unsigned int minor_id); diff --git a/drivers/gpu/drm/drm_prime.c b/drivers/gpu/drm/drm_prime.c index e3f09f18110c7..bd5366b16381b 100644 --- a/drivers/gpu/drm/drm_prime.c +++ b/drivers/gpu/drm/drm_prime.c @@ -190,29 +190,33 @@ static int drm_prime_lookup_buf_handle(struct drm_prime_file_private *prime_fpri return -ENOENT; } -void drm_prime_remove_buf_handle_locked(struct drm_prime_file_private *prime_fpriv, - struct dma_buf *dma_buf) +void drm_prime_remove_buf_handle(struct drm_prime_file_private *prime_fpriv, + uint32_t handle) { struct rb_node *rb; - rb = prime_fpriv->dmabufs.rb_node; + mutex_lock(&prime_fpriv->lock); + + rb = prime_fpriv->handles.rb_node; while (rb) { struct drm_prime_member *member; - member = rb_entry(rb, struct drm_prime_member, dmabuf_rb); - if (member->dma_buf == dma_buf) { + member = rb_entry(rb, struct drm_prime_member, handle_rb); + if (member->handle == handle) { rb_erase(&member->handle_rb, &prime_fpriv->handles); rb_erase(&member->dmabuf_rb, &prime_fpriv->dmabufs); - dma_buf_put(dma_buf); + dma_buf_put(member->dma_buf); kfree(member); - return; - } else if (member->dma_buf < dma_buf) { + break; + } else if (member->handle < handle) { rb = rb->rb_right; } else { rb = rb->rb_left; } } + + mutex_unlock(&prime_fpriv->lock); } void drm_prime_init_file_private(struct drm_prime_file_private *prime_fpriv) -- 2.35.1