From patchwork Mon Nov 4 17:52:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kairui Song X-Patchwork-Id: 13861759 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 BA7ABD1BDD4 for ; Mon, 4 Nov 2024 17:55:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 449836B0096; Mon, 4 Nov 2024 12:55:12 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3FA046B0098; Mon, 4 Nov 2024 12:55:12 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 29A336B0099; Mon, 4 Nov 2024 12:55:12 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 0C6866B0096 for ; Mon, 4 Nov 2024 12:55:12 -0500 (EST) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id B4ADCAC238 for ; Mon, 4 Nov 2024 17:55:11 +0000 (UTC) X-FDA: 82749162426.13.D986EC1 Received: from mail-pf1-f180.google.com (mail-pf1-f180.google.com [209.85.210.180]) by imf30.hostedemail.com (Postfix) with ESMTP id 10CD080011 for ; Mon, 4 Nov 2024 17:54:08 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=h8BWhuhy; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf30.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.210.180 as permitted sender) smtp.mailfrom=ryncsn@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730742851; a=rsa-sha256; cv=none; b=XZxNkcuq49ff8co84DV/jzik5wrifYp8WoOS1gFWNOksi/cV2shMjJ+od/oB/0Afm/fFvS DiChzvqqOaU5K1GSeBKjzoNd6B9anCLZ7tR4rQVTF5gLywxzIoassBdhI8Wfk/X/zRvdhw h0II08ZkVOYYl3pfvpvH9wEWCXsVozA= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=h8BWhuhy; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf30.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.210.180 as permitted sender) smtp.mailfrom=ryncsn@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1730742851; h=from:from:sender:reply-to: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=iL9IvtjIkFQSQS96IExA8P6PV6Z5oBDu+RSPWvdKwUw=; b=VUutWM70C+tknrq72p5H38/1HNMKdyFJv6iYgD/ZH5ydaUFGwPsGDTVi9ZrJI+FjGg4Qq7 E+gB7/qu8fgMbpjYN52iZEwSRvWRuhl0zUArVNrFOqXSeSCE3nDEIH5Suy25GLW2iD/ZxN wkSUIsSAXLGkcx/FxVJWhsOoHVcE++E= Received: by mail-pf1-f180.google.com with SMTP id d2e1a72fcca58-720be27db27so3610959b3a.2 for ; Mon, 04 Nov 2024 09:55:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1730742908; x=1731347708; darn=kvack.org; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:from:to:cc:subject :date:message-id:reply-to; bh=iL9IvtjIkFQSQS96IExA8P6PV6Z5oBDu+RSPWvdKwUw=; b=h8BWhuhy4FSbsRl57yki79iHbMvmphFuZT7e6V5qbzyo15iKP5xQ1hSbNyJDCKj0Br CqOo4XY5foZViV7ojrBTCO8wQ6RMtKn1svKSpKQBRbpfmIc5HSiqYRnH99sZfAaZgPv3 mM9mwwE0v1+nacpebN4UGZuZLLMA85kZ5ldaSYFF7CgS5F2A1KXUaCsVQ/jJn//YDxCf zjl5BS1ziWaDwRi4rcCBaZZR7/JD9RwQmueWOMK7UZHde4Q79Q07BnI4Y7XUaU4dgxOg Ga/nYl66zmnRIseYGi0rWqzKuEspjA87ZwdCTuKq9o9jvPE/AziuHWGGyVXwABmY1X0u 0d3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730742908; x=1731347708; h=content-transfer-encoding:mime-version:reply-to: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=iL9IvtjIkFQSQS96IExA8P6PV6Z5oBDu+RSPWvdKwUw=; b=TspsGmA0KJeXp/Hy9fQBwXl77fznGQ05eIm2idYd4pAPspSz4oV41awaU1nFSFfTMA TddRQbSo63PfKKNsHxGPQUXdEr0XXU7ZcJeGZP/j6iH64G1ZvpLy0Yz7zpqKhwgW1fRu go6CMgaa/euaVG5coLqLjwFfdHPRxui8M9ALY5wZfJesFJoCatzCEToYFUvze1WXzJhQ ZU58XYNxV5Ppm4KEEppPmkYMKLoLn6NG3l2ofZ+PHTfwo1SF9Rc23LA+AndsYMzkzG/W tiicpbOpfKKXok7fO+fDSPgXsOghK8OMkpULru6GklcAmavszrRSqtJ0XZIAFRWbJxxC 0dnQ== X-Gm-Message-State: AOJu0YwaM16coqMOnVvN/7DX/NgWcD7bzCSZ0NGSuzumi+J2A+fgufG9 rxcV0VmMLo0IdnDccFrhlJmzU7RECdAnH0/xDwna7TrJzLapwOxc3E+oN2eS6E8= X-Google-Smtp-Source: AGHT+IFcJ2lqm5wdX7MS5DNufJZ3E89XUP2EPJ1Z5Isz/XIavZ/J0/34TdR7wCXBdSTpRahIjuGfzA== X-Received: by 2002:a05:6a00:2191:b0:71e:44f6:6900 with SMTP id d2e1a72fcca58-720ab3f39d8mr28676270b3a.16.1730742907924; Mon, 04 Nov 2024 09:55:07 -0800 (PST) Received: from KASONG-MC4.tencent.com ([106.37.121.39]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-720bc1e5f5bsm7777039b3a.55.2024.11.04.09.55.03 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 04 Nov 2024 09:55:07 -0800 (PST) From: Kairui Song To: linux-mm@kvack.org Cc: Andrew Morton , Matthew Wilcox , Johannes Weiner , Roman Gushchin , Waiman Long , Shakeel Butt , Michal Hocko , Chengming Zhou , Qi Zheng , Muchun Song , Kairui Song Subject: [PATCH v3 3/6] mm/list_lru: code clean up for reparenting Date: Tue, 5 Nov 2024 01:52:54 +0800 Message-ID: <20241104175257.60853-4-ryncsn@gmail.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241104175257.60853-1-ryncsn@gmail.com> References: <20241104175257.60853-1-ryncsn@gmail.com> Reply-To: Kairui Song MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: 10CD080011 X-Rspamd-Server: rspam01 X-Stat-Signature: gyjxrxk6japa8bqds7khcoxexmx6sbox X-HE-Tag: 1730742848-640512 X-HE-Meta: U2FsdGVkX1//E1CS4CMoI8xQti2oX+SvYZqz1hoXoGbOHbbjuEr9wa7JDEqzCV4SOEXARGlV1ZyWVw+ozLqA+TXHPkX6q3LlNkJGgwrMKbxO423v9y5hZxNKmAC8Mr0QT+JhdjgLiCT3giiaXyOxIVzbSAnTG7WKI/nvfSmKLhH2WjlgmIunMrA1rM8O/qNkGqCs9/w88SMRV29JXY7PxX0thl35JftSxEIFq9PnR7V5T8jTdnaI5pPQhnzmsmURZYdWfaAiyvgbpzelV8aTfaiemA7pES1Agww32q3rQl3WS1PpBalnpflyNFaFjrFfkFUuILpuzEZMfMMjqaWkEkZY23jmG9V67J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/+0PT2UQn6SRsqxieuxor050LxAcdBhLQBvTe0FYqg5B6YNiZ8pfwUdy88A26DPZblRZGkco/KqGlJ9JG9v8kYOGuVgWjgfJDco3kp+YIlAnmMtCKKb/bRskxWuuYRnJj1XxmlharzcWt1DJtshQDN4vJxX3N0F4jMR/iuUPo9G2xkSamkFixklgYI= 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: List-Subscribe: List-Unsubscribe: From: Kairui Song No feature change, just change of code structure and fix comment. The list lrus are not empty until memcg_reparent_list_lru_node() calls are all done, so the comments in memcg_offline_kmem were slightly inaccurate. Signed-off-by: Kairui Song Reviewed-by: Muchun Song Acked-by: Shakeel Butt --- mm/list_lru.c | 39 +++++++++++++++++---------------------- mm/memcontrol.c | 7 ------- 2 files changed, 17 insertions(+), 29 deletions(-) diff --git a/mm/list_lru.c b/mm/list_lru.c index a798e7624f69..b54f092d4d65 100644 --- a/mm/list_lru.c +++ b/mm/list_lru.c @@ -421,35 +421,16 @@ static void memcg_reparent_list_lru_node(struct list_lru *lru, int nid, spin_unlock_irq(&nlru->lock); } -static void memcg_reparent_list_lru(struct list_lru *lru, - int src_idx, struct mem_cgroup *dst_memcg) -{ - int i; - - for_each_node(i) - memcg_reparent_list_lru_node(lru, i, src_idx, dst_memcg); - - memcg_list_lru_free(lru, src_idx); -} - void memcg_reparent_list_lrus(struct mem_cgroup *memcg, struct mem_cgroup *parent) { struct cgroup_subsys_state *css; struct list_lru *lru; - int src_idx = memcg->kmemcg_id; + int src_idx = memcg->kmemcg_id, i; /* * Change kmemcg_id of this cgroup and all its descendants to the * parent's id, and then move all entries from this cgroup's list_lrus * to ones of the parent. - * - * After we have finished, all list_lrus corresponding to this cgroup - * are guaranteed to remain empty. So we can safely free this cgroup's - * list lrus in memcg_list_lru_free(). - * - * Changing ->kmemcg_id to the parent can prevent memcg_list_lru_alloc() - * from allocating list lrus for this cgroup after memcg_list_lru_free() - * call. */ rcu_read_lock(); css_for_each_descendant_pre(css, &memcg->css) { @@ -460,9 +441,23 @@ void memcg_reparent_list_lrus(struct mem_cgroup *memcg, struct mem_cgroup *paren } rcu_read_unlock(); + /* + * With kmemcg_id set to parent, holding the lock of each list_lru_node + * below can prevent list_lru_{add,del,isolate} from touching the lru, + * safe to reparent. + */ mutex_lock(&list_lrus_mutex); - list_for_each_entry(lru, &memcg_list_lrus, list) - memcg_reparent_list_lru(lru, src_idx, parent); + list_for_each_entry(lru, &memcg_list_lrus, list) { + for_each_node(i) + memcg_reparent_list_lru_node(lru, i, src_idx, parent); + + /* + * Here all list_lrus corresponding to the cgroup are guaranteed + * to remain empty, we can safely free this lru, any further + * memcg_list_lru_alloc() call will simply bail out. + */ + memcg_list_lru_free(lru, src_idx); + } mutex_unlock(&list_lrus_mutex); } diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 7845c64a2c57..8e90aa026c47 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -3099,13 +3099,6 @@ static void memcg_offline_kmem(struct mem_cgroup *memcg) parent = root_mem_cgroup; memcg_reparent_objcgs(memcg, parent); - - /* - * After we have finished memcg_reparent_objcgs(), all list_lrus - * corresponding to this cgroup are guaranteed to remain empty. - * The ordering is imposed by list_lru_node->lock taken by - * memcg_reparent_list_lrus(). - */ memcg_reparent_list_lrus(memcg, parent); }