From patchwork Tue Nov 10 03:49:32 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: John Stultz X-Patchwork-Id: 321004 Delivered-To: patches@linaro.org Received: by 2002:a92:7b12:0:0:0:0:0 with SMTP id w18csp2273783ilc; Mon, 9 Nov 2020 19:49:45 -0800 (PST) X-Received: by 2002:a17:90b:316:: with SMTP id ay22mr2863804pjb.8.1604980185561; Mon, 09 Nov 2020 19:49:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1604980185; cv=none; d=google.com; s=arc-20160816; b=QEVB2GBbUs+6kujc4Nx8McZdzeJ6geye1L0NkkfH5zOHDQg6rNjOypQovpDXeths2b pYdl99I6jgQIKqgRsF/qfM9L3NiEIO/t/zheG2P71ItfF8e8Un/pQLH6soSIosLOkkkK obeVpgM1TPzdFhx+djkoOnTWge6VSOyedLkNxFZknznpm5D4Fz4fFQaVDK6H+WvqchaO x0wyNJzl8InqGNUKITd2cq6EB5D1IsvaTMt3JEMV9m3MKOcuBqu8PfHcx5UoLxWHbqN6 ZYEMI1N33ZiAsFgDfW3/ekLIB/hw69BX396OLdNMZYFpgeEaB3g5s2IugO5CygpNlhNy rn5A== 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=nNmqdzWYoDvSeHb8QCenr/dmp78MUbTXse3pLbyKbEo=; b=W25d2A+TYn4gA0I1VZ2EM0MQ1VEFDIyI2A3nW5UIoRX/g2Y6gew0fOWOVuuNKQgVpY ILSVoTI+XSUcLH5gUgDvf7y75jp70FkRTy8D4K6eNGzU8b4aCLpN3eYxot5VclRYSPvh gp1VsGobLruzOWb+JZd4RZhKmabGiQqU8s6Lfea/uFiJTEloLDSCuDG3DYzZ/YSca7C1 hKEyvbM//OqrFnEq5v7zg8SS88XchEolKY60qpWCiY+Wv9GdFQA8vaB9o1C4TqtrrTpE JoESU0dQgNf6xqfnsylKkf9qulhKXEwLLAxE23PKZs+0Le/U6cZIGIki6K62YYtH8ONf lpUw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=jIqzNEC1; 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 s1sor1091507pjq.0.2020.11.09.19.49.45 for (Google Transport Security); Mon, 09 Nov 2020 19:49:45 -0800 (PST) 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=jIqzNEC1; 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=nNmqdzWYoDvSeHb8QCenr/dmp78MUbTXse3pLbyKbEo=; b=jIqzNEC11+sopjAKQET00KYR1iTaKU9/kPZSwUeDTZHaLjz0H5aA77Mh/w6mguH+OU Wor6jU+8uvYMQnAsZTcaAndJ8vHX7MgHHzjbKHYxprOmSSEjATS2Ve1UVZQ3hj4uDU4p dU7n7yntdKoCdeHIoHyE6v+oJqYDzKbGZjqU1TQDGKNAoKZ3WMlA8xwFtlHzGpO2ry69 hZnuotZKZZorm+gcgm5+ftl6G6vmWj6mLcUTN+UI7lgISmSI2y0Wszpbk+kTLlNNJ6/+ u0LUItRXy0p8QuvkUmHxQ2e4J6NRwZXMJKk8QTb0ggOY7P/DvTB0vBYBVnH42zOfFbsF ZFpA== 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=nNmqdzWYoDvSeHb8QCenr/dmp78MUbTXse3pLbyKbEo=; b=UPS9tgcBi/Svi2YHSH5DJoNFaYpRrM/dYzo+ID5DBl4G+YZHwFgxG4K2NWyvMZNuA7 61WIhGMl0RwvJhTKp6jIySx9J+0/KZcurbjVkGKaP5qzofykd+0Juwg6bMPZNr/NJLrY Praly+2cjB44KN6lMFFUcAD0VkhZDrZq485an9NQYl0ZwVTyvNAqY9hOXpD99ZFRmq0X YmPxWHB4tD24bKfbteDtO5gqsYAjj/JRFRSo0BWK//LULxPWOiTTf01OBCaglTPU2Ksx t0tcP6XW2/iK5LFj3V4xGVBqH5qR/iqaKEeMiWbSIcF9fNH3r2DRDXYALiFAHzEirgXv tDfw== X-Gm-Message-State: AOAM532fufAbvfq3A3/U8P4Rx4mQnH0cvCk7wR4Kw6NOVo8c2mtytRgF YQlSvcYQ6iveUjav0yHBtVxRFJ3+ X-Google-Smtp-Source: ABdhPJx5GNFfSduF3OA1PRsG1wKaRHVkKkJW+m9bd5i+0UUC0FlKv72ozwWbAk/HTlTE7XGuu2QVpw== X-Received: by 2002:a17:90b:4383:: with SMTP id in3mr2705741pjb.112.1604980185304; Mon, 09 Nov 2020 19:49:45 -0800 (PST) Return-Path: Received: from localhost.localdomain ([2601:1c2:680:1319:692:26ff:feda:3a81]) by smtp.gmail.com with ESMTPSA id b4sm12380693pfi.208.2020.11.09.19.49.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Nov 2020 19:49:44 -0800 (PST) From: John Stultz To: lkml Cc: John Stultz , Sumit Semwal , Liam Mark , Laura Abbott , Brian Starkey , Hridya Valsaraju , Suren Baghdasaryan , Sandeep Patil , Daniel Mentz , Chris Goldsworthy , =?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 v5 5/7] dma-buf: system_heap: Allocate higher order pages if available Date: Tue, 10 Nov 2020 03:49:32 +0000 Message-Id: <20201110034934.70898-6-john.stultz@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20201110034934.70898-1-john.stultz@linaro.org> References: <20201110034934.70898-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: Daniel Mentz Cc: Chris Goldsworthy 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 Reviewed-by: Brian Starkey Signed-off-by: John Stultz --- v3: * Use page_size() rather then opencoding it v5: * Add comment explaining order size rational --- drivers/dma-buf/heaps/system_heap.c | 89 +++++++++++++++++++++++------ 1 file changed, 71 insertions(+), 18 deletions(-) -- 2.17.1 diff --git a/drivers/dma-buf/heaps/system_heap.c b/drivers/dma-buf/heaps/system_heap.c index 15b36bc862b1..55367266a47b 100644 --- a/drivers/dma-buf/heaps/system_heap.c +++ b/drivers/dma-buf/heaps/system_heap.c @@ -40,6 +40,20 @@ struct dma_heap_attachment { bool mapped; }; +#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}; +/* + * The selection of the orders used for allocation (1MB, 64K, 4K) is designed + * to match with the sizes often found in IOMMUs. Using order 4 pages instead + * of order 0 pages can significantly improve the performance of many IOMMUs + * by reducing TLB pressure and time spent updating page tables. + */ +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; @@ -270,8 +284,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); } @@ -289,6 +306,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, @@ -296,11 +333,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); @@ -312,25 +351,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; + goto free_buffer; + + list_add_tail(&page->lru, &pages); + size_remaining -= page_size(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(page), 0); sg = sg_next(sg); + list_del(&page->lru); } /* create the dmabuf */ @@ -350,14 +399,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;