From patchwork Mon Dec 5 01:01:03 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hugh Dickins X-Patchwork-Id: 29483 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp2019003wrr; Sun, 4 Dec 2022 17:44:07 -0800 (PST) X-Google-Smtp-Source: AA0mqf7HYctpETCpWjbFz9swGEvGZih0wd1ktfA69+tTq2HamSJ1S8DiSCR6ZUI0gyznFiCWso0E X-Received: by 2002:a05:6402:e8c:b0:468:89dd:aa0b with SMTP id h12-20020a0564020e8c00b0046889ddaa0bmr73206701eda.258.1670204647510; Sun, 04 Dec 2022 17:44:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670204647; cv=none; d=google.com; s=arc-20160816; b=ZQKcrl2di2jBUxkK77Ap+pS4xis28UsNr+k/iWq3tRx9eI++o25F5gsGwPwtvnBkzR d5rC8uJmxc63grgDD1Na2q1zBJVkJjM94nuYJZ466i623DgiOzP4GhDQk3SOdM4Q6cJE R8xdgBMgjyg1x4htZyLk+g+O1swdWdwmNwrbdKetgWbqBaUuM/4kllI1VGWiSLrlEclJ t3kNrwy1+L/FgiCzE74a4Lmz29piraAo8YusS2d1/IrRPJjvnex1ViBu6bXrs2HXJGQw AtZFyC/o3tao7uTtPYBbSNHh/hFjK3Nv1o/4qOaxOqifc6q3lxSemhK2bdiL3WbvW2oE SPHg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:message-id:subject:cc:to:from:date :dkim-signature; bh=XQiQTDYJ2jY/fTbAU6ju+a6cwLf1VHUEdZD/PoKH6D0=; b=yPPyQvl0TUvEF7DbL11RNbLa/LpOaMk2AeOQtTte+8QR75cjXv9SbIno1G9K5mPsKA C8VLXkTtTDXt1EzLlnqTW2HwXP43ELJqGUV5bwZtufYbfQFpB6DpY3nSsW9V+rHL2jG6 Wkfz6jBb3v0iK9J4IGRwvs+2MYWnGeSfajVNOqEkat/HHf65WaaaOWjmaWKIl9HX/ULK MTg1nUCdMj3bFZ5kmbk+B5i8wbh6yQM2l69iZxeKWsDcgF9RLoNE2p6kOHSFof/FN61i VrIVdolR4tkDxlxlM19aQhXrHKYbBZCvQwjWydw1WF/kGo0HRm8qFUnREPUuTHCEXsey cLxg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=Zg8MJRjf; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id hb42-20020a170907162a00b007bd112b2764si3151004ejc.381.2022.12.04.17.43.44; Sun, 04 Dec 2022 17:44:07 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=Zg8MJRjf; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S231164AbiLEBBM (ORCPT + 99 others); Sun, 4 Dec 2022 20:01:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43724 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230508AbiLEBBK (ORCPT ); Sun, 4 Dec 2022 20:01:10 -0500 Received: from mail-qv1-xf2d.google.com (mail-qv1-xf2d.google.com [IPv6:2607:f8b0:4864:20::f2d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C678812AC8 for ; Sun, 4 Dec 2022 17:01:06 -0800 (PST) Received: by mail-qv1-xf2d.google.com with SMTP id i12so7305251qvs.2 for ; Sun, 04 Dec 2022 17:01:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=mime-version:message-id:subject:cc:to:from:date:from:to:cc:subject :date:message-id:reply-to; bh=XQiQTDYJ2jY/fTbAU6ju+a6cwLf1VHUEdZD/PoKH6D0=; b=Zg8MJRjfsHEIk0G0RZ9mM9jssIp+D4MW8a9QIFQyMCw+6+QCRDGnT1jiw2jue1OAEF BsE2Svp+Gg9zHgen7JkXvd+jlbGlKw+Wh1VlKcWj8+qjYLNxzjsBzpeNo6PzW7LPOnt+ Y2NSPCppRVgbet5aFy6hNmRzAH57yk9zjwUCMPE8Rjts+zR2k6H+3egA1nUiC+DuAfK1 EvbLwTL4pgfRnzGsYR+HTdaAj+LLCIimd312sUDYO/MMl1vuf1r+IdmlhDDi3UIZX8jg 2p7l/11X0CNvW518g5V25ardOKcLsa2ZZS3Fk+Rr+KdIgpJ6RuO/jg27nU/XsOtJwpbw DTow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=mime-version:message-id:subject:cc:to:from:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=XQiQTDYJ2jY/fTbAU6ju+a6cwLf1VHUEdZD/PoKH6D0=; b=1orpS+yg1QJwNvULVusLCCeL9CBvessVdqddadj5e4yoLbDDeIKLm86fxitiSKCUIm 8c2x8dNIxkkCmWlrzkQkPoXwWNaNeYTC8fWz6OhE7nIpvJ2pdm2SqVGSFv/Fyxc1ZNR3 eUdcBm2UvZwITpc5QZf0vInOa90pUJAVzRbSW+RdgA0J6ZMhEceNLoMVsRQdmoyLZvwZ Nr4iN1rlNpC2nhJKGejDT1KC2MjdaCAKAM4Bgjk+p8zXDcaD52gotky05wTuGe4ozX8i U671K315mfBBqDTqZy1QZxECnSf2V5nRlT64N2vnFYHGyhT2SjFALUP2MpWIVDq9sHsh 0T9w== X-Gm-Message-State: ANoB5pk5xw73EF/Mgmw+wUME4OwD/YvSD5lnZIqZuQX18s2fj/oxqShX KNRkrHBEHuEicy6UVAjVszlUQw== X-Received: by 2002:ad4:4343:0:b0:4c7:b0f:675c with SMTP id q3-20020ad44343000000b004c70b0f675cmr25171954qvs.23.1670202065815; Sun, 04 Dec 2022 17:01:05 -0800 (PST) Received: from ripple.attlocal.net (172-10-233-147.lightspeed.sntcca.sbcglobal.net. [172.10.233.147]) by smtp.gmail.com with ESMTPSA id s14-20020a05620a254e00b006fb9bbb071fsm11147542qko.29.2022.12.04.17.01.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 04 Dec 2022 17:01:05 -0800 (PST) Date: Sun, 4 Dec 2022 17:01:03 -0800 (PST) From: Hugh Dickins X-X-Sender: hugh@ripple.attlocal.net To: Andrew Morton cc: Shakeel Butt , Michal Hocko , Roman Gushchin , Johannes Weiner , Yu Zhao , linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH] mm: memcg: fix swapcached stat accounting Message-ID: MIME-Version: 1.0 X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1751336508689760993?= X-GMAIL-MSGID: =?utf-8?q?1751336508689760993?= I'd been worried by high "swapcached" counts in memcg OOM reports, thought we had a problem freeing swapcache, but it was just the accounting that was wrong. Two issues: 1. When __remove_mapping() removes swapcache, __delete_from_swap_cache() relies on memcg_data for the right counts to be updated; but that had already been reset by mem_cgroup_swapout(). Swap those calls around - mem_cgroup_swapout() does not require the swapcached flag to be set. 6.1 commit ac35a4902374 ("mm: multi-gen LRU: minimal implementation") already made a similar swap for workingset_eviction(), but not for this. 2. memcg's "swapcached" count was added for memcg v2 stats, but displayed on OOM even for memcg v1: so mem_cgroup_move_account() ought to move it. Fixes: b6038942480e ("mm: memcg: add swapcache stat for memcg v2") Signed-off-by: Hugh Dickins Acked-by: Shakeel Butt Acked-by: Johannes Weiner --- mm/memcontrol.c | 6 ++++++ mm/vmscan.c | 3 +-- 2 files changed, 7 insertions(+), 2 deletions(-) --- 6.1-rc8/mm/memcontrol.c +++ linux/mm/memcontrol.c @@ -5741,6 +5741,12 @@ static int mem_cgroup_move_account(struc } } +#ifdef CONFIG_SWAP + if (folio_test_swapcache(folio)) { + __mod_lruvec_state(from_vec, NR_SWAPCACHE, -nr_pages); + __mod_lruvec_state(to_vec, NR_SWAPCACHE, nr_pages); + } +#endif if (folio_test_writeback(folio)) { __mod_lruvec_state(from_vec, NR_WRITEBACK, -nr_pages); __mod_lruvec_state(to_vec, NR_WRITEBACK, nr_pages); --- 6.1-rc8/mm/vmscan.c +++ linux/mm/vmscan.c @@ -1346,11 +1346,10 @@ static int __remove_mapping(struct addre if (folio_test_swapcache(folio)) { swp_entry_t swap = folio_swap_entry(folio); - /* get a shadow entry before mem_cgroup_swapout() clears folio_memcg() */ if (reclaimed && !mapping_exiting(mapping)) shadow = workingset_eviction(folio, target_memcg); - mem_cgroup_swapout(folio, swap); __delete_from_swap_cache(folio, swap, shadow); + mem_cgroup_swapout(folio, swap); xa_unlock_irq(&mapping->i_pages); put_swap_folio(folio, swap); } else {