From patchwork Tue May 5 19:58:54 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stultz X-Patchwork-Id: 186213 Delivered-To: patches@linaro.org Received: by 2002:a92:8d81:0:0:0:0:0 with SMTP id w1csp1458621ill; Tue, 5 May 2020 12:59:08 -0700 (PDT) X-Received: by 2002:a65:62c7:: with SMTP id m7mr4036490pgv.16.1588708748253; Tue, 05 May 2020 12:59:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588708748; cv=none; d=google.com; s=arc-20160816; b=0LaF9JLTv98Gdwm4H70md535N9tWFtINa/kFJdm4HQEVwCfwLEje2c+2hJWWYzCEJU ukQRh5/vf/MU8ZCKyKOPoBQE+i8IMDZPP23Yq1p91FWAGfClAWNZyGEhrW6JfcL8jLrE WTINwhfziIkNr0AjeXpZ89bKj2If1gXHrKXl+Im+tjMIiyfiZR0WcVllGWPBm6XnFCbZ nnCtwyn7ZNJgI/Ona63jt895ntjRIwqrXQKvveC5RMXaV7KOvDFnzEwkumXp9wxSeIrz QcPRCglvMPz8nSkw4fMxd87H02y4rWgfU5GwiOMg9JOTyEB6UHpa6Slaesj/6oQrY5qo BKTQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=message-id:date:subject:cc:to:from:dkim-signature; bh=f1+hms/HzM6XhVGpCUGj/whrgwNh6rfdziobohzGjU0=; b=EbLKlCWHtDvOHL06W6n97vwstztinl49nonJahr+MBj2Ppnmr6OK0ohDjDmbToxSos VSlAz0lrVsffnR7uS2G4/BX2UtZjJo0MOocMr+Q6PJDT46/NEbW3Klvg7rO8/kUCB7sv YOYGs3uQk7yKkVPmGxS06HOYYi9sKYban2R5nSWak9cI2NmXNuuvhd2NJ0yq4YBVt3FV OFcC2oHdFzyHRT++mJizsmC7hLRa7iEBGXXwlYtBdhMcCKqt0HFGxxK0ghHX5Jkp54o1 irWpADjGvg7rXWNrshK6cVAD6mXIHWPN+Iyfoig/56I5ITlbkx8Bt0aTFBdDlruvIXZV UsMg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=j5icav5E; spf=pass (google.com: domain of john.stultz@linaro.org designates 209.85.220.65 as permitted sender) smtp.mailfrom=john.stultz@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id w23sor2854645pfi.49.2020.05.05.12.59.08 for (Google Transport Security); Tue, 05 May 2020 12:59:08 -0700 (PDT) Received-SPF: pass (google.com: domain of john.stultz@linaro.org designates 209.85.220.65 as permitted sender) client-ip=209.85.220.65; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=j5icav5E; spf=pass (google.com: domain of john.stultz@linaro.org designates 209.85.220.65 as permitted sender) smtp.mailfrom=john.stultz@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id; bh=f1+hms/HzM6XhVGpCUGj/whrgwNh6rfdziobohzGjU0=; b=j5icav5EDStMOR/2B21rulvYuqmWnmvmIeOriztRfGhaPPUGNpO8dlZp160uR706ZD CN2QM5AhYuYJeuug7vVImTiEEiTMGsbPeeJ8eF7fqxtEhv5IKislP2NiZR0wnIyEP+6f rK6eFlxeOHWZ8uyyHoholRW5OG86eulBa4N8KAS0LFWQ4v5HnXcKlo09ickPrFdKv9Fl SVQwRB5sUrWTArMbmlzeqabNtZtM6Alu25x09aKKycamTKKAJZCpkZssyCHTVYayY6qm Gk11BZ+OM61c5XDtH02v30t9c4Dk2oncvar+SlxiEamjVSxqZjTVrvBfWXpygYfsB9DR 8uAg== 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=f1+hms/HzM6XhVGpCUGj/whrgwNh6rfdziobohzGjU0=; b=hluNK/AUI3ZMc5OuNj8bldE/SiHr0My+Az1fQSJ6GOAPFIHI+qbNQj8dI/aOHc9JpR IgeLtqfQ8w3xmkRBdpNmVX0xyt6v9pTouLbijtBykvT4A0epZlAsuHLVKVVpNjHiHmO+ 9V/GVsQ5b5lFGCpdLzVi2vvFikQQ7q5clF4OXprmSUqi9tQ/0JyTJIClDB75eVQlzvdQ KctGcP19IrxVy/Pz+Zh+gL+gX/V9Ob6OjPEAJFpj4SbhtLjAS73AyIB7e0fb19H0SlAC GulcSqtPjFliEABehnuxXXtdoQdbyavFWni+/1pzncrAWMlIljhP3L8+GOt76FdE0SKf scIA== X-Gm-Message-State: AGi0PuaIeHAjHBG9NqPnMX4WnD16WKR/twsNj2pkxN11bT+aHxNNgjyV XB/t4Ndu9xFXeykyOFnRGZIId4qR X-Google-Smtp-Source: APiQypJTkn2fb2KSXsJtRyKxtCBL8YFjFle7PrivbJtU3P7dZajSC4xcII15Rx0VXANN2wYvfl7zlw== X-Received: by 2002:a62:888a:: with SMTP id l132mr594480pfd.125.1588708747776; Tue, 05 May 2020 12:59:07 -0700 (PDT) Return-Path: Received: from localhost.localdomain ([2601:1c2:680:1319:692:26ff:feda:3a81]) by smtp.gmail.com with ESMTPSA id c1sm2559317pfo.152.2020.05.05.12.59.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 May 2020 12:59:07 -0700 (PDT) From: John Stultz To: "Andrew F. Davis" , Sumit Semwal Cc: John Stultz Subject: [PATCH 1/2] dma-heap: Refactor code to allow for future in-kernel users Date: Tue, 5 May 2020 19:58:54 +0000 Message-Id: <20200505195855.94934-1-john.stultz@linaro.org> X-Mailer: git-send-email 2.17.1 Really early RFC on this. Signed-off-by: John Stultz --- drivers/dma-buf/dma-heap.c | 42 +++++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 16 deletions(-) -- 2.17.1 diff --git a/drivers/dma-buf/dma-heap.c b/drivers/dma-buf/dma-heap.c index afd22c9dbdcf..de09a0626a61 100644 --- a/drivers/dma-buf/dma-heap.c +++ b/drivers/dma-buf/dma-heap.c @@ -48,10 +48,31 @@ static dev_t dma_heap_devt; static struct class *dma_heap_class; static DEFINE_XARRAY_ALLOC(dma_heap_minors); +static struct dma_heap *dma_heap_find(const char* name) +{ + struct dma_heap *h; + + /* check the name is unique */ + mutex_lock(&heap_list_lock); + list_for_each_entry(h, &heap_list, list) { + if (!strcmp(h->name, name)) { + mutex_unlock(&heap_list_lock); + return h; + } + } + mutex_unlock(&heap_list_lock); + return NULL; +} + static int dma_heap_buffer_alloc(struct dma_heap *heap, size_t len, unsigned int fd_flags, unsigned int heap_flags) { + if (fd_flags & ~DMA_HEAP_VALID_FD_FLAGS) + return -EINVAL; + + if (heap_flags & ~DMA_HEAP_VALID_HEAP_FLAGS) + return -EINVAL; /* * Allocations from all heaps have to begin * and end on page boundaries. @@ -89,12 +110,6 @@ static long dma_heap_ioctl_allocate(struct file *file, void *data) if (heap_allocation->fd) return -EINVAL; - if (heap_allocation->fd_flags & ~DMA_HEAP_VALID_FD_FLAGS) - return -EINVAL; - - if (heap_allocation->heap_flags & ~DMA_HEAP_VALID_HEAP_FLAGS) - return -EINVAL; - fd = dma_heap_buffer_alloc(heap, heap_allocation->len, heap_allocation->fd_flags, heap_allocation->heap_flags); @@ -192,7 +207,7 @@ void *dma_heap_get_drvdata(struct dma_heap *heap) struct dma_heap *dma_heap_add(const struct dma_heap_export_info *exp_info) { - struct dma_heap *heap, *h, *err_ret; + struct dma_heap *heap, *err_ret; struct device *dev_ret; unsigned int minor; int ret; @@ -208,16 +223,11 @@ struct dma_heap *dma_heap_add(const struct dma_heap_export_info *exp_info) } /* check the name is unique */ - mutex_lock(&heap_list_lock); - list_for_each_entry(h, &heap_list, list) { - if (!strcmp(h->name, exp_info->name)) { - mutex_unlock(&heap_list_lock); - pr_err("dma_heap: Already registered heap named %s\n", - exp_info->name); - return ERR_PTR(-EINVAL); - } + if (dma_heap_find(exp_info->name)) { + pr_err("dma_heap: Already registered heap named %s\n", + exp_info->name); + return ERR_PTR(-EINVAL); } - mutex_unlock(&heap_list_lock); heap = kzalloc(sizeof(*heap), GFP_KERNEL); if (!heap)