From patchwork Mon Aug 7 11:09:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qi Zheng X-Patchwork-Id: 13343696 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 8DB63C001DB for ; Mon, 7 Aug 2023 11:19:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2D4CF6B0080; Mon, 7 Aug 2023 07:19:12 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 25CD78D0005; Mon, 7 Aug 2023 07:19:12 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0AF6B8D0003; Mon, 7 Aug 2023 07:19:12 -0400 (EDT) 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 ED26E6B0080 for ; Mon, 7 Aug 2023 07:19:11 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id BBEA4809E0 for ; Mon, 7 Aug 2023 11:19:11 +0000 (UTC) X-FDA: 81097062102.30.C661820 Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.173]) by imf23.hostedemail.com (Postfix) with ESMTP id AF4AC140003 for ; Mon, 7 Aug 2023 11:19:09 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=Te7kg9d2; spf=pass (imf23.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.214.173 as permitted sender) smtp.mailfrom=zhengqi.arch@bytedance.com; dmarc=pass (policy=quarantine) header.from=bytedance.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1691407149; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=dp1PQp+5gTkm4DG/kxrQVAZ0rB7KyI/rGW6TiEnUaUw=; b=R0dFq2gfyUckve4YvtO1U7y8HGodYHc2T9uj9fr/apLeAtbLLq84TW8l9WIm5H1lJHlRXi XaRL57ZrMf9Kvj3tmM4Pq97dteMm4Ofl1ZWtOI3cQ0fprU/kPumMGB2QJtPSw5mdA4bTVA Ai6YTuJrencL2Vx2lFVHPLn3ngvjQ70= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1691407149; a=rsa-sha256; cv=none; b=qsVb9bU9pGEfrESA9IIwOvC407ERIaZdpKEc8H3wSxsslvXNFVfnNPQ+LrW6JzvVdwtuXh TRfeaZw3GMIHvcpIMBZ29oNchj7LOU0C2GTNDBsZiL+EyMJLcycpdNU5GdJo5CdEZ+jj2B LqT4hmikGvORD4K7/udcTFitngcvLyw= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=Te7kg9d2; spf=pass (imf23.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.214.173 as permitted sender) smtp.mailfrom=zhengqi.arch@bytedance.com; dmarc=pass (policy=quarantine) header.from=bytedance.com Received: by mail-pl1-f173.google.com with SMTP id d9443c01a7336-1bc4dc65aa7so5904255ad.0 for ; Mon, 07 Aug 2023 04:19:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1691407148; x=1692011948; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=dp1PQp+5gTkm4DG/kxrQVAZ0rB7KyI/rGW6TiEnUaUw=; b=Te7kg9d2kQW6gac9UP89KtIVcLTfWDu6srH7dtxrmXTL4ErpAX/pJkQtXd7hg2HtRV b8geFLLbM8+wRhZIYuLjJhyOfyGHnlddPjbSoTi8+7JUn0itl8DrGEf9FkegNhDgT4QU +aRhY4yC93Sy/uol3bSwIULh/giMXKgqvscX8PDfZ03M6CNIZOjJqm4ZovfIFxg7FXvq HemaZ47NKkfhG9oH07mD8LDQiWRUxbmI+sshlvUeh5qK7EeOWS/jAB9AyDVCuXh9Krsn cGXM1IoaSAiEJffIAN7TmuDV3qm2xRt9nfiAYhQ3/VTYWFSG3zxU2XV/jGXOIaUTjpLr zeJQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691407148; x=1692011948; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=dp1PQp+5gTkm4DG/kxrQVAZ0rB7KyI/rGW6TiEnUaUw=; b=RQmEehiFQ9KlhZsJg/qX5z+WCKpzQAmEEV3piJtYNvr8wUyB6DNsvZOZuigIWY7gKy NdiTw+BCEGhfAi19olY+xrKb4pSpVtILiIBZ2kUno408E64ZeLJPUKRORh6WwGJRnCKb t7l3AlUL5Xpb+HEo3cR7RzArjg27/jrWGzqj//39SeE1HeM7PVCexg5tbrJkLplV8NWo 3/eVepSN4K52nhaVmDMCRgQJ+FiJHfJajegHXgk7C+c4quQIByXDMMFGrkIWdAyCMTlj My/oY+gzWegJA3Zfv2j2EyS4ag6ZQjAHscnckX83fWprNxT7TjBb7/cYxtAUhBoQA4Qu worw== X-Gm-Message-State: ABy/qLamZHNa+ZOQZFxLjeeM1pSw/tTRtIDSlyDCffAvgkSOWB1YqrXZ uyw769CDT/M98NW8LG3LF/57wA== X-Google-Smtp-Source: APBJJlGWGDf3ZP6TvEF/B95Km+XRmBUarTmlKp42txasQQnYUyfeo2SyrhxLvRA3BfrSE4M4+QRzMg== X-Received: by 2002:a17:902:dace:b0:1b8:17e8:5472 with SMTP id q14-20020a170902dace00b001b817e85472mr32807567plx.1.1691407148534; Mon, 07 Aug 2023 04:19:08 -0700 (PDT) Received: from C02DW0BEMD6R.bytedance.net ([203.208.167.146]) by smtp.gmail.com with ESMTPSA id y13-20020a17090aca8d00b0025be7b69d73sm5861191pjt.12.2023.08.07.04.18.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Aug 2023 04:19:07 -0700 (PDT) From: Qi Zheng To: akpm@linux-foundation.org, david@fromorbit.com, tkhai@ya.ru, vbabka@suse.cz, roman.gushchin@linux.dev, djwong@kernel.org, brauner@kernel.org, paulmck@kernel.org, tytso@mit.edu, steven.price@arm.com, cel@kernel.org, senozhatsky@chromium.org, yujie.liu@intel.com, gregkh@linuxfoundation.org, muchun.song@linux.dev, simon.horman@corigine.com, dlemoal@kernel.org Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, x86@kernel.org, kvm@vger.kernel.org, xen-devel@lists.xenproject.org, linux-erofs@lists.ozlabs.org, linux-f2fs-devel@lists.sourceforge.net, cluster-devel@redhat.com, linux-nfs@vger.kernel.org, linux-mtd@lists.infradead.org, rcu@vger.kernel.org, netdev@vger.kernel.org, dri-devel@lists.freedesktop.org, linux-arm-msm@vger.kernel.org, dm-devel@redhat.com, linux-raid@vger.kernel.org, linux-bcache@vger.kernel.org, virtualization@lists.linux-foundation.org, linux-fsdevel@vger.kernel.org, linux-ext4@vger.kernel.org, linux-xfs@vger.kernel.org, linux-btrfs@vger.kernel.org, Qi Zheng , Muchun Song Subject: [PATCH v4 43/48] drm/ttm: introduce pool_shrink_rwsem Date: Mon, 7 Aug 2023 19:09:31 +0800 Message-Id: <20230807110936.21819-44-zhengqi.arch@bytedance.com> X-Mailer: git-send-email 2.24.3 (Apple Git-128) In-Reply-To: <20230807110936.21819-1-zhengqi.arch@bytedance.com> References: <20230807110936.21819-1-zhengqi.arch@bytedance.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: AF4AC140003 X-Rspam-User: X-Stat-Signature: 3edfimqij61k58g7p97wt9df1ip8qnea X-Rspamd-Server: rspam03 X-HE-Tag: 1691407149-787047 X-HE-Meta: U2FsdGVkX1+Y8kSDJ14JIm0IDp5v67rYej+W/vGSwlIRD5kr232mtFpZQ+p4IHkGSuG6EIwo4oZt29Oi0urhDG+XcH/cut/UZFmmIlCFKEJ+MdoyfAl5+dxhVHfA3HtGgDA5oo/ZGmxHekdc67t37j20ua2Bg695lf99nsOqnKVsL7wtmcQ0ZADTzJnwkOu5+zht9agigTcFRjPe3A5rxhIftUpk/K6Vncswvyl3bE6vBwP17knCM5uEHbWbePRSlwbE6yNKmTDVV2i107LmttopV5vBJCNScUiAvYK7F4I01hVnUM2fZ9YXBGb7ClrPrjlD8QMK7GAkib+78d+uc6Z5KYY5ajx1E5sDta5Rhq+I/dRkxFlVTSSzywG5J/NE01d8xlL5PrvFsFb3yPpLT31Jy1R89RyfXcg074+nM50XQ1+ofDRCzDqAdvrSd9EVlHEYglNjadhAAZ10fBV/VeaFnw3coTh4Hdlnf17Jjvc09NAmOjgotAIDg5MsuVaEiworlDls42coyICRTwkglIH9S4JiK6j57MtUP8ZCFxX/WXTYjGtOEPyrAcW4iulkRPpG3v3jutlZYIKTUVKstvZXHtxu3MKIOsLtJF+gRvawFxmitN8Roq4mLnx+SCwsOe4RgNxZMPI9o315iQrg4pRiTz5ry/ixm9nXwWX9+EBpg3L+OTvZhD2U1dUl1CShh2mPMk99JO9VZaMnJWAXu++t+JPT+TuqSA/6S7+U/T+dyyDkSME17h4DIaGRmwEL685xCvywniKbWNszRqEcemvuz12Znlq2Zr3ZTSFfVz2a4FmCPf9Ue1+D8q5uJwY+h/n+LKrmm1slPLlfuXmPEq/xelPOv5Z12aih34+M/avd3eAqv2zgZ4MlOi6vasChtxTiNspdPml1VitUnvw+SjwGvzAzEhKjLP2a2BIy8jKCMN2BzWKRjHjfU3PIylWIdjntMYXgUKgcJJ77j5c 8gcbPdCj SGUPWDkFm4aJ7wgnV4BTNPy101qkrXSHnRYoBPj4lGu5yF4WpK5a+iWE7Zs/uTgGKiwYxXnjuXX5pUsVucdXah2lGNx2rFei3fK3sRQDsaVXpmpeXi/x+diZs1F4Hy26fOuxDbhgbZYIdO7mjOPJdRBo/WQdwmwxZ6EP2207Mn40YW29FL3hZrUt7FB1qUM6p4Sap6WEMCcnmxgQSXSLYHV1k4ihecvQcu4owtx9wDwWATKk5Z7v1RW6JQWdNwZre3o2/Rshy7Wxt0HYL1EgVfMVIlwoy31oXlujgh1NIQ5m5grdMXRFUBXHV8OIsSr6fGRtIZ6e9ZlOGBxz+du9naY3nAykHExuvUBWo+5NGdMTfe6ernM7UOZZ6rGS3oVF21bHRGc+y6BGgM7WSxkbCBdIaBXmXTpZ+OhN3LJOt3psgmsCvgQ8REtO4USIos2jOWscNmAlpCJyUxJUiMGQtkGzg3w== 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: Currently, the synchronize_shrinkers() is only used by TTM pool. It only requires that no shrinkers run in parallel. After we use RCU+refcount method to implement the lockless slab shrink, we can not use shrinker_rwsem or synchronize_rcu() to guarantee that all shrinker invocations have seen an update before freeing memory. So we introduce a new pool_shrink_rwsem to implement a private synchronize_shrinkers(), so as to achieve the same purpose. Signed-off-by: Qi Zheng Reviewed-by: Muchun Song Acked-by: Daniel Vetter --- drivers/gpu/drm/ttm/ttm_pool.c | 15 +++++++++++++++ include/linux/shrinker.h | 2 -- mm/shrinker.c | 15 --------------- 3 files changed, 15 insertions(+), 17 deletions(-) diff --git a/drivers/gpu/drm/ttm/ttm_pool.c b/drivers/gpu/drm/ttm/ttm_pool.c index c9c9618c0dce..38b4c280725c 100644 --- a/drivers/gpu/drm/ttm/ttm_pool.c +++ b/drivers/gpu/drm/ttm/ttm_pool.c @@ -74,6 +74,7 @@ static struct ttm_pool_type global_dma32_uncached[MAX_ORDER + 1]; static spinlock_t shrinker_lock; static struct list_head shrinker_list; static struct shrinker *mm_shrinker; +static DECLARE_RWSEM(pool_shrink_rwsem); /* 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, @@ -317,6 +318,7 @@ static unsigned int ttm_pool_shrink(void) unsigned int num_pages; struct page *p; + down_read(&pool_shrink_rwsem); spin_lock(&shrinker_lock); pt = list_first_entry(&shrinker_list, typeof(*pt), shrinker_list); list_move_tail(&pt->shrinker_list, &shrinker_list); @@ -329,6 +331,7 @@ static unsigned int ttm_pool_shrink(void) } else { num_pages = 0; } + up_read(&pool_shrink_rwsem); return num_pages; } @@ -572,6 +575,18 @@ void ttm_pool_init(struct ttm_pool *pool, struct device *dev, } EXPORT_SYMBOL(ttm_pool_init); +/** + * synchronize_shrinkers - Wait for all running shrinkers to complete. + * + * This is useful to guarantee that all shrinker invocations have seen an + * update, before freeing memory, similar to rcu. + */ +static void synchronize_shrinkers(void) +{ + down_write(&pool_shrink_rwsem); + up_write(&pool_shrink_rwsem); +} + /** * ttm_pool_fini - Cleanup a pool * diff --git a/include/linux/shrinker.h b/include/linux/shrinker.h index c55c07c3f0cb..025c8070dd86 100644 --- a/include/linux/shrinker.h +++ b/include/linux/shrinker.h @@ -103,8 +103,6 @@ struct shrinker *shrinker_alloc(unsigned int flags, const char *fmt, ...); void shrinker_register(struct shrinker *shrinker); void shrinker_free(struct shrinker *shrinker); -extern void synchronize_shrinkers(void); - #ifdef CONFIG_SHRINKER_DEBUG extern int __printf(2, 3) shrinker_debugfs_rename(struct shrinker *shrinker, const char *fmt, ...); diff --git a/mm/shrinker.c b/mm/shrinker.c index 3ab301ff122d..a27779ed3798 100644 --- a/mm/shrinker.c +++ b/mm/shrinker.c @@ -650,18 +650,3 @@ void shrinker_free(struct shrinker *shrinker) kfree(shrinker); } EXPORT_SYMBOL_GPL(shrinker_free); - -/** - * synchronize_shrinkers - Wait for all running shrinkers to complete. - * - * This is equivalent to calling unregister_shrink() and register_shrinker(), - * but atomically and with less overhead. This is useful to guarantee that all - * shrinker invocations have seen an update, before freeing memory, similar to - * rcu. - */ -void synchronize_shrinkers(void) -{ - down_write(&shrinker_rwsem); - up_write(&shrinker_rwsem); -} -EXPORT_SYMBOL(synchronize_shrinkers);