From mboxrd@z Thu Jan 1 00:00:00 1970 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 4399DC4321E for ; Wed, 30 Nov 2022 16:09:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BABB06B0074; Wed, 30 Nov 2022 11:09:15 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B5B706B0075; Wed, 30 Nov 2022 11:09:15 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A24456B0078; Wed, 30 Nov 2022 11:09:15 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 96B856B0074 for ; Wed, 30 Nov 2022 11:09:15 -0500 (EST) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 545EAA10BE for ; Wed, 30 Nov 2022 16:09:15 +0000 (UTC) X-FDA: 80190593070.07.5D2D5F1 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf29.hostedemail.com (Postfix) with ESMTP id F0A39120019 for ; Wed, 30 Nov 2022 16:09:14 +0000 (UTC) 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-qt1-f199.google.com (mail-qt1-f199.google.com [209.85.160.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-139-RXynMnsGMeCFx4bTJvkdGA-1; Wed, 30 Nov 2022 11:09:13 -0500 X-MC-Unique: RXynMnsGMeCFx4bTJvkdGA-1 Received: by mail-qt1-f199.google.com with SMTP id cj6-20020a05622a258600b003a519d02f59so28999699qtb.5 for ; Wed, 30 Nov 2022 08:09:13 -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=shqT9r4wqV3nrxxFoe2gpRt00ahwuGqSoD0I0GcyvL5ZYMI0hk0cZQdQ3oR05dhGrh auYgEQ9shM9aa51wsNw7B0R5gnwy0EKDfafLpkllnhU2MjWlRb+frgBN5pmMOrhnl5Ni H/X9Wd/bccNKY8frDuF+5ryqmSDFEQaTyLUuG5p1nqiNvcaDqRh5erjJOJ5l9pXPeSsM mz2FikvdM+7YY4YL0Qy3XcUGzRoZBjrjxyqM5EhgBixSVY+cHc53H9mVsQ3VazTV//bd oQeBclX4a//SrLqbe5ubmgDReEqQa8FRD7M+NGKA5jiOhve5IKUGZri1dItS2k6WBjCB CC2w== X-Gm-Message-State: ANoB5pkR9L5Do2vFiGW4TqoxHZ5C3mNoUEcz5g8aCN31XGn28Y/qvN3q YDn4P0hMX3olvvtSUfOZHKrB0wq68WiRwjgQaaJzIuQGAxuo7cNn6LG+AHr8cqLUpVCO9A8A/Ns VqyEUU8AhNFU= X-Received: by 2002:ac8:70c1:0:b0:3a6:68cb:cabe with SMTP id g1-20020ac870c1000000b003a668cbcabemr26389377qtp.248.1669824552507; Wed, 30 Nov 2022 08:09:12 -0800 (PST) X-Google-Smtp-Source: AA0mqf7+5FApY5Lkzsvu8FwhWz5OwpNKnQmBNUfsPsPf9nFQYtWE+tl59aWe1keTgdGtYvWUDLX5tg== 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 In-Reply-To: X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8 Content-Disposition: inline ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1669824555; a=rsa-sha256; cv=none; b=BAicdDQoeCv9Ini2+poUXwPUFUIpOYy9DnZhmPr0PMyGutiUOxdaN7nKV8B/bCp/662M0h ZcZe7ggOTAM63PvZv6ANFqHGXS/BcIS9COngJDpub8hGpXdx5SP6cSSN6lOiVMtHJQ4z57 hdUXWycVfjfy7k3Q4gVz7IsbFenJHpM= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=JO2lTmE5; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf29.hostedemail.com: domain of peterx@redhat.com designates 170.10.129.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=1669824555; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=OyR/MXm0lMpBX9jSEqneOMrJEYTZcm5iKu/+2H/UcuY=; b=vngUg1eSHOOW79l8XQoVSeAupuRDCEPMCbVjW6oUgUKrXJpMM/Uu5UMSdyYZXAKmIy4SQ1 o3cPbM79QfYKwG6XK1aPpGVRANCL88xYz+9W2/7kavNQbtDwFVs+HVRQOvp292Ph3q2YG9 mzyXmd4IxGXF51kLfArzey/K5JnuF/I= X-Stat-Signature: adr835yacuznj4mhdbszjg87krytu7k4 X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: F0A39120019 Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=JO2lTmE5; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf29.hostedemail.com: domain of peterx@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=peterx@redhat.com X-Rspam-User: X-HE-Tag: 1669824554-288316 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: 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