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 2767EC433FE for ; Wed, 30 Nov 2022 16:25:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B9F8E6B0073; Wed, 30 Nov 2022 11:25:09 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B4FE66B0074; Wed, 30 Nov 2022 11:25:09 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A182B6B0075; Wed, 30 Nov 2022 11:25:09 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 951736B0073 for ; Wed, 30 Nov 2022 11:25:09 -0500 (EST) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 2CEAA12110A for ; Wed, 30 Nov 2022 16:25:09 +0000 (UTC) X-FDA: 80190633138.28.9611D10 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf18.hostedemail.com (Postfix) with ESMTP id B330E1C0017 for ; Wed, 30 Nov 2022 16:25:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1669825508; 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=OW4fKClhGHajD6zp9BCF2+LLlrB3sbikqsBRGd8LqJE=; b=CxTDumZSxnlITyJd4cNUEbGbymA/DA6Wilspadqlzai2KjowDZkJOLDn0sijCn9xKUj+v7 PTCgUE3bAE78+RAKevQFjcAYJ5qR3SYaRM6xe7VxZUhoNS9fTusjw85u8LQ1e3QJKIRoWK RjrlYioAjNlijJ+l0rQBSdh3nBU7sc8= Received: from mail-qv1-f69.google.com (mail-qv1-f69.google.com [209.85.219.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-624-Ed-floVTPeuNbq3g-NZzDA-1; Wed, 30 Nov 2022 11:25:06 -0500 X-MC-Unique: Ed-floVTPeuNbq3g-NZzDA-1 Received: by mail-qv1-f69.google.com with SMTP id d8-20020a0cfe88000000b004bb65193fdcso26748605qvs.12 for ; Wed, 30 Nov 2022 08:25:06 -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=OW4fKClhGHajD6zp9BCF2+LLlrB3sbikqsBRGd8LqJE=; b=rK/ZFjcp+MBApzDQYNw5PVRkMR3MUWPw2TvPkQ4TfsXlDbubdtp39cY9gLIw45rF0w RrWgspFPENCOM3fO1K8w81Gt8y6hTqHXYR1d3CT5Y2fmsPyhbd1liWqYX+szkOvL3ll4 dBhe2JDoiRHPimX1fshHpGeYbckYP6rE20HvNsqw8rCBDEHcR9OTRtH7mceuvxmW+ri3 m1aMOEfEMPmf4p76UwRkcG5bLjSHfhSWRBlgCw2S2U7xUqSyohjl25yPoFYEdpfNHzcx pI0mM7r80cPDwWp96p7jORZAoT3LWCW37o79DCZK6JNSwKS/gAviN/CfeSdMa0LHokV0 zKnA== X-Gm-Message-State: ANoB5plCA7ambc+in5tEO/L0IBiDptgBjjQ4YsJ85nVlp0x4gLdSLbWc pinKqWxMyoEpirmPoC5WdUa+3sWjyZxaDN12yofYvJ+yOQp4CsolaYHB74CBf9HTrRyvHpjjsjY A3CRM8vk/YSo= X-Received: by 2002:a37:9647:0:b0:6fa:2e16:b776 with SMTP id y68-20020a379647000000b006fa2e16b776mr53386520qkd.212.1669825506073; Wed, 30 Nov 2022 08:25:06 -0800 (PST) X-Google-Smtp-Source: AA0mqf7XniJ7VKQOaiBf5ZK8TmNW5rCfJvmhLFhhxgrlHoCHBFPF62lNS79qCLkdHGO2LPPXI7y3qQ== X-Received: by 2002:a37:9647:0:b0:6fa:2e16:b776 with SMTP id y68-20020a379647000000b006fa2e16b776mr53386491qkd.212.1669825505806; Wed, 30 Nov 2022 08:25:05 -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 bs42-20020a05620a472a00b006b61b2cb1d2sm1485856qkb.46.2022.11.30.08.25.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 30 Nov 2022 08:25:05 -0800 (PST) Date: Wed, 30 Nov 2022 11:25:04 -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=1669825508; a=rsa-sha256; cv=none; b=Bli42hQ4MD+LwWR9pVexETF01Ev1FuIV9ULqR92AuDzzUnyy8yy0xhBcAbnkDI5yvX3u8B J6Ai6qY99Qqpd9bgqIpmNMun3crhx8yhOQTOK1hYsQjMTlfc1+dvYqA6Cv8t/GSthA0yEn FRMBUPZUsi1TKaPNcsLMFGQdOBd/uxg= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=CxTDumZS; spf=pass (imf18.hostedemail.com: domain of peterx@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1669825508; 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=OW4fKClhGHajD6zp9BCF2+LLlrB3sbikqsBRGd8LqJE=; b=mJEGareIWbITw67DDBz+xdzUckKQXAE/yx0TO9e6SPsadtJoi32MxUMbzYhCoCfElA3KzU sKLsRzla/+qS7gKeBOEFHvtveg3IuS5dx5cmfCS1m+bPaiXr1cyaQTmUuuSpArOxuG3QOK WR5pwwE9hy7BGL1Db/vsd9uwpJn/uy8= X-Stat-Signature: 88s1cnhiox15cgumrsjiz75dpmabyrey X-Rspamd-Queue-Id: B330E1C0017 X-Rspam-User: Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=CxTDumZS; spf=pass (imf18.hostedemail.com: domain of peterx@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-Rspamd-Server: rspam05 X-HE-Tag: 1669825508-858413 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 05:11:36PM +0100, David Hildenbrand wrote: > On 30.11.22 17:09, Peter Xu wrote: > > 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. > > IMHO it makes coding a lot more consistent and less error-prone when not > care about whether to the the lock or not (as an optimization) and just > having this handled "automatically". > > Optimizing a jump out would rather smell like a micro-optimization. Or we can move the lock helpers into the headers, too. -- Peter Xu