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=-17.3 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,NICE_REPLY_A, SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_1 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 76C59C433B4 for ; Mon, 19 Apr 2021 15:57:05 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 0806561285 for ; Mon, 19 Apr 2021 15:57:05 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 0806561285 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 7CF1E6B006C; Mon, 19 Apr 2021 11:57:04 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7A5CD6B006E; Mon, 19 Apr 2021 11:57:04 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 61F766B0070; Mon, 19 Apr 2021 11:57:04 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0007.hostedemail.com [216.40.44.7]) by kanga.kvack.org (Postfix) with ESMTP id 30EDB6B006C for ; Mon, 19 Apr 2021 11:57:04 -0400 (EDT) Received: from smtpin24.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id DBF43181AEF3F for ; Mon, 19 Apr 2021 15:57:03 +0000 (UTC) X-FDA: 78049570326.24.A07FFCA Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf01.hostedemail.com (Postfix) with ESMTP id 5D86D500152E for ; Mon, 19 Apr 2021 15:57:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1618847823; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=cMDl/lLdep1N0/um0AmnQLaDki5aIWjkuWZjHPmQpqY=; b=I8PqGz6B/fQF/ZiXZcKgR+JAu5bhka6LHGQdvPWYAfOtevynUtcOi3LMV1sONd6sLcz8BR j9hquDtpZFBqXMbkyghy087gFgUpYRRm90u2Q1XlFF9AAHZUl/llXU62PkGip9CEfFw8tk +OS1X+Z3a0I3RmWRn9Xyr5nrXrmN2yI= Received: from mail-qk1-f197.google.com (mail-qk1-f197.google.com [209.85.222.197]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-522-6Fs6QS0GPHGSxX6Iw9V2HQ-1; Mon, 19 Apr 2021 11:57:01 -0400 X-MC-Unique: 6Fs6QS0GPHGSxX6Iw9V2HQ-1 Received: by mail-qk1-f197.google.com with SMTP id t126-20020a37aa840000b02902e3c5b3abeaso2131965qke.10 for ; Mon, 19 Apr 2021 08:57:01 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:subject:to:cc:references:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding :content-language; bh=cMDl/lLdep1N0/um0AmnQLaDki5aIWjkuWZjHPmQpqY=; b=OBTsXtVFjbUp8Q5jY+C3dvPWAczx5R9KbFbmQ2GJlhVN+7fA65T7UkS60w6JQV1kQu mspezXcx52I9Nljbl3+FcrCwHXndKtNbtJchgvIKsEKOSH844YBJ5H9/8ojh3+sSRsVU 9AxjYL8XbCpheTg3DyDuoXaLvHydSIyGZ3kGRIOB3NdBmAiNhS/MS/dHxKfqzYKrMHj7 2kRnRDEitVhJcfMhePgshUYkYe1LKcLrB1eH75hPRji4tRbvBK7x+YsLR2j/vJ1YEOz4 MgOMwH2vYO3Xa1nUrZvRE5fuSuqEsn0MmWwOUdEDPPQkZkeZ0q4LUme4VS0b+v6+EVIX xcMA== X-Gm-Message-State: AOAM531aLKjwqkDQHRNPEInzG0SGpjjPlBVLbxxI25aq4NjgcKX3BBIL PF9r/hq7zrTxcARLzoL0MgekrvevjXQUhABnGnuckZ2N0CbCXmB6z0mZtcWZbQDhruUpWI3+Vfu qs1jeHivicfg= X-Received: by 2002:a05:620a:d5e:: with SMTP id o30mr12416014qkl.288.1618847820856; Mon, 19 Apr 2021 08:57:00 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzCUXH+Qh1JrQB39fMDOdO1NkpZZqYz8SyTMXanP4rBFDGLrAYf2Gco03W/tyV9+hCZSBA2YQ== X-Received: by 2002:a05:620a:d5e:: with SMTP id o30mr12415992qkl.288.1618847820631; Mon, 19 Apr 2021 08:57:00 -0700 (PDT) Received: from llong.remote.csb ([2601:191:8500:76c0::cdbc]) by smtp.gmail.com with ESMTPSA id w4sm10077790qkd.94.2021.04.19.08.56.59 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 19 Apr 2021 08:57:00 -0700 (PDT) From: Waiman Long X-Google-Original-From: Waiman Long Subject: Re: [External] [PATCH v4 5/5] mm/memcg: Improve refill_obj_stock() performance To: Muchun Song 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 Memory Management List , Shakeel Butt , Alex Shi , Chris Down , Yafang Shao , Wei Yang , Masayoshi Mizuma , Xing Zhengjun , Matthew Wilcox References: <20210419000032.5432-1-longman@redhat.com> <20210419000032.5432-6-longman@redhat.com> Message-ID: Date: Mon, 19 Apr 2021 11:56:58 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.9.0 MIME-Version: 1.0 In-Reply-To: Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=llong@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Content-Language: en-US X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 5D86D500152E X-Stat-Signature: kkiyswpwc5kfwinpj768p1e7qnien987 Received-SPF: none (redhat.com>: No applicable sender policy available) receiver=imf01; identity=mailfrom; envelope-from=""; helo=us-smtp-delivery-124.mimecast.com; client-ip=170.10.133.124 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1618847822-357713 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 4/19/21 2:06 AM, 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. Thanks for the comment. Will update my patch with call to obj_cgroup_uncharge_pages(). Cheers, Longman