Received: by 2002:a05:7412:518d:b0:e2:908c:2ebd with SMTP id fn13csp450878rdb; Thu, 5 Oct 2023 10:28:27 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGB2fuEr+sKz/yH9DfaBygMGVPGEo3qUCPC4Am2bSceJLycf2K0LAtHCQKUpEmTw72IxGKg X-Received: by 2002:a17:902:c112:b0:1c3:1f0c:fb82 with SMTP id 18-20020a170902c11200b001c31f0cfb82mr5458921pli.41.1696526906756; Thu, 05 Oct 2023 10:28:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696526906; cv=none; d=google.com; s=arc-20160816; b=vGKmSsoEbFOkAiow7iEZPRD1XDU4h75B4O+M4p+SvM8YcH+pc2ok0uxiK78iS8Uber BtHggExMMJonD1M+1ZUUGXd2buK5ZT6AogEddVzD+WNestTtmIrrh6WJNw3RWmpwKpaD E3urSxVRmo/fHQgXuQTJSioffLgfrg2ir6RkmF898wB82fr61WQ6SX4TnR2oKvBlB7Dx BTlimjN0ZwrCYgZxXWfhiZSUHrqu61gckjYyATDx5fy2nsPllcXDVnybOKj58vILoByI hyQgzabjOq4n+23Cvk6dYqVPg4D8GChXAVotHdzNs+Wn/0caHDcZEaxjTM/INrKqUVx1 y8VA== 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=i5Ua/wdmvBo/wu1nXQ0Cl5cuox9DFv/B5F3hb3WMJFo=; fh=GrOh8lyhpWnv6fQ7PEuyh7hTNeBQucAMexY2PVem5IM=; b=QADsiVokOMj0G4rbXpOVluW9xLgdw9IrJvYet4bZCt4n3Q4AtvMVxrPSiQ4wTgfQO8 8SYnCXeoiNWY7R3ilgJoQCHaVX/htl3GOmI9uK8uHwLYaJuRKr4orzemDuteaSZXlfjh CcCp9g5J8ikz0TB2v2FKDmk5SQAUnrpkwk855+9lIt1S6afD4QAiCwqAmWdOPcbPk3GG +a/mJEAaZAIDHhnQjw22AaI3FDg7vvxQgqCsApv6cBpIglXqgrKXERYkZ4SBuNPKSRbD 3OLQ3dmuKi3VEMtJRT7xdNGUuJ2ztq2H+fMiCvATBvPWWvLyINaNN7XCOsHY68Pps97o fWlQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=iy2KaghV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 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 morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id d6-20020a170902654600b001c747f47c9dsi1791763pln.526.2023.10.05.10.28.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Oct 2023 10:28:26 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=iy2KaghV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 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 morse.vger.email (Postfix) with ESMTP id 1A28280D7F74; Fri, 29 Sep 2023 11:01:51 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233767AbjI2SB1 (ORCPT + 99 others); Fri, 29 Sep 2023 14:01:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49100 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233727AbjI2SBZ (ORCPT ); Fri, 29 Sep 2023 14:01:25 -0400 Received: from out-208.mta0.migadu.com (out-208.mta0.migadu.com [91.218.175.208]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 148321AE for ; Fri, 29 Sep 2023 11:01:22 -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=1696010477; 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=i5Ua/wdmvBo/wu1nXQ0Cl5cuox9DFv/B5F3hb3WMJFo=; b=iy2KaghVwmRlwfpu6b7+AOhBWIOkmf5hD6kyAjLMK5XlcnnjDizfH24Jr/mHe5sgea67k7 4WiyJ8S09oEuEbj4kODKMMxIn9KtFUE1gjaYCUzSTGsq7ELsKoT6yLcMwvrxUsNOMZ4P39 APJaRZfIVaJdT9iKHDsVsLui/4J0bOY= 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 , Roman Gushchin Subject: [PATCH v1 1/5] mm: kmem: optimize get_obj_cgroup_from_current() Date: Fri, 29 Sep 2023 11:00:51 -0700 Message-ID: <20230929180056.1122002-2-roman.gushchin@linux.dev> In-Reply-To: <20230929180056.1122002-1-roman.gushchin@linux.dev> References: <20230929180056.1122002-1-roman.gushchin@linux.dev> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.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 (morse.vger.email [0.0.0.0]); Fri, 29 Sep 2023 11:01:51 -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) Acked-by: Shakeel Butt --- 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