Received: by 2002:a05:6a10:17d3:0:0:0:0 with SMTP id hz19csp2551836pxb; Tue, 13 Apr 2021 04:55:01 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzZn8wPr402BhMPiUutENPDqvbscl8q5yiJNlNgTMfhzRjQ55ChXjbzKHmL24V9kCQnshyn X-Received: by 2002:a17:90a:138c:: with SMTP id i12mr1477039pja.226.1618314900823; Tue, 13 Apr 2021 04:55:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618314900; cv=none; d=google.com; s=arc-20160816; b=ZBhqQuwrfNe7LcyHrN5KOl9/fSFvSn2Edr6+PLUMWxv2IhJ1aeXKRx5xDgsVpWr44S h/oo9+8yJJN8C2qbvNdxJIxFA6tF64ZA0Z7nDBmBxR16sPf2gR0oYHQ3httXmTzLbb0M G1XBNv8lUQKW9JWo5IxYEgNYSYRtCFnaOsScBSj2vAl2oqD1EFFRJj6N8aMGaEkgGv0W jqxFDmXjEpbVGonn3U28KD0LddUiZQ3B4BtEGhnKTt79an5edkUg83EnDEmjJmwhzcdo Rii3bp79taZkrit9kur9MDtE6+dELRZp73DPXfp+s+aD0/wWd96WZgioST08Lm1C9UT5 QIhg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=/duzMH/OTq6aaOv0cDTj1fS9M5eKwgWI2Ns6h3aePII=; b=ryAuRjXUKkJZTwTN1Eo0ulv/qBFYKqNmWWeOQFUEb1HpFiPd4UPTzmZOK0UbPENE1r kbHTL9NN+Dw377p3847X46blZc84tfGqnF1yJEa0ds1U1kqkDsSxw0OGaXMPOyOsZKfP Nlfmf2WUx2q2PzVYRCeCgMB4pqD9RMNyrJONQdjMnmrRaMHMwwuQzTk6DT5XP61+b5Y5 kkxyfxEyAvCi66GS2PHsA/4/EIjs7uUVBI3B1CQHxB4kEloO+zkhXESyL1JLCuWVvWX1 PKx3JEtIHK87a6w2mAr58y6zbvBvsHEXCcLWboTDT8zbMMZAbq+kSAcgjtTSlkJQ2fGw ndow== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=NQ1Ykeyi; 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=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id mp6si2224852pjb.53.2021.04.13.04.54.48; Tue, 13 Apr 2021 04:55:00 -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=@google.com header.s=20161025 header.b=NQ1Ykeyi; 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=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242114AbhDLXOp (ORCPT + 99 others); Mon, 12 Apr 2021 19:14:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57378 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242062AbhDLXOm (ORCPT ); Mon, 12 Apr 2021 19:14:42 -0400 Received: from mail-lf1-x133.google.com (mail-lf1-x133.google.com [IPv6:2a00:1450:4864:20::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3F596C061574 for ; Mon, 12 Apr 2021 16:14:23 -0700 (PDT) Received: by mail-lf1-x133.google.com with SMTP id 12so24002091lfq.13 for ; Mon, 12 Apr 2021 16:14:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=/duzMH/OTq6aaOv0cDTj1fS9M5eKwgWI2Ns6h3aePII=; b=NQ1YkeyiLSUMD3jWDiGlc6wYoFZTbrTUVCYIGGrWuZqu87Dn3X0VLGDBLQwLx7pKwu /JEpw2GxwEpZJiN7Q2hten9sbEV8lUkUN+HGTSj6WXhrUuK4B/ihsvysUzvp6jN1jrOT iJOX1wVmhk9ANkOmQy8M20dshGYkGVBzEgfR5cUuHTT4hRCKlNliYyge/CWCKShmjy9/ 0Wj2cykBVj6158M/fVwGueo1LTOzMAieRuPpe9Z2ULeCJfKHVWWN8hSkkaIQr+03gLIi 6bBbSqVV8/rNNlgMQ0QmCKi0Fs1IQQ6y0IbuwxbQbvKveEwvx7N+AQeCyMcOGKq+n/kv uP7Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=/duzMH/OTq6aaOv0cDTj1fS9M5eKwgWI2Ns6h3aePII=; b=C44Tku7GoH6I+fcsi36H+aueyWNVAS1x5cmS1PM2GKqWbX3YSV1FRWoZfTB9HlW833 DRlgY+BlklAn9Fte7sNitFe6ZlQCFJ3Gma58UV/hPPOYUdvEJEStB36FXwLIAfccuTPf ByMcYTyXl2Kp+eC5ATWQ+N2IvCoRCXNrzp3wKbGJQIwVZcK3/8uc2CXJs39S/dqO9NH1 qDHXmOeJPuutKbu1q6+RV2nWC05YZF/v+yc77S+PYLLeVADC+p3+fwPovAvIQ//nuunt 5HdJonvaIemEBE6YlTP7M93w4JmGDJYM5p4eQSvXxDuzkLVw2BeKAr8v1KCgE42mRG+x Be6A== X-Gm-Message-State: AOAM531rpoxoiwh31XjgLyuhKThf7f2RT4KN7iUfYdYdPhtWifCzJP89 zr0zko0FIr503B+ooZyg31mtee37Q4+A/PT7ushrxg== X-Received: by 2002:a05:6512:3703:: with SMTP id z3mr20217180lfr.358.1618269261596; Mon, 12 Apr 2021 16:14:21 -0700 (PDT) MIME-Version: 1.0 References: <20210412225503.15119-1-longman@redhat.com> <20210412225503.15119-6-longman@redhat.com> In-Reply-To: From: Shakeel Butt Date: Mon, 12 Apr 2021 16:14:10 -0700 Message-ID: Subject: Re: [PATCH v2 5/5] mm/memcg: Optimize user context object stock access To: Waiman Long Cc: Johannes Weiner , Michal Hocko , Vladimir Davydov , Andrew Morton , Tejun Heo , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Vlastimil Babka , Roman Gushchin , LKML , Cgroups , Linux MM , Muchun Song , Alex Shi , Chris Down , Yafang Shao , Wei Yang , Masayoshi Mizuma , Xing Zhengjun Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Apr 12, 2021 at 4:10 PM Shakeel Butt wrote: > > On Mon, Apr 12, 2021 at 3:55 PM 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 > > --- > > mm/memcontrol.c | 73 +++++++++++++++++++++++++++++++++++++++---------- > > 1 file changed, 59 insertions(+), 14 deletions(-) > > > > diff --git a/mm/memcontrol.c b/mm/memcontrol.c > > index 69f728383efe..29f2df76644a 100644 > > --- a/mm/memcontrol.c > > +++ b/mm/memcontrol.c > > @@ -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); \ > > The above line was missing in the previous version. > > > + 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); > > } > > > > /** > > @@ -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(); > > } > > > > @@ -3193,7 +3233,7 @@ static bool consume_obj_stock(struct obj_cgroup *objcg, unsigned int nr_bytes) > > unsigned long flags; > > bool ret = false; > > > > - local_irq_save(flags); > > + stock = get_obj_stock(flags); > > > > stock = current_obj_stock(); > > The above is redundant. > After cleanup you can add: Reviewed-by: Shakeel Butt