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 DFB00C3F6B0 for ; Wed, 10 Aug 2022 17:10:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5C1306B0072; Wed, 10 Aug 2022 13:10:00 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5718F8E0002; Wed, 10 Aug 2022 13:10:00 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 411E78E0001; Wed, 10 Aug 2022 13:10:00 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 315FC6B0072 for ; Wed, 10 Aug 2022 13:10:00 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id F1C77A11E9 for ; Wed, 10 Aug 2022 17:09:59 +0000 (UTC) X-FDA: 79784320518.03.3122E82 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf11.hostedemail.com (Postfix) with ESMTP id 7A9BA4019E for ; Wed, 10 Aug 2022 17:09:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1660151398; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=/nqhyggpe6XCtIwWAg9ZL+8LA2Yq47qLEz8joLIyWGk=; b=AyNwEjpKyVHt7V+eoG/7x5gpNtK77Bo6N5mn4ppkj8o1Z0eiXAwhOSftccQc7oJ9L9UJn8 TtCFkUBjdVgwjrbvEb+OADmxU2hRd+tncHG03u3rKW1Orfagq11ebnve1oz8023RCGlDws QLCB4ZD8VlRuH3ye1JneTSFqbNBKnJA= Received: from mail-io1-f69.google.com (mail-io1-f69.google.com [209.85.166.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-639-EAEwYLAeOIuRphjqEvol2Q-1; Wed, 10 Aug 2022 13:09:57 -0400 X-MC-Unique: EAEwYLAeOIuRphjqEvol2Q-1 Received: by mail-io1-f69.google.com with SMTP id l18-20020a6bd112000000b0067cb64ad9b2so8464661iob.20 for ; Wed, 10 Aug 2022 10:09:57 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc; bh=/nqhyggpe6XCtIwWAg9ZL+8LA2Yq47qLEz8joLIyWGk=; b=f55/3l82nQjy5VemvjQ11JmMrT0fRgTo/A6CjEBHiezXYXj58As/IkB5hZTvRgE4ue qXKBvG9PuCpysQdV2h3WoRidaAyeQXo2fKTzXYiOdh1x2WWrG/1fOmsQYs1shYZ2Nnqx IRmXIcU5dgtybEs48pvA9ybWfBVCpdo+iJ3pCvpzFEsTpbbuhpFDzbX707pYzoi3aIdU ta33WMRbhybsi5/Q+2gYdH/oLIGoRV6JgdH9U6+b/ocwLleFhRXCNaFfjyOplb0z1CPQ whmL8xczGuZry2CtzMCun0NsdO0RLS24aw7qh+0PIDYiQTPz0cXIorqzXSmZkEZZI660 laPA== X-Gm-Message-State: ACgBeo0HPGTFkLd1K8JTk7//tDRenVMBKj4D9dGJpbzU9IrEvPRIH4fJ 46XVS4RG1eUYKiZzDWU/hvd9f1DmrkXGMH9zBRK504gYa7OSIPmdR3dious20bqGfZP0DZ7ahhR d0GTBcy+WvkA= X-Received: by 2002:a5d:87d7:0:b0:680:651d:fd5a with SMTP id q23-20020a5d87d7000000b00680651dfd5amr12129535ios.145.1660151396672; Wed, 10 Aug 2022 10:09:56 -0700 (PDT) X-Google-Smtp-Source: AA6agR79pXZsjRY8UcBeEs22hOBGTnRl2wWH/Q7ZJLepDiKnp6XFZcVzTizVMrYu/sfYtIxflRpxlA== X-Received: by 2002:a5d:87d7:0:b0:680:651d:fd5a with SMTP id q23-20020a5d87d7000000b00680651dfd5amr12129523ios.145.1660151396413; Wed, 10 Aug 2022 10:09:56 -0700 (PDT) Received: from xz-m1.local (bras-base-aurron9127w-grc-35-70-27-3-10.dsl.bell.ca. [70.27.3.10]) by smtp.gmail.com with ESMTPSA id z26-20020a056638215a00b00342847fc497sm7586643jaj.8.2022.08.10.10.09.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Aug 2022 10:09:56 -0700 (PDT) Date: Wed, 10 Aug 2022 13:09:54 -0400 From: Peter Xu To: "Huang, Ying" Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Minchan Kim , David Hildenbrand , Nadav Amit , Andrew Morton , Hugh Dickins , Vlastimil Babka , Andrea Arcangeli , Andi Kleen , "Kirill A . Shutemov" Subject: Re: [PATCH v3 7/7] mm/swap: Cache swap migration A/D bits support Message-ID: References: <20220809220100.20033-1-peterx@redhat.com> <20220809220100.20033-8-peterx@redhat.com> <87tu6keh8r.fsf@yhuang6-desk2.ccr.corp.intel.com> MIME-Version: 1.0 In-Reply-To: <87tu6keh8r.fsf@yhuang6-desk2.ccr.corp.intel.com> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8 Content-Disposition: inline ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1660151399; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=/nqhyggpe6XCtIwWAg9ZL+8LA2Yq47qLEz8joLIyWGk=; b=e4y45ivisuh7003Wm6K8+xsVsBAqRlYnf3g6oR38eoTKj8x6+pGyoUYOIMa3tBkaVY73kl b7z7+VSSd2MS+q/UOsuOX0I2fcMsjxWvLzZVb+brQRat4NSdW5qhPDp7FzrzX6oeloOx8H IG4cUPAiAdtVYEIwxtu24h9J5e0SAo4= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=AyNwEjpK; spf=pass (imf11.hostedemail.com: domain of peterx@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1660151399; a=rsa-sha256; cv=none; b=t7H+HYnATfNXuxIwtgFoLVWM09zOCh6J/49DOnHxiyzBRGXF2A4XxQ1AVhuhN99zX/mgPo cREsjv1JjMh+HlQPVfGHI5M/aMAohDhaOwnrPzgnIj4CF5Sprr0v0vPxMxGGQrzqCb7m6o yAgPsYPbkP2eZz5ad3X0A2UApsAjLdI= X-Stat-Signature: fh7prexwda5qtzf7fzpzj5m18ctw1bcn X-Rspamd-Queue-Id: 7A9BA4019E Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=AyNwEjpK; spf=pass (imf11.hostedemail.com: domain of peterx@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-Rspam-User: X-Rspamd-Server: rspam09 X-HE-Tag: 1660151399-353434 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 Wed, Aug 10, 2022 at 02:37:40PM +0800, Huang, Ying wrote: > Peter Xu writes: > > > Introduce a variable swap_migration_ad_supported to cache whether the arch > > supports swap migration A/D bits. > > > > Here one thing to mention is that SWP_MIG_TOTAL_BITS will internally > > reference the other macro MAX_PHYSMEM_BITS, which is a function call on > > x86 (constant on all the rest of archs). > > > > It's safe to reference it in swapfile_init() because when reaching here > > we're already during initcalls level 4 so we must have initialized 5-level > > pgtable for x86_64 (right after early_identify_cpu() finishes). > > > > - start_kernel > > - setup_arch > > - early_cpu_init > > - get_cpu_cap --> fetch from CPUID (including X86_FEATURE_LA57) > > - early_identify_cpu --> clear X86_FEATURE_LA57 (if early lvl5 not enabled (USE_EARLY_PGTABLE_L5)) > > - arch_call_rest_init > > - rest_init > > - kernel_init > > - kernel_init_freeable > > - do_basic_setup > > - do_initcalls --> calls swapfile_init() (initcall level 4) > > > > This should slightly speed up the migration swap entry handlings. > > > > Signed-off-by: Peter Xu > > --- > > include/linux/swapfile.h | 1 + > > include/linux/swapops.h | 7 +------ > > mm/swapfile.c | 8 ++++++++ > > 3 files changed, 10 insertions(+), 6 deletions(-) > > > > diff --git a/include/linux/swapfile.h b/include/linux/swapfile.h > > index 54078542134c..87ec5e2cdb02 100644 > > --- a/include/linux/swapfile.h > > +++ b/include/linux/swapfile.h > > @@ -9,5 +9,6 @@ > > extern struct swap_info_struct *swap_info[]; > > extern unsigned long generic_max_swapfile_size(void); > > extern unsigned long max_swapfile_size(void); > > +extern bool swap_migration_ad_supported; > > > > #endif /* _LINUX_SWAPFILE_H */ > > diff --git a/include/linux/swapops.h b/include/linux/swapops.h > > index 0e9579b90659..e6afc77c51ad 100644 > > --- a/include/linux/swapops.h > > +++ b/include/linux/swapops.h > > @@ -301,13 +301,8 @@ static inline swp_entry_t make_writable_migration_entry(pgoff_t offset) > > */ > > static inline bool migration_entry_supports_ad(void) > > { > > - /* > > - * max_swapfile_size() returns the max supported swp-offset plus 1. > > - * We can support the migration A/D bits iff the pfn swap entry has > > - * the offset large enough to cover all of them (PFN, A & D bits). > > - */ > > #ifdef CONFIG_SWAP > > - return max_swapfile_size() >= (1UL << SWP_MIG_TOTAL_BITS); > > + return swap_migration_ad_supported; > > #else /* CONFIG_SWAP */ > > return false; > > #endif /* CONFIG_SWAP */ > > diff --git a/mm/swapfile.c b/mm/swapfile.c > > index 794fa37bd0c3..c49cf25f0d08 100644 > > --- a/mm/swapfile.c > > +++ b/mm/swapfile.c > > @@ -64,6 +64,9 @@ EXPORT_SYMBOL_GPL(nr_swap_pages); > > long total_swap_pages; > > static int least_priority = -1; > > static unsigned long swapfile_maximum_size; > > +#ifdef CONFIG_MIGRATION > > +bool swap_migration_ad_supported; > > +#endif /* CONFIG_MIGRATION */ > > > > static const char Bad_file[] = "Bad swap file entry "; > > static const char Unused_file[] = "Unused swap file entry "; > > @@ -3685,6 +3688,11 @@ static int __init swapfile_init(void) > > > > swapfile_maximum_size = arch_max_swapfile_size(); > > > > +#ifdef CONFIG_MIGRATION > > + if (swapfile_maximum_size >= (1UL << SWP_MIG_TOTAL_BITS)) > > + swap_migration_ad_supported = true; > > +#endif /* CONFIG_MIGRATION */ > > + > > return 0; > > } > > subsys_initcall(swapfile_init); > > I don't think it's necessary to add a variable for such a simple > function and it's not a super hot path. But I don't have strong > opinions here. Logically referencing SWP_MIG_TOTAL_BITS needs to go check MAX_PHYSMEM_BITS, which should further go with: # define MAX_PHYSMEM_BITS (pgtable_l5_enabled() ? 52 : 46) Then since swapfile.c doesn't have USE_EARLY_PGTABLE_L5 defined.. #define pgtable_l5_enabled() cpu_feature_enabled(X86_FEATURE_LA57) Then, #define cpu_feature_enabled(bit) \ (__builtin_constant_p(bit) && DISABLED_MASK_BIT_SET(bit) ? 0 : static_cpu_has(bit)) I think LA57 shouldn't be in DISABLED_MASK_BIT_SET() at all, in our case the relevant disable mask is: #define DISABLED_MASK16 (DISABLE_PKU|DISABLE_OSPKE|DISABLE_LA57|DISABLE_UMIP| \ DISABLE_ENQCMD) Here we should have: #ifdef CONFIG_X86_5LEVEL # define DISABLE_LA57 0 #else # define DISABLE_LA57 (1<<(X86_FEATURE_LA57 & 31)) #endif So DISABLE_LA57 should be 0 when 5level enabled (true in my case). Then we really should land at static_cpu_has(). I checked up the code generated and surprisingly it's fairly fast indeed: (after fetching swapfile_maximum_size() and put into %rax, I'll change that into a variable soon..) 0xffffffff83932e41 <+185>: mov $0x1,%edx 0xffffffff83932e46 <+190>: shl $0x24,%rdx 0xffffffff83932e4a <+194>: xor %r8d,%r8d 0xffffffff83932e4d <+197>: cmp %rdx,%rax 0xffffffff83932e50 <+200>: jb 0xffffffff83932e59 0xffffffff83932e52 <+202>: movb $0x1,0xeab897(%rip) # 0xffffffff847de6f0 0xffffffff83932e59 <+209>: mov %r8d,%eax Obviously on my testing host SWP_MIG_TOTAL_BITS is directly set as $0x24 (which reflects a 4-level pgtable) but frankly I cannot tell how it did that without checking boot cpu x86_capabilities flags.. I'm pretty sure my kernel config has CONFIG_X86_5LEVEL=y. It'll be great if anyone already notices why it can be optimized into a constant, but even if so I'm not confident that'll be a constant for all the hosts and whether static_cpu_has() will still consume some insns. Since the change is fairly simple after previous patch, I think it'll be nice to keep it too. Thanks, -- Peter Xu