From patchwork Fri Jun 23 14:29:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13290734 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 2522BEB64D7 for ; Fri, 23 Jun 2023 14:29:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CC4EE8D0005; Fri, 23 Jun 2023 10:29:49 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C005E8D0001; Fri, 23 Jun 2023 10:29:49 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A06028D0006; Fri, 23 Jun 2023 10:29:49 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 779E08D0001 for ; Fri, 23 Jun 2023 10:29:49 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 3F58A1C8F54 for ; Fri, 23 Jun 2023 14:29:49 +0000 (UTC) X-FDA: 80934246498.08.2D6D293 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf26.hostedemail.com (Postfix) with ESMTP id 7FE7814000C for ; Fri, 23 Jun 2023 14:29:46 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=BupfHnaM; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf26.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=1687530586; 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=6gk9nBOPG6j2+jM6FIDDaVYVwIkM1l3Tetg7sVWCV7Y=; b=jPDJN/chZUucEzVIPXLslsOG9lVRTrGgsdB3ot3xrWA7oDbs8keH/GiK31VljfdBs2o2P1 HF7/RcVnCBB3X47E01GOYLq0o7MMQ5gi7u96Zv3S/N2TaWHcQ561cUUiwyNEA2u2z3NtUG ybGDkg0mxC5Zf6TjOhKeZ6+MOF6xWrc= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=BupfHnaM; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf26.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=1687530586; a=rsa-sha256; cv=none; b=oH3qRLKuuPgzvdakCiTIzv5Zhrv8nOMm6UMApdbYmkg0An/imYzYx1KRkJ6pLQ7L8gQb3p O4XHrCmz8dzXTS3bO0xO69d5myXwMmQKl4QI9kGskxXMSPZ+PvOBBpqd25V4MW6VrqpUCr 3mdvCtJF6905jvZTr/73AJG+GuJdFGU= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1687530585; 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=6gk9nBOPG6j2+jM6FIDDaVYVwIkM1l3Tetg7sVWCV7Y=; b=BupfHnaM5CAo6ndolRUqy6fl+iS0EGf2lChEMbNJeZmrS2uYctos5I2A9XDJiQc+1GqtHw AJ1QsYtr+LkdIbkN4q89qOwLv4YCDmoqExtvbpZJIrpm9zJjQgk9FcYDoiwi/uXvZncyXJ v7YFQ4yadrI6XCUykYznic4p0bxupeQ= Received: from mail-qv1-f70.google.com (mail-qv1-f70.google.com [209.85.219.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-306-tC_h3gOHOni4Wm-6b6DpDA-1; Fri, 23 Jun 2023 10:29:43 -0400 X-MC-Unique: tC_h3gOHOni4Wm-6b6DpDA-1 Received: by mail-qv1-f70.google.com with SMTP id 6a1803df08f44-62ffa1214edso1381506d6.0 for ; Fri, 23 Jun 2023 07:29:43 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687530583; x=1690122583; 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=6gk9nBOPG6j2+jM6FIDDaVYVwIkM1l3Tetg7sVWCV7Y=; b=KEXGtRKC9IepgdtQWKmYcWTsoI2J2vqxH1wM27VuoD9zS60Dyq1Y3e7l8u22JgZ6sn uwQDlaF4pelRYaRLpqYIqfZggryNQTbzRZgVIVw+IRBJYX4itN01epXqeMiS9aMM9Lus KopXn3WBEHn3qmmMN+EUUWoLMCIQz2k77dFlCS2P3o75P/qIu/cPDrbXKh1eTWk5Iobb x9v2KnKK7wEnp6SRBYANeLnjAjwxmkRYUo2Y04gXhsFnL1HfjnYeglmuxLpN0QZm0jYk a8rczyal2pj+A2Qkj+s9QGs1fcOqdlE2xXozMwqwmBfDmSPJSp72Ev84b4mqQSeahVpl VFRQ== X-Gm-Message-State: AC+VfDxjGC9isUNSbQqHFwbXSNpBh9qkesAJCKT8DXW9XOjFJ+vRNrql LG+av0yDpZ+Y9lKsIN2//wwzgd4DWGdETvnhsOhAQm4MiHdABsmNp7uzf+GJswGvu2UkKnVK/tX UWYgU5d0XT2vrshpUNk91O/oALx8eHgWzigpk7/6QPUnK7HvfRhw4RYG3ot//UPb6pRq+ X-Received: by 2002:a05:6214:27eb:b0:616:870c:96b8 with SMTP id jt11-20020a05621427eb00b00616870c96b8mr25389420qvb.3.1687530582761; Fri, 23 Jun 2023 07:29:42 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6b6BfAOlLY4UY84oPtrElMpGPs2JooejEq2dKySJ7sqg4gO6FWfBJO1DRPK0VM93X3ONSVMg== X-Received: by 2002:a05:6214:27eb:b0:616:870c:96b8 with SMTP id jt11-20020a05621427eb00b00616870c96b8mr25389388qvb.3.1687530582350; Fri, 23 Jun 2023 07:29:42 -0700 (PDT) Received: from x1n.. (cpe5c7695f3aee0-cm5c7695f3aede.cpe.net.cable.rogers.com. [99.254.144.39]) by smtp.gmail.com with ESMTPSA id b9-20020a0cc989000000b0062821057ac7sm5104827qvk.39.2023.06.23.07.29.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Jun 2023 07:29:41 -0700 (PDT) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Lorenzo Stoakes , John Hubbard , Andrew Morton , Mike Rapoport , David Hildenbrand , peterx@redhat.com, Yang Shi , Andrea Arcangeli , Vlastimil Babka , "Kirill A . Shutemov" , James Houghton , Matthew Wilcox , Mike Kravetz , Hugh Dickins , Jason Gunthorpe Subject: [PATCH v3 2/8] mm/hugetlb: Prepare hugetlb_follow_page_mask() for FOLL_PIN Date: Fri, 23 Jun 2023 10:29:30 -0400 Message-Id: <20230623142936.268456-3-peterx@redhat.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230623142936.268456-1-peterx@redhat.com> References: <20230623142936.268456-1-peterx@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Rspamd-Queue-Id: 7FE7814000C X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: 55fzt3or1cf8johi7cq7e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crBHqeMa/1QRRKNWToRWdCVU4Hj5y3d5zXFzgP7Fqsz0YGjUQWznager1gqiBEJTPw5GmSUsMfaSESPq+ZSrMELSUU35G3xe3qQ/i7mzAaGTPdMtNVtbU8v+mFbEO2dCBELlsJPNgTu0lUEQ30dKzwEUAM51p0FSyMJfiSwR5cdVUY7Vs/2raUTN3Y7Mk202QneBlJ8WDXqZLOydmWwamAswQ5wIvrrf8YeGBTijckogb3IAxOli92B1FmydT0TKjNb4fbk32PLT8M6maNeeiHJiMKxUDTABRKRz57AH2rRmUm+qvWqXsS3TMviC7Nbsg2OiSFQsx9xIDvCviXiwqlHCsnWTk2RZrONbW5i2rTuaVngbd3tMvg2lAo0dzkbIw+EhJXJj1CizRhRSKWuWS3E= 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 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==NULL, capture the errno instead. Reviewed-by: Mike Kravetz Signed-off-by: Peter Xu Reviewed-by: David Hildenbrand --- mm/hugetlb.c | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index f75f5e78ff0b..27367edf5c72 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 = 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; hugetlb_vma_lock_read(vma); pte = hugetlb_walk(vma, haddr, huge_page_size(h)); @@ -6478,8 +6472,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 ((flags & FOLL_WRITE) && !huge_pte_write(entry)) { + page = NULL; + goto out; + } + + if (gup_must_unshare(vma, flags, page)) { + /* Tell the caller to do unsharing */ + page = ERR_PTR(-EMLINK); + 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. @@ -6489,8 +6496,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 = NULL; + ret = try_grab_page(page, flags); + + if (WARN_ON_ONCE(ret)) { + page = ERR_PTR(ret); goto out; } }