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 D6DF0C4332F for ; Sun, 5 Nov 2023 16:32:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6DF68440159; Sun, 5 Nov 2023 11:32:38 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 667F2440150; Sun, 5 Nov 2023 11:32:38 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4E227440159; Sun, 5 Nov 2023 11:32:38 -0500 (EST) 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 3B2EF440150 for ; Sun, 5 Nov 2023 11:32:38 -0500 (EST) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 14B55160318 for ; Sun, 5 Nov 2023 16:32:38 +0000 (UTC) X-FDA: 81424443996.07.8C04064 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf27.hostedemail.com (Postfix) with ESMTP id 2841F40002 for ; Sun, 5 Nov 2023 16:32:35 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=FqGt3wtF; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf27.hostedemail.com: domain of pbonzini@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=pbonzini@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1699201956; 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=AdANIEnF562apfJyOTMYsmSKgc8hkZrlNovbg28IThg=; b=Azx8NYNMmlvUa6o2U/DbnSbaptPb8eOQXi+PdFztnFD3A742LQBv56eFktthloFbH4cenH 2A2cMPjiE3WsqkJo/uf6Yrc4UFvuZtN1ChzbdF+R3xN5jSZl51sKeS/gR2/VQqFmCRtvdY B1dKHHa7bfGtb6JBoXDP9wVTNPsvqF0= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=FqGt3wtF; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf27.hostedemail.com: domain of pbonzini@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=pbonzini@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1699201956; a=rsa-sha256; cv=none; b=TRZjMmhXebdCtVgUSJDFloR9H0iKJGH7KusMiqNKULnmGaW2MsHCm29Ah8yLuobz4LAuvR NJhUIXNEDy5PQiut/ksPSlxO0Go1b/vrzgwZcNXYUGDBMK1YL85kfeVynjB2uS4hMT+kuV Cn+6H2HPkkfb9UE9pAx5M/1ZMJnbVU4= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1699201955; 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=AdANIEnF562apfJyOTMYsmSKgc8hkZrlNovbg28IThg=; b=FqGt3wtFHq0kXwI4QmEBMHDZbaoQW/vY/1J35zqwJGhtPo7UNzL0128TeZVQhzUjZSwHVG J2mU56t7xJSpKC33T7J6hJ0aNCZnydmmwtCn5XZwf/VAMyU4p9rvfiMNErOLOWeZGXbsgN Ap+GO9Vb9l8aJ2619rCs2gBX4zqA5zk= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-303-c8LRdjnxO_20IIX1RMc9ZA-1; Sun, 05 Nov 2023 11:32:32 -0500 X-MC-Unique: c8LRdjnxO_20IIX1RMc9ZA-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 3DB2738117F4; Sun, 5 Nov 2023 16:32:30 +0000 (UTC) Received: from avogadro.redhat.com (unknown [10.39.192.93]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4E1F52166B26; Sun, 5 Nov 2023 16:32:23 +0000 (UTC) From: Paolo Bonzini 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=20Sala=C3=BCn?= , Vlastimil Babka , Vishal Annapurve , Ackerley Tng , Maciej Szmigiero , David Hildenbrand , Quentin Perret , Michael Roth , Wang , Liam Merwick , Isaku Yamahata , "Kirill A. Shutemov" Subject: [PATCH 13/34] mm: Add AS_UNMOVABLE to mark mapping as completely unmovable Date: Sun, 5 Nov 2023 17:30:16 +0100 Message-ID: <20231105163040.14904-14-pbonzini@redhat.com> In-Reply-To: <20231105163040.14904-1-pbonzini@redhat.com> References: <20231105163040.14904-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Type: text/plain Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.6 X-Rspam-User: X-Stat-Signature: ojzsr3ked7asscntgs5jix1p7gae85ms X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 2841F40002 X-HE-Tag: 1699201955-981288 X-HE-Meta: U2FsdGVkX1+SDIk3HzP8XFh5U0O0O5BFQNb5YhAQM6YHx7RtLWBGp/AG34yQK0cN3zSWPC9XmLS9drXTNR50wDWJyM99Nz7AMqybr6u5A4PWXdQjHRc8h4Px0SJBbgYzxJLVnanufalq2Rbaw3dJ/xkkoNMmmpCjn3vGGUeD9k3gNqodsAeCPopLm+IMsXeWaJ9Z05XwT4dPfeDqwjoOt4iuzWtfq6PnAD3JZt6uElOn3OXnvRP3EpMjz3cqpgdQxIO8Ofn43NbNH3l0WbtJ1iX3eIm88NciZUVCPIIjeL31ByeSpEmcmwQLiaKtUsW1fMc9+pZKYjrmIX50LRSmCCMUh6NXwllUfHUywDa5jXBzF14seVJ7oI4wX4ZRBWfKjqfYgeA15IlyHbU7/y5/v12qnZ3pxnlk2DvjCk1QKxJX+lZDsbXuikWY+hZMp/3BZkin/3iq0uir6zyh1GZ33/Bwf8fx1joP6gMUIcncbLZYRRjxa5yhszPKfqt/3J8pZpcI7WqzIT/bkSiHTz+vjOwNw+50qcVoyp9ncze7vv7L0/PPnkOjFfp6+BcLUh7O1DFFB4FnK64wNhBpJY6Q8vgLZ9q/03iW9a+Sp5pRFhq54CFYljag1i3AkXopyrS8eqCUunRlczIRKgm216ui76wUL37/eygeKK7AB+pGqlxOp09Wo3AS98EdKUo/BGe+VQMHM+GRXKLMM106g8U6XV7yR/KwCfiwGvCJdWyfxuTFX4bKGOrbf/XEAkCPwq877OX5iWfQY7TqgSKTTt1XFDKox2j6Fak72Di+K/llIYiAme4xGGHfYYY+SeeAX4lgxo3/6fewG28VtjzdbFu9KYKhLlKkfutAWOBJG61Xr9Y3bx93723lLJQPc8+S4vRPqXkXt6e6sb3lOA5dza5a1Hev3GhIvjCNDcEE4+XG/+pacFF9p0E/RE2Gx5s4JUNTSLz+fCniyt09cELkHx1 Zm1gYEwf +apsVBtVMFv7tdG1TlCmKtddqs55rMWBU/hQYkPDUFw8bfI4DcF2cASH3ftgW/Bs1k7cu9kdUPaMP6uuhmQ9Qqfjh1QSN+PoYShEWr/p7v9FtEWWb20CI4QK9WnnsOrJy++nbsr84bJsGCI3DpvisrM/gwcUhqmTTBwl4lzluI4GzJcC6NhR0PUmknKUcAapR+63HOJc7CfAlg5UKTavqkILlYQRXds15KS537Y1hJJBg4fFmwiCZlfOCBh6hBBlgZfA2hhv66oW4u4BFDr/h0Q+1nkcQPyKPXpvB1QmiZUC8eYBs4xHQEeu6wmC3HVlw87KH2rUz5Wi91fMcuYFBNITAWr6I66WAGqLUWNTbFcpRVcqYPnA4COxHUw== 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: From: Sean Christopherson 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 Message-Id: <20231027182217.3615211-15-seanjc@google.com> Signed-off-by: Paolo Bonzini --- 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 06086dc9da28..60f2ff6b36aa 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 -- 2.39.1