Received: by 2002:a05:6a10:17d3:0:0:0:0 with SMTP id hz19csp1021889pxb; Thu, 15 Apr 2021 11:55:38 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzRLN9IVHFCaUfNdRjz4VjWwLQhqMg69sVtggRhKKlqlwcN6SmgbPzumggLjqed4oeudGox X-Received: by 2002:a17:90a:5898:: with SMTP id j24mr5423291pji.103.1618512938813; Thu, 15 Apr 2021 11:55:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618512938; cv=none; d=google.com; s=arc-20160816; b=0uVhCNTwNQMwJK3zoo0RZB45kWBGd0WXLHXJH2w2WB1zERICX/K/T4HLjIoXj061Am FGVYBwEBzd0hS6W9IwiNoqwgeNu+VlJqm3sUE77qIv2orVL7W8EytL5yt9aBoXV7dRfy NETZEcjmbv6NM/LMe9VYNdenH2PIyteDMeOu6qfBA2LSa2/h/w3tWHZCmkz89eO9Wfl1 ooKHsNb/3iR71rpmBqATY4EHfcLA3Vsiqz+uHzHEL3WAOgCObxA4nL5yuy0TOdNpZF7+ mPa9KO4kQTsuHe1nr7n993rGSmdDRsxdqI6tp78fZvGhvrLtXokQjq5+nDQMDq3f/xPm 548A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=96tuU81fXCkin4WB3Y1v64uEdVONNHvsDjpceLw3bM0=; b=J3jYUXM+fInuJavUGWU1gOjdU1Z/hLp7oOPwQy7RlyXds8oI0LyRqXjOEa1gAjJ2Tr lSZ2lFS0mn65xqrlIiaGP0R77hY02AmVLM0mIC4vJkihKe2BxR0hTVc3IDMBLcHPFgyX f+vhkHxKec05RjHsxvs5yOYnkDSgvvjYN80kXDcMdFIXmMn4Z3zdY1RT1BNcpUbhHCRm 44Nzr0xVyL96Be8iqoLZO7MqEB/131Zp6ryIu+1lG6RteApmQ+RIZJQTAGj03scTk/tc EstqsiKiDYipK2jSqe21zYUuOi9wGxBPPBmGxafJe6mjPxMkldJp+X090iSVk0tKLd0n yuCw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@cmpxchg-org.20150623.gappssmtp.com header.s=20150623 header.b=KucSbo71; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=cmpxchg.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id d12si4267868plh.161.2021.04.15.11.55.26; Thu, 15 Apr 2021 11:55:38 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@cmpxchg-org.20150623.gappssmtp.com header.s=20150623 header.b=KucSbo71; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=cmpxchg.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234481AbhDOSxn (ORCPT + 99 others); Thu, 15 Apr 2021 14:53:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40124 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234130AbhDOSxm (ORCPT ); Thu, 15 Apr 2021 14:53:42 -0400 Received: from mail-qv1-xf2c.google.com (mail-qv1-xf2c.google.com [IPv6:2607:f8b0:4864:20::f2c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EDD3BC061574 for ; Thu, 15 Apr 2021 11:53:17 -0700 (PDT) Received: by mail-qv1-xf2c.google.com with SMTP id i3so6686764qvj.7 for ; Thu, 15 Apr 2021 11:53:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cmpxchg-org.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=96tuU81fXCkin4WB3Y1v64uEdVONNHvsDjpceLw3bM0=; b=KucSbo71ppmRAwrtk+zE9FdgB87Qc+tda3KCUaEMRGH1MZkW8VY/3PWwvP2IP4pxau FziHIC410OdYUCirJSscKoC+x30M84iaj9fFwF4aQIBMaWpf7Zu1rlM8jArzkSlTXJYg v9hKVGygzsc8f2nO+sHjIiFVZADBkBHZyg2L4d9eH2oz7jdgsdctVIKvxYtts3ZAPy6j GcDaOUmPffy9jfWGe9Nf+xYXMmIuL99ftu/DtIhGM5qj41NGcEgcMGQ+2FF1btH+3Y9A CI6hqs7zAosirfWv49F8TFvyk8deLlV+oIGcFUgxmO7meeRc85l79LRS6BSU6zMn8gP7 9RlQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=96tuU81fXCkin4WB3Y1v64uEdVONNHvsDjpceLw3bM0=; b=BITtxrguvXeQyhIepvxHAv0jIt5hsJ9YsaHK02eQ82QngW5xKNcyQzUOHliDaDulqf BjQZiruAwlW8jLr3ieE03GzUg7owrpIy3JtV4NclpLuFTf+oB1pH8WE8febsYfqqAXHx 0ZymAk5NEQ1VlwwfP1ixaz3M7S9yzuJGIQXa499RvYymsr+DfggfpVl8ArYFHdJ9Qxnq jBa1juMlsw5TVil3oBVtt3amqyQvEj085ghI5/DNdYaM/tZgRf//sDF03B6UHePpgE+5 fBdTwMDQqOGg9zkWdsSBbCPct9WgTh4f91il29S8rdlLUWLKBI89d+lFV9EFpv76oMaH JNQA== X-Gm-Message-State: AOAM5316p0krZV7vNexvtCGBTMCsnm7RzHChjUTH2hA89LpDQhsxvWpt 2xuxSB8GueBxcMsuM4ltd1C4kw== X-Received: by 2002:ad4:4c4a:: with SMTP id cs10mr4836974qvb.14.1618512797099; Thu, 15 Apr 2021 11:53:17 -0700 (PDT) Received: from localhost (70.44.39.90.res-cmts.bus.ptd.net. [70.44.39.90]) by smtp.gmail.com with ESMTPSA id z18sm2638060qkg.42.2021.04.15.11.53.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Apr 2021 11:53:16 -0700 (PDT) Date: Thu, 15 Apr 2021 14:53:15 -0400 From: Johannes Weiner To: Waiman Long Cc: Michal Hocko , Vladimir Davydov , Andrew Morton , Tejun Heo , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Vlastimil Babka , Roman Gushchin , linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, linux-mm@kvack.org, Shakeel Butt , Muchun Song , Alex Shi , Chris Down , Yafang Shao , Wei Yang , Masayoshi Mizuma , Xing Zhengjun Subject: Re: [PATCH v3 5/5] mm/memcg: Optimize user context object stock access Message-ID: References: <20210414012027.5352-1-longman@redhat.com> <20210414012027.5352-6-longman@redhat.com> <8dbd3505-9c51-362f-82d8-5efa5773e020@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <8dbd3505-9c51-362f-82d8-5efa5773e020@redhat.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Apr 15, 2021 at 02:16:17PM -0400, Waiman Long wrote: > On 4/15/21 1:53 PM, Johannes Weiner wrote: > > On Tue, Apr 13, 2021 at 09:20:27PM -0400, Waiman Long wrote: > > > Most kmem_cache_alloc() calls are from user context. With instrumentation > > > enabled, the measured amount of kmem_cache_alloc() calls from non-task > > > context was about 0.01% of the total. > > > > > > The irq disable/enable sequence used in this case to access content > > > from object stock is slow. To optimize for user context access, there > > > are now two object stocks for task context and interrupt context access > > > respectively. > > > > > > The task context object stock can be accessed after disabling preemption > > > which is cheap in non-preempt kernel. The interrupt context object stock > > > can only be accessed after disabling interrupt. User context code can > > > access interrupt object stock, but not vice versa. > > > > > > The mod_objcg_state() function is also modified to make sure that memcg > > > and lruvec stat updates are done with interrupted disabled. > > > > > > The downside of this change is that there are more data stored in local > > > object stocks and not reflected in the charge counter and the vmstat > > > arrays. However, this is a small price to pay for better performance. > > > > > > Signed-off-by: Waiman Long > > > Acked-by: Roman Gushchin > > > Reviewed-by: Shakeel Butt > > This makes sense, and also explains the previous patch a bit > > better. But please merge those two. > The reason I broke it into two is so that the patches are individually > easier to review. I prefer to update the commit log of patch 4 to explain > why the obj_stock structure is introduced instead of merging the two. Well I did not find them easier to review separately. > > > @@ -2327,7 +2365,9 @@ static void drain_local_stock(struct work_struct *dummy) > > > local_irq_save(flags); > > > stock = this_cpu_ptr(&memcg_stock); > > > - drain_obj_stock(&stock->obj); > > > + drain_obj_stock(&stock->irq_obj); > > > + if (in_task()) > > > + drain_obj_stock(&stock->task_obj); > > > drain_stock(stock); > > > clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags); > > > @@ -3183,7 +3223,7 @@ static inline void mod_objcg_state(struct obj_cgroup *objcg, > > > memcg = obj_cgroup_memcg(objcg); > > > if (pgdat) > > > lruvec = mem_cgroup_lruvec(memcg, pgdat); > > > - __mod_memcg_lruvec_state(memcg, lruvec, idx, nr); > > > + mod_memcg_lruvec_state(memcg, lruvec, idx, nr); > > > rcu_read_unlock(); > > This is actually a bug introduced in the earlier patch, isn't it? > > Calling __mod_memcg_lruvec_state() without irqs disabled... > > > Not really, in patch 3, mod_objcg_state() is called only in the stock update > context where interrupt had already been disabled. But now, that is no > longer the case, that is why i need to update mod_objcg_state() to make sure > irq is disabled before updating vmstat data array. Oh, I see it now. Man, that's subtle. We've had several very hard to track down preemption bugs in those stats, because they manifest as counter imbalances and you have no idea if there is a leak somewhere. The convention for these functions is that the __ prefix indicates that preemption has been suitably disabled. Please always follow this convention, even if the semantic change is temporary. Btw, is there a reason why the stock caching isn't just part of mod_objcg_state()? Why does the user need to choose if they want the caching or not? It's not like we ask for this when charging, either.