From patchwork Mon Jun 19 23:10:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13285009 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8AD0BEB64DB for ; Mon, 19 Jun 2023 23:10:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0C1A88D0005; Mon, 19 Jun 2023 19:10:55 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 073DF8D0001; Mon, 19 Jun 2023 19:10:55 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E2D618D0005; Mon, 19 Jun 2023 19:10:54 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id D63138D0001 for ; Mon, 19 Jun 2023 19:10:54 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id A46EB4047B for ; Mon, 19 Jun 2023 23:10:54 +0000 (UTC) X-FDA: 80921044428.23.19EDF3F Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf13.hostedemail.com (Postfix) with ESMTP id 7B8BC20006 for ; Mon, 19 Jun 2023 23:10:52 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=DRTFrl8V; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf13.hostedemail.com: domain of peterx@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=peterx@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1687216252; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=AeVTfhKtj8N8KTpZF3pjKuqjoCywpEvllD4d3FaXf9c=; b=0qri0enfVBJWxuGGM/bx3DHoMSJkKASAaNg507vwoJJ832f6psao43l7PziVxvyUYVFJ4R VJ7yy9OE1lq6Aa6u1hkcPUnr11rCmwkb4431mhPr2ENEEsh5BHf3V09/CKpcF1U9o8/3iS hNz2EBJtV/xIxzRUu/yBbMJMTRAenvM= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=DRTFrl8V; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf13.hostedemail.com: domain of peterx@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=peterx@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1687216252; a=rsa-sha256; cv=none; b=s5Nd5XFINfMwbMtrBIyWH1AInzFUGi6+FpDjZOYiJXZRUSinLvujboXO10pKjddJMqoGlo PlcUcJfmhHggXv/PMoAVkFtNCQqfgOL1pzIaRJVfUs6V/Fvbz5WhxuX84qm6UpCIW9/jOs +esoTRE51Kdl9YGHqTzlVbzX7qCAZk0= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1687216251; 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: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=AeVTfhKtj8N8KTpZF3pjKuqjoCywpEvllD4d3FaXf9c=; b=DRTFrl8VDRY4j2hMgJ2dkEp64rZmU6jCP1Y3spWcZvS2WycmuhEaIduwfJm2VYbd7t2UQy RRrM1lQPSDmE0R3YTibu1S532jOKGJRZnGJ4XMrHvP9h1oOPrNaDzCfmDRL6+ck5zm1ay3 6N7xjcZVfxm/abqpvCHs+gjMYwkTMPw= Received: from mail-qk1-f200.google.com (mail-qk1-f200.google.com [209.85.222.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-22-tVQDZ8NLPkyJj5vft-H-Rg-1; Mon, 19 Jun 2023 19:10:50 -0400 X-MC-Unique: tVQDZ8NLPkyJj5vft-H-Rg-1 Received: by mail-qk1-f200.google.com with SMTP id af79cd13be357-7625940fbf3so29806485a.0 for ; Mon, 19 Jun 2023 16:10:50 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687216250; x=1689808250; 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=AeVTfhKtj8N8KTpZF3pjKuqjoCywpEvllD4d3FaXf9c=; b=eSNx9yDp67xMQfoJ9bBl9Q5a2MxeRjBJTLjo5K0/Dj/EgDjF6AyAjJoB+JDmnL55rD Ack4Fg8m6VptYh8zO/kOKYBtz7luqYvPnZpHhronWZybSy9qKAfquFxNS/hCUKygtY5G xA2MtyDfin89PzAnAIP2xRFCoefuFy2zWhKt0dSS4pbMUULekGUL92iYiyx+iC/urtBz opgs5vnq5WPk4DEBscQIrKDJYdQ/tXm0hIR+QzUspuUgaes1Smd3m99/IcCLuFuFz/rg wDl6bLQ8XfjaqPzj3l7ZFXWO5AwHrRECp7FyjoqKqn1gY13UsZbjp0UjuumSkgk/vYep BTJA== X-Gm-Message-State: AC+VfDywy6TgCwsYqaqbKJyLVoXaNPlJAIq1d2YPuSpNxrTTp1huti8H M/t+cu7Gp53PNp/PfY3KU0RJyHYWGZmJrb1DgFHV6jjWmt+LLs7ahQhETYHGo9mNL0o27s06QoM EZpc6kwVaPfH8OBoGx3iHsdOONrmPtX6yhw3lL2bO1fM6n8XKGDsCAT/Fvts1+ePppXEs X-Received: by 2002:a05:620a:2889:b0:75b:23a1:82a4 with SMTP id j9-20020a05620a288900b0075b23a182a4mr13246253qkp.5.1687216249888; Mon, 19 Jun 2023 16:10:49 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5+hV0zbUQ9aN/HCxdwdLyhYSlUogcbP8i7JCZJTHjyygAvLUH/tHfcgC5j3ZztZhUlzdg3lA== X-Received: by 2002:a05:620a:2889:b0:75b:23a1:82a4 with SMTP id j9-20020a05620a288900b0075b23a182a4mr13246224qkp.5.1687216249526; Mon, 19 Jun 2023 16:10:49 -0700 (PDT) Received: from x1n.. (cpe5c7695f3aee0-cm5c7695f3aede.cpe.net.cable.rogers.com. [99.254.144.39]) by smtp.gmail.com with ESMTPSA id t15-20020a05620a034f00b007592f2016f4sm405864qkm.110.2023.06.19.16.10.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Jun 2023 16:10:49 -0700 (PDT) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Andrea Arcangeli , Mike Rapoport , David Hildenbrand , Matthew Wilcox , Vlastimil Babka , John Hubbard , "Kirill A . Shutemov" , James Houghton , Andrew Morton , Lorenzo Stoakes , Hugh Dickins , Mike Kravetz , peterx@redhat.com, Jason Gunthorpe Subject: [PATCH v2 2/8] mm/hugetlb: Prepare hugetlb_follow_page_mask() for FOLL_PIN Date: Mon, 19 Jun 2023 19:10:38 -0400 Message-Id: <20230619231044.112894-3-peterx@redhat.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230619231044.112894-1-peterx@redhat.com> References: <20230619231044.112894-1-peterx@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Rspamd-Queue-Id: 7B8BC20006 X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: pxasjpgu419s5ott9356rqbewbtu49dw X-HE-Tag: 1687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ZKYgSmBWWKScujxZR2Mb/xsg4dTQ5O7kwvUVeoddR5Z5ZTCp2KGQNlbfRMsL+LIdW9qgDloA7mt6eAH9b8HXmfgKRzRqV01RevnRjcc0H/8fc2SO9M97ihPtWsq6lTeANXC8Y8DEtBB73qKzT5NfmOJIcv23QAHKh8fEN1adWOBtdxnAtD6m9Vv8yJ0oELe6dG02v2C35XrCZpY29xjDmFLy/+3KuSpAIWbTSi6iOkHKZFZEhpPDlFQ3pu6fnqVi9OqsRgFT0wSdECyP0Kg1UOmp2Hm4oy8MvrTPgZPsNrEIwQkWJD/q7vzqt57FlEOqd9LK53lAU7UJrom8Lln8IsnIxfn5Cw+CyXppRd0rbXfT9clh1/5qISEdxxzL18I= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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 CoR 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 do CoR". 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. Reviewed-by: Mike Kravetz Signed-off-by: Peter Xu --- mm/hugetlb.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index f75f5e78ff0b..9a6918c4250a 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -6463,13 +6463,6 @@ struct page *hugetlb_follow_page_mask(struct vm_area_struct *vma, 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; - hugetlb_vma_lock_read(vma); pte = hugetlb_walk(vma, haddr, huge_page_size(h)); if (!pte) @@ -6478,8 +6471,21 @@ struct page *hugetlb_follow_page_mask(struct vm_area_struct *vma, ptl = huge_pte_lock(h, mm, pte); entry = huge_ptep_get(pte); if (pte_present(entry)) { - page = pte_page(entry) + - ((address & ~huge_page_mask(h)) >> PAGE_SHIFT); + page = pte_page(entry); + + if (gup_must_unshare(vma, flags, page)) { + /* Tell the caller to do Copy-On-Read */ + page = ERR_PTR(-EMLINK); + goto out; + } + + if ((flags & FOLL_WRITE) && !pte_write(entry)) { + page = NULL; + goto out; + } + + page += ((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.