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 2AE1FC433ED for ; Thu, 15 Apr 2021 17:53:32 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 83C1F61132 for ; Thu, 15 Apr 2021 17:53:31 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 83C1F61132 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 0934D6B0036; Thu, 15 Apr 2021 13:53:31 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0683B6B006C; Thu, 15 Apr 2021 13:53:31 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E4D1D6B0070; Thu, 15 Apr 2021 13:53:30 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0090.hostedemail.com [216.40.44.90]) by kanga.kvack.org (Postfix) with ESMTP id C490E6B0036 for ; Thu, 15 Apr 2021 13:53:30 -0400 (EDT) Received: from smtpin22.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 8552118015866 for ; Thu, 15 Apr 2021 17:53:30 +0000 (UTC) X-FDA: 78035348580.22.A224531 Received: from mail-qv1-f52.google.com (mail-qv1-f52.google.com [209.85.219.52]) by imf21.hostedemail.com (Postfix) with ESMTP id CE9ABE000114 for ; Thu, 15 Apr 2021 17:53:27 +0000 (UTC) Received: by mail-qv1-f52.google.com with SMTP id dp18so7491847qvb.5 for ; Thu, 15 Apr 2021 10:53:29 -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=INGh5k1Yj8szNl1l5Z8qYaD8P6YvvcJTSCldK0t4pLE=; b=q+VO14PGTuNSIxED5tGGeUakeYctoqOQ0nJkbh0Vvp7quHEzTPE5abUN2YsdznM5oi mXTFT434TbT7mysw7yWGazDjFqRq+YmsxrjpomEQAuHY96tmqCLoZt7lqa4LSklgrn9e uRS8Dp45ihliK04vKKPAdld1KQQ8qm+2NHe3vYYeu4UyWirtudNSFErs29T78GJ5CPjs wWwqc/PKCWJfxwTi5zqIU7TqrW6iJZcOQST2Om59t0knllllekcuN9GVeodH7qf/zBjG vlkhN+abb/sMuFNbNaCiRCTNmyGsiuzKUuCtDMAtF/1y4UXWAHmF0/mC0tMCBJ0rIwiC psKQ== 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=INGh5k1Yj8szNl1l5Z8qYaD8P6YvvcJTSCldK0t4pLE=; b=UIsSl56UfBaSs4ShOPBMhlplNeMsa2d71HWM9ecdfkyWNNQlT+crpWF94WEBbm0Aar kEaOliHTC5oZW4OX++vBWCPTMwak11rWeMiEnT43jXokF83D69565VS84AXFLNgmwAuu z0h2w2CsylgVhOuIPl/I500ASQTYCUCbIPspX4/mtKYbf3K55w0/U694j3rEusR58DWF O0qL+bFjqnI0WVnerkLZsyCOvSmGlhzfmZGAYnnb/IvFnBeACm0ESJ9ojDtQH2DnUaIX 48w5AhsjCR0qB+n8iuPi/O/9gXkEQra9h1IpHbGFKcPQCb9c2RL5DjuWA97E0jEQO5hA 3IZg== X-Gm-Message-State: AOAM533rTp+vKnh8s6nGGAEFR3O79nKaZkQU1sazxMnJqHP5AkM5lc9c 2FRCosS3VK1YgDVbRWlJxmxu3A== X-Google-Smtp-Source: ABdhPJzo+DulslHHMeIPKQfo+X00FLdIMTkZOLHNkCdxeB60C8i7fQG8oyQ9R9/2zBJktP2rCdoqbw== X-Received: by 2002:a0c:e2c5:: with SMTP id t5mr4255809qvl.27.1618509209056; Thu, 15 Apr 2021 10:53:29 -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 a11sm2470206qkn.12.2021.04.15.10.53.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Apr 2021 10:53:28 -0700 (PDT) Date: Thu, 15 Apr 2021 13:53:27 -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> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210414012027.5352-6-longman@redhat.com> X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: CE9ABE000114 X-Stat-Signature: ajxd67zymuzshemjdszgngjc61emhqof Received-SPF: none (cmpxchg.org>: No applicable sender policy available) receiver=imf21; identity=mailfrom; envelope-from=""; helo=mail-qv1-f52.google.com; client-ip=209.85.219.52 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1618509207-464460 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 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. > @@ -2229,7 +2229,8 @@ struct obj_stock { > struct memcg_stock_pcp { > struct mem_cgroup *cached; /* this never be root cgroup */ > unsigned int nr_pages; > - struct obj_stock obj; > + struct obj_stock task_obj; > + struct obj_stock irq_obj; > > struct work_struct work; > unsigned long flags; > @@ -2254,11 +2255,48 @@ static bool obj_stock_flush_required(struct memcg_stock_pcp *stock, > } > #endif > > +/* > + * Most kmem_cache_alloc() calls are from user context. 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 by disabling preemption only > + * 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. > + */ > static inline struct obj_stock *current_obj_stock(void) > { > struct memcg_stock_pcp *stock = this_cpu_ptr(&memcg_stock); > > - return &stock->obj; > + return in_task() ? &stock->task_obj : &stock->irq_obj; > +} > + > +#define get_obj_stock(flags) \ > +({ \ > + struct memcg_stock_pcp *stock; \ > + struct obj_stock *obj_stock; \ > + \ > + if (in_task()) { \ > + preempt_disable(); \ > + (flags) = -1L; \ > + stock = this_cpu_ptr(&memcg_stock); \ > + obj_stock = &stock->task_obj; \ > + } else { \ > + local_irq_save(flags); \ > + stock = this_cpu_ptr(&memcg_stock); \ > + obj_stock = &stock->irq_obj; \ > + } \ > + obj_stock; \ > +}) > + > +static inline void put_obj_stock(unsigned long flags) > +{ > + if (flags == -1L) > + preempt_enable(); > + else > + local_irq_restore(flags); > } Please make them both functions and use 'unsigned long *flags'. Also I'm not sure doing in_task() twice would actually be more expensive than the == -1 special case, and easier to understand. > @@ -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...