From patchwork Fri Feb 5 08:06:17 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: John Stultz X-Patchwork-Id: 12069523 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.5 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 91E3CC433DB for ; Fri, 5 Feb 2021 08:06:34 +0000 (UTC) Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 3D0C364F3F for ; Fri, 5 Feb 2021 08:06:34 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 3D0C364F3F Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=linaro.org Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=dri-devel-bounces@lists.freedesktop.org Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id E9DE46F406; Fri, 5 Feb 2021 08:06:31 +0000 (UTC) Received: from mail-pg1-x532.google.com (mail-pg1-x532.google.com [IPv6:2607:f8b0:4864:20::532]) by gabe.freedesktop.org (Postfix) with ESMTPS id 626946F405 for ; Fri, 5 Feb 2021 08:06:31 +0000 (UTC) Received: by mail-pg1-x532.google.com with SMTP id n10so3982307pgl.10 for ; Fri, 05 Feb 2021 00:06:31 -0800 (PST) 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=Eppkc7cbhBhkKsBh0TVfEHFBhagPwlMZuH5kYXNg9Sk=; b=LvkxxuZDAt3g61EzURtwZNN7B+c9BI9CcoYDAacfHmWWZaJmA/Q8ueMntoQL87InPz x3cic234jsbfsSUbgOlH9EphjhLyv75y5ljVcj96lD8GWTzU94A237kWBn6zXhPaZSVv Qxl5gI8MIE7AZ+8ju558OSalzhBAC+kamKS43qb/4rZZJCsnWqBeGMaKqEO4z6XB3UBy NICgSOqMmB85OIVfSU/BHZ31T9K5ZA/WU4F1VuEGj27Tm0IsEqPG/qmh1F+9RQIJlF/+ 72ItAQdnjOo4oeXkb2xUerZye6TyguzkprgbaF9p5TRXlRe/6wuok2G0jnFH6JqwoP9I KBsw== 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=Eppkc7cbhBhkKsBh0TVfEHFBhagPwlMZuH5kYXNg9Sk=; b=lp7XWF8LQg1qoBvM9UP1ksMdbyI3nvZ+3MOtvAY9ZIAcN1PSt8JfB9Secdnx9Hu+MP SGeSkChRq6R6C9oML1IB8jahweSzR4KWA6+EpnlJmQ6R8j2N3FHwLIKpok/Awt3Kucqj HSK+oBpeh8ET8hBnjB167FEctgJm62d2b2duqmljDeAKmf5pqpZbvPCQT+ilkD8BNHq3 dc9SIiR23HRGwR8cYU8G42USTxbD1UiegOVjbl8gXsOp75WhHsBzK1sqz43C9BYKWM37 Ft1GTpY7PzkDqId8xWxM/zQ634wGSV+zSKYAsOQE+DLn6l1JzY369iw21mgIuoAZiLut yk6A== X-Gm-Message-State: AOAM533Xhn2iCZNcJqE3tjqerKfvQgPgQTlvrulVfY/m1cfZQjql5jgT gcSRTnfmeacHpLgTy06s5tHusw== X-Google-Smtp-Source: ABdhPJx48bhhs1D3gknefKfUqkMUbp2y+z4at6TszkoS7X+/fr9erWj+jlUYkxFBhUUgi39EnSaBGA== X-Received: by 2002:aa7:946c:0:b029:1ce:3f04:3f67 with SMTP id t12-20020aa7946c0000b02901ce3f043f67mr3589630pfq.6.1612512390927; Fri, 05 Feb 2021 00:06:30 -0800 (PST) Received: from localhost.localdomain ([2601:1c2:680:1319:692:26ff:feda:3a81]) by smtp.gmail.com with ESMTPSA id 32sm9520070pgq.80.2021.02.05.00.06.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Feb 2021 00:06:30 -0800 (PST) From: John Stultz To: lkml Subject: [RFC][PATCH v6 3/7] drm: ttm_pool: Rework ttm_pool_free_page to allow us to use it as a function pointer Date: Fri, 5 Feb 2021 08:06:17 +0000 Message-Id: <20210205080621.3102035-4-john.stultz@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210205080621.3102035-1-john.stultz@linaro.org> References: <20210205080621.3102035-1-john.stultz@linaro.org> MIME-Version: 1.0 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: dri-devel@lists.freedesktop.org, Sandeep Patil , Ezequiel Garcia , Robin Murphy , James Jones , Liam Mark , Laura Abbott , Chris Goldsworthy , Hridya Valsaraju , =?utf-8?q?=C3=98rjan_Eide?= , linux-media@vger.kernel.org, Suren Baghdasaryan , Christian Koenig , Daniel Mentz Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" This refactors ttm_pool_free_page(), and by adding extra entries to ttm_pool_page_dat, we then use it for all allocations, which allows us to simplify the arguments needed to be passed to ttm_pool_free_page(). This is critical for allowing the free function to be called by the sharable drm_page_pool logic. Cc: Daniel Vetter Cc: Christian Koenig Cc: Sumit Semwal Cc: Liam Mark Cc: Chris Goldsworthy Cc: Laura Abbott Cc: Brian Starkey Cc: Hridya Valsaraju Cc: Suren Baghdasaryan Cc: Sandeep Patil Cc: Daniel Mentz 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/gpu/drm/ttm/ttm_pool.c | 60 ++++++++++++++++++---------------- 1 file changed, 32 insertions(+), 28 deletions(-) diff --git a/drivers/gpu/drm/ttm/ttm_pool.c b/drivers/gpu/drm/ttm/ttm_pool.c index c0274e256be3..eca36678f967 100644 --- a/drivers/gpu/drm/ttm/ttm_pool.c +++ b/drivers/gpu/drm/ttm/ttm_pool.c @@ -44,10 +44,14 @@ /** * struct ttm_pool_page_dat - Helper object for coherent DMA mappings * + * @pool: ttm_pool pointer the page was allocated by + * @caching: the caching value the allocated page was configured for * @addr: original DMA address returned for the mapping * @vaddr: original vaddr return for the mapping and order in the lower bits */ struct ttm_pool_page_dat { + struct ttm_pool *pool; + enum ttm_caching caching; dma_addr_t addr; unsigned long vaddr; }; @@ -71,13 +75,20 @@ static struct shrinker mm_shrinker; /* Allocate pages of size 1 << order with the given gfp_flags */ static struct page *ttm_pool_alloc_page(struct ttm_pool *pool, gfp_t gfp_flags, - unsigned int order) + unsigned int order, enum ttm_caching caching) { unsigned long attr = DMA_ATTR_FORCE_CONTIGUOUS; struct ttm_pool_page_dat *dat; struct page *p; void *vaddr; + dat = kmalloc(sizeof(*dat), GFP_KERNEL); + if (!dat) + return NULL; + + dat->pool = pool; + dat->caching = caching; + /* Don't set the __GFP_COMP flag for higher order allocations. * Mapping pages directly into an userspace process and calling * put_page() on a TTM allocated page is illegal. @@ -88,15 +99,13 @@ static struct page *ttm_pool_alloc_page(struct ttm_pool *pool, gfp_t gfp_flags, if (!pool->use_dma_alloc) { p = alloc_pages(gfp_flags, order); - if (p) - p->private = order; + if (!p) + goto error_free; + dat->vaddr = order; + p->private = (unsigned long)dat; return p; } - dat = kmalloc(sizeof(*dat), GFP_KERNEL); - if (!dat) - return NULL; - if (order) attr |= DMA_ATTR_NO_WARN; @@ -123,34 +132,34 @@ static struct page *ttm_pool_alloc_page(struct ttm_pool *pool, gfp_t gfp_flags, } /* Reset the caching and pages of size 1 << order */ -static void ttm_pool_free_page(struct ttm_pool *pool, enum ttm_caching caching, - unsigned int order, struct page *p) +static int ttm_pool_free_page(struct page *p, unsigned int order) { unsigned long attr = DMA_ATTR_FORCE_CONTIGUOUS; - struct ttm_pool_page_dat *dat; + struct ttm_pool_page_dat *dat = (void *)p->private; void *vaddr; #ifdef CONFIG_X86 /* We don't care that set_pages_wb is inefficient here. This is only * used when we have to shrink and CPU overhead is irrelevant then. */ - if (caching != ttm_cached && !PageHighMem(p)) + if (dat->caching != ttm_cached && !PageHighMem(p)) set_pages_wb(p, 1 << order); #endif - if (!pool || !pool->use_dma_alloc) { + if (!dat->pool || !dat->pool->use_dma_alloc) { __free_pages(p, order); - return; + goto out; } if (order) attr |= DMA_ATTR_NO_WARN; - dat = (void *)p->private; vaddr = (void *)(dat->vaddr & PAGE_MASK); - dma_free_attrs(pool->dev, (1UL << order) * PAGE_SIZE, vaddr, dat->addr, + dma_free_attrs(dat->pool->dev, (1UL << order) * PAGE_SIZE, vaddr, dat->addr, attr); +out: kfree(dat); + return 1 << order; } /* Apply a new caching to an array of pages */ @@ -264,7 +273,7 @@ static void ttm_pool_type_fini(struct ttm_pool_type *pt) mutex_unlock(&shrinker_lock); list_for_each_entry_safe(p, tmp, &pt->pages, lru) - ttm_pool_free_page(pt->pool, pt->caching, pt->order, p); + ttm_pool_free_page(p, pt->order); } /* Return the pool_type to use for the given caching and order */ @@ -307,7 +316,7 @@ static unsigned int ttm_pool_shrink(void) p = ttm_pool_type_take(pt); if (p) { - ttm_pool_free_page(pt->pool, pt->caching, pt->order, p); + ttm_pool_free_page(p, pt->order); num_freed = 1 << pt->order; } else { num_freed = 0; @@ -322,13 +331,9 @@ static unsigned int ttm_pool_shrink(void) /* Return the allocation order based for a page */ static unsigned int ttm_pool_page_order(struct ttm_pool *pool, struct page *p) { - if (pool->use_dma_alloc) { - struct ttm_pool_page_dat *dat = (void *)p->private; - - return dat->vaddr & ~PAGE_MASK; - } + struct ttm_pool_page_dat *dat = (void *)p->private; - return p->private; + return dat->vaddr & ~PAGE_MASK; } /** @@ -379,7 +384,7 @@ int ttm_pool_alloc(struct ttm_pool *pool, struct ttm_tt *tt, if (p) { apply_caching = true; } else { - p = ttm_pool_alloc_page(pool, gfp_flags, order); + p = ttm_pool_alloc_page(pool, gfp_flags, order, tt->caching); if (p && PageHighMem(p)) apply_caching = true; } @@ -428,13 +433,13 @@ int ttm_pool_alloc(struct ttm_pool *pool, struct ttm_tt *tt, ttm_mem_global_free_page(&ttm_mem_glob, p, (1 << order) * PAGE_SIZE); error_free_page: - ttm_pool_free_page(pool, tt->caching, order, p); + ttm_pool_free_page(p, order); error_free_all: num_pages = tt->num_pages - num_pages; for (i = 0; i < num_pages; ) { order = ttm_pool_page_order(pool, tt->pages[i]); - ttm_pool_free_page(pool, tt->caching, order, tt->pages[i]); + ttm_pool_free_page(tt->pages[i], order); i += 1 << order; } @@ -470,8 +475,7 @@ void ttm_pool_free(struct ttm_pool *pool, struct ttm_tt *tt) if (pt) ttm_pool_type_give(pt, tt->pages[i]); else - ttm_pool_free_page(pool, tt->caching, order, - tt->pages[i]); + ttm_pool_free_page(tt->pages[i], order); i += num_pages; }