Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp21298201rwd; Thu, 29 Jun 2023 14:02:31 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5Bc6wuIX6zXxJfjjtB1S8/upuiICiMa5MnHb/OSREkwVGLaDlr9bIoAJMfkYMbcPGOk/Jw X-Received: by 2002:a05:6a20:9390:b0:126:23d:cd10 with SMTP id x16-20020a056a20939000b00126023dcd10mr1173264pzh.21.1688072551592; Thu, 29 Jun 2023 14:02:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688072551; cv=none; d=google.com; s=arc-20160816; b=awNTGeo5zYfw1+4SeXdowKzYyYoBk6KaQuhgZhUwiO9NAtItxO7dYm+xwMNPQ8b8gp y4owhZsxWvW5ykqu8n8z2NK6Dx+jvaW1hIS/KIO1CvX5qFmWxsvSa+sLuz/H5tmMqPLR 8L3+xJPeawq8stvx+ypQ+oG+15ZPR83mJpgvJo7c8s81Xb1A0PjQ51xAN1cqTfzRrqo9 KQWZGtsaX5wiQWI1YiuuH27fpZhSBy+g4PTKsD2ZASGxYMrrJeAWjsvHqNRlebfxFSvv lbqnOaZyzDMDX08CrVuri41qUL3nQQy8zAjAjJSMPjV/RWGv+jutPtNwDca2af7xnGKF pQ1w== 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:dkim-signature; bh=R8jkAmCGbYNygAZgSop/zKP0BAnGYhZIuFEhOa1in5M=; fh=eRaYFbc68TLHCmej1r/Q9kziindMZl6i1kh9xMHvl2k=; b=dkAx1BGXRd4TTiIeWuoGN4QIu5ROvcp3TY02RqrXtAnCHz5izTlm7uiJCrW6+zVB4+ L4lBS3sQ7/M/c0Ptm0SjGZmsLJkpGu6GiPQZEX2tSsYF2GlK2aPiKdlOAgf/I7pXKdcz fDXjkzc0krTsSzyGOmnKGrKeLr0yS63xgXLpc+GHB2oQ6hlGzNC/h6kveplKiS1cuSUx 1XXP1ULJi2MboNWt1xS51niMWeGj3gjd76QxzUNC4t5fVWV5BG7tBUEyUTHa0nK7dp5G RpUDSzjC1s4l+ny6RRKgcWvwOaWnpsQ0YuDto9tXE5fOI1VU9zd4CetWxoVn2t8c2u0x CZ1g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=a8O0wagy; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e12-20020a63db0c000000b0055b57fa6d19si677754pgg.208.2023.06.29.14.02.18; Thu, 29 Jun 2023 14:02:31 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=a8O0wagy; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232298AbjF2Uu7 (ORCPT + 99 others); Thu, 29 Jun 2023 16:50:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45946 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232017AbjF2Uuy (ORCPT ); Thu, 29 Jun 2023 16:50:54 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CD5533582 for ; Thu, 29 Jun 2023 13:50:50 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-bff1f2780ebso923590276.1 for ; Thu, 29 Jun 2023 13:50:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1688071850; x=1690663850; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=R8jkAmCGbYNygAZgSop/zKP0BAnGYhZIuFEhOa1in5M=; b=a8O0wagyUtUHav4dJ+0FTU4BfULv1hqXducKGWGQFuo+G+tIPjHy7DS1ADLv5ngmlB RSngMc0CnR4cfT4Dl1/3ekbmm7f4jkeC6RGHf37xUtQNDImjBtBI9DhIP8Z9AdXeugxu EEIqVXFJE09pcCYmkIu4KbepOYvezPBB0mUQ8g4biI54D+MfCLg+3QhsHFv6yxYBACnS hW5iDv/qv08V0YHMgwph0e9b+5pGUlXVr84spJ6yslQMHWase0Q3RfrmBefxTbxnzdJF i1oPLS3gMTFjIwMQZ2twKVJdYWAw+m3Lbi649+iUQ0hPFKbJ4IuR/hqznd3O1UEd0bUk 66bA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688071850; x=1690663850; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=R8jkAmCGbYNygAZgSop/zKP0BAnGYhZIuFEhOa1in5M=; b=UU0j/A20Nf5Eae7TIgpnsVC5CZlOtlXPaK4NjsSnzenRO4v9yJsVkScXJ4wVT6G42R 3Euv/CNsJA9RWg+3FdE8/2wURE4VBqKdXXzfT8op5yz+ujyUbHoVuspaAtQK0tu2Za8O MnjL/+T5cwfEUppNYynBTU2y2u3gSqjkl5ljjPTRgyseFnHeornChNvMgpo/KQQBRdQ9 X1rg5JC66T9mWhn5SWeN7PjYM5ShJDedbLRnomTwBsPWfNNF16DbmrHEDVxl4P8RDTBg QbAS5yusYDAcUDxYsTlFi2vKQLsrMExmuQM+KWIdf+QYkCAkixxHLHJHCBG9ELG/9L7f USNQ== X-Gm-Message-State: ABy/qLYgauaQS3mNXj2oAX2Hk6nebqer1MItGDDh0XFtUn2rTRPbqy7K Bq0YaNgvV4NuLGmCQ6EaowixIPW1G9J9zJW6uLl2 X-Received: from axel.svl.corp.google.com ([2620:15c:2a3:200:e20f:5917:3efa:d4bb]) (user=axelrasmussen job=sendgmr) by 2002:a25:738e:0:b0:bcc:285c:66ea with SMTP id o136-20020a25738e000000b00bcc285c66eamr5716ybc.3.1688071850044; Thu, 29 Jun 2023 13:50:50 -0700 (PDT) Date: Thu, 29 Jun 2023 13:50:36 -0700 In-Reply-To: <20230629205040.665834-1-axelrasmussen@google.com> Mime-Version: 1.0 References: <20230629205040.665834-1-axelrasmussen@google.com> X-Mailer: git-send-email 2.41.0.255.g8b1d071c50-goog Message-ID: <20230629205040.665834-2-axelrasmussen@google.com> Subject: [PATCH v2 2/6] mm: userfaultfd: refactor hugetlb folio allocation / lookup code From: Axel Rasmussen To: Alexander Viro , Andrew Morton , Christian Brauner , David Hildenbrand , Huang Ying , Hugh Dickins , James Houghton , Jiaqi Yan , Jonathan Corbet , "Liam R. Howlett" , Miaohe Lin , Mike Kravetz , "Mike Rapoport (IBM)" , Muchun Song , Nadav Amit , Naoya Horiguchi , Peter Xu , Shuah Khan , ZhangPeng 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 Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- 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