From patchwork Tue Jan 23 18:45:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kairui Song X-Patchwork-Id: 13527976 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4200EC47DDB for ; Tue, 23 Jan 2024 18:46:07 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BD1916B0082; Tue, 23 Jan 2024 13:46:06 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B0C456B0083; Tue, 23 Jan 2024 13:46:06 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 962E76B0089; Tue, 23 Jan 2024 13:46:06 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 81DD96B0082 for ; Tue, 23 Jan 2024 13:46:06 -0500 (EST) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 4BE06A0C2B for ; Tue, 23 Jan 2024 18:46:06 +0000 (UTC) X-FDA: 81711455532.17.D14F9A6 Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.179]) by imf02.hostedemail.com (Postfix) with ESMTP id 6D01780013 for ; Tue, 23 Jan 2024 18:46:04 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=GJDQCPXe; spf=pass (imf02.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.214.179 as permitted sender) smtp.mailfrom=ryncsn@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706035564; h=from:from:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=4tUupe0pRs64+ug0l5Lv4L/wYRNFF+WyGC/Eo0dQs5k=; b=MER3GryB4+t95oKGKRAHxsoVnjaytbWzIQhpgdUJDaAzzPA0mHYyvBZm2iB3dpAk4ir2s8 OJC4obkegOjhF869xOtIpwRgwSCK2SHVZezlC/NidEiUbqpHpqNNDzyrrOWbG7Ux2VGQeS XlIep7JGdQudBEX6nLR8WAtnZBJeaA8= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=GJDQCPXe; spf=pass (imf02.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.214.179 as permitted sender) smtp.mailfrom=ryncsn@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706035564; a=rsa-sha256; cv=none; b=G/bbL4wuUKoDguUQQ1/GSfESAeZiO8Zs2V3VXGIZ+M0oKwZa0i2HHhc9fQg9kTqTkF4s9j ozQMV7BjNqiIv/KlLiykXvBYGXtayI1p1Py60mv7qVwVD/ZkkVi/yvATfEknWTK7zI/nYs G5K0F/WQqtLw11g7PZFni5zYjevSIbA= Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-1d75ea3a9b6so18104775ad.2 for ; Tue, 23 Jan 2024 10:46:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1706035562; x=1706640362; darn=kvack.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=4tUupe0pRs64+ug0l5Lv4L/wYRNFF+WyGC/Eo0dQs5k=; b=GJDQCPXe3xgfC/1TJAb/OD8NRA8C7exmYyTy7rCzNF19xiSqOC5bVIQ8Mi48RJgFjw +k8z1cppN5rUkO1iSUUlFtGsTqCxlCFDbMhdtJNZm2LJ+obgT82gX9Z70FWv4ID0RQ5m TYH8zONdqQ8Xpr7/MMGLqTWoNbpeRa4Ul06yLK1wAtz99gdSaaOjp2S/FW3BMhGlwtzm I4LAp2sMHkO4cq9k2xqrtCX+hIvSN+KkZzcmpKEoTHIQ+Eq1DNTA7CIat8gaAip0ST/u YjsMZ2D/tYboDTXxPU4Kpr/x7Bd5IF8G4XB914KkCdoUwzIGWBC8rIdP6yyIj51US8GR Wn1g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706035562; x=1706640362; 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=4tUupe0pRs64+ug0l5Lv4L/wYRNFF+WyGC/Eo0dQs5k=; b=tSGMF2LqmbzqyZhsVG8ICl2UMFdGeCWurj0KaGFwEN8dKjC7f9NLqQ7w7AQgD2+qkZ tAE5u7FX6lCjhIAODaDLdoQHVz1KvMRu86xJnbsGdsCq89im7FPQIF/oX+LcOGAuJ/ZZ nvF6OWWNLmz6/dQlAYWUmqLl0Zw+o07hxqgXsT0RbIMskYjYPaJBxABhmFRX9kEnqrPd f+Y/7uuYPgKFW6ZEj25J/MHWRQ0EDZB7hOnnPRRPpla7dZPlGyJHfqXtSNfMngTYxAAy F7UyqtFWhcAcZRLelGUMCwzWBh53e8MRMrSFOC5VgU6z8KRiMPGBWdmS9WrvxjOe4s0s 3uXg== X-Gm-Message-State: AOJu0YzXQw/u0JBz1Dj/ccPHgOzxXAvIXYpHQkgHerZEaGOQCiFgwwIV /ut9vDzRv2Tn9QFdxBnOXQlS5Py5OYjvOv+VqqSykIju3wFDXAt78nkSMSsw/9YkAw== X-Google-Smtp-Source: AGHT+IHzOTaNc2ALEPfuFIm2wjtMBHoRPdcf8S7u1UHGQhnOuc8VCGjIMwWrNjGtijcUP07sJotD5A== X-Received: by 2002:a17:902:bc41:b0:1d7:2bd6:23e6 with SMTP id t1-20020a170902bc4100b001d72bd623e6mr6093896plz.128.1706035562123; Tue, 23 Jan 2024 10:46:02 -0800 (PST) Received: from KASONG-MB2.tencent.com ([1.203.117.98]) by smtp.gmail.com with ESMTPSA id d5-20020a170902b70500b001d74c285b55sm4035196pls.67.2024.01.23.10.45.59 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 23 Jan 2024 10:46:01 -0800 (PST) From: Kairui Song To: linux-mm@kvack.org Cc: Andrew Morton , Yu Zhao , Wei Xu , Chris Li , Matthew Wilcox , linux-kernel@vger.kernel.org, Kairui Song Subject: [PATCH v3 1/3] mm, lru_gen: try to prefetch next page when scanning LRU Date: Wed, 24 Jan 2024 02:45:50 +0800 Message-ID: <20240123184552.59758-2-ryncsn@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240123184552.59758-1-ryncsn@gmail.com> References: <20240123184552.59758-1-ryncsn@gmail.com> Reply-To: Kairui Song MIME-Version: 1.0 X-Rspamd-Queue-Id: 6D01780013 X-Rspam-User: X-Stat-Signature: koczjhi6kyqanwjjz19db1xas5pkjntn X-Rspamd-Server: rspam01 X-HE-Tag: 1706035564-2679 X-HE-Meta: U2FsdGVkX19KHref+0KYC+nUVE6DSd20wEAoEE+lQbf6ax4RAeqVsuvTZBJFGbzqbyDFQCtRxkw+63CDZeRMP+fbEKFOaeK9Cz7i3lc2+jOh22hqaoZIK0tgZsn3Qayl4NubqKRrPbRrqLe+wCv90w2r1IlkNxULNIOPEXXl0b90rqPh103NHDKrwGdTP3AKimdIRwqmx01xu05Mnvg1aJHyYf6YgU3EZ7B3AlOkgktq+1T+ViEWR6OYbdNjNHMveJ6tJqN0rP/SuDBtQHBGUpOOwrdTwESiiYwTI3sydnBEUcvOoNwdTQZuA0cCa/lTvW9//jLW/Xs7Qdnii5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rSp2VOvDKucLCWlYvuYsIZ99zdc5seNFS3V4iNzlZeJzPWEo6JkiwocRQSHBy0wpYP3BmeIO7THVqK8Z596xa26j9FI9aQjSSHhHVLi05jh9kwCqB/7owS1bjcIYju6wQ5reGuE6KPHEurv45XDyQ6Az/uKZ5nCRvSjPmInjzr8ZBSSu9wiRb5HRLGtxF+iu76WiXAivz9vCUJXb592z599J+GAxhV8fhdlWqBpU2D32UnuFd9A4oEJBssVw/6Xx9bIA+pzsym/67s8dHgTe8mYktXrgwp4pZm1ab0iaw= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000043, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Kairui Song Prefetch for inactive/active LRU have been long exiting, apply the same optimization for MGLRU. Test 1: Ramdisk fio ro test in a 4G memcg on a EPYC 7K62: fio -name=mglru --numjobs=16 --directory=/mnt --size=960m \ --buffered=1 --ioengine=io_uring --iodepth=128 \ --iodepth_batch_submit=32 --iodepth_batch_complete=32 \ --rw=randread --random_distribution=zipf:0.5 --norandommap \ --time_based --ramp_time=1m --runtime=6m --group_reporting Before this patch: bw ( MiB/s): min= 7758, max= 9239, per=100.00%, avg=8747.59, stdev=16.51, samples=11488 iops : min=1986251, max=2365323, avg=2239380.87, stdev=4225.93, samples=11488 After this patch (+7.2%): bw ( MiB/s): min= 8360, max= 9771, per=100.00%, avg=9381.31, stdev=15.67, samples=11488 iops : min=2140296, max=2501385, avg=2401613.91, stdev=4010.41, samples=11488 Test 2: Ramdisk fio hybrid test for 30m in a 4G memcg on a EPYC 7K62 (3 times): fio --buffered=1 --numjobs=8 --size=960m --directory=/mnt \ --time_based --ramp_time=1m --runtime=30m \ --ioengine=io_uring --iodepth=128 --iodepth_batch_submit=32 \ --iodepth_batch_complete=32 --norandommap \ --name=mglru-ro --rw=randread --random_distribution=zipf:0.7 \ --name=mglru-rw --rw=randrw --random_distribution=zipf:0.7 Before this patch: READ: 6622.0 MiB/s. Stdev: 22.090722 WRITE: 1256.3 MiB/s. Stdev: 5.249339 After this patch (+4.6%, +3.3%): READ: 6926.6 MiB/s, Stdev: 37.950260 WRITE: 1297.3 MiB/s, Stdev: 7.408704 Test 3: 30m of MySQL test in 6G memcg (12 times): echo 'set GLOBAL innodb_buffer_pool_size=16106127360;' | \ mysql -u USER -h localhost --password=PASS sysbench /usr/share/sysbench/oltp_read_only.lua \ --mysql-user=USER --mysql-password=PASS --mysql-db=DB \ --tables=48 --table-size=2000000 --threads=16 --time=1800 run Before this patch Avg: 134743.714545 qps. Stdev: 582.242189 After this patch (+0.2%): Avg: 135005.779091 qps. Stdev: 295.299027 Test 4: Build linux kernel in 2G memcg with make -j48 with SSD swap (for memory stress, 18 times): Before this patch: Avg: 1456.768899 s. Stdev: 20.106973 After this patch (+0.0%): Avg: 1455.659254 s. Stdev: 15.274481 Test 5: Memtier test in a 4G cgroup using brd as swap (18 times): memcached -u nobody -m 16384 -s /tmp/memcached.socket \ -a 0766 -t 16 -B binary & memtier_benchmark -S /tmp/memcached.socket \ -P memcache_binary -n allkeys \ --key-minimum=1 --key-maximum=16000000 -d 1024 \ --ratio=1:0 --key-pattern=P:P -c 1 -t 16 --pipeline 8 -x 3 Before this patch: Avg: 50317.984000 Ops/sec. Stdev: 2568.965458 After this patch (-5.7%): Avg: 47691.343500 Ops/sec. Stdev: 3925.772473 It seems prefetch is helpful in most cases, but the memtier test is either hitting a case where prefetch causes higher cache miss or it's just too noisy (high stdev). Signed-off-by: Kairui Song --- mm/vmscan.c | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/mm/vmscan.c b/mm/vmscan.c index 4f9c854ce6cc..03631cedb3ab 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -3681,15 +3681,26 @@ static bool inc_min_seq(struct lruvec *lruvec, int type, bool can_swap) /* prevent cold/hot inversion if force_scan is true */ for (zone = 0; zone < MAX_NR_ZONES; zone++) { struct list_head *head = &lrugen->folios[old_gen][type][zone]; + struct folio *prev = NULL; - while (!list_empty(head)) { - struct folio *folio = lru_to_folio(head); + if (!list_empty(head)) + prev = lru_to_folio(head); + + while (prev) { + struct folio *folio = prev; VM_WARN_ON_ONCE_FOLIO(folio_test_unevictable(folio), folio); VM_WARN_ON_ONCE_FOLIO(folio_test_active(folio), folio); VM_WARN_ON_ONCE_FOLIO(folio_is_file_lru(folio) != type, folio); VM_WARN_ON_ONCE_FOLIO(folio_zonenum(folio) != zone, folio); + if (unlikely(list_is_first(&folio->lru, head))) { + prev = NULL; + } else { + prev = lru_to_folio(&folio->lru); + prefetchw(&prev->flags); + } + new_gen = folio_inc_gen(lruvec, folio, false); list_move_tail(&folio->lru, &lrugen->folios[new_gen][type][zone]); @@ -4341,11 +4352,15 @@ static int scan_folios(struct lruvec *lruvec, struct scan_control *sc, for (i = MAX_NR_ZONES; i > 0; i--) { LIST_HEAD(moved); int skipped_zone = 0; + struct folio *prev = NULL; int zone = (sc->reclaim_idx + i) % MAX_NR_ZONES; struct list_head *head = &lrugen->folios[gen][type][zone]; - while (!list_empty(head)) { - struct folio *folio = lru_to_folio(head); + if (!list_empty(head)) + prev = lru_to_folio(head); + + while (prev) { + struct folio *folio = prev; int delta = folio_nr_pages(folio); VM_WARN_ON_ONCE_FOLIO(folio_test_unevictable(folio), folio); @@ -4355,6 +4370,13 @@ static int scan_folios(struct lruvec *lruvec, struct scan_control *sc, scanned += delta; + if (unlikely(list_is_first(&folio->lru, head))) { + prev = NULL; + } else { + prev = lru_to_folio(&folio->lru); + prefetchw(&prev->flags); + } + if (sort_folio(lruvec, folio, sc, tier)) sorted += delta; else if (isolate_folio(lruvec, folio, sc)) {