Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp28154973rwd; Tue, 4 Jul 2023 14:05:56 -0700 (PDT) X-Google-Smtp-Source: APBJJlHSXtyuLO9Vu//8jciJyUVm1rburqoy/0aJjYeEDL0SoapWd8mVd+rywL/qIum9VJ2efQge X-Received: by 2002:a05:6a21:3394:b0:12d:f22f:3b61 with SMTP id yy20-20020a056a21339400b0012df22f3b61mr10923566pzb.6.1688504756248; Tue, 04 Jul 2023 14:05:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688504756; cv=none; d=google.com; s=arc-20160816; b=hMW1CDUnxzcdlqcIFFwz70z2ipHgzQRAcWg1MbiEm2ECMQERGX22P0DQazN86LJNGW mh1crkdipgiB2WmySsUrqZpzi2dIdikc2bIkZTh3kqgUAFDmHXy5QQaBN2KeixLzKaf8 An8h0hKqbBL3ti7D2SSFWW+YTlJPENYylmTyIjWEgTZnqxHFA4E0BCBvP58PaBKr2qPO Uz/+gOqzBi/dByHcIFKFhqUtbgX0Nsb+urt0/WCDI4F5mIlVZdjp5nqWu8QJGTp4y7J9 vv7meMeyhfmAKkotVUAiNr7NxwqvvF8Ro5LWlsTY039xL5C1CJ4TCwnbAK2rqj8jeQ8D dz4Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=1xoQAy1k59UO+UqvJlN8ZZmc0nx2ezBp5tE6xHVc6K0=; fh=0t4louP/8+xP3ORiRbUZtig8/xKb5OX3R1/WptJFEDo=; b=kBgjL+izlxN8iHCocrWFC4cukao3IOruiAU8l1MA4owUQAQR+LxmXf6vm3RzelOiAG mvz7N+QPWlVIOBjZlmDaOiSTrOrzlVSda+FDjzEc4KiDuE31P3/dLG3nLb8F11P+Xhvt AAtDr5cK/5sEhlls1aYf1PI1fwU6oG7XwkfNw73djR4DeZAQBOPtmgPr5LpSzTjQ84T6 vgB9/0V4GM69lvPCvUzcCL2OA4pfoxlx4CDUCf6KNOwvLbIpofYHSONuRDphg3tE2eqM A3EazlbRL7rNJWo6NLOF9vlZxc562N6h9SBG1vbVpmKSSDchhZzVRXTQvAk/bkFTzZWg M84w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=iV0DQRDE; 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=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ij11-20020a170902ab4b00b001b8ae668446si262726plb.354.2023.07.04.14.05.43; Tue, 04 Jul 2023 14:05:56 -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=@redhat.com header.s=mimecast20190719 header.b=iV0DQRDE; 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=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230129AbjGDU6r (ORCPT + 99 others); Tue, 4 Jul 2023 16:58:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55484 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229903AbjGDU6q (ORCPT ); Tue, 4 Jul 2023 16:58:46 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8EE0710CA for ; Tue, 4 Jul 2023 13:57:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1688504278; 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: in-reply-to:in-reply-to:references:references; bh=1xoQAy1k59UO+UqvJlN8ZZmc0nx2ezBp5tE6xHVc6K0=; b=iV0DQRDEKDPBv/p0zk6qlr0fByaf8SI6loNCI52i/SzdLXp0FRuASHiuEcV/1kXukdS4Im NAOVJN+4EdN59LvPr9wweSG6427YSQ74NVNPtWq0alVl0v8fOFC0iGmH0TwiDQez3cyjth V2X+dwaO6Fu/TYJI1eyJn+f9X74W9Fw= Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-675-2Lg81LuVN3-OR8YT1hjgug-1; Tue, 04 Jul 2023 16:57:57 -0400 X-MC-Unique: 2Lg81LuVN3-OR8YT1hjgug-1 Received: by mail-qk1-f199.google.com with SMTP id af79cd13be357-7672918d8a4so172484885a.0 for ; Tue, 04 Jul 2023 13:57:57 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688504277; x=1691096277; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=1xoQAy1k59UO+UqvJlN8ZZmc0nx2ezBp5tE6xHVc6K0=; b=B9hz0JCu8HvmIr7xRFdT3mfFJowzoVhdbdJIBMaCnJni2XIU7MHlh/YyR3Hah7T3NK YlrXxeifgra6BbGH2zWCaCA0UvvtNQN6kJ8+NBA+xLJTo1W1xPduvCuQp6FGWsR3QWn0 /mvYeFQGnPzUNfUK+yeEdK6pJ1V1NtntSDtiCXcTDuZRjhqGgCPvN4eCHwf3ZHcoQ976 jXR717+tsCt37hEpdkEmziRQwLpsjahvww44XCP1X6RkBwve6kN+6nPuV87huPezZaL6 AdQukshoBporc969vJYM7s2mH9+UlvTiShWLFn2p+YRZV9Pmcv+SAXhPbkOket8m4wLM Rt/g== X-Gm-Message-State: AC+VfDwvtgCJfbaOQHFh4hp7u7CNEgUSY3edQOZa3W7whsxzVCME5pA9 2d81Br6NxzCr/IwDL7pwoKrvVBz3zEhmkc6VbhIiOyf3gz4hQcvm2r1/5o7KEIqM9mACcDP2WB9 kKqEFPHZTdrkU5xK7MLSOmEXL X-Received: by 2002:a05:620a:4010:b0:766:3190:8052 with SMTP id h16-20020a05620a401000b0076631908052mr19696474qko.0.1688504277097; Tue, 04 Jul 2023 13:57:57 -0700 (PDT) X-Received: by 2002:a05:620a:4010:b0:766:3190:8052 with SMTP id h16-20020a05620a401000b0076631908052mr19696444qko.0.1688504276815; Tue, 04 Jul 2023 13:57:56 -0700 (PDT) Received: from x1n (cpe5c7695f3aee0-cm5c7695f3aede.cpe.net.cable.rogers.com. [99.254.144.39]) by smtp.gmail.com with ESMTPSA id 19-20020a05620a06d300b007621bdc9d98sm91846qky.87.2023.07.04.13.57.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Jul 2023 13:57:56 -0700 (PDT) Date: Tue, 4 Jul 2023 16:57:54 -0400 From: Peter Xu To: Axel Rasmussen Cc: 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 , Shuah Khan , ZhangPeng , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org Subject: Re: [PATCH v2 2/6] mm: userfaultfd: refactor hugetlb folio allocation / lookup code Message-ID: References: <20230629205040.665834-1-axelrasmussen@google.com> <20230629205040.665834-2-axelrasmussen@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20230629205040.665834-2-axelrasmussen@google.com> X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE 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 On Thu, Jun 29, 2023 at 01:50:36PM -0700, Axel Rasmussen wrote: > 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 I didn't get why this patch is needed.. IIUC you added MFILL_ATOMIC_POISON handling at the entry of hugetlb_mfill_atomic_pte() anyway. Maybe it can even have its own hugetlb_mfill_atomic_poison()? Did I miss something? > --- > 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 > -- Peter Xu