Received: by 2002:a05:6a10:17d3:0:0:0:0 with SMTP id hz19csp982549pxb; Thu, 15 Apr 2021 10:54:59 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyJf2RWv1IzznbOcdvZlWE9pQFALaK1gfcLCxb6xL0YRHMleP9othxa76w+WMY8mCoHAEap X-Received: by 2002:aa7:c7cc:: with SMTP id o12mr5744470eds.291.1618509299285; Thu, 15 Apr 2021 10:54:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618509299; cv=none; d=google.com; s=arc-20160816; b=0PnRhj3RnWlCHjrV+Ky2ERbL/287P4doTeyc+sbqd9p8CxuOFYDCv2Uie+yz40h8IG bYBnNzgf9g1Gkt21JCBfvlpaovlQqWeRwrDUy2gMaCmxd7E1yWEScBoiO3N7vHZUPbvN eO5aA+z6Kb55VVLZNLHAxuM4b7MHEzFQ/AtJ0b5tbttavc5iM1NwDn234mr0RCpYwO2p 9SG3s/iC/4JFQxK/c8m+UadjrZFZ2cD5KXqaJfgwWC4myosy6h9nFTpvnQKjtdiuxr46 FantldGh3HFx53N2qEITgO7CACasmlGEgQ2oqKSRGn5tlL2Xlot4hGPVyyLbTuupEShK jJkw== 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=INGh5k1Yj8szNl1l5Z8qYaD8P6YvvcJTSCldK0t4pLE=; b=WMf7bBk3WYpYPvDi4uoEk7H4KMP+OMjozVLX10fyzKUGNvHJoQJ5lzKu6BSAdLBBt0 nKqqdlFzkPjQMPcigCwjbd52GN75KQgg3GYhgrxYWDhhxzSTLAFwYWUeNcK1bWDbda8r qXtwwN2w3dp3uW41O3JonmfI6UlWX+0GU6H0D5decYAluiDCRV87UMU61kRmnugyIulr 1XytU9FQ+NRLRo9A0O3pxsk4ctb9Zqu83uyUQNL95gojBvY21usm5ZefcdZ9TAgqqCmi vMVZKalBohZ0va+bYsXDBysmt+bRQoJkCkFO9SOsNRkgZBKvHs/hha3Tb/dln53Uf7eW 6xCg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@cmpxchg-org.20150623.gappssmtp.com header.s=20150623 header.b=q+VO14PG; 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 w11si3251096ede.2.2021.04.15.10.54.29; Thu, 15 Apr 2021 10:54:59 -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=q+VO14PG; 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 S233595AbhDORxx (ORCPT + 99 others); Thu, 15 Apr 2021 13:53:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55216 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233666AbhDORxx (ORCPT ); Thu, 15 Apr 2021 13:53:53 -0400 Received: from mail-qv1-xf2a.google.com (mail-qv1-xf2a.google.com [IPv6:2607:f8b0:4864:20::f2a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C51F1C061574 for ; Thu, 15 Apr 2021 10:53:29 -0700 (PDT) Received: by mail-qv1-xf2a.google.com with SMTP id bs7so11625517qvb.12 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=sxRAx9xQ4xyg+9NfgkqRRtHbiAEHE2DmB+Ff903f8C6UGd3PKV240XeiRJUkBgVxZA QJEfUUxFSOddey+nyvGDQnPJhP4untRAByexTQXZGGQWkF5qg58gBxw4GsS2YCMOkH66 x7gSd9hCBsJPjkzvZruSyhx3Rs/aDgUlmoyhcVnBtJA1+e2g+L/hW7DYJqyZ+ouYBlP7 LcmwUAIPHQlGTuLj4wHrt9UYSK4fqZVyv7AdYfOTLLWJgoPbuehrdwYHusGacj5+N0oY plLGqc9MBFZNPzHQwXZloCDcGGL43OqPwzGb0LC8cZr3lThGRSX6qsd2t9V4JtTREDwT WGKQ== X-Gm-Message-State: AOAM532ZEQcOhYpAlranFqnnOcu7gwPb4BYujAkegFqRXGnrJxOvIuzQ 2YqT3xZKzXq6P1KKeJYBc8hzkw== 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> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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...