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: 12342701 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 09C6FC49EA5 for ; Thu, 24 Jun 2021 17:47:43 +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 92EC4613C7 for ; Thu, 24 Jun 2021 17:47:42 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 92EC4613C7 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=jlekstrand.net Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=intel-gfx-bounces@lists.freedesktop.org Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 3FACE6EC73; Thu, 24 Jun 2021 17:47:42 +0000 (UTC) Received: from mail-pg1-x52b.google.com (mail-pg1-x52b.google.com [IPv6:2607:f8b0:4864:20::52b]) by gabe.freedesktop.org (Postfix) with ESMTPS id 385656EC73 for ; Thu, 24 Jun 2021 17:47:41 +0000 (UTC) Received: by mail-pg1-x52b.google.com with SMTP id e22so5339242pgv.10 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=p6XNbx+art489mvczr1XtwE9m86OSCbu2+/STU4vw9R3tfoe7IqeVZu7nT1MO9trlq x0mnhwm6pSn3wKaPbH9Ki4vinR1nWo17X1qYPahIu3d3aRKbyD18V5XWN59kXZzz2q0y K2hNmryrJV4s8RBxntxVFByhniUserJGriZonCYdQE/u4ocXsi1R/K8vbCC7R0L9kdK4 pIe2wExBaXbVsl3s+yXYCYynTjsY35nyV1VArRmqwGLU3SdnugKGiSb8JnXLCkOudrKq eD3C7bfWtHJnXlqHNBwqMJ6kXU+z7loNAILo/dzkmIMcaLFqVKWU+wjAiqtAwiMOF6+y W1Xw== X-Gm-Message-State: AOAM532lbHZAf4+/Kx1km4clxe8LH54uBuSMZdoxDt6RDIiwxOTKuHrM WjdkR7MarSX6KjGKDCwRNsr+yg== 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 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 Subject: [Intel-gfx] [PATCH] dma-buf/sync_file: Don't leak fences on merge failure X-BeenThere: intel-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Intel graphics driver community testing & development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Gustavo Padovan , =?utf-8?q?Christian_K?= =?utf-8?q?=C3=B6nig?= Errors-To: intel-gfx-bounces@lists.freedesktop.org Sender: "Intel-gfx" 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;