Received: by 2002:a25:e74b:0:0:0:0:0 with SMTP id e72csp2664529ybh; Fri, 24 Jul 2020 20:27:25 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyOrrfdt4M71NBZwZrVX7qn0IsXuVuLcPb384A/kb5Ke+u8uUbIKg61w+ul8AbEM9BfskRo X-Received: by 2002:a17:906:488b:: with SMTP id v11mr12333813ejq.173.1595647645353; Fri, 24 Jul 2020 20:27:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1595647645; cv=none; d=google.com; s=arc-20160816; b=VI+NavKum3gUnFyg5+4Bk41HTMCRNOnj3oOdDZhtFP96Hr9s5s1ZbkZKz/7qsFysC6 F8ssdSwrwb238+NhpC1OkWmf318h8m/ZVxCyTahjNMToWY6H+tI6CrzBbx9ccIZBzh5H 0vQgIe7wvsZGeamm9EWZdjy462oVdPJCxBgRh0hu+gFwjtTibyJ4k4TDHAMyk0vPbI0B U97jEC7DUalyNig8ck4SDrVwcvFN3B1K8/z/pafQJgIsHf4G9phL+S2YFEpSg7utEpna YiEzZxj2tk3mygKgVaKU9FiGMrFdeIEcTjDkdWbkRZ0ujdubGjAUx1DMemLzaes7Sk1o 9C3Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature; bh=H0jwgkKJw1F2QGrxp3XRsBO4Zv0vbxAb7H7LFDlABiQ=; b=v/6xnKDgNIiz1o7FE4keYuBjX3ovqC0kkbp0h7an6iVqJheBsg22jsmXur1l7+j2GF xC5SyAO2xybaOi63tNWMG5oOivx+ogUDixeA9psCmAU68uTfRXiSczUO709v7lapTqQy +0QZ8Djb2XBZQgZVpbhmjQ/hUsBNjtU6p9/AGNOq6/jDjbcgvkJvZon3d++Dr6x8ROfK bHtb+MUwUHo+daNOqLnD9YsjJGyKwyTVoRvvxxC9O6KDKX2vCB+0pnn/Q7BCkjSJS0+r yr2vxyYphtyBQJ7Zg5ErXhjDaGI2iKciy7htMgcGW8X5lZVDLoZnl3MpOWp3NSLPOclo ycUQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=RbNTN2vP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id m1si1672263edr.207.2020.07.24.20.27.02; Fri, 24 Jul 2020 20:27:25 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=RbNTN2vP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726837AbgGYD0i (ORCPT + 99 others); Fri, 24 Jul 2020 23:26:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55354 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726613AbgGYD0i (ORCPT ); Fri, 24 Jul 2020 23:26:38 -0400 Received: from mail-pf1-x442.google.com (mail-pf1-x442.google.com [IPv6:2607:f8b0:4864:20::442]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 15203C0619E4 for ; Fri, 24 Jul 2020 20:26:38 -0700 (PDT) Received: by mail-pf1-x442.google.com with SMTP id z3so6277759pfn.12 for ; Fri, 24 Jul 2020 20:26:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id; bh=H0jwgkKJw1F2QGrxp3XRsBO4Zv0vbxAb7H7LFDlABiQ=; b=RbNTN2vPJ+dA/LwMFiC3sJgHNi9P7iDpEf0QPN9vRRqQ7v6rh8ugXQpi5Vouhpb/+x E/SZz2L0MCrCHEVHr9cQu4Fa+J9Oq6lk738yyuyDRw+2Ej5YQOGNQjVEs6rpQ38YxvVY mrkXyY/dpROjus1mzoaCe3ZvJaPBbZsCVRiB3LkavibRHIGwVH5CbMFDNohLUpHSyO1R Hf0jM7DvjDSq3Id5l1M0ElsmQ1zPlI5eziR8fpmSC7io0yxpn4ckZiTj+RBX2EA7SUhy pmLxYj5HhfjFkeeE+7fkuSm24xWP8b4xfqAthKMGLZI9OKgeJEKSaT1uD/uQzBOK3UST A6Tw== 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; bh=H0jwgkKJw1F2QGrxp3XRsBO4Zv0vbxAb7H7LFDlABiQ=; b=HjMc6+t0mkuA9ryhQigN2+sRZG5IU/ir9amK3VC35Db+0ddzSsNnFkiFTc0XiUtJE8 5civnDgS7A7CGGMW0y5N8hMaWiOvREshoGR73/AAmZiH7WZrzGF3jAi/Z1C7Arl7UGxR Qk4QGkhUHOXSUTSEpqJLPjTnx8Rd4lUlmhlnh56JA1kFM+to2Xui2zZ8AX4EiAt5jeZl uVRwINNZ5mtKWCy7mncsyKMMIscTCt/NAqFuBiVANlo21RnlrFXKAZrHulg17NCDV70M 4rRffyNNosF3ig00FZS1WTOT7AfXG0tmLz8P4dnw3w6h7h3O5U4bUPPCXhUDR30HZYih ApJw== X-Gm-Message-State: AOAM531jdahI41mi2EdpCwxfniwmDxyJIYylZhG+OHSuVw6fYY/rD6lb pNfYveznmNBsZJk376YWDJvFpPJeuxE= X-Received: by 2002:a63:fe42:: with SMTP id x2mr11177808pgj.207.1595647597349; Fri, 24 Jul 2020 20:26:37 -0700 (PDT) Received: from localhost.localdomain ([2601:1c2:680:1319:692:26ff:feda:3a81]) by smtp.gmail.com with ESMTPSA id 204sm7754074pfx.3.2020.07.24.20.26.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Jul 2020 20:26:36 -0700 (PDT) From: John Stultz To: lkml Cc: John Stultz , Sumit Semwal , "Andrew F . Davis" , Benjamin Gaignard , Liam Mark , Laura Abbott , Brian Starkey , linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org Subject: [RFC][PATCH] dma-heap: Add proper kref handling on dma-buf heaps Date: Sat, 25 Jul 2020 03:26:33 +0000 Message-Id: <20200725032633.125006-1-john.stultz@linaro.org> X-Mailer: git-send-email 2.17.1 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add proper refcounting on the dma_heap structure. While existing heaps are built-in, we may eventually have heaps loaded from modules, and we'll need to be able to properly handle the references to the heaps Cc: Sumit Semwal Cc: Andrew F. Davis Cc: Benjamin Gaignard Cc: Liam Mark Cc: Laura Abbott Cc: Brian Starkey Cc: linux-media@vger.kernel.org Cc: dri-devel@lists.freedesktop.org Signed-off-by: John Stultz --- drivers/dma-buf/dma-heap.c | 31 +++++++++++++++++++++++++++---- include/linux/dma-heap.h | 6 ++++++ 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/drivers/dma-buf/dma-heap.c b/drivers/dma-buf/dma-heap.c index afd22c9dbdcf..90c3720acc1c 100644 --- a/drivers/dma-buf/dma-heap.c +++ b/drivers/dma-buf/dma-heap.c @@ -40,6 +40,8 @@ struct dma_heap { dev_t heap_devt; struct list_head list; struct cdev heap_cdev; + int minor; + struct kref refcount; }; static LIST_HEAD(heap_list); @@ -190,11 +192,31 @@ void *dma_heap_get_drvdata(struct dma_heap *heap) return heap->priv; } +static void dma_heap_release(struct kref *ref) +{ + struct dma_heap *heap = container_of(ref, struct dma_heap, refcount); + + /* Remove heap from the list */ + mutex_lock(&heap_list_lock); + list_del(&heap->list); + mutex_unlock(&heap_list_lock); + + device_destroy(dma_heap_class, heap->heap_devt); + cdev_del(&heap->heap_cdev); + xa_erase(&dma_heap_minors, heap->minor); + + kfree(heap); +} + +void dma_heap_put(struct dma_heap *h) +{ + kref_put(&h->refcount, dma_heap_release); +} + struct dma_heap *dma_heap_add(const struct dma_heap_export_info *exp_info) { struct dma_heap *heap, *h, *err_ret; struct device *dev_ret; - unsigned int minor; int ret; if (!exp_info->name || !strcmp(exp_info->name, "")) { @@ -223,12 +245,13 @@ struct dma_heap *dma_heap_add(const struct dma_heap_export_info *exp_info) if (!heap) return ERR_PTR(-ENOMEM); + kref_init(&heap->refcount); heap->name = exp_info->name; heap->ops = exp_info->ops; heap->priv = exp_info->priv; /* Find unused minor number */ - ret = xa_alloc(&dma_heap_minors, &minor, heap, + ret = xa_alloc(&dma_heap_minors, &heap->minor, heap, XA_LIMIT(0, NUM_HEAP_MINORS - 1), GFP_KERNEL); if (ret < 0) { pr_err("dma_heap: Unable to get minor number for heap\n"); @@ -237,7 +260,7 @@ struct dma_heap *dma_heap_add(const struct dma_heap_export_info *exp_info) } /* Create device */ - heap->heap_devt = MKDEV(MAJOR(dma_heap_devt), minor); + heap->heap_devt = MKDEV(MAJOR(dma_heap_devt), heap->minor); cdev_init(&heap->heap_cdev, &dma_heap_fops); ret = cdev_add(&heap->heap_cdev, heap->heap_devt, 1); @@ -267,7 +290,7 @@ struct dma_heap *dma_heap_add(const struct dma_heap_export_info *exp_info) err2: cdev_del(&heap->heap_cdev); err1: - xa_erase(&dma_heap_minors, minor); + xa_erase(&dma_heap_minors, heap->minor); err0: kfree(heap); return err_ret; diff --git a/include/linux/dma-heap.h b/include/linux/dma-heap.h index 454e354d1ffb..c1572f29cfac 100644 --- a/include/linux/dma-heap.h +++ b/include/linux/dma-heap.h @@ -56,4 +56,10 @@ void *dma_heap_get_drvdata(struct dma_heap *heap); */ struct dma_heap *dma_heap_add(const struct dma_heap_export_info *exp_info); +/** + * dma_heap_put - drops a reference to a dmabuf heaps, potentially freeing it + * @heap: heap pointer + */ +void dma_heap_put(struct dma_heap *heap); + #endif /* _DMA_HEAPS_H */ -- 2.17.1