Received: by 2002:a05:6a10:17d3:0:0:0:0 with SMTP id hz19csp2594864pxb; Mon, 19 Apr 2021 09:10:06 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzz58NNt28uYsa5CrkkPlzD9KGAZDYupizq10J16ju/OAt9ur0ALYiHvxrAUtVXGvg2cQs3 X-Received: by 2002:a05:6402:b41:: with SMTP id bx1mr9192922edb.105.1618848606101; Mon, 19 Apr 2021 09:10:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618848606; cv=none; d=google.com; s=arc-20160816; b=iceEheg73+18OIWrnd4yv22ishA5+Gr7HaY4RQROTa7g/ZPCprrqLLQMlj6QoAerna c85h+1Gi/hSRz4ME6s89Jpux1KAzHjUN1xRLqpzdF7pK6+BsFslzK0ZcXI6YfvK12S+N 7Mz8xKCiN4zPQVAS6NJlCOXI+7elAYfokElCnCXNCKNfq52PxNWaBvPIcBrv8XExytOo 3yQEddPm3zTBn3x7oEcZb60f1XrC16lSlkJgv4OVX0wUY2TZsO9fSKg61VM06d/DBfW2 JOjmEtJqzF3bzKLfU/9wex+Ag9e93cdT18n3a57xLcsWLzd+pzrRBnTVSRY2InrZgurK nadg== 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=wJcxPKcW0Veo1gCFL+CuNN37VW8R09vglUBE4v/xI9U=; b=d5JMzM2QwzzfSlg468sM1SA14iPJH4DRbWvRDhf0jaZJHBEHZ0NxydKPnWYi30JlbS rkjRqL0FIXdesqJf12M5xT2rxJY2FfAr5mgiRwayDe6GDMboELK0EUmkjSiI98AYq4eB dM03SaNri0C+D90MjAvkelvCBdYD6AtWAhiqIxRl06brBNjzpyfyk3oc+DxIbJqdeCay 1+2ozgnCf7L676tMij/+xukBWejWKgB7TnP6x0JNUeSZwNBpWfu/FuGaCLaoxFVsW0GN f/fRnybwd3ZSFG5toR8+lOoK3GJcFHsiEd77Jubd439JSuyDW598fa7mlyKW8UUNIpVu dD8g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b="AT/Bs0Z5"; 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 a11si4395319edr.201.2021.04.19.09.09.41; Mon, 19 Apr 2021 09:10:06 -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="AT/Bs0Z5"; 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 S239653AbhDSPBG (ORCPT + 99 others); Mon, 19 Apr 2021 11:01:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53364 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238859AbhDSPBG (ORCPT ); Mon, 19 Apr 2021 11:01:06 -0400 Received: from mail-lj1-x236.google.com (mail-lj1-x236.google.com [IPv6:2a00:1450:4864:20::236]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E582FC06174A for ; Mon, 19 Apr 2021 08:00:35 -0700 (PDT) Received: by mail-lj1-x236.google.com with SMTP id z8so39706133ljm.12 for ; Mon, 19 Apr 2021 08:00:35 -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=wJcxPKcW0Veo1gCFL+CuNN37VW8R09vglUBE4v/xI9U=; b=AT/Bs0Z5qEWwjLyYLiTX6W0h7JJDDXlbbH1gNiMaQcF+icW/iFjk2BO/NlULM5/k3R FIQOadrNxR/LQZ/E6LpXhxDsllaWOUj3r7nH7N38qaGsdwqtn2qT6PVPZ+82bMRXeDVE 4qkn/DA4HLAd+C+uF7pMGzGwH+1eAqsf2gXlNaEA05Eing9VFkTd9LdNsVdfhWgvaEwS USrRJT6rrfxCyqkI9J5XlVd41fifaNVinnCF7lVTQD93EsE4R6NqIpt16bxDZBqS+cB4 cbzzwsMtySElnFgVXYuTAZh/XOp9KBm+yIyWffMzHqtYTKkGMryd1+0PQvmmZabjeCHC AGbg== 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=wJcxPKcW0Veo1gCFL+CuNN37VW8R09vglUBE4v/xI9U=; b=GgjDeAo/oThBX6iOPeKrQ5Nl7+qDxM9j/FuCzagfNQ3rcMkvrMO/I9E95/ikb8fV9K OAoBDh/M9oqPE26MFUHLWgdEeEDxwnaZqr3I6wXfNkCAcLLeLf1SAq93H04bGDvk5scV myXRbUpabMgdbfPo3ug2c5iVl1FTdnQ/abrLUDTzwD+Yy30s6X0k8+fzy9LxiVcLkAfF DvxxlcNMdHiQIKeUYyAPU2y0s/hgHN1bs5H5gEuHZ+bKTtn9qL35DXJYLKq2AuRz2xj+ Gq2aLXYVzUzTa6TpWx+JcZeqNmvInaB2GCM/EJaJsBqWWw+Rfgk22j9uHN9Q0LgRwb4E KRhA== X-Gm-Message-State: AOAM533K9kobR3WKcDj8LLMgpDhz6YsXj2xSOJCcVbwzILTmaDHjKXu7 3NAVXBYBkHFdEzEVXlO1CzB4iNFLTz2XJA3LCeubrQ== X-Received: by 2002:a2e:9cc4:: with SMTP id g4mr11824695ljj.34.1618844432793; Mon, 19 Apr 2021 08:00:32 -0700 (PDT) MIME-Version: 1.0 References: <20210419000032.5432-1-longman@redhat.com> <20210419000032.5432-6-longman@redhat.com> In-Reply-To: From: Shakeel Butt Date: Mon, 19 Apr 2021 08:00:21 -0700 Message-ID: Subject: Re: [External] [PATCH v4 5/5] mm/memcg: Improve refill_obj_stock() performance To: Muchun Song Cc: Waiman Long , 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 Memory Management List , Alex Shi , Chris Down , Yafang Shao , Wei Yang , Masayoshi Mizuma , Xing Zhengjun , Matthew Wilcox Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sun, Apr 18, 2021 at 11:07 PM Muchun Song wrote: > > On Mon, Apr 19, 2021 at 8:01 AM Waiman Long wrote: > > > > There are two issues with the current refill_obj_stock() code. First of > > all, when nr_bytes reaches over PAGE_SIZE, it calls drain_obj_stock() to > > atomically flush out remaining bytes to obj_cgroup, clear cached_objcg > > and do a obj_cgroup_put(). It is likely that the same obj_cgroup will > > be used again which leads to another call to drain_obj_stock() and > > obj_cgroup_get() as well as atomically retrieve the available byte from > > obj_cgroup. That is costly. Instead, we should just uncharge the excess > > pages, reduce the stock bytes and be done with it. The drain_obj_stock() > > function should only be called when obj_cgroup changes. > > > > Secondly, when charging an object of size not less than a page in > > obj_cgroup_charge(), it is possible that the remaining bytes to be > > refilled to the stock will overflow a page and cause refill_obj_stock() > > to uncharge 1 page. To avoid the additional uncharge in this case, > > a new overfill flag is added to refill_obj_stock() which will be set > > when called from obj_cgroup_charge(). > > > > Signed-off-by: Waiman Long > > --- > > mm/memcontrol.c | 23 +++++++++++++++++------ > > 1 file changed, 17 insertions(+), 6 deletions(-) > > > > diff --git a/mm/memcontrol.c b/mm/memcontrol.c > > index a6dd18f6d8a8..d13961352eef 100644 > > --- a/mm/memcontrol.c > > +++ b/mm/memcontrol.c > > @@ -3357,23 +3357,34 @@ static bool obj_stock_flush_required(struct memcg_stock_pcp *stock, > > return false; > > } > > > > -static void refill_obj_stock(struct obj_cgroup *objcg, unsigned int nr_bytes) > > +static void refill_obj_stock(struct obj_cgroup *objcg, unsigned int nr_bytes, > > + bool overfill) > > { > > unsigned long flags; > > struct obj_stock *stock = get_obj_stock(&flags); > > + unsigned int nr_pages = 0; > > > > if (stock->cached_objcg != objcg) { /* reset if necessary */ > > - drain_obj_stock(stock); > > + if (stock->cached_objcg) > > + drain_obj_stock(stock); > > obj_cgroup_get(objcg); > > stock->cached_objcg = objcg; > > stock->nr_bytes = atomic_xchg(&objcg->nr_charged_bytes, 0); > > } > > stock->nr_bytes += nr_bytes; > > > > - if (stock->nr_bytes > PAGE_SIZE) > > - drain_obj_stock(stock); > > + if (!overfill && (stock->nr_bytes > PAGE_SIZE)) { > > + nr_pages = stock->nr_bytes >> PAGE_SHIFT; > > + stock->nr_bytes &= (PAGE_SIZE - 1); > > + } > > > > put_obj_stock(flags); > > + > > + if (nr_pages) { > > + rcu_read_lock(); > > + __memcg_kmem_uncharge(obj_cgroup_memcg(objcg), nr_pages); > > + rcu_read_unlock(); > > + } > > It is not safe to call __memcg_kmem_uncharge() under rcu lock > and without holding a reference to memcg. More details can refer > to the following link. > > https://lore.kernel.org/linux-mm/20210319163821.20704-2-songmuchun@bytedance.com/ > > In the above patchset, we introduce obj_cgroup_uncharge_pages to > uncharge some pages from object cgroup. You can use this safe > API. > I would recommend just rebase the patch series over the latest mm tree.