From patchwork Thu Aug 17 11:24:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Qi Zheng X-Patchwork-Id: 13356258 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 9419BC41513 for ; Thu, 17 Aug 2023 11:25:01 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1134228003C; Thu, 17 Aug 2023 07:25:01 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0C500940020; Thu, 17 Aug 2023 07:25:01 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EA66E28003C; Thu, 17 Aug 2023 07:25:00 -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 D83CB940020 for ; Thu, 17 Aug 2023 07:25:00 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id B21461CA08F for ; Thu, 17 Aug 2023 11:25:00 +0000 (UTC) X-FDA: 81133364760.29.933FE38 Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) by imf18.hostedemail.com (Postfix) with ESMTP id CF26B1C0023 for ; Thu, 17 Aug 2023 11:24:58 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=cA4f838c; dmarc=pass (policy=quarantine) header.from=bytedance.com; spf=pass (imf18.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.214.182 as permitted sender) smtp.mailfrom=zhengqi.arch@bytedance.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1692271498; a=rsa-sha256; cv=none; b=kygf0BtpGvOgHQsWL0itjTBF2pX9pGS+8MA2qD3eiYQsvYGzMF+nPdPKPb7XN38HhSCDmh ugVNCgFkZGlntnsjw1zXx+gWmrqbGPF9MhmDf/SJ97xe9JzxoHIR2fSzLG848RLGgzO7JE 6k4qHNzAKspimq0MOlAoEhyxCxHStrg= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=cA4f838c; dmarc=pass (policy=quarantine) header.from=bytedance.com; spf=pass (imf18.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.214.182 as permitted sender) smtp.mailfrom=zhengqi.arch@bytedance.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1692271498; 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=911+1tsknd0Axh08FSyo1C2XHRSPudC4DWkW5UJVRrk=; b=jPXw3jheWKH5QiROr2OCNR75B3usD2F8sigVRXdthMSUUnIUZ6yUWvwfYBMSZrNW2pqIN0 Qcaen5gNVvZtEeSp9nVkg87o9mQbrK6q3Q3+lvMj4/dG1IRPATimyIhZt2xvkcMTCG8lYX Hi17IBL1DI+f93DJffZHSbMdU0bx7Zg= Received: by mail-pl1-f182.google.com with SMTP id d9443c01a7336-1bb91c20602so13678795ad.0 for ; Thu, 17 Aug 2023 04:24:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1692271497; x=1692876297; 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=911+1tsknd0Axh08FSyo1C2XHRSPudC4DWkW5UJVRrk=; b=cA4f838cwKDCXl304yY6Bdya3sZtOAKC+Hsn8W3mZrcTRgcxiCgLsQyvfxL1jukZL9 nKr7MMZpxGVrpwvL2HsAfMZdb1P8xQBG1DppWMK8w9t56teRJ/teUPED6u3L9/uys/xX pE6WrpK4I0zbl4vH6z6dOKlI/bR24JFeBwreq4oD73+dC1NHlwxN7wwMH2HpmTViXWud XmTi1nDWRHDsKRPmHbck7NCInMFhiWEvBUww2iL0lYBMLx/f3wnp6jV0cS02Na2xFH2J b0j9WovTWwU9nWIqbktzYe2d+KJ2M3ejDKtpHkNKcXs1nw21tqUXbZcC+SpD4cHbDcP6 wTtw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692271497; x=1692876297; 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=911+1tsknd0Axh08FSyo1C2XHRSPudC4DWkW5UJVRrk=; b=aP2Pzee2Aw35V6n77LizUVinzTTBB+39vLu6SXwvZsaoCI6lI127oChHrcH9UiaAMG aZKdPjbz2ZeMcjsQHDJvb0l49nU0KZGYIf+Q09s7IRrb3v3Gnsi0XSqXqf64YVA5WFAy K3VVPTUTp9UBqgpAdxmspXOUikWkbTDzQHFXSRUBY33DC0Qqpmb55VNKag9DaQw311a1 YapwWTuSdDc3s0GkItdHKPcO3yhbjsxy1vYSbGsLzlXBRcGdBfsLirkuYFI/d6Rs4BAT qY11Kq7qXww4+gDFifSOJOBgTBSTxaZBqGyw9fzUlCgASWcSkXdqzTNwCyKY06fzxIya G+jw== X-Gm-Message-State: AOJu0Yyyenqg87lmjnm0kvGVvHmansFomMhoW8VG1Rie5gKDd2h4b770 9V6ZVVClaD0JRd1FegCNEzEUaQ== X-Google-Smtp-Source: AGHT+IEi6XCkkhZUGIIuk//7TiBrpWGPkb49JnYoPeW/vawLwjM+O4sUYu0TPRQhIOAxSsp6dmnszA== X-Received: by 2002:a17:902:db0b:b0:1b3:d8ac:8db3 with SMTP id m11-20020a170902db0b00b001b3d8ac8db3mr5165064plx.6.1692271497563; Thu, 17 Aug 2023 04:24:57 -0700 (PDT) Received: from C02DW0BEMD6R.bytedance.net ([203.208.167.146]) by smtp.gmail.com with ESMTPSA id jd22-20020a170903261600b001bde877a7casm7229309plb.264.2023.08.17.04.24.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Aug 2023 04:24:57 -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, joel@joelfernandes.org, christian.koenig@amd.com Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, dri-devel@lists.freedesktop.org, linux-fsdevel@vger.kernel.org, Qi Zheng , Muchun Song Subject: [PATCH v2 4/5] drm/ttm: introduce pool_shrink_rwsem Date: Thu, 17 Aug 2023 19:24:01 +0800 Message-Id: <20230817112402.77010-5-zhengqi.arch@bytedance.com> X-Mailer: git-send-email 2.24.3 (Apple Git-128) In-Reply-To: <20230817112402.77010-1-zhengqi.arch@bytedance.com> References: <20230817112402.77010-1-zhengqi.arch@bytedance.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: CF26B1C0023 X-Stat-Signature: wzqps5bmax1sufdxixdx6bgi86cpjjex X-HE-Tag: 1692271498-970428 X-HE-Meta: U2FsdGVkX1/bJMr4vre7q/QeLqpzInbA+4yX22JKcaeadevNxSVMknrwHI2Oc1CA8aBP2oOl/BklcyOq7MeBkqPRzuf22j82vNLetpvAG963Tid8tLivbXck/nkcAErBW4/whQeusrPLccfXZjaVW5bNim1KK4InWJ294KrrINQ+kv+Fjfp1x+jkeGqkE4NiunIBjDrJbfv1RuNap2R+z1XQ9/Hb6NQe2rvwO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N+lxbjJ7GqtAnOpDSJ+riGMaPO1S2TcpX8emvJi6RMPJieQh5CjLETln8WFkOSkTE6wYaFnQYlZ1biEGZixwDeD72L4BeC6X1Fe2/tzez9G0ea7tgDsp0Hu02wVADsuYOoSbdEiO2PCITDpxemOSnQqITg3zCICk2QWxwOSQQniD99BshSAMHf8bnMvkw== 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 ttm_pool_synchronize_shrinkers(), so as to achieve the same purpose. Signed-off-by: Qi Zheng Reviewed-by: Muchun Song Reviewed-by: Christian König --- drivers/gpu/drm/ttm/ttm_pool.c | 17 ++++++++++++++++- include/linux/shrinker.h | 1 - mm/shrinker.c | 15 --------------- 3 files changed, 16 insertions(+), 17 deletions(-) diff --git a/drivers/gpu/drm/ttm/ttm_pool.c b/drivers/gpu/drm/ttm/ttm_pool.c index cddb9151d20f..648ca70403a7 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); +/** + * ttm_pool_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 ttm_pool_synchronize_shrinkers(void) +{ + down_write(&pool_shrink_rwsem); + up_write(&pool_shrink_rwsem); +} + /** * ttm_pool_fini - Cleanup a pool * @@ -593,7 +608,7 @@ void ttm_pool_fini(struct ttm_pool *pool) /* We removed the pool types from the LRU, but we need to also make sure * that no shrinker is concurrently freeing pages from the pool. */ - synchronize_shrinkers(); + ttm_pool_synchronize_shrinkers(); } EXPORT_SYMBOL(ttm_pool_fini); diff --git a/include/linux/shrinker.h b/include/linux/shrinker.h index 8dc15aa37410..6b5843c3b827 100644 --- a/include/linux/shrinker.h +++ b/include/linux/shrinker.h @@ -103,7 +103,6 @@ extern int __printf(2, 3) register_shrinker(struct shrinker *shrinker, const char *fmt, ...); extern void unregister_shrinker(struct shrinker *shrinker); extern void free_prealloced_shrinker(struct shrinker *shrinker); -extern void synchronize_shrinkers(void); #ifdef CONFIG_SHRINKER_DEBUG extern int __printf(2, 3) shrinker_debugfs_rename(struct shrinker *shrinker, diff --git a/mm/shrinker.c b/mm/shrinker.c index 043c87ccfab4..a16cd448b924 100644 --- a/mm/shrinker.c +++ b/mm/shrinker.c @@ -692,18 +692,3 @@ void unregister_shrinker(struct shrinker *shrinker) shrinker->nr_deferred = NULL; } EXPORT_SYMBOL(unregister_shrinker); - -/** - * 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);