Received: by 2002:a05:7412:2a8c:b0:e2:908c:2ebd with SMTP id u12csp2708183rdh; Wed, 27 Sep 2023 10:12:35 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHfesF/++ZzsMrCxefr3ofmQTv2h3t62u2SOKaAYcejGSn+AQrMXIcXJ7Bt6cHIyUU1yyzC X-Received: by 2002:a17:902:ec82:b0:1c5:d8a3:8781 with SMTP id x2-20020a170902ec8200b001c5d8a38781mr2740937plg.55.1695834755516; Wed, 27 Sep 2023 10:12:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695834755; cv=none; d=google.com; s=arc-20160816; b=0uk2bIHgG7Pe7Phpeo7GJfngvHgRkj4GhqG7ky1lQGHEVwooUmW9DCSqtOIZtqq/mf Luua2rp3jOb8BPgzdSdtZPXP3IyVXJ16gPL3ewQXV5W00XT4ySLADoppopC7ffivKqQH LqgISLH/bXD8eUiEpEDoea1kr3qGW/Bz8nejcApP3zZWRaXcayy8yXp9FMIqQiETXxos RFvtfL3V2281rHz0lLhlosvw4LAL+1FAr5VK6hotOBlLPWh10yBaWUQLdfuDX3xdFUv3 TpxnNJO41gYxy4MWEc0rni+bZQWSHzyO6Xpng6PO77XSOoh2tKa6qvqyFcuJ9YPCcPRU YImg== 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=J+ThODwbvpIaijJ/a/gIBgC20RizoNlQ1sjxEf1nY4o=; b=WkzsaVsVqW5wnySyFufmBy3tFGGcnz06ktuZhAuD4iU6SYSiD3ujZpCRtwbR+xrEkW tA7PpmIZDFUcj7UTyaZs4W37fjLfJG0b7pBgZJo5NO9FhLfeaX9G8YfMgvCd4/6t9XZ0 8a8i6PgNVpgjn6zRgBXUPFavtK5JTV07FYpcYsOB2q87IHebHwUtT01toD5WErj+UCq2 k9GNOrZkMAnoEr6Xa6zAYkGiT+5hC529N5p9lxz+KyP54wwudf/jKleOO+0obRJwjXUL Mxekx+cca4gyQUOvYnJCwA0pLxeRedebpe0sNIb5h50fU7e5twz+e7nKJFlldqVmOgv/ YGKg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=Q8hMpLXl; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 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 pete.vger.email (pete.vger.email. [2620:137:e000::3:6]) by mx.google.com with ESMTPS id c7-20020a170903234700b001a6bb7b7a44si16891516plh.307.2023.09.27.10.12.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Sep 2023 10:12:35 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) client-ip=2620:137:e000::3:6; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=Q8hMpLXl; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 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 pete.vger.email (Postfix) with ESMTP id 32E2480CF543; Wed, 27 Sep 2023 08:09:28 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232341AbjI0PJB (ORCPT + 99 others); Wed, 27 Sep 2023 11:09:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58892 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232336AbjI0PI7 (ORCPT ); Wed, 27 Sep 2023 11:08:59 -0400 Received: from out-203.mta1.migadu.com (out-203.mta1.migadu.com [IPv6:2001:41d0:203:375::cb]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B02F1139 for ; Wed, 27 Sep 2023 08:08:57 -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=1695827335; 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=Q8hMpLXlwyHYuhYTY7QmN3ucJ4BymSYDpw0OZAW82OI1HjO9QhUqV7hIgxRE3/+F1Zdf1X 9pwvp7A1SXynG+K4IEOnW40uYr4B8pTu6BDJ26Y+WSYnLIfaJXQFLXbhSCwQWYyEHZxai0 mR7jQfl6X8WWs0zHRyt6d4SZPcg5cVY= 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 1/5] mm: kmem: optimize get_obj_cgroup_from_current() Date: Wed, 27 Sep 2023 08:08:28 -0700 Message-ID: <20230927150832.335132-2-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 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 pete.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 (pete.vger.email [0.0.0.0]); Wed, 27 Sep 2023 08:09:28 -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