From patchwork Tue Jun 13 21:53:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13279233 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 DBB9FEB64D7 for ; Tue, 13 Jun 2023 21:54:01 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D86E06B0078; Tue, 13 Jun 2023 17:54:00 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D361B6B007B; Tue, 13 Jun 2023 17:54:00 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BD7238E0002; Tue, 13 Jun 2023 17:54:00 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id B09F56B0078 for ; Tue, 13 Jun 2023 17:54:00 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 8480A40207 for ; Tue, 13 Jun 2023 21:54:00 +0000 (UTC) X-FDA: 80899077840.14.AA03CC8 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf08.hostedemail.com (Postfix) with ESMTP id 671CF160012 for ; Tue, 13 Jun 2023 21:53:58 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=NwkvWmSt; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf08.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=1686693238; a=rsa-sha256; cv=none; b=wE1Z7cgSNppI2W6C+H/DTWoDgKaiB3wjHWUwxCDXo7QOyIEKMZgyVnK0VXxKNKYWdeEmAQ fOSXJE4b51jDUC9yDuK+d6zMU9gi0YW0URk8b0jV139kEJhXdTGW41zdcdjpgM6bVAMded x6E96SinOEjpQXA7eA2LM5P23BKzZcE= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=NwkvWmSt; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf08.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=1686693238; 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=J0Si/SHg9D7WH2a+07u6wT7XlhbAs86SLIvfSHyM0qs=; b=QGCRafepJaY2mNY1XN15/rhiIZCwRZtzcZ2CILBJdyUmHAXXiT34mm6bmM1+mO1wSz+jfF R9jgziGWMS/5oNsonjTy3rHBEh7l1b9BBwchX2JiWyCE8gUxDeqcvMukOlxQL88kt3PN8H W/eKXEeqTUFH7/3DFNSrPoQl3+q4XKw= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1686693237; 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=J0Si/SHg9D7WH2a+07u6wT7XlhbAs86SLIvfSHyM0qs=; b=NwkvWmStPzXjOE1c3eSf0Uy0SsB8gMjkT54CgKFhebI1/+EBDVLiRjMql09zPzRW8v5Tjg EvmgvwdpqCtUCjS9bbZfWeAbX0Ff7gTaUrKjL0gaMwsHeyht8uFMhvW9tVZNqVab5EsJWl XqXdfCiiAQx/ZcEw2ZwkibBBQ1n7kDU= Received: from mail-qt1-f198.google.com (mail-qt1-f198.google.com [209.85.160.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-416-hTNqCtOGNRSf8Z5zWwSbIg-1; Tue, 13 Jun 2023 17:53:55 -0400 X-MC-Unique: hTNqCtOGNRSf8Z5zWwSbIg-1 Received: by mail-qt1-f198.google.com with SMTP id d75a77b69052e-3f86a3ce946so13356631cf.1 for ; Tue, 13 Jun 2023 14:53:54 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686693234; x=1689285234; 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=J0Si/SHg9D7WH2a+07u6wT7XlhbAs86SLIvfSHyM0qs=; b=fFb4alVz5WiW09Gspsx7kyZ+3UvPu2AUvCCKjDWkArbKoiokTvc3zhLkbOeVMbCY2o 4wztvdH6nsFuGW2MhBDFyglV8MENF5lLx/RDvYdIMTdqkVaz7Qn/6myk8Cm7yeL0/xcj Mu81xwtwU5oUSpXszDqo4YESjBeHgMTxOnTiEGHvZIcJ9srZv2/cNKt6nB5aJ/oquAIj rjzC1s8HJ1H1ybOpyl/OmZfHYhq0z5Y6KTpSGaO3zNySp58DyeomID/pW58NFYRl0qUZ Z3L81/zNYNa9w0u9x0J6Xc7CBs00ViYI2JjEGntfPntT/1xXoWWPkhPsykWILqOtdsnw cSfw== X-Gm-Message-State: AC+VfDx0Lb/s4WFVLX44WDK+H9nZSBc/gCtqnNR5NTGmQGROzfd5pzut vVUUpfoQrOZaaFuFO0WD9qmgbHDFmdAd47+fr6MINlXpE0JF28NiOGD3xJZQ5zafVBwHniA7Xm5 Y+Mpeol6XkjU= X-Received: by 2002:a05:622a:1981:b0:3f5:315f:5c1d with SMTP id u1-20020a05622a198100b003f5315f5c1dmr16598118qtc.4.1686693234461; Tue, 13 Jun 2023 14:53:54 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4tfbjVj7iW6/DtRd/s2e+Ti93d6WI6EV2BJqT02+lq+41oEnqcND6tzmbEH3x8gd+k6nduCQ== X-Received: by 2002:a05:622a:1981:b0:3f5:315f:5c1d with SMTP id u1-20020a05622a198100b003f5315f5c1dmr16598098qtc.4.1686693234224; Tue, 13 Jun 2023 14:53:54 -0700 (PDT) Received: from x1n.redhat.com (cpe5c7695f3aee0-cm5c7695f3aede.cpe.net.cable.rogers.com. [99.254.144.39]) by smtp.gmail.com with ESMTPSA id fz24-20020a05622a5a9800b003f9bccc3182sm4522330qtb.32.2023.06.13.14.53.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Jun 2023 14:53:53 -0700 (PDT) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Matthew Wilcox , Andrea Arcangeli , John Hubbard , Mike Rapoport , David Hildenbrand , Vlastimil Babka , peterx@redhat.com, "Kirill A . Shutemov" , Andrew Morton , Mike Kravetz , James Houghton , Hugh Dickins Subject: [PATCH 2/7] mm/hugetlb: Fix hugetlb_follow_page_mask() on permission checks Date: Tue, 13 Jun 2023 17:53:41 -0400 Message-Id: <20230613215346.1022773-3-peterx@redhat.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230613215346.1022773-1-peterx@redhat.com> References: <20230613215346.1022773-1-peterx@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 671CF160012 X-Stat-Signature: ntf6gfi5hhkpg3b17xujgiefz4kpwz8a X-HE-Tag: 1686693238-817898 X-HE-Meta: U2FsdGVkX1+l99wwiY1ENcSBpGoUqRqojtZ9AKMojRTR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/BXlTcznCArNmYzu2yLE3aHgWwjT6jPlq7eH7Sn7h5Y0ZBPuovIl0w0XqJuQabYwo2pCGV3vJ5uHc1hqqsXy+c7OTcQIybigKtc5IZfJt8LC6GoAXF9+CWciyxOzkHGyMLYuAKaIFmMVXb75FG3j0d149fNglIB30Sh705norvjM3JyS4nmhwXtiJzPuf4XNw0ZWGcB8rvf83BAG7RwYprP6/ec7mx/ZaRJL4084Dw3kENTtm/lkFUjwb4/MI+NGrxkmTSxlQTMX+/inbyuVrYPXTD17Oytr1ZxRS7rpm7GqPaMl7Q== 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: It seems hugetlb_follow_page_mask() was missing permission checks. For example, one follow_page() can get the hugetlb page with FOLL_WRITE even if the page is read-only. And it wasn't there even in the old follow_page_mask(), where we can reference from before commit 57a196a58421 ("hugetlb: simplify hugetlb handling in follow_page_mask"). Let's add them, 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(). I just doubt how many of us care for that, for FOLL_PIN follow_page doesn't really happen at all. But we'll care, and care more if we switch over slow-gup to use hugetlb_follow_page_mask(). We'll also care when to return -EMLINK then, as that's the gup internal api to mean "we should do CoR". When at it, switching the try_grab_page() to use WARN_ON_ONCE(), to be clear that it just should never fail. Signed-off-by: Peter Xu Reviewed-by: Mike Kravetz --- mm/hugetlb.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 82dfdd96db4c..9c261921b2cf 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -6481,8 +6481,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. @@ -6492,10 +6505,7 @@ 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 = NULL; - goto out; - } + WARN_ON_ONCE(try_grab_page(page, flags)); } out: spin_unlock(ptl);