From patchwork Thu Feb 17 06:07:43 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 12749419 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 7C869C433F5 for ; Thu, 17 Feb 2022 06:08:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E76866B0075; Thu, 17 Feb 2022 01:08:04 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id DFC536B0078; Thu, 17 Feb 2022 01:08:04 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C505D6B007B; Thu, 17 Feb 2022 01:08:04 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0191.hostedemail.com [216.40.44.191]) by kanga.kvack.org (Postfix) with ESMTP id B2E026B0075 for ; Thu, 17 Feb 2022 01:08:04 -0500 (EST) Received: from smtpin25.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 5FC6293D95 for ; Thu, 17 Feb 2022 06:08:04 +0000 (UTC) X-FDA: 79151241288.25.A83C8FF Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf20.hostedemail.com (Postfix) with ESMTP id CF4DC1C0002 for ; Thu, 17 Feb 2022 06:08:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1645078083; 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=ZSU30vCJtk7GotZgltWgn1HG21KfRp7j8JvnndBOhWw=; b=aae/IZX3xJWhqhMDwyYfYLsazld5ideSAqvBOew0caLV7bjp/PhQGvoEiAeL6E6xI3r8xX nwnBYHp4kIPZr7jUPsHusvYaQzPp/FC1fJ9GBfc670mV5zD6LTOWNK7I2KDEXD8gYWLWGT sqVbrR8JM3JBLE0G8G9HshpHXw9KW7I= Received: from mail-pj1-f69.google.com (mail-pj1-f69.google.com [209.85.216.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-16-I2vvCoKcMrqKPHpxyYII4w-1; Thu, 17 Feb 2022 01:08:02 -0500 X-MC-Unique: I2vvCoKcMrqKPHpxyYII4w-1 Received: by mail-pj1-f69.google.com with SMTP id jf17-20020a17090b175100b001b90cf26a4eso2804792pjb.3 for ; Wed, 16 Feb 2022 22:08:01 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ZSU30vCJtk7GotZgltWgn1HG21KfRp7j8JvnndBOhWw=; b=UzoadJLZCeAmrD1aIJwu79kY88Q4GzH8v8tCXpTBGBiGpYjjV0BB9tUIStji+BOAwz 8Eg3ZKvFhxYKvYirn9TrD6CkjfP2koNj6QvA4jffEWqlNdFYFB5BCLG2FJe90JPyT0hr wGs96jwLjCbroWhY0ALT9oToi61VnRrt9e9GinbN+6f2PklxwciPjJ8WPipos7NtfoGB up/lxVLDdY556qqAI3sjhFy9fxodpr1niEYhY43/LhznOx4LJYudCT785vT6P2/1pw3L 5j43k/bdU/XqrrmLnhO1FJWrQJ4IPzO//KvC+5X5OuQk85q7F630AcZOWf+CYSFnQ8Ut Tabg== X-Gm-Message-State: AOAM532VfTX8KZA9yIl5TT884eXKurdXIFAhwZB/t5Vepbx2hMvIT77p nAyy4J4RvLLY93in2L3jhYzEfetGtSoQ/scjOAV37idSXv3pYY56i4M6uv0dpKiPG63n4Zqu53l 5nprAp6iHxHMEJ9tGpwMCYDH3cpua3YQenfzHnX7MscqdAJ7zDCr+E310Ae0C X-Received: by 2002:a17:902:da91:b0:14d:58de:5667 with SMTP id j17-20020a170902da9100b0014d58de5667mr1392103plx.95.1645078080733; Wed, 16 Feb 2022 22:08:00 -0800 (PST) X-Google-Smtp-Source: ABdhPJyDUnDyEDIU63kvmjH9FX83zNUGtXLAelwg+LX3uRrYzwGaMsuEvfOVDWTrPD9itq0ndmmSlQ== X-Received: by 2002:a17:902:da91:b0:14d:58de:5667 with SMTP id j17-20020a170902da9100b0014d58de5667mr1392070plx.95.1645078080296; Wed, 16 Feb 2022 22:08:00 -0800 (PST) Received: from localhost.localdomain ([94.177.118.126]) by smtp.gmail.com with ESMTPSA id j8sm224230pjc.11.2022.02.16.22.07.55 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Wed, 16 Feb 2022 22:08:00 -0800 (PST) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Alistair Popple , Matthew Wilcox , peterx@redhat.com, David Hildenbrand , Andrea Arcangeli , Hugh Dickins , Yang Shi , Vlastimil Babka , John Hubbard , Andrew Morton , "Kirill A . Shutemov" Subject: [PATCH v5 1/4] mm: Don't skip swap entry even if zap_details specified Date: Thu, 17 Feb 2022 14:07:43 +0800 Message-Id: <20220217060746.71256-2-peterx@redhat.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220217060746.71256-1-peterx@redhat.com> References: <20220217060746.71256-1-peterx@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Rspam-User: X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: CF4DC1C0002 X-Stat-Signature: byj4bgjymzehwzufh9c9qraqcft8mpoe Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="aae/IZX3"; spf=none (imf20.hostedemail.com: domain of peterx@redhat.com has no SPF policy when checking 170.10.133.124) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-HE-Tag: 1645078083-475589 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: The "details" pointer shouldn't be the token to decide whether we should skip swap entries. For example, when the callers specified details->zap_mapping==NULL, it means the user wants to zap all the pages (including COWed pages), then we need to look into swap entries because there can be private COWed pages that was swapped out. Skipping some swap entries when details is non-NULL may lead to wrongly leaving some of the swap entries while we should have zapped them. A reproducer of the problem: ===8<=== #define _GNU_SOURCE /* See feature_test_macros(7) */ #include #include #include #include #include int page_size; int shmem_fd; char *buffer; void main(void) { int ret; char val; page_size = getpagesize(); shmem_fd = memfd_create("test", 0); assert(shmem_fd >= 0); ret = ftruncate(shmem_fd, page_size * 2); assert(ret == 0); buffer = mmap(NULL, page_size * 2, PROT_READ | PROT_WRITE, MAP_PRIVATE, shmem_fd, 0); assert(buffer != MAP_FAILED); /* Write private page, swap it out */ buffer[page_size] = 1; madvise(buffer, page_size * 2, MADV_PAGEOUT); /* This should drop private buffer[page_size] already */ ret = ftruncate(shmem_fd, page_size); assert(ret == 0); /* Recover the size */ ret = ftruncate(shmem_fd, page_size * 2); assert(ret == 0); /* Re-read the data, it should be all zero */ val = buffer[page_size]; if (val == 0) printf("Good\n"); else printf("BUG\n"); } ===8<=== We don't need to touch up the pmd path, because pmd never had a issue with swap entries. For example, shmem pmd migration will always be split into pte level, and same to swapping on anonymous. Add another helper should_zap_cows() so that we can also check whether we should zap private mappings when there's no page pointer specified. This patch drops that trick, so we handle swap ptes coherently. Meanwhile we should do the same check upon migration entry, hwpoison entry and genuine swap entries too. To be explicit, we should still remember to keep the private entries if even_cows==false, and always zap them when even_cows==true. The issue seems to exist starting from the initial commit of git. Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Reviewed-by: John Hubbard Signed-off-by: Peter Xu --- mm/memory.c | 40 +++++++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/mm/memory.c b/mm/memory.c index c125c4969913..533da5d6c32c 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -1313,6 +1313,17 @@ struct zap_details { struct folio *single_folio; /* Locked folio to be unmapped */ }; +/* Whether we should zap all COWed (private) pages too */ +static inline bool should_zap_cows(struct zap_details *details) +{ + /* By default, zap all pages */ + if (!details) + return true; + + /* Or, we zap COWed pages only if the caller wants to */ + return !details->zap_mapping; +} + /* * We set details->zap_mapping when we want to unmap shared but keep private * pages. Return true if skip zapping this page, false otherwise. @@ -1320,11 +1331,15 @@ struct zap_details { static inline bool zap_skip_check_mapping(struct zap_details *details, struct page *page) { - if (!details || !page) + /* If we can make a decision without *page.. */ + if (should_zap_cows(details)) + return false; + + /* E.g. the caller passes NULL for the case of a zero page */ + if (!page) return false; - return details->zap_mapping && - (details->zap_mapping != page_rmapping(page)); + return details->zap_mapping != page_rmapping(page); } static unsigned long zap_pte_range(struct mmu_gather *tlb, @@ -1405,17 +1420,24 @@ static unsigned long zap_pte_range(struct mmu_gather *tlb, continue; } - /* If details->check_mapping, we leave swap entries. */ - if (unlikely(details)) - continue; - - if (!non_swap_entry(entry)) + if (!non_swap_entry(entry)) { + /* Genuine swap entry, hence a private anon page */ + if (!should_zap_cows(details)) + continue; rss[MM_SWAPENTS]--; - else if (is_migration_entry(entry)) { + } else if (is_migration_entry(entry)) { struct page *page; page = pfn_swap_entry_to_page(entry); + if (zap_skip_check_mapping(details, page)) + continue; rss[mm_counter(page)]--; + } else if (is_hwpoison_entry(entry)) { + if (!should_zap_cows(details)) + continue; + } else { + /* We should have covered all the swap entry types */ + WARN_ON_ONCE(1); } if (unlikely(!free_swap_and_cache(entry))) print_bad_pte(vma, addr, ptent, NULL); From patchwork Thu Feb 17 06:07:44 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 12749420 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 B1DF8C433EF for ; Thu, 17 Feb 2022 06:08:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 43AFC6B0078; Thu, 17 Feb 2022 01:08:10 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3C2BC6B007B; Thu, 17 Feb 2022 01:08:10 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 215926B007D; Thu, 17 Feb 2022 01:08:10 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0091.hostedemail.com [216.40.44.91]) by kanga.kvack.org (Postfix) with ESMTP id 11EA76B0078 for ; Thu, 17 Feb 2022 01:08:10 -0500 (EST) Received: from smtpin12.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id BB38395B09 for ; Thu, 17 Feb 2022 06:08:09 +0000 (UTC) X-FDA: 79151241498.12.E08143B Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf14.hostedemail.com (Postfix) with ESMTP id 1F212100002 for ; Thu, 17 Feb 2022 06:08:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1645078088; 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=GodqcRh1SHgqGa/hH7oS86cAqo3cWdJIC+9/AWlAQQI=; b=M31IJBEiTEwTCWXMfMLqMQn37Gi1NczmGszeqlvp9ymb5FzvDjxzyf6uMFjOg5pX3cR8nQ l+Fe3yu4nZsBnxowLCdHexyG7GpYkDESPxNZ42DSwWt5/dC4fFD0ZqEQ3pJSFzi5f5bxgA k38tX7jxhdmgB1lqJWLEoFGbAln28m8= Received: from mail-pj1-f72.google.com (mail-pj1-f72.google.com [209.85.216.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-311-kDpHxoVSP5i6kmFurXc6UA-1; Thu, 17 Feb 2022 01:08:07 -0500 X-MC-Unique: kDpHxoVSP5i6kmFurXc6UA-1 Received: by mail-pj1-f72.google.com with SMTP id s10-20020a17090a948a00b001b96be201f6so2799647pjo.4 for ; Wed, 16 Feb 2022 22:08:07 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=GodqcRh1SHgqGa/hH7oS86cAqo3cWdJIC+9/AWlAQQI=; b=LBOo+IvltM+/NAtkMzOQKyq5hswdVBAiwHKiExf++xtyQfZJw/vX0Yar9U/btDDUEC k+xZ9embEUY5/CIVaVikZAqnwLEcIOWn7VLa3GNkwqxQpkB6ABBfncPGk5XUjfTPipEo VfNuNWROEFmhnqAWvGBMkZGUFGlrr4wfaGOHt9Q8w2pa/N0yBEPy0IrDJJw152DP63iW JVClzXcZ11YoPHrc5s53Qml/1YBiJv22NLcqpg+rgQvwoQNvpIzKljRKgIh+W9ngtbze QbSnhMPfgeugOHiYuuuVXH6trHMdbPLo2eW4H8vdov2fGxjkJ57opzZlABNKbypHdcX0 TXyQ== X-Gm-Message-State: AOAM531ruVNMUQF2sVfV5OQXRHXRrSociTDeOTICXJ4fdPfo3LTGuOJI Iat4QDjMFK2OEBod8MNCY1Iq6T6brUPaLLAj5ty6VG3/CJq/SVpZ9ieHR01XO8xZuzF6ZIkZwtV JPuBRJnMMdV4DA1Yg6ydv9K5tKmiQNu1gLR1ZL+p+T23AWUrT0q7Jhyd5/i9Y X-Received: by 2002:a17:902:be0a:b0:14d:5db0:7a14 with SMTP id r10-20020a170902be0a00b0014d5db07a14mr1368624pls.155.1645078085980; Wed, 16 Feb 2022 22:08:05 -0800 (PST) X-Google-Smtp-Source: ABdhPJwxmgTyql91syHq0iBmulNuYN38si4IUsKVfqHWUowF2A+HcKnj8DOPPRsO38J/B724ZyqK7Q== X-Received: by 2002:a17:902:be0a:b0:14d:5db0:7a14 with SMTP id r10-20020a170902be0a00b0014d5db07a14mr1368590pls.155.1645078085600; Wed, 16 Feb 2022 22:08:05 -0800 (PST) Received: from localhost.localdomain ([94.177.118.126]) by smtp.gmail.com with ESMTPSA id j8sm224230pjc.11.2022.02.16.22.08.00 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Wed, 16 Feb 2022 22:08:05 -0800 (PST) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Alistair Popple , Matthew Wilcox , peterx@redhat.com, David Hildenbrand , Andrea Arcangeli , Hugh Dickins , Yang Shi , Vlastimil Babka , John Hubbard , Andrew Morton , "Kirill A . Shutemov" Subject: [PATCH v5 2/4] mm: Rename zap_skip_check_mapping() to should_zap_page() Date: Thu, 17 Feb 2022 14:07:44 +0800 Message-Id: <20220217060746.71256-3-peterx@redhat.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220217060746.71256-1-peterx@redhat.com> References: <20220217060746.71256-1-peterx@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 1F212100002 X-Stat-Signature: uonydn4rk8yrfbyj7x4sq4fa9dfez9de Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=M31IJBEi; dmarc=pass (policy=none) header.from=redhat.com; spf=none (imf14.hostedemail.com: domain of peterx@redhat.com has no SPF policy when checking 170.10.129.124) smtp.mailfrom=peterx@redhat.com X-HE-Tag: 1645078088-559787 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: The previous name is against the natural way people think. Invert the meaning and also the return value. No functional change intended. Suggested-by: David Hildenbrand Suggested-by: Hugh Dickins Reviewed-by: David Hildenbrand Reviewed-by: John Hubbard Signed-off-by: Peter Xu --- mm/memory.c | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/mm/memory.c b/mm/memory.c index 533da5d6c32c..3728632ea993 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -1326,20 +1326,19 @@ static inline bool should_zap_cows(struct zap_details *details) /* * We set details->zap_mapping when we want to unmap shared but keep private - * pages. Return true if skip zapping this page, false otherwise. + * pages. Return true if we should zap this page, false otherwise. */ -static inline bool -zap_skip_check_mapping(struct zap_details *details, struct page *page) +static inline bool should_zap_page(struct zap_details *details, struct page *page) { /* If we can make a decision without *page.. */ if (should_zap_cows(details)) - return false; + return true; /* E.g. the caller passes NULL for the case of a zero page */ if (!page) - return false; + return true; - return details->zap_mapping != page_rmapping(page); + return details->zap_mapping == page_rmapping(page); } static unsigned long zap_pte_range(struct mmu_gather *tlb, @@ -1374,7 +1373,7 @@ static unsigned long zap_pte_range(struct mmu_gather *tlb, struct page *page; page = vm_normal_page(vma, addr, ptent); - if (unlikely(zap_skip_check_mapping(details, page))) + if (unlikely(!should_zap_page(details, page))) continue; ptent = ptep_get_and_clear_full(mm, addr, pte, tlb->fullmm); @@ -1408,7 +1407,7 @@ static unsigned long zap_pte_range(struct mmu_gather *tlb, is_device_exclusive_entry(entry)) { struct page *page = pfn_swap_entry_to_page(entry); - if (unlikely(zap_skip_check_mapping(details, page))) + if (unlikely(!should_zap_page(details, page))) continue; pte_clear_not_present_full(mm, addr, pte, tlb->fullmm); rss[mm_counter(page)]--; @@ -1429,7 +1428,7 @@ static unsigned long zap_pte_range(struct mmu_gather *tlb, struct page *page; page = pfn_swap_entry_to_page(entry); - if (zap_skip_check_mapping(details, page)) + if (!should_zap_page(details, page)) continue; rss[mm_counter(page)]--; } else if (is_hwpoison_entry(entry)) { From patchwork Thu Feb 17 06:07:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 12749421 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 67895C433F5 for ; Thu, 17 Feb 2022 06:08:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F0FEB6B007B; Thu, 17 Feb 2022 01:08:14 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E985A6B007D; Thu, 17 Feb 2022 01:08:14 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D12E96B007E; Thu, 17 Feb 2022 01:08:14 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0222.hostedemail.com [216.40.44.222]) by kanga.kvack.org (Postfix) with ESMTP id C09046B007B for ; Thu, 17 Feb 2022 01:08:14 -0500 (EST) Received: from smtpin12.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 83409181AC9C6 for ; Thu, 17 Feb 2022 06:08:14 +0000 (UTC) X-FDA: 79151241708.12.BE42C86 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 0608F140003 for ; Thu, 17 Feb 2022 06:08:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1645078093; 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=A1vbZxsyzMifaaJqgkhHlizU7SHJnrILT5bLrmNnzMQ=; b=UF3otFFdlgED7oMXBGZHUacxQsvcyU15vFgTclZxvD8KhqpovrLqxowtUkt3LjzOvtcTMe WaA+oE035x+VA79LDH5E02m8Pkf7h2UwAzpnY1hqsvjf1t9OIpnJpvccqmS5E1y0iOGz2V Lj/jomIXCARDsErcL02s3/aQGIoPj0E= Received: from mail-pj1-f72.google.com (mail-pj1-f72.google.com [209.85.216.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-448-hB8rWLKKPlum9-3xlzb0RA-1; Thu, 17 Feb 2022 01:08:12 -0500 X-MC-Unique: hB8rWLKKPlum9-3xlzb0RA-1 Received: by mail-pj1-f72.google.com with SMTP id c6-20020a17090a020600b001b9b44c18aaso5840959pjc.4 for ; Wed, 16 Feb 2022 22:08:12 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=A1vbZxsyzMifaaJqgkhHlizU7SHJnrILT5bLrmNnzMQ=; b=pbbGxiv+w7Edae5lzWApha/SHC2wcEnIfBdozWmGjY5cceq/fiVfpWpLHwgQqV6oWo s6QWnhJgBloCxwOkXgsKcy1Ss39Ph4Sx27yEiDDiuE/QR5Mdikh+ncNIxetUc5VVR/pb wJsWz15eRl+249u7p36fghudS63n1PMdFX+rS4v4MkyjuEoY3QnKINQGn3jsIkyJ0+yc EtyviMYnppV/uX/7AUFJmw3PQKyhDO3+Sm8rFRJrN5DqFFvyrVQy/bc1dRVjIfwZ4nVM qsALbe5hzs0RZHMUKyzo2O1gYaX3NeR5Tv+4rXVbwjelSEy5sj90BIy0RyzIgPuqTFld jnvg== X-Gm-Message-State: AOAM531VvNKEWTDk9p3TEiU0+zXX4WzlM5hygqg8UR8sAvz95qvSzcI5 h+opV5V6EPoKSM6WtLAdgv81vYFb0oKmFRsLHgBv/0/zyJPp9dCdOCnCddhV/hS/qUOWMeDShsx Rmh9ksKyLbl7fRtfR7NPlOhkFBn+cmP91oUWWJ18hqcQK65MpyFZbEWK9nn2z X-Received: by 2002:a17:90a:ec10:b0:1b8:9da7:3d13 with SMTP id l16-20020a17090aec1000b001b89da73d13mr1531885pjy.194.1645078091273; Wed, 16 Feb 2022 22:08:11 -0800 (PST) X-Google-Smtp-Source: ABdhPJxJ+MRoSejjpjSfq3CLN/Ow3BCwyndgoRTeQRVbvX65z98rR9y1roji3xMcenHn/prKAwWNxg== X-Received: by 2002:a17:90a:ec10:b0:1b8:9da7:3d13 with SMTP id l16-20020a17090aec1000b001b89da73d13mr1531852pjy.194.1645078090846; Wed, 16 Feb 2022 22:08:10 -0800 (PST) Received: from localhost.localdomain ([94.177.118.126]) by smtp.gmail.com with ESMTPSA id j8sm224230pjc.11.2022.02.16.22.08.06 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Wed, 16 Feb 2022 22:08:10 -0800 (PST) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Alistair Popple , Matthew Wilcox , peterx@redhat.com, David Hildenbrand , Andrea Arcangeli , Hugh Dickins , Yang Shi , Vlastimil Babka , John Hubbard , Andrew Morton , "Kirill A . Shutemov" Subject: [PATCH v5 3/4] mm: Change zap_details.zap_mapping into even_cows Date: Thu, 17 Feb 2022 14:07:45 +0800 Message-Id: <20220217060746.71256-4-peterx@redhat.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220217060746.71256-1-peterx@redhat.com> References: <20220217060746.71256-1-peterx@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 0608F140003 X-Rspam-User: Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=UF3otFFd; spf=none (imf26.hostedemail.com: domain of peterx@redhat.com has no SPF policy when checking 170.10.133.124) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-Stat-Signature: wzatub9u8bedu6i3rpxffrgu3e39eyzz X-HE-Tag: 1645078093-658189 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: Currently we have a zap_mapping pointer maintained in zap_details, when it is specified we only want to zap the pages that has the same mapping with what the caller has specified. But what we want to do is actually simpler: we want to skip zapping private (COW-ed) pages in some cases. It comes from unmap_mapping_pages() where we could have passed in different even_cows values. The other user is unmap_mapping_folio() where we always want to skip private pages. According to Hugh, we used a mapping pointer for historical reason, as explained here: https://lore.kernel.org/lkml/391aa58d-ce84-9d4-d68d-d98a9c533255@google.com/ Quoting partly from Hugh: Which raises the question again of why I did not just use a boolean flag there originally: aah, I think I've found why. In those days there was a horrible "optimization", for better performance on some benchmark I guess, which when you read from /dev/zero into a private mapping, would map the zero page there (look up read_zero_pagealigned() and zeromap_page_range() if you dare). So there was another category of page to be skipped along with the anon COWs, and I didn't want multiple tests in the zap loop, so checking check_mapping against page->mapping did both. I think nowadays you could do it by checking for PageAnon page (or genuine swap entry) instead. This patch replaced the zap_details.zap_mapping pointer into the even_cows boolean, then we check it against PageAnon. Suggested-by: Hugh Dickins Reviewed-by: John Hubbard Signed-off-by: Peter Xu --- mm/memory.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/mm/memory.c b/mm/memory.c index 3728632ea993..c2defe8a1472 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -1309,8 +1309,8 @@ copy_page_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma) * Parameter block passed down to zap_pte_range in exceptional cases. */ struct zap_details { - struct address_space *zap_mapping; /* Check page->mapping if set */ struct folio *single_folio; /* Locked folio to be unmapped */ + bool even_cows; /* Zap COWed private pages too? */ }; /* Whether we should zap all COWed (private) pages too */ @@ -1321,13 +1321,10 @@ static inline bool should_zap_cows(struct zap_details *details) return true; /* Or, we zap COWed pages only if the caller wants to */ - return !details->zap_mapping; + return details->even_cows; } -/* - * We set details->zap_mapping when we want to unmap shared but keep private - * pages. Return true if we should zap this page, false otherwise. - */ +/* Decides whether we should zap this page with the page pointer specified */ static inline bool should_zap_page(struct zap_details *details, struct page *page) { /* If we can make a decision without *page.. */ @@ -1338,7 +1335,8 @@ static inline bool should_zap_page(struct zap_details *details, struct page *pag if (!page) return true; - return details->zap_mapping == page_rmapping(page); + /* Otherwise we should only zap non-anon pages */ + return !PageAnon(page); } static unsigned long zap_pte_range(struct mmu_gather *tlb, @@ -3398,7 +3396,7 @@ void unmap_mapping_folio(struct folio *folio) first_index = folio->index; last_index = folio->index + folio_nr_pages(folio) - 1; - details.zap_mapping = mapping; + details.even_cows = false; details.single_folio = folio; i_mmap_lock_write(mapping); @@ -3427,7 +3425,7 @@ void unmap_mapping_pages(struct address_space *mapping, pgoff_t start, pgoff_t first_index = start; pgoff_t last_index = start + nr - 1; - details.zap_mapping = even_cows ? NULL : mapping; + details.even_cows = even_cows; if (last_index < first_index) last_index = ULONG_MAX; From patchwork Thu Feb 17 06:07:46 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 12749422 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 82FFCC433F5 for ; Thu, 17 Feb 2022 06:08:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0448B6B007D; Thu, 17 Feb 2022 01:08:20 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id EE6066B007E; Thu, 17 Feb 2022 01:08:19 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D604C6B0080; Thu, 17 Feb 2022 01:08:19 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0167.hostedemail.com [216.40.44.167]) by kanga.kvack.org (Postfix) with ESMTP id C68C46B007D for ; Thu, 17 Feb 2022 01:08:19 -0500 (EST) Received: from smtpin16.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 8146818108638 for ; Thu, 17 Feb 2022 06:08:19 +0000 (UTC) X-FDA: 79151241918.16.092488C Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf29.hostedemail.com (Postfix) with ESMTP id EE24A120008 for ; Thu, 17 Feb 2022 06:08:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1645078098; 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=7Iw+/fHKv6eLf6uybneXrBCZa8a9WBXNMPZFyFhXGnU=; b=hnrFThXaZ7tiyraKlfvOsSbYHvOzHgd1gxr59YfaEZBoJgVYY30BH4tq/R8DDYSZs5UuBI bCik27U9zxTbbFO0vmI093rhBg4GxHEo/h57uORDt+vEJRkxCrX46uf3aJVwS38d3zDOcD ULw70c/b4p7/OPn0auukQQ2xMIZw+No= Received: from mail-pj1-f69.google.com (mail-pj1-f69.google.com [209.85.216.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-84-fZ9mjwQAMIeBywyOFirOVQ-1; Thu, 17 Feb 2022 01:08:17 -0500 X-MC-Unique: fZ9mjwQAMIeBywyOFirOVQ-1 Received: by mail-pj1-f69.google.com with SMTP id fa22-20020a17090af0d600b001bb93310b41so1648622pjb.3 for ; Wed, 16 Feb 2022 22:08:17 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=7Iw+/fHKv6eLf6uybneXrBCZa8a9WBXNMPZFyFhXGnU=; b=5hY8RaFOB5onTKTfabWhb+ugy4w+VyqCVV6948e3eXYx6429ett6djy5M4jlfYcwvT bf2oQxTxqQoG+bbregyl9dnx7wJ8RdA83LhTvYZA9TJl7mfOJUE+ipvJv5nECwInm1tC uht6cLJYHKFHI2RzOvpDjzls5pkPL0zTaLulmsJ2QdOn4e1vKsbY6i9rtpJ6NML82V9g mS9FAjXKtK8sKl2WawChzphIRfRzQMCAavHwXlgAnLvwX0oWXHK6kT4YkHyTViIuGcmk +83l0smwt0A1aKSvctIG6BIYc3SLMbgliiuwpqI/SRYAw6i0X2CWOtEPJ59/IDwybiTP 0tYA== X-Gm-Message-State: AOAM532o0xQFIEzK+4295DC4lhroR4RXZouBpbuYFLnmbv0ePtPI9HVg dUq7uCTwZstEB08Jp5xZ+Si9jVJNSnMBKEFc0NLKhIilNGQtD8szk9rb+CearEUWTJzuUYWXlF1 9WicTpsQ+51BbiUGKGj+4yGhw2OtrtDvx8wmxOIbqU9obat8hHCTDQMqpzqqO X-Received: by 2002:a05:6a00:8c5:b0:4c7:f9a5:ebc6 with SMTP id s5-20020a056a0008c500b004c7f9a5ebc6mr1690190pfu.34.1645078096197; Wed, 16 Feb 2022 22:08:16 -0800 (PST) X-Google-Smtp-Source: ABdhPJzJq01ZMuxcXHBVvWHdhve6h2WPzO7RXn4GgLcncsq4yBWdeLBav4ZSIjD78cJP8QrYAxhIEQ== X-Received: by 2002:a05:6a00:8c5:b0:4c7:f9a5:ebc6 with SMTP id s5-20020a056a0008c500b004c7f9a5ebc6mr1690157pfu.34.1645078095837; Wed, 16 Feb 2022 22:08:15 -0800 (PST) Received: from localhost.localdomain ([94.177.118.126]) by smtp.gmail.com with ESMTPSA id j8sm224230pjc.11.2022.02.16.22.08.11 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Wed, 16 Feb 2022 22:08:15 -0800 (PST) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Alistair Popple , Matthew Wilcox , peterx@redhat.com, David Hildenbrand , Andrea Arcangeli , Hugh Dickins , Yang Shi , Vlastimil Babka , John Hubbard , Andrew Morton , "Kirill A . Shutemov" Subject: [PATCH v5 4/4] mm: Rework swap handling of zap_pte_range Date: Thu, 17 Feb 2022 14:07:46 +0800 Message-Id: <20220217060746.71256-5-peterx@redhat.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220217060746.71256-1-peterx@redhat.com> References: <20220217060746.71256-1-peterx@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Rspamd-Queue-Id: EE24A120008 X-Rspam-User: Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=hnrFThXa; spf=none (imf29.hostedemail.com: domain of peterx@redhat.com has no SPF policy when checking 170.10.133.124) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-Stat-Signature: kzoh8rgqbirdg5ugi7heutgwd35mxgrb X-Rspamd-Server: rspam03 X-HE-Tag: 1645078098-688752 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: Clean the code up by merging the device private/exclusive swap entry handling with the rest, then we merge the pte clear operation too. struct* page is defined in multiple places in the function, move it upward. free_swap_and_cache() is only useful for !non_swap_entry() case, put it into the condition. No functional change intended. Reviewed-by: John Hubbard Signed-off-by: Peter Xu --- mm/memory.c | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/mm/memory.c b/mm/memory.c index c2defe8a1472..6aa51ee2a307 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -1361,6 +1361,8 @@ static unsigned long zap_pte_range(struct mmu_gather *tlb, arch_enter_lazy_mmu_mode(); do { pte_t ptent = *pte; + struct page *page; + if (pte_none(ptent)) continue; @@ -1368,8 +1370,6 @@ static unsigned long zap_pte_range(struct mmu_gather *tlb, break; if (pte_present(ptent)) { - struct page *page; - page = vm_normal_page(vma, addr, ptent); if (unlikely(!should_zap_page(details, page))) continue; @@ -1403,28 +1403,21 @@ static unsigned long zap_pte_range(struct mmu_gather *tlb, entry = pte_to_swp_entry(ptent); if (is_device_private_entry(entry) || is_device_exclusive_entry(entry)) { - struct page *page = pfn_swap_entry_to_page(entry); - + page = pfn_swap_entry_to_page(entry); if (unlikely(!should_zap_page(details, page))) continue; - pte_clear_not_present_full(mm, addr, pte, tlb->fullmm); rss[mm_counter(page)]--; - if (is_device_private_entry(entry)) page_remove_rmap(page, false); - put_page(page); - continue; - } - - if (!non_swap_entry(entry)) { + } else if (!non_swap_entry(entry)) { /* Genuine swap entry, hence a private anon page */ if (!should_zap_cows(details)) continue; rss[MM_SWAPENTS]--; + if (unlikely(!free_swap_and_cache(entry))) + print_bad_pte(vma, addr, ptent, NULL); } else if (is_migration_entry(entry)) { - struct page *page; - page = pfn_swap_entry_to_page(entry); if (!should_zap_page(details, page)) continue; @@ -1436,8 +1429,6 @@ static unsigned long zap_pte_range(struct mmu_gather *tlb, /* We should have covered all the swap entry types */ WARN_ON_ONCE(1); } - if (unlikely(!free_swap_and_cache(entry))) - print_bad_pte(vma, addr, ptent, NULL); pte_clear_not_present_full(mm, addr, pte, tlb->fullmm); } while (pte++, addr += PAGE_SIZE, addr != end);