From patchwork Thu Oct 1 01:21:49 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: John Stultz X-Patchwork-Id: 313917 Delivered-To: patches@linaro.org Received: by 2002:a05:6e02:1081:0:0:0:0 with SMTP id r1csp109050ilj; Wed, 30 Sep 2020 18:22:01 -0700 (PDT) X-Received: by 2002:a17:902:9a06:b029:d0:cb2d:f276 with SMTP id v6-20020a1709029a06b02900d0cb2df276mr4793989plp.15.1601515321848; Wed, 30 Sep 2020 18:22:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1601515321; cv=none; d=google.com; s=arc-20160816; b=auLk++FWMr9B63KW1nA4EO66jZS2IDiLlRxWziTiGUTc3XWiH+vzZvv9MWYK69r7+K vgzaCFy8gK6KDua/xFW14fBWfo+qW9s7w1ARwumxPaJvToaRH0209jVQwYorgar0xSvw 4onZoZ1P78wAZFPdDABo8O7BkfVpXDxplHSyB5VXoawJL51bZDjYF5uyG6+Z9OOMgwj8 dVAg23iC9/YMmbcm8nlNK1EMTaupXjEeuvMoUfrFArsMvGxmayo2wHzQ7yvZPRfwxVH5 49REAzTllQOkeXw7Uhzb7uNLU29pmVZ0HLM15o3i6r5HKqQbEmOTJx+YPPhJDdcr1pQy MTDw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=1vBTIiPWzM/IlhEEYSWgJfaQ6H5IRqB1EzAsJMQfGDQ=; b=tPbNTfGB/i2O2rYThLDlteS5WerqUHKbxzZuY3rW5WB0g2yQtQgxC/OrjbkQLEW6du g4fsC5oXXuqbwQHwhok/DMzbIhtgeXG8ww4FC0UhApnurjg4osnwO1Zj/FjDlJ72TeAd s3i99Ma0xTVvw0crTMK9JzmrjrQmC1tWHj/A6McX9K4Y9lz4FGOHur6Hfncfetvau2Gd zAQtv+PMa+g2H9BiJwr2edk99C1xuS6cWAGfqiYsjoL3bwb3YOFkYrepUAUvAr79qLHe CSy3GfPQ16a+qOZ+ckspqvLR4Oi7zCaeB01Z3qh9s4t+VSRA4vZiJCRW7UkWuu2ZJCyM /lMg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=QIHtowFH; 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 y18sor1577668pgf.26.2020.09.30.18.22.01 for (Google Transport Security); Wed, 30 Sep 2020 18:22:01 -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=QIHtowFH; 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:in-reply-to:references :mime-version:content-transfer-encoding; bh=1vBTIiPWzM/IlhEEYSWgJfaQ6H5IRqB1EzAsJMQfGDQ=; b=QIHtowFHywcb2OA2JmyEG738gATOnYSXSQWXrzJBOgk78EntjJM/HEjEuj6obwBc2W qqlDCHm6CMMkd6JoL+/QVLamZnFiJnYRQqFQIS5cHm9v6n7lv+tQI7YDizxp79dCkiU7 H5PBDkbuGBWM4qF78HWRZITwwodwN6kL0l0sZjrXvtQhvm+e6fzeAuQ/KKAbWH0VTz+M lhwWcq+rtXVA0YTXKvUeVIoUs4xBYTMulAP6BMB7EU4NFoF+4br5eIqRwWiw9XOvk6Ax nktcGv9OsUyY9xZoA7nq6a6uLRItdRQ7NSwUSmBvYezxvmZz6QnL9L64LOw8U8zBhouW 8nJQ== 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:in-reply-to :references:mime-version:content-transfer-encoding; bh=1vBTIiPWzM/IlhEEYSWgJfaQ6H5IRqB1EzAsJMQfGDQ=; b=KfsYxmaISk0YYa2+27C02ViNtS3aloufhyI44KKHq4iagzY1QYfoApQLCDep+6mreh TCh07MOq8AgHh58jKng0lkgUOPdsKFoDL55ua4d9sbI9OarzTKz9Bj/qGoE/HuAcLMuf H5gJw1PAJDobyNezPhkahb7K4aYmAfGb3tHRxs1ZLy5ulSTYwot+Lr15n7TMglHBgfte e/eh90vmAuQuj62+igIwowXSTi88lzG1hziiN5iUzW5QFr6dUv/3XNmOf7nWNcdb5dmN 6k1nQtyXv4v2N6+x0XOJ+aro8Azz+EDdoNjg9xretP9QXC8TPTA5e0o+l7HoAdl4ybww Q9+g== X-Gm-Message-State: AOAM532SKof66j5l8nqU08wNm7IpneLx24p0/rNJ/7JP40mjvr3GHDwd HNqYCHqcx+KyBxvvgdOv3i/nZb2O X-Google-Smtp-Source: ABdhPJzBHe24b6jfAfo/83c6MDDYvzPWmon/XBZ5L2YBcnRaT9JAl84oGmaEWx3EDUJgl6GJdhmsmg== X-Received: by 2002:a63:4959:: with SMTP id y25mr4275782pgk.206.1601515321478; Wed, 30 Sep 2020 18:22:01 -0700 (PDT) Return-Path: Received: from localhost.localdomain ([2601:1c2:680:1319:692:26ff:feda:3a81]) by smtp.gmail.com with ESMTPSA id s187sm4229372pfc.134.2020.09.30.18.22.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 30 Sep 2020 18:22:00 -0700 (PDT) From: John Stultz To: lkml Cc: John Stultz , Sumit Semwal , Liam Mark , Laura Abbott , Brian Starkey , Hridya Valsaraju , Suren Baghdasaryan , Sandeep Patil , =?utf-8?q?=C3=98rjan_Eide?= , Robin Murphy , Ezequiel Garcia , Simon Ser , James Jones , linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org Subject: [PATCH v2 4/6] dma-buf: system_heap: Allocate higher order pages if available Date: Thu, 1 Oct 2020 01:21:49 +0000 Message-Id: <20201001012151.21149-5-john.stultz@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20201001012151.21149-1-john.stultz@linaro.org> References: <20201001012151.21149-1-john.stultz@linaro.org> MIME-Version: 1.0 While the system heap can return non-contiguous pages, try to allocate larger order pages if possible. This will allow slight performance gains and make implementing page pooling easier. Cc: Sumit Semwal Cc: Liam Mark Cc: Laura Abbott Cc: Brian Starkey Cc: Hridya Valsaraju Cc: Suren Baghdasaryan Cc: Sandeep Patil Cc: Ørjan Eide Cc: Robin Murphy Cc: Ezequiel Garcia Cc: Simon Ser Cc: James Jones Cc: linux-media@vger.kernel.org Cc: dri-devel@lists.freedesktop.org Signed-off-by: John Stultz --- drivers/dma-buf/heaps/system_heap.c | 85 ++++++++++++++++++++++------- 1 file changed, 66 insertions(+), 19 deletions(-) -- 2.17.1 diff --git a/drivers/dma-buf/heaps/system_heap.c b/drivers/dma-buf/heaps/system_heap.c index 7ec58f4e2bd3..d18937fa5b18 100644 --- a/drivers/dma-buf/heaps/system_heap.c +++ b/drivers/dma-buf/heaps/system_heap.c @@ -39,6 +39,14 @@ struct dma_heap_attachment { struct list_head list; }; +#define HIGH_ORDER_GFP (((GFP_HIGHUSER | __GFP_ZERO | __GFP_NOWARN \ + | __GFP_NORETRY) & ~__GFP_RECLAIM) \ + | __GFP_COMP) +#define LOW_ORDER_GFP (GFP_HIGHUSER | __GFP_ZERO | __GFP_COMP) +static gfp_t order_flags[] = {HIGH_ORDER_GFP, LOW_ORDER_GFP, LOW_ORDER_GFP}; +static const unsigned int orders[] = {8, 4, 0}; +#define NUM_ORDERS ARRAY_SIZE(orders) + static struct sg_table *dup_sg_table(struct sg_table *table) { struct sg_table *new_table; @@ -260,8 +268,11 @@ static void system_heap_dma_buf_release(struct dma_buf *dmabuf) int i; table = &buffer->sg_table; - for_each_sgtable_sg(table, sg, i) - __free_page(sg_page(sg)); + for_each_sg(table->sgl, sg, table->nents, i) { + struct page *page = sg_page(sg); + + __free_pages(page, compound_order(page)); + } sg_free_table(table); kfree(buffer); } @@ -279,6 +290,26 @@ static const struct dma_buf_ops system_heap_buf_ops = { .release = system_heap_dma_buf_release, }; +static struct page *alloc_largest_available(unsigned long size, + unsigned int max_order) +{ + struct page *page; + int i; + + for (i = 0; i < NUM_ORDERS; i++) { + if (size < (PAGE_SIZE << orders[i])) + continue; + if (max_order < orders[i]) + continue; + + page = alloc_pages(order_flags[i], orders[i]); + if (!page) + continue; + return page; + } + return NULL; +} + static int system_heap_allocate(struct dma_heap *heap, unsigned long len, unsigned long fd_flags, @@ -286,11 +317,13 @@ static int system_heap_allocate(struct dma_heap *heap, { struct system_heap_buffer *buffer; DEFINE_DMA_BUF_EXPORT_INFO(exp_info); + unsigned long size_remaining = len; + unsigned int max_order = orders[0]; struct dma_buf *dmabuf; struct sg_table *table; struct scatterlist *sg; - pgoff_t pagecount; - pgoff_t pg; + struct list_head pages; + struct page *page, *tmp_page; int i, ret = -ENOMEM; buffer = kzalloc(sizeof(*buffer), GFP_KERNEL); @@ -302,25 +335,35 @@ static int system_heap_allocate(struct dma_heap *heap, buffer->heap = heap; buffer->len = len; - table = &buffer->sg_table; - pagecount = len / PAGE_SIZE; - if (sg_alloc_table(table, pagecount, GFP_KERNEL)) - goto free_buffer; - - sg = table->sgl; - for (pg = 0; pg < pagecount; pg++) { - struct page *page; + INIT_LIST_HEAD(&pages); + i = 0; + while (size_remaining > 0) { /* * Avoid trying to allocate memory if the process * has been killed by SIGKILL */ if (fatal_signal_pending(current)) - goto free_pages; - page = alloc_page(GFP_KERNEL | __GFP_ZERO); + goto free_buffer; + + page = alloc_largest_available(size_remaining, max_order); if (!page) - goto free_pages; - sg_set_page(sg, page, page_size(page), 0); + goto free_buffer; + + list_add_tail(&page->lru, &pages); + size_remaining -= PAGE_SIZE << compound_order(page); + max_order = compound_order(page); + i++; + } + + table = &buffer->sg_table; + if (sg_alloc_table(table, i, GFP_KERNEL)) + goto free_buffer; + + sg = table->sgl; + list_for_each_entry_safe(page, tmp_page, &pages, lru) { + sg_set_page(sg, page, PAGE_SIZE << compound_order(page), 0); sg = sg_next(sg); + list_del(&page->lru); } /* create the dmabuf */ @@ -340,14 +383,18 @@ static int system_heap_allocate(struct dma_heap *heap, /* just return, as put will call release and that will free */ return ret; } - return ret; free_pages: - for_each_sgtable_sg(table, sg, i) - __free_page(sg_page(sg)); + for_each_sgtable_sg(table, sg, i) { + struct page *p = sg_page(sg); + + __free_pages(p, compound_order(p)); + } sg_free_table(table); free_buffer: + list_for_each_entry_safe(page, tmp_page, &pages, lru) + __free_pages(page, compound_order(page)); kfree(buffer); return ret;