From nobody Wed Dec 17 19:51:16 2025 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B03A215667B for ; Tue, 31 Dec 2024 04:35:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735619749; cv=none; b=rS9gKuhaz//ekMHoenGFA9WZPTyT89IxxcFxjOvQ1DyWExaVhrvjsSHntLrygjVp4KO4MSLeZYAvRYJNC+Z4YiY76XARBZP1hmdd790ZXV3W55CszAmQuUBBhbIBet7UR7QEMyXGs5EWze8YaFWdJY/XYpwZiAQ+7SJcxyZD4Tg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735619749; c=relaxed/simple; bh=RWveGCVh3Wwamff71MYhhhcXA2Seyh0CDZrRZOu7WKU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=CJalck19qqH3bKKDimOeFBGYriQBeMPhzko6g69rwX0fidaSfCqDCh17a06FSS299OwRN8bayi+hD9K6XIvnIkGxs0ffoKbv7cA1B1t6Q3qN6LcYOTLXivnQra5sCR6nItJqGci4M9F2XTkvGOZbz18/yh5tLhHVzQz16QdNT2Q= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--yuzhao.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=eZKkb/yO; arc=none smtp.client-ip=209.85.216.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--yuzhao.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="eZKkb/yO" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2ef9e4c5343so20994018a91.0 for ; Mon, 30 Dec 2024 20:35:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1735619747; x=1736224547; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=Xw2CHOQueA9zkMREwjqXXK1goFWTBSTiadQe8+3ME3M=; b=eZKkb/yOn6rf9G7nOKpmxbgziGt0QQkwECcnZ36D0nXD/B2aKh1/IU08S/H2yRh6YA RMxsw6eHqAOMW2oxiryQAgLiBwCSTP1xjGduOxI6KiIYlLF2650bkKdlNwFc6+vh2bHQ AU/St+Amo6UnRn4URUr6MAprYBwH5cCprXpPNKPoP/FG7nKWbTdEq1/oBGQzyVEC6OzV onOys8rqeKQDYZ+wuYosNqT9MSh8ERrbaheS/jTa39X+su75Gj4mULTAlbw4uvj5pfx3 wqYm7uYZdKJnhbHJ99vXFvujvVT5Ngib9JUmTMg0Pzxyq/87R4SR2PROhmlVjdMM01CX DPWw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735619747; x=1736224547; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Xw2CHOQueA9zkMREwjqXXK1goFWTBSTiadQe8+3ME3M=; b=gfTqO6illo4P5NxDeZhRXbNYNFbvwgkPqKgy0UXQ23yrP5mRC2gen9vo1JNuKi9TWM Avwd0o3MRA+NlLDQWK2kcBi5NmuPdfJzpKNQFTi23Vn1+PMgShWSQP3Xp0uwApQ0uMNI VxVc9+YltgtOBcg+qRX4EGCZOJaF3BAjsPJ4H1JJp8VA849RMH5bzBHHM0DgcvXe6V27 XM5KdsrTtU+UWIsPoxrouoVQu9pbg4iU0gqiHmKxgBYng4dY5WccvrWXUNwWrryaDhOp 3tZTzM0hPIeR6QKT7gx4iZR/wzfH51enuJJyIadfbVuILJ8hKm8iO3n0mu1hsO1st28f 2Slg== X-Forwarded-Encrypted: i=1; AJvYcCXXxfhy8RsRBskm8BcH9TT46jJNTNT8jDbYcCsiY3DAJowg5KIlDUGUNF3TUYQjBFUS0HYrf1vEyFox/DA=@vger.kernel.org X-Gm-Message-State: AOJu0YxV+nqL9GJIBJhqgr7P91LZ/kJbrn2dfN91uRoTkRJoMrY48oCX C/cKA1iNRb4tsX5zRqTv1Z9gdnnTpJA6eQMrzZ9ZdL4h3nz/fLPB+Ko4b0M/qP4M2MAABIIVEur 2ug== X-Google-Smtp-Source: AGHT+IFqFvRA79cO3VpI5IoQ9FD/iueUtKICiJreOXklTdeZ0vkOif4CjJ0SLo91oWp/vZ2d3gnF2M2cOO0= X-Received: from pjbpw17.prod.google.com ([2002:a17:90b:2791:b0:2ef:94c6:5048]) (user=yuzhao job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:524b:b0:2ee:9d49:3ae6 with SMTP id 98e67ed59e1d1-2f452e1d12fmr56370216a91.10.1735619747003; Mon, 30 Dec 2024 20:35:47 -0800 (PST) Date: Mon, 30 Dec 2024 21:35:32 -0700 In-Reply-To: <20241231043538.4075764-1-yuzhao@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241231043538.4075764-1-yuzhao@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20241231043538.4075764-2-yuzhao@google.com> Subject: [PATCH mm-unstable v4 1/7] mm/mglru: clean up workingset From: Yu Zhao To: Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Yu Zhao , Kalesh Singh Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Move VM_BUG_ON_FOLIO() to cover both the default and MGLRU paths. Also use a pair of rcu_read_lock() and rcu_read_unlock() within each path, to improve readability. This change should not have any side effects. Signed-off-by: Yu Zhao Tested-by: Kalesh Singh --- mm/workingset.c | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/mm/workingset.c b/mm/workingset.c index a4705e196545..ad181d1b8cf1 100644 --- a/mm/workingset.c +++ b/mm/workingset.c @@ -428,17 +428,17 @@ bool workingset_test_recent(void *shadow, bool file, = bool *workingset, struct pglist_data *pgdat; unsigned long eviction; =20 - rcu_read_lock(); - if (lru_gen_enabled()) { - bool recent =3D lru_gen_test_recent(shadow, file, - &eviction_lruvec, &eviction, workingset); + bool recent; =20 + rcu_read_lock(); + recent =3D lru_gen_test_recent(shadow, file, &eviction_lruvec, + &eviction, workingset); rcu_read_unlock(); return recent; } =20 - + rcu_read_lock(); unpack_shadow(shadow, &memcgid, &pgdat, &eviction, workingset); eviction <<=3D bucket_order; =20 @@ -459,14 +459,12 @@ bool workingset_test_recent(void *shadow, bool file, = bool *workingset, * configurations instead. */ eviction_memcg =3D mem_cgroup_from_id(memcgid); - if (!mem_cgroup_disabled() && - (!eviction_memcg || !mem_cgroup_tryget(eviction_memcg))) { - rcu_read_unlock(); + if (!mem_cgroup_tryget(eviction_memcg)) + eviction_memcg =3D NULL; + rcu_read_unlock(); + + if (!mem_cgroup_disabled() && !eviction_memcg) return false; - } - - rcu_read_unlock(); - /* * Flush stats (and potentially sleep) outside the RCU read section. * @@ -544,6 +542,8 @@ void workingset_refault(struct folio *folio, void *shad= ow) bool workingset; long nr; =20 + VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio); + if (lru_gen_enabled()) { lru_gen_refault(folio, shadow); return; @@ -558,7 +558,6 @@ void workingset_refault(struct folio *folio, void *shad= ow) * is actually experiencing the refault event. Make sure the folio is * locked to guarantee folio_memcg() stability throughout. */ - VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio); nr =3D folio_nr_pages(folio); memcg =3D folio_memcg(folio); pgdat =3D folio_pgdat(folio); --=20 2.47.1.613.gc27f4b7a9f-goog From nobody Wed Dec 17 19:51:16 2025 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6BC61175D29 for ; Tue, 31 Dec 2024 04:35:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735619752; cv=none; b=YzVAQxY3m3ekW5tt+NxrerCMUOtLss3tvTgvcXG5KUJPAIZbr0FuN+Xqk5U1MXVU151AAJKWjOpoUID3v8S7ALUGMNSH0OyfDRoR4oKS80sb0SOD95mYMATGtUPoU5aiV/K85iEfQ5jf3EWQuyy4Z4u27IZ8X6cQN63FwRM5l4k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735619752; c=relaxed/simple; bh=ZQi1mfxZn3uWJVcrBZifiX93S0ay564vYZ3QXBoREyg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=C/3EvlWJ+pdFkmf/5tFGGXfFVjawDZ7ov8V0yutHTTVUDg4bHH7IwEZ4SDBr23PxggPNA+7/BuUf7z4bBNA0AqnjE2kvr1qj6KJLnrsj5xbtRAJN+zsMLeaPUzKjs8ns8wsYKSYD0o0farv6tayMNvpGcjPH7DxYcAeZIWr8O7M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--yuzhao.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=1sjiLERE; arc=none smtp.client-ip=209.85.214.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--yuzhao.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="1sjiLERE" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-216387ddda8so125575445ad.3 for ; Mon, 30 Dec 2024 20:35:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1735619750; x=1736224550; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=n2jVLy+uRLHye3kRN9bJLrDt1hriY6TngJrphV7c4w0=; b=1sjiLEREU/E1Huiy948BGjX97kSMxdpVeHtNy/bSvFwVsojNYp3Bw4ruR4eH6rtNP2 U8gKLoEI+tlvOxlYMdAMisPWQQbb81csdTYfvhvdtj6urTC42/l9Cs9qESGBUGyG39Tl M0fEaC8p7cgdYi7rmsRbVH1YmyE0H13foJ9CA45LVNQor7H1LBhgYhiJNUW6RJbMTOt0 47hMBdI6jYpWihqPRAvuNN559Gzu66kVTdtik52EBwDkvssP3sTrBsRxvDschM3YSzeK 9B/FgV573LILUaafsdlzga8srdah+rTdIM+juw2wq9JJ7NAQQkfzF4wVg0i9VCZvUlOk h25g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735619750; x=1736224550; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=n2jVLy+uRLHye3kRN9bJLrDt1hriY6TngJrphV7c4w0=; b=mOwgODB5uve0jfEiJ0/fu2HSUq5E3laLRaXbddl+SH/2gLGg/1C7IygIaYYpQPrwPN S89XvCYxjmoBLrojrbqjcBU6cAlUg6XQsWes8qqERtd/K6/9IDeKTj2RVmnQWNuWBEuO 0DsIqi8FpNalvyQt17heQZ7rdCZLReLK+txJyEYESd7Zvg1NUnzzVHuwMvUarnPS8RS5 ocCBpPPBv9FCuiLdPU88WTFHTrE4ulyaNnWDIV7tukFo7bQA0mr/d3DlQ0nHOA08ZrFR vavvSRe/ftL7sU6kKr1pEs/NNf89Rx62OnbtvnyBLftKpIWldBW4q4STcsPyBKj6Am/T aMfg== X-Forwarded-Encrypted: i=1; AJvYcCWcFKrw+z4UskRIjgSyLhj5AKA+j5905ZM4/46BRGes9Bswv7/LmRTkLkgUamIBddB25Q4NQ1lmwT6iPDM=@vger.kernel.org X-Gm-Message-State: AOJu0Yx5oZMbYY6E4exx7qJTmsp26K75QqK2pz369Dbgbe21TQ20Irfu f26eVjHZaoJKDYu28eWse9edjPTb4FMJluVNtNPte8RY5K9mgWqtN1U2geZd2svNWwHjMjXzQqD 1sw== X-Google-Smtp-Source: AGHT+IEyFV9cXf/CY6ZxXUeDDPF7xVQGYuDgmn8dUSrnoiU1eyKeiE9Z7M5a9yhOQj+r6LPmuJijye0+/+0= X-Received: from pgtq9.prod.google.com ([2002:a65:6849:0:b0:845:b983:9e34]) (user=yuzhao job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:ebc6:b0:216:501e:e321 with SMTP id d9443c01a7336-219e6f2700cmr576287415ad.56.1735619749657; Mon, 30 Dec 2024 20:35:49 -0800 (PST) Date: Mon, 30 Dec 2024 21:35:33 -0700 In-Reply-To: <20241231043538.4075764-1-yuzhao@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241231043538.4075764-1-yuzhao@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20241231043538.4075764-3-yuzhao@google.com> Subject: [PATCH mm-unstable v4 2/7] mm/mglru: optimize deactivation From: Yu Zhao To: Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Yu Zhao , Bharata B Rao , Kalesh Singh Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Do not shuffle a folio in the deactivation paths if it is already in the oldest generation. This reduces the LRU lock contention. Before this patch, the contention is reproducible by FIO, e.g., fio -filename=3D/dev/nvme1n1p2 -direct=3D0 -thread -size=3D1024G \ -rwmixwrite=3D30 --norandommap --randrepeat=3D0 -ioengine=3Dsync \ -bs=3D4k -numjobs=3D400 -runtime=3D25000 --time_based \ -group_reporting -name=3Dmglru 98.96%--_raw_spin_lock_irqsave folio_lruvec_lock_irqsave | --98.78%--folio_batch_move_lru | --98.63%--deactivate_file_folio mapping_try_invalidate invalidate_mapping_pages invalidate_bdev blkdev_common_ioctl blkdev_ioctl After this patch, deactivate_file_folio() bails out early without taking the LRU lock. A side effect is that a folio can be left at the head of the oldest generation, rather than the tail. If reclaim happens at the same time, it cannot reclaim this folio immediately. Since there is no known correlation between truncation and reclaim, this side effect is considered insignificant. Reported-by: Bharata B Rao Closes: https://lore.kernel.org/CAOUHufawNerxqLm7L9Yywp3HJFiYVrYO26ePUb1jH-= qxNGWzyA@mail.gmail.com/ Signed-off-by: Yu Zhao Tested-by: Kalesh Singh --- mm/swap.c | 48 +++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 41 insertions(+), 7 deletions(-) diff --git a/mm/swap.c b/mm/swap.c index 3a01acfd5a89..649ef7f2b74b 100644 --- a/mm/swap.c +++ b/mm/swap.c @@ -379,7 +379,8 @@ static void __lru_cache_activate_folio(struct folio *fo= lio) } =20 #ifdef CONFIG_LRU_GEN -static void folio_inc_refs(struct folio *folio) + +static void lru_gen_inc_refs(struct folio *folio) { unsigned long new_flags, old_flags =3D READ_ONCE(folio->flags); =20 @@ -406,10 +407,34 @@ static void folio_inc_refs(struct folio *folio) new_flags |=3D old_flags & ~LRU_REFS_MASK; } while (!try_cmpxchg(&folio->flags, &old_flags, new_flags)); } -#else -static void folio_inc_refs(struct folio *folio) + +static bool lru_gen_clear_refs(struct folio *folio) { + struct lru_gen_folio *lrugen; + int gen =3D folio_lru_gen(folio); + int type =3D folio_is_file_lru(folio); + + if (gen < 0) + return true; + + set_mask_bits(&folio->flags, LRU_REFS_MASK | LRU_REFS_FLAGS, 0); + + lrugen =3D &folio_lruvec(folio)->lrugen; + /* whether can do without shuffling under the LRU lock */ + return gen =3D=3D lru_gen_from_seq(READ_ONCE(lrugen->min_seq[type])); } + +#else /* !CONFIG_LRU_GEN */ + +static void lru_gen_inc_refs(struct folio *folio) +{ +} + +static bool lru_gen_clear_refs(struct folio *folio) +{ + return false; +} + #endif /* CONFIG_LRU_GEN */ =20 /** @@ -428,7 +453,7 @@ static void folio_inc_refs(struct folio *folio) void folio_mark_accessed(struct folio *folio) { if (lru_gen_enabled()) { - folio_inc_refs(folio); + lru_gen_inc_refs(folio); return; } =20 @@ -524,7 +549,7 @@ void folio_add_lru_vma(struct folio *folio, struct vm_a= rea_struct *vma) */ static void lru_deactivate_file(struct lruvec *lruvec, struct folio *folio) { - bool active =3D folio_test_active(folio); + bool active =3D folio_test_active(folio) || lru_gen_enabled(); long nr_pages =3D folio_nr_pages(folio); =20 if (folio_test_unevictable(folio)) @@ -589,7 +614,10 @@ static void lru_lazyfree(struct lruvec *lruvec, struct= folio *folio) =20 lruvec_del_folio(lruvec, folio); folio_clear_active(folio); - folio_clear_referenced(folio); + if (lru_gen_enabled()) + lru_gen_clear_refs(folio); + else + folio_clear_referenced(folio); /* * Lazyfree folios are clean anonymous folios. They have * the swapbacked flag cleared, to distinguish them from normal @@ -657,6 +685,9 @@ void deactivate_file_folio(struct folio *folio) if (folio_test_unevictable(folio)) return; =20 + if (lru_gen_enabled() && lru_gen_clear_refs(folio)) + return; + folio_batch_add_and_move(folio, lru_deactivate_file, true); } =20 @@ -670,7 +701,10 @@ void deactivate_file_folio(struct folio *folio) */ void folio_deactivate(struct folio *folio) { - if (folio_test_unevictable(folio) || !(folio_test_active(folio) || lru_ge= n_enabled())) + if (folio_test_unevictable(folio)) + return; + + if (lru_gen_enabled() ? lru_gen_clear_refs(folio) : !folio_test_active(fo= lio)) return; =20 folio_batch_add_and_move(folio, lru_deactivate, true); --=20 2.47.1.613.gc27f4b7a9f-goog From nobody Wed Dec 17 19:51:16 2025 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 56579154433 for ; Tue, 31 Dec 2024 04:35:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735619755; cv=none; b=X1dEp/6SUHE49FXDsAh1+kOzSAzl0QoERyz7lw1TU2XRPJ5dWW/5NJ10p5ZJ7FGZH4wEMthiQxAxpfmgBry2+9PEirchqGi1TAMCKXIwR1pffAJCkh6SdmdRIei4I8Wo3XOeehaxvR3qeQNMt1L3eriKhbxTbPidTuDC/HjijsU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735619755; c=relaxed/simple; bh=JmurZNY3bKU74FjFIUf7Yt0rZJUtuY0d03M13taVH4M=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=nWhdI1qvE+6G4TR56Lyn4EX5jXyYra7FDqiEM3Muk8OmYsdclIA5gHzOWRyEOIDtUjB/n31a55btKZJC2d5n+bx/4QbWcNfsKAFflpru///B89BK9YfC2jbLzLDU3CH2C6NI8Y0PYVQi+KTU/9n+G2+I0VPlqSsy2Kr/5o8chuk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--yuzhao.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=c4C/YmFc; arc=none smtp.client-ip=209.85.216.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--yuzhao.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="c4C/YmFc" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2ee8ced572eso13429627a91.0 for ; Mon, 30 Dec 2024 20:35:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1735619752; x=1736224552; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=sDhUReo2deRGp+tnJMIYJBVB2YFMdmBRAUrx9f/kOiA=; b=c4C/YmFcXpBRzret4u7Q2V8GzZ8FgS87cxDjwdvlWOsxA+QVZlOzKuesRSQjsp0XvG YL507BK5V6X9fG22Udp4qy4TL5s4NPTsmRppaXmuDr5EOn59W3ibnNeRTiSdHnXa5yXd YXwgEBDhiLx3qe5bSUddPrLKpkCU66d+/siFxXT4NjZFUWSPHR4igBDSf3ion0vZOPiC ps2c2bo6cO5GGvA7DeHjfB8OpD7bAm4JaBNM47gxz9Bfqy/eF+Jb0s3QSCv1B4BqXSL/ Mhu+Z8gliFEqJY+3kQ6s4o41wU+iTIkqhjbl6BZDYQGIRqhjlFU4oTPEMJLR+BibwC1E dMXQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735619752; x=1736224552; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=sDhUReo2deRGp+tnJMIYJBVB2YFMdmBRAUrx9f/kOiA=; b=aTATpTAClEDCGNhj+r0+ypNi/cQPjigwhcv4K7ncJkm9ByCQyboN/9Y2Hx/sbjbe1r Wy1bvWi/rM/DS9YtkdWj+cr/hoekNZNw0/dJJy7Yjxr2b3hAV4QBVLJaWtVZeHj1o7zf ERrH8D7AGODxfCSzYLzOctN9FkpORPByyv8SJzfYlamNdD78FohmKJEH12Jb2QAq3fCv 2GKd0BItE/0odz8P7xuKnVEqwnb2pIyLxeI2dRC8sl4qoyeChkTCUlMrGz19dVNA5l5m HU568pWJWnVx4PTCz6r2BO/L7IwDcOiDfBGhDtL+ZgqOqsPstpMjNMShHYfy69e0fT3o nM3g== X-Forwarded-Encrypted: i=1; AJvYcCU9JzwLWYbrrcP3A5zzAJtNmpSh/oMZmHpqHFQh43cqltGQ/VTnd4WL0twCubvpEpYddLTXlOTHDuASp30=@vger.kernel.org X-Gm-Message-State: AOJu0Yw8b4p+Kd0vgiSageAREK8D/r146O1Ho0ZV868Azv4bIiZatEX4 NaBckEyw82jvmue2aFMR5GJMuFFgC6L9854pTrJ7zHjC9ZelDn2yW9LiTOxOgygRCUDZ7x8ZgEq 0lA== X-Google-Smtp-Source: AGHT+IG6nxstyXgWqSu+L2n9Zxz+hgGy42XY3ZsLF+ONO7heKN9bb4QBB98vkqXP6rvyE1kIrVxQ7Qx/GdU= X-Received: from pfxa12.prod.google.com ([2002:a05:6a00:1d0c:b0:725:d350:a304]) (user=yuzhao job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:8d8d:b0:725:e37d:cd36 with SMTP id d2e1a72fcca58-72abdd3c476mr58090892b3a.2.1735619752645; Mon, 30 Dec 2024 20:35:52 -0800 (PST) Date: Mon, 30 Dec 2024 21:35:34 -0700 In-Reply-To: <20241231043538.4075764-1-yuzhao@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241231043538.4075764-1-yuzhao@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20241231043538.4075764-4-yuzhao@google.com> Subject: [PATCH mm-unstable v4 3/7] mm/mglru: rework aging feedback From: Yu Zhao To: Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Yu Zhao , David Stevens , Kalesh Singh Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The aging feedback is based on both the number of generations and the distribution of folios in each generation. The number of generations is currently the distance between max_seq and anon min_seq. This is because anon min_seq is not allowed to move past file min_seq. The rationale for that is that file is always evictable whereas anon is not. However, for use cases where anon is a lot cheaper than file: 1. Anon in the second oldest generation can be a better choice than file in the oldest generation. 2. A large amount of file in the oldest generation can skew the distribution, making should_run_aging() return false negative. Allow anon and file min_seq to move independently, and use solely the number of generations as the feedback for aging. Specifically, when both anon and file are evictable, anon min_seq can now be greater than file min_seq, and therefore the number of generations becomes the distance between max_seq and min(min_seq[0],min_seq[1]). And should_run_aging() returns true if and only if the number of generations is less than MAX_NR_GENS. As the first step to the final optimization, this change by itself should not have userspace-visiable effects beyond performance. The next twos patch will take advantage of this change; the last patch in this series will better distribute folios across MAX_NR_GENS. Reported-by: David Stevens Signed-off-by: Yu Zhao Tested-by: Kalesh Singh --- include/linux/mmzone.h | 17 ++-- mm/vmscan.c | 200 ++++++++++++++++++----------------------- 2 files changed, 96 insertions(+), 121 deletions(-) diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h index b36124145a16..8245ecb0400b 100644 --- a/include/linux/mmzone.h +++ b/include/linux/mmzone.h @@ -421,12 +421,11 @@ enum { /* * The youngest generation number is stored in max_seq for both anon and f= ile * types as they are aged on an equal footing. The oldest generation numbe= rs are - * stored in min_seq[] separately for anon and file types as clean file pa= ges - * can be evicted regardless of swap constraints. - * - * Normally anon and file min_seq are in sync. But if swapping is constrai= ned, - * e.g., out of swap space, file min_seq is allowed to advance and leave a= non - * min_seq behind. + * stored in min_seq[] separately for anon and file types so that they can= be + * incremented independently. Ideally min_seq[] are kept in sync when both= anon + * and file types are evictable. However, to adapt to situations like extr= eme + * swappiness, they are allowed to be out of sync by at most + * MAX_NR_GENS-MIN_NR_GENS-1. * * The number of pages in each generation is eventually consistent and the= refore * can be transiently negative when reset_batch_size() is pending. @@ -446,8 +445,8 @@ struct lru_gen_folio { unsigned long avg_refaulted[ANON_AND_FILE][MAX_NR_TIERS]; /* the exponential moving average of evicted+protected */ unsigned long avg_total[ANON_AND_FILE][MAX_NR_TIERS]; - /* the first tier doesn't need protection, hence the minus one */ - unsigned long protected[NR_HIST_GENS][ANON_AND_FILE][MAX_NR_TIERS - 1]; + /* can only be modified under the LRU lock */ + unsigned long protected[NR_HIST_GENS][ANON_AND_FILE][MAX_NR_TIERS]; /* can be modified without holding the LRU lock */ atomic_long_t evicted[NR_HIST_GENS][ANON_AND_FILE][MAX_NR_TIERS]; atomic_long_t refaulted[NR_HIST_GENS][ANON_AND_FILE][MAX_NR_TIERS]; @@ -498,7 +497,7 @@ struct lru_gen_mm_walk { int mm_stats[NR_MM_STATS]; /* total batched items */ int batched; - bool can_swap; + int swappiness; bool force_scan; }; =20 diff --git a/mm/vmscan.c b/mm/vmscan.c index f236db86de8a..f767e3d34e73 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -2627,11 +2627,17 @@ static bool should_clear_pmd_young(void) READ_ONCE((lruvec)->lrugen.min_seq[LRU_GEN_FILE]), \ } =20 +#define evictable_min_seq(min_seq, swappiness) \ + min((min_seq)[!(swappiness)], (min_seq)[(swappiness) !=3D MAX_SWAPPINESS]) + #define for_each_gen_type_zone(gen, type, zone) \ for ((gen) =3D 0; (gen) < MAX_NR_GENS; (gen)++) \ for ((type) =3D 0; (type) < ANON_AND_FILE; (type)++) \ for ((zone) =3D 0; (zone) < MAX_NR_ZONES; (zone)++) =20 +#define for_each_evictable_type(type, swappiness) \ + for ((type) =3D !(swappiness); (type) <=3D ((swappiness) !=3D MAX_SWAPPIN= ESS); (type)++) + #define get_memcg_gen(seq) ((seq) % MEMCG_NR_GENS) #define get_memcg_bin(bin) ((bin) % MEMCG_NR_BINS) =20 @@ -2677,10 +2683,16 @@ static int get_nr_gens(struct lruvec *lruvec, int t= ype) =20 static bool __maybe_unused seq_is_valid(struct lruvec *lruvec) { - /* see the comment on lru_gen_folio */ - return get_nr_gens(lruvec, LRU_GEN_FILE) >=3D MIN_NR_GENS && - get_nr_gens(lruvec, LRU_GEN_FILE) <=3D get_nr_gens(lruvec, LRU_GEN= _ANON) && - get_nr_gens(lruvec, LRU_GEN_ANON) <=3D MAX_NR_GENS; + int type; + + for (type =3D 0; type < ANON_AND_FILE; type++) { + int n =3D get_nr_gens(lruvec, type); + + if (n < MIN_NR_GENS || n > MAX_NR_GENS) + return false; + } + + return true; } =20 /*************************************************************************= ***** @@ -3087,9 +3099,8 @@ static void read_ctrl_pos(struct lruvec *lruvec, int = type, int tier, int gain, pos->refaulted =3D lrugen->avg_refaulted[type][tier] + atomic_long_read(&lrugen->refaulted[hist][type][tier]); pos->total =3D lrugen->avg_total[type][tier] + + lrugen->protected[hist][type][tier] + atomic_long_read(&lrugen->evicted[hist][type][tier]); - if (tier) - pos->total +=3D lrugen->protected[hist][type][tier - 1]; pos->gain =3D gain; } =20 @@ -3116,17 +3127,15 @@ static void reset_ctrl_pos(struct lruvec *lruvec, i= nt type, bool carryover) WRITE_ONCE(lrugen->avg_refaulted[type][tier], sum / 2); =20 sum =3D lrugen->avg_total[type][tier] + + lrugen->protected[hist][type][tier] + atomic_long_read(&lrugen->evicted[hist][type][tier]); - if (tier) - sum +=3D lrugen->protected[hist][type][tier - 1]; WRITE_ONCE(lrugen->avg_total[type][tier], sum / 2); } =20 if (clear) { atomic_long_set(&lrugen->refaulted[hist][type][tier], 0); atomic_long_set(&lrugen->evicted[hist][type][tier], 0); - if (tier) - WRITE_ONCE(lrugen->protected[hist][type][tier - 1], 0); + WRITE_ONCE(lrugen->protected[hist][type][tier], 0); } } } @@ -3261,7 +3270,7 @@ static int should_skip_vma(unsigned long start, unsig= ned long end, struct mm_wal return true; =20 if (vma_is_anonymous(vma)) - return !walk->can_swap; + return !walk->swappiness; =20 if (WARN_ON_ONCE(!vma->vm_file || !vma->vm_file->f_mapping)) return true; @@ -3271,7 +3280,10 @@ static int should_skip_vma(unsigned long start, unsi= gned long end, struct mm_wal return true; =20 if (shmem_mapping(mapping)) - return !walk->can_swap; + return !walk->swappiness; + + if (walk->swappiness =3D=3D MAX_SWAPPINESS) + return true; =20 /* to exclude special mappings like dax, etc. */ return !mapping->a_ops->read_folio; @@ -3359,7 +3371,7 @@ static unsigned long get_pmd_pfn(pmd_t pmd, struct vm= _area_struct *vma, unsigned } =20 static struct folio *get_pfn_folio(unsigned long pfn, struct mem_cgroup *m= emcg, - struct pglist_data *pgdat, bool can_swap) + struct pglist_data *pgdat) { struct folio *folio; =20 @@ -3370,10 +3382,6 @@ static struct folio *get_pfn_folio(unsigned long pfn= , struct mem_cgroup *memcg, if (folio_memcg(folio) !=3D memcg) return NULL; =20 - /* file VMAs can contain anon pages from COW */ - if (!folio_is_file_lru(folio) && !can_swap) - return NULL; - return folio; } =20 @@ -3429,7 +3437,7 @@ static bool walk_pte_range(pmd_t *pmd, unsigned long = start, unsigned long end, if (pfn =3D=3D -1) continue; =20 - folio =3D get_pfn_folio(pfn, memcg, pgdat, walk->can_swap); + folio =3D get_pfn_folio(pfn, memcg, pgdat); if (!folio) continue; =20 @@ -3514,7 +3522,7 @@ static void walk_pmd_range_locked(pud_t *pud, unsigne= d long addr, struct vm_area if (pfn =3D=3D -1) goto next; =20 - folio =3D get_pfn_folio(pfn, memcg, pgdat, walk->can_swap); + folio =3D get_pfn_folio(pfn, memcg, pgdat); if (!folio) goto next; =20 @@ -3726,22 +3734,26 @@ static void clear_mm_walk(void) kfree(walk); } =20 -static bool inc_min_seq(struct lruvec *lruvec, int type, bool can_swap) +static bool inc_min_seq(struct lruvec *lruvec, int type, int swappiness) { int zone; int remaining =3D MAX_LRU_BATCH; struct lru_gen_folio *lrugen =3D &lruvec->lrugen; + int hist =3D lru_hist_from_seq(lrugen->min_seq[type]); int new_gen, old_gen =3D lru_gen_from_seq(lrugen->min_seq[type]); =20 - if (type =3D=3D LRU_GEN_ANON && !can_swap) + if (type ? swappiness =3D=3D MAX_SWAPPINESS : !swappiness) goto done; =20 - /* prevent cold/hot inversion if force_scan is true */ + /* prevent cold/hot inversion if the type is evictable */ for (zone =3D 0; zone < MAX_NR_ZONES; zone++) { struct list_head *head =3D &lrugen->folios[old_gen][type][zone]; =20 while (!list_empty(head)) { struct folio *folio =3D lru_to_folio(head); + int refs =3D folio_lru_refs(folio); + int tier =3D lru_tier_from_refs(refs); + int delta =3D folio_nr_pages(folio); =20 VM_WARN_ON_ONCE_FOLIO(folio_test_unevictable(folio), folio); VM_WARN_ON_ONCE_FOLIO(folio_test_active(folio), folio); @@ -3751,6 +3763,9 @@ static bool inc_min_seq(struct lruvec *lruvec, int ty= pe, bool can_swap) new_gen =3D folio_inc_gen(lruvec, folio, false); list_move_tail(&folio->lru, &lrugen->folios[new_gen][type][zone]); =20 + WRITE_ONCE(lrugen->protected[hist][type][tier], + lrugen->protected[hist][type][tier] + delta); + if (!--remaining) return false; } @@ -3762,7 +3777,7 @@ static bool inc_min_seq(struct lruvec *lruvec, int ty= pe, bool can_swap) return true; } =20 -static bool try_to_inc_min_seq(struct lruvec *lruvec, bool can_swap) +static bool try_to_inc_min_seq(struct lruvec *lruvec, int swappiness) { int gen, type, zone; bool success =3D false; @@ -3772,7 +3787,7 @@ static bool try_to_inc_min_seq(struct lruvec *lruvec,= bool can_swap) VM_WARN_ON_ONCE(!seq_is_valid(lruvec)); =20 /* find the oldest populated generation */ - for (type =3D !can_swap; type < ANON_AND_FILE; type++) { + for_each_evictable_type(type, swappiness) { while (min_seq[type] + MIN_NR_GENS <=3D lrugen->max_seq) { gen =3D lru_gen_from_seq(min_seq[type]); =20 @@ -3788,13 +3803,17 @@ static bool try_to_inc_min_seq(struct lruvec *lruve= c, bool can_swap) } =20 /* see the comment on lru_gen_folio */ - if (can_swap) { - min_seq[LRU_GEN_ANON] =3D min(min_seq[LRU_GEN_ANON], min_seq[LRU_GEN_FIL= E]); - min_seq[LRU_GEN_FILE] =3D max(min_seq[LRU_GEN_ANON], lrugen->min_seq[LRU= _GEN_FILE]); + if (swappiness && swappiness !=3D MAX_SWAPPINESS) { + unsigned long seq =3D lrugen->max_seq - MIN_NR_GENS; + + if (min_seq[LRU_GEN_ANON] > seq && min_seq[LRU_GEN_FILE] < seq) + min_seq[LRU_GEN_ANON] =3D seq; + else if (min_seq[LRU_GEN_FILE] > seq && min_seq[LRU_GEN_ANON] < seq) + min_seq[LRU_GEN_FILE] =3D seq; } =20 - for (type =3D !can_swap; type < ANON_AND_FILE; type++) { - if (min_seq[type] =3D=3D lrugen->min_seq[type]) + for_each_evictable_type(type, swappiness) { + if (min_seq[type] <=3D lrugen->min_seq[type]) continue; =20 reset_ctrl_pos(lruvec, type, true); @@ -3805,8 +3824,7 @@ static bool try_to_inc_min_seq(struct lruvec *lruvec,= bool can_swap) return success; } =20 -static bool inc_max_seq(struct lruvec *lruvec, unsigned long seq, - bool can_swap, bool force_scan) +static bool inc_max_seq(struct lruvec *lruvec, unsigned long seq, int swap= piness) { bool success; int prev, next; @@ -3824,13 +3842,11 @@ static bool inc_max_seq(struct lruvec *lruvec, unsi= gned long seq, if (!success) goto unlock; =20 - for (type =3D ANON_AND_FILE - 1; type >=3D 0; type--) { + for (type =3D 0; type < ANON_AND_FILE; type++) { if (get_nr_gens(lruvec, type) !=3D MAX_NR_GENS) continue; =20 - VM_WARN_ON_ONCE(!force_scan && (type =3D=3D LRU_GEN_FILE || can_swap)); - - if (inc_min_seq(lruvec, type, can_swap)) + if (inc_min_seq(lruvec, type, swappiness)) continue; =20 spin_unlock_irq(&lruvec->lru_lock); @@ -3874,7 +3890,7 @@ static bool inc_max_seq(struct lruvec *lruvec, unsign= ed long seq, } =20 static bool try_to_inc_max_seq(struct lruvec *lruvec, unsigned long seq, - bool can_swap, bool force_scan) + int swappiness, bool force_scan) { bool success; struct lru_gen_mm_walk *walk; @@ -3885,7 +3901,7 @@ static bool try_to_inc_max_seq(struct lruvec *lruvec,= unsigned long seq, VM_WARN_ON_ONCE(seq > READ_ONCE(lrugen->max_seq)); =20 if (!mm_state) - return inc_max_seq(lruvec, seq, can_swap, force_scan); + return inc_max_seq(lruvec, seq, swappiness); =20 /* see the comment in iterate_mm_list() */ if (seq <=3D READ_ONCE(mm_state->seq)) @@ -3910,7 +3926,7 @@ static bool try_to_inc_max_seq(struct lruvec *lruvec,= unsigned long seq, =20 walk->lruvec =3D lruvec; walk->seq =3D seq; - walk->can_swap =3D can_swap; + walk->swappiness =3D swappiness; walk->force_scan =3D force_scan; =20 do { @@ -3920,7 +3936,7 @@ static bool try_to_inc_max_seq(struct lruvec *lruvec,= unsigned long seq, } while (mm); done: if (success) { - success =3D inc_max_seq(lruvec, seq, can_swap, force_scan); + success =3D inc_max_seq(lruvec, seq, swappiness); WARN_ON_ONCE(!success); } =20 @@ -3961,13 +3977,13 @@ static bool lruvec_is_sizable(struct lruvec *lruvec= , struct scan_control *sc) { int gen, type, zone; unsigned long total =3D 0; - bool can_swap =3D get_swappiness(lruvec, sc); + int swappiness =3D get_swappiness(lruvec, sc); struct lru_gen_folio *lrugen =3D &lruvec->lrugen; struct mem_cgroup *memcg =3D lruvec_memcg(lruvec); DEFINE_MAX_SEQ(lruvec); DEFINE_MIN_SEQ(lruvec); =20 - for (type =3D !can_swap; type < ANON_AND_FILE; type++) { + for_each_evictable_type(type, swappiness) { unsigned long seq; =20 for (seq =3D min_seq[type]; seq <=3D max_seq; seq++) { @@ -3987,6 +4003,7 @@ static bool lruvec_is_reclaimable(struct lruvec *lruv= ec, struct scan_control *sc { int gen; unsigned long birth; + int swappiness =3D get_swappiness(lruvec, sc); struct mem_cgroup *memcg =3D lruvec_memcg(lruvec); DEFINE_MIN_SEQ(lruvec); =20 @@ -3996,8 +4013,7 @@ static bool lruvec_is_reclaimable(struct lruvec *lruv= ec, struct scan_control *sc if (!lruvec_is_sizable(lruvec, sc)) return false; =20 - /* see the comment on lru_gen_folio */ - gen =3D lru_gen_from_seq(min_seq[LRU_GEN_FILE]); + gen =3D lru_gen_from_seq(evictable_min_seq(min_seq, swappiness)); birth =3D READ_ONCE(lruvec->lrugen.timestamps[gen]); =20 return time_is_before_jiffies(birth + min_ttl); @@ -4064,7 +4080,6 @@ bool lru_gen_look_around(struct page_vma_mapped_walk = *pvmw) unsigned long addr =3D pvmw->address; struct vm_area_struct *vma =3D pvmw->vma; struct folio *folio =3D pfn_folio(pvmw->pfn); - bool can_swap =3D !folio_is_file_lru(folio); struct mem_cgroup *memcg =3D folio_memcg(folio); struct pglist_data *pgdat =3D folio_pgdat(folio); struct lruvec *lruvec =3D mem_cgroup_lruvec(memcg, pgdat); @@ -4117,7 +4132,7 @@ bool lru_gen_look_around(struct page_vma_mapped_walk = *pvmw) if (pfn =3D=3D -1) continue; =20 - folio =3D get_pfn_folio(pfn, memcg, pgdat, can_swap); + folio =3D get_pfn_folio(pfn, memcg, pgdat); if (!folio) continue; =20 @@ -4333,8 +4348,8 @@ static bool sort_folio(struct lruvec *lruvec, struct = folio *folio, struct scan_c gen =3D folio_inc_gen(lruvec, folio, false); list_move_tail(&folio->lru, &lrugen->folios[gen][type][zone]); =20 - WRITE_ONCE(lrugen->protected[hist][type][tier - 1], - lrugen->protected[hist][type][tier - 1] + delta); + WRITE_ONCE(lrugen->protected[hist][type][tier], + lrugen->protected[hist][type][tier] + delta); return true; } =20 @@ -4533,7 +4548,6 @@ static int isolate_folios(struct lruvec *lruvec, stru= ct scan_control *sc, int sw { int i; int type; - int scanned; int tier =3D -1; DEFINE_MIN_SEQ(lruvec); =20 @@ -4558,21 +4572,23 @@ static int isolate_folios(struct lruvec *lruvec, st= ruct scan_control *sc, int sw else type =3D get_type_to_scan(lruvec, swappiness, &tier); =20 - for (i =3D !swappiness; i < ANON_AND_FILE; i++) { + for_each_evictable_type(i, swappiness) { + int scanned; + if (tier < 0) tier =3D get_tier_idx(lruvec, type); =20 + *type_scanned =3D type; + scanned =3D scan_folios(lruvec, sc, type, tier, list); if (scanned) - break; + return scanned; =20 type =3D !type; tier =3D -1; } =20 - *type_scanned =3D type; - - return scanned; + return 0; } =20 static int evict_folios(struct lruvec *lruvec, struct scan_control *sc, in= t swappiness) @@ -4588,6 +4604,7 @@ static int evict_folios(struct lruvec *lruvec, struct= scan_control *sc, int swap struct reclaim_stat stat; struct lru_gen_mm_walk *walk; bool skip_retry =3D false; + struct lru_gen_folio *lrugen =3D &lruvec->lrugen; struct mem_cgroup *memcg =3D lruvec_memcg(lruvec); struct pglist_data *pgdat =3D lruvec_pgdat(lruvec); =20 @@ -4597,7 +4614,7 @@ static int evict_folios(struct lruvec *lruvec, struct= scan_control *sc, int swap =20 scanned +=3D try_to_inc_min_seq(lruvec, swappiness); =20 - if (get_nr_gens(lruvec, !swappiness) =3D=3D MIN_NR_GENS) + if (evictable_min_seq(lrugen->min_seq, swappiness) + MIN_NR_GENS > lrugen= ->max_seq) scanned =3D 0; =20 spin_unlock_irq(&lruvec->lru_lock); @@ -4669,63 +4686,32 @@ static int evict_folios(struct lruvec *lruvec, stru= ct scan_control *sc, int swap } =20 static bool should_run_aging(struct lruvec *lruvec, unsigned long max_seq, - bool can_swap, unsigned long *nr_to_scan) + int swappiness, unsigned long *nr_to_scan) { int gen, type, zone; - unsigned long old =3D 0; - unsigned long young =3D 0; - unsigned long total =3D 0; + unsigned long size =3D 0; struct lru_gen_folio *lrugen =3D &lruvec->lrugen; DEFINE_MIN_SEQ(lruvec); =20 - /* whether this lruvec is completely out of cold folios */ - if (min_seq[!can_swap] + MIN_NR_GENS > max_seq) { - *nr_to_scan =3D 0; + *nr_to_scan =3D 0; + /* have to run aging, since eviction is not possible anymore */ + if (evictable_min_seq(min_seq, swappiness) + MIN_NR_GENS > max_seq) return true; - } =20 - for (type =3D !can_swap; type < ANON_AND_FILE; type++) { + for_each_evictable_type(type, swappiness) { unsigned long seq; =20 for (seq =3D min_seq[type]; seq <=3D max_seq; seq++) { - unsigned long size =3D 0; - gen =3D lru_gen_from_seq(seq); =20 for (zone =3D 0; zone < MAX_NR_ZONES; zone++) size +=3D max(READ_ONCE(lrugen->nr_pages[gen][type][zone]), 0L); - - total +=3D size; - if (seq =3D=3D max_seq) - young +=3D size; - else if (seq + MIN_NR_GENS =3D=3D max_seq) - old +=3D size; } } =20 - *nr_to_scan =3D total; - - /* - * The aging tries to be lazy to reduce the overhead, while the eviction - * stalls when the number of generations reaches MIN_NR_GENS. Hence, the - * ideal number of generations is MIN_NR_GENS+1. - */ - if (min_seq[!can_swap] + MIN_NR_GENS < max_seq) - return false; - - /* - * It's also ideal to spread pages out evenly, i.e., 1/(MIN_NR_GENS+1) - * of the total number of pages for each generation. A reasonable range - * for this average portion is [1/MIN_NR_GENS, 1/(MIN_NR_GENS+2)]. The - * aging cares about the upper bound of hot pages, while the eviction - * cares about the lower bound of cold pages. - */ - if (young * MIN_NR_GENS > total) - return true; - if (old * (MIN_NR_GENS + 2) < total) - return true; - - return false; + *nr_to_scan =3D size; + /* better to run aging even though eviction is still possible */ + return evictable_min_seq(min_seq, swappiness) + MIN_NR_GENS =3D=3D max_se= q; } =20 /* @@ -4733,7 +4719,7 @@ static bool should_run_aging(struct lruvec *lruvec, u= nsigned long max_seq, * 1. Defer try_to_inc_max_seq() to workqueues to reduce latency for memcg * reclaim. */ -static long get_nr_to_scan(struct lruvec *lruvec, struct scan_control *sc,= bool can_swap) +static long get_nr_to_scan(struct lruvec *lruvec, struct scan_control *sc,= int swappiness) { bool success; unsigned long nr_to_scan; @@ -4743,7 +4729,7 @@ static long get_nr_to_scan(struct lruvec *lruvec, str= uct scan_control *sc, bool if (mem_cgroup_below_min(sc->target_mem_cgroup, memcg)) return -1; =20 - success =3D should_run_aging(lruvec, max_seq, can_swap, &nr_to_scan); + success =3D should_run_aging(lruvec, max_seq, swappiness, &nr_to_scan); =20 /* try to scrape all its memory if this memcg was deleted */ if (nr_to_scan && !mem_cgroup_online(memcg)) @@ -4754,7 +4740,7 @@ static long get_nr_to_scan(struct lruvec *lruvec, str= uct scan_control *sc, bool return nr_to_scan >> sc->priority; =20 /* stop scanning this lruvec as it's low on cold folios */ - return try_to_inc_max_seq(lruvec, max_seq, can_swap, false) ? -1 : 0; + return try_to_inc_max_seq(lruvec, max_seq, swappiness, false) ? -1 : 0; } =20 static bool should_abort_scan(struct lruvec *lruvec, struct scan_control *= sc) @@ -5298,8 +5284,7 @@ static void lru_gen_seq_show_full(struct seq_file *m,= struct lruvec *lruvec, s =3D "rep"; n[0] =3D atomic_long_read(&lrugen->refaulted[hist][type][tier]); n[1] =3D atomic_long_read(&lrugen->evicted[hist][type][tier]); - if (tier) - n[2] =3D READ_ONCE(lrugen->protected[hist][type][tier - 1]); + n[2] =3D READ_ONCE(lrugen->protected[hist][type][tier]); } =20 for (i =3D 0; i < 3; i++) @@ -5354,7 +5339,7 @@ static int lru_gen_seq_show(struct seq_file *m, void = *v) seq_printf(m, " node %5d\n", nid); =20 if (!full) - seq =3D min_seq[LRU_GEN_ANON]; + seq =3D evictable_min_seq(min_seq, MAX_SWAPPINESS / 2); else if (max_seq >=3D MAX_NR_GENS) seq =3D max_seq - MAX_NR_GENS + 1; else @@ -5394,23 +5379,14 @@ static const struct seq_operations lru_gen_seq_ops = =3D { }; =20 static int run_aging(struct lruvec *lruvec, unsigned long seq, - bool can_swap, bool force_scan) + int swappiness, bool force_scan) { DEFINE_MAX_SEQ(lruvec); - DEFINE_MIN_SEQ(lruvec); - - if (seq < max_seq) - return 0; =20 if (seq > max_seq) return -EINVAL; =20 - if (!force_scan && min_seq[!can_swap] + MAX_NR_GENS - 1 <=3D max_seq) - return -ERANGE; - - try_to_inc_max_seq(lruvec, max_seq, can_swap, force_scan); - - return 0; + return try_to_inc_max_seq(lruvec, max_seq, swappiness, force_scan) ? 0 : = -EEXIST; } =20 static int run_eviction(struct lruvec *lruvec, unsigned long seq, struct s= can_control *sc, @@ -5426,7 +5402,7 @@ static int run_eviction(struct lruvec *lruvec, unsign= ed long seq, struct scan_co while (!signal_pending(current)) { DEFINE_MIN_SEQ(lruvec); =20 - if (seq < min_seq[!swappiness]) + if (seq < evictable_min_seq(min_seq, swappiness)) return 0; =20 if (sc->nr_reclaimed >=3D nr_to_reclaim) --=20 2.47.1.613.gc27f4b7a9f-goog From nobody Wed Dec 17 19:51:16 2025 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CDDBE15EFA1 for ; Tue, 31 Dec 2024 04:35:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735619757; cv=none; b=Ih5Zft/0OQZSFmUNKfA//sx7qtJM1XPfnrIbihtkizwBd9NhLcJXptfHGcUzqIIfnCWUKKEAWw+gVjGGHc/fZJ7WCHp9BXuDXkFifhYyIM68yPoelk+QkTlm8V0uvAzu9JT+Yod5CLe1Hn0l5A2i8w1Hlumhtzx/Wlf/2QQCDEI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735619757; c=relaxed/simple; bh=q1WnSZgXmLYzutr+EunR5Q1Z3UubbeTqmVf960GbNbY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=a5PwxkSMkKxWgU9qPihf2Ja0t0tGMCc56ftmbQFyEKen0TauCzyQ4M2TyjMDEQJc+EBsWF7pHvzW1pzpHtqdf/w/k36qivjRpLV44ix8gsDpbtzRFE1LCeVRGssV1xANgGcGIM11KGSMymClua4GZXjEC8P4fhDN0CMar7XiF/g= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--yuzhao.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=vHVzVgMG; arc=none smtp.client-ip=209.85.216.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--yuzhao.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="vHVzVgMG" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2ef79d9c692so20669045a91.0 for ; Mon, 30 Dec 2024 20:35:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1735619755; x=1736224555; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=lq6dqRxgSDV3CyACy0+VqeWU26YT+ayL2zVnCSsSGnY=; b=vHVzVgMGSbY0RpOhzx+S09mSI4V1wJWg0LLsEPnf9R4qvH5ODvu1LDwp/5I9H+sgTa zUZs6ZcPuFL4qeb/mMtjPeTvoTsTq75XsYbvUDLb5fCYvnZv1x1iSlyD8bYASQQvVZrC a9pch3Gv8TNPaClqXJ2SvS8Sa1nVsQC0C42F6ll3vxJ/nGlM+k0bv//O9TRoY06WlzOV DtLlwVhD5l75D/COE+frz4lG6LAWgGHDELRh2EUdu7CmUttHScoE9sH2kcXpbYlW1o3Z FNrURFAYy6y0url5tL45Pjl5q4ig27sI/so9uo3HR2XvhewANAYYSLmEhR277EVHUFua r2Jg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735619755; x=1736224555; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=lq6dqRxgSDV3CyACy0+VqeWU26YT+ayL2zVnCSsSGnY=; b=CkxMzv/l6WJ1wTrMzrjjYyMVr+oEDoF1wpmY2vIpN87IpVCqDVY9awL5HdQF1saRxS R6g8esSNkAcHa4ANyGosSb14IqRI37gmfRXTDSzrf7GBgamwe2Mme7hJQZF2rjxLFDkZ hf2O54erJuH8L4HIjqutsVeOcCQwXpOi6IRPpfLqDUIliyxXzieb0AfEg55zP0bspg/e ZvISttuDy0DBEIYxYgU7pJwpFu/GbiccnS5dOFFEIAcKQX1vMIMvY7sfH+RcK8YXZd38 HCTSUs54/AVavB/5HsieZL9ckn2shU/0A9zGsfMnEzAsr1JjBb5cve4YyrwwmFEVtrdP AdTQ== X-Forwarded-Encrypted: i=1; AJvYcCX5EeCQdUfvf1mPdIl3p6b43uXCVY9CcS9rDGdiyMYB2fjenFWn4x6eXvgVPvuvfhmK5+grYUrAtxdFx1c=@vger.kernel.org X-Gm-Message-State: AOJu0YycZou/mFfQMFNApn/bZZqJEGXNzvxwA6LW0AokcKMXWCkL84rY yHzswE27H5fOIa3W/XYUfgURr+aj1C2RhhpiDHjA5ScTnqPKWiG9M9bZBdSI/FZ6hdiTm2d2t8j AaQ== X-Google-Smtp-Source: AGHT+IHeZyv9yGkkGSeakHyyxsc/LoAVc3WVdX/rKDrbygxdcMqZjnwE7jl/rLSwE6a6nOYHrpf3ke3Jfww= X-Received: from pfd7.prod.google.com ([2002:a05:6a00:a807:b0:727:2d74:d385]) (user=yuzhao job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:430d:b0:725:db34:6a8c with SMTP id d2e1a72fcca58-72abddbd4f7mr55420073b3a.13.1735619755260; Mon, 30 Dec 2024 20:35:55 -0800 (PST) Date: Mon, 30 Dec 2024 21:35:35 -0700 In-Reply-To: <20241231043538.4075764-1-yuzhao@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241231043538.4075764-1-yuzhao@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20241231043538.4075764-5-yuzhao@google.com> Subject: [PATCH mm-unstable v4 4/7] mm/mglru: rework type selection From: Yu Zhao To: Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Yu Zhao , David Stevens , Kalesh Singh Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" With anon and file min_seq being able to move independently, rework type selection so that it is based on the total refaults from all tiers of each type. Also allow a type to be selected until that type reaches MIN_NR_GENS, regardless of whether that type has a larger min_seq or not, to accommodate extreme swappiness. Since some tiers of a selected type can have higher refaults than the first tier of the other type, use a less larger gain factor 2:3 instead of 1:2, in order for those tiers in the selected type to be better protected. As an intermediate step to the final optimization, this change by itself should not have userspace-visiable effects beyond performance. Reported-by: David Stevens Signed-off-by: Yu Zhao Tested-by: Kalesh Singh --- mm/vmscan.c | 82 +++++++++++++++++------------------------------------ 1 file changed, 26 insertions(+), 56 deletions(-) diff --git a/mm/vmscan.c b/mm/vmscan.c index f767e3d34e73..a33221298fd0 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -3093,15 +3093,20 @@ struct ctrl_pos { static void read_ctrl_pos(struct lruvec *lruvec, int type, int tier, int g= ain, struct ctrl_pos *pos) { + int i; struct lru_gen_folio *lrugen =3D &lruvec->lrugen; int hist =3D lru_hist_from_seq(lrugen->min_seq[type]); =20 - pos->refaulted =3D lrugen->avg_refaulted[type][tier] + - atomic_long_read(&lrugen->refaulted[hist][type][tier]); - pos->total =3D lrugen->avg_total[type][tier] + - lrugen->protected[hist][type][tier] + - atomic_long_read(&lrugen->evicted[hist][type][tier]); pos->gain =3D gain; + pos->refaulted =3D pos->total =3D 0; + + for (i =3D tier % MAX_NR_TIERS; i <=3D min(tier, MAX_NR_TIERS - 1); i++) { + pos->refaulted +=3D lrugen->avg_refaulted[type][i] + + atomic_long_read(&lrugen->refaulted[hist][type][i]); + pos->total +=3D lrugen->avg_total[type][i] + + lrugen->protected[hist][type][i] + + atomic_long_read(&lrugen->evicted[hist][type][i]); + } } =20 static void reset_ctrl_pos(struct lruvec *lruvec, int type, bool carryover) @@ -4501,13 +4506,13 @@ static int get_tier_idx(struct lruvec *lruvec, int = type) struct ctrl_pos sp, pv; =20 /* - * To leave a margin for fluctuations, use a larger gain factor (1:2). + * To leave a margin for fluctuations, use a larger gain factor (2:3). * This value is chosen because any other tier would have at least twice * as many refaults as the first tier. */ - read_ctrl_pos(lruvec, type, 0, 1, &sp); + read_ctrl_pos(lruvec, type, 0, 2, &sp); for (tier =3D 1; tier < MAX_NR_TIERS; tier++) { - read_ctrl_pos(lruvec, type, tier, 2, &pv); + read_ctrl_pos(lruvec, type, tier, 3, &pv); if (!positive_ctrl_err(&sp, &pv)) break; } @@ -4515,68 +4520,34 @@ static int get_tier_idx(struct lruvec *lruvec, int = type) return tier - 1; } =20 -static int get_type_to_scan(struct lruvec *lruvec, int swappiness, int *ti= er_idx) +static int get_type_to_scan(struct lruvec *lruvec, int swappiness) { - int type, tier; struct ctrl_pos sp, pv; - int gain[ANON_AND_FILE] =3D { swappiness, MAX_SWAPPINESS - swappiness }; =20 + if (!swappiness) + return LRU_GEN_FILE; + + if (swappiness =3D=3D MAX_SWAPPINESS) + return LRU_GEN_ANON; /* - * Compare the first tier of anon with that of file to determine which - * type to scan. Also need to compare other tiers of the selected type - * with the first tier of the other type to determine the last tier (of - * the selected type) to evict. + * Compare the sum of all tiers of anon with that of file to determine + * which type to scan. */ - read_ctrl_pos(lruvec, LRU_GEN_ANON, 0, gain[LRU_GEN_ANON], &sp); - read_ctrl_pos(lruvec, LRU_GEN_FILE, 0, gain[LRU_GEN_FILE], &pv); - type =3D positive_ctrl_err(&sp, &pv); + read_ctrl_pos(lruvec, LRU_GEN_ANON, MAX_NR_TIERS, swappiness, &sp); + read_ctrl_pos(lruvec, LRU_GEN_FILE, MAX_NR_TIERS, MAX_SWAPPINESS - swappi= ness, &pv); =20 - read_ctrl_pos(lruvec, !type, 0, gain[!type], &sp); - for (tier =3D 1; tier < MAX_NR_TIERS; tier++) { - read_ctrl_pos(lruvec, type, tier, gain[type], &pv); - if (!positive_ctrl_err(&sp, &pv)) - break; - } - - *tier_idx =3D tier - 1; - - return type; + return positive_ctrl_err(&sp, &pv); } =20 static int isolate_folios(struct lruvec *lruvec, struct scan_control *sc, = int swappiness, int *type_scanned, struct list_head *list) { int i; - int type; - int tier =3D -1; - DEFINE_MIN_SEQ(lruvec); - - /* - * Try to make the obvious choice first, and if anon and file are both - * available from the same generation, - * 1. Interpret swappiness 1 as file first and MAX_SWAPPINESS as anon - * first. - * 2. If !__GFP_IO, file first since clean pagecache is more likely to - * exist than clean swapcache. - */ - if (!swappiness) - type =3D LRU_GEN_FILE; - else if (min_seq[LRU_GEN_ANON] < min_seq[LRU_GEN_FILE]) - type =3D LRU_GEN_ANON; - else if (swappiness =3D=3D 1) - type =3D LRU_GEN_FILE; - else if (swappiness =3D=3D MAX_SWAPPINESS) - type =3D LRU_GEN_ANON; - else if (!(sc->gfp_mask & __GFP_IO)) - type =3D LRU_GEN_FILE; - else - type =3D get_type_to_scan(lruvec, swappiness, &tier); + int type =3D get_type_to_scan(lruvec, swappiness); =20 for_each_evictable_type(i, swappiness) { int scanned; - - if (tier < 0) - tier =3D get_tier_idx(lruvec, type); + int tier =3D get_tier_idx(lruvec, type); =20 *type_scanned =3D type; =20 @@ -4585,7 +4556,6 @@ static int isolate_folios(struct lruvec *lruvec, stru= ct scan_control *sc, int sw return scanned; =20 type =3D !type; - tier =3D -1; } =20 return 0; --=20 2.47.1.613.gc27f4b7a9f-goog From nobody Wed Dec 17 19:51:16 2025 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DDCB1188010 for ; Tue, 31 Dec 2024 04:35:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735619760; cv=none; b=QjeUWKORCc9ZSuy78lXPnaB+GEv+NKVDLopVCIthQPDkb8Kms/t7jbfummrEwqMWnhFI6lFL2IZmD29j4tuGOX1SirjfdqB9Ox+mZgmBX2vLwgWHpC5JZZfARe/KsZCr/iqCajoPW5IqOc+hSu92GU3Me8EYpaqiymzgjD1K7VI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735619760; c=relaxed/simple; bh=uTp7bD44RNzwMxHC/uNni/sVAGSpZwMMVsfwcGnYVDQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=RSDVEuyk7M0eR0IUtfpZQ8uon3Dc01fy1U/ZENoglkPta13J6grprDycbkH8qIrMGQgSxKRlOeP98CJM5UjbQz0xS/RRGszCL1Q7lE5AC3qFSd0W+a9GSfk7cpjjBGKaFfvhtyS8X4iVzw+mkJynlrQlhHJdvP4J11//uPxzVQI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--yuzhao.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=09PoE1fx; arc=none smtp.client-ip=209.85.214.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--yuzhao.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="09PoE1fx" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-2161d185f04so96103355ad.3 for ; Mon, 30 Dec 2024 20:35:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1735619758; x=1736224558; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=o6XRtkbexDya+FE96V37S0c0XRTdkGfwnwLQlfXry/0=; b=09PoE1fxn/CkucqrPFzJ9Qsl19mQEdHbls4J0xjwWhpmmNgOFs3i4KRR9C2ZCNFqZf tdtVs2Lun+uVMdtjwGVjXCNJII1DTcwtObFdsGcclfN13a3a2iKMidTE8uYGyaza4uqi WlezcbEwfNcDjHWZvR6aQ2KzDi2OsYwtaEP1d+V26VWhRtrSXLm/6vm/aLQKHCg5L8dG Q7zDhJTiTpvfQR5b60Hy+QnQ7PBeLmWBX6Gi9S2SftSE89jfhnjZ2b2WmIbNzGLJsEDN SQOc9ko0BZpHwONLhFt4J75YNT2t0R9fcYuOEM2+nQpeP1AuCwDVNkuExMoEImNBs9bk 59ZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735619758; x=1736224558; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=o6XRtkbexDya+FE96V37S0c0XRTdkGfwnwLQlfXry/0=; b=kk0NbKAOCGaWvPRPR7ehBIJ8zC3fMfWchmSYeUX/MufkSBLzeK+46ijPq8OIMr1pPF gb/EF3eLGyanAsXj+IrTw2n1ateqzTO15WKpY7ld5E2JW2k99mBqjUe9BjAqWaQN7C5h AbX/K6aNupzjAuxpBwhXaRSekadbEPQeQ+9Xiv1YbQqotg6kmfuVGij99xvQtWy0sywN g41DQXHDxJqcumE8kEMoQEsE+J5I/Qxoc82udb9zVhKkM3sc6ij8roRdGtRo/kgaT5KU lNj7PhSoOYcmPBx53g5YPX6mlXfIknZwprPcpu4YzH0ISDcwTe9MG9t4ABi8LOET+JEK MO8Q== X-Forwarded-Encrypted: i=1; AJvYcCW1GDMPTkRcnKycm69bLbjRpJ07/5Hzx1YoBsTFHA47Bk56ZgwXUEj3Cjh2Ic7EcStlpxDD3GT8LIBIEgQ=@vger.kernel.org X-Gm-Message-State: AOJu0YyprlPIibxY/wpwY5k02c+5VUm4MHPvrtVWsJOXUmiZ15PBx2gi a8rbC9mgSVh+rmGFUQzy4dOo9KB8WvQNAjGv8qHUCjvbOltFZwKHVuTP0psiyfGX3Pdg+uHLbIo AsQ== X-Google-Smtp-Source: AGHT+IFGxXIKapCxHqzF+SSkn6gmP8awPE8NmQhmaf+NKsYjxZAWuTACPo/hzKbtWVqcyzBxJYfvlIdIpEw= X-Received: from plmm9.prod.google.com ([2002:a17:902:c449:b0:215:6f59:ff5f]) (user=yuzhao job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:d502:b0:216:4348:149d with SMTP id d9443c01a7336-219e6f25d60mr601318685ad.53.1735619758185; Mon, 30 Dec 2024 20:35:58 -0800 (PST) Date: Mon, 30 Dec 2024 21:35:36 -0700 In-Reply-To: <20241231043538.4075764-1-yuzhao@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241231043538.4075764-1-yuzhao@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20241231043538.4075764-6-yuzhao@google.com> Subject: [PATCH mm-unstable v4 5/7] mm/mglru: rework refault detection From: Yu Zhao To: Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Yu Zhao , Kairui Song , Kalesh Singh Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" With anon and file min_seq being able to move independently, rework workingset protection as well so that the comparison of refaults between anon and file is always on an equal footing. Specifically, make lru_gen_test_recent() return true for refaults happening within the distance of MAX_NR_GENS. For example, if min_seq of a type is max_seq-MIN_NR_GENS, refaults from min_seq-1, i.e., max_seq-MIN_NR_GENS-1, are also considered recent, since the distance max_seq-(max_seq-MIN_NR_GENS-1), i.e., MIN_NR_GENS+1 is less than MAX_NR_GENS. As an intermediate step to the final optimization, this change by itself should not have userspace-visiable effects beyond performance. Reported-by: Kairui Song Closes: https://lore.kernel.org/CAOUHufahuWcKf5f1Sg3emnqX+cODuR=3D2TQo7T4Gr= -QYLujn4RA@mail.gmail.com/ Signed-off-by: Yu Zhao Tested-by: Kalesh Singh --- mm/workingset.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/mm/workingset.c b/mm/workingset.c index ad181d1b8cf1..2c310c29f51e 100644 --- a/mm/workingset.c +++ b/mm/workingset.c @@ -260,11 +260,11 @@ static void *lru_gen_eviction(struct folio *folio) * Tests if the shadow entry is for a folio that was recently evicted. * Fills in @lruvec, @token, @workingset with the values unpacked from sha= dow. */ -static bool lru_gen_test_recent(void *shadow, bool file, struct lruvec **l= ruvec, +static bool lru_gen_test_recent(void *shadow, struct lruvec **lruvec, unsigned long *token, bool *workingset) { int memcg_id; - unsigned long min_seq; + unsigned long max_seq; struct mem_cgroup *memcg; struct pglist_data *pgdat; =20 @@ -273,8 +273,10 @@ static bool lru_gen_test_recent(void *shadow, bool fil= e, struct lruvec **lruvec, memcg =3D mem_cgroup_from_id(memcg_id); *lruvec =3D mem_cgroup_lruvec(memcg, pgdat); =20 - min_seq =3D READ_ONCE((*lruvec)->lrugen.min_seq[file]); - return (*token >> LRU_REFS_WIDTH) =3D=3D (min_seq & (EVICTION_MASK >> LRU= _REFS_WIDTH)); + max_seq =3D READ_ONCE((*lruvec)->lrugen.max_seq); + max_seq &=3D EVICTION_MASK >> LRU_REFS_WIDTH; + + return abs_diff(max_seq, *token >> LRU_REFS_WIDTH) < MAX_NR_GENS; } =20 static void lru_gen_refault(struct folio *folio, void *shadow) @@ -290,7 +292,7 @@ static void lru_gen_refault(struct folio *folio, void *= shadow) =20 rcu_read_lock(); =20 - recent =3D lru_gen_test_recent(shadow, type, &lruvec, &token, &workingset= ); + recent =3D lru_gen_test_recent(shadow, &lruvec, &token, &workingset); if (lruvec !=3D folio_lruvec(folio)) goto unlock; =20 @@ -331,7 +333,7 @@ static void *lru_gen_eviction(struct folio *folio) return NULL; } =20 -static bool lru_gen_test_recent(void *shadow, bool file, struct lruvec **l= ruvec, +static bool lru_gen_test_recent(void *shadow, struct lruvec **lruvec, unsigned long *token, bool *workingset) { return false; @@ -432,8 +434,7 @@ bool workingset_test_recent(void *shadow, bool file, bo= ol *workingset, bool recent; =20 rcu_read_lock(); - recent =3D lru_gen_test_recent(shadow, file, &eviction_lruvec, - &eviction, workingset); + recent =3D lru_gen_test_recent(shadow, &eviction_lruvec, &eviction, work= ingset); rcu_read_unlock(); return recent; } --=20 2.47.1.613.gc27f4b7a9f-goog From nobody Wed Dec 17 19:51:16 2025 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9F93F188A3A for ; Tue, 31 Dec 2024 04:36:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735619764; cv=none; b=n3SFqnklg811nlo65r/qX3G9+UTs5VJ38334RovdVkZh1CK/jxXnrlJxTEeelbT7F04oStgQeOL9w0llTB5o2irfgUdt8qc/UTUYnsII8SULmUiTPrHdkOAiz/WaHN6/9zGTkyPfnTunrE7qbrAxaG0zVwDvbGEEUQ9ljY45tk8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735619764; c=relaxed/simple; bh=gvDpAs5xvwSCyb39pIZs5CQO7usLnZ5mV5aHRcUGnTo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=G/IbumZtWYPsd3k/iTSViH2pa5z/WASrgl0jHHfbgLM1bywju68nbUE5U0KPP/EylcdkX1riiry4bFMCWhfxWQjNgmGlqCrlztnXLQQHJ7bnve3/ZQNykp1H0Mg65C3dVGqWQdUiq/p97fVccArQMzJJhHpj6Bw8HmJIUmuyTVY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--yuzhao.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=MRhkRoYr; arc=none smtp.client-ip=209.85.214.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--yuzhao.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="MRhkRoYr" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-2165433e229so132387965ad.1 for ; Mon, 30 Dec 2024 20:36:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1735619761; x=1736224561; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=HKwjES3e0jLy2VuhFIOMlzL3YWtlXtEvY7LLG+FXeWU=; b=MRhkRoYrBTNcaAdeop9bvfcDdkF9d9LThp72RrbSB80BdJ9x4np6f7ADEYytnS7lj0 P1rSCU55CXznn8wurwVeP9PzMo+wuk2ys0L+RsT1OnK+V/XiwkY7c5Zs3Hejt5a21m7K XGVZjPhJ1k4pWlxdDPMBNTkkXQAlq3QxXU9R1BX1/0GKNDmmWRKqre501WdxEPfVpx2C PHW5GJSPKUc9GARbk4PDOMnT09NLIrjqLbZW87Qrlrgx9wL/GFQ8/jSCAuCWt6kZ3q8w B/UOtIliT73kgCNOCu9bZfoDQYrDsmtxgcdm6OKsk+XJCQ9E7Hn51vyftXU+WjftZWtO y1dQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735619761; x=1736224561; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=HKwjES3e0jLy2VuhFIOMlzL3YWtlXtEvY7LLG+FXeWU=; b=QfKPZQbIJQYSvuqfcI1CvDyA5SyYiR5ct2eiXmR/q8PVFUo5GMDeXLPT73By9fjCUy SlhcKbwNBn3Sajd++OQFbJzBDEse8RW2pbWWdF7OCoAzPuLZzmwD9JV+Z5r4sazN8z6n YKL/3tYLFhgn0hnZFApG8Y61nnFXe2fnjTOELoOZxlIdZxZnaxLz8JtgVRobnGoaFqIY sRCSZZ/3QXV/pFGnGB6xA2DTI4GZLTUCi6texn654l1mTOozREqBYUKeJ3yFR3eSCcOc f2tagvqK/2GeMkIsJC9ziwKlCdgQkDXy2NeB7JqEqer6eHWAfWdxg7xVQZLD/3eYGlLk +SBQ== X-Forwarded-Encrypted: i=1; AJvYcCU+H+pjmaMM3y701tO1tz/uHdJemKwg5EIbkU0/5ahwNm+x88i1l4KeMRKYKchSzxnPWbojIv1MBASfQEc=@vger.kernel.org X-Gm-Message-State: AOJu0YwFTDVWmOfyx3aItu15DcebAaDkZ+71jcaG+5z45A3tPIeEa+MF NkvHt1L566CRo+epKIiA+dO8niuow+S/b6nAPAqpItpeTU/bA1XFLuY8rmoNcYRxA4KmVmZV7TU bxg== X-Google-Smtp-Source: AGHT+IHWy99TyDT8NaxCGIoVaFrDEtF9zWOWP/0IUrjxMV7CP85GkRQziNTadZKOeAd6kPsRyTnxbmlSQC4= X-Received: from pgjs19.prod.google.com ([2002:a63:f053:0:b0:800:502a:791c]) (user=yuzhao job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:78a5:b0:1e1:a0b6:9882 with SMTP id adf61e73a8af0-1e5e046331fmr54638220637.17.1735619760939; Mon, 30 Dec 2024 20:36:00 -0800 (PST) Date: Mon, 30 Dec 2024 21:35:37 -0700 In-Reply-To: <20241231043538.4075764-1-yuzhao@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241231043538.4075764-1-yuzhao@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20241231043538.4075764-7-yuzhao@google.com> Subject: [PATCH mm-unstable v4 6/7] mm/mglru: rework workingset protection From: Yu Zhao To: Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Yu Zhao , Kairui Song , Kalesh Singh Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" With the aging feedback no longer considering the distribution of folios in each generation, rework workingset protection to better distribute folios across MAX_NR_GENS. This is achieved by reusing PG_workingset and PG_referenced/LRU_REFS_FLAGS in a slightly different way. For folios accessed multiple times through file descriptors, make lru_gen_inc_refs() set additional bits of LRU_REFS_WIDTH in folio->flags after PG_referenced, then PG_workingset after LRU_REFS_WIDTH. After all its bits are set, i.e., LRU_REFS_FLAGS|BIT(PG_workingset), a folio is lazily promoted into the second oldest generation in the eviction path. And when folio_inc_gen() does that, it clears LRU_REFS_FLAGS so that lru_gen_inc_refs() can start over. For this case, LRU_REFS_MASK is only valid when PG_referenced is set. For folios accessed multiple times through page tables, folio_update_gen() from a page table walk or lru_gen_set_refs() from a rmap walk sets PG_referenced after the accessed bit is cleared for the first time. Thereafter, those two paths set PG_workingset and promote folios to the youngest generation. Like folio_inc_gen(), when folio_update_gen() does that, it also clears PG_referenced. For this case, LRU_REFS_MASK is not used. For both of the cases, after PG_workingset is set on a folio, it remains until this folio is either reclaimed, or "deactivated" by lru_gen_clear_refs(). It can be set again if lru_gen_test_recent() returns true upon a refault. When adding folios to the LRU lists, lru_gen_folio_seq() distributes them as follows: +---------------------------------+---------------------------------+ | Accessed thru page tables | Accessed thru file descriptors | +---------------------------------+---------------------------------+ | PG_active (set while isolated) | | +----------------+----------------+----------------+----------------+ | PG_workingset | PG_referenced | PG_workingset | LRU_REFS_FLAGS | +---------------------------------+---------------------------------+ |<--------- MIN_NR_GENS --------->| | |<-------------------------- MAX_NR_GENS -------------------------->| After this patch, some typical client and server workloads showed improvements under heavy memory pressure. For example, Python TPC-C, which was used to benchmark a different approach [1] to better detect refault distances, showed a significant decrease in total refaults: Before After Change Time (seconds) 10801 10801 0% Executed (transactions) 41472 43663 +5% workingset_nodes 109070 120244 +10% workingset_refault_anon 5019627 7281831 +45% workingset_refault_file 1294678786 554855564 -57% workingset_refault_total 1299698413 562137395 -57% [1] https://lore.kernel.org/20230920190244.16839-1-ryncsn@gmail.com/ Reported-by: Kairui Song Closes: https://lore.kernel.org/CAOUHufahuWcKf5f1Sg3emnqX+cODuR=3D2TQo7T4Gr= -QYLujn4RA@mail.gmail.com/ Signed-off-by: Yu Zhao Tested-by: Kalesh Singh --- include/linux/mm_inline.h | 88 +++++++++++------------ include/linux/mmzone.h | 82 +++++++++++++-------- mm/swap.c | 24 +++---- mm/vmscan.c | 147 ++++++++++++++++++++++---------------- mm/workingset.c | 29 ++++---- 5 files changed, 204 insertions(+), 166 deletions(-) diff --git a/include/linux/mm_inline.h b/include/linux/mm_inline.h index 34e5097182a0..f9157a0c42a5 100644 --- a/include/linux/mm_inline.h +++ b/include/linux/mm_inline.h @@ -133,31 +133,25 @@ static inline int lru_hist_from_seq(unsigned long seq) return seq % NR_HIST_GENS; } =20 -static inline int lru_tier_from_refs(int refs) +static inline int lru_tier_from_refs(int refs, bool workingset) { VM_WARN_ON_ONCE(refs > BIT(LRU_REFS_WIDTH)); =20 - /* see the comment in folio_lru_refs() */ - return order_base_2(refs + 1); + /* see the comment on MAX_NR_TIERS */ + return workingset ? MAX_NR_TIERS - 1 : order_base_2(refs); } =20 static inline int folio_lru_refs(struct folio *folio) { unsigned long flags =3D READ_ONCE(folio->flags); - bool workingset =3D flags & BIT(PG_workingset); =20 + if (!(flags & BIT(PG_referenced))) + return 0; /* - * Return the number of accesses beyond PG_referenced, i.e., N-1 if the - * total number of accesses is N>1, since N=3D0,1 both map to the first - * tier. lru_tier_from_refs() will account for this off-by-one. Also see - * the comment on MAX_NR_TIERS. + * Return the total number of accesses including PG_referenced. Also see + * the comment on LRU_REFS_FLAGS. */ - return ((flags & LRU_REFS_MASK) >> LRU_REFS_PGOFF) + workingset; -} - -static inline void folio_clear_lru_refs(struct folio *folio) -{ - set_mask_bits(&folio->flags, LRU_REFS_MASK | LRU_REFS_FLAGS, 0); + return ((flags & LRU_REFS_MASK) >> LRU_REFS_PGOFF) + 1; } =20 static inline int folio_lru_gen(struct folio *folio) @@ -223,11 +217,43 @@ static inline void lru_gen_update_size(struct lruvec = *lruvec, struct folio *foli VM_WARN_ON_ONCE(lru_gen_is_active(lruvec, old_gen) && !lru_gen_is_active(= lruvec, new_gen)); } =20 +static inline unsigned long lru_gen_folio_seq(struct lruvec *lruvec, struc= t folio *folio, + bool reclaiming) +{ + int gen; + int type =3D folio_is_file_lru(folio); + struct lru_gen_folio *lrugen =3D &lruvec->lrugen; + + /* + * +-----------------------------------+---------------------------------= --+ + * | Accessed through page tables and | Accessed through file descriptor= s | + * | promoted by folio_update_gen() | and protected by folio_inc_gen()= | + * +-----------------------------------+---------------------------------= --+ + * | PG_active (set while isolated) | = | + * +-----------------+-----------------+-----------------+---------------= --+ + * | PG_workingset | PG_referenced | PG_workingset | LRU_REFS_FLAG= S | + * +-----------------------------------+---------------------------------= --+ + * |<---------- MIN_NR_GENS ---------->| = | + * |<---------------------------- MAX_NR_GENS ---------------------------= ->| + */ + if (folio_test_active(folio)) + gen =3D MIN_NR_GENS - folio_test_workingset(folio); + else if (reclaiming) + gen =3D MAX_NR_GENS; + else if ((!folio_is_file_lru(folio) && !folio_test_swapcache(folio)) || + (folio_test_reclaim(folio) && + (folio_test_dirty(folio) || folio_test_writeback(folio)))) + gen =3D MIN_NR_GENS; + else + gen =3D MAX_NR_GENS - folio_test_workingset(folio); + + return max(READ_ONCE(lrugen->max_seq) - gen + 1, READ_ONCE(lrugen->min_se= q[type])); +} + static inline bool lru_gen_add_folio(struct lruvec *lruvec, struct folio *= folio, bool reclaiming) { unsigned long seq; unsigned long flags; - unsigned long mask; int gen =3D folio_lru_gen(folio); int type =3D folio_is_file_lru(folio); int zone =3D folio_zonenum(folio); @@ -237,40 +263,12 @@ static inline bool lru_gen_add_folio(struct lruvec *l= ruvec, struct folio *folio, =20 if (folio_test_unevictable(folio) || !lrugen->enabled) return false; - /* - * There are four common cases for this page: - * 1. If it's hot, i.e., freshly faulted in, add it to the youngest - * generation, and it's protected over the rest below. - * 2. If it can't be evicted immediately, i.e., a dirty page pending - * writeback, add it to the second youngest generation. - * 3. If it should be evicted first, e.g., cold and clean from - * folio_rotate_reclaimable(), add it to the oldest generation. - * 4. Everything else falls between 2 & 3 above and is added to the - * second oldest generation if it's considered inactive, or the - * oldest generation otherwise. See lru_gen_is_active(). - */ - if (folio_test_active(folio)) - seq =3D lrugen->max_seq; - else if ((type =3D=3D LRU_GEN_ANON && !folio_test_swapcache(folio)) || - (folio_test_reclaim(folio) && - (folio_test_dirty(folio) || folio_test_writeback(folio)))) - seq =3D lrugen->max_seq - 1; - else if (reclaiming || lrugen->min_seq[type] + MIN_NR_GENS >=3D lrugen->m= ax_seq) - seq =3D lrugen->min_seq[type]; - else - seq =3D lrugen->min_seq[type] + 1; =20 + seq =3D lru_gen_folio_seq(lruvec, folio, reclaiming); gen =3D lru_gen_from_seq(seq); flags =3D (gen + 1UL) << LRU_GEN_PGOFF; /* see the comment on MIN_NR_GENS about PG_active */ - mask =3D LRU_GEN_MASK; - /* - * Don't clear PG_workingset here because it can affect PSI accounting - * if the activation is due to workingset refault. - */ - if (folio_test_active(folio)) - mask |=3D LRU_REFS_MASK | BIT(PG_referenced) | BIT(PG_active); - set_mask_bits(&folio->flags, mask, flags); + set_mask_bits(&folio->flags, LRU_GEN_MASK | BIT(PG_active), flags); =20 lru_gen_update_size(lruvec, folio, -1, gen); /* for folio_rotate_reclaimable() */ diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h index 8245ecb0400b..9540b41894da 100644 --- a/include/linux/mmzone.h +++ b/include/linux/mmzone.h @@ -332,66 +332,88 @@ enum lruvec_flags { #endif /* !__GENERATING_BOUNDS_H */ =20 /* - * Evictable pages are divided into multiple generations. The youngest and= the + * Evictable folios are divided into multiple generations. The youngest an= d the * oldest generation numbers, max_seq and min_seq, are monotonically incre= asing. * They form a sliding window of a variable size [MIN_NR_GENS, MAX_NR_GENS= ]. An * offset within MAX_NR_GENS, i.e., gen, indexes the LRU list of the * corresponding generation. The gen counter in folio->flags stores gen+1 = while - * a page is on one of lrugen->folios[]. Otherwise it stores 0. + * a folio is on one of lrugen->folios[]. Otherwise it stores 0. * - * A page is added to the youngest generation on faulting. The aging needs= to - * check the accessed bit at least twice before handing this page over to = the - * eviction. The first check takes care of the accessed bit set on the ini= tial - * fault; the second check makes sure this page hasn't been used since the= n. - * This process, AKA second chance, requires a minimum of two generations, - * hence MIN_NR_GENS. And to maintain ABI compatibility with the active/in= active - * LRU, e.g., /proc/vmstat, these two generations are considered active; t= he - * rest of generations, if they exist, are considered inactive. See - * lru_gen_is_active(). + * After a folio is faulted in, the aging needs to check the accessed bit = at + * least twice before handing this folio over to the eviction. The first c= heck + * clears the accessed bit from the initial fault; the second check makes = sure + * this folio hasn't been used since then. This process, AKA second chance, + * requires a minimum of two generations, hence MIN_NR_GENS. And to mainta= in ABI + * compatibility with the active/inactive LRU, e.g., /proc/vmstat, these t= wo + * generations are considered active; the rest of generations, if they exi= st, + * are considered inactive. See lru_gen_is_active(). * - * PG_active is always cleared while a page is on one of lrugen->folios[] = so - * that the aging needs not to worry about it. And it's set again when a p= age - * considered active is isolated for non-reclaiming purposes, e.g., migrat= ion. - * See lru_gen_add_folio() and lru_gen_del_folio(). + * PG_active is always cleared while a folio is on one of lrugen->folios[]= so + * that the sliding window needs not to worry about it. And it's set again= when + * a folio considered active is isolated for non-reclaiming purposes, e.g., + * migration. See lru_gen_add_folio() and lru_gen_del_folio(). * * MAX_NR_GENS is set to 4 so that the multi-gen LRU can support twice the * number of categories of the active/inactive LRU when keeping track of * accesses through page tables. This requires order_base_2(MAX_NR_GENS+1)= bits - * in folio->flags. + * in folio->flags, masked by LRU_GEN_MASK. */ #define MIN_NR_GENS 2U #define MAX_NR_GENS 4U =20 /* - * Each generation is divided into multiple tiers. A page accessed N times - * through file descriptors is in tier order_base_2(N). A page in the firs= t tier - * (N=3D0,1) is marked by PG_referenced unless it was faulted in through p= age - * tables or read ahead. A page in any other tier (N>1) is marked by - * PG_referenced and PG_workingset. This implies a minimum of two tiers is - * supported without using additional bits in folio->flags. + * Each generation is divided into multiple tiers. A folio accessed N times + * through file descriptors is in tier order_base_2(N). A folio in the fir= st + * tier (N=3D0,1) is marked by PG_referenced unless it was faulted in thro= ugh page + * tables or read ahead. A folio in the last tier (MAX_NR_TIERS-1) is mark= ed by + * PG_workingset. A folio in any other tier (1flags. * * In contrast to moving across generations which requires the LRU lock, m= oving * across tiers only involves atomic operations on folio->flags and theref= ore * has a negligible cost in the buffered access path. In the eviction path, - * comparisons of refaulted/(evicted+protected) from the first tier and the - * rest infer whether pages accessed multiple times through file descripto= rs - * are statistically hot and thus worth protecting. + * comparisons of refaulted/(evicted+protected) from the first tier and th= e rest + * infer whether folios accessed multiple times through file descriptors a= re + * statistically hot and thus worth protecting. * * MAX_NR_TIERS is set to 4 so that the multi-gen LRU can support twice the * number of categories of the active/inactive LRU when keeping track of * accesses through file descriptors. This uses MAX_NR_TIERS-2 spare bits = in - * folio->flags. + * folio->flags, masked by LRU_REFS_MASK. */ #define MAX_NR_TIERS 4U =20 #ifndef __GENERATING_BOUNDS_H =20 -struct lruvec; -struct page_vma_mapped_walk; - #define LRU_GEN_MASK ((BIT(LRU_GEN_WIDTH) - 1) << LRU_GEN_PGOFF) #define LRU_REFS_MASK ((BIT(LRU_REFS_WIDTH) - 1) << LRU_REFS_PGOFF) =20 +/* + * For folios accessed multiple times through file descriptors, + * lru_gen_inc_refs() sets additional bits of LRU_REFS_WIDTH in folio->fla= gs + * after PG_referenced, then PG_workingset after LRU_REFS_WIDTH. After all= its + * bits are set, i.e., LRU_REFS_FLAGS|BIT(PG_workingset), a folio is lazily + * promoted into the second oldest generation in the eviction path. And wh= en + * folio_inc_gen() does that, it clears LRU_REFS_FLAGS so that + * lru_gen_inc_refs() can start over. Note that for this case, LRU_REFS_MA= SK is + * only valid when PG_referenced is set. + * + * For folios accessed multiple times through page tables, folio_update_ge= n() + * from a page table walk or lru_gen_set_refs() from a rmap walk sets + * PG_referenced after the accessed bit is cleared for the first time. + * Thereafter, those two paths set PG_workingset and promote folios to the + * youngest generation. Like folio_inc_gen(), folio_update_gen() also clea= rs + * PG_referenced. Note that for this case, LRU_REFS_MASK is not used. + * + * For both cases above, after PG_workingset is set on a folio, it remains= until + * this folio is either reclaimed, or "deactivated" by lru_gen_clear_refs(= ). It + * can be set again if lru_gen_test_recent() returns true upon a refault. + */ +#define LRU_REFS_FLAGS (LRU_REFS_MASK | BIT(PG_referenced)) + +struct lruvec; +struct page_vma_mapped_walk; + #ifdef CONFIG_LRU_GEN =20 enum { @@ -406,8 +428,6 @@ enum { NR_LRU_GEN_CAPS }; =20 -#define LRU_REFS_FLAGS (BIT(PG_referenced) | BIT(PG_workingset)) - #define MIN_LRU_BATCH BITS_PER_LONG #define MAX_LRU_BATCH (MIN_LRU_BATCH * 64) =20 diff --git a/mm/swap.c b/mm/swap.c index 649ef7f2b74b..746a5ceba42c 100644 --- a/mm/swap.c +++ b/mm/swap.c @@ -387,24 +387,20 @@ static void lru_gen_inc_refs(struct folio *folio) if (folio_test_unevictable(folio)) return; =20 + /* see the comment on LRU_REFS_FLAGS */ if (!folio_test_referenced(folio)) { - folio_set_referenced(folio); + set_mask_bits(&folio->flags, LRU_REFS_MASK, BIT(PG_referenced)); return; } =20 - if (!folio_test_workingset(folio)) { - folio_set_workingset(folio); - return; - } - - /* see the comment on MAX_NR_TIERS */ do { - new_flags =3D old_flags & LRU_REFS_MASK; - if (new_flags =3D=3D LRU_REFS_MASK) - break; + if ((old_flags & LRU_REFS_MASK) =3D=3D LRU_REFS_MASK) { + if (!folio_test_workingset(folio)) + folio_set_workingset(folio); + return; + } =20 - new_flags +=3D BIT(LRU_REFS_PGOFF); - new_flags |=3D old_flags & ~LRU_REFS_MASK; + new_flags =3D old_flags + BIT(LRU_REFS_PGOFF); } while (!try_cmpxchg(&folio->flags, &old_flags, new_flags)); } =20 @@ -417,7 +413,7 @@ static bool lru_gen_clear_refs(struct folio *folio) if (gen < 0) return true; =20 - set_mask_bits(&folio->flags, LRU_REFS_MASK | LRU_REFS_FLAGS, 0); + set_mask_bits(&folio->flags, LRU_REFS_FLAGS | BIT(PG_workingset), 0); =20 lrugen =3D &folio_lruvec(folio)->lrugen; /* whether can do without shuffling under the LRU lock */ @@ -499,7 +495,7 @@ void folio_add_lru(struct folio *folio) folio_test_unevictable(folio), folio); VM_BUG_ON_FOLIO(folio_test_lru(folio), folio); =20 - /* see the comment in lru_gen_add_folio() */ + /* see the comment in lru_gen_folio_seq() */ if (lru_gen_enabled() && !folio_test_unevictable(folio) && lru_gen_in_fault() && !(current->flags & PF_MEMALLOC)) folio_set_active(folio); diff --git a/mm/vmscan.c b/mm/vmscan.c index a33221298fd0..74bc85fc7cdf 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -862,6 +862,31 @@ enum folio_references { FOLIOREF_ACTIVATE, }; =20 +#ifdef CONFIG_LRU_GEN +/* + * Only used on a mapped folio in the eviction (rmap walk) path, where pro= motion + * needs to be done by taking the folio off the LRU list and then adding i= t back + * with PG_active set. In contrast, the aging (page table walk) path uses + * folio_update_gen(). + */ +static bool lru_gen_set_refs(struct folio *folio) +{ + /* see the comment on LRU_REFS_FLAGS */ + if (!folio_test_referenced(folio) && !folio_test_workingset(folio)) { + set_mask_bits(&folio->flags, LRU_REFS_MASK, BIT(PG_referenced)); + return false; + } + + set_mask_bits(&folio->flags, LRU_REFS_FLAGS, BIT(PG_workingset)); + return true; +} +#else +static bool lru_gen_set_refs(struct folio *folio) +{ + return false; +} +#endif /* CONFIG_LRU_GEN */ + static enum folio_references folio_check_references(struct folio *folio, struct scan_control *sc) { @@ -870,7 +895,6 @@ static enum folio_references folio_check_references(str= uct folio *folio, =20 referenced_ptes =3D folio_referenced(folio, 1, sc->target_mem_cgroup, &vm_flags); - referenced_folio =3D folio_test_clear_referenced(folio); =20 /* * The supposedly reclaimable folio was found to be in a VM_LOCKED vma. @@ -888,6 +912,15 @@ static enum folio_references folio_check_references(st= ruct folio *folio, if (referenced_ptes =3D=3D -1) return FOLIOREF_KEEP; =20 + if (lru_gen_enabled()) { + if (!referenced_ptes) + return FOLIOREF_RECLAIM; + + return lru_gen_set_refs(folio) ? FOLIOREF_ACTIVATE : FOLIOREF_KEEP; + } + + referenced_folio =3D folio_test_clear_referenced(folio); + if (referenced_ptes) { /* * All mapped folios start out with page table @@ -1092,11 +1125,6 @@ static unsigned int shrink_folio_list(struct list_he= ad *folio_list, if (!sc->may_unmap && folio_mapped(folio)) goto keep_locked; =20 - /* folio_update_gen() tried to promote this page? */ - if (lru_gen_enabled() && !ignore_references && - folio_mapped(folio) && folio_test_referenced(folio)) - goto keep_locked; - /* * The number of dirty pages determines if a node is marked * reclaim_congested. kswapd will stall and start writing @@ -3167,16 +3195,19 @@ static int folio_update_gen(struct folio *folio, in= t gen) =20 VM_WARN_ON_ONCE(gen >=3D MAX_NR_GENS); =20 + /* see the comment on LRU_REFS_FLAGS */ + if (!folio_test_referenced(folio) && !folio_test_workingset(folio)) { + set_mask_bits(&folio->flags, LRU_REFS_MASK, BIT(PG_referenced)); + return -1; + } + do { /* lru_gen_del_folio() has isolated this page? */ - if (!(old_flags & LRU_GEN_MASK)) { - /* for shrink_folio_list() */ - new_flags =3D old_flags | BIT(PG_referenced); - continue; - } + if (!(old_flags & LRU_GEN_MASK)) + return -1; =20 - new_flags =3D old_flags & ~(LRU_GEN_MASK | LRU_REFS_MASK | LRU_REFS_FLAG= S); - new_flags |=3D (gen + 1UL) << LRU_GEN_PGOFF; + new_flags =3D old_flags & ~(LRU_GEN_MASK | LRU_REFS_FLAGS); + new_flags |=3D ((gen + 1UL) << LRU_GEN_PGOFF) | BIT(PG_workingset); } while (!try_cmpxchg(&folio->flags, &old_flags, new_flags)); =20 return ((old_flags & LRU_GEN_MASK) >> LRU_GEN_PGOFF) - 1; @@ -3200,7 +3231,7 @@ static int folio_inc_gen(struct lruvec *lruvec, struc= t folio *folio, bool reclai =20 new_gen =3D (old_gen + 1) % MAX_NR_GENS; =20 - new_flags =3D old_flags & ~(LRU_GEN_MASK | LRU_REFS_MASK | LRU_REFS_FLAG= S); + new_flags =3D old_flags & ~(LRU_GEN_MASK | LRU_REFS_FLAGS); new_flags |=3D (new_gen + 1UL) << LRU_GEN_PGOFF; /* for folio_end_writeback() */ if (reclaiming) @@ -3378,9 +3409,11 @@ static unsigned long get_pmd_pfn(pmd_t pmd, struct v= m_area_struct *vma, unsigned static struct folio *get_pfn_folio(unsigned long pfn, struct mem_cgroup *m= emcg, struct pglist_data *pgdat) { - struct folio *folio; + struct folio *folio =3D pfn_folio(pfn); + + if (folio_lru_gen(folio) < 0) + return NULL; =20 - folio =3D pfn_folio(pfn); if (folio_nid(folio) !=3D pgdat->node_id) return NULL; =20 @@ -3757,8 +3790,7 @@ static bool inc_min_seq(struct lruvec *lruvec, int ty= pe, int swappiness) while (!list_empty(head)) { struct folio *folio =3D lru_to_folio(head); int refs =3D folio_lru_refs(folio); - int tier =3D lru_tier_from_refs(refs); - int delta =3D folio_nr_pages(folio); + bool workingset =3D folio_test_workingset(folio); =20 VM_WARN_ON_ONCE_FOLIO(folio_test_unevictable(folio), folio); VM_WARN_ON_ONCE_FOLIO(folio_test_active(folio), folio); @@ -3768,8 +3800,14 @@ static bool inc_min_seq(struct lruvec *lruvec, int t= ype, int swappiness) new_gen =3D folio_inc_gen(lruvec, folio, false); list_move_tail(&folio->lru, &lrugen->folios[new_gen][type][zone]); =20 - WRITE_ONCE(lrugen->protected[hist][type][tier], - lrugen->protected[hist][type][tier] + delta); + /* don't count the workingset being lazily promoted */ + if (refs + workingset !=3D BIT(LRU_REFS_WIDTH) + 1) { + int tier =3D lru_tier_from_refs(refs, workingset); + int delta =3D folio_nr_pages(folio); + + WRITE_ONCE(lrugen->protected[hist][type][tier], + lrugen->protected[hist][type][tier] + delta); + } =20 if (!--remaining) return false; @@ -4155,16 +4193,10 @@ bool lru_gen_look_around(struct page_vma_mapped_wal= k *pvmw) old_gen =3D folio_update_gen(folio, new_gen); if (old_gen >=3D 0 && old_gen !=3D new_gen) update_batch_size(walk, folio, old_gen, new_gen); - - continue; - } - - old_gen =3D folio_lru_gen(folio); - if (old_gen < 0) - folio_set_referenced(folio); - else if (old_gen !=3D new_gen) { - folio_clear_lru_refs(folio); - folio_activate(folio); + } else if (lru_gen_set_refs(folio)) { + old_gen =3D folio_lru_gen(folio); + if (old_gen >=3D 0 && old_gen !=3D new_gen) + folio_activate(folio); } } =20 @@ -4325,7 +4357,8 @@ static bool sort_folio(struct lruvec *lruvec, struct = folio *folio, struct scan_c int zone =3D folio_zonenum(folio); int delta =3D folio_nr_pages(folio); int refs =3D folio_lru_refs(folio); - int tier =3D lru_tier_from_refs(refs); + bool workingset =3D folio_test_workingset(folio); + int tier =3D lru_tier_from_refs(refs, workingset); struct lru_gen_folio *lrugen =3D &lruvec->lrugen; =20 VM_WARN_ON_ONCE_FOLIO(gen >=3D MAX_NR_GENS, folio); @@ -4347,14 +4380,17 @@ static bool sort_folio(struct lruvec *lruvec, struc= t folio *folio, struct scan_c } =20 /* protected */ - if (tier > tier_idx || refs =3D=3D BIT(LRU_REFS_WIDTH)) { - int hist =3D lru_hist_from_seq(lrugen->min_seq[type]); - + if (tier > tier_idx || refs + workingset =3D=3D BIT(LRU_REFS_WIDTH) + 1) { gen =3D folio_inc_gen(lruvec, folio, false); - list_move_tail(&folio->lru, &lrugen->folios[gen][type][zone]); + list_move(&folio->lru, &lrugen->folios[gen][type][zone]); =20 - WRITE_ONCE(lrugen->protected[hist][type][tier], - lrugen->protected[hist][type][tier] + delta); + /* don't count the workingset being lazily promoted */ + if (refs + workingset !=3D BIT(LRU_REFS_WIDTH) + 1) { + int hist =3D lru_hist_from_seq(lrugen->min_seq[type]); + + WRITE_ONCE(lrugen->protected[hist][type][tier], + lrugen->protected[hist][type][tier] + delta); + } return true; } =20 @@ -4374,8 +4410,7 @@ static bool sort_folio(struct lruvec *lruvec, struct = folio *folio, struct scan_c } =20 /* waiting for writeback */ - if (folio_test_locked(folio) || writeback || - (type =3D=3D LRU_GEN_FILE && dirty)) { + if (writeback || (type =3D=3D LRU_GEN_FILE && dirty)) { gen =3D folio_inc_gen(lruvec, folio, true); list_move(&folio->lru, &lrugen->folios[gen][type][zone]); return true; @@ -4404,13 +4439,12 @@ static bool isolate_folio(struct lruvec *lruvec, st= ruct folio *folio, struct sca return false; } =20 - /* see the comment on MAX_NR_TIERS */ + /* see the comment on LRU_REFS_FLAGS */ if (!folio_test_referenced(folio)) - folio_clear_lru_refs(folio); + set_mask_bits(&folio->flags, LRU_REFS_MASK, 0); =20 /* for shrink_folio_list() */ folio_clear_reclaim(folio); - folio_clear_referenced(folio); =20 success =3D lru_gen_del_folio(lruvec, folio, true); VM_WARN_ON_ONCE_FOLIO(!success, folio); @@ -4600,31 +4634,24 @@ static int evict_folios(struct lruvec *lruvec, stru= ct scan_control *sc, int swap type ? LRU_INACTIVE_FILE : LRU_INACTIVE_ANON); =20 list_for_each_entry_safe_reverse(folio, next, &list, lru) { + DEFINE_MIN_SEQ(lruvec); + if (!folio_evictable(folio)) { list_del(&folio->lru); folio_putback_lru(folio); continue; } =20 - if (folio_test_reclaim(folio) && - (folio_test_dirty(folio) || folio_test_writeback(folio))) { - /* restore LRU_REFS_FLAGS cleared by isolate_folio() */ - if (folio_test_workingset(folio)) - folio_set_referenced(folio); - continue; - } - - if (skip_retry || folio_test_active(folio) || folio_test_referenced(foli= o) || - folio_mapped(folio) || folio_test_locked(folio) || - folio_test_dirty(folio) || folio_test_writeback(folio)) { - /* don't add rejected folios to the oldest generation */ - set_mask_bits(&folio->flags, LRU_REFS_MASK | LRU_REFS_FLAGS, - BIT(PG_active)); - continue; - } - /* retry folios that may have missed folio_rotate_reclaimable() */ - list_move(&folio->lru, &clean); + if (!skip_retry && !folio_test_active(folio) && !folio_mapped(folio) && + !folio_test_dirty(folio) && !folio_test_writeback(folio)) { + list_move(&folio->lru, &clean); + continue; + } + + /* don't add rejected folios to the oldest generation */ + if (lru_gen_folio_seq(lruvec, folio, false) =3D=3D min_seq[type]) + set_mask_bits(&folio->flags, LRU_REFS_FLAGS, BIT(PG_active)); } =20 spin_lock_irq(&lruvec->lru_lock); diff --git a/mm/workingset.c b/mm/workingset.c index 2c310c29f51e..4841ae8af411 100644 --- a/mm/workingset.c +++ b/mm/workingset.c @@ -239,7 +239,8 @@ static void *lru_gen_eviction(struct folio *folio) int type =3D folio_is_file_lru(folio); int delta =3D folio_nr_pages(folio); int refs =3D folio_lru_refs(folio); - int tier =3D lru_tier_from_refs(refs); + bool workingset =3D folio_test_workingset(folio); + int tier =3D lru_tier_from_refs(refs, workingset); struct mem_cgroup *memcg =3D folio_memcg(folio); struct pglist_data *pgdat =3D folio_pgdat(folio); =20 @@ -253,7 +254,7 @@ static void *lru_gen_eviction(struct folio *folio) hist =3D lru_hist_from_seq(min_seq); atomic_long_add(delta, &lrugen->evicted[hist][type][tier]); =20 - return pack_shadow(mem_cgroup_id(memcg), pgdat, token, refs); + return pack_shadow(mem_cgroup_id(memcg), pgdat, token, workingset); } =20 /* @@ -304,24 +305,20 @@ static void lru_gen_refault(struct folio *folio, void= *shadow) lrugen =3D &lruvec->lrugen; =20 hist =3D lru_hist_from_seq(READ_ONCE(lrugen->min_seq[type])); - /* see the comment in folio_lru_refs() */ - refs =3D (token & (BIT(LRU_REFS_WIDTH) - 1)) + workingset; - tier =3D lru_tier_from_refs(refs); + refs =3D (token & (BIT(LRU_REFS_WIDTH) - 1)) + 1; + tier =3D lru_tier_from_refs(refs, workingset); =20 atomic_long_add(delta, &lrugen->refaulted[hist][type][tier]); - mod_lruvec_state(lruvec, WORKINGSET_ACTIVATE_BASE + type, delta); =20 - /* - * Count the following two cases as stalls: - * 1. For pages accessed through page tables, hotter pages pushed out - * hot pages which refaulted immediately. - * 2. For pages accessed multiple times through file descriptors, - * they would have been protected by sort_folio(). - */ - if (lru_gen_in_fault() || refs >=3D BIT(LRU_REFS_WIDTH) - 1) { - set_mask_bits(&folio->flags, 0, LRU_REFS_MASK | BIT(PG_workingset)); + /* see folio_add_lru() where folio_set_active() will be called */ + if (lru_gen_in_fault()) + mod_lruvec_state(lruvec, WORKINGSET_ACTIVATE_BASE + type, delta); + + if (workingset) { + folio_set_workingset(folio); mod_lruvec_state(lruvec, WORKINGSET_RESTORE_BASE + type, delta); - } + } else + set_mask_bits(&folio->flags, LRU_REFS_MASK, (refs - 1UL) << LRU_REFS_PGO= FF); unlock: rcu_read_unlock(); } --=20 2.47.1.613.gc27f4b7a9f-goog From nobody Wed Dec 17 19:51:16 2025 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3A0C7189915 for ; Tue, 31 Dec 2024 04:36:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735619765; cv=none; b=ajls8xbZBswdPJEXfe4p9LU1GZMDh1z9fo7khE2LshdExTwiCspXBbVJRPOl+JKW5ga71nPq8opnDj+m4hyXvvptAuAWnv+//n8lPZRAxzxvQPtYKrlqEZOVL8xEZnZY6PPK7439R9FSdFFQ6d1cFAwMsd8GKfyS6h+TvyV3b3c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735619765; c=relaxed/simple; bh=pozeMboL+q+r1YKjg311oeCCqbNAn6Alpdz2xWCa2hY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=eWnXn5VfXBiA/AKIsPhu2tQRiJzSRslHTGBctVIfCe9A7gUWE2ZFJrBhnR+agv51lbS0kwNNC/KaGw/C6aA0wLewANxjGNCUG9U9yKV2Q8UstWpJRmr1wq/wmCCRlEMNuOZRW0WyGQeR2EZD1dcnnSbJhjCoDYOuv4/dXAUx/PM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--yuzhao.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=034O1oXy; arc=none smtp.client-ip=209.85.214.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--yuzhao.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="034O1oXy" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-2162259a5dcso235599515ad.3 for ; Mon, 30 Dec 2024 20:36:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1735619764; x=1736224564; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=uSnhijC2SU9G2JpiGLLPy0X8NVAvldR4ppcAmuCmJd8=; b=034O1oXySHX/oM0yS6jiNXtNWXwGw/hmyUdzsYxeI3g5bzG7ejhGR6bxHgt+fPpsu5 Ep+hea1m8wuRaNjG+zlNo8UThg+vSFqqivFSSewvpldknFbFtvJhNeE2sTtgOgjQ6Kx6 d7vzXZYgSgE2rj7alrSKrq7WXdO8SBsHFFjv9hA1I04nRnJZ/JWHUu4oopU24iS1Xstb /qGErRlOuXMgGAO1g26CDV0p61asaumdTvlPD7ipIoEPbeKzbZHeX6jfJpPSywXmNRSv DpqE5UmJ3vxUiGhFHrdM1GhQlx3GWDmiwYKXAmk/y4iae5ORNCtSeIxerNJdTJ0VkuOC cl4w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735619764; x=1736224564; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=uSnhijC2SU9G2JpiGLLPy0X8NVAvldR4ppcAmuCmJd8=; b=vz8Xr7WkwS0eOtmT6oUL8K80ED3yaxLT7odwVRBsQG+kJPmk1CApKHRUEa07fqcxBH NjJsIO1Z/TVHbJQ0L45peEKe0OY3JEIfYektCchMGCpcPS16YC6qUMiWjZkiguamTqBL /Dv+AiVKHR0ucH5c3MWxJpNQwREbIvrJA5ohhy41BqE7l+WkXQ8w98HMiOdagH75hAWO SIPvvaqkRdyhZhqCTDKwniwJNWAcXIlWjbZlPh0kfuIBTkfIGkaK94vT6SHVGxE6ybhS O4lM7pT7hxw3yVwN6aKV1ioWY0m977rkzhmPdSNk6+l3O2o2x78jIzU2O9h/4YhSoRoA 85Vw== X-Forwarded-Encrypted: i=1; AJvYcCWD3lLX6WAvQiU2CjYWJC7F4e/pED8E7awX5HCuIi4s02Gzc0NnrrSfYWJ5we6UmU/GnxZloCrvuIasTKU=@vger.kernel.org X-Gm-Message-State: AOJu0YxEoK9y1rFRdKYL07KH39bqdwNDZGehhyq/Zlw4WUk/OcmPmRfG RaXr5LC9ZosP8OcFbvFibxZ+XhaRLGYRnw7IJoGGyq9PGyyqfsS3UvaN6UoPD+5TxziYXzUxMm3 oLA== X-Google-Smtp-Source: AGHT+IG+LWeqA7gxlbhJ4Aa6fTniLxLak1adrDfoZ2HZ8A15k6jPqKUZa911/S4AQd/cYmc6w8UN3NHudRE= X-Received: from pgbda9.prod.google.com ([2002:a05:6a02:2389:b0:7fd:577a:6d1e]) (user=yuzhao job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:ef4e:b0:216:7761:cc36 with SMTP id d9443c01a7336-219e6f14bbamr619775545ad.43.1735619763742; Mon, 30 Dec 2024 20:36:03 -0800 (PST) Date: Mon, 30 Dec 2024 21:35:38 -0700 In-Reply-To: <20241231043538.4075764-1-yuzhao@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241231043538.4075764-1-yuzhao@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20241231043538.4075764-8-yuzhao@google.com> Subject: [PATCH mm-unstable v4 7/7] mm/mglru: fix PTE-mapped large folios From: Yu Zhao To: Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Yu Zhao , Barry Song , Kalesh Singh Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Count the accessed bits from PTEs mapping the same large folio as one access rather than multiple accesses. The last patch changed how folios accessed through page tables are promoted: rather than getting promoted after the accessed bit is cleared for the first time, a folio only gets promoted thereafter. Counting the accessed bits from the same large folio as multiple accesses can cause that folio to be promoted prematurely, which in turn can cause overprotection of single-use large folios. This patch reduced the sys time of the kernel compilation by 95% CI [2, 5]% on Altra M128-30 with 3GB DRAM, 12GB zram, 16KB THPs and -j32. Reported-by: Barry Song Signed-off-by: Yu Zhao Tested-by: Kalesh Singh --- mm/vmscan.c | 110 ++++++++++++++++++++++++++++++++++------------------ 1 file changed, 72 insertions(+), 38 deletions(-) diff --git a/mm/vmscan.c b/mm/vmscan.c index 74bc85fc7cdf..a099876fa029 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -3431,29 +3431,55 @@ static bool suitable_to_scan(int total, int young) return young * n >=3D total; } =20 +static void walk_update_folio(struct lru_gen_mm_walk *walk, struct folio *= folio, + int new_gen, bool dirty) +{ + int old_gen; + + if (!folio) + return; + + if (dirty && !folio_test_dirty(folio) && + !(folio_test_anon(folio) && folio_test_swapbacked(folio) && + !folio_test_swapcache(folio))) + folio_mark_dirty(folio); + + if (walk) { + old_gen =3D folio_update_gen(folio, new_gen); + if (old_gen >=3D 0 && old_gen !=3D new_gen) + update_batch_size(walk, folio, old_gen, new_gen); + } else if (lru_gen_set_refs(folio)) { + old_gen =3D folio_lru_gen(folio); + if (old_gen >=3D 0 && old_gen !=3D new_gen) + folio_activate(folio); + } +} + static bool walk_pte_range(pmd_t *pmd, unsigned long start, unsigned long = end, struct mm_walk *args) { int i; + bool dirty; pte_t *pte; spinlock_t *ptl; unsigned long addr; int total =3D 0; int young =3D 0; + struct folio *last =3D NULL; struct lru_gen_mm_walk *walk =3D args->private; struct mem_cgroup *memcg =3D lruvec_memcg(walk->lruvec); struct pglist_data *pgdat =3D lruvec_pgdat(walk->lruvec); DEFINE_MAX_SEQ(walk->lruvec); - int old_gen, new_gen =3D lru_gen_from_seq(max_seq); + int gen =3D lru_gen_from_seq(max_seq); pmd_t pmdval; =20 - pte =3D pte_offset_map_rw_nolock(args->mm, pmd, start & PMD_MASK, &pmdval, - &ptl); + pte =3D pte_offset_map_rw_nolock(args->mm, pmd, start & PMD_MASK, &pmdval= , &ptl); if (!pte) return false; + if (!spin_trylock(ptl)) { pte_unmap(pte); - return false; + return true; } =20 if (unlikely(!pmd_same(pmdval, pmdp_get_lockless(pmd)))) { @@ -3482,19 +3508,23 @@ static bool walk_pte_range(pmd_t *pmd, unsigned lon= g start, unsigned long end, if (!ptep_clear_young_notify(args->vma, addr, pte + i)) continue; =20 + if (last !=3D folio) { + walk_update_folio(walk, last, gen, dirty); + + last =3D folio; + dirty =3D false; + } + + if (pte_dirty(ptent)) + dirty =3D true; + young++; walk->mm_stats[MM_LEAF_YOUNG]++; - - if (pte_dirty(ptent) && !folio_test_dirty(folio) && - !(folio_test_anon(folio) && folio_test_swapbacked(folio) && - !folio_test_swapcache(folio))) - folio_mark_dirty(folio); - - old_gen =3D folio_update_gen(folio, new_gen); - if (old_gen >=3D 0 && old_gen !=3D new_gen) - update_batch_size(walk, folio, old_gen, new_gen); } =20 + walk_update_folio(walk, last, gen, dirty); + last =3D NULL; + if (i < PTRS_PER_PTE && get_next_vma(PMD_MASK, PAGE_SIZE, args, &start, &= end)) goto restart; =20 @@ -3508,13 +3538,15 @@ static void walk_pmd_range_locked(pud_t *pud, unsig= ned long addr, struct vm_area struct mm_walk *args, unsigned long *bitmap, unsigned long *first) { int i; + bool dirty; pmd_t *pmd; spinlock_t *ptl; + struct folio *last =3D NULL; struct lru_gen_mm_walk *walk =3D args->private; struct mem_cgroup *memcg =3D lruvec_memcg(walk->lruvec); struct pglist_data *pgdat =3D lruvec_pgdat(walk->lruvec); DEFINE_MAX_SEQ(walk->lruvec); - int old_gen, new_gen =3D lru_gen_from_seq(max_seq); + int gen =3D lru_gen_from_seq(max_seq); =20 VM_WARN_ON_ONCE(pud_leaf(*pud)); =20 @@ -3567,20 +3599,23 @@ static void walk_pmd_range_locked(pud_t *pud, unsig= ned long addr, struct vm_area if (!pmdp_clear_young_notify(vma, addr, pmd + i)) goto next; =20 + if (last !=3D folio) { + walk_update_folio(walk, last, gen, dirty); + + last =3D folio; + dirty =3D false; + } + + if (pmd_dirty(pmd[i])) + dirty =3D true; + walk->mm_stats[MM_LEAF_YOUNG]++; - - if (pmd_dirty(pmd[i]) && !folio_test_dirty(folio) && - !(folio_test_anon(folio) && folio_test_swapbacked(folio) && - !folio_test_swapcache(folio))) - folio_mark_dirty(folio); - - old_gen =3D folio_update_gen(folio, new_gen); - if (old_gen >=3D 0 && old_gen !=3D new_gen) - update_batch_size(walk, folio, old_gen, new_gen); next: i =3D i > MIN_LRU_BATCH ? 0 : find_next_bit(bitmap, MIN_LRU_BATCH, i) + = 1; } while (i <=3D MIN_LRU_BATCH); =20 + walk_update_folio(walk, last, gen, dirty); + arch_leave_lazy_mmu_mode(); spin_unlock(ptl); done: @@ -4115,9 +4150,11 @@ static void lru_gen_age_node(struct pglist_data *pgd= at, struct scan_control *sc) bool lru_gen_look_around(struct page_vma_mapped_walk *pvmw) { int i; + bool dirty; unsigned long start; unsigned long end; struct lru_gen_mm_walk *walk; + struct folio *last =3D NULL; int young =3D 1; pte_t *pte =3D pvmw->pte; unsigned long addr =3D pvmw->address; @@ -4128,7 +4165,7 @@ bool lru_gen_look_around(struct page_vma_mapped_walk = *pvmw) struct lruvec *lruvec =3D mem_cgroup_lruvec(memcg, pgdat); struct lru_gen_mm_state *mm_state =3D get_mm_state(lruvec); DEFINE_MAX_SEQ(lruvec); - int old_gen, new_gen =3D lru_gen_from_seq(max_seq); + int gen =3D lru_gen_from_seq(max_seq); =20 lockdep_assert_held(pvmw->ptl); VM_WARN_ON_ONCE_FOLIO(folio_test_lru(folio), folio); @@ -4182,24 +4219,21 @@ bool lru_gen_look_around(struct page_vma_mapped_wal= k *pvmw) if (!ptep_clear_young_notify(vma, addr, pte + i)) continue; =20 - young++; + if (last !=3D folio) { + walk_update_folio(walk, last, gen, dirty); =20 - if (pte_dirty(ptent) && !folio_test_dirty(folio) && - !(folio_test_anon(folio) && folio_test_swapbacked(folio) && - !folio_test_swapcache(folio))) - folio_mark_dirty(folio); - - if (walk) { - old_gen =3D folio_update_gen(folio, new_gen); - if (old_gen >=3D 0 && old_gen !=3D new_gen) - update_batch_size(walk, folio, old_gen, new_gen); - } else if (lru_gen_set_refs(folio)) { - old_gen =3D folio_lru_gen(folio); - if (old_gen >=3D 0 && old_gen !=3D new_gen) - folio_activate(folio); + last =3D folio; + dirty =3D false; } + + if (pte_dirty(ptent)) + dirty =3D true; + + young++; } =20 + walk_update_folio(walk, last, gen, dirty); + arch_leave_lazy_mmu_mode(); =20 /* feedback from rmap walkers to page table walkers */ --=20 2.47.1.613.gc27f4b7a9f-goog