From nobody Sun Feb 8 18:15:25 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 71AF3EB64D7 for ; Wed, 28 Jun 2023 21:54:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232117AbjF1VyY (ORCPT ); Wed, 28 Jun 2023 17:54:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37748 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231303AbjF1VyJ (ORCPT ); Wed, 28 Jun 2023 17:54:09 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B47E61FFB for ; Wed, 28 Jun 2023 14:53:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1687989197; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=v3NUxws11iZimZs89ga1+mchuciCcK5t8cj7qgaLP/g=; b=OEl983S61xAqUc7taiXElasALKboGk6fgtZlBFgUd3JbyO63frx7h8JFAIq9/CMAz5m3Kd p6gr7hj052LFA+Milgu40T1D1LtvRkcc/GZnl9NwOl0bexv3vNv45p1tVaFCv/09zukdP2 aNVoUAp2Pqa47jUVLdeeflnZ8lC8zCg= Received: from mail-qv1-f72.google.com (mail-qv1-f72.google.com [209.85.219.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-558-JOaH0agJOf6-Ev5MPtQW0A-1; Wed, 28 Jun 2023 17:53:16 -0400 X-MC-Unique: JOaH0agJOf6-Ev5MPtQW0A-1 Received: by mail-qv1-f72.google.com with SMTP id 6a1803df08f44-62dd79f63e0so60436d6.0 for ; Wed, 28 Jun 2023 14:53:16 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687989195; x=1690581195; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=v3NUxws11iZimZs89ga1+mchuciCcK5t8cj7qgaLP/g=; b=KtfDYZi4WJPdTgeral5vIpmT5bcvhfjPh57yKrTRUDfCUY2MeHnlYGdrtM/OGnuhvJ /fDaeZWKQkcwcawYT1aF6FgeRb5O+WSDd3VdbLQuqsqOgWuINYxLyVf7GJUoD2bNvL0F g04zGBKT7QwQaKGv9nPVD+4Pv/eLPIu9krWLUMkqoSvSalhgYBouT9QpemVTpAPPAcPI 1NrXMxBJDZ5RMy1QY6C2+0OKM9r47A6N4USodc2c1w3VwZA9U+YEXcIUT8t9gjO7nBlp REuAtz9jzoedQH+yRBJCk9jy+cj0cdMtQ2FkxbZ78ywAnxhcNjnSuK8rQ+PPrvyPz5pO Ou+w== X-Gm-Message-State: AC+VfDyueb1hZSCRtAOP/g9plnRY+BjX1WCFtuQhPr7TK8D7rYrgdCZl qkQKtRASk85m7RGw6WxU2AlL4qkR2VfR1u0A4xWDT7h3jRruk9Cb0dt0zC9+5Y8DbtwazBZ8iBj 9nG0Dr/0is829bfVXlIOzJy2t X-Received: by 2002:a05:6214:f2f:b0:62b:6c6f:b3e3 with SMTP id iw15-20020a0562140f2f00b0062b6c6fb3e3mr45427363qvb.3.1687989195524; Wed, 28 Jun 2023 14:53:15 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6HBU77CP7xJIw4KayPpNq4pGfYosv7bGjCnvBLq1S/WNi+e+thj4Rrto2qPbX4dx7awzouIw== X-Received: by 2002:a05:6214:f2f:b0:62b:6c6f:b3e3 with SMTP id iw15-20020a0562140f2f00b0062b6c6fb3e3mr45427346qvb.3.1687989195254; Wed, 28 Jun 2023 14:53:15 -0700 (PDT) Received: from x1n.. (cpe5c7695f3aee0-cm5c7695f3aede.cpe.net.cable.rogers.com. [99.254.144.39]) by smtp.gmail.com with ESMTPSA id p3-20020a0cfac3000000b00631fea4d5bcsm6277797qvo.95.2023.06.28.14.53.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Jun 2023 14:53:15 -0700 (PDT) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: David Hildenbrand , "Kirill A . Shutemov" , Andrew Morton , Andrea Arcangeli , Mike Rapoport , John Hubbard , Matthew Wilcox , Mike Kravetz , Vlastimil Babka , Yang Shi , James Houghton , Jason Gunthorpe , Lorenzo Stoakes , Hugh Dickins , peterx@redhat.com Subject: [PATCH v4 2/8] mm/hugetlb: Prepare hugetlb_follow_page_mask() for FOLL_PIN Date: Wed, 28 Jun 2023 17:53:04 -0400 Message-ID: <20230628215310.73782-3-peterx@redhat.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230628215310.73782-1-peterx@redhat.com> References: <20230628215310.73782-1-peterx@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" follow_page() doesn't use FOLL_PIN, meanwhile hugetlb seems to not be the target of FOLL_WRITE either. However add the checks. Namely, either the need to CoW due to missing write bit, or proper unsharing on !AnonExclusive pages over R/O pins to reject the follow page. That brings this function closer to follow_hugetlb_page(). So we don't care before, and also for now. But we'll care if we switch over slow-gup to use hugetlb_follow_page_mask(). We'll also care when to return -EMLINK properly, as that's the gup internal api to mean "we should unshare". Not really needed for follow page path, though. When at it, switching the try_grab_page() to use WARN_ON_ONCE(), to be clear that it just should never fail. When error happens, instead of setting page=3D=3DNULL, capture the errno instead. Reviewed-by: Mike Kravetz Reviewed-by: David Hildenbrand Signed-off-by: Peter Xu --- mm/hugetlb.c | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index d04ba5782fdd..4410139cf890 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -6462,13 +6462,7 @@ struct page *hugetlb_follow_page_mask(struct vm_area= _struct *vma, struct page *page =3D NULL; spinlock_t *ptl; pte_t *pte, entry; - - /* - * FOLL_PIN is not supported for follow_page(). Ordinary GUP goes via - * follow_hugetlb_page(). - */ - if (WARN_ON_ONCE(flags & FOLL_PIN)) - return NULL; + int ret; =20 hugetlb_vma_lock_read(vma); pte =3D hugetlb_walk(vma, haddr, huge_page_size(h)); @@ -6478,8 +6472,23 @@ struct page *hugetlb_follow_page_mask(struct vm_area= _struct *vma, ptl =3D huge_pte_lock(h, mm, pte); entry =3D huge_ptep_get(pte); if (pte_present(entry)) { - page =3D pte_page(entry) + - ((address & ~huge_page_mask(h)) >> PAGE_SHIFT); + page =3D pte_page(entry); + + if (!huge_pte_write(entry)) { + if (flags & FOLL_WRITE) { + page =3D NULL; + goto out; + } + + if (gup_must_unshare(vma, flags, page)) { + /* Tell the caller to do unsharing */ + page =3D ERR_PTR(-EMLINK); + goto out; + } + } + + page +=3D ((address & ~huge_page_mask(h)) >> PAGE_SHIFT); + /* * Note that page may be a sub-page, and with vmemmap * optimizations the page struct may be read only. @@ -6489,8 +6498,10 @@ struct page *hugetlb_follow_page_mask(struct vm_area= _struct *vma, * try_grab_page() should always be able to get the page here, * because we hold the ptl lock and have verified pte_present(). */ - if (try_grab_page(page, flags)) { - page =3D NULL; + ret =3D try_grab_page(page, flags); + + if (WARN_ON_ONCE(ret)) { + page =3D ERR_PTR(ret); goto out; } } --=20 2.41.0