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 76983C25B08 for ; Fri, 5 Aug 2022 16:36:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F288E6B0071; Fri, 5 Aug 2022 12:36:55 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EB20B8E0002; Fri, 5 Aug 2022 12:36:55 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D2B958E0001; Fri, 5 Aug 2022 12:36:55 -0400 (EDT) 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 BDB0A6B0071 for ; Fri, 5 Aug 2022 12:36:55 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 95A24C1985 for ; Fri, 5 Aug 2022 16:36:55 +0000 (UTC) X-FDA: 79766093190.22.EFD6643 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf02.hostedemail.com (Postfix) with ESMTP id 1E01A80142 for ; Fri, 5 Aug 2022 16:36:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1659717414; 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=8Tfx8xV8I3dUG2KlFo6XKUZmlUBS6xXBNsMNZTg5/+M=; b=fF4xoRrciAya4NNirWZlJqtDw7YtoxCmdWb+YF7z+YvpQT8aOLmpK6HHT0YHlS7n/58Xc0 qCoM2wFMZT87Kwr7OS2MgVOpVFWV1ZnVubhjTm/lz6Bt+aFgJmYkk5rkPM2KMJ/cIG0O3z QsmXq8IMJYyLX0gVOKWQ4PoB1Khgor4= Received: from mail-qt1-f200.google.com (mail-qt1-f200.google.com [209.85.160.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-473-WvWkDnlcM6uCCO4lP5vaBw-1; Fri, 05 Aug 2022 12:36:53 -0400 X-MC-Unique: WvWkDnlcM6uCCO4lP5vaBw-1 Received: by mail-qt1-f200.google.com with SMTP id e3-20020ac84b43000000b0033efb06a30aso2257439qts.22 for ; Fri, 05 Aug 2022 09:36:53 -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=8Tfx8xV8I3dUG2KlFo6XKUZmlUBS6xXBNsMNZTg5/+M=; b=PYpsdN3g+4nsYnTS4RSqxzCcX32NVtPVX7nuifsW4btRSuKYoN6XT0VdGQ58N1GUHV Ounm70GHrC0o1G7QLers+KdjNFmH/oV7fOBxmnvz4xLEPeKZ6TjUCSMcqdgaTO/e6HfO I05B69SR5y/JURuf+hVZ9t3VAz8gmdIjYcOXL6ivfTIOJGC3+YvFYsyZfc0VIrkcjdcw h1lODiC/qS/kJvh8iIM+Jfhhtc6jLvPXmvCKuZSOplL/alV4Vkyd3JLynMT+qqY+mHQE JqCTF3u5dyGfY7R4XgWiagF2KlN+xtvPCR3Y0bj0QMilaArhFgFJRuCeP5D5sVivzqmb /YBA== X-Gm-Message-State: ACgBeo1IewsX8IoygYk7faizBh61vKcgWvKs4dtGvs2dNA0NSCr6jAbV r5UVVsXi0g8hD6l4yRhq89KazcumT/cmY0iSdXS0WuRAsMjHp+3eu4vTOkUnn48zE01Af9qV1fD vHTKXLZScXsY= X-Received: by 2002:a05:622a:174a:b0:31e:f68d:66c with SMTP id l10-20020a05622a174a00b0031ef68d066cmr6738132qtk.476.1659717413008; Fri, 05 Aug 2022 09:36:53 -0700 (PDT) X-Google-Smtp-Source: AA6agR4hT/jkBcfkO40XTgJXLXq45NwcY6dfXm4fsskstjmPEI2oo9ap8a75LXWY8jXj1QwbDVn7wQ== X-Received: by 2002:a05:622a:174a:b0:31e:f68d:66c with SMTP id l10-20020a05622a174a00b0031ef68d066cmr6738086qtk.476.1659717412418; Fri, 05 Aug 2022 09:36:52 -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 az38-20020a05620a172600b006b8619a67f4sm3050478qkb.34.2022.08.05.09.36.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Aug 2022 09:36:51 -0700 (PDT) Date: Fri, 5 Aug 2022 12:36:50 -0400 From: Peter Xu To: David Hildenbrand Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Huang Ying , Andrea Arcangeli , Minchan Kim , Andrew Morton , Vlastimil Babka , Nadav Amit , Hugh Dickins , Andi Kleen , "Kirill A . Shutemov" Subject: Re: [PATCH v2 2/2] mm: Remember young/dirty bit for page migrations Message-ID: References: <20220804203952.53665-1-peterx@redhat.com> <20220804203952.53665-3-peterx@redhat.com> <62d52657-12d2-8563-4ead-027480065d9f@redhat.com> MIME-Version: 1.0 In-Reply-To: <62d52657-12d2-8563-4ead-027480065d9f@redhat.com> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8 Content-Disposition: inline ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=fF4xoRrc; spf=pass (imf02.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=1659717415; a=rsa-sha256; cv=none; b=ayl4SFzaggMUWeExqGFnWRRwubWyjcB3NIKoVLnG5xOkTbzbUvnYRDgs1HNPiVZ88CubJr 8xLjmam8Z7bvyAIe5AsvTc3obQLlCCDA+hFqFEnNSRcaDQHep+SmybOPXw753O6Jh69AMZ SzImI4Zk+7H0ejU/xC1LtQUMidl6bfQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1659717415; 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=8Tfx8xV8I3dUG2KlFo6XKUZmlUBS6xXBNsMNZTg5/+M=; b=TE4Y7XkaJogbpJ/WWWw6wbxdSdG+lxghA0uAPD9fByqWZweCTyXlgRpOEoxbZ1X9sYYsI2 +9DpgSqZpQaomRjnC4QvH9/Y4f6Japq6N6PN0qeOb18cU15qH6h7Ierg7ilopVUDuB7KRu aHuU9e/n1aaAWf2lL40/Es4HqnhYtbE= X-Stat-Signature: fzk3yr3hpapjqquodq539dp58ducmais X-Rspamd-Queue-Id: 1E01A80142 Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=fF4xoRrc; spf=pass (imf02.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: rspam12 X-HE-Tag: 1659717414-326345 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 Fri, Aug 05, 2022 at 02:17:55PM +0200, David Hildenbrand wrote: > On 04.08.22 22:39, Peter Xu wrote: > > When page migration happens, we always ignore the young/dirty bit settings > > in the old pgtable, and marking the page as old in the new page table using > > either pte_mkold() or pmd_mkold(), and keeping the pte clean. > > > > That's fine from functional-wise, but that's not friendly to page reclaim > > because the moving page can be actively accessed within the procedure. Not > > to mention hardware setting the young bit can bring quite some overhead on > > some systems, e.g. x86_64 needs a few hundreds nanoseconds to set the bit. > > The same slowdown problem to dirty bits when the memory is first written > > after page migration happened. > > > > Actually we can easily remember the A/D bit configuration and recover the > > information after the page is migrated. To achieve it, define a new set of > > bits in the migration swap offset field to cache the A/D bits for old pte. > > Then when removing/recovering the migration entry, we can recover the A/D > > bits even if the page changed. > > > > One thing to mention is that here we used max_swapfile_size() to detect how > > many swp offset bits we have, and we'll only enable this feature if we know > > the swp offset can be big enough to store both the PFN value and the young > > bit. Otherwise the A/D bits are dropped like before. > > > > Signed-off-by: Peter Xu > > --- > > include/linux/swapops.h | 91 +++++++++++++++++++++++++++++++++++++++++ > > mm/huge_memory.c | 26 +++++++++++- > > mm/migrate.c | 6 ++- > > mm/migrate_device.c | 4 ++ > > mm/rmap.c | 5 ++- > > 5 files changed, 128 insertions(+), 4 deletions(-) > > > > diff --git a/include/linux/swapops.h b/include/linux/swapops.h > > index 1d17e4bb3d2f..34aa448ac6ee 100644 > > --- a/include/linux/swapops.h > > +++ b/include/linux/swapops.h > > @@ -8,6 +8,8 @@ > > > > #ifdef CONFIG_MMU > > > > +#include > > + > > /* > > * swapcache pages are stored in the swapper_space radix tree. We want to > > * get good packing density in that tree, so the index should be dense in > > @@ -35,6 +37,24 @@ > > #endif > > #define SWP_PFN_MASK ((1UL << SWP_PFN_BITS) - 1) > > > > +/** > > + * Migration swap entry specific bitfield definitions. > > + * > > + * @SWP_MIG_YOUNG_BIT: Whether the page used to have young bit set > > + * @SWP_MIG_DIRTY_BIT: Whether the page used to have dirty bit set > > + * > > + * Note: these bits will be stored in migration entries iff there're enough > > + * free bits in arch specific swp offset. By default we'll ignore A/D bits > > + * when migrating a page. Please refer to migration_entry_supports_ad() > > + * for more information. > > + */ > > +#define SWP_MIG_YOUNG_BIT (SWP_PFN_BITS) > > +#define SWP_MIG_DIRTY_BIT (SWP_PFN_BITS + 1) > > +#define SWP_MIG_TOTAL_BITS (SWP_PFN_BITS + 2) > > + > > +#define SWP_MIG_YOUNG (1UL << SWP_MIG_YOUNG_BIT) > > +#define SWP_MIG_DIRTY (1UL << SWP_MIG_DIRTY_BIT) > > + > > static inline bool is_pfn_swap_entry(swp_entry_t entry); > > > > /* Clear all flags but only keep swp_entry_t related information */ > > @@ -265,6 +285,57 @@ static inline swp_entry_t make_writable_migration_entry(pgoff_t offset) > > return swp_entry(SWP_MIGRATION_WRITE, offset); > > } > > > > +/* > > + * Returns whether the host has large enough swap offset field to support > > + * carrying over pgtable A/D bits for page migrations. The result is > > + * pretty much arch specific. > > + */ > > +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); > > +#else > > + return false; > > +#endif > > +} > > > This looks much cleaner to me. It might be helpful to draw an ascii > picture where exatcly these bits reside isnide the offset. Yes that'll be helpful especially when more bits are defined. Not sure how much it'll help for now but I can definitely do that. > > > + > > +static inline swp_entry_t make_migration_entry_young(swp_entry_t entry) > > +{ > > + if (migration_entry_supports_ad()) > > Do we maybe want to turn that into a static key and enable it once and > for all? As Nadav says, the repeated max_swapfile_size() calls/checks > might be worth optimizing out. Since there're a few arch related issues to answer (as replied to Nadav - both max_swapfile_size and SWP_MIG_TOTAL_BITS may not be constant), my current plan is to first attach the const attribute, then leave the other optimizations for later. If this is a super hot path, I probably need to do this in reversed order, but hopefully it's fine to this case. Thanks, -- Peter Xu