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: 13064056 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 581FFC4332F for ; Mon, 5 Dec 2022 01:01:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 81E048E0002; Sun, 4 Dec 2022 20:01:07 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7CD9D8E0001; Sun, 4 Dec 2022 20:01:07 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 695768E0002; Sun, 4 Dec 2022 20:01:07 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 5CC498E0001 for ; Sun, 4 Dec 2022 20:01:07 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 10845A08A5 for ; Mon, 5 Dec 2022 01:01:07 +0000 (UTC) X-FDA: 80206448574.25.01C2D88 Received: from mail-qv1-f51.google.com (mail-qv1-f51.google.com [209.85.219.51]) by imf23.hostedemail.com (Postfix) with ESMTP id 9A17A140004 for ; Mon, 5 Dec 2022 01:01:06 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=Zg8MJRjf; spf=pass (imf23.hostedemail.com: domain of hughd@google.com designates 209.85.219.51 as permitted sender) smtp.mailfrom=hughd@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1670202066; 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-type:content-transfer-encoding:in-reply-to: references:dkim-signature; bh=XQiQTDYJ2jY/fTbAU6ju+a6cwLf1VHUEdZD/PoKH6D0=; b=XU1JfRDX4lJesMzbLQaAbEZWX0+Ti2XdyK/XADus4SrvbhE4+d0M3WEOiY8uRkCJgzFluM ymhJHfzZBN4uS624qii8QjoNhp7mY2xcLlkT2s8OxaKzVfgYIVC2NLLrTsBxoK8ZES5g4O yjUEEMMiIiTj8pzncb9p7e4lp8vXR6Y= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=Zg8MJRjf; spf=pass (imf23.hostedemail.com: domain of hughd@google.com designates 209.85.219.51 as permitted sender) smtp.mailfrom=hughd@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1670202066; a=rsa-sha256; cv=none; b=ncA8lCjCVG1hScM6qgt9oIrRDt0mVZeKuV696oS66wMpcDpQ7F+VntLYsZmynLSgzztpeB oIa2U2/n2G/PaR1JS4nM2Y1I0SgXtoBxjKLHOHrR+ftWnTpfo6GZDtHq0TEl9z2zNPxAI5 S/e+BHPFvjUULMzcOZReG9WFH4sbdmI= Received: by mail-qv1-f51.google.com with SMTP id o12so7304519qvn.3 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=dcFVVvHJZA/zZJXP62Jqbl9zenJr5T6ZxRpyxlKi4y5rrRUHjIWzIvhL3thMxH+pON CDrLMjr8kpnJcNKIXFnPBBuaDn8DdOLcjzmNwnJORRMx+gKCu09A0QcALRzFDoBYQwME qaXPefJxwfp9KrIx9gSgXXXid3hprRJoLLh8Xlih3CWP6jcaEqdQ+t/JtakMR+osrhfW oqkzGUJT000JoHuojq9kHm6usHj0L0MNHBKwDVo/v2igDlmYUuiocVTfnlqKrAzqGaJ9 EehwFwMu3i7EU+OfWGnH8QP6e7siV8RtGrgg9BwTRzEzDzEm3ogujXCLcbHy6Ht9S9rf rdEQ== X-Gm-Message-State: ANoB5pkk+HcP1XGJvBr7bhytOw1MWwMNXM8Xk4smElLkeEsXRBoxM+IV knPmTcmF0DiSPR1uoncULur8+Q== X-Google-Smtp-Source: AA0mqf7hqtOBz59DeGLCi7IQRtjH8Fqm+J2TV230sQLghZnCBhi10bNddh2dbeznHyMciZ5mdHhDCw== 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-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 9A17A140004 X-Stat-Signature: 8yfuhw1uzortmafhqscr5deihryhbw8w X-Spamd-Result: default: False [2.70 / 9.00]; SORBS_IRL_BL(3.00)[209.85.219.51:from]; BAYES_HAM(-0.40)[71.17%]; MIME_GOOD(-0.10)[text/plain]; RCVD_NO_TLS_LAST(0.10)[]; BAD_REP_POLICIES(0.10)[]; TO_DN_SOME(0.00)[]; RCPT_COUNT_SEVEN(0.00)[8]; DMARC_POLICY_ALLOW(0.00)[google.com,reject]; DKIM_TRACE(0.00)[google.com:+]; ARC_NA(0.00)[]; MIME_TRACE(0.00)[0:+]; FROM_EQ_ENVFROM(0.00)[]; FROM_HAS_DN(0.00)[]; TO_MATCH_ENVRCPT_SOME(0.00)[]; MID_RHS_MATCH_FROM(0.00)[]; R_SPF_ALLOW(0.00)[+ip4:209.85.128.0/17]; RCVD_COUNT_THREE(0.00)[3]; R_DKIM_ALLOW(0.00)[google.com:s=20210112]; ARC_SIGNED(0.00)[hostedemail.com:s=arc-20220608:i=1]; PREVIOUSLY_DELIVERED(0.00)[linux-mm@kvack.org]; RCVD_VIA_SMTP_AUTH(0.00)[] X-Rspam-User: X-HE-Tag: 1670202066-121935 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: 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 {