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 B0BE0C07545 for ; Tue, 24 Oct 2023 03:34:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3870B6B0175; Mon, 23 Oct 2023 23:34:37 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 336516B0176; Mon, 23 Oct 2023 23:34:37 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 226746B0177; Mon, 23 Oct 2023 23:34:37 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 0F95E6B0175 for ; Mon, 23 Oct 2023 23:34:37 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id C69F4A0C05 for ; Tue, 24 Oct 2023 03:34:36 +0000 (UTC) X-FDA: 81378937752.14.E7C47D1 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) by imf10.hostedemail.com (Postfix) with ESMTP id B1A92C0003 for ; Tue, 24 Oct 2023 03:34:34 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=m1IziZaL; spf=none (imf10.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1698118475; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding:in-reply-to: references:dkim-signature; bh=J+JFyKdgR57mxN3Wh+YWsNWGN6wMkImo50SNHo3VozQ=; b=tE/tHxIVNPmuW+NUyRn+n99Oqfd7QN1H1s2jTTutht2bk8KkLHWDTlWi379gEvs6XnyRDH 5wMvKj9EpJN6ODNp2MBDQ05FXbgyoG4ClKgOXQmncl9lNQOWht8lS+N7v6Bm5SLj45SzBN mo1+D+tjr0E56Z4odJPCmCwTkkJzt2A= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698118475; a=rsa-sha256; cv=none; b=vbwqMNKNmhJOVU06U9pPcQawT7f4gH3nXO3JnY1tkrvlfKdSnLKoPZLtue4aWdGBA08cTX 4NWHxEllsoPPCNKyy/noC/E7LrwjuKsaC6HeUwU0Q2YQJidbPFLqqshVGl5zuZRdP89qnL 7v0WrDeOhVzsKyYxCWqkBvOTnxPqDXs= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=m1IziZaL; spf=none (imf10.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org; dmarc=none DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Type:MIME-Version:Message-ID: Subject:To:From:Date:Sender:Reply-To:Cc:Content-Transfer-Encoding:Content-ID: Content-Description:In-Reply-To:References; bh=J+JFyKdgR57mxN3Wh+YWsNWGN6wMkImo50SNHo3VozQ=; b=m1IziZaLUmHRBEGpDbN5bWUuVo ckRlThGoaOFgNVbP27BTmhYqwhf2WYtvJWOjtrrwZg77J0lwChzmAHJiYH6Aew/J6nR2xslmmXHQE 1nBVYj7HwSLtSfGZ37SHd7qMZLdti0suOtEGEordFl3md1Tp0R/KM7pz0ldF1OJjwmC2wHEDMEeh0 CnTm28rVcF49UYtNtA7wypxCwYqvTUwjK/IiaGEiiUwjTwfIcaosb5zgfGmk6YnkRPQKEUlXn8BBS 2im6jn8CgP/fB9kgdbFYFkkclCUz4C/lPF8NSpBy5qnGz/6xAEA3Iq0UOf3RhVAn4V+5Dggk/t4j5 Vdmy8SsA==; Received: from willy by casper.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1qv8Bc-0007Gn-9i for linux-mm@kvack.org; Tue, 24 Oct 2023 03:34:32 +0000 Date: Tue, 24 Oct 2023 04:34:32 +0100 From: Matthew Wilcox To: linux-mm@kvack.org Subject: Virtual Scanning Considered Harmful Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Stat-Signature: zwmg9awepjrueyqrco7urk45r8qdz1r1 X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: B1A92C0003 X-Rspam-User: X-HE-Tag: 1698118474-946541 X-HE-Meta: U2FsdGVkX198/MoxTtTlSCbUrkcpyS+Ug2OuLFvzKSNz5OKe5wxgUQVmYt2ZYp2tAN61It+iQ5phPtBuXv6RuXoLOWwpBURJPui68FV3fvfEQQ7T580EYxqQF640f7saK6Kpd6t9E+gy8RbPmM2IC7pNjJ8RKUjMEjAYhcZ9CtjXR+HFPsdwWFuhusMlarwCMZ8Rcxo7Alsd4A7LK0laChGLFzWqR24sqZU9ADZGJvFt7YYeLsLIoSWiYD2OCkiXvPo4VoW1bBLxxdpUVO07Lgo09Z14XTcVxmWrCk90KUn3wymsHxuc23NZWQsrjRQOAVGDAU7rFTDfMtULlmiruqK2pIe6Vm/Mk6Kkq69ZoEKN5B1QUIAI0tnwq8FipXNB5D0rlOkUjkemq0wz096wP+gb0qzUj8JcXtryIOp+AVMPYqwz0fesV2ZGMIntgIFpaEl48FBXCBN79BcyniG65DIHut7bnZOxuVrJkfjVu5uz89wlPFsSrXZembnT+RrUnweZ0z3fPav4z5yJsgSf10OffcGWqnGdGK5NebIj+/Gf6XoLb8lzwHs9oZhXrmXzj+RLukHb99WjbcaS6mUrbaRsGug+qMR3px500U1aThuNNJ38fd6OOFzF1HBtXNOTeCldlpAdP+X+J7jwex6qVA9bkCwKOV0GaYCWvR9S1ABkmCDUqSLzcx4For4f5X9j1h3wilBQM3y9yuNJSmW2DaryBmupzHSknxfadR/3EeRtzxXJoAT16Zr8MOI5Xqmz++y2ouhEQCW6+Zjue6N+vgIvdSoybH/lBY7SVM8tJqhfTe4hdfbq68PzUlX2bZxKIDrrJVCwdWm17461AEfR7Nk220tYKctnmuQEzSq9P2kroul3K4/ZpYuG+YKmjLVutn6JCQ18kKk5YessgRoiWqW2AN54N8wYoNX1vhMyBgBrOZ21oMFnTvfZlvOynFM3TfrlmiG4WoOuL5WxrNG /mAG4Gzp CPF14+4DyQc3mMwH1LvLZzWgGmSFSoWN32Rd6/cj63uASz7XUHFoz0sJ4dEIbJ6HuLpl2NpmRa+m4p4nMQWPtLD4MLFQCdDjBR7kJWqDanTTbo79FpRcjwrAlpAIQ8KH4TH44OZvp17XXImKdoyAmm/UCtOENr1Z0rPpcKgmwjCKciK4F+ptR+XPh7R80lmxpiCrvcJ58dYbPJmdBT/Zgqxidzr0NB0zZ0+O7jMMyTuFhaDRT+VGRIpZH+u99PWmyZX8Db7tDyeOW/EuHq69Itstp+mvaCTiJa/woSgCNLembIqGPiEI0qVovIbJYXsfSAPlkfsItguDjgDs1JXubgftrMg== X-Bogosity: Ham, tests=bogofilter, spamicity=0.021407, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: In spite of https://meyerweb.com/eric/comment/chech.html I must pay homage to https://dl.acm.org/doi/10.1145/362929.362947 Modern CPUs are bad at walking linked lists. When you can issue 6 instructions per clock tick and it takes 100ns for an L3 cache miss, the ability for your CPU to predict your next memory reference and prefetch it as far ahead of time as possible is critical. I had originally planned to write a missive pleading for using a better data structure than a linked list for storing pages in an LRU. In my investigation, that turned out to be the wrong idea by a factor of about 3-4x. I now believe that _any_ virtual scanning is a mistake and we should be scanning memory physically. And I have the code to back up that belief. First, the code that simulates walking an LRU list vs an LRU array vs walking the physical array directly: https://www.infradead.org/~willy/linux/scan.c If you're going to run it for yourself, I recommend running it with -v to check how long it takes to run the physical scan; if it's too small, you may get some unstable results. I like to specify -c 10 (simulates 40GB of memory) to get more stable numbers. On my core i7-1165G7 @ 2.8GHz, I get $ ./scan -c 10 Physical scan is 37.962013 faster than ListLRU Physical scan is 3.847013 faster than ArrayLRU That bounces around a bit, but generally, it's 37-38x faster to do a physical scan than a ListLRU scan. I asked a few people to try it on their machines. 17.2x with -c 50 on E5-2650 V4 @ 2.20GHz 31.6x with -c 50 on Threadripper 3960X 17.7x on cortex-A72. basically raspberrypi4. on a coffee lake i9-9900k I see ~20 - 25 with a fair bit of variance with no -c specified, with -c 50 around ~28 with less variance The relative speed of memory & CPU, along with the effectiveness / aggressiveness of the CPU prefetchers will affect the exact numbers here, but you can see modern machines are all showing an advantage. I had intended to investigate using a radix tree or B-tree to store the LRU list, but a linear array of pointers to struct page should beat any data structure that is essentially a segmented array of pointers to struct page. And that is still about 3.8x slower than a physical scan, so I see no need to carry out the experiment of storing pages in an XArray, Maple Tree, or any other data structure. Admittedly, this is a very crude benchmark. In no way does it model the various tests done on the pages/folios that are scanned. It doesn't allocate large folios; they're all single pages. There are probably half a dozen other things wrong with it. Nevertheless, a 4x speedup on LRU scan would be amazing. A 40x speedup would be stunning. I don't have time to take on this project. I need to work on folios and other projects that I have started. I haven't done enough research to understand the various options we have for switching to a physical scan (and the downsides of those approaches!). I have no well-founded opinions on how we might go about this. Hopefully the potential win here is enough to motivate someone to want to work on this.