From patchwork Wed Feb 15 16:13:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Thomas Hellstrom X-Patchwork-Id: 13141851 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4DF53C636D4 for ; Wed, 15 Feb 2023 16:15:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E40AE6B0085; Wed, 15 Feb 2023 11:15:20 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id DF0E56B0087; Wed, 15 Feb 2023 11:15:20 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CB8A06B0088; Wed, 15 Feb 2023 11:15:20 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id B77306B0085 for ; Wed, 15 Feb 2023 11:15:20 -0500 (EST) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 5163C1208DE for ; Wed, 15 Feb 2023 16:15:20 +0000 (UTC) X-FDA: 80470026000.19.1131857 Received: from mga06.intel.com (mga06b.intel.com [134.134.136.31]) by imf09.hostedemail.com (Postfix) with ESMTP id 1E97A14000E for ; Wed, 15 Feb 2023 16:15:17 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=AakKCsX9; spf=none (imf09.hostedemail.com: domain of thomas.hellstrom@linux.intel.com has no SPF policy when checking 134.134.136.31) smtp.mailfrom=thomas.hellstrom@linux.intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1676477718; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=tVzeTFs1CmQHooRkDPM9dhKu/cAVh+uQXtpE6xUm/tA=; b=4XWeu4HE4QTJ3QbR/4RB5lzThNqvNjic/y3UBMJzGwhdJxNUXHGCl9n8PZfSoFE2gLQr+U Vw0BEng0yUcEs8rAy7kNmoIT3hI8CKnsjp49JwIYWwa6ZBee1pYicFB8HnUAH7XZBPB/7d 8pCk0Lbz8s++Lx784cmBAvtYivsQ7rk= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=AakKCsX9; spf=none (imf09.hostedemail.com: domain of thomas.hellstrom@linux.intel.com has no SPF policy when checking 134.134.136.31) smtp.mailfrom=thomas.hellstrom@linux.intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676477718; a=rsa-sha256; cv=none; b=5kAwxhLn4SbABEbjzkzhwV/ay7Zk722PJxBUKyx3VYSolHO+BDHK+4kPf8RhQrHIcQd0h2 TzKytbVHW++pev3E1euzNDkf0b/rfzZm0gy21ikOWDBNiaC6nrKqtd9kKQADKvx6uax7MA mKvNMU1Np4IZr7EGTh+ysh1+nkr+msQ= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676477718; x=1708013718; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=CA6Gd5JNe0QIjwqEaLurGUUyFsS+XKhHwV/GCIHG1zw=; b=AakKCsX99plUdfGH8SbAL292jriIjFAAB/B8NEXd3K8m9/Tprp06fDRS gpIRR1Bz91mGVJgZWspbGoGd7mB086ejEfJuwjzvkp0cn7tG1OsAIpXMQ uoQshIlHSbVCqBe/R+ks9P5A0zCu4OHYzK516CmgZbht6Y4/2+PT1zx6Z GTDsVnd1VjNl4TNfFcMMKM62h3YJ2etqvJkGaUQEtFNYWIfiPazu/Zdel KXo+CPaTQduRuSvwO4E2Z2GdvCtkEtoIhqHjAEt7kUSrEm0INu9KBB7g6 idi0YaUBLvGBz+m09MxDWAw1GlIavmFAGm4okEth8HxKI0QuR6Yk5dfGX Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10622"; a="393870962" X-IronPort-AV: E=Sophos;i="5.97,300,1669104000"; d="scan'208";a="393870962" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 15 Feb 2023 08:15:15 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10622"; a="758472509" X-IronPort-AV: E=Sophos;i="5.97,300,1669104000"; d="scan'208";a="758472509" Received: from auliel-mobl1.ger.corp.intel.com (HELO thellstr-mobl1.intel.com) ([10.249.254.14]) by fmsmga003-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 15 Feb 2023 08:15:10 -0800 From: =?utf-8?q?Thomas_Hellstr=C3=B6m?= To: dri-devel@lists.freedesktop.org Cc: =?utf-8?q?Thomas_Hellstr=C3=B6m?= , Andrew Morton , "Matthew Wilcox (Oracle)" , Miaohe Lin , David Hildenbrand , Johannes Weiner , Peter Xu , NeilBrown , Daniel Vetter , Christian Koenig , Dave Airlie , Dave Hansen , Matthew Auld , linux-graphics-maintainer@vmware.com, linux-mm@kvack.org, intel-gfx@lists.freedesktop.org Subject: [RFC PATCH 09/16] drm/ttm: Introduce shrink throttling. Date: Wed, 15 Feb 2023 17:13:58 +0100 Message-Id: <20230215161405.187368-10-thomas.hellstrom@linux.intel.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20230215161405.187368-1-thomas.hellstrom@linux.intel.com> References: <20230215161405.187368-1-thomas.hellstrom@linux.intel.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 1E97A14000E X-Stat-Signature: 96yfxsm7b89w6bapjkutpqi3ed3ar1ni X-HE-Tag: 1676477717-293984 X-HE-Meta: U2FsdGVkX193jxmJji0uaoa05nRDucYMcjfwiLRJ+yp5mj+wuggmTEVGekBSfbiHDeIuRhVMObFdTuvtWj+tnlhrAu+/PJc71VrGeS9ASq8DTVBt/cUPqQlpfRUAFcqRIswsv6p7YxO6fHbMRamxXnFNutWdH2fbdyRJi9t2YKn693Gj75Pr+NxFtH4FmDbj8AiBrPcNByIZqWsT1jP8Pk1oq/18L9KN9g5t/+AQENMQoxz2Ss5htskCK4ukMR/yj0n9Xj5W1clayMGlWuhgZ4pr4UtOHD6WlNQg7l1nadQuyDzxZ9umN5Jd70y0Z4wcSs851wDXJjK+ixMqhT3Fh7S8amiYg036vhloodfT2krANsLGmcfqC8KQ0HNAX4bVGqFkqqT1AwhJiMgdrRfttSORXrlAyF0TZYSq0AjMyJLRo1YMwAMPVV5Xq+sixPQueofd3KAOLFarpjnB3kC2/bRcsWRX4POaklwjhaOORjQyK5MIn91oPZLSxMLDi3iDVh5LEUze9GRrGZS5k7pQlg8mDsahwXYKmRdrvbh+r81sWLGK/lrWPLf+m18YE+pVKwLGF1g548pviWhraEMEtJqGsYYXoAfWEXpEACSlBimeReuKrIIfktadHK/3itP4cyh1/A3FHNyOSw9JAYJunCosKTWhlKxaoLPTkzhM/0RNACNl2OY3AF/wnOwe2+xn4DR97zAamB0taVxM6clGIp70Yr//tMOmgV7DCr9VvTOIF2AoQm2O+N+K9eNCmESD8jOHv+5YWuvyqghR4iMk1offmQZnLbGLZMF5zpUGAJo3m29P5gYeHF0hjzqeNDyjBbedIesXJbKuFWkXuv+/iXkf9dntuUCUlKALqtEbzi4pYTm1CLTUoevDytjejsCm2Fg8+mBJpX6MjgtDPJ++foavzhnLRagKAiU9fzlL1OIXXuFR7Y4dWn5HTB1UkLElpfkSfvex859cCjuCXb4 0wfQH2xx 9kdOFiXnJiLNSfqTGTSVlgxOxMY7XAKuh9utoQLikQZ6zhB4dCMtYCOgy32nR5tkjTLUA74/AAZboj3gOt2PeMpX4Zk3kgdcT201zSU/IZcemf3CFd1sI0ewZMjEZLDvSWTiRgetrTmuHwAFgk3WbbWYM5AWIueJI8bMmjJQzqYuCb+YyJxHq25ve7m5FDR4txYXy 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: Since pages are not immediately freed by the TTM shrinker but rather inserted into the swap cache, the system will keep on calling the shrinker rapidly filling the swap cache which has a negative impact on system performance. When shrinking, throttle on the number of pages present in the swap cache. Signed-off-by: Thomas Hellström --- drivers/gpu/drm/ttm/ttm_tt.c | 40 ++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/drivers/gpu/drm/ttm/ttm_tt.c b/drivers/gpu/drm/ttm/ttm_tt.c index 5a57117c21ec..848adf2a623e 100644 --- a/drivers/gpu/drm/ttm/ttm_tt.c +++ b/drivers/gpu/drm/ttm/ttm_tt.c @@ -432,6 +432,42 @@ static unsigned long ttm_tt_shrinker_count(struct shrinker *shrink, return num_pages ? num_pages : SHRINK_EMPTY; } +#define TTM_SWAP_MIN_SWAP_PAGES (SZ_128M >> PAGE_SHIFT) +#define TTM_SWAP_MAX_SWAPCACHE_PAGES (SZ_1G >> PAGE_SHIFT) +static unsigned long ttm_tt_shrinker_throttle(unsigned long pages) +{ + unsigned long + tmp = get_nr_swap_pages(); + + /* + * Draining available swap space too far will trigger + * systemd-oomd even if there are a huge number of dirty pages + * available for laundry and free in the swap cache. Don't drain + * the available swap-space too far. + */ + if (tmp > TTM_SWAP_MIN_SWAP_PAGES) + tmp -= TTM_SWAP_MIN_SWAP_PAGES; + else + tmp = 0; + + pages = min(tmp, pages); + + /* + * Our shrinker doesn't immediately free pages unless they belong + * to purgeable objects. Rather they are inserted into the swap-cache. + * But the system doesn't really get this and continues to call our + * shrinker thinking it's still out of memory, when it could just + * laundry pages in the swap cache and free them. So throttle on the + * number of pages in the swap cache. + */ + + tmp = total_swapcache_pages(); + if (tmp > TTM_SWAP_MAX_SWAPCACHE_PAGES) + pages = 0; + + return pages; +} + static unsigned long ttm_tt_shrinker_scan(struct shrinker *shrink, struct shrink_control *sc) { @@ -459,6 +495,10 @@ static unsigned long ttm_tt_shrinker_scan(struct shrinker *shrink, nr_to_scan -= freed; else nr_to_scan = 0; + + if (nr_to_scan) + nr_to_scan = ttm_tt_shrinker_throttle(nr_to_scan); + if (!nr_to_scan) return freed ? freed : SHRINK_STOP;