From patchwork Tue Aug 6 15:01:28 2019 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: 11085983 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 7C64A1395 for ; Fri, 9 Aug 2019 09:21:37 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6A7BB28961 for ; Fri, 9 Aug 2019 09:21:37 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5EB5928C47; Fri, 9 Aug 2019 09:21:37 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.2 required=2.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED autolearn=unavailable version=3.3.1 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 0C63128961 for ; Fri, 9 Aug 2019 09:21:37 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 0268A6EDE0; Fri, 9 Aug 2019 09:21:35 +0000 (UTC) X-Original-To: intel-gfx@lists.freedesktop.org Delivered-To: intel-gfx@lists.freedesktop.org Received: from mail-wr1-x443.google.com (mail-wr1-x443.google.com [IPv6:2a00:1450:4864:20::443]) by gabe.freedesktop.org (Postfix) with ESMTPS id D847A6E3AC; Tue, 6 Aug 2019 15:01:38 +0000 (UTC) Received: by mail-wr1-x443.google.com with SMTP id n9so63166191wrr.4; Tue, 06 Aug 2019 08:01:38 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=iygOewm8tSkT172r4bWrbjM8Y7R1HsBfIZtC5P+jXJs=; b=ICaLxMHzv7g8wFpRD8Pxxp2jwwFHe5fv2j1AvDa823dkqphNBNE+qmRx4WXa8lLs2x l3CXB/2gyIO1zE6L6tyhTSCgJUlaLA3EXFb1ZwnJeCnZ+Q1bNUnAqKEfhQFJfKnj6n+a dufaT75vYDMwa48vZt19rtpdETzDKd2k/aIHlk56dKGCReu/UaYZE766O4c3hq8FdmEI h4Hc8UX6g0EDy/tjrLhZEt34ZyWzKOraSfZvsgnMlBlu/HUdtsn3tjq3oGQnDY5DMCfs x8p8gOA2pMPUKCLlXtfiyKJOoIiqlY+AJp6/E2ITUF84w3RAVCbfg7ipq3FrWC7gqaMX h0rA== X-Gm-Message-State: APjAAAWYs49rFcd1GNk7+ET1aaGN6kje3vuUhkvO9Q5xBdg5/G3UGEo0 5RZQsYrwRF2BhUhz3joMSWf4zlGM X-Google-Smtp-Source: APXvYqzy2Kgz+9jPZPS+CzJD1Vr2OThygCavWLHFRvHw8JOOmHMQXdC5hdEXm8RgUR8n2nXfs2/8pQ== X-Received: by 2002:adf:a344:: with SMTP id d4mr5285047wrb.237.1565103697141; Tue, 06 Aug 2019 08:01:37 -0700 (PDT) Received: from abel.fritz.box ([2a02:908:1252:fb60:d1e0:fffc:b0e3:c66d]) by smtp.gmail.com with ESMTPSA id g2sm78766828wmh.0.2019.08.06.08.01.36 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 06 Aug 2019 08:01:36 -0700 (PDT) From: " =?utf-8?q?Christian_K=C3=B6nig?= " X-Google-Original-From: =?utf-8?q?Christian_K=C3=B6nig?= To: intel-gfx@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, dri-devel@lists.freedesktop.org, chris@chris-wilson.co.uk Date: Tue, 6 Aug 2019 17:01:28 +0200 Message-Id: <20190806150134.104222-2-christian.koenig@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190806150134.104222-1-christian.koenig@amd.com> References: <20190806150134.104222-1-christian.koenig@amd.com> MIME-Version: 1.0 X-Mailman-Approved-At: Fri, 09 Aug 2019 09:15:44 +0000 X-Mailman-Original-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=iygOewm8tSkT172r4bWrbjM8Y7R1HsBfIZtC5P+jXJs=; b=D3TV3+KBThN+SxHpvUbrEo0rLzptGNEz/ZTVHUI5FC0EIYgTvaubUxWpQNGpWQGSVP CYrd2NoH8VoOcX+JpS9zgo0kO2CruEW2cMJcSzKohj2dIzGKGZjw37YvoQYGNWF2bSOW o+Znb73FC3/TxelbyHTQF7SIxBKO8H4FhfavE/HNLXhytFmCeXIjZM5zgWZ7yg3jjIbY fUf8aNKeM7j5mXmU0qiR3ku/upYqOxm0OqRcalfTDd0GFsShIpYxDyYOZNE3KiXfmp43 OirjW64GtkfzXBKBlM2NofKsNIQ4fHYmt+7x23S+QLgmI39Ig4dysOAu+OK7q6FBMiKC uGyQ== Subject: [Intel-gfx] [PATCH 2/8] dma-buf: fix shared fence list handling in reservation_object_copy_fences X-BeenThere: intel-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Intel graphics driver community testing & development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: intel-gfx-bounces@lists.freedesktop.org Sender: "Intel-gfx" X-Virus-Scanned: ClamAV using ClamSMTP Add some helpers to correctly allocate/free reservation_object_lists. Otherwise we might forget to drop dma_fence references on list destruction. Signed-off-by: Christian König --- drivers/dma-buf/reservation.c | 65 +++++++++++++++++++++++++---------- 1 file changed, 46 insertions(+), 19 deletions(-) diff --git a/drivers/dma-buf/reservation.c b/drivers/dma-buf/reservation.c index d59207ca72d2..c0ba05936ab6 100644 --- a/drivers/dma-buf/reservation.c +++ b/drivers/dma-buf/reservation.c @@ -55,6 +55,47 @@ EXPORT_SYMBOL(reservation_seqcount_class); const char reservation_seqcount_string[] = "reservation_seqcount"; EXPORT_SYMBOL(reservation_seqcount_string); +/** + * reservation_object_list_alloc - allocate fence list + * @shared_max: number of fences we need space for + * + * Allocate a new reservation_object_list and make sure to correctly initialize + * shared_max. + */ +static struct reservation_object_list * +reservation_object_list_alloc(unsigned int shared_max) +{ + struct reservation_object_list *list; + + list = kmalloc(offsetof(typeof(*list), shared[shared_max]), GFP_KERNEL); + if (!list) + return NULL; + + list->shared_max = (ksize(list) - offsetof(typeof(*list), shared)) / + sizeof(*list->shared); + + return list; +} + +/** + * reservation_object_list_free - free fence list + * @list: list to free + * + * Free a reservation_object_list and make sure to drop all references. + */ +static void reservation_object_list_free(struct reservation_object_list *list) +{ + unsigned int i; + + if (!list) + return; + + for (i = 0; i < list->shared_count; ++i) + dma_fence_put(rcu_dereference_protected(list->shared[i], true)); + + kfree_rcu(list, rcu); +} + /** * reservation_object_init - initialize a reservation object * @obj: the reservation object @@ -76,7 +117,6 @@ EXPORT_SYMBOL(reservation_object_init); */ void reservation_object_fini(struct reservation_object *obj) { - int i; struct reservation_object_list *fobj; struct dma_fence *excl; @@ -89,13 +129,7 @@ void reservation_object_fini(struct reservation_object *obj) dma_fence_put(excl); fobj = rcu_dereference_protected(obj->fence, 1); - if (fobj) { - for (i = 0; i < fobj->shared_count; ++i) - dma_fence_put(rcu_dereference_protected(fobj->shared[i], 1)); - - kfree(fobj); - } - + reservation_object_list_free(fobj); ww_mutex_destroy(&obj->lock); } EXPORT_SYMBOL(reservation_object_fini); @@ -132,7 +166,7 @@ int reservation_object_reserve_shared(struct reservation_object *obj, max = 4; } - new = kmalloc(offsetof(typeof(*new), shared[max]), GFP_KERNEL); + new = reservation_object_list_alloc(max); if (!new) return -ENOMEM; @@ -153,9 +187,6 @@ int reservation_object_reserve_shared(struct reservation_object *obj, RCU_INIT_POINTER(new->shared[j++], fence); } new->shared_count = j; - new->shared_max = - (ksize(new) - offsetof(typeof(*new), shared)) / - sizeof(*new->shared); /* * We are not changing the effective set of fences here so can @@ -286,7 +317,6 @@ int reservation_object_copy_fences(struct reservation_object *dst, { struct reservation_object_list *src_list, *dst_list; struct dma_fence *old, *new; - size_t size; unsigned i; reservation_object_assert_held(dst); @@ -298,10 +328,9 @@ int reservation_object_copy_fences(struct reservation_object *dst, if (src_list) { unsigned shared_count = src_list->shared_count; - size = offsetof(typeof(*src_list), shared[shared_count]); rcu_read_unlock(); - dst_list = kmalloc(size, GFP_KERNEL); + dst_list = reservation_object_list_alloc(shared_count); if (!dst_list) return -ENOMEM; @@ -313,7 +342,6 @@ int reservation_object_copy_fences(struct reservation_object *dst, } dst_list->shared_count = 0; - dst_list->shared_max = shared_count; for (i = 0; i < src_list->shared_count; ++i) { struct dma_fence *fence; @@ -323,7 +351,7 @@ int reservation_object_copy_fences(struct reservation_object *dst, continue; if (!dma_fence_get_rcu(fence)) { - kfree(dst_list); + reservation_object_list_free(dst_list); src_list = rcu_dereference(src->fence); goto retry; } @@ -353,8 +381,7 @@ int reservation_object_copy_fences(struct reservation_object *dst, write_seqcount_end(&dst->seq); preempt_enable(); - if (src_list) - kfree_rcu(src_list, rcu); + reservation_object_list_free(src_list); dma_fence_put(old); return 0;