From patchwork Thu Jun 24 17:47:32 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jason Ekstrand X-Patchwork-Id: 12342699 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.5 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5CF6EC49EA6 for ; Thu, 24 Jun 2021 17:47:45 +0000 (UTC) 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 mail.kernel.org (Postfix) with ESMTPS id 04DC3613C7 for ; Thu, 24 Jun 2021 17:47:44 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 04DC3613C7 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=jlekstrand.net Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=dri-devel-bounces@lists.freedesktop.org Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id AF43D6EC75; Thu, 24 Jun 2021 17:47:42 +0000 (UTC) Received: from mail-pg1-x536.google.com (mail-pg1-x536.google.com [IPv6:2607:f8b0:4864:20::536]) by gabe.freedesktop.org (Postfix) with ESMTPS id 5F5086EC73 for ; Thu, 24 Jun 2021 17:47:41 +0000 (UTC) Received: by mail-pg1-x536.google.com with SMTP id e33so5354371pgm.3 for ; Thu, 24 Jun 2021 10:47:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jlekstrand-net.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=Tz/DuF/FND0AtzBbmjaoWlKt/aPhPThKNTwr9Xn43ZY=; b=mUdywTQcwlGKOE7OWwYQdxjXsDaW66xFphJXbcZaHPQWM1XW1H9ZUkhuDs0FPkY/9Y UEcPpnRE4mIjEccZBgw9PG7DESEF1A/rxrd0sWdpGos5pb0O2am1Q5QOH6SP7bubXQDf eY0m3QA3laJ7RO33+PhUpbriBN0v/ETCeccUX+zQvJHN5GzU0yqP2+IhM+BgXkqXQoOI h/Ke5l5G3NY8ir8F48Dp+E0i0nlgf4VZRkud9pMwTUAwPVvNv6SSCk+lWaPQPNvcGAC/ yNT1IcHaebZHyiA72yPR8Jte9dmAtTjtbVQO4+AUJyaAnx9xhfWYc7YrldYbRJNG4iI7 FOOw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=Tz/DuF/FND0AtzBbmjaoWlKt/aPhPThKNTwr9Xn43ZY=; b=jzcJB3eLR5ZvCwcBrnAyAzwRxAFGWB1LJvsewBWJV8LXh/24VNXWOxEKm0mAEZraae Ezy8e0blmSPNhWylGtJ8JMw0LKV6rohZQVIKI3zZFDhCy1bfMd/A4GnRac/zOhW29JjE HcXp/LIjEt87vbpfu1JBHAAeAmVGFm+WF5vNbYQZhYHHR0bF4Y/EzlptalKI7uOg0wli Gzkz55RFMKRh4Avvsr3SJl4xsgniMkMkJ3m1cAb9UiMd2Wq87jz+T+54EqBi6Uxj9E/A v33pO+tgjy9H7sXdn7+ElZBmAs9Qgk6VmgBReRaTb8Zf7fMfENdMhLRcLE7uQy4tmkav xRhA== X-Gm-Message-State: AOAM531EQuvul1/xRD7AGuVcSzfhuY/K+esVyMf78hgMKxXb2MIto0yk 2v0Qhb+HTWqp9ATkMdc9EMuOqKODRiqBqQ== X-Google-Smtp-Source: ABdhPJzAJUXKOu/Owpa4Ppem0EN3JzZS9ocke4+a3A0aamByRKXT2Z/LKeWyDmXuPfBVTGE+1efu+g== X-Received: by 2002:a65:49c4:: with SMTP id t4mr5784474pgs.108.1624556860603; Thu, 24 Jun 2021 10:47:40 -0700 (PDT) Received: from omlet.lan (jfdmzpr04-ext.jf.intel.com. [134.134.137.73]) by smtp.gmail.com with ESMTPSA id y1sm3013280pgr.70.2021.06.24.10.47.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Jun 2021 10:47:40 -0700 (PDT) From: Jason Ekstrand To: dri-devel@lists.freedesktop.org, intel-gfx@lists.freedesktop.org Subject: [PATCH] dma-buf/sync_file: Don't leak fences on merge failure Date: Thu, 24 Jun 2021 12:47:32 -0500 Message-Id: <20210624174732.1754546-1-jason@jlekstrand.net> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Gustavo Padovan , =?utf-8?q?Christian_K?= =?utf-8?q?=C3=B6nig?= , Jason Ekstrand Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" Each add_fence() call does a dma_fence_get() on the relevant fence. In the error path, we weren't calling dma_fence_put() so all those fences got leaked. Also, in the krealloc_array failure case, we weren't freeing the fences array. Instead, ensure that i and fences are always zero-initialized and dma_fence_put() all the fences and kfree(fences) on every error path. Signed-off-by: Jason Ekstrand Fixes: a02b9dc90d84 ("dma-buf/sync_file: refactor fence storage in struct sync_file") Cc: Gustavo Padovan Cc: Christian König Reviewed-by: Christian König --- drivers/dma-buf/sync_file.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/drivers/dma-buf/sync_file.c b/drivers/dma-buf/sync_file.c index 20d9bddbb985b..394e6e1e96860 100644 --- a/drivers/dma-buf/sync_file.c +++ b/drivers/dma-buf/sync_file.c @@ -211,8 +211,8 @@ static struct sync_file *sync_file_merge(const char *name, struct sync_file *a, struct sync_file *b) { struct sync_file *sync_file; - struct dma_fence **fences, **nfences, **a_fences, **b_fences; - int i, i_a, i_b, num_fences, a_num_fences, b_num_fences; + struct dma_fence **fences = NULL, **nfences, **a_fences, **b_fences; + int i = 0, i_a, i_b, num_fences, a_num_fences, b_num_fences; sync_file = sync_file_alloc(); if (!sync_file) @@ -236,7 +236,7 @@ static struct sync_file *sync_file_merge(const char *name, struct sync_file *a, * If a sync_file can only be created with sync_file_merge * and sync_file_create, this is a reasonable assumption. */ - for (i = i_a = i_b = 0; i_a < a_num_fences && i_b < b_num_fences; ) { + for (i_a = i_b = 0; i_a < a_num_fences && i_b < b_num_fences; ) { struct dma_fence *pt_a = a_fences[i_a]; struct dma_fence *pt_b = b_fences[i_b]; @@ -277,15 +277,16 @@ static struct sync_file *sync_file_merge(const char *name, struct sync_file *a, fences = nfences; } - if (sync_file_set_fence(sync_file, fences, i) < 0) { - kfree(fences); + if (sync_file_set_fence(sync_file, fences, i) < 0) goto err; - } strlcpy(sync_file->user_name, name, sizeof(sync_file->user_name)); return sync_file; err: + while (i) + dma_fence_put(fences[--i]); + kfree(fences); fput(sync_file->file); return NULL;