Received: by 2002:a05:6358:16cc:b0:ea:6187:17c9 with SMTP id r12csp25622rwl; Wed, 4 Jan 2023 14:34:40 -0800 (PST) X-Google-Smtp-Source: AMrXdXuxSaXRAcaGsGLoUxsVSLFyKvVEAi8MKOq45+ap8xZEnp4ws53AENsDj6dd3xQ3UsHP+Rie X-Received: by 2002:a17:906:b00d:b0:7c1:435c:d777 with SMTP id v13-20020a170906b00d00b007c1435cd777mr40700140ejy.9.1672871680714; Wed, 04 Jan 2023 14:34:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672871680; cv=none; d=google.com; s=arc-20160816; b=RV8HOcpjHROX8jdMshP9XnesbgExCsiTmDI/cB8HGhJAqXY1Coz3Vd2+wN99g/yaZB 8fsacRQdT1U5D0HhT3Bg9yedpU6Jn6oHUUuSBSxhZjazMKmpstnLzTRGmupVCT32Hg4d 7d9dQAW9QFoPi85o4/WsIutkSoU0Ly6FDqNclS9SfuJTPNfYvQN1cNG6C0yapzwuTrnm sbenVmrQcb3Ww7OKEXCOzBGI3rRKlbfsWCXjNgJCmcsG0q50qndbsmIGT9KJJGApaa5P KpUiQQ01QjV7fUOhqDYxOUzdAc3A2R75h4SyG3hSUkyf9kPjtVvQC0vfRCUD06i3wPlQ 5hJg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=npFjL6Dds3DKnQo07iUTlbqXMaucW9/yvERNl8ZNQRo=; b=ZrsgHKkTu0t2QIFJhxoe4zFvB+l+w8DtYWblbuN+Se9Pmwhb27jbGYOlJQlWGCW9gE skOsxk30hlShzT+QXLTzNTdgMGx/dWhN3eTzHBDtxUw/8Dq6/U0V0DkLUSFH/8sY+EIa cjpGatXyv+Ts760bI5InjBDmDEIIIh46nttqcekAwfUioxXYkoTC76xFl2o12zfxGg4h wa5WXgN/Vw2eP3aAotLu8HYBE8jby6tM0upSONcZnNVNHzwMf5iy/s9uC4SXOJHMS6Ri l1qj7aSGAiGILgmjUrhtW33wYuSzlQOFfqjevH5piQm/cOF1YVw4Ygnn/IIOkIaNYCl8 MCKA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b="OWeh/xJa"; 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=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ek8-20020a056402370800b00485d0d6ea57si21880708edb.619.2023.01.04.14.34.27; Wed, 04 Jan 2023 14:34:40 -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=@gmail.com header.s=20210112 header.b="OWeh/xJa"; 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=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240071AbjADW36 (ORCPT + 56 others); Wed, 4 Jan 2023 17:29:58 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47734 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240297AbjADW3w (ORCPT ); Wed, 4 Jan 2023 17:29:52 -0500 Received: from mail-pj1-x102b.google.com (mail-pj1-x102b.google.com [IPv6:2607:f8b0:4864:20::102b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0466B42600 for ; Wed, 4 Jan 2023 14:29:46 -0800 (PST) Received: by mail-pj1-x102b.google.com with SMTP id o21so7645222pjw.0 for ; Wed, 04 Jan 2023 14:29:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=npFjL6Dds3DKnQo07iUTlbqXMaucW9/yvERNl8ZNQRo=; b=OWeh/xJasix7ucatObuWl3DONvbabYvNQFmjbkxy4TsWrHQ6sgQ4Q1RrAkB4fHWo+R QqcOI5wHYusIxypkiUE8QA01I5znwb5MuEjGOOxYlGRJXgJ+t/Y07y47zlCa+bDecmCa jRLDzay7BhzAr/uX7Bq2XoBrcJNGjwJAn35htNsjVF1VEApIDR1mSs2kCG3nZpWp7gOg Bv65mwAtANq3K4PBT7gLkZyUIoth4O9joCJSfs0ZH5tVn9bhQrv8h6OrMlNoPYCDJn8L sPOIgorTOJPWjB04nmHqo48GY30cOLowMhG9Ue3oL4PXSXkMHjI0kIOTZs49MzFZYsTj Zy4Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=npFjL6Dds3DKnQo07iUTlbqXMaucW9/yvERNl8ZNQRo=; b=w9Q3K+OySUwoQe9/21Us6gutRc1TDLk/eR1NLG6psZlVBNcmgyIsAKrgey/kQrayyD 4XdUdaQf2t/diZWU4I+2yZ50GEDh6Jmm70Rgl0L6Sci83C42FkXrmQOej7cH+FJqZ9oI 1k3PWONAd6UhT4WyEIZcghu+6HZXipZFSSxTCTCNEOU+V56L6gX0IMeG6rkwDC82E5oe FR0tLG8Q8JHVR50FOnfaoYJ03H/pVMVVcDS/Ac5IuNiz4raCvtmN5KUANZConDz8PLb7 zrhaGcRF0cfKyURuwQNDv1yVMIozygWW/Bg+115aBb/+HNB/RWAstL2QuVEgISpNuzVG gnKQ== X-Gm-Message-State: AFqh2koPz8dXsXi63u+gbjEY8VlQK+dzzpCTIceIqi13r+dFw8v2+koS fLUZjCwjuAhqJzCbVvJl4WQE5GBS+kk= X-Received: by 2002:a17:902:848d:b0:191:1e89:35de with SMTP id c13-20020a170902848d00b001911e8935demr52352601plo.9.1672871386139; Wed, 04 Jan 2023 14:29:46 -0800 (PST) Received: from localhost (fwdproxy-prn-012.fbsv.net. [2a03:2880:ff:c::face:b00c]) by smtp.gmail.com with ESMTPSA id im22-20020a170902bb1600b00192944e3650sm15456333plb.268.2023.01.04.14.29.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Jan 2023 14:29:45 -0800 (PST) From: Nhat Pham To: akpm@linux-foundation.org Cc: hannes@cmpxchg.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, bfoster@redhat.com, willy@infradead.org, kernel-team@meta.com Subject: [PATCH] workingset: fix confusion around eviction vs refault container Date: Wed, 4 Jan 2023 14:29:44 -0800 Message-Id: <20230104222944.2380117-1-nphamcs@gmail.com> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS 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 From: Johannes Weiner Refault decisions are made based on the lruvec where the page was evicted, as that determined its LRU order while it was alive. Stats and workingset aging must then occur on the lruvec of the new page, as that's the node and cgroup that experience the refault and that's the lruvec whose nonresident info ages out by a new resident page. Those lruvecs could be different when a page is shared between cgroups, or the refaulting page is allocated on a different node. There are currently two mix-ups: 1. When swap is available, the resident anon set must be considered when comparing the refault distance. The comparison is made against the right anon set, but the check for swap is not. When pages get evicted from a cgroup with swap, and refault in one without, this can incorrectly consider a hot refault as cold - and vice versa. Fix that by using the eviction cgroup for the swap check. 2. The stats and workingset age are updated against the wrong lruvec altogether: the right cgroup but the wrong NUMA node. When a page refaults on a different NUMA node, this will have confusing stats and distort the workingset age on a different lruvec - again possibly resulting in hot/cold misclassifications down the line. Fix the swap check and the refault pgdat to address both concerns. This was found during code review. It hasn't caused notable issues in production, suggesting that those refault-migrations are relatively rare in practice. Signed-off-by: Johannes Weiner Co-developed-by: Nhat Pham Signed-off-by: Nhat Pham --- mm/workingset.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mm/workingset.c b/mm/workingset.c index ae7e984b23c6..79585d55c45d 100644 --- a/mm/workingset.c +++ b/mm/workingset.c @@ -457,6 +457,7 @@ void workingset_refault(struct folio *folio, void *shadow) */ nr = folio_nr_pages(folio); memcg = folio_memcg(folio); + pgdat = folio_pgdat(folio); lruvec = mem_cgroup_lruvec(memcg, pgdat); mod_lruvec_state(lruvec, WORKINGSET_REFAULT_BASE + file, nr); @@ -474,7 +475,7 @@ void workingset_refault(struct folio *folio, void *shadow) workingset_size += lruvec_page_state(eviction_lruvec, NR_INACTIVE_FILE); } - if (mem_cgroup_get_nr_swap_pages(memcg) > 0) { + if (mem_cgroup_get_nr_swap_pages(eviction_memcg) > 0) { workingset_size += lruvec_page_state(eviction_lruvec, NR_ACTIVE_ANON); if (file) { -- 2.30.2