Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp1793709pxk; Fri, 2 Oct 2020 21:04:59 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyrDz0CQq2KbRPRhtfUmRBzf0L4a6Qkh6+vADili0imZCDNpUUmyR1HCmtHD9gkUFE9PI0A X-Received: by 2002:aa7:cf96:: with SMTP id z22mr6290088edx.120.1601697898949; Fri, 02 Oct 2020 21:04:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1601697898; cv=none; d=google.com; s=arc-20160816; b=ZssVoYpqcYRfdtyqhIGkCHU9Em3CfdmRVQoYUUc1aE/Hgqs6F7g65nkEEIb/xlA/Wo jqd59uKcnYAnrx7TPveHn+Px11lhqRKf/iqpdLxoP4fNatLB0s4dnQaeCD3T0PkmCcss M4tqW0RxwE9PWkbvD6RToipLb4qQ0QfjKj0nWGABkCFAVWPUPvEQtDtEYN0ogZB5Q131 Ccci6uqP4+egGltOTC+RrtSsuMT2X+5D0Nh/Ly9tsq8i318MRgu+A+nxTNG8rXXgBS7a FaIyIiPo+S2YlniLdTGE/Qg5wtEq+jE3ua6f6cbBo8gZSq29B/K9J5THVwov5QkVU0bj /fKA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=PGBUf2FeLay+nQHyuni0t4hgT0kPOlsr2Zfrvy6inwQ=; b=wZDzq5mUDXuUvSTZrQ2LnDMGiO4HdIXbAde81PfJnBQPGkySV+7j4qX0kwo0AE57em 9AD94v5gaVPAe1FauoU7r7LY0bvd249rdiqxffQulxW0w7oAZwXV53dBZjqSXZqAAAHI uv/EpaIoPCnkC8upI+PiMZUToCDkHrZcenJAzEcO4UgR5g8j40hNdXCzxoqwimw+y2Ah oVQ9o+WdoucWDgrEs+Tu+9lxpyxvpIV2L7yw6heyU+cAzWk8J0La5HHbRSCvJLWajidW PlfxM03K9z5OXG1bPXrpRIFlU1TiuUTLE3c6c1mk8ScgZPoUEK21coiFjWr9jfB4PBHB T/iA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=dlmqtogw; 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 z21si532556ejj.329.2020.10.02.21.04.36; Fri, 02 Oct 2020 21:04:58 -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=dlmqtogw; 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 S1725848AbgJCEDX (ORCPT + 99 others); Sat, 3 Oct 2020 00:03:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39076 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725830AbgJCEDP (ORCPT ); Sat, 3 Oct 2020 00:03:15 -0400 Received: from mail-pj1-x1042.google.com (mail-pj1-x1042.google.com [IPv6:2607:f8b0:4864:20::1042]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2DB4DC0613E5 for ; Fri, 2 Oct 2020 21:03:14 -0700 (PDT) Received: by mail-pj1-x1042.google.com with SMTP id t7so2310859pjd.3 for ; Fri, 02 Oct 2020 21:03:14 -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:in-reply-to:references :mime-version:content-transfer-encoding; bh=PGBUf2FeLay+nQHyuni0t4hgT0kPOlsr2Zfrvy6inwQ=; b=dlmqtogwHIsTBC7zsDHkdYC3vPfe+xOYAzKF2XBWDKezceULr8cxnchrdhZ47vJkF/ Yf/yIs3uNKubchja1cAPvlaDQidn2DY6o8YYHf/fpF2plRQpgv17W6ZlvPIByJjPUc8j hKwxAeraiMdPRWzmkfvf3NqIHRR/XxkvHPq8KmUxmRN2gx2t9VuX0B5U18JMuym+7JKL 8Qo4oAg7HHxMO9aHrBMqnI3bFtarP/DlMF+7ExOErzVxgtH3w2o3flvLCgvA7AbhNKgJ XiDTYWh/CjA2rvvFRZLWJUN27l8D1nKZYPHF50RtTeVPc90jyvidnR30FruSb9vT6WnB aLhQ== 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=PGBUf2FeLay+nQHyuni0t4hgT0kPOlsr2Zfrvy6inwQ=; b=A8/PoMyxmvG8moq/NJH1Omlpxb49FmwhgMTxw/Hnkd/FjJJNXSuhGx3qWUbyhf6x5D o7JkXbbhyQ2lYItE3cEWslOMbBaoJ9Dutp2qxmZPj+5s8R0fSb8LH+zddEeWkUH1tiPl Bmeh2YRE84B6cW40YT0hwWz3szs/Y+WtXgjgiaRIl9te2gBvvP1mZN227hiHxpfnbWOw wIqRNRKuAoYX9r/2BPHIrdywXjiTdzoYlBoEb3BdOJgQkcoU4531raZ8UIXvNeXyRymf Y5Oh6o/brjxBJ8Nt23aQr/F9r3ECyBm92E5+iNdXQ7b2H6nB/6NkjhC97JEWNA2FhnW0 UdFQ== X-Gm-Message-State: AOAM532dB/hE6wbmO/Q39VDED0gYiJCziFvotCkT8fm7ahqOkvspJTO5 7S0jRuZjSSdpEhnmDOdJ/YY2BCXTxsAVTg== X-Received: by 2002:a17:90b:1114:: with SMTP id gi20mr5784908pjb.12.1601697793150; Fri, 02 Oct 2020 21:03:13 -0700 (PDT) Received: from localhost.localdomain ([2601:1c2:680:1319:692:26ff:feda:3a81]) by smtp.gmail.com with ESMTPSA id 190sm3909290pfy.22.2020.10.02.21.03.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Oct 2020 21:03:12 -0700 (PDT) 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=20Eide?= , Robin Murphy , Ezequiel Garcia , Simon Ser , James Jones , linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org Subject: [PATCH v3 5/7] dma-buf: system_heap: Allocate higher order pages if available Date: Sat, 3 Oct 2020 04:02:55 +0000 Message-Id: <20201003040257.62768-6-john.stultz@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20201003040257.62768-1-john.stultz@linaro.org> References: <20201003040257.62768-1-john.stultz@linaro.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 Signed-off-by: John Stultz --- v3: * Use page_size() rather then opencoding it --- drivers/dma-buf/heaps/system_heap.c | 83 ++++++++++++++++++++++------- 1 file changed, 65 insertions(+), 18 deletions(-) diff --git a/drivers/dma-buf/heaps/system_heap.c b/drivers/dma-buf/heaps/system_heap.c index ef8d47e5a7ff..2b8d4b6abacb 100644 --- a/drivers/dma-buf/heaps/system_heap.c +++ b/drivers/dma-buf/heaps/system_heap.c @@ -40,6 +40,14 @@ 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}; +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 +278,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 +300,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 +327,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 +345,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 +393,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; -- 2.17.1