From patchwork Wed Mar 24 13:48:45 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Christian_K=C3=B6nig?= X-Patchwork-Id: 12161187 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=-13.5 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED,DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,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 E0013C433DB for ; Wed, 24 Mar 2021 13:48:49 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 71A85619FE for ; Wed, 24 Mar 2021 13:48:49 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 71A85619FE Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id F1F4B6B02CC; Wed, 24 Mar 2021 09:48:48 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id ECEDB6B02CD; Wed, 24 Mar 2021 09:48:48 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D70E66B02CE; Wed, 24 Mar 2021 09:48:48 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0209.hostedemail.com [216.40.44.209]) by kanga.kvack.org (Postfix) with ESMTP id BB5136B02CC for ; Wed, 24 Mar 2021 09:48:48 -0400 (EDT) Received: from smtpin22.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 7BDE1824999B for ; Wed, 24 Mar 2021 13:48:48 +0000 (UTC) X-FDA: 77954898336.22.241D2BE Received: from mail-ej1-f41.google.com (mail-ej1-f41.google.com [209.85.218.41]) by imf28.hostedemail.com (Postfix) with ESMTP id 44893200024E for ; Wed, 24 Mar 2021 13:48:47 +0000 (UTC) Received: by mail-ej1-f41.google.com with SMTP id b7so32991450ejv.1 for ; Wed, 24 Mar 2021 06:48:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=r2IWoqM36LXeehPaGUYlNvmGcx1BKjIaO3ch4KCxnH0=; b=tUajGvt3nRradugavak5cZX9rmAFanSaGJWhMO+kgaPacdSsZe1lG5Qe9uWBjhAv+F qXNrOLkr1Q0IJskWPbhBruqUoI2PVmhysDes1QH/2dxJ6SmeLWoeKiS1Nr8RZuiug6fN jK1mUQmbhwOQnemJZT5P06M3BgkImFEJTHdYRc3f1bOe7jrwO0IAee3fPH54k5VIuD8t f8l23V7QeymCDAHQRQ6FWUTKfvreylUBhcq3qSKUS43GFa+T5znzcu2BYkR0bvgz7Zd8 U+5Umiizl1UkVpIlpDzyWv+fH1+igf+xmGtJZr2gVHslYylgzxT6RQ06KXTUJyrmVSnm yB5w== 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:mime-version :content-transfer-encoding; bh=r2IWoqM36LXeehPaGUYlNvmGcx1BKjIaO3ch4KCxnH0=; b=LSORjprt3TS1znSTd172ruBTDnP30Xq1bkWg9EdJF7FbuGtfFyMZqsYhi6kIHIYl/Q t/bTQUrrYa+ILYFelEztLPTExPbwlo5L73kluWnaF+KRhaRHb1eT5Uj+R4+4Okcw3ZCS FciN0iVG2bgrw06JK6PqeUjiL8/draWegw9b1y9dErm6u35gsGi1DIrjFfVsmaTufZwj b6Ag4uXuKpZEgdLWvIJxPe7/gQfvGKFXL7Q8xcT4SiAH3qQDKlAUiG81Nurs67VeADx3 0kGiGPJQ2Q8drp7kFblF8yFXGCN8/OEoi8a5HZtcP6SFDLxWQCJr6MeFL0EO46AlAQQv k49A== X-Gm-Message-State: AOAM533MewneeZWCsuhaTT84fwaN1gnb/CClDbDQ39NstK7ngTdaqF6B 5R4PndsffWVc55B3vPZeh20= X-Google-Smtp-Source: ABdhPJzLo7pX6n92/sVws9NCjp7X/q1xcsQUe3JGUKy82vNGhGn2m7s9evZ9g1fTzStuSKT3wYYYrA== X-Received: by 2002:a17:906:2a16:: with SMTP id j22mr3805903eje.247.1616593726637; Wed, 24 Mar 2021 06:48:46 -0700 (PDT) Received: from abel.fritz.box ([2a02:908:1252:fb60:4aeb:be19:1df6:ccb6]) by smtp.gmail.com with ESMTPSA id p3sm963077ejd.7.2021.03.24.06.48.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Mar 2021 06:48:46 -0700 (PDT) From: " =?utf-8?q?Christian_K=C3=B6nig?= " X-Google-Original-From: =?utf-8?q?Christian_K=C3=B6nig?= To: dri-devel@lists.freedesktop.org, linux-mm@kvack.org Cc: Liang.Liang@amd.com, daniel@ffwll.ch, thomas_os@shipmail.org Subject: [PATCH] drm/ttm: switch back to static allocation limits for now Date: Wed, 24 Mar 2021 14:48:45 +0100 Message-Id: <20210324134845.2338-1-christian.koenig@amd.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 44893200024E X-Stat-Signature: dwwoirx6cgk4e9q17hgj3b9etg8pcyyy Received-SPF: none (gmail.com>: No applicable sender policy available) receiver=imf28; identity=mailfrom; envelope-from=""; helo=mail-ej1-f41.google.com; client-ip=209.85.218.41 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1616593727-390924 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: The shrinker based approach still has some flaws. Especially that we need temporary pages to free up the pages allocated to the driver is problematic in a shrinker. Signed-off-by: Christian König Acked-by: Daniel Vetter --- drivers/gpu/drm/ttm/ttm_device.c | 14 ++-- drivers/gpu/drm/ttm/ttm_tt.c | 112 ++++++++++++------------------- include/drm/ttm/ttm_tt.h | 3 +- 3 files changed, 53 insertions(+), 76 deletions(-) diff --git a/drivers/gpu/drm/ttm/ttm_device.c b/drivers/gpu/drm/ttm/ttm_device.c index 95e1b7b1f2e6..388da2a7f0bb 100644 --- a/drivers/gpu/drm/ttm/ttm_device.c +++ b/drivers/gpu/drm/ttm/ttm_device.c @@ -53,7 +53,6 @@ static void ttm_global_release(void) goto out; ttm_pool_mgr_fini(); - ttm_tt_mgr_fini(); __free_page(glob->dummy_read_page); memset(glob, 0, sizeof(*glob)); @@ -64,7 +63,7 @@ static void ttm_global_release(void) static int ttm_global_init(void) { struct ttm_global *glob = &ttm_glob; - unsigned long num_pages; + unsigned long num_pages, num_dma32; struct sysinfo si; int ret = 0; unsigned i; @@ -79,8 +78,15 @@ static int ttm_global_init(void) * system memory. */ num_pages = ((u64)si.totalram * si.mem_unit) >> PAGE_SHIFT; - ttm_pool_mgr_init(num_pages * 50 / 100); - ttm_tt_mgr_init(); + num_pages /= 2; + + /* But for DMA32 we limit ourself to only use 2GiB maximum. */ + num_dma32 = (u64)(si.totalram - si.totalhigh) * si.mem_unit + >> PAGE_SHIFT; + num_dma32 = min(num_dma32, 2UL << (30 - PAGE_SHIFT)); + + ttm_pool_mgr_init(num_pages); + ttm_tt_mgr_init(num_pages, num_dma32); spin_lock_init(&glob->lru_lock); glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32); diff --git a/drivers/gpu/drm/ttm/ttm_tt.c b/drivers/gpu/drm/ttm/ttm_tt.c index 2f0833c98d2c..5d8820725b75 100644 --- a/drivers/gpu/drm/ttm/ttm_tt.c +++ b/drivers/gpu/drm/ttm/ttm_tt.c @@ -40,8 +40,18 @@ #include "ttm_module.h" -static struct shrinker mm_shrinker; -static atomic_long_t swapable_pages; +static unsigned long ttm_pages_limit; + +MODULE_PARM_DESC(pages_limit, "Limit for the allocated pages"); +module_param_named(pages_limit, ttm_pages_limit, ulong, 0644); + +static unsigned long ttm_dma32_pages_limit; + +MODULE_PARM_DESC(dma32_pages_limit, "Limit for the allocated DMA32 pages"); +module_param_named(dma32_pages_limit, ttm_dma32_pages_limit, ulong, 0644); + +static atomic_long_t ttm_pages_allocated; +static atomic_long_t ttm_dma32_pages_allocated; /* * Allocates a ttm structure for the given BO. @@ -294,8 +304,6 @@ static void ttm_tt_add_mapping(struct ttm_device *bdev, struct ttm_tt *ttm) for (i = 0; i < ttm->num_pages; ++i) ttm->pages[i]->mapping = bdev->dev_mapping; - - atomic_long_add(ttm->num_pages, &swapable_pages); } int ttm_tt_populate(struct ttm_device *bdev, @@ -309,12 +317,25 @@ int ttm_tt_populate(struct ttm_device *bdev, if (ttm_tt_is_populated(ttm)) return 0; + atomic_long_add(ttm->num_pages, &ttm_pages_allocated); + if (bdev->pool.use_dma32) + atomic_long_add(ttm->num_pages, &ttm_dma32_pages_allocated); + + while (atomic_long_read(&ttm_pages_allocated) > ttm_pages_limit || + atomic_long_read(&ttm_dma32_pages_allocated) > + ttm_dma32_pages_limit) { + + ret = ttm_bo_swapout(ctx, GFP_KERNEL); + if (ret) + goto error; + } + if (bdev->funcs->ttm_tt_populate) ret = bdev->funcs->ttm_tt_populate(bdev, ttm, ctx); else ret = ttm_pool_alloc(&bdev->pool, ttm, ctx); if (ret) - return ret; + goto error; ttm_tt_add_mapping(bdev, ttm); ttm->page_flags |= TTM_PAGE_FLAG_PRIV_POPULATED; @@ -327,6 +348,12 @@ int ttm_tt_populate(struct ttm_device *bdev, } return 0; + +error: + atomic_long_sub(ttm->num_pages, &ttm_pages_allocated); + if (bdev->pool.use_dma32) + atomic_long_sub(ttm->num_pages, &ttm_dma32_pages_allocated); + return ret; } EXPORT_SYMBOL(ttm_tt_populate); @@ -342,12 +369,9 @@ static void ttm_tt_clear_mapping(struct ttm_tt *ttm) (*page)->mapping = NULL; (*page++)->index = 0; } - - atomic_long_sub(ttm->num_pages, &swapable_pages); } -void ttm_tt_unpopulate(struct ttm_device *bdev, - struct ttm_tt *ttm) +void ttm_tt_unpopulate(struct ttm_device *bdev, struct ttm_tt *ttm) { if (!ttm_tt_is_populated(ttm)) return; @@ -357,76 +381,24 @@ void ttm_tt_unpopulate(struct ttm_device *bdev, bdev->funcs->ttm_tt_unpopulate(bdev, ttm); else ttm_pool_free(&bdev->pool, ttm); - ttm->page_flags &= ~TTM_PAGE_FLAG_PRIV_POPULATED; -} - -/* As long as pages are available make sure to release at least one */ -static unsigned long ttm_tt_shrinker_scan(struct shrinker *shrink, - struct shrink_control *sc) -{ - struct ttm_operation_ctx ctx = { - .no_wait_gpu = false - }; - int ret; - - ret = ttm_bo_swapout(&ctx, GFP_NOFS); - return ret < 0 ? SHRINK_EMPTY : ret; -} - -/* Return the number of pages available or SHRINK_EMPTY if we have none */ -static unsigned long ttm_tt_shrinker_count(struct shrinker *shrink, - struct shrink_control *sc) -{ - unsigned long num_pages; - - num_pages = atomic_long_read(&swapable_pages); - return num_pages ? num_pages : SHRINK_EMPTY; -} -#ifdef CONFIG_DEBUG_FS + atomic_long_sub(ttm->num_pages, &ttm_pages_allocated); + if (bdev->pool.use_dma32) + atomic_long_sub(ttm->num_pages, &ttm_dma32_pages_allocated); -/* Test the shrinker functions and dump the result */ -static int ttm_tt_debugfs_shrink_show(struct seq_file *m, void *data) -{ - struct shrink_control sc = { .gfp_mask = GFP_KERNEL }; - - fs_reclaim_acquire(GFP_KERNEL); - seq_printf(m, "%lu/%lu\n", ttm_tt_shrinker_count(&mm_shrinker, &sc), - ttm_tt_shrinker_scan(&mm_shrinker, &sc)); - fs_reclaim_release(GFP_KERNEL); - - return 0; + ttm->page_flags &= ~TTM_PAGE_FLAG_PRIV_POPULATED; } -DEFINE_SHOW_ATTRIBUTE(ttm_tt_debugfs_shrink); - -#endif - - /** * ttm_tt_mgr_init - register with the MM shrinker * * Register with the MM shrinker for swapping out BOs. */ -int ttm_tt_mgr_init(void) +void ttm_tt_mgr_init(unsigned long num_pages, unsigned long num_dma32_pages) { -#ifdef CONFIG_DEBUG_FS - debugfs_create_file("tt_shrink", 0400, ttm_debugfs_root, NULL, - &ttm_tt_debugfs_shrink_fops); -#endif - - mm_shrinker.count_objects = ttm_tt_shrinker_count; - mm_shrinker.scan_objects = ttm_tt_shrinker_scan; - mm_shrinker.seeks = 1; - return register_shrinker(&mm_shrinker); -} + if (!ttm_pages_limit) + ttm_pages_limit = num_pages; -/** - * ttm_tt_mgr_fini - unregister our MM shrinker - * - * Unregisters the MM shrinker. - */ -void ttm_tt_mgr_fini(void) -{ - unregister_shrinker(&mm_shrinker); + if (!ttm_dma32_pages_limit) + ttm_dma32_pages_limit = num_dma32_pages; } diff --git a/include/drm/ttm/ttm_tt.h b/include/drm/ttm/ttm_tt.h index 069f8130241a..134d09ef7766 100644 --- a/include/drm/ttm/ttm_tt.h +++ b/include/drm/ttm/ttm_tt.h @@ -157,8 +157,7 @@ int ttm_tt_populate(struct ttm_device *bdev, struct ttm_tt *ttm, struct ttm_oper */ void ttm_tt_unpopulate(struct ttm_device *bdev, struct ttm_tt *ttm); -int ttm_tt_mgr_init(void); -void ttm_tt_mgr_fini(void); +void ttm_tt_mgr_init(unsigned long num_pages, unsigned long num_dma32_pages); #if IS_ENABLED(CONFIG_AGP) #include