From mboxrd@z Thu Jan 1 00:00:00 1970 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 04FEDC64ED8 for ; Thu, 23 Feb 2023 20:49:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 746F56B0071; Thu, 23 Feb 2023 15:49:40 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6F7B66B0072; Thu, 23 Feb 2023 15:49:40 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5BFEF6B0073; Thu, 23 Feb 2023 15:49:40 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 4B1426B0071 for ; Thu, 23 Feb 2023 15:49:40 -0500 (EST) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 2114540D48 for ; Thu, 23 Feb 2023 20:49:40 +0000 (UTC) X-FDA: 80499747720.30.696E21B Received: from mail-vs1-f53.google.com (mail-vs1-f53.google.com [209.85.217.53]) by imf08.hostedemail.com (Postfix) with ESMTP id 56252160012 for ; Thu, 23 Feb 2023 20:49:38 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=EpX9CHX7; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf08.hostedemail.com: domain of yuzhao@google.com designates 209.85.217.53 as permitted sender) smtp.mailfrom=yuzhao@google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1677185378; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=3SF3XhVP3fAY++OBxv67A9DpjZx+ZDC9K9+v7wrHIg4=; b=viEnPqPVHSNSRehK3tYARS62fVof4qNwZRI1HxUmWUwuZLuY2NigJKNzUwityq8D1+J2j8 6AccA/JQ7QlZiBQaEXd7HHfUY2aCFqG+xOkQqVEkUrb5D3uA4VUUJ2VvNMv/9UwdzhsMrM fXBP1NL+A9tRF7pyUDtuiE6ER1TrFs8= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=EpX9CHX7; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf08.hostedemail.com: domain of yuzhao@google.com designates 209.85.217.53 as permitted sender) smtp.mailfrom=yuzhao@google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1677185378; a=rsa-sha256; cv=none; b=Gb67MOm6FSC7hrDOet0KH6H43/2kU0vWpNORx85j+wLbD/GFkvgKvCaYq2nm1hb9IOLwr6 eX433CTFoAn7iqN8dTSMUOGhVKC7Mx6di4LWVon3BkttCmdq0ZB14RK/lXuYH2eVOI7CKA cIP+rAUn0vTiggnVdvkD5ZQgja1KIjY= Received: by mail-vs1-f53.google.com with SMTP id s1so1395306vsk.5 for ; Thu, 23 Feb 2023 12:49:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=3SF3XhVP3fAY++OBxv67A9DpjZx+ZDC9K9+v7wrHIg4=; b=EpX9CHX76iRG0Fs24LnxWWmUrr6GORT9E0/M4Xni5oa8PVB+NyUf9xnluacNRHiJLi oTPsaXyvlxhYWm1ndp1ryFZ2TYjXOvmoy1HRUGzS0OcGc0zMZqTcHgAXNM4rvtYhkBVg qf1J0mPiiAUp/S0+3TAXNUeuKCd0ORgY+E6d2A7I7obvZdxZj53WV4QC5FeppMbcQG4J w3LEbf37wFLbDqttTYWl7EPewRkSSHAJUCRfzZv8DTS9QEc8hXTrQprXo+iEqU+77E4a oj0f9UT2zBFwspHpPsFBXYoak9R9do+2VFEDllmPwgJrEESjvU63PTjiVmuQ4E+SRpNl YYCA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=3SF3XhVP3fAY++OBxv67A9DpjZx+ZDC9K9+v7wrHIg4=; b=mILGTKTHjQQHKo3kL5R3Un+I7OidlJZu/tfKpCwXW65BDiy3dz9S69PwrAnp5WJJ8h H6ysQpHd6dhuMzcIKC4LyfnvXzzPpWUuF1d+6djpdN9T0KMwra+e5A9XiqOA9CNn1n0p O+Zk7ItWPk1Zf4o8Lw0avRt8xS7t3L10NCHe+qRgXaOLkN3DU3xZmdupzJcyBcjWv8s7 92nDzQXWFuGVl30XTF2WvX3/9Q57zMPxeJgfJ3o7GahwkCtuA1wbYlRJvGuffAxbHV6q Vh4nIhLvIobrIRRsziDC3tPYb9jJkf2GOBdVU2NNIiCtiPimxqxq8+/+i6u9erq56svx Xy4g== X-Gm-Message-State: AO0yUKXYu1sH81gYyVQm7JCk5iHjQYrju8rkrsYPuPhH3bEKp5908qR4 gVuDxVM3d4nfUOWremBLuocKuBD/jzbxbR232bfRww== X-Google-Smtp-Source: AK7set+PFnbYU2JD49LX096IfpIl1qWCv3pt6jtJWvZdeEDsrHSa6XgcIEspmONr+RBUjp9C7QlPEAawwJ6rNQ/sErI= X-Received: by 2002:a67:fb96:0:b0:411:bf89:685c with SMTP id n22-20020a67fb96000000b00411bf89685cmr800789vsr.6.1677185377290; Thu, 23 Feb 2023 12:49:37 -0800 (PST) MIME-Version: 1.0 References: <20230217041230.2417228-1-yuzhao@google.com> <20230217041230.2417228-6-yuzhao@google.com> In-Reply-To: From: Yu Zhao Date: Thu, 23 Feb 2023 13:48:59 -0700 Message-ID: Subject: Re: [PATCH mm-unstable v1 5/5] mm: multi-gen LRU: use mmu_notifier_test_clear_young() To: Sean Christopherson Cc: Johannes Weiner , Andrew Morton , Paolo Bonzini , Jonathan Corbet , Michael Larabel , kvmarm@lists.linux.dev, kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linuxppc-dev@lists.ozlabs.org, x86@kernel.org, linux-mm@google.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 56252160012 X-Stat-Signature: dj7yg9uns48o3fyaguyx5c6p6cjpk8mr X-HE-Tag: 1677185378-277098 X-HE-Meta: U2FsdGVkX1+FKuMsP9bm1Cxz/58TIXGQrg6xmY3Q9pt8wDoWcB8q1CustkaSygnMEWjbjbmtfDUscdLICuDXJRQCSbdlY8CRzMwoyZXDjQeQ9e56WzbmIzpAa/TYLZGN1zVGgGsKxUWegx20q7yu+rMcKf1Gf/kZQVQv/50CNcpdlJswWJNfIdHppX6/iQtlk4/laOhBO9OvfdVLwaWmXH5Ga++rbJ8D9bTVTbA7mPmODbpUDw01pd5Rb9iwWcHBu3Gf8vclIQ9cRgwTV0I9U7PQjG3708uqnWrw1nHMR7GHdY4FlJkr/pIQFQ8zfrZe+fnuTohgub/lpqEKxXR4hipI2iN7JTSj2j6RL5NGOJylvRiyR1Ll+E8S7AZzYTHTApxkS+5bfpVWn9XbpuhcHPK+9PbV2bZBIvswM+AezFD0flbZb/EY4gKrVz8yNEIAAO2HoHT/b4hAdsg9L3En5tgS3RWRIlZPd73gR3JtfZI9XHD5gy7Oe7kETv7Qhyk1X+CoPGzofshPp8TFSzZf2JuVxFgP/ynahPpr6HAI5OGfyw7O7WR1cUFUnYJ/2vihE8wjtt2uYt1Qow4v/jguyZZuvafMRAVrDDivljjfWXVNnmt5wxNtxJs7GuKaH4F/q8T3pPitcA74ryKJqNtDTnZygjvz17JBkt7edfxhcWr0kuKxC4ub7MY9PhAiTbiEBDncAuOW1mpt2Pykc6JMqShIG3pk4DjJhsQXBYvCeGpHqUGXcTyAUb+5/IGrkHiATJTTr9PEFvpiub191s5ffV9yGSDcECnFSSFHC0FJhBJOzCR4yuijst4l5heEpA3Uy7qa64v0lQjgR6NGBpU3uYWXcGf2YUH1kE3jcWAswiWPSMQlvNJbZl+q61KGanK0+dSm8mH3AFuckE60ElvySzmRxn/SfoDuhKXmiPruIZyT7S1BCGeP+coCopQRYqkicJTfvpyumb3Jvc2w9AY KvlgfNJL 2CxewG8gB/pxHA081nNE/oPmnLWgzShBQoS1qqH6nLAWkRYzabUGT3du5n1PA2sf6qsbqHjUSYLW8ulDVSodNkF4EZpFGKULWRfhEnfc8ZHkz5CsCZw5S1kgeGs4rxGypKALku8H7xCbeHUbWSNFGtslYJVpOMEMCqwB15D0jdxZzAL/8kezFwtb0tzCHTGUYYLORL8Am9Hrh4zowHIRWiuzFj7Hx5qpl/CEX6cGJcN9qmygLa0kQnweSObXH9sbjFkjIMbTYcuyTOTA/IFPsCMJ2rL+oO8Phpu9tFQqguF6gvrDqytmWQ2hTvoTTDoX4Va5D X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Thu, Feb 23, 2023 at 1:29=E2=80=AFPM Sean Christopherson wrote: > > On Thu, Feb 23, 2023, Yu Zhao wrote: > > On Thu, Feb 23, 2023 at 12:58=E2=80=AFPM Sean Christopherson wrote: > > > > > > On Thu, Feb 23, 2023, Yu Zhao wrote: > > > > On Thu, Feb 23, 2023 at 12:11=E2=80=AFPM Sean Christopherson wrote: > > > > > > > > > > On Thu, Feb 23, 2023, Yu Zhao wrote: > > > > > > > As alluded to in patch 1, unless batching the walks even if K= VM does _not_ support > > > > > > > a lockless walk is somehow _worse_ than using the existing mm= u_notifier_clear_flush_young(), > > > > > > > I think batching the calls should be conditional only on LRU_= GEN_SPTE_WALK. Or > > > > > > > if we want to avoid batching when there are no mmu_notifier l= isteners, probe > > > > > > > mmu_notifiers. But don't call into KVM directly. > > > > > > > > > > > > I'm not sure I fully understand. Let's present the problem on t= he MM > > > > > > side: assuming KVM supports lockless walks, batching can still = be > > > > > > worse (very unlikely), because GFNs can exhibit no memory local= ity at > > > > > > all. So this option allows userspace to disable batching. > > > > > > > > > > I'm asking the opposite. Is there a scenario where batching+lock= is worse than > > > > > !batching+lock? If not, then don't make batching depend on lockl= ess walks. > > > > > > > > Yes, absolutely. batching+lock means we take/release mmu_lock for > > > > every single PTE in the entire VA space -- each small batch contain= s > > > > 64 PTEs but the entire batch is the whole KVM. > > > > > > Who is "we"? > > > > Oops -- shouldn't have used "we". > > > > > I don't see anything in the kernel that triggers walking the whole > > > VMA, e.g. lru_gen_look_around() limits the walk to a single PMD. I f= eel like I'm > > > missing something... > > > > walk_mm() -> walk_pud_range() -> walk_pmd_range() -> walk_pte_range() > > -> test_spte_young() -> mmu_notifier_test_clear_young(). > > > > MGLRU takes two passes: during the first pass, it sweeps entire VA > > space on each MM (per MM/KVM); during the second pass, it uses the rmap= on each > > folio (per folio). > > Ah. IIUC, userspace can use LRU_GEN_SPTE_WALK to control whether or not = to walk > secondary MMUs, and the kernel further restricts LRU_GEN_SPTE_WALK to sec= ondary > MMUs that implement a lockless walk. And if the answer is "no", secondar= y MMUs > are simply not consulted. Sorry for the bad naming -- probably LRU_GEN_SPTE_BATCH_WALK would be less confusing. MGLRU always consults the secondary MMU for each page it's going to reclaim (during the second pass), i.e., it checks the A-bit in the SPTE mapping a page (by the rmap) it plans to reclaim so that it won't take a hot page away from KVM. If the lockless walk is supported, MGLRU doesn't need to work at page granularity: (physical) pages on the LRU list may have nothing in common (e.g., from different processes), checking their PTEs/SPTEs one by one is expensive. Instead, it sweeps the entire KVM spaces in the first pass and checks the *adjacent SPTEs* of a page it plans to reclaim in the second pass. Both rely on the *assumption* there would be some spatial locality to exploit. This assumption can be wrong, and LRU_GEN_SPTE_WALK disables it. > If that's correct, then the proper way to handle this is by extending mmu= _notifier_ops > to query (a) if there's at least one register listeners that implements > test_clear_young() and (b) if all registered listeners that implement tes= t_clear_young() > support lockless walks. That avoids direct dependencies on KVM, and avoi= ds making > assumptions that may not always hold true, e.g. that KVM is the only mmu_= notifier > user that supports the young APIs. > > P.S. all of this info absolutely belongs in documentation and/or changelo= gs. Will do.