From patchwork Fri Oct 27 18:21:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13438941 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 65D4EC25B70 for ; Fri, 27 Oct 2023 18:23:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D9CD180016; Fri, 27 Oct 2023 14:22:56 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D25418000C; Fri, 27 Oct 2023 14:22:56 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B9E4680016; Fri, 27 Oct 2023 14:22:56 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 9FE0E8000C for ; Fri, 27 Oct 2023 14:22:56 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 65782B61E3 for ; Fri, 27 Oct 2023 18:22:56 +0000 (UTC) X-FDA: 81392062752.18.91AA3E2 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by imf05.hostedemail.com (Postfix) with ESMTP id 76FB010000C for ; Fri, 27 Oct 2023 18:22:54 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=HfqSisbO; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf05.hostedemail.com: domain of 3_f87ZQYKCB4M84HD6AIIAF8.6IGFCHOR-GGEP46E.ILA@flex--seanjc.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3_f87ZQYKCB4M84HD6AIIAF8.6IGFCHOR-GGEP46E.ILA@flex--seanjc.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1698430974; h=from:from:sender:reply-to: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=KesXVMWlLgDxABh+fu6pK0xRQzS+y87PSmDv/paodW4=; b=M5Jt1O9HINsDdcxfa9PmnwQL22obitryPknGAKoCHyY4ex3Jwo9+8NjG/SYVFJuk5hS3vB HvJUxmrQHW+rgDKbZoENABCKOynpt8jk9r5w390mr+WJOa8EQuZmjZ39ZeK9lhTok7R5TU kKysYpJSzs4q4nhQ7Gzn/g4N/9bxkio= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=HfqSisbO; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf05.hostedemail.com: domain of 3_f87ZQYKCB4M84HD6AIIAF8.6IGFCHOR-GGEP46E.ILA@flex--seanjc.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3_f87ZQYKCB4M84HD6AIIAF8.6IGFCHOR-GGEP46E.ILA@flex--seanjc.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698430974; a=rsa-sha256; cv=none; b=Yb1Hi0vq1xUEwEYIVgUgBhOJrlXcBLX6lIYkZZFESGG5SfwcBjDSNtCHheZqx4rd6Tm2J9 v27HWT8ibNMzlIpdlpH9XfWJHRGSjfk3IpQq40sFha1BCM8qWA3fOCjgtU4kWP1HzcPVvi 7boofWRa38+JJ58N27yst+Zu9yhTAAQ= Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-d8486b5e780so1772917276.0 for ; Fri, 27 Oct 2023 11:22:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698430973; x=1699035773; darn=kvack.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=KesXVMWlLgDxABh+fu6pK0xRQzS+y87PSmDv/paodW4=; b=HfqSisbOav1qnaXt+cqAB0kBmWUjm/Is/vDyxkeKw1hFVQydFFphrArB3QGl+C/sK+ ncJj96dHVofWaj6SJ1IT38MG0Ux8gaMw/PI4nEROrwahk2L2/8neXZdzpQ2BqRsbt6Pp rEsiOy4bzdB8UlDxvclmTLiTq0md0vSdVlpW2/wbmXyuibjS7F/wzcZjTVotmieVFg82 j5YAfC8S34hCJMWKYmNoxlhSJo/gQMP/GJkIl+iRw7QOBChI/zxRPSSVtCO5e8u6lyvI x/22ZYMiFCujTpMS33OTMKF0+VOue76rLBxC1h9ckuRpioyWO9YALKx7Bc9PvRr+1tbl WYEg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698430973; x=1699035773; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=KesXVMWlLgDxABh+fu6pK0xRQzS+y87PSmDv/paodW4=; b=WNzCudU2iqecL1AXZFlEL403Xekp6FsvHNXJfvplWz2+5+r8YcA8G8R8RxisuW1xAv O5ty0l090/RmaYs8WfjmPFMZBeRi4ZuDNOoVY1vct7AdSZnkLn7Wmmg/UysiuIVrsA2P LtunQbTtA402RK0K16fNUNb85yhafpXW9emqDB0/MYhaQdIsBRkamivcpxnwZ0yTgqZ8 Pf9UT4ilzVX6udomIZlRojanXLbnHmvXfconIti6PWg3CmCt9Rde+odvaUBERjImzSV9 F74Sgbu6cLMSKw713TJCIdDXHdVSe+wnN4xwOP68+mBHi7D5yvIUdjzpGsufEa/Bb1ho 8AoA== X-Gm-Message-State: AOJu0YwoPJhWu5w92fZJyOr4Wbo3pklJzCIUPpStP+uUMbgBEvena8Uz OrDbDewkbgU/yd7sxhn2Rib4qhaT0yg= X-Google-Smtp-Source: AGHT+IEnuAFhzspZaQ7IFc3ileY3RuCJW0iKBNsx2McP6lY6HFfWXzfm0mDQuIa0sPNZvKStqElvm4PIJQ8= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:1083:b0:da0:567d:f819 with SMTP id v3-20020a056902108300b00da0567df819mr78694ybu.10.1698430973571; Fri, 27 Oct 2023 11:22:53 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 27 Oct 2023 11:21:56 -0700 In-Reply-To: <20231027182217.3615211-1-seanjc@google.com> Mime-Version: 1.0 References: <20231027182217.3615211-1-seanjc@google.com> X-Mailer: git-send-email 2.42.0.820.g83a721a137-goog Message-ID: <20231027182217.3615211-15-seanjc@google.com> Subject: [PATCH v13 14/35] mm: Add AS_UNMOVABLE to mark mapping as completely unmovable From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Sean Christopherson , Alexander Viro , Christian Brauner , "Matthew Wilcox (Oracle)" , Andrew Morton Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Xiaoyao Li , Xu Yilun , Chao Peng , Fuad Tabba , Jarkko Sakkinen , Anish Moorthy , David Matlack , Yu Zhang , Isaku Yamahata , " =?utf-8?q?Micka=C3=ABl_Sala?= =?utf-8?q?=C3=BCn?= " , Vlastimil Babka , Vishal Annapurve , Ackerley Tng , Maciej Szmigiero , David Hildenbrand , Quentin Perret , Michael Roth , Wang , Liam Merwick , Isaku Yamahata , "Kirill A . Shutemov" X-Rsp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qCPTEeAmnBICNs3BopvdMGoo D6Ri6dsJ 01UGgLc+sGYdMOYkNrp1dNLgO9JFcmx8spgd3zJSGt1JxObpJeVHkdifaXMaYyllRXP4WYLkr6CcsLTSwTUJ/r+rlZPd+fIcbArktZvTOXsK+DU2u7V/enJ9kk/KQ0loBpG9C+NM6Q7GB3aiahM7y31hbzQ72SDZYtYfM99cs43HP7NBEmTX2e/4Oi2P8YWwJ3Ta3EwtIHvv6XC004Zf/ZKxU1p4sk/qRfPQ2MQA8jDvA5QrpT0xgk5nqIaU5n2PCUzEyrBxY9Jk6Z8EsbjNFYv1HdfJRbcJjRNfo5bOMpMwS8YDQV/a25GMI2RWdT4K0hZEE5f9/+cbgo4M64y8CFZ5lq/TINfvMmA/PoM0Rd4NO3f49HnvuGFTuMaxhwX7OeCurCT3DB05F/DQDuLqLE7QF6Y++M8K6N6DYGqcUJ35X+p5s77GzQgSrUHvoSRWH1Z262PCxKTed2CinKijQIZvuCVjLUWekBDlgmSwVsnLJW+vLNP71X9p1U/+EVBVgv0SIdySFYm9uHBN7M4vSp0SyHOVMjMn9ozby970fKWECEKW0ZB88YRPIevPAuUjr0DqwdjJzclX0VCfxU/TgsGexvVwsZf9Va37esPNNm1nkWVtGYNj1wrk4ESZHnXHSokrpVYBsEp7bAFKMHwdzf7ZCHaKKJ2lxPZ22xAQEcLHmtq3g7oGuJVaC8U6IXGwawn6HvyhHVrXSnxDWkglTcTS1+Sd9yGU+fLHgAz20mf98HUqw2PqNrd+KJxd5MODbcOMfUwAR2MhO9IUlOZ7BQ8UCyizEAIqhr0czCH0F+BTp4Nry4YxsX080HoZ6BbwQ0aWIPHZRn4HOfic4NIS8NgrcQ27M/2VyMvIU0XwlP++TuqUDIh9pE1dDgg== 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: List-Subscribe: List-Unsubscribe: Add an "unmovable" flag for mappings that cannot be migrated under any circumstance. KVM will use the flag for its upcoming GUEST_MEMFD support, which will not support compaction/migration, at least not in the foreseeable future. Test AS_UNMOVABLE under folio lock as already done for the async compaction/dirty folio case, as the mapping can be removed by truncation while compaction is running. To avoid having to lock every folio with a mapping, assume/require that unmovable mappings are also unevictable, and have mapping_set_unmovable() also set AS_UNEVICTABLE. Cc: Matthew Wilcox Co-developed-by: Vlastimil Babka Signed-off-by: Vlastimil Babka Signed-off-by: Sean Christopherson --- include/linux/pagemap.h | 19 +++++++++++++++++- mm/compaction.c | 43 +++++++++++++++++++++++++++++------------ mm/migrate.c | 2 ++ 3 files changed, 51 insertions(+), 13 deletions(-) diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h index 351c3b7f93a1..82c9bf506b79 100644 --- a/include/linux/pagemap.h +++ b/include/linux/pagemap.h @@ -203,7 +203,8 @@ enum mapping_flags { /* writeback related tags are not used */ AS_NO_WRITEBACK_TAGS = 5, AS_LARGE_FOLIO_SUPPORT = 6, - AS_RELEASE_ALWAYS, /* Call ->release_folio(), even if no private data */ + AS_RELEASE_ALWAYS = 7, /* Call ->release_folio(), even if no private data */ + AS_UNMOVABLE = 8, /* The mapping cannot be moved, ever */ }; /** @@ -289,6 +290,22 @@ static inline void mapping_clear_release_always(struct address_space *mapping) clear_bit(AS_RELEASE_ALWAYS, &mapping->flags); } +static inline void mapping_set_unmovable(struct address_space *mapping) +{ + /* + * It's expected unmovable mappings are also unevictable. Compaction + * migrate scanner (isolate_migratepages_block()) relies on this to + * reduce page locking. + */ + set_bit(AS_UNEVICTABLE, &mapping->flags); + set_bit(AS_UNMOVABLE, &mapping->flags); +} + +static inline bool mapping_unmovable(struct address_space *mapping) +{ + return test_bit(AS_UNMOVABLE, &mapping->flags); +} + static inline gfp_t mapping_gfp_mask(struct address_space * mapping) { return mapping->gfp_mask; diff --git a/mm/compaction.c b/mm/compaction.c index 38c8d216c6a3..12b828aed7c8 100644 --- a/mm/compaction.c +++ b/mm/compaction.c @@ -883,6 +883,7 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn, /* Time to isolate some pages for migration */ for (; low_pfn < end_pfn; low_pfn++) { + bool is_dirty, is_unevictable; if (skip_on_failure && low_pfn >= next_skip_pfn) { /* @@ -1080,8 +1081,10 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn, if (!folio_test_lru(folio)) goto isolate_fail_put; + is_unevictable = folio_test_unevictable(folio); + /* Compaction might skip unevictable pages but CMA takes them */ - if (!(mode & ISOLATE_UNEVICTABLE) && folio_test_unevictable(folio)) + if (!(mode & ISOLATE_UNEVICTABLE) && is_unevictable) goto isolate_fail_put; /* @@ -1093,26 +1096,42 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn, if ((mode & ISOLATE_ASYNC_MIGRATE) && folio_test_writeback(folio)) goto isolate_fail_put; - if ((mode & ISOLATE_ASYNC_MIGRATE) && folio_test_dirty(folio)) { - bool migrate_dirty; + is_dirty = folio_test_dirty(folio); + + if (((mode & ISOLATE_ASYNC_MIGRATE) && is_dirty) || + (mapping && is_unevictable)) { + bool migrate_dirty = true; + bool is_unmovable; /* * Only folios without mappings or that have - * a ->migrate_folio callback are possible to - * migrate without blocking. However, we may - * be racing with truncation, which can free - * the mapping. Truncation holds the folio lock - * until after the folio is removed from the page - * cache so holding it ourselves is sufficient. + * a ->migrate_folio callback are possible to migrate + * without blocking. + * + * Folios from unmovable mappings are not migratable. + * + * However, we can be racing with truncation, which can + * free the mapping that we need to check. Truncation + * holds the folio lock until after the folio is removed + * from the page so holding it ourselves is sufficient. + * + * To avoid locking the folio just to check unmovable, + * assume every unmovable folio is also unevictable, + * which is a cheaper test. If our assumption goes + * wrong, it's not a correctness bug, just potentially + * wasted cycles. */ if (!folio_trylock(folio)) goto isolate_fail_put; mapping = folio_mapping(folio); - migrate_dirty = !mapping || - mapping->a_ops->migrate_folio; + if ((mode & ISOLATE_ASYNC_MIGRATE) && is_dirty) { + migrate_dirty = !mapping || + mapping->a_ops->migrate_folio; + } + is_unmovable = mapping && mapping_unmovable(mapping); folio_unlock(folio); - if (!migrate_dirty) + if (!migrate_dirty || is_unmovable) goto isolate_fail_put; } diff --git a/mm/migrate.c b/mm/migrate.c index 2053b54556ca..ed874e43ecd7 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -956,6 +956,8 @@ static int move_to_new_folio(struct folio *dst, struct folio *src, if (!mapping) rc = migrate_folio(mapping, dst, src, mode); + else if (mapping_unmovable(mapping)) + rc = -EOPNOTSUPP; else if (mapping->a_ops->migrate_folio) /* * Most folios have a mapping and most filesystems