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 79B2EC00140 for ; Fri, 5 Aug 2022 16:30:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EFE818E0005; Fri, 5 Aug 2022 12:30:14 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EAF7C8E0001; Fri, 5 Aug 2022 12:30:14 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D29838E0005; Fri, 5 Aug 2022 12:30:14 -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 C07DB8E0001 for ; Fri, 5 Aug 2022 12:30:14 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 9459112190E for ; Fri, 5 Aug 2022 16:30:14 +0000 (UTC) X-FDA: 79766076348.07.424F7D0 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf26.hostedemail.com (Postfix) with ESMTP id 5E5E9140145 for ; Fri, 5 Aug 2022 16:30:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1659717012; 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: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=QLBK3y06oewrdTqiS5Eur6v+Y+JKLw+q+kw7LKVARhI=; b=AAyG2pMcEbz9g6rcU7b57HgEOIFn46Pnli7Gw7XW8fhGB7BCMVztR/Vc+tOaUmZjMDwXRn LbpjcHNW84i6mz0bDiVTQ3TNokVkWsD2iuQ7Ag3vPfavlAzNfjnzRmnUNcS5r8NBvI+6o5 EQnjyUTw0G97Ncj2T31TQ1sZx6b5UVw= Received: from mail-qk1-f200.google.com (mail-qk1-f200.google.com [209.85.222.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-424-z92bXTCUOMivqcMCqNpjxw-1; Fri, 05 Aug 2022 12:30:11 -0400 X-MC-Unique: z92bXTCUOMivqcMCqNpjxw-1 Received: by mail-qk1-f200.google.com with SMTP id bs41-20020a05620a472900b006b8e84d6cddso2322727qkb.19 for ; Fri, 05 Aug 2022 09:30:11 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :x-gm-message-state:from:to:cc; bh=QLBK3y06oewrdTqiS5Eur6v+Y+JKLw+q+kw7LKVARhI=; b=pqpkUUJAeSeb2C1HHDskcPj/TVX/6lg2e/PhEXwcGc/YErEc4mPqagORwOds9+n+Eb idtmORzj2zkemELURUeI1CWU/KuvUVIyisxXlnOvCG0J6iO5jQng/CpqBfHKcQHs1v0t xmbk3zZbxK/RKmaOV3DlAffVDBbOQjoP5cZvNoMAnHO9FWBn+Chp9BjkrbZOJJgcrOSM J7ESLuYZ+AjM1/Jz1ccor3nBJiHj9rmONa0O5FWJDJXPM5YhmzMRbTbxrSQMrxC4FoMV uU/gI5rZUyn4ceaNzrfo+9Cz1LhpR8adfhMYXR24DFTNWbSvOjnHeOVCxM2GfXaefrAw mUlg== X-Gm-Message-State: ACgBeo1Sy7IGpcGtZd3JTrmwwugJ5me/JGeHc63EypjUrvjFIu4VbJaz CU+9gdEEHE43uWE09rqEpepX1wtvHN4ph38iDtgQDTWb67N23WB8QTM9zj+6TPufL40MojOixKp U8D2amR1RMUk= X-Received: by 2002:a05:620a:404d:b0:6a7:d3d:df9b with SMTP id i13-20020a05620a404d00b006a70d3ddf9bmr5539920qko.530.1659717010590; Fri, 05 Aug 2022 09:30:10 -0700 (PDT) X-Google-Smtp-Source: AA6agR4zbYHH6346SGrZSYnDtUFlIw87LqOAw1abHdw2afxGDi1D819B+4fFErIVtH+D97Chhrs7Ag== X-Received: by 2002:a05:620a:404d:b0:6a7:d3d:df9b with SMTP id i13-20020a05620a404d00b006a70d3ddf9bmr5539902qko.530.1659717010277; Fri, 05 Aug 2022 09:30:10 -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 w16-20020a05620a425000b006b60e1cf2c2sm2954502qko.0.2022.08.05.09.30.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Aug 2022 09:30:09 -0700 (PDT) Date: Fri, 5 Aug 2022 12:30:08 -0400 From: Peter Xu To: Nadav Amit Cc: Linux MM , linux-kernel@vger.kernel.org, Huang Ying , Andrea Arcangeli , David Hildenbrand , Minchan Kim , Andrew Morton , Vlastimil Babka , 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> MIME-Version: 1.0 In-Reply-To: X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1659717013; 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=QLBK3y06oewrdTqiS5Eur6v+Y+JKLw+q+kw7LKVARhI=; b=h/9EkcVZm1RL5M/0+2Z44xkTFIFBnGpvu0KJOfvII4pEmopn3FznkBMXU+KAiiHH843lJZ zi3NE+D5OPJ5XbULufwkb6clIU1SUVoxIdfAf1QBIsyQX2Q+iWtZUmt+uUQ6ORcscQkqtI lhDfOjPyeU1Y4uj+P8BYKqTmR9eh02U= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=AAyG2pMc; spf=pass (imf26.hostedemail.com: domain of peterx@redhat.com designates 170.10.133.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=1659717013; a=rsa-sha256; cv=none; b=ZRuKxSyXw2FnIQbzA1ezNrfO1A6DBsIQzZd3xo4vFGMk10CYoAG6uIzIGJtWU9QH3PIVtz AFOfcl0yGSdRU9zNGl8wNmh4EW9WvkhCjKuIds100E8/XDSEeC8G69GnRRubwpHCOBkB3N 7kzFjacUenECByJKjYm9wyOsY2kj33U= X-Stat-Signature: z5dwhcu7woxz4ra39ma5timermoodawh X-Rspam-User: X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 5E5E9140145 Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=AAyG2pMc; spf=pass (imf26.hostedemail.com: domain of peterx@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-HE-Tag: 1659717013-439447 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, Aug 04, 2022 at 03:40:57PM -0700, Nadav Amit wrote: > On Aug 4, 2022, at 1:39 PM, 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 > > Shouldn’t the ifdef go into linux/swapfile.h if that’s the right thing to do > to prevent others from mistakenly including it? swapfile.h is for max_swapfile_size() that's referenced in the new code. If !CONFIG_MMU then there shouldn't be a reference to max_swapfile_size() even if swapops.h included, then logically it should be after CONFIG_MMU? > > > + > > /* > > * 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) > > Any reason not to use BIT(x) ? Yeh why not.. > > > + > > 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); > > This is an actual a function call (unless LTO has some trick). A bit of a > shame it cannot be at least memoized. > > Or at least mark max_swapfile_size() as __attribute_const__ so it would not > be called twice for make_migration_entry_young() and > make_migration_entry_dirty(). I didn't take too much effort on this one since we're on swap path and I assumed that's not a super hot path. But __attribute_const__ sounds good and easy to get, thanks. Perhaps I should mark it on migration_entry_supports_ad() as a whole? Note that unfortunately SWP_MIG_TOTAL_BITS may not be a const either (see how that define roots back to MAX_PHYSMEM_BITS, where on x86_64 it needs to check 5-lvl). > > > +#else > > + return false; > > +#endif > > +} > > + > > +static inline swp_entry_t make_migration_entry_young(swp_entry_t entry) > > +{ > > + if (migration_entry_supports_ad()) > > + return swp_entry(swp_type(entry), > > + swp_offset(entry) | SWP_MIG_YOUNG); > > + return entry; > > +} > > + > > +static inline bool is_migration_entry_young(swp_entry_t entry) > > +{ > > + if (migration_entry_supports_ad()) > > + return swp_offset(entry) & SWP_MIG_YOUNG; > > + /* Keep the old behavior of aging page after migration */ > > + return false; > > +} > > + > > +static inline swp_entry_t make_migration_entry_dirty(swp_entry_t entry) > > +{ > > + if (migration_entry_supports_ad()) > > + return swp_entry(swp_type(entry), > > + swp_offset(entry) | SWP_MIG_DIRTY); > > + return entry; > > +} > > + > > +static inline bool is_migration_entry_dirty(swp_entry_t entry) > > +{ > > + if (migration_entry_supports_ad()) > > + return swp_offset(entry) & SWP_MIG_YOUNG_BIT; > > Shouldn’t it be SWP_MIG_DIRTY ? Oops, yes. > > > + /* Keep the old behavior of clean page after migration */ > > + return false; > > +} > > + > > extern void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep, > > spinlock_t *ptl); > > extern void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd, > > @@ -311,6 +382,26 @@ static inline int is_readable_migration_entry(swp_entry_t entry) > > return 0; > > } > > > > +static inline swp_entry_t make_migration_entry_young(swp_entry_t entry) > > +{ > > + return entry; > > +} > > + > > +static inline bool is_migration_entry_young(swp_entry_t entry) > > +{ > > + return false; > > +} > > + > > +static inline swp_entry_t make_migration_entry_dirty(swp_entry_t entry) > > +{ > > + return entry; > > +} > > + > > +static inline bool is_migration_entry_dirty(swp_entry_t entry) > > +{ > > + return false; > > +} > > + > > #endif > > While at it, can you change to: > > #endif /* CONFIG_MIGRATION */ > > [ these ifdefs burn my eyes ] Ok, but there're a bunch of "ifdef"s in this header that are missing those. Maybe I'll add a separate patch for all of them just to do the commenting. > > Other than that looks good. Thanks, -- Peter Xu