From patchwork Wed Jan 31 17:56:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13539827 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 1826CC47258 for ; Wed, 31 Jan 2024 17:56:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6ACD48D0001; Wed, 31 Jan 2024 12:56:24 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 65B046B0085; Wed, 31 Jan 2024 12:56:24 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 523638D0001; Wed, 31 Jan 2024 12:56:24 -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 436056B0075 for ; Wed, 31 Jan 2024 12:56:24 -0500 (EST) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 186001C0DB4 for ; Wed, 31 Jan 2024 17:56:24 +0000 (UTC) X-FDA: 81740360688.04.090BEC3 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf09.hostedemail.com (Postfix) with ESMTP id 50A4F14001F for ; Wed, 31 Jan 2024 17:56:22 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=4cCXdPrY; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf09.hostedemail.com: domain of 3xYm6ZQYKCPYqspclZemmejc.amkjglsv-kkitYai.mpe@flex--surenb.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3xYm6ZQYKCPYqspclZemmejc.amkjglsv-kkitYai.mpe@flex--surenb.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706723782; 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:in-reply-to: references:dkim-signature; bh=fCByHLn0az29T/ST8c+YfQsbX2Bxnb2yCgdqjn90lwk=; b=2Uba+4YJAIF4xDP3k7g/qilWSiGjER3ritoGt6ejeU6ODKSUXqRTKHZlTw2bQdubXUqPYz wUsHanal5b4Rxl9S0wuxV8zFWcgKq9VNJR+9RhiQGSm8ZVSgw+1U3hp8y97IUpg0Fx8sLW SUNbYPRQa3GsuZu8AS51fBgwxRfeWWY= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=4cCXdPrY; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf09.hostedemail.com: domain of 3xYm6ZQYKCPYqspclZemmejc.amkjglsv-kkitYai.mpe@flex--surenb.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3xYm6ZQYKCPYqspclZemmejc.amkjglsv-kkitYai.mpe@flex--surenb.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706723782; a=rsa-sha256; cv=none; b=0RT3G6Fimz3JpqBGRqfIgsEt/W1gJ2BsegVVHa9TPM9IthfKdLfiw6lnfSycYuJr6IqMc3 yddJv20Tn1zGHjLnwVn4Wi3EoEOvFkP7SfpB5cVS7JAiVsIOG9FKrKSKr99/oIbk01cYSZ u0s3DrFWhEhUlXJU5HHhhql1TPiKIxc= Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-60413725f39so436287b3.2 for ; Wed, 31 Jan 2024 09:56:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1706723781; x=1707328581; darn=kvack.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=fCByHLn0az29T/ST8c+YfQsbX2Bxnb2yCgdqjn90lwk=; b=4cCXdPrYJcLmhuLnO5wKe0x9LPtJxiL5Tz2Tc4B7SVO3UCvEpkSNnDtfmc1Yl3ZIwu 6kuM1Q9kM6F3XdpNZYcpUOscPmUAQpq7cwtzVF5BJouERe3utD0HNHzu1E3iQEDvXRvt euAa/NFXJb4TdjUe/h3Ks+JYMs1+8TApztea5SdGEn2Rj8rRFlHmIsX8/yB6FoOsevXk 4pPl74Vr0PbP0/f9h/p4SNULGhJhjTcQsNP7VF5shn1MxqtCo3GOG5SGkS901fMrwStR t4t5V1NNjcUpX5/ZEMHowxVMTRPA6NPpGv2MgKwWF6nGtJTrfGBbkJMDvToI0XxqKuKk FcUg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706723781; x=1707328581; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=fCByHLn0az29T/ST8c+YfQsbX2Bxnb2yCgdqjn90lwk=; b=oodYZDapp5JvWclxhi+JAWeb+FdfIsvrLYVWyPIa/E1esQxYzOHzLjkpenVAhxZH5L lC2UlgERjWTrCpK8GcxFilYaeEgF8+hnrxGiVK65YLxv5qfD0JNhm7xOzM3eWAVX2kdL zwpHdNHcpWeNY16iWhNGdXtSpLUeHQoFFUvNe7O56l7/yjHhzX8ch5KM3NQGZOv2oJ1J f6E+BQukNY9FJijXR99ArKEY/3Ks9Nc03Cz7s4hpK867S7+oKH8uO0PSzPCvdTmhSmE2 KmskYSxkuUPtql2ae4uo+cngu+654szLEjgIRkGIb9st9Fnt0d7SrxhznvWTa+w72JCj jfUQ== X-Gm-Message-State: AOJu0YzVUjb5CTIjxFbkdfAeucV+a5rgAymt0Fq2Pp8NN2h0qyGATn+F URXIWkZchMvoIdfXymftBDbsmehuXUlxlIBvMi7zRjNs8YA1Aq9VndlzAnKaRIEsZofuUiPJ4gp 9nA== X-Google-Smtp-Source: AGHT+IE1n60RugQOzQULYwOhEaJv2HO9AtQ3jXHu8wpAwOVVAkN13vRAKSD9bgTo4IjB/XxJUscAyJfsWAM= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:1af6:3bbe:455c:bdbd]) (user=surenb job=sendgmr) by 2002:a05:6902:2303:b0:dc2:2d2c:962a with SMTP id do3-20020a056902230300b00dc22d2c962amr631113ybb.8.1706723781432; Wed, 31 Jan 2024 09:56:21 -0800 (PST) Date: Wed, 31 Jan 2024 09:56:18 -0800 Mime-Version: 1.0 X-Mailer: git-send-email 2.43.0.429.g432eaa2c6b-goog Message-ID: <20240131175618.2417291-1-surenb@google.com> Subject: [PATCH v2 1/1] userfaultfd: handle zeropage moves by UFFDIO_MOVE From: Suren Baghdasaryan To: akpm@linux-foundation.org Cc: viro@zeniv.linux.org.uk, brauner@kernel.org, shuah@kernel.org, aarcange@redhat.com, lokeshgidra@google.com, peterx@redhat.com, david@redhat.com, ryan.roberts@arm.com, hughd@google.com, mhocko@suse.com, axelrasmussen@google.com, rppt@kernel.org, willy@infradead.org, Liam.Howlett@oracle.com, jannh@google.com, zhangpeng362@huawei.com, bgeffon@google.com, kaleshsingh@google.com, ngeoffray@google.com, jdduke@google.com, dan.carpenter@linaro.org, surenb@google.com, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, kernel-team@android.com, kernel test robot X-Rspamd-Queue-Id: 50A4F14001F X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: 6hhzha8zi4qmnon66aa3jg978zet3h8k X-HE-Tag: 1706723782-39199 X-HE-Meta: U2FsdGVkX19qX9yM/z//w/GZpUSTX7yPOryKlzwx4mZYBj2p4FwlliyKNpHa/ByoFCQQdCNgLq7hpZIz0iA+ncCQ2p+uTe/fNUgd5d3VVZAVUhV5d87Lpwy1wk14IGrclhJ1zX53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KfSXOteXo6R+4qdAXfek2gobgM4jNthniaBr+v2/hBabhuF9hMGN1JZ5/BAbepYSWKafMkkDU75Iq78EdGbY4kf2IODr/DD+YZvfSkulKaz4Y2U4lrQKRIQB/34IREPVv3+nyXucBYYQNdkGcqB/C4KMJ/hjFI04mmr326SXc+d+YrH8i/xWXrXLD+SipMMaR+4FF4IN1ej1lLqo/zlr9rPaDBumyXxNvKTwim2fTX9TFAOSwuDdS66CrEAgtPFeatcw2K3WmTluvCiRlBklL3Nqg4Yigkl3p0hxKnrZE0P2JyOk3m0cH1S/Ej/V5hj88OcTc4PEayGDGKGx6iOOdRCc8TFsOXScdWPB85a3swufHt9LReoBSsQOZwpMqOibqJvYcd54XXSwzja2mqzTQkTBi8JpUQPQZuQt1YgKydnQrnK0UmKxq6XnHAbRZ+Lvkt4nRrXFsfF+8829j83OKhLiEq24okDTeVxBt9dt1+/5oKn7/EXnhCC8OlFD98btoB/+ulgXaAENwy1yvqTK65KHHHy7wvCRmc3RAS6xXnYOpAjNFhbpMnkvZ9QhmefHx7481WGx 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: Current implementation of UFFDIO_MOVE fails to move zeropages and returns EBUSY when it encounters one. We can handle them by mapping a zeropage at the destination and clearing the mapping at the source. This is done both for ordinary and for huge zeropages. Reported-by: kernel test robot Reported-by: Dan Carpenter Closes: https://lore.kernel.org/r/202401300107.U8iMAkTl-lkp@intel.com/ Signed-off-by: Suren Baghdasaryan --- Changes since v1 [1] - Added missing double_pt_unlock in move_zeropage_pte, per Dan Carpenter - Added Reported-by and Closes tags per bug report [2] Applies cleanly over mm-unstable branch. [1] https://lore.kernel.org/all/20240125001328.335127-1-surenb@google.com/ [2] https://lore.kernel.org/all/202401300107.U8iMAkTl-lkp@intel.com/ mm/huge_memory.c | 105 +++++++++++++++++++++++++++-------------------- mm/userfaultfd.c | 44 ++++++++++++++++---- 2 files changed, 98 insertions(+), 51 deletions(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index f005f0424735..016e20bd813e 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -2200,13 +2200,18 @@ int move_pages_huge_pmd(struct mm_struct *mm, pmd_t *dst_pmd, pmd_t *src_pmd, pm } src_page = pmd_page(src_pmdval); - if (unlikely(!PageAnonExclusive(src_page))) { - spin_unlock(src_ptl); - return -EBUSY; - } - src_folio = page_folio(src_page); - folio_get(src_folio); + if (!is_huge_zero_pmd(src_pmdval)) { + if (unlikely(!PageAnonExclusive(src_page))) { + spin_unlock(src_ptl); + return -EBUSY; + } + + src_folio = page_folio(src_page); + folio_get(src_folio); + } else + src_folio = NULL; + spin_unlock(src_ptl); flush_cache_range(src_vma, src_addr, src_addr + HPAGE_PMD_SIZE); @@ -2214,19 +2219,22 @@ int move_pages_huge_pmd(struct mm_struct *mm, pmd_t *dst_pmd, pmd_t *src_pmd, pm src_addr + HPAGE_PMD_SIZE); mmu_notifier_invalidate_range_start(&range); - folio_lock(src_folio); + if (src_folio) { + folio_lock(src_folio); - /* - * split_huge_page walks the anon_vma chain without the page - * lock. Serialize against it with the anon_vma lock, the page - * lock is not enough. - */ - src_anon_vma = folio_get_anon_vma(src_folio); - if (!src_anon_vma) { - err = -EAGAIN; - goto unlock_folio; - } - anon_vma_lock_write(src_anon_vma); + /* + * split_huge_page walks the anon_vma chain without the page + * lock. Serialize against it with the anon_vma lock, the page + * lock is not enough. + */ + src_anon_vma = folio_get_anon_vma(src_folio); + if (!src_anon_vma) { + err = -EAGAIN; + goto unlock_folio; + } + anon_vma_lock_write(src_anon_vma); + } else + src_anon_vma = NULL; dst_ptl = pmd_lockptr(mm, dst_pmd); double_pt_lock(src_ptl, dst_ptl); @@ -2235,45 +2243,54 @@ int move_pages_huge_pmd(struct mm_struct *mm, pmd_t *dst_pmd, pmd_t *src_pmd, pm err = -EAGAIN; goto unlock_ptls; } - if (folio_maybe_dma_pinned(src_folio) || - !PageAnonExclusive(&src_folio->page)) { - err = -EBUSY; - goto unlock_ptls; - } + if (src_folio) { + if (folio_maybe_dma_pinned(src_folio) || + !PageAnonExclusive(&src_folio->page)) { + err = -EBUSY; + goto unlock_ptls; + } - if (WARN_ON_ONCE(!folio_test_head(src_folio)) || - WARN_ON_ONCE(!folio_test_anon(src_folio))) { - err = -EBUSY; - goto unlock_ptls; - } + if (WARN_ON_ONCE(!folio_test_head(src_folio)) || + WARN_ON_ONCE(!folio_test_anon(src_folio))) { + err = -EBUSY; + goto unlock_ptls; + } - folio_move_anon_rmap(src_folio, dst_vma); - WRITE_ONCE(src_folio->index, linear_page_index(dst_vma, dst_addr)); + folio_move_anon_rmap(src_folio, dst_vma); + WRITE_ONCE(src_folio->index, linear_page_index(dst_vma, dst_addr)); - src_pmdval = pmdp_huge_clear_flush(src_vma, src_addr, src_pmd); - /* Folio got pinned from under us. Put it back and fail the move. */ - if (folio_maybe_dma_pinned(src_folio)) { - set_pmd_at(mm, src_addr, src_pmd, src_pmdval); - err = -EBUSY; - goto unlock_ptls; - } + src_pmdval = pmdp_huge_clear_flush(src_vma, src_addr, src_pmd); + /* Folio got pinned from under us. Put it back and fail the move. */ + if (folio_maybe_dma_pinned(src_folio)) { + set_pmd_at(mm, src_addr, src_pmd, src_pmdval); + err = -EBUSY; + goto unlock_ptls; + } - _dst_pmd = mk_huge_pmd(&src_folio->page, dst_vma->vm_page_prot); - /* Follow mremap() behavior and treat the entry dirty after the move */ - _dst_pmd = pmd_mkwrite(pmd_mkdirty(_dst_pmd), dst_vma); + _dst_pmd = mk_huge_pmd(&src_folio->page, dst_vma->vm_page_prot); + /* Follow mremap() behavior and treat the entry dirty after the move */ + _dst_pmd = pmd_mkwrite(pmd_mkdirty(_dst_pmd), dst_vma); + } else { + src_pmdval = pmdp_huge_clear_flush(src_vma, src_addr, src_pmd); + _dst_pmd = mk_huge_pmd(src_page, dst_vma->vm_page_prot); + } set_pmd_at(mm, dst_addr, dst_pmd, _dst_pmd); src_pgtable = pgtable_trans_huge_withdraw(mm, src_pmd); pgtable_trans_huge_deposit(mm, dst_pmd, src_pgtable); unlock_ptls: double_pt_unlock(src_ptl, dst_ptl); - anon_vma_unlock_write(src_anon_vma); - put_anon_vma(src_anon_vma); + if (src_anon_vma) { + anon_vma_unlock_write(src_anon_vma); + put_anon_vma(src_anon_vma); + } unlock_folio: /* unblock rmap walks */ - folio_unlock(src_folio); + if (src_folio) + folio_unlock(src_folio); mmu_notifier_invalidate_range_end(&range); - folio_put(src_folio); + if (src_folio) + folio_put(src_folio); return err; } #endif /* CONFIG_USERFAULTFD */ diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c index ae80c3714829..9cc93cc1330b 100644 --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -959,6 +959,33 @@ static int move_swap_pte(struct mm_struct *mm, return 0; } +static int move_zeropage_pte(struct mm_struct *mm, + struct vm_area_struct *dst_vma, + struct vm_area_struct *src_vma, + unsigned long dst_addr, unsigned long src_addr, + pte_t *dst_pte, pte_t *src_pte, + pte_t orig_dst_pte, pte_t orig_src_pte, + spinlock_t *dst_ptl, spinlock_t *src_ptl) +{ + pte_t zero_pte; + + double_pt_lock(dst_ptl, src_ptl); + if (!pte_same(ptep_get(src_pte), orig_src_pte) || + !pte_same(ptep_get(dst_pte), orig_dst_pte)) { + double_pt_unlock(dst_ptl, src_ptl); + return -EAGAIN; + } + + zero_pte = pte_mkspecial(pfn_pte(my_zero_pfn(dst_addr), + dst_vma->vm_page_prot)); + ptep_clear_flush(src_vma, src_addr, src_pte); + set_pte_at(mm, dst_addr, dst_pte, zero_pte); + double_pt_unlock(dst_ptl, src_ptl); + + return 0; +} + + /* * The mmap_lock for reading is held by the caller. Just move the page * from src_pmd to dst_pmd if possible, and return true if succeeded @@ -1041,6 +1068,14 @@ static int move_pages_pte(struct mm_struct *mm, pmd_t *dst_pmd, pmd_t *src_pmd, } if (pte_present(orig_src_pte)) { + if (is_zero_pfn(pte_pfn(orig_src_pte))) { + err = move_zeropage_pte(mm, dst_vma, src_vma, + dst_addr, src_addr, dst_pte, src_pte, + orig_dst_pte, orig_src_pte, + dst_ptl, src_ptl); + goto out; + } + /* * Pin and lock both source folio and anon_vma. Since we are in * RCU read section, we can't block, so on contention have to @@ -1404,19 +1439,14 @@ ssize_t move_pages(struct userfaultfd_ctx *ctx, struct mm_struct *mm, err = -ENOENT; break; } - /* Avoid moving zeropages for now */ - if (is_huge_zero_pmd(*src_pmd)) { - spin_unlock(ptl); - err = -EBUSY; - break; - } /* Check if we can move the pmd without splitting it. */ if (move_splits_huge_pmd(dst_addr, src_addr, src_start + len) || !pmd_none(dst_pmdval)) { struct folio *folio = pfn_folio(pmd_pfn(*src_pmd)); - if (!folio || !PageAnonExclusive(&folio->page)) { + if (!folio || (!is_huge_zero_page(&folio->page) && + !PageAnonExclusive(&folio->page))) { spin_unlock(ptl); err = -EBUSY; break;