From patchwork Tue Oct 10 00:09:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roman Gushchin X-Patchwork-Id: 13414697 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 8730FE95A97 for ; Tue, 10 Oct 2023 00:10:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3AFB18D00A0; Mon, 9 Oct 2023 20:10:08 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 338878D0089; Mon, 9 Oct 2023 20:10:08 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1637E8D00A0; Mon, 9 Oct 2023 20:10:08 -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 F3C028D0089 for ; Mon, 9 Oct 2023 20:10:07 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id CAB17140451 for ; Tue, 10 Oct 2023 00:10:07 +0000 (UTC) X-FDA: 81327619254.24.320A4A7 Received: from out-197.mta0.migadu.com (out-197.mta0.migadu.com [91.218.175.197]) by imf23.hostedemail.com (Postfix) with ESMTP id 17AF8140015 for ; Tue, 10 Oct 2023 00:10:05 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=qLsl13mT; spf=pass (imf23.hostedemail.com: domain of roman.gushchin@linux.dev designates 91.218.175.197 as permitted sender) smtp.mailfrom=roman.gushchin@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1696896606; 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=jfnE2/YTK2KTtvwJ+nJA5/HU7PGBa8pUnySPUFyhBIQ=; b=TJjcd/wewG9xijQ9jMUiKXqOf4bG7RhA3wM9U9YLKMMr13RcYBXpliKX5p2bzgRDZpkh1e 3W3n1TXn1xEiKqm3xap00pcoQroTiD4hV2QALCjRryA0JDJ3OlbzbITuREVwVdHtM7lJJA iWpp5mZmRDMPFgmwK4lglYvL1jLrK08= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=qLsl13mT; spf=pass (imf23.hostedemail.com: domain of roman.gushchin@linux.dev designates 91.218.175.197 as permitted sender) smtp.mailfrom=roman.gushchin@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1696896606; a=rsa-sha256; cv=none; b=W/ptSWQdY4JfXd86F6aa11kDtlfHTtbjNdXD+kBaGzETjYGhECAoM2C3Pe0P2EysSfRbNE MUCTOenfX9KF7bVRRmUL5xe1CmsV3Ih2vulEdnH+EvTiBrjrPlOswaNGunJ3BsG5xc8EGL i9eADYQpOEmkhtdg+NZ1P9vAOIwnwQc= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1696896604; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=jfnE2/YTK2KTtvwJ+nJA5/HU7PGBa8pUnySPUFyhBIQ=; b=qLsl13mTbXz/4i7wT4UNbU/Yuhrs6p0pMqUkgW5cQutUavEUX53hOM0UdEJeTs+zo2p5sJ GQojwhgKw9URgh6Cnj8JskC/4by4Jp7qKD398cvjsd/sOFJjD43axUombC6Fgh4TUL5CuQ PNw7ZFBmb9QNHRWuBYOV43b0EnZyUDM= From: Roman Gushchin To: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, Johannes Weiner , Michal Hocko , Shakeel Butt , Muchun Song , Dennis Zhou , Andrew Morton , David Rientjes , Vlastimil Babka , Naresh Kamboju , Roman Gushchin Subject: [PATCH v2 3/5] mm: kmem: make memcg keep a reference to the original objcg Date: Mon, 9 Oct 2023 17:09:27 -0700 Message-ID: <20231010000929.450702-4-roman.gushchin@linux.dev> In-Reply-To: <20231010000929.450702-1-roman.gushchin@linux.dev> References: <20231010000929.450702-1-roman.gushchin@linux.dev> MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: 17AF8140015 X-Rspam-User: X-Stat-Signature: aqwzh44yi7dciz6xmjbysqayar51orkq X-Rspamd-Server: rspam01 X-HE-Tag: 1696896605-923645 X-HE-Meta: U2FsdGVkX1+nRUTyR9U6TDkjOAyOPHWy+vzh77fPoAeYDppcQMZgb3KXfauWfJSosos+6Ya51yabw7YAbMKguvZIdgZDO5M+mL19VAPdV114ByRQFxJ1ONOXmv0f/FzvoEy0Gw9J70z41hhpZbw68d8XImpxdCA22jiepVHzwuwUVipERQKyPXexSul3Gelh/w65GhSKRCe2fjqN1v0LH7F+mIUyEyYswy4aV9SNyJ//nJqBcsCK9Et1YaVSPIUTFkA9MKq3YPRBvwmRcVWTBk92z0yTysw1u+qvZT2LtcULuyjCFoKZLR+gLInsIKa8grYfDObtPysNWj5iJrtUTZ581WT3jMDG9OHmuz5K+j5zk7LLIPshlqpXV7ddrsnHlWjMmWayi0GRVlKWlQRzfiT8AP2feSIPgc5wWU+qCfysfuApycLJVDAvkr7gIZY5qzBeL5DqVxgwvtypiOciMa42r6HLfper9jqpgfGn65akbfo2sPlsMALIUWokIzNoASeHLKmUMpgrNTcj0E49reYyLiafb0kVTcmw5bETg2Y09mwu6as27yFuAEshTylxqm6ONja46E7UwU41ld4ILm5szlaJk+x17UzackRkzebWeVygWr/6owdivZsNGkgG8F6ZGH8TuqwvmODTc09Fw2wr1o+rAUohI2BF3w/om9URB2khEOwYOO6PkRIwFQ1OUeaopwdG/7LxKMt54fVlDV4kBQtOnN3nC8I07cII3JIsz+YkSGwT72ALru/86r9Vvrff11nipELg+cWaz+2Drvbywv7zJD31MHqY8+QyFqoY3GleeyS5W6ZSTFBwFvqlfbzfknuOqLwl1EKi0PGJoZcdAShLViF565kceHSfAka3TgkU9k/dB/J8zHkYxM4DISvWHjJY7SYuuihk0u3XssXeRv0gXJuLoq/897mizuV5tV9b6ko8ta57DlyHOMfe/BYUpmU7sf/p16ZO0Aj SyAY9DUi jKPveUq8+Hpt1Xuq4BAP9Mj6Vf6zvK7g2Lj7+orzR2gtCT/2JKiB11jpFKl8g9KNEyPMp4ANsh+7r2ww= 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: Keep a reference to the original objcg object for the entire life of a memcg structure. This allows to simplify the synchronization on the kernel memory allocation paths: pinning a (live) memcg will also pin the corresponding objcg. The memory overhead of this change is minimal because object cgroups usually outlive their corresponding memory cgroups even without this change, so it's only an additional pointer per memcg. Signed-off-by: Roman Gushchin (Cruise) Acked-by: Shakeel Butt --- include/linux/memcontrol.h | 8 +++++++- mm/memcontrol.c | 5 +++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index ab94ad4597d0..277690af383d 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -299,7 +299,13 @@ struct mem_cgroup { #ifdef CONFIG_MEMCG_KMEM int kmemcg_id; - struct obj_cgroup __rcu *objcg; + /* + * memcg->objcg is wiped out as a part of the objcg repaprenting + * process. memcg->orig_objcg preserves a pointer (and a reference) + * to the original objcg until the end of live of memcg. + */ + struct obj_cgroup __rcu *objcg; + struct obj_cgroup *orig_objcg; /* list of inherited objcgs, protected by objcg_lock */ struct list_head objcg_list; #endif diff --git a/mm/memcontrol.c b/mm/memcontrol.c index a2efeea43058..490c5f67be79 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -3808,6 +3808,8 @@ static int memcg_online_kmem(struct mem_cgroup *memcg) objcg->memcg = memcg; rcu_assign_pointer(memcg->objcg, objcg); + obj_cgroup_get(objcg); + memcg->orig_objcg = objcg; static_branch_enable(&memcg_kmem_online_key); @@ -5302,6 +5304,9 @@ static void __mem_cgroup_free(struct mem_cgroup *memcg) { int node; + if (memcg->orig_objcg) + obj_cgroup_put(memcg->orig_objcg); + for_each_node(node) free_mem_cgroup_per_node_info(memcg, node); kfree(memcg->vmstats);