Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp16420611rwd; Mon, 26 Jun 2023 09:44:11 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5iVVWTjNh9Dnx3QYDEmI3/YCR0W8jCSYbvHbB1vR6oU4YOFw3hCTZ8mATbc/Nwk2GPNbnv X-Received: by 2002:a17:902:ec8f:b0:1b4:5697:d9a8 with SMTP id x15-20020a170902ec8f00b001b45697d9a8mr4558539plg.24.1687797851140; Mon, 26 Jun 2023 09:44:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687797851; cv=none; d=google.com; s=arc-20160816; b=AL0wRaEcyr9VIFNqdm/juY8HXI0kSqzjHWhPDGHvqOMlCf1iMN83SGfTR1o/y9f9Z9 3htnGm0NbthYpYeu5PA7RDvBHnjhZVZ7qS28AU2c9IpZcqD38gq+Sz/lWA3RvSTKfLuL k/vOgTrSOX7vIZiGDiorHtf7Z8VdBoEgGxShHKNyOhKiPxFDMhYaKTi9tiUHNXprxM4y tpxXL4NQbA0zEWAeArP1aw2xJu76TGKtU6gusabJlRRVYoeX9+2raJdqToOjmpHzGyej /fUnNAH/EdcPrTPxMmLstQYxsYs16moEQepCV40DXrOvQZ8iiiKxtyWEz/u+r53aedSk sDUQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=igMfXFR5kNoI10TQDurWKUzwN6Z45JekZK2Q72EILNo=; fh=sR2QF2XiyXDjqrgdL2F8WjAC69kQ2IAawteu7VTEIdI=; b=FRCqpXlBOzNCwgX79ooOi0F7LUhY0GoF8MpcgbqCkdOjRgpWmuP44ju21pMtI0xKEa KTs68RUH/cihjraQw7GOcegfj598nOGXSZVVkDVq5mOp6xeM87LYATnXwbaqunh5yo28 t7rwgDv1csnLn9DCpcM4tFhKLFqCAMyKTMnaFx/hNOYG2aTmLFrawkurt6xfL34IIHYV Ou7Jwm5zg1ifiH+F8BQTIzRgLdNyGhHvBohdraB8UmJx0zV73NcWmumRu2reJ7/G86Ka DI2l98TKvtM9+N79Ls+a+aOpDCb6J6udOS48O9Cc7oSoHozg7t36dEgoqDHLj6eTNCH3 CBUg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=GaDyr3cV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ba12-20020a170902720c00b001b514752ef9si5050185plb.338.2023.06.26.09.43.57; Mon, 26 Jun 2023 09:44:11 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=GaDyr3cV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230241AbjFZQZb (ORCPT + 99 others); Mon, 26 Jun 2023 12:25:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52796 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231558AbjFZQYk (ORCPT ); Mon, 26 Jun 2023 12:24:40 -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 00BE22107 for ; Mon, 26 Jun 2023 09:23:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1687796586; 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: in-reply-to:in-reply-to:references:references; bh=igMfXFR5kNoI10TQDurWKUzwN6Z45JekZK2Q72EILNo=; b=GaDyr3cVu1DNKsQ8pqoXBjwzdBQgtkJsV/H4MtFLVgjE1uzKebyqeODdCoT8GTs97WtG4B 6r1y21BMTtOqHUB+eW9sWEPF3Nve2ZIss0CTkzxwtoEbXjk5k+xNllZYXguX4uWx7Rafvb L4Vbmgfyy9XqGQ/9rNvMQWA7l7dMGOQ= Received: from mail-qv1-f71.google.com (mail-qv1-f71.google.com [209.85.219.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-620-t9xs6tmKORif65ww_Ke-bA-1; Mon, 26 Jun 2023 12:23:03 -0400 X-MC-Unique: t9xs6tmKORif65ww_Ke-bA-1 Received: by mail-qv1-f71.google.com with SMTP id 6a1803df08f44-62ffa1214edso6296736d6.0 for ; Mon, 26 Jun 2023 09:23:03 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687796582; x=1690388582; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=igMfXFR5kNoI10TQDurWKUzwN6Z45JekZK2Q72EILNo=; b=P7ZwJhmf+u1xy1vlxTCIUimGX80E0UmCBvpL5m9OQ0b6fqrF8kEh2UbKdO8Kvrcrhj cI3tkbWUdV/StqTcSn7/QYkJyX/3dxoxoj083qV36rJlBOgTh/f5RAOygXWYmuJdI08g s50GPIpBYvISrinw4ZwswrnGh13V8zh+wdigonIuAMeOtHUwPixQ1uCPcZHM+GAoTKs3 64dB6T9rwzG7nSfCqJ8UXqF2ty2WWTCw7Vzbv8JTJImsborF9uk7fCSPMsBxOnC0OyZJ ZpELx6WG4kj+YfcHj7/xQk+onQv0sYb1gsub0R3OB5GL288CUON/S6EPM6NDv4ZvZfuj ASxg== X-Gm-Message-State: AC+VfDzBYeFwAqHp0AB4zwGMTp3C/qr6Dj9ClWjFn+bh1D6kmqO9xTfN Yvg41n/u33fEI1dOyRoR+o1tAveENcO9kI0CG9ZE/lmlmkCGC9lOjidq13zm0brYB6+YzzP6Uux ZJ6X41kBkNn9/eg9c4L/12TLw48KBJI/5 X-Received: by 2002:a05:6214:27eb:b0:616:870c:96b8 with SMTP id jt11-20020a05621427eb00b00616870c96b8mr36132629qvb.3.1687796582595; Mon, 26 Jun 2023 09:23:02 -0700 (PDT) X-Received: by 2002:a05:6214:27eb:b0:616:870c:96b8 with SMTP id jt11-20020a05621427eb00b00616870c96b8mr36132612qvb.3.1687796582255; Mon, 26 Jun 2023 09:23:02 -0700 (PDT) Received: from x1n (cpe5c7695f3aee0-cm5c7695f3aede.cpe.net.cable.rogers.com. [99.254.144.39]) by smtp.gmail.com with ESMTPSA id f19-20020ae9ea13000000b007592174cb08sm2864932qkg.10.2023.06.26.09.23.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Jun 2023 09:23:01 -0700 (PDT) Date: Mon, 26 Jun 2023 12:23:00 -0400 From: Peter Xu To: David Hildenbrand Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Lorenzo Stoakes , John Hubbard , Andrew Morton , Mike Rapoport , Yang Shi , Andrea Arcangeli , Vlastimil Babka , "Kirill A . Shutemov" , James Houghton , Matthew Wilcox , Mike Kravetz , Hugh Dickins , Jason Gunthorpe Subject: Re: [PATCH v3 2/8] mm/hugetlb: Prepare hugetlb_follow_page_mask() for FOLL_PIN Message-ID: References: <20230623142936.268456-1-peterx@redhat.com> <20230623142936.268456-3-peterx@redhat.com> <89fee4bf-29a1-db19-e0ae-dd827d277504@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <89fee4bf-29a1-db19-e0ae-dd827d277504@redhat.com> X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Jun 26, 2023 at 10:06:24AM +0200, David Hildenbrand wrote: > On 23.06.23 16:29, Peter Xu wrote: > > 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 > > --- > > 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; > > + } > > > No need to check if the page is writable (like all other callers and as > gup_must_unshare() documents -- "for which pages that are write-protected in > the page table") > > if (!huge_pte_write(entry) && gup_must_unshare(vma, flags, page)) { Sure. I was wondering whether we should just allow passing in "write" into gup_must_unshare(), it'll just be a bit weird that it'll return false directly if write, meanwhile hopefully that makes it easier to be consistent. I'll leave that as-is for now, anyway. For this one I'll just merge it into: if (!huge_pte_write(entry)) { if (flags & FOLL_WRITE) { page = NULL; goto out; } if (gup_must_unshare(vma, flags, page)) { /* Tell the caller to do unsharing */ page = ERR_PTR(-EMLINK); goto out; } } > > > With that > > Reviewed-by: David Hildenbrand Thanks, -- Peter Xu