From patchwork Tue Mar 7 06:56:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qi Zheng X-Patchwork-Id: 13162869 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 1A2B9C678DB for ; Tue, 7 Mar 2023 06:56:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 901EF6B0078; Tue, 7 Mar 2023 01:56:54 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8B3566B007B; Tue, 7 Mar 2023 01:56:54 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 752FA6B007D; Tue, 7 Mar 2023 01:56:54 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 660BB6B0078 for ; Tue, 7 Mar 2023 01:56:54 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 2F63CA09BD for ; Tue, 7 Mar 2023 06:56:54 +0000 (UTC) X-FDA: 80541194748.25.E95ABF0 Received: from mail-pg1-f170.google.com (mail-pg1-f170.google.com [209.85.215.170]) by imf09.hostedemail.com (Postfix) with ESMTP id 6AFB3140011 for ; Tue, 7 Mar 2023 06:56:52 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=j8xEkJaY; spf=pass (imf09.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.215.170 as permitted sender) smtp.mailfrom=zhengqi.arch@bytedance.com; dmarc=pass (policy=none) header.from=bytedance.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1678172212; a=rsa-sha256; cv=none; b=rps57CaVR/DVSgRRRL6uioG0htF1aOl7Y3Pnw27+HozjpdmJFhdYZqs55e3ulsax89FgR7 B309hIgWd2u/j1b9DObdDVn2VDxSZZLnLqmQXS6bOuD8l71V/n1WVybkjKEb/o3ll0yiHA 0dtzSrdCu0PSb3RVDsWabARK2mQ+sSY= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=j8xEkJaY; spf=pass (imf09.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.215.170 as permitted sender) smtp.mailfrom=zhengqi.arch@bytedance.com; dmarc=pass (policy=none) header.from=bytedance.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1678172212; 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=hlfbC5hLUOyZ5eiKWKe3FNliR+/5Zth66owFIX3duxQ=; b=t+qM2SX0KBo3d9ft4YSEu9RhUHyJW0H9RNR9wk8v3HKYxGXNXDp0+ARrK+kbL6mMp37JP/ m/6SUbP6e9+4XxfNNMDx+XIdGPfChM/5CkqSVCeKnITNI5d414b2NqBXBEoEczBFQtMl7V 4IkfWyUFoVln0SxZUQ0E14mXSUhRQTw= Received: by mail-pg1-f170.google.com with SMTP id 132so6987438pgh.13 for ; Mon, 06 Mar 2023 22:56:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1678172211; 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=hlfbC5hLUOyZ5eiKWKe3FNliR+/5Zth66owFIX3duxQ=; b=j8xEkJaYof6JnjV2P0RsveJBbsnpcn9RY4V+ifKMT79Be93zvpgebNY5nSfWdFD0NU 1JW1AYTHaO+N+RFx2VLXc9tAFRn/DsFZYM5L65TiSMr1hMRgmru3xvORasp/75YD6J/y rGn2vVrH8HJoacjH3PbF9GnOpdyo3gX0mQEUZgRXnTBovRgY8MVxTw8BI9uz52Q/LQHG E2dcdZEf3BV3Hh0v3X7btzqm0okyXgWthyJJzVeextpZ9F1g0oAYjtHaaqZ/iEWplil2 10Sb5xodkY2XripisufbYdb8ro/zRPnyPWGLDA9/6bcUB7x768fm0jxge+41ve9sBsI8 3UMQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678172211; 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=hlfbC5hLUOyZ5eiKWKe3FNliR+/5Zth66owFIX3duxQ=; b=emGMswa8tKOkusl3M+KkmiJ8dty+UlexHot+kd09uEy2rCUbi//r3t7OHaAOnIwRHm 8TcAEo1lcDWoV3drJrb1j8Djh+bxJFcOvmTIjLNpA3CUZ3mNJLk4MdsOXrmquqV4B8wR +uEXGEi1tFYCLN7/HBQTBNCn8tNigb0J5a2okVYu3egzz4fHiuoD771cpugzV4/844dC saoST2CwhUflDCO64hDUPQCTHf6hCyUjsf44nUR5ZsJD2VosUDefomEmZqmxZwtP78tI 9Y9uOybflr2EG/PD3zHJtFcQNxvSZCahbGFTw3/uDyzYc6Fv2UbWvnZMgv4e8c4Ws+tI ynog== X-Gm-Message-State: AO0yUKWh+UOfCwwPKVnBIFrC8ohku/N3NLyGuj+5dkU/KNoiswVLt3EP kQU4EaCcx+ILU5zNaNfRUxLEig== X-Google-Smtp-Source: AK7set9MiEUf3QenLOBMQ3cdZKmZL5CffIDhT1D/uEa1g/R5V5cbMebfmciE6mPU6pFS2nArJyvZ4A== X-Received: by 2002:a05:6a00:4087:b0:5d1:f76:d1d7 with SMTP id bw7-20020a056a00408700b005d10f76d1d7mr14022848pfb.1.1678172211105; Mon, 06 Mar 2023 22:56:51 -0800 (PST) Received: from C02DW0BEMD6R.bytedance.net ([139.177.225.245]) by smtp.gmail.com with ESMTPSA id e5-20020a62ee05000000b00608dae58695sm7230854pfi.209.2023.03.06.22.56.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Mar 2023 22:56:50 -0800 (PST) From: Qi Zheng To: akpm@linux-foundation.org, tkhai@ya.ru, hannes@cmpxchg.org, shakeelb@google.com, mhocko@kernel.org, roman.gushchin@linux.dev, muchun.song@linux.dev, david@redhat.com, shy828301@gmail.com, rppt@kernel.org Cc: sultan@kerneltoast.com, dave@stgolabs.net, penguin-kernel@I-love.SAKURA.ne.jp, paulmck@kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Qi Zheng Subject: [PATCH v4 4/8] mm: vmscan: add shrinker_srcu_generation Date: Tue, 7 Mar 2023 14:56:01 +0800 Message-Id: <20230307065605.58209-5-zhengqi.arch@bytedance.com> X-Mailer: git-send-email 2.24.3 (Apple Git-128) In-Reply-To: <20230307065605.58209-1-zhengqi.arch@bytedance.com> References: <20230307065605.58209-1-zhengqi.arch@bytedance.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: 6AFB3140011 X-Rspamd-Server: rspam01 X-Stat-Signature: tzjx17nfg9y8k3wxj91wrerns5wy33ik X-HE-Tag: 1678172212-693236 X-HE-Meta: U2FsdGVkX18U9xAg/4/582HTKRUbMkGrYyPIOR/FpS4QJ2xm6BzymIDMXMsL5FWJHPhmsAbaAleLOfR/6yLQP0WU+CGhL9T6OtFwsU295bZbuzhk4MJE1LB8ivKN8HNHMnc9iq0tbxG+rpRfTYTA9VzbA9MyiHo2clFhtwq1XlJknBaP9GzihcPrQ+Rev9Bl048woR9M9FHbOGZIGnVg3nD0tK5yuerUtojLdrtTTV7slZi21m4tb2qxf/hpTPM8bbIKDKLibAB4h2vYSMCU8aTcRNduTOkam71qyhQTuV+F1NlbndOVgZ0OC0mB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GQc7tNvwuBwF55znYBmZy3rcThp3fOpkDvAs2Txfj4ynqMDkP1WnienxhSGgMXnD8C3wwmBrWsw6zWmWYOjv4jMF0pTzpivHkNRJCEd0U+873mUn8wFKpYkQoVWpyy3UIA== 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: From: Kirill Tkhai After we make slab shrink lockless with SRCU, the longest sleep unregister_shrinker() will be a sleep waiting for all do_shrink_slab() calls. To aviod long unbreakable action in the unregister_shrinker(), add shrinker_srcu_generation to restore a check similar to the rwsem_is_contendent() check that we had before. And for memcg slab shrink, we unlock SRCU and continue iterations from the next shrinker id. Signed-off-by: Kirill Tkhai Signed-off-by: Qi Zheng Acked-by: Vlastimil Babka --- mm/vmscan.c | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/mm/vmscan.c b/mm/vmscan.c index 1de9bc3e5aa2..9a5a3da5c8b5 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -204,6 +204,7 @@ static void set_task_reclaim_state(struct task_struct *task, LIST_HEAD(shrinker_list); DECLARE_RWSEM(shrinker_rwsem); DEFINE_SRCU(shrinker_srcu); +static atomic_t shrinker_srcu_generation = ATOMIC_INIT(0); #ifdef CONFIG_MEMCG static int shrinker_nr_max; @@ -782,6 +783,7 @@ void unregister_shrinker(struct shrinker *shrinker) debugfs_entry = shrinker_debugfs_remove(shrinker); up_write(&shrinker_rwsem); + atomic_inc(&shrinker_srcu_generation); synchronize_srcu(&shrinker_srcu); debugfs_remove_recursive(debugfs_entry); @@ -803,6 +805,7 @@ void synchronize_shrinkers(void) { down_write(&shrinker_rwsem); up_write(&shrinker_rwsem); + atomic_inc(&shrinker_srcu_generation); synchronize_srcu(&shrinker_srcu); } EXPORT_SYMBOL(synchronize_shrinkers); @@ -912,18 +915,20 @@ static unsigned long shrink_slab_memcg(gfp_t gfp_mask, int nid, { struct shrinker_info *info; unsigned long ret, freed = 0; - int srcu_idx; - int i; + int srcu_idx, generation; + int i = 0; if (!mem_cgroup_online(memcg)) return 0; +again: srcu_idx = srcu_read_lock(&shrinker_srcu); info = shrinker_info_srcu(memcg, nid); if (unlikely(!info)) goto unlock; - for_each_set_bit(i, info->map, info->map_nr_max) { + generation = atomic_read(&shrinker_srcu_generation); + for_each_set_bit_from(i, info->map, info->map_nr_max) { struct shrink_control sc = { .gfp_mask = gfp_mask, .nid = nid, @@ -969,6 +974,11 @@ static unsigned long shrink_slab_memcg(gfp_t gfp_mask, int nid, set_shrinker_bit(memcg, nid, i); } freed += ret; + if (atomic_read(&shrinker_srcu_generation) != generation) { + srcu_read_unlock(&shrinker_srcu, srcu_idx); + i++; + goto again; + } } unlock: srcu_read_unlock(&shrinker_srcu, srcu_idx); @@ -1008,7 +1018,7 @@ static unsigned long shrink_slab(gfp_t gfp_mask, int nid, { unsigned long ret, freed = 0; struct shrinker *shrinker; - int srcu_idx; + int srcu_idx, generation; /* * The root memcg might be allocated even though memcg is disabled @@ -1022,6 +1032,7 @@ static unsigned long shrink_slab(gfp_t gfp_mask, int nid, srcu_idx = srcu_read_lock(&shrinker_srcu); + generation = atomic_read(&shrinker_srcu_generation); list_for_each_entry_srcu(shrinker, &shrinker_list, list, srcu_read_lock_held(&shrinker_srcu)) { struct shrink_control sc = { @@ -1034,6 +1045,11 @@ static unsigned long shrink_slab(gfp_t gfp_mask, int nid, if (ret == SHRINK_EMPTY) ret = 0; freed += ret; + + if (atomic_read(&shrinker_srcu_generation) != generation) { + freed = freed ? : 1; + break; + } } srcu_read_unlock(&shrinker_srcu, srcu_idx);