From nobody Wed Dec 17 21:39:24 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