From nobody Mon Feb 9 15:07:24 2026 Received: from mail-pf1-f174.google.com (mail-pf1-f174.google.com [209.85.210.174]) (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 D0D8413CC44 for ; Tue, 26 Mar 2024 19:04:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711479863; cv=none; b=tZshbS+TxfQs3n4vYNwvi1oKZ6cBTLOZeIZ91KpzNpP/O/iuRdYq9oQrVqTzGm9vxV09r6m1eGoXdS/4QtNyypTqZCIeFRjFCQm+77nxsrnYOWZXuoxjRYsOWGzsGkF5+OCh+eR8nobdVW4rxofIuv3q/2tK/QvppSCMyoSYk44= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711479863; c=relaxed/simple; bh=tkhzRdtmDHxGPLSNGrCz95fBiO47MFeM9d6uWVZhm/k=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=V549Xv4xPhjvMebThlmr9QSpIFGsFyTvm6g7Ao7xFncr+t7SMHDHr1ko+jNCcu3W3hTc+ptCp7u8oOkyOa4JUJTYPwYIr+7qGoL6WsYMwTge+dIkQR6cphJEmBL6+ROf4F0r805ZI5/YGlX/aEAGj14BbKTAUS7jsHEDUzbN3UQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=LfFZL5W9; arc=none smtp.client-ip=209.85.210.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="LfFZL5W9" Received: by mail-pf1-f174.google.com with SMTP id d2e1a72fcca58-6e6b5432439so4576676b3a.1 for ; Tue, 26 Mar 2024 12:04:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1711479861; x=1712084661; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:from:to:cc:subject :date:message-id:reply-to; bh=MPCeiBQ6Vuv/VMKCl+70rHoy18EK+MdrkqBGmpRbWL4=; b=LfFZL5W9iI8VA0D87qczM7mAbvunmMc6ltI0tk36kwnYX0S3s8Ei0gSxMxpXIN6c2j DnPWc3A5Z7amYnWfonyKeenwgwghOrCLLft8UcXDIwFJMhnNuCWttpk4Jyy9J4lTBIYq cthoTByMsCiXIoLMqfKs0Gbv7UfUjiSRDwRWiFTPxogvxs5uHKV4Iji3oIy2pRENLrdH 8+iohbDEs8JUwc0DXFfxrk0pibpAgiEcsXf8Dr3eK6slwANvCW9O/k1tPv8Xl8OMG+lh VAPlr+316zCGHnx+07/aeiqRp7ruEZOlG23UBEYq3TuCJYOraGbaUREFFjiMSS1YluZF Wl5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711479861; x=1712084661; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=MPCeiBQ6Vuv/VMKCl+70rHoy18EK+MdrkqBGmpRbWL4=; b=MrsJtFM7Rv2/txNxOZ6U5tKNpSE6VNjFmgCes52mLPwsmTzD1AMe7gONncVnRW54j5 M1xcCYUCvRUIf6XApHhyK2UeMNjbVuNzkDzE7sClmFGmwpgVCk6chSxs+HLuDqgQvgPI QymkzleW2zZs3nJIivMTkbBttX8zgtC3hztOZ3r3bb1tB1gZL/iVqiRxLGA1AItXZtgG 9SGTenLHkmBrSYkK+jUw7rvjBkTOZLYieJH2UpLwmClNt5L3SHR1yFM534oNPAP93bFi 8vrUY2kAC/sJH17IS9/xHIYdjUZTi5bc2NewoWDdFsoB1aKYgB+GGA4RVtEeViSo69si qq8A== X-Forwarded-Encrypted: i=1; AJvYcCWQr3wlk0lOuYhddkWSYh5RCxHyY/b3tOVObw09TyyPqvY7TxF34h6qZ1kWU64Mx2Xc3p36FAsaXbGXd+tX3/xsN34zUoyA8z1o+B8N X-Gm-Message-State: AOJu0YwCDF4CwiS+9n//r0sUIzJN3f0KddIAEN8IFEm68cfUiB3CD7LA 1DtJC3zUIqlGbmXG/i+0bOG19/QCXNzNUpDS/ugdyVLrsFA5Ak8Z X-Google-Smtp-Source: AGHT+IEvuj2NywPp4edKIveEUgd69iy3TfxxMJh0u9KQ2EoAOriC83NY/O9bepwSOY7NB56Qt1OqEA== X-Received: by 2002:a05:6a00:814:b0:6e6:830:cd13 with SMTP id m20-20020a056a00081400b006e60830cd13mr2478320pfk.23.1711479860988; Tue, 26 Mar 2024 12:04:20 -0700 (PDT) Received: from KASONG-MB2.tencent.com ([115.171.40.106]) by smtp.gmail.com with ESMTPSA id j14-20020aa783ce000000b006ea790c2232sm6298350pfn.79.2024.03.26.12.04.16 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 26 Mar 2024 12:04:20 -0700 (PDT) From: Kairui Song To: linux-mm@kvack.org Cc: "Huang, Ying" , Chris Li , Minchan Kim , Barry Song , Ryan Roberts , Yu Zhao , SeongJae Park , David Hildenbrand , Yosry Ahmed , Johannes Weiner , Matthew Wilcox , Nhat Pham , Chengming Zhou , Andrew Morton , linux-kernel@vger.kernel.org, Kairui Song Subject: [RFC PATCH 01/10] mm/filemap: split filemap storing logic into a standalone helper Date: Wed, 27 Mar 2024 02:50:23 +0800 Message-ID: <20240326185032.72159-2-ryncsn@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240326185032.72159-1-ryncsn@gmail.com> References: <20240326185032.72159-1-ryncsn@gmail.com> Reply-To: Kairui Song Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Kairui Song Swapcache can reuse this part for multi index support, no change of performance from page cache side except noise: Test in 8G memory cgroup and 16G brd ramdisk. echo 3 > /proc/sys/vm/drop_caches fio -name=3Dcached --numjobs=3D16 --filename=3D/mnt/test.img \ --buffered=3D1 --ioengine=3Dmmap --rw=3Drandread --time_based \ --ramp_time=3D30s --runtime=3D5m --group_reporting Before: bw ( MiB/s): min=3D 493, max=3D 3947, per=3D100.00%, avg=3D2625.56, stdev= =3D25.74, samples=3D8651 iops : min=3D126454, max=3D1010681, avg=3D672142.61, stdev=3D6590.48= , samples=3D8651 After: bw ( MiB/s): min=3D 298, max=3D 3840, per=3D100.00%, avg=3D2614.34, stdev= =3D23.77, samples=3D8689 iops : min=3D76464, max=3D983045, avg=3D669270.35, stdev=3D6084.31, = samples=3D8689 Test result with THP (do a THP randread then switch to 4K page in hope it issues a lot of splitting): echo 3 > /proc/sys/vm/drop_caches fio -name=3Dcached --numjobs=3D16 --filename=3D/mnt/test.img \ --buffered=3D1 --ioengine=3Dmmap -thp=3D1 --readonly \ --rw=3Drandread --time_based --ramp_time=3D30s --runtime=3D10m \ --group_reporting fio -name=3Dcached --numjobs=3D16 --filename=3D/mnt/test.img \ --buffered=3D1 --ioengine=3Dmmap \ --rw=3Drandread --time_based --runtime=3D5s --group_reporting Before: bw ( KiB/s): min=3D 4611, max=3D15370, per=3D100.00%, avg=3D8928.74, stdev= =3D105.17, samples=3D19146 iops : min=3D 1151, max=3D 3842, avg=3D2231.27, stdev=3D26.29, sampl= es=3D19146 READ: bw=3D4635B/s (4635B/s), 4635B/s-4635B/s (4635B/s-4635B/s), io=3D64.0K= iB (65.5kB), run=3D14137-14137msec After: bw ( KiB/s): min=3D 4691, max=3D15666, per=3D100.00%, avg=3D8890.30, stdev= =3D104.53, samples=3D19056 iops : min=3D 1167, max=3D 3913, avg=3D2218.68, stdev=3D26.15, sampl= es=3D19056 READ: bw=3D4590B/s (4590B/s), 4590B/s-4590B/s (4590B/s-4590B/s), io=3D64.0K= iB (65.5kB), run=3D14275-14275msec Signed-off-by: Kairui Song --- mm/filemap.c | 124 +++++++++++++++++++++++++++------------------------ 1 file changed, 65 insertions(+), 59 deletions(-) diff --git a/mm/filemap.c b/mm/filemap.c index 90b86f22a9df..0ccdc9e92764 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -848,38 +848,23 @@ void replace_page_cache_folio(struct folio *old, stru= ct folio *new) } EXPORT_SYMBOL_GPL(replace_page_cache_folio); =20 -noinline int __filemap_add_folio(struct address_space *mapping, - struct folio *folio, pgoff_t index, gfp_t gfp, void **shadowp) +static int __filemap_lock_store(struct xa_state *xas, struct folio *folio, + pgoff_t index, gfp_t gfp, void **shadowp) { - XA_STATE(xas, &mapping->i_pages, index); - void *alloced_shadow =3D NULL; - int alloced_order =3D 0; - bool huge; - long nr; - - VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio); - VM_BUG_ON_FOLIO(folio_test_swapbacked(folio), folio); - mapping_set_update(&xas, mapping); - - VM_BUG_ON_FOLIO(index & (folio_nr_pages(folio) - 1), folio); - xas_set_order(&xas, index, folio_order(folio)); - huge =3D folio_test_hugetlb(folio); - nr =3D folio_nr_pages(folio); - + void *entry, *old, *alloced_shadow =3D NULL; + int order, split_order, alloced_order =3D 0; gfp &=3D GFP_RECLAIM_MASK; - folio_ref_add(folio, nr); - folio->mapping =3D mapping; - folio->index =3D xas.xa_index; =20 for (;;) { - int order =3D -1, split_order =3D 0; - void *entry, *old =3D NULL; + order =3D -1; + split_order =3D 0; + old =3D NULL; =20 - xas_lock_irq(&xas); - xas_for_each_conflict(&xas, entry) { + xas_lock_irq(xas); + xas_for_each_conflict(xas, entry) { old =3D entry; if (!xa_is_value(entry)) { - xas_set_err(&xas, -EEXIST); + xas_set_err(xas, -EEXIST); goto unlock; } /* @@ -887,72 +872,93 @@ noinline int __filemap_add_folio(struct address_space= *mapping, * it will be the first and only entry iterated. */ if (order =3D=3D -1) - order =3D xas_get_order(&xas); + order =3D xas_get_order(xas); } =20 /* entry may have changed before we re-acquire the lock */ if (alloced_order && (old !=3D alloced_shadow || order !=3D alloced_orde= r)) { - xas_destroy(&xas); + xas_destroy(xas); alloced_order =3D 0; } =20 if (old) { if (order > 0 && order > folio_order(folio)) { - /* How to handle large swap entries? */ - BUG_ON(shmem_mapping(mapping)); if (!alloced_order) { split_order =3D order; goto unlock; } - xas_split(&xas, old, order); - xas_reset(&xas); + xas_split(xas, old, order); + xas_reset(xas); } if (shadowp) *shadowp =3D old; } =20 - xas_store(&xas, folio); - if (xas_error(&xas)) - goto unlock; - - mapping->nrpages +=3D nr; - - /* hugetlb pages do not participate in page cache accounting */ - if (!huge) { - __lruvec_stat_mod_folio(folio, NR_FILE_PAGES, nr); - if (folio_test_pmd_mappable(folio)) - __lruvec_stat_mod_folio(folio, - NR_FILE_THPS, nr); - } - + xas_store(xas, folio); + if (!xas_error(xas)) + return 0; unlock: - xas_unlock_irq(&xas); + xas_unlock_irq(xas); =20 /* split needed, alloc here and retry. */ if (split_order) { - xas_split_alloc(&xas, old, split_order, gfp); - if (xas_error(&xas)) + xas_split_alloc(xas, old, split_order, gfp); + if (xas_error(xas)) goto error; alloced_shadow =3D old; alloced_order =3D split_order; - xas_reset(&xas); + xas_reset(xas); continue; } =20 - if (!xas_nomem(&xas, gfp)) + if (!xas_nomem(xas, gfp)) break; } =20 - if (xas_error(&xas)) - goto error; - - trace_mm_filemap_add_to_page_cache(folio); - return 0; error: - folio->mapping =3D NULL; - /* Leave page->index set: truncation relies upon it */ - folio_put_refs(folio, nr); - return xas_error(&xas); + return xas_error(xas); +} + +noinline int __filemap_add_folio(struct address_space *mapping, + struct folio *folio, pgoff_t index, gfp_t gfp, void **shadowp) +{ + XA_STATE(xas, &mapping->i_pages, index); + bool huge; + long nr; + int ret; + + VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio); + VM_BUG_ON_FOLIO(folio_test_swapbacked(folio), folio); + mapping_set_update(&xas, mapping); + + VM_BUG_ON_FOLIO(index & (folio_nr_pages(folio) - 1), folio); + xas_set_order(&xas, index, folio_order(folio)); + huge =3D folio_test_hugetlb(folio); + nr =3D folio_nr_pages(folio); + + folio_ref_add(folio, nr); + folio->mapping =3D mapping; + folio->index =3D xas.xa_index; + + ret =3D __filemap_lock_store(&xas, folio, index, gfp, shadowp); + if (!ret) { + mapping->nrpages +=3D nr; + /* hugetlb pages do not participate in page cache accounting */ + if (!huge) { + __lruvec_stat_mod_folio(folio, NR_FILE_PAGES, nr); + if (folio_test_pmd_mappable(folio)) + __lruvec_stat_mod_folio(folio, + NR_FILE_THPS, nr); + } + xas_unlock_irq(&xas); + trace_mm_filemap_add_to_page_cache(folio); + } else { + folio->mapping =3D NULL; + /* Leave page->index set: truncation relies upon it */ + folio_put_refs(folio, nr); + } + + return ret; } ALLOW_ERROR_INJECTION(__filemap_add_folio, ERRNO); =20 --=20 2.43.0