From patchwork Fri Dec 6 01:35:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chen Ridong X-Patchwork-Id: 13896220 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 64525E77171 for ; Fri, 6 Dec 2024 01:45:14 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7C1026B014D; Thu, 5 Dec 2024 20:45:12 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6D8FB6B0150; Thu, 5 Dec 2024 20:45:12 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4679C6B014D; Thu, 5 Dec 2024 20:45:12 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 17FE66B0150 for ; Thu, 5 Dec 2024 20:45:12 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 943B080BBC for ; Fri, 6 Dec 2024 01:45:11 +0000 (UTC) X-FDA: 82862840592.25.8FA16A3 Received: from dggsgout12.his.huawei.com (dggsgout12.his.huawei.com [45.249.212.56]) by imf16.hostedemail.com (Postfix) with ESMTP id B73EE180010 for ; Fri, 6 Dec 2024 01:44:51 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=none; spf=pass (imf16.hostedemail.com: domain of chenridong@huaweicloud.com designates 45.249.212.56 as permitted sender) smtp.mailfrom=chenridong@huaweicloud.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1733449502; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=CcvOGVirthUfv80HLNyKloVSeZGr/n2sGkEM368NT9M=; b=5lWJTYQVy/OfxpYn5zd5Nfe7z2vXHsujhCGNfAo9knNcadnB+eMT3eFp+uu4ln2M80tOSC 4ZWb6B52GV9OmNMoqm6OkbMGl9VfvmTHsP5Hba3ff9z5/omuzWHFltVWZZh8K3G2kAHpPs CkVg4x+dtJ5sfv8Afk5r9Rj28qg9i7I= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1733449502; a=rsa-sha256; cv=none; b=AWCeL5mGOS61fW+TV7u6q4vu6rxVbJBKYRbG3dmkls5g74/tlvfwyLp72H1nqEValzjddR UK8KFc0VPWKCPLZyLgoxUb+oD31PKuzBk+1QYvD+VVAG/rhaPXWP+dCzhvZDY9Mz9M/zLn 2kzK5ldyG4NKJSsl3nIiXkvDuOj9SmE= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=none; spf=pass (imf16.hostedemail.com: domain of chenridong@huaweicloud.com designates 45.249.212.56 as permitted sender) smtp.mailfrom=chenridong@huaweicloud.com; dmarc=none Received: from mail.maildlp.com (unknown [172.19.93.142]) by dggsgout12.his.huawei.com (SkyGuard) with ESMTP id 4Y4DZz4X62z4f3kF4 for ; Fri, 6 Dec 2024 09:44:43 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.128]) by mail.maildlp.com (Postfix) with ESMTP id AE7171A07BD for ; Fri, 6 Dec 2024 09:45:02 +0800 (CST) Received: from hulk-vt.huawei.com (unknown [10.67.174.121]) by APP4 (Coremail) with SMTP id gCh0CgD30oYKV1JnkkskDw--.31315S6; Fri, 06 Dec 2024 09:45:02 +0800 (CST) From: Chen Ridong To: akpm@linux-foundation.org, mhocko@kernel.org, hannes@cmpxchg.org, yosryahmed@google.com, roman.gushchin@linux.dev, shakeel.butt@linux.dev, muchun.song@linux.dev, davidf@vimeo.com, vbabka@suse.cz Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, chenridong@huawei.com, wangweiyang2@huawei.com Subject: [next -v1 4/5] memcg: factor out the __refill_obj_stock function Date: Fri, 6 Dec 2024 01:35:11 +0000 Message-Id: <20241206013512.2883617-5-chenridong@huaweicloud.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241206013512.2883617-1-chenridong@huaweicloud.com> References: <20241206013512.2883617-1-chenridong@huaweicloud.com> MIME-Version: 1.0 X-CM-TRANSID: gCh0CgD30oYKV1JnkkskDw--.31315S6 X-Coremail-Antispam: 1UD129KBjvJXoWxJF43Zry7Xry8tr43ArW5ZFb_yoW5GrWDpr ZrWa4UKw4kZFWxWrs8CF47Zr1rJF1vqFnFyw4Iqw1xCFnagFn0q34jya4jya48JrySqF4x Jr4qy3Z7CayUG37anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUmEb4IE77IF4wAFF20E14v26rWj6s0DM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28IrcIa0xkI8VA2jI8067AKxVWUAV Cq3wA2048vs2IY020Ec7CjxVAFwI0_Xr0E3s1l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0 rcxSw2x7M28EF7xvwVC0I7IYx2IY67AKxVW7JVWDJwA2z4x0Y4vE2Ix0cI8IcVCY1x0267 AKxVW8Jr0_Cr1UM28EF7xvwVC2z280aVAFwI0_GcCE3s1l84ACjcxK6I8E87Iv6xkF7I0E 14v26rxl6s0DM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7 xfMcIj6xIIjxv20xvE14v26r106r15McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Y z7v_Jr0_Gr1lF7xvr2IYc2Ij64vIr41lFIxGxcIEc7CjxVA2Y2ka0xkIwI1lc7CjxVAaw2 AFwI0_Jw0_GFyl42xK82IYc2Ij64vIr41l4c8EcI0Ec7CjxVAaw2AFwI0_Jw0_GFyl4I8I 3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s026x8GjcxK67AKxV WUGVWUWwC2zVAF1VAY17CE14v26r1q6r43MIIYrxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAF wI0_Jr0_JF4lIxAIcVC0I7IYx2IY6xkF7I0E14v26F4j6r4UJwCI42IY6xAIw20EY4v20x vaj40_Jr0_JF4lIxAIcVC2z280aVAFwI0_Jr0_Gr1lIxAIcVC2z280aVCY1x0267AKxVW8 JVW8JrUvcSsGvfC2KfnxnUUI43ZEXa7IUbXAw7UUUUU== X-CM-SenderInfo: hfkh02xlgr0w46kxt4xhlfz01xgou0bp/ X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: B73EE180010 X-Stat-Signature: wfj1bkthoio5nic4idan8f5kueedkwp3 X-Rspam-User: X-HE-Tag: 1733449491-805173 X-HE-Meta: U2FsdGVkX19QfS5bsw8MyFWTxEmiaAYDcxGnwvh4FXBBwwEB58lnyCcP9da+qhfQxHyh3T0R7siMkDPf9L17PUCCd6i1+2snnhWQZP3XgJr6vTMFLJ4YG4+gIfYVQBnL0NuqOakXS2wpRZ1nVwBpqmYajqei0n8ZRWbhmQnLtGgT4B1C94CRNUUMM7eHCqRfXqZcMEish2NB+f1fJaXldqqjXrqzBqYDx48+BYYN+wpvwdn+6vM1pS8V0yD0lRVjJ/z3bFJ59YlYXwuC24RWRHkeevsWYGZCzupSPSAZoJKtj+hosfAtAkI3BD3VZAa2BzX4mGdJDzvn8TXLkxVZZpapLATq/D3/f3tPpe4IaM6fPPgwjiiSWv3PYTWb/e8x544ou1BfHN5aKHYvLUmgqziAYOcECzWzzBD0CrMpTiCQLB2GNUoICXCEd3nJ3rdmvxJbYTArtWTH57RSFXe7syFnSOy+2jDSTaR55o1oXWCMMry8eWOGZ5gRfkS68LnrSeAef7VXw3zX4WZDjq2wq0KyMQwVFDRXausaBjtNsQ/3CqyS/FIRb5q+ZbeH+l0sc6iAN5M1x27ggsQOXqnhHey0gIqqpDz9MKrSlrxVoJFRpT4mBQxJD7O/R3qjkjpTlJ5uzY/HCNYhp+RQKAN4sGhBIfUlOw0BhQAxddIdQOvQESEYrHyFjGXEPQ+b43MajSe375R6hoddWdlII1Ht9j7ekOZVfC6Kapjndea54i8CA8Qj1iSrRxluvsvFVMfqMS29GoWg+t43XKIe/40i4VPZVnnBbQW/iydm5CKcR/PJUvdtGABUidsi2jXeSvr/HXFir9FmVPlK619o3e0QSaHDbcpYEectITIjF2sokxZgh/UQDVPlX7SEfna3UtOJZpil4I2Pqp7GM3cvKGJ9tkbGGbJoS9ff87G0ORs44gCql08FM2VNPLaGw0olol4OjboglMdYECOXgLW1HKf IFkXelzn OYGIp4ZSPV37AS15PxD08GlDZpHrvh/FXvXJrBTJy8B95Q6geIDoPH2zKYZ2k9m3BKbJPidmQLd3gvaEok0dLMlzPGO2rJhqOP5M79Me8KxUJ5dOqYk5gxTOwSl/8V9Qa3e+urrs+iLn16/z1VcV4Rk58vsVdt8ht39M+KEd8Lk213BOEzDVK2KNXDw== 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: List-Subscribe: List-Unsubscribe: From: Chen Ridong Factor out the '__refill_obj_stock' function to make the code more cohesive. Signed-off-by: Chen Ridong --- mm/memcontrol.c | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index f977e0be1c04..0c9331d7b606 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -2697,6 +2697,21 @@ void __memcg_kmem_uncharge_page(struct page *page, int order) obj_cgroup_put(objcg); } +/* If the cached_objcg was refilled, return true; otherwise, return false */ +static bool __refill_obj_stock(struct memcg_stock_pcp *stock, + struct obj_cgroup *objcg, struct obj_cgroup **old_objcg) +{ + if (READ_ONCE(stock->cached_objcg) != objcg) { + *old_objcg = drain_obj_stock(stock); + obj_cgroup_get(objcg); + stock->nr_bytes = atomic_read(&objcg->nr_charged_bytes) + ? atomic_xchg(&objcg->nr_charged_bytes, 0) : 0; + WRITE_ONCE(stock->cached_objcg, objcg); + return true; + } + return false; +} + static void mod_objcg_state(struct obj_cgroup *objcg, struct pglist_data *pgdat, enum node_stat_item idx, int nr) { @@ -2713,12 +2728,7 @@ static void mod_objcg_state(struct obj_cgroup *objcg, struct pglist_data *pgdat, * accumulating over a page of vmstat data or when pgdat or idx * changes. */ - if (READ_ONCE(stock->cached_objcg) != objcg) { - old = drain_obj_stock(stock); - obj_cgroup_get(objcg); - stock->nr_bytes = atomic_read(&objcg->nr_charged_bytes) - ? atomic_xchg(&objcg->nr_charged_bytes, 0) : 0; - WRITE_ONCE(stock->cached_objcg, objcg); + if (__refill_obj_stock(stock, objcg, &old)) { stock->cached_pgdat = pgdat; } else if (stock->cached_pgdat != pgdat) { /* Flush the existing cached vmstat data */ @@ -2871,14 +2881,9 @@ static void refill_obj_stock(struct obj_cgroup *objcg, unsigned int nr_bytes, local_lock_irqsave(&memcg_stock.stock_lock, flags); stock = this_cpu_ptr(&memcg_stock); - if (READ_ONCE(stock->cached_objcg) != objcg) { /* reset if necessary */ - old = drain_obj_stock(stock); - obj_cgroup_get(objcg); - WRITE_ONCE(stock->cached_objcg, objcg); - stock->nr_bytes = atomic_read(&objcg->nr_charged_bytes) - ? atomic_xchg(&objcg->nr_charged_bytes, 0) : 0; + if (__refill_obj_stock(stock, objcg, &old)) allow_uncharge = true; /* Allow uncharge when objcg changes */ - } + stock->nr_bytes += nr_bytes; if (allow_uncharge && (stock->nr_bytes > PAGE_SIZE)) {