From patchwork Wed Sep 27 15:08:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roman Gushchin X-Patchwork-Id: 13400989 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 F2C8FE82CA7 for ; Wed, 27 Sep 2023 15:09:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 302896B0169; Wed, 27 Sep 2023 11:09:03 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2AE9A6B016B; Wed, 27 Sep 2023 11:09:03 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 176776B016C; Wed, 27 Sep 2023 11:09:03 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 08C4E6B0169 for ; Wed, 27 Sep 2023 11:09:03 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id D7DA6120A12 for ; Wed, 27 Sep 2023 15:09:02 +0000 (UTC) X-FDA: 81282710124.03.911C1C5 Received: from out-202.mta1.migadu.com (out-202.mta1.migadu.com [95.215.58.202]) by imf20.hostedemail.com (Postfix) with ESMTP id 2248F1C0040 for ; Wed, 27 Sep 2023 15:09:00 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=pNocy6o9; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf20.hostedemail.com: domain of roman.gushchin@linux.dev designates 95.215.58.202 as permitted sender) smtp.mailfrom=roman.gushchin@linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1695827341; 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=NJPTphOT9Ah3iL7MZ9KbERD0ROIs5HQQzvQjuLNJnns=; b=Q8bsOH/LzBFdIhYlDaGnkobvze2vROtTq1sO3TNTHeKwZNYo3aFLlM4oJoHKT+xii6TKOP t4+r8qpZTrSWyh92BsMrNoJBo/7yx4JsQoKPRDL8diaXyiVMi0NieDQcEpo+AkBh0xRRfm UA1ppKhd9eYLNsOdyOs4xbwRZqe8UTs= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=pNocy6o9; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf20.hostedemail.com: domain of roman.gushchin@linux.dev designates 95.215.58.202 as permitted sender) smtp.mailfrom=roman.gushchin@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1695827341; a=rsa-sha256; cv=none; b=S0fxoSSQkLiFdqexX6/ZikmW9t9qA5yGdd//Uhzs2noh7rxu76GnZ1KgWspSmK254Ciuwo +NJVfIbNP8K4u20M4vgDz1tKZJTFOVI59wNYA3FUsICZm7gfbZ6rp9YFmSWYIOJihi1Yzs rz7bs5EvFKeNFuYq1Aj1XM1wvv3bVL8= 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=1695827339; 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=NJPTphOT9Ah3iL7MZ9KbERD0ROIs5HQQzvQjuLNJnns=; b=pNocy6o9sigu+gpBp2nZU0nrp9UmQjhqPdMh8hND8MX7ixqhRzLWJmdV9/s/k8Rs1A5Qa5 6IMESlh4qTyehhfmr0rDc9in0xsrFhOJIDRwOsOUFW2sCFbjw/rB+qADTL7a3cw1BDr2Zq iq5OmF6L+8t6+ySa0+DaftZUm1opJI4= 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 , Roman Gushchin Subject: [PATCH rfc 3/5] mm: kmem: make memcg keep a reference to the original objcg Date: Wed, 27 Sep 2023 08:08:30 -0700 Message-ID: <20230927150832.335132-4-roman.gushchin@linux.dev> In-Reply-To: <20230927150832.335132-1-roman.gushchin@linux.dev> References: <20230927150832.335132-1-roman.gushchin@linux.dev> MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: 2248F1C0040 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: o65qc1xuiwhupigpya3e4jk7w6t15ufu X-HE-Tag: 1695827340-109954 X-HE-Meta: U2FsdGVkX1+insPqRi9AQuhP/wXUZY9wzuF2oJyTn8wCT1F0erbBXeBWnGlwkL8hsG/sa0nChLR5AznNaYQhcmRg7lK7htzZVzoOTpwjJww01cQHTBuiZQ3ej66YHayGuT5ngRcRGR7D5AclXvXrbZYCX5RkWxu3WsapPfWsF6Hv117VzbzYdd0oaWptTMJTlRh/X824mInDD9QW9ksagWmLeUs/yHBfPeIiXiONGO4YmvmvIErvP17jeFWICJ0GPC90/02R7NClowT7FFSVjLR/rjigd18N+hm6FtK+7EUz6DJ/3nMMPjYVAjgCyLjR8vc8/2wyDIcoWmhhx1mklkVZXKnf6JgTbL7T88SyjFn3i0k/aTuaUA1oGq2XbqzLZA6YCYrNgeMbXqKohxmA6zXizF46y/LEHDQZS1pOXyPJgl/sXHr5yO1+yyQ5Wf3uBjZDd6+uNswvkz4BfkLELCr/NtwzgIJrPVK9F4Rwo9xdSC5zLpjYlDszzMHLMKGnX+tEWQOt7ZGAkFveiHkIzuTFrkPGBDkzckB9XkQdtUhUTM4x8a1nGIxsqqn+nGPa+2idAf/yMjemYZtNZp1dg6ivsI2g+eFbS1i0necbA+VBBxkn59k8i4xDk2BFgJJOxYoAmZZaetyFU073GNlMzq8r35KTUw48dkGJYP+2pt8i2tIlyOwIrC8IGka0wHR8eF/dFgg5npXfPxqi2wCluZybOYZJC4f4spKNUCmPbchDU5CdcvTx9BjrTCjCH3ky3m8BT8F6lbWyyy7zIPcHd2sp5xWXDWLJSOSQSLK3hw8bTH1/UXFUh9IGazHSu+ED4CtER+PA05cvTPoN6viTg7H5Sv2l+anTSN6tmBYBmgzqkeQ53ED4kzRpJ1BqeMhfE/KPmw5N5FY1dSfeSe6Gs+iUnKZnZMSMAc2+46ZSBJKomQmF2LLlsRx0XZzA8fcQlaq8h6wpezMQ0k18BHD Yt/1NAvJ GFaZO5vD6T4tBaC5f7INq+W/G+LHwgMm9EZJkoxmKnCkyK2Ymb1B3cilbTe7zrwTgzdoxdbMnBJUSW00= 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) --- 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 84425bfe4124..412ff0e8694d 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 7f33a503d600..4815f897758c 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -3803,6 +3803,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); @@ -5297,6 +5299,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);