Received: by 2002:a05:7412:31a9:b0:e2:908c:2ebd with SMTP id et41csp4034776rdb; Thu, 14 Sep 2023 09:47:21 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFaDVY98/5aM4TQmFXs0QwnZac7J18quDXxo77ctx1amZ2eYBAKpNCO+wfSHzIwFl0HXPl3 X-Received: by 2002:a05:6a20:8e0c:b0:13c:bda3:79c3 with SMTP id y12-20020a056a208e0c00b0013cbda379c3mr6612323pzj.4.1694710041506; Thu, 14 Sep 2023 09:47:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694710041; cv=none; d=google.com; s=arc-20160816; b=cmkS2autyCSHn1Bv9/vnLhGc8sjOkXPeWW4LRltCnCsy5Vh0GDIVz4gJwGfO6S1OSd y8esXsOZzU2BPDTCq25KcDeajf7Ft85P9LqSE0J3FSjVmEgDM1e451xyfxbarc0tqitY 4NnWVkuf6EPxbZu6MtXpgyQ4PPEOeycalXP7YlQhe9mIMK9LIKUvELdNRYSjDo3fZqL4 6OGZ8AYYYdTvqNteOOo/2k+UM/Ms/IuDEJQPIMbJiQok2HNrHXvGb/KhPwpWn6oRW5T3 UWEBMEGRgExLhGVff3tfE4dOKYGObQpDoWHV2iEjUXAk11LPsie9rF8784Aiwe/dctb2 YNug== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:reply-to:dkim-signature; bh=IroGtA6z4X06VM4XXyW5Hwunrfyb2UP8Ai0nAjfYz+0=; fh=61hsfVoef5Tbbo+Rm06/Hxsz4fAtyORDF8Po5ZVRZDI=; b=L28BRUGrc5Z6w5w+fXuctNuHwqP4iIQQO1yG+P6Kh5b13kmzZaE0Ioz9uMNoajUnuI 06EtlzbNgf1ogoNTVy7NnFQWx3Auif3DR4D9D62PalpeKErzWEznQGpI+mED/7MNL8I4 PdyA//4txeMSYHOZ38l4HfhOcHy28ckZLdeIImOxwrmgIQLZ6EXNUqMQPOud1USdgJuz MjpC8tChtr4d/Df68/0ec73BhSPfg3PWUqlbRHdKzIzpFso/6hzcnD6X9O1Q+z7OJrD/ Ce8MrjKwNNzg3oY2q2esWvkvaCr5J83iB3zEwIKAxSeJyAE83/VVthmk+559YST7tJZY JaLw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=a8129G7k; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from howler.vger.email (howler.vger.email. [23.128.96.34]) by mx.google.com with ESMTPS id b9-20020a633409000000b0057787e286c4si1838160pga.680.2023.09.14.09.47.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Sep 2023 09:47:21 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) client-ip=23.128.96.34; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=a8129G7k; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id E227382A6767; Wed, 13 Sep 2023 18:58:10 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234367AbjINB5y (ORCPT + 99 others); Wed, 13 Sep 2023 21:57:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60558 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234182AbjINB5F (ORCPT ); Wed, 13 Sep 2023 21:57:05 -0400 Received: from mail-pl1-x649.google.com (mail-pl1-x649.google.com [IPv6:2607:f8b0:4864:20::649]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5BEF91BE3 for ; Wed, 13 Sep 2023 18:56:01 -0700 (PDT) Received: by mail-pl1-x649.google.com with SMTP id d9443c01a7336-1c40ac5b6e7so2355455ad.0 for ; Wed, 13 Sep 2023 18:56:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1694656561; x=1695261361; darn=vger.kernel.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=IroGtA6z4X06VM4XXyW5Hwunrfyb2UP8Ai0nAjfYz+0=; b=a8129G7kgtHcuYcHLYdS3M0dn52BM3vB0X81WiUF9rskEvT7L/oT3aIjqOZ5j+urO6 M/FJTJXNmAJClYCtMTctv6y7p68Ovv72a687eRgGaHgH+OBmZrUh2IacjB4G1CEVRDmX jXEb93sTpARfIDROmq5q4fL0m7NXZpfyiSKXPiJwEGPPz2/pagWOH3/Hg3gPwj263NuO m6in1BGsyij5Ij6IwWuTJdCWu1wyspJg7RELIC81WoGqDopuJj3ymFJknpMXbX3BM1Qy v8mghdbcms0pr/s7GsCZll314BPfWUbA0xF1w+k+NuzhzxoroBqYpyr7QKBj3vKQQpPz YLSA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694656561; x=1695261361; 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=IroGtA6z4X06VM4XXyW5Hwunrfyb2UP8Ai0nAjfYz+0=; b=k1G6CcQEAI3P/6a74s9jqkhwPyH22V8LDA9hTu2bIFf++lC6BTWuIoRww71+TPpgcJ vUKHOAG3a+gehqnd1l+ym36jk+IbNyoEOoOKLM+uYdBeZtQD4ikFpydYdX/Pz9VHK34N fpm2Cxh6QiPWGvCSuHPqZkwQFNMAp4cw2FO/nfZaUgG5QlbzkSE4JGk2W47jml1Fe16A +6SkkDhvypqnWIhGcs0EKiSnGq8dE/6hMC9PEsA42UqZGSA76tq1SiHnU2rygvY4fXnI LW7cd2+RxIoa2X6NSqh+0wFkfSpfJbMPcY0OpNawoI8IpV1Gzob8V7b4IE1YrO2ZsAdN HMxg== X-Gm-Message-State: AOJu0Ywo5Thd6fyqTGWl36O2GdKD1mP10D88Mc0xA1ZzdSs4z3OQl69M wy4P9nhxSrBNp8Lrd1TrVzQG9XPDFug= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:fb83:b0:1c0:ac09:4032 with SMTP id lg3-20020a170902fb8300b001c0ac094032mr144135plb.9.1694656559883; Wed, 13 Sep 2023 18:55:59 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 13 Sep 2023 18:55:10 -0700 In-Reply-To: <20230914015531.1419405-1-seanjc@google.com> Mime-Version: 1.0 References: <20230914015531.1419405-1-seanjc@google.com> X-Mailer: git-send-email 2.42.0.283.g2d96d420d3-goog Message-ID: <20230914015531.1419405-13-seanjc@google.com> Subject: [RFC PATCH v12 12/33] 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 , "Matthew Wilcox (Oracle)" , Andrew Morton , Paul Moore , James Morris , "Serge E. Hallyn" 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-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, Chao Peng , Fuad Tabba , Jarkko Sakkinen , Anish Moorthy , Yu Zhang , Isaku Yamahata , Xu Yilun , Vlastimil Babka , Vishal Annapurve , Ackerley Tng , Maciej Szmigiero , David Hildenbrand , Quentin Perret , Michael Roth , Wang , Liam Merwick , Isaku Yamahata , "Kirill A . Shutemov" Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Wed, 13 Sep 2023 18:58:11 -0700 (PDT) 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 b7fa020003f3..3d25c145098d 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -953,6 +953,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.42.0.283.g2d96d420d3-goog