Received: by 2002:a05:7412:da14:b0:e2:908c:2ebd with SMTP id fe20csp2206422rdb; Mon, 9 Oct 2023 17:10:26 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFzCqhe0Yyc0zYqdv0NPO0kuCYGax7pKA5jv/xj5ISkSyGhv2g23LHGfPAiOGsB9Jd1tLhp X-Received: by 2002:a05:6808:d48:b0:3a9:9bcb:8760 with SMTP id w8-20020a0568080d4800b003a99bcb8760mr21545027oik.39.1696896626353; Mon, 09 Oct 2023 17:10:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696896626; cv=none; d=google.com; s=arc-20160816; b=cWZlVRpfh3kgVZ7NWwNC9ll2AEkt8lCbhj2pGU1LjFTty1Ri5t6Nav4ru/62ai//Sx TkO8UfmE8nCasCe5i2mzH7lHSCzWIoqWoPRnZpe9TBLB+C1PrA5TpyfEtEmIDNrfUnbA mG1toNUiPUVfEnomWeilvSTzrjD9hNQ3s029GO7Xrwj6HGgrDfb8dS0JmIpdxRGXKp7a 6+uck/RW0uIcXQuee1YBPS5eRvWrQufg+ETnOAFEvbhAtDVMmRl8lE972KHRnlTqabTK OC1Bb0rfbt1sMKIRQlAfqXS4Bwjrj+W0ElIEOykDCV5G8TIsL/V6dt5WnqNUMAmrPaOF 12mg== 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=6BDA26poRN0lVkuNk023R6ElLY4Pdz30rttSzJzWmU0=; fh=eIV/wktpjV+EeUDLeXhmhAmcuYEtVvgOWNik/7Ld4iQ=; b=opBd/QWrx8RaO3Lk01jO4qbM6P8kcn7yKUDn+gTBQ1sqfbWK+yhjmIxe+xEIgJUny9 r+xEPMavA34Dl2M9zjXEys+E6cOaJEhHJzyMDHLnuM1fkZQXr3R6XCGXD7zVVU6woCl+ crLoAaSUda0PhozjEDAvUFZAcurnsiSp1SHuO1dvJspd7hFAR7If2mE2WOSC3ERRuoDT JatEKU/5/Q71J5dr2SKJRVsEmChEQugIUbbll2c+QRfWtH0FfEpDcbFG6PDQ9p9u/U4h 6slHcpynvlRb3dABBi07XD0u5QMj+6MC6GHoTGCU9KYy2gSwtHkVzh4yBI9Pzhl5t9hQ AUWA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=ft97nbcy; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 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. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id z1-20020a63c041000000b00573fd9be4bdsi10601150pgi.493.2023.10.09.17.10.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Oct 2023 17:10:26 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=ft97nbcy; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 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 6EFFD81DAAC9; Mon, 9 Oct 2023 17:10:21 -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 S1379154AbjJJAKF (ORCPT + 99 others); Mon, 9 Oct 2023 20:10:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42702 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1379138AbjJJAKD (ORCPT ); Mon, 9 Oct 2023 20:10:03 -0400 Received: from out-199.mta0.migadu.com (out-199.mta0.migadu.com [91.218.175.199]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 72B9CA4 for ; Mon, 9 Oct 2023 17:10:02 -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=1696896599; 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=6BDA26poRN0lVkuNk023R6ElLY4Pdz30rttSzJzWmU0=; b=ft97nbcyFPqeUqZ4lisCE8IdqHS0q8h0Wnsl4HIdSRB7hY45IRuQS+OuTRiaAGnlAil0tV 6n6m78HGgBIkfz0ByXmzPARF8sfyEGsDiqZLrbTdayMXeSR6UAx8qwH0gjpYcPSmPHKjtQ qzYOWVCL8FrVR9Z2F277gR71ZjBKiJU= 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 1/5] mm: kmem: optimize get_obj_cgroup_from_current() Date: Mon, 9 Oct 2023 17:09:25 -0700 Message-ID: <20231010000929.450702-2-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 autolearn=no 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]); Mon, 09 Oct 2023 17:10:21 -0700 (PDT) X-Spam-Level: ** 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 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