From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 70B3FC433B4 for ; Thu, 15 Apr 2021 18:53:20 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id DB461610FA for ; Thu, 15 Apr 2021 18:53:19 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org DB461610FA Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=cmpxchg.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 30E338D0002; Thu, 15 Apr 2021 14:53:19 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2CFAC6B006C; Thu, 15 Apr 2021 14:53:19 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0032C8D0002; Thu, 15 Apr 2021 14:53:18 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id B9C686B0036 for ; Thu, 15 Apr 2021 14:53:18 -0400 (EDT) Received: from smtpin12.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 6A8C58248D52 for ; Thu, 15 Apr 2021 18:53:18 +0000 (UTC) X-FDA: 78035499276.12.E6890A4 Received: from mail-qv1-f44.google.com (mail-qv1-f44.google.com [209.85.219.44]) by imf26.hostedemail.com (Postfix) with ESMTP id 4581540002CF for ; Thu, 15 Apr 2021 18:53:13 +0000 (UTC) Received: by mail-qv1-f44.google.com with SMTP id j3so12225651qvs.1 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=mipLLhBuVBj9x4eooK1BbrGKy88g+qVfSdnYMu/NGhtUzfPhkAMwXa6MO2EoqQYijg 0QEOtX+p4EePaphp1I8RRofqa2+q4GkYqdq4OaQ6XohoSYbb8YOfGOc/HEE6hikbBDG3 9P1x0wnsiPvOO1qUXvRhA82gXEK6RgOOyWIEsjtkLmMyUkfumOa4WfGNyekjX/fKh9sh QlCDvovUlplv4AwbHT7QYJkvI42k/cLJLplqEg+T7+wD6DkyE4k5K7DPnePucZrBHL3B 3sAdlO7GVoqCO0nWF1l2qMkZOQZP8/Q5BxzQspsToxW00tV3zQE94wsx9beOeXBzQd2v bftw== X-Gm-Message-State: AOAM532mFvLNrjF2T2+ODflFfU6mHEV9UD/n+L/zsx/fqxGyoqKbn3FR OvCh7JbtB3P/3DS0BUXUz2LIxA== X-Google-Smtp-Source: ABdhPJw9P8Kpim/vGmjqJwgzAVFTO6UGfsnHmGwxmbB9cfF9fe6Gb8lgaZAMGEoWhGSQ1UF3KctUyw== 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> X-Rspamd-Queue-Id: 4581540002CF X-Stat-Signature: cuo18ak9d4a7qc6msy9kbr3igr4167rr X-Rspamd-Server: rspam02 Received-SPF: none (cmpxchg.org>: No applicable sender policy available) receiver=imf26; identity=mailfrom; envelope-from=""; helo=mail-qv1-f44.google.com; client-ip=209.85.219.44 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1618512793-944451 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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.