All of lore.kernel.org
 help / color / mirror / Atom feed
From: Axel Rasmussen <axelrasmussen@google.com>
To: Alexander Viro <viro@zeniv.linux.org.uk>,
	Andrew Morton <akpm@linux-foundation.org>,
	Christian Brauner <brauner@kernel.org>,
	David Hildenbrand <david@redhat.com>,
	Huang Ying <ying.huang@intel.com>,
	Hugh Dickins <hughd@google.com>,
	James Houghton <jthoughton@google.com>,
	Jiaqi Yan <jiaqiyan@google.com>, Jonathan Corbet <corbet@lwn.net>,
	"Liam R. Howlett" <Liam.Howlett@oracle.com>,
	Miaohe Lin <linmiaohe@huawei.com>,
	Mike Kravetz <mike.kravetz@oracle.com>,
	"Mike Rapoport (IBM)" <rppt@kernel.org>,
	Muchun Song <muchun.song@linux.dev>,
	Nadav Amit <namit@vmware.com>,
	Naoya Horiguchi <naoya.horiguchi@nec.com>,
	Peter Xu <peterx@redhat.com>, Shuah Khan <shuah@kernel.org>,
	ZhangPeng <zhangpeng362@huawei.com>
Cc: linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org,
	linux-fsdevel@vger.kernel.org, linux-mm@kvack.org,
	linux-kselftest@vger.kernel.org,
	Axel Rasmussen <axelrasmussen@google.com>
Subject: [PATCH v2 2/6] mm: userfaultfd: refactor hugetlb folio allocation / lookup code
Date: Thu, 29 Jun 2023 13:50:36 -0700	[thread overview]
Message-ID: <20230629205040.665834-2-axelrasmussen@google.com> (raw)
In-Reply-To: <20230629205040.665834-1-axelrasmussen@google.com>

At the top of `hugetlb_mfill_atomic_pte`, we need to get the folio we're
going to be mapping. There are three basic cases we're dealing with
here:

1. We're doing a UFFDIO_CONTINUE, in which case we lookup an existing
   folio in the pagecache, instead of allocating a new one.
2. We need to allocate a new folio.
3. We previously failed while populating our new folio, so we "returned"
   a temporary folio using `foliop` and had our caller retry.

In a future commit I'm going to add a fourth case for UFFDIO_POISON,
where we aren't going to map a folio at all (newly allocated or
otherwise). This end state will be simpler, and we can re-use a bit more
code, if we stop using `if (...)` to distinguish the cases.

So, refactor the cases so they share most of the same code, and instead
switch to `goto` to skip some parts depending on the case at hand.

Signed-off-by: Axel Rasmussen <axelrasmussen@google.com>
---
 mm/hugetlb.c | 53 +++++++++++++++++++++++++---------------------------
 1 file changed, 25 insertions(+), 28 deletions(-)

diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index bce28cca73a1..38711d49e4db 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -6259,22 +6259,32 @@ int hugetlb_mfill_atomic_pte(pte_t *dst_pte,
 		if (IS_ERR(folio))
 			goto out;
 		folio_in_pagecache = true;
-	} else if (!*foliop) {
-		/* If a folio already exists, then it's UFFDIO_COPY for
-		 * a non-missing case. Return -EEXIST.
-		 */
-		if (vm_shared &&
-		    hugetlbfs_pagecache_present(h, dst_vma, dst_addr)) {
-			ret = -EEXIST;
-			goto out;
+		goto ready;
+	}
+
+	/* If a folio already exists, then it's UFFDIO_COPY for
+	 * a non-missing case. Return -EEXIST.
+	 */
+	if (vm_shared && hugetlbfs_pagecache_present(h, dst_vma, dst_addr)) {
+		ret = -EEXIST;
+		if (*foliop) {
+			folio_put(*foliop);
+			*foliop = NULL;
 		}
+		goto out;
+	}
 
-		folio = alloc_hugetlb_folio(dst_vma, dst_addr, 0);
-		if (IS_ERR(folio)) {
-			ret = -ENOMEM;
-			goto out;
+	folio = alloc_hugetlb_folio(dst_vma, dst_addr, 0);
+	if (IS_ERR(folio)) {
+		ret = -ENOMEM;
+		if (*foliop) {
+			folio_put(*foliop);
+			*foliop = NULL;
 		}
+		goto out;
+	}
 
+	if (!*foliop) {
 		ret = copy_folio_from_user(folio, (const void __user *) src_addr,
 					   false);
 
@@ -6302,22 +6312,7 @@ int hugetlb_mfill_atomic_pte(pte_t *dst_pte,
 			 */
 			goto out;
 		}
-	} else {
-		if (vm_shared &&
-		    hugetlbfs_pagecache_present(h, dst_vma, dst_addr)) {
-			folio_put(*foliop);
-			ret = -EEXIST;
-			*foliop = NULL;
-			goto out;
-		}
-
-		folio = alloc_hugetlb_folio(dst_vma, dst_addr, 0);
-		if (IS_ERR(folio)) {
-			folio_put(*foliop);
-			ret = -ENOMEM;
-			*foliop = NULL;
-			goto out;
-		}
+	} else { /* Caller retried because we set *foliop previously */
 		ret = copy_user_large_folio(folio, *foliop, dst_addr, dst_vma);
 		folio_put(*foliop);
 		*foliop = NULL;
@@ -6327,6 +6322,8 @@ int hugetlb_mfill_atomic_pte(pte_t *dst_pte,
 		}
 	}
 
+ready: /* `folio` ready to map (non-NULL, populated) */
+
 	/*
 	 * The memory barrier inside __folio_mark_uptodate makes sure that
 	 * preceding stores to the page contents become visible before
-- 
2.41.0.255.g8b1d071c50-goog


  reply	other threads:[~2023-06-29 20:51 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-29 20:50 [PATCH v2 1/6] mm: userfaultfd: add new UFFDIO_POISON ioctl Axel Rasmussen
2023-06-29 20:50 ` Axel Rasmussen [this message]
2023-07-04 20:57   ` [PATCH v2 2/6] mm: userfaultfd: refactor hugetlb folio allocation / lookup code Peter Xu
2023-06-29 20:50 ` [PATCH v2 3/6] mm: userfaultfd: support UFFDIO_POISON for hugetlbfs Axel Rasmussen
2023-07-04 20:59   ` Peter Xu
2023-06-29 20:50 ` [PATCH v2 4/6] selftests/mm: refactor uffd_poll_thread to allow custom fault handlers Axel Rasmussen
2023-07-04 21:03   ` Peter Xu
2023-07-05 17:50     ` Axel Rasmussen
2023-07-05 18:17       ` Peter Xu
2023-06-29 20:50 ` [PATCH v2 5/6] selftests/mm: add uffd unit test for UFFDIO_POISON Axel Rasmussen
2023-07-04 21:15   ` Peter Xu
2023-06-29 20:50 ` [PATCH v2 6/6] mm: userfaultfd: add basic documentation " Axel Rasmussen
2023-06-29 21:04   ` Randy Dunlap
2023-07-04 21:16   ` Peter Xu
2023-07-04 20:38 ` [PATCH v2 1/6] mm: userfaultfd: add new UFFDIO_POISON ioctl Peter Xu
2023-07-05 16:09   ` James Houghton
2023-07-05 16:15     ` Peter Xu
2023-07-05 16:27       ` James Houghton
2023-07-05 16:38         ` Peter Xu
2023-07-05 17:56           ` Axel Rasmussen
2023-07-05 15:58 ` James Houghton
2023-07-05 18:17 ` Jiaqi Yan
2023-07-05 18:23   ` Axel Rasmussen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230629205040.665834-2-axelrasmussen@google.com \
    --to=axelrasmussen@google.com \
    --cc=Liam.Howlett@oracle.com \
    --cc=akpm@linux-foundation.org \
    --cc=brauner@kernel.org \
    --cc=corbet@lwn.net \
    --cc=david@redhat.com \
    --cc=hughd@google.com \
    --cc=jiaqiyan@google.com \
    --cc=jthoughton@google.com \
    --cc=linmiaohe@huawei.com \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=mike.kravetz@oracle.com \
    --cc=muchun.song@linux.dev \
    --cc=namit@vmware.com \
    --cc=naoya.horiguchi@nec.com \
    --cc=peterx@redhat.com \
    --cc=rppt@kernel.org \
    --cc=shuah@kernel.org \
    --cc=viro@zeniv.linux.org.uk \
    --cc=ying.huang@intel.com \
    --cc=zhangpeng362@huawei.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.