Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp2789458rdg; Mon, 16 Oct 2023 15:19:35 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGu4n/ItGgKmGftqlQNZ6Ao/YCU0hhWCGoyuknxmx37SMRaaVKiXVptKGgeYIdH3ytMXar6 X-Received: by 2002:a17:90b:4d08:b0:27d:60b1:4f2c with SMTP id mw8-20020a17090b4d0800b0027d60b14f2cmr479049pjb.4.1697494775273; Mon, 16 Oct 2023 15:19:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697494775; cv=none; d=google.com; s=arc-20160816; b=NJk086GOz/iuq0w7prWsjFCpmjoIHpTRot6oqblv4y591dFo4ojmqh6Hc1pzJYiMmA qDLR5DcGA49EqS1BzFJRDecq2m1MidQ9H4EfHaOKP76A3tqYZzoAprl1122zBynZux+N EdbGDSheGGfugJxxwxoGpU5njBifGD5NRLLyVqN14e+zsyNLn/MXG6+7ReJuywWuO3wD ETblCKWMgjAtrlYu4kpc68+xsf/RIYbN7vwCs/PjRYFvudqM2PMPrCFcDDNrY2oOnZQP LJCw3a9YVnl9nS57rryeodHsXlw0q3cJUJ6hpGyQZY7NGENfCe4HOe3UcQPZENlNm49H MNvQ== 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=Qo1YPZiBE3t+4Vaa4b0Fou7dzWJJ5TkLVY3p5Qh+0JM=; fh=TyPg14X/GXePfX2HFCpC8BoYIRT/2UqYFN7bT1+n50k=; b=As6F29ivggxjosQ8cPcDtpVd1JfEdSTlHD1EfpxvgQz7Ht0r5ZVIMi2kDWyEPRMbo7 cCL1K0uvcBzs1C+KSBG0QNOXLNWQsjDF8M2uIMFe9e9XzFkDMfmlDDqy0KRAI6dIV5Up gGJVViXL9VNjoFMpe59f4mkEUt6B9eUKxAOlXQvBgQST8rRN0woMtJic14EKA/is75vQ YDwfWDbyzvTIAabw/yB8mHrApL/fwWaWbDd9liFD2I8iSRf17ZXWQdAw+An9OFtvEksR nrqDcQ86QEShtUnqtnanX7p2wMDzpsrFUKG11+I23C2BtqSN9RJKd7xDkjkP3i/uj8Lp tdGA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linux.dev header.s=key1 header.b=t4+D5b2o; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id b7-20020a637147000000b005ab74d58e04si277278pgn.58.2023.10.16.15.19.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Oct 2023 15:19:35 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=fail header.i=@linux.dev header.s=key1 header.b=t4+D5b2o; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (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 snail.vger.email (Postfix) with ESMTP id 7BB2B8028872; Mon, 16 Oct 2023 15:19:34 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229943AbjJPWT2 (ORCPT + 99 others); Mon, 16 Oct 2023 18:19:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57174 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234164AbjJPWT1 (ORCPT ); Mon, 16 Oct 2023 18:19:27 -0400 Received: from out-197.mta1.migadu.com (out-197.mta1.migadu.com [95.215.58.197]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1C21A9B for ; Mon, 16 Oct 2023 15:19:25 -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=1697494764; 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=Qo1YPZiBE3t+4Vaa4b0Fou7dzWJJ5TkLVY3p5Qh+0JM=; b=t4+D5b2ohims4xmfGrzn/7ohW+Sf1HKAp2WgnGX/vHds2piEpV/vNNlAymLlRqdz76x+AN 1KSuuR9IKnmKBFdjygx31p4IdK1IlYSUsRUMldFjNZPT3F6UztYULUNijYqDhqKJ/VEtqy AgCi61ESxsfBhffhea6oWKV75TAEaCk= From: Roman Gushchin To: Andrew Morton Cc: linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, Johannes Weiner , Michal Hocko , Shakeel@vger.kernel.org, Butt@vger.kernel.org, shakeelb@google.com, Muchun Song , Dennis Zhou , David Rientjes , Vlastimil Babka , Naresh Kamboju , Roman Gushchin Subject: [PATCH v3 1/5] mm: kmem: optimize get_obj_cgroup_from_current() Date: Mon, 16 Oct 2023 15:18:56 -0700 Message-ID: <20231016221900.4031141-2-roman.gushchin@linux.dev> In-Reply-To: <20231016221900.4031141-1-roman.gushchin@linux.dev> References: <20231016221900.4031141-1-roman.gushchin@linux.dev> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net 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 (snail.vger.email [0.0.0.0]); Mon, 16 Oct 2023 15:19:34 -0700 (PDT) Manually inline memcg_kmem_bypass() and active_memcg() to speed up get_obj_cgroup_from_current() by avoiding duplicate in_task() checks and active_memcg() readings. Also add a likely() macro to __get_obj_cgroup_from_memcg(): obj_cgroup_tryget() should succeed at almost all times except a very unlikely race with the memcg deletion path. Signed-off-by: Roman Gushchin (Cruise) Tested-by: Naresh Kamboju Acked-by: Shakeel Butt Acked-by: Johannes Weiner --- mm/memcontrol.c | 34 ++++++++++++++-------------------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 9741d62d0424..16ac2a5838fb 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -1068,19 +1068,6 @@ struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_struct *mm) } EXPORT_SYMBOL(get_mem_cgroup_from_mm); -static __always_inline bool memcg_kmem_bypass(void) -{ - /* Allow remote memcg charging from any context. */ - if (unlikely(active_memcg())) - return false; - - /* Memcg to charge can't be determined. */ - if (!in_task() || !current->mm || (current->flags & PF_KTHREAD)) - return true; - - return false; -} - /** * mem_cgroup_iter - iterate over memory cgroup hierarchy * @root: hierarchy root @@ -3007,7 +2994,7 @@ static struct obj_cgroup *__get_obj_cgroup_from_memcg(struct mem_cgroup *memcg) for (; !mem_cgroup_is_root(memcg); memcg = parent_mem_cgroup(memcg)) { objcg = rcu_dereference(memcg->objcg); - if (objcg && obj_cgroup_tryget(objcg)) + if (likely(objcg && obj_cgroup_tryget(objcg))) break; objcg = NULL; } @@ -3016,16 +3003,23 @@ static struct obj_cgroup *__get_obj_cgroup_from_memcg(struct mem_cgroup *memcg) __always_inline struct obj_cgroup *get_obj_cgroup_from_current(void) { - struct obj_cgroup *objcg = NULL; struct mem_cgroup *memcg; + struct obj_cgroup *objcg; - if (memcg_kmem_bypass()) - return NULL; + if (in_task()) { + memcg = current->active_memcg; + + /* Memcg to charge can't be determined. */ + if (likely(!memcg) && (!current->mm || (current->flags & PF_KTHREAD))) + return NULL; + } else { + memcg = this_cpu_read(int_active_memcg); + if (likely(!memcg)) + return NULL; + } rcu_read_lock(); - if (unlikely(active_memcg())) - memcg = active_memcg(); - else + if (!memcg) memcg = mem_cgroup_from_task(current); objcg = __get_obj_cgroup_from_memcg(memcg); rcu_read_unlock(); -- 2.42.0