Received: by 2002:a05:7412:da14:b0:e2:908c:2ebd with SMTP id fe20csp2208616rdb; Mon, 9 Oct 2023 17:16:27 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFh2eoj+QGfsogMY7r7I+QNRF6b5UqgA2iVbx3ZdFvhk9br4dv61dx7yPez8YsqBmAd3sU/ X-Received: by 2002:a17:903:11d2:b0:1c7:2697:ec0a with SMTP id q18-20020a17090311d200b001c72697ec0amr18448980plh.30.1696896987638; Mon, 09 Oct 2023 17:16:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696896987; cv=none; d=google.com; s=arc-20160816; b=TN46w/QHwN5UWy2MJQ0k2bQ0dx6AUgfOB1GW5/LolmpyT+GcfeY1wahs7vrSsFwcB4 /QX4bQtXVg/OsIN1SekoMWy5IxhtnoVfVvkIyuE9hNn8DVbv0u43ctcqZVnR8J6IHyw7 me8dmm0ChaiaAFd19NMl2WcuuNvacLK75f7Cu6K8MIBVNEpdCOHXI2XClsYPX/thYzm8 XHDw9Z4hzG3n6IfnHWLQgslR47mi7v5T9ncc4PdsMFGObrBcwuUu+CjzFcXjhiQThmv1 wsPqBHGyYfCqaVyfHVb15YBX5PrcPRRIccJEV7ujywrR6fZp+vJyHGZXx66CtXcpcxFc tUAQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=jfnE2/YTK2KTtvwJ+nJA5/HU7PGBa8pUnySPUFyhBIQ=; fh=eIV/wktpjV+EeUDLeXhmhAmcuYEtVvgOWNik/7Ld4iQ=; b=G4eTs+Exme2QPP15OIMOvYlp3DjzeqXQNZi0iVoCzboQwA1Hsm2aOkpiFZ6CC90c8w UW3do+mmu9JbYy5dTGh5sm3KCqIDH0Kqb5TyR1gnBQL0FpBjcBc6VpNLHQ5ibQLdNSP4 04Y9DJc0jUlsyEJ7wIogf0iQaXkmaXJ/D608Cc4pPWdF+Xo4hsLdVtxGleljhsXQhrII lot5unl04qqCeLwPZlQlxBf0aXT3cDNEqvqjxdOLyIwTPBmW/nd/qwBxBD9XYODWXfMo zGsfqSIh4VbI4VmeRdi4c6wLxf+cNPTu9zsC3+FPkP8Ge/kUfTbp4jXqORPUduboC78S zA7Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=qLsl13mT; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id q14-20020a170902eb8e00b001c724f99804si10603631plg.615.2023.10.09.17.16.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Oct 2023 17:16:27 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=qLsl13mT; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id B1AC980BD5FE; Mon, 9 Oct 2023 17:16:24 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1379139AbjJJAKN (ORCPT + 99 others); Mon, 9 Oct 2023 20:10:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51046 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1379166AbjJJAKK (ORCPT ); Mon, 9 Oct 2023 20:10:10 -0400 Received: from out-195.mta0.migadu.com (out-195.mta0.migadu.com [91.218.175.195]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 67C6DCF for ; Mon, 9 Oct 2023 17:10:08 -0700 (PDT) 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 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT X-Spam-Status: No, score=2.7 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, RCVD_IN_SBL_CSS,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Mon, 09 Oct 2023 17:16:25 -0700 (PDT) X-Spam-Level: ** 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 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); -- 2.42.0