Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp2694376rwb; Wed, 30 Nov 2022 09:37:17 -0800 (PST) X-Google-Smtp-Source: AA0mqf4xco6EpYlbptZbknWHxueEt+RRQuipruRTvPPoa1PjKOL7pU+wNP/KgJnaUy5/HtCxvzeY X-Received: by 2002:a17:906:9f07:b0:7be:2fbd:828f with SMTP id fy7-20020a1709069f0700b007be2fbd828fmr17581794ejc.593.1669829836798; Wed, 30 Nov 2022 09:37:16 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669829836; cv=none; d=google.com; s=arc-20160816; b=Wiqy5qRquE9t9ZSwl6NTrLZ6KH1q2+qcRtlmkj2cNgSaxyRq6sdRZU9hffXnXlb/DA gwznAVmAJFTA0AwM/XNeNLlSX2G4k9/9+WIhlA1+8gJJx7jAzPkHxx4nr0x35+2z+hke cd3K1QepKQSOfc2iEberrh+ZVI+nIvIOK7UE385zmwKzAGSresaDwomkjz27Agfu1UKn jPRsTLLgxlHEc9PTNYoqed0aUV8VTnSB2voC8bUqGXd87ZsF6pb7fmoIio7L0x+inYeR va3fsSmLmKzod3EzrCgUJSW4z19GWLXesFwwWpYeAS490ttf3p2+j5rFlQEl3mJMe8dO qM1g== 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=OyR/MXm0lMpBX9jSEqneOMrJEYTZcm5iKu/+2H/UcuY=; b=IhL99J0xpZmLNU9EfYr5NHEmW3QD8TyzopoU9pBkIeIKdjsisKq0TusTSm+IW67uEe LgOSjgGgZ8rnghOnaR7PxA+xIzH5llic8xK+Yph4uIstEDSYDw2YGHdzQx30mZZYtgRi 6pgH8Ko8vdVfZmyq+h0SyTUeyPRB7MbRPtkb0KJHR2/4XTdn8+y6HxKIUiEE80q5JLYF w/yd4qSauH1ZGECzkCyMBdRg8ii2Zy93Jt18hO6EqrJvvkA4vbpmYKwgWvPTu5nohDK0 rBZajp3JiWYLLDQOiZmMexOgUK8507uvRQf6xYauWjsOCtK+Jblv+Gti4PWWZx8usrbd amUQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=JO2lTmE5; 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 qa13-20020a170907868d00b007c08a2c2409si1910437ejc.77.2022.11.30.09.36.56; Wed, 30 Nov 2022 09:37:16 -0800 (PST) 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=JO2lTmE5; 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 S230132AbiK3QKT (ORCPT + 83 others); Wed, 30 Nov 2022 11:10:19 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60532 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230131AbiK3QKN (ORCPT ); Wed, 30 Nov 2022 11:10:13 -0500 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 273F12CE3E for ; Wed, 30 Nov 2022 08:09:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1669824554; 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=OyR/MXm0lMpBX9jSEqneOMrJEYTZcm5iKu/+2H/UcuY=; b=JO2lTmE55mzuLDtQkeebLbhtaxAhdIQgv9Azud5urUq0iBcfUzZL048XPAmawaMIGeXaRG fXBEEhMkCzSYPEC2hAhcABA+aAM13CwbBvOHacoVd+qhzBCqbqj0D759ZH8BBMoxeqeXCB +MsVT1xk2f4/kGuVYWxeYMt8fqLJhB0= Received: from mail-qk1-f197.google.com (mail-qk1-f197.google.com [209.85.222.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-344-_cHd1FCbOhW9ZW2yfcO-lA-1; Wed, 30 Nov 2022 11:09:13 -0500 X-MC-Unique: _cHd1FCbOhW9ZW2yfcO-lA-1 Received: by mail-qk1-f197.google.com with SMTP id bs7-20020a05620a470700b006fac7447b1cso40195671qkb.17 for ; Wed, 30 Nov 2022 08:09:12 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=OyR/MXm0lMpBX9jSEqneOMrJEYTZcm5iKu/+2H/UcuY=; b=dwPm7mmhdDCVJ+tPe1E0JueHW42i1SdjU6shwTBzzE9wWLuyyTzgCaW+83L+//R5Gs Hw38j/HFaYB9dMxmDKZecxFUcplltDn6IQEoAbCaVULnAqx58mpAcJZ5btUtXMyJRkkQ w5Z6qsIjsHwcgizLxziwck2gBSdwETNRtLNbkKVptWCzZbU4sc3hPgzTFZjrvzMf7/6W WLNCzMG6lygophjU0VO6kwIwRrGg2wC5pDbqC0Q2rnodoD57abBH3qfof0AY2/s/MN+y YwgtY0iL6mxxYG18Y7xQbaNJ864ZLUcXOLIy3EzjLZP1GZoUqlcW7T69o8emAu/vvD+U 03Zg== X-Gm-Message-State: ANoB5pmrhXqzC9yaeGD60l/Piti4UUFim7bB6gN7V6jmmLUPxtVaXT0r VK3Fll5xsb/7lPBuQU0P+eLbrRbDufk3xfPII6lp56pUQHy2ZhxZ69fweuYIEbZsmJbEkiN+AaP wM6BxjKoMMigLC5LmDAaS2Exq X-Received: by 2002:ac8:70c1:0:b0:3a6:68cb:cabe with SMTP id g1-20020ac870c1000000b003a668cbcabemr26389369qtp.248.1669824552503; Wed, 30 Nov 2022 08:09:12 -0800 (PST) X-Received: by 2002:ac8:70c1:0:b0:3a6:68cb:cabe with SMTP id g1-20020ac870c1000000b003a668cbcabemr26389341qtp.248.1669824552160; Wed, 30 Nov 2022 08:09:12 -0800 (PST) Received: from x1n (bras-base-aurron9127w-grc-46-70-31-27-79.dsl.bell.ca. [70.31.27.79]) by smtp.gmail.com with ESMTPSA id s12-20020a05620a0bcc00b006ee7e223bb8sm1478164qki.39.2022.11.30.08.09.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 30 Nov 2022 08:09:11 -0800 (PST) Date: Wed, 30 Nov 2022 11:09:10 -0500 From: Peter Xu To: David Hildenbrand Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, James Houghton , Jann Horn , Andrew Morton , Andrea Arcangeli , Rik van Riel , Nadav Amit , Miaohe Lin , Muchun Song , Mike Kravetz Subject: Re: [PATCH 03/10] mm/hugetlb: Document huge_pte_offset usage Message-ID: References: <20221129193526.3588187-1-peterx@redhat.com> <20221129193526.3588187-4-peterx@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: 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_H2,SPF_HELO_NONE,SPF_NONE 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 Wed, Nov 30, 2022 at 11:24:34AM +0100, David Hildenbrand wrote: > On 29.11.22 20:35, Peter Xu wrote: > > huge_pte_offset() is potentially a pgtable walker, looking up pte_t* for a > > hugetlb address. > > > > Normally, it's always safe to walk a generic pgtable as long as we're with > > the mmap lock held for either read or write, because that guarantees the > > pgtable pages will always be valid during the process. > > > > But it's not true for hugetlbfs, especially shared: hugetlbfs can have its > > pgtable freed by pmd unsharing, it means that even with mmap lock held for > > current mm, the PMD pgtable page can still go away from under us if pmd > > unsharing is possible during the walk. > > > > So we have two ways to make it safe even for a shared mapping: > > > > (1) If we're with the hugetlb vma lock held for either read/write, it's > > okay because pmd unshare cannot happen at all. > > > > (2) If we're with the i_mmap_rwsem lock held for either read/write, it's > > okay because even if pmd unshare can happen, the pgtable page cannot > > be freed from under us. > > > > Document it. > > > > Signed-off-by: Peter Xu > > --- > > include/linux/hugetlb.h | 32 ++++++++++++++++++++++++++++++++ > > 1 file changed, 32 insertions(+) > > > > diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h > > index 551834cd5299..81efd9b9baa2 100644 > > --- a/include/linux/hugetlb.h > > +++ b/include/linux/hugetlb.h > > @@ -192,6 +192,38 @@ extern struct list_head huge_boot_pages; > > pte_t *huge_pte_alloc(struct mm_struct *mm, struct vm_area_struct *vma, > > unsigned long addr, unsigned long sz); > > +/* > > + * huge_pte_offset(): Walk the hugetlb pgtable until the last level PTE. > > + * Returns the pte_t* if found, or NULL if the address is not mapped. > > + * > > + * Since this function will walk all the pgtable pages (including not only > > + * high-level pgtable page, but also PUD entry that can be unshared > > + * concurrently for VM_SHARED), the caller of this function should be > > + * responsible of its thread safety. One can follow this rule: > > + * > > + * (1) For private mappings: pmd unsharing is not possible, so it'll > > + * always be safe if we're with the mmap sem for either read or write. > > + * This is normally always the case, IOW we don't need to do anything > > + * special. > > Maybe worth mentioning that hugetlb_vma_lock_read() and friends already > optimize for private mappings, to not take the VMA lock if not required. Yes we can. I assume this is not super urgent so I'll hold a while to see whether there's anything else that needs amending for the documents. Btw, even with hugetlb_vma_lock_read() checking SHARED for a private only code path it's still better to not take the lock at all, because that still contains a function jump which will be unnecesary. Thanks, -- Peter Xu