From patchwork Thu Jan 25 00:13:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13529778 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 83682C46CD2 for ; Thu, 25 Jan 2024 00:13:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BF88F6B006E; Wed, 24 Jan 2024 19:13:34 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id BA7C76B0071; Wed, 24 Jan 2024 19:13:34 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A489E6B0072; Wed, 24 Jan 2024 19:13:34 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 9557F6B006E for ; Wed, 24 Jan 2024 19:13:34 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 0F25C80D80 for ; Thu, 25 Jan 2024 00:13:34 +0000 (UTC) X-FDA: 81715909548.15.05FE5F4 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf01.hostedemail.com (Postfix) with ESMTP id 7CDA540007 for ; Thu, 25 Jan 2024 00:13:32 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=eu2nFldF; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf01.hostedemail.com: domain of 3q6exZQYKCPQoqnajXckkcha.Ykihejqt-iigrWYg.knc@flex--surenb.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3q6exZQYKCPQoqnajXckkcha.Ykihejqt-iigrWYg.knc@flex--surenb.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706141612; 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=cPu242y3p9rQer5EHrWEZnnEQJcAmzowYqkBYJsGGGI=; b=EY3EQsKoDAqjRgrPKIjPFeDig1ZgnEg1mPsUZktxtFZow8ZxlhnibyGouV1+YwWFCxirT8 dJLSz41TGiU49rJY3lG2jkAPfwwnY52jzFzTd9Rc8LUjdE2DFJ12qL+o4SVJa4fDz8qOrw FNP+XBLobnZ0x4+ntphUQTD1UCLgaMk= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=eu2nFldF; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf01.hostedemail.com: domain of 3q6exZQYKCPQoqnajXckkcha.Ykihejqt-iigrWYg.knc@flex--surenb.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3q6exZQYKCPQoqnajXckkcha.Ykihejqt-iigrWYg.knc@flex--surenb.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706141612; a=rsa-sha256; cv=none; b=7AXbBBYfXtOcIlAgfHB4o+4w3qV+gDmUHDMRh3LTffJdD3gTnZcFpFN03rNruRmI3kubGX Ru0k1VBr8x1K30Vs4iRyCI3pOgN4gihPSXcxyXpxqUudsWhTDz+GPTjb9XtXoof1HJ33I3 Sc4VlTO4NmrF0A8A91MU9YpaXdeGBoY= Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6029c8e8758so16828047b3.1 for ; Wed, 24 Jan 2024 16:13:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1706141611; x=1706746411; darn=kvack.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=cPu242y3p9rQer5EHrWEZnnEQJcAmzowYqkBYJsGGGI=; b=eu2nFldFZomzJejfIXmRQQPHKbPmHO9XGJznXsN5OwWkXpEn9fpBuimVds3le+lh7g Oq1RQ/aYIi1Wwp5I9smRPi+0M2DNqooGP1XE6FKnlbeH4U1cbpnTO90T9vTWiuE/jHtr l3D6d2QtrgCmjriI/uuC9swcXUWLbK8Dl/jmCanSI8XL6rnRh4dBVT2yqhWPfXKG9G2d jHIEHi/xdkEhDpHZJQbxY6pa2Jt0T7HWxrjS3E3IBpsBddj2o9eplfysf9nnX4z56v7P xP8CCGyIhN1RZy5ZSFahkW5OgTiPc1k7WBMnmcG+SU69+s1LnpL1eX8EnKKqyzkYRUbd Iw3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706141611; x=1706746411; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=cPu242y3p9rQer5EHrWEZnnEQJcAmzowYqkBYJsGGGI=; b=LMD8EuCx9nYfqfFMDSWrKYG83zRiTaUv6VNHE6xdij3B7XFQMuBxK8zMwKQgKrVvYE /Q1dNvmqFj0kxu0OdSS0eSIOthY6RfSPorJsS+T5pe9W5TsFMvg9pHizZRT5Ztolt2k7 +du3rqsGAp3Pl3V7TrPXGa40b89ARuUG2tQ5w2l3ENetNCgb+TnhMGn3+Xi376QivLo9 hgrL+IsnfZNNEclPrn75PLEybulDAV3TG3lnxWWY95lyEM889oPF1GwbU6iAh5bcnV8D 1IACHTCTLmZArLvwa9NmLwAKsxBSfEN9ZyKuJ+KgI7QZe0lmEviicBUdQbzIajVObwvJ D3mA== X-Gm-Message-State: AOJu0YzmLZdDv5D/7OfEyIS3zg1rRIo2q8FH4jEqAgXRCpyIbKJAaC9F 0X6sersyDKrWHYiqqKD9myynpC58Pjl1QAkz/YHs+s5WVlhSaAnS30Byfg0ibiqUm8U6ZblXmDA Yrg== X-Google-Smtp-Source: AGHT+IEZLtB9otomnw6MNDdoYLF/2uX4yU3la82s4yNMlhvEgVEVg/VGQzuPnFpome6BU13TgA+OWJtInTs= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:ff85:6a8d:d789:76fa]) (user=surenb job=sendgmr) by 2002:a0d:eb89:0:b0:5ff:ea65:3a71 with SMTP id u131-20020a0deb89000000b005ffea653a71mr549405ywe.3.1706141611543; Wed, 24 Jan 2024 16:13:31 -0800 (PST) Date: Wed, 24 Jan 2024 16:13:28 -0800 Mime-Version: 1.0 X-Mailer: git-send-email 2.43.0.429.g432eaa2c6b-goog Message-ID: <20240125001328.335127-1-surenb@google.com> Subject: [PATCH 1/2] 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, 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 X-Rspamd-Queue-Id: 7CDA540007 X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: xab5h3psuj9adizsaiubj19esb6ow1as X-HE-Tag: 1706141612-989948 X-HE-Meta: U2FsdGVkX18LXoqCHX+NNZn9b5/ye16EhZAVWkzw6dUVZGZJl3ZSV0/VUneb4ZR+ISoUw3Tkd75YnGCnpAxxwvKZe/NU1Z4sojZ6PbBO9AIOASw3rgVgMF48IfUnKek1/miAAxT3SFcIybutMx3pXzpiCIyRHM3oXUoAPr+g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O/Xra0ZNbFgduQi4WOyEZSADQiAVbuBrZqxWZJUtbYJnZ8Xq60Ar5ubVVQ79t7/6MOb4FJ3LWJcy+9IwI4lWIS1GmboXMamIeJGZazJ3P7IUO+wSgqjU1fj2xoXVAF28seAatw+ldVcp+rfqnAV8= 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. Signed-off-by: Suren Baghdasaryan --- Applies cleanly over mm-unstable branch. mm/huge_memory.c | 105 +++++++++++++++++++++++++++-------------------- mm/userfaultfd.c | 42 +++++++++++++++---- 2 files changed, 96 insertions(+), 51 deletions(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index f40feb31b507..5dcc02c25e97 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -2190,13 +2190,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); @@ -2204,19 +2209,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); @@ -2225,45 +2233,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 3548b3e31a97..5fbf4da15c5c 100644 --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -959,6 +959,31 @@ 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)) + 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 +1066,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 +1437,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;