From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DA0AF2D8379 for ; Thu, 23 Apr 2026 13:42:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776951722; cv=none; b=XfJMDkPI4yCEL8zZ+iJU18YdokOFdI1GHv7esaaKWJsFzgS8004oDxKLT3CqTzPJR1LQ9FukTZz4D26e2+xCHDaZXhe5/0CyVJ/GID5Cgt/FdVK0bOqVh+FJAT+tMSCVwAUe5WGth2ZXnZLbr1LnbT/TT/H4SK32Ob8QSREYXOE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776951722; c=relaxed/simple; bh=qOaA7NIPr2tw79ziq7W+A48uOM2/yb3Pe/Bnkwop12Y=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=VGot2uAWwzuDyftPhU86bqbtw66B48ThOMVYTtdKusSpmdj3J1w8M8MAwvbkptFUKdDkM0GzhwCIfyMn9ANfke3n88jzxGkRyXzcHlvKg8n4otfdBu7eOSuJgjHpCi7Yx5AgS1icd0gwT/FxYZt/LR+7PscWipKnFHEQeAxSYg0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=PsfsVAy8; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="PsfsVAy8" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2C5C6C2BCAF; Thu, 23 Apr 2026 13:42:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1776951722; bh=qOaA7NIPr2tw79ziq7W+A48uOM2/yb3Pe/Bnkwop12Y=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=PsfsVAy8uQgOpI3Y4M5eaBM3Sft+GDNd+f16EfK60t9R55sNhFrzmy53+Ltz38S3L L8JzHVNDr1UrdeJ3HgbUIioD0UZnjTk0UOTbupzHzmm2SAKYkvlG3aFqsOOHErFN9N B8mgTPo81ti72GOKRH7bDgMvTmKvGxI3/mRfrAOM= Date: Thu, 23 Apr 2026 15:42:00 +0200 From: Greg Kroah-Hartman To: "David Hildenbrand (Arm)" Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrew Morton , Jason Gunthorpe , John Hubbard , Peter Xu Subject: Re: [PATCH] mm/gup: honour FOLL_PIN in NOMMU __get_user_pages_locked() Message-ID: <2026042336-hermit-stout-a63f@gregkh> References: <2026042334-acutely-unadorned-e05c@gregkh> <2026042314-traffic-riverbank-d9a1@gregkh> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: On Thu, Apr 23, 2026 at 03:04:56PM +0200, David Hildenbrand (Arm) wrote: > > >> It just follows what we do everywhere else (e.g., follow_page_pte()). > >> > >> > >>> + if (try_grab_folio(page_folio(pages[i]), 1, > >>> + foll_flags)) { > >>> + pages[i] = NULL; > >>> + break; > >>> + } > >>> + } > >> > >> If it fails on the first iteration, we return -EFAULT instead of -ENOMEM. > >> > >> I know, I know, nobody cares. But if we touch it, we might just want to return > >> the error we get from try_grab_folio(). > > > > So just abort here and return it? No, that will not work, there's a > > lock we would jump around. How about something like this horrid thing, > > adding back in the relevant unlikely() to match the other calls like > > this: > > > > > > diff --git a/mm/gup.c b/mm/gup.c > > index ad9ded39609c..8fa5b37be8b7 100644 > > --- a/mm/gup.c > > +++ b/mm/gup.c > > @@ -1983,6 +1983,7 @@ static long __get_user_pages_locked(struct mm_struct *mm, unsigned long start, > > struct vm_area_struct *vma; > > bool must_unlock = false; > > vm_flags_t vm_flags; > > + int ret; > > long i; > > > > if (!nr_pages) > > @@ -2019,8 +2020,15 @@ static long __get_user_pages_locked(struct mm_struct *mm, unsigned long start, > > > > if (pages) { > > pages[i] = virt_to_page((void *)start); > > - if (pages[i]) > > - get_page(pages[i]); > > + if (pages[i]) { > > + ret = try_grab_folio(page_folio(pages[i]), 1, > > + foll_flags); > > + if (unlikely(ret)) { > > + pages[i] = NULL; > > + i = ret; > > + break; > > So, we have to report the old i in case of an error. > > Assuming we want to also return -EFAULT on !pages[i] -- which i think we want, > maybe the following (uncompiled and untested)? > > diff --git a/mm/gup.c b/mm/gup.c > index ad9ded39609c..d00ff8d988fa 100644 > --- a/mm/gup.c > +++ b/mm/gup.c > @@ -1983,6 +1983,7 @@ static long __get_user_pages_locked(struct mm_struct *mm, unsigned long start, > struct vm_area_struct *vma; > bool must_unlock = false; > vm_flags_t vm_flags; > + int ret, err = -EFAULT; > long i; > > if (!nr_pages) > @@ -2019,8 +2020,14 @@ static long __get_user_pages_locked(struct mm_struct *mm, unsigned long start, > > if (pages) { > pages[i] = virt_to_page((void *)start); > - if (pages[i]) > - get_page(pages[i]); > + if (!pages[i]) > + break; > + ret = try_grab_folio(page_folio(pages[i]), 1, foll_flags); > + if (ret) { > + pages[i] = NULL; > + err = ret; > + break > + } > } > > start = (start + PAGE_SIZE) & PAGE_MASK; > @@ -2031,7 +2038,7 @@ static long __get_user_pages_locked(struct mm_struct *mm, unsigned long start, > *locked = 0; > } > > - return i ? : -EFAULT; > + return i ? : err; > } > #endif /* !CONFIG_MMU */ Yup, this passes my tests, let me go send a v2 with this version, thanks! greg k-h