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 8F2E5C4332F for ; Thu, 8 Dec 2022 13:05:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DF60B8E0003; Thu, 8 Dec 2022 08:05:46 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D7EE48E0001; Thu, 8 Dec 2022 08:05:46 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BF9AD8E0003; Thu, 8 Dec 2022 08:05:46 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id AE38C8E0001 for ; Thu, 8 Dec 2022 08:05:46 -0500 (EST) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 85113A0B43 for ; Thu, 8 Dec 2022 13:05:46 +0000 (UTC) X-FDA: 80219161092.06.C5B227B Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf01.hostedemail.com (Postfix) with ESMTP id 4F68A4000F for ; Thu, 8 Dec 2022 13:05:42 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=B1atHWQy; spf=pass (imf01.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@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=1670504743; 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=G4cFc8Temq+w0DYrWdjMcxtoGARvIjvLcHmmwSHCKIw=; b=vAgzn7X68aBMsXalkrVTSP0EMrmAVHldJ5tNyhvoCaHx889jfnb6u3tSVCI5sQpjBQAwa3 ldRN2ti0lW/d5udXH1M7eeX/4knw6U5mSRhoeDv9+1wpc+jo+xRlk4J72NmuWRR9MNDYQa 6yHcS//bGNZ2GYqoUBWRHGTxPGn7i+s= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=B1atHWQy; spf=pass (imf01.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1670504743; a=rsa-sha256; cv=none; b=PYsD7W8pS7FiHQRlYe9hacFOeTvqk4C6glhRx5XZdbstV/vPlWLFnNSv0ZQFo/yGd9Dn4R Pz7hK2HG/lWbrN6DI1p0Mr8uSPsHIwp6dOuJ6b3o7Em1F2BwiiCvInhk2vHTgVBHtcVNk7 CyL/zEwARGzTERXcN6o3IoCfgPUZB8I= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1670504741; 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=G4cFc8Temq+w0DYrWdjMcxtoGARvIjvLcHmmwSHCKIw=; b=B1atHWQyp5MUJlMpSOzZ3wUkvqQI86odr2FQpyy/Lo+0h9XTAlcyHGWqfsDA27aRjS1sjz lsCTLN7xVvnFt5JZttDNx93Cqc6yXlfrQsF3PF7u0TDaKRJ7SimgoHfJt9dDAaBDjkmsdn jYOZxRAegnh3YV9jwz7PM2yvQ681Wts= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-613-Dmgj8aTiOYqwOjB4EJfpnQ-1; Thu, 08 Dec 2022 08:05:38 -0500 X-MC-Unique: Dmgj8aTiOYqwOjB4EJfpnQ-1 Received: by mail-wm1-f71.google.com with SMTP id a6-20020a05600c224600b003d1f3ed49adso609399wmm.4 for ; Thu, 08 Dec 2022 05:05:37 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:organization:from:references :cc:to:content-language:subject:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=G4cFc8Temq+w0DYrWdjMcxtoGARvIjvLcHmmwSHCKIw=; b=zwCRcn81sFducAtkQISKHXZzNo3yF8O90mGFeCnbtLiytWSlGPaVjFmPS3TQvTra+t CIcxZYtMgFBkk7GJLn89amR6qbWkzkqV9XjiFw+9GUbf4xenwniL6JcPWUObCC5QgNkt DJxoanJ/m51IpWxYEUNiuW4E8LbehsEgxCQDule1vcT/AOiJWk2drd2T40C2Uv0seT/Z 4418/Be1j6h8g9fn2HcPckRfzuqyX3t0IAfpJ2cQSetc0cRjjdENWkEsemW+X4HljKdF lJXimCzhBwXNa5ic3055t/hQzGgLVuj6Us7izMyQ1ADIFtmGyAhLn+6b1GETQ+KpOFwZ Py2A== X-Gm-Message-State: ANoB5pnx6dCoi6Ewii0u7bK8olaHm+c5ciiUaCc9JK4nahfXU6bb0Qid yZ5VMtSJVwKnhOHhbbjmcf1ypuX+CRChKkR5iAn/KuZysJD3wJvyOKerj/AHnZtZw6ebzxljcxh jDMG6Ga5fZN8= X-Received: by 2002:adf:a413:0:b0:242:1cbf:8f73 with SMTP id d19-20020adfa413000000b002421cbf8f73mr28184010wra.317.1670504736906; Thu, 08 Dec 2022 05:05:36 -0800 (PST) X-Google-Smtp-Source: AA0mqf5TGCakyMMVHnsf95161xPf76MW4Brp2FkZmMATCV3JFnGxzfGy7/DZIxZRlKyD5UxVm1NeKg== X-Received: by 2002:adf:a413:0:b0:242:1cbf:8f73 with SMTP id d19-20020adfa413000000b002421cbf8f73mr28183996wra.317.1670504736583; Thu, 08 Dec 2022 05:05:36 -0800 (PST) Received: from ?IPV6:2a09:80c0:192:0:5dac:bf3d:c41:c3e7? ([2a09:80c0:192:0:5dac:bf3d:c41:c3e7]) by smtp.gmail.com with ESMTPSA id l39-20020a05600c1d2700b003c6c4639ac6sm5444673wms.34.2022.12.08.05.05.35 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 08 Dec 2022 05:05:35 -0800 (PST) Message-ID: <63404406-1da7-901f-cec0-83746c9ea8a9@redhat.com> Date: Thu, 8 Dec 2022 14:05:34 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.5.0 Subject: Re: [PATCH v2 03/10] mm/hugetlb: Document huge_pte_offset usage To: John Hubbard , Peter Xu , linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Muchun Song , Andrea Arcangeli , James Houghton , Jann Horn , Rik van Riel , Miaohe Lin , Andrew Morton , Mike Kravetz , Nadav Amit References: <20221207203034.650899-1-peterx@redhat.com> <20221207203034.650899-4-peterx@redhat.com> From: David Hildenbrand Organization: Red Hat In-Reply-To: X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Rspamd-Queue-Id: 4F68A4000F X-Stat-Signature: 91ua6ydxdkn37ds4dyh3oonmnmdp9t4i X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1670504742-269136 X-HE-Meta: U2FsdGVkX1+2BzE+VZr2e3jndlDpY/x0pgHZbmb2wSzIU+obYYxAlK5dVkCyb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Pphq2TVOKp14h59vV4gItU6ClF7YLoY/2RFk6t+TagVPxYJzKWiI3zzMG+5RJbnduXox1eb5FUD9XI7FmC37w1x5qOy2rEMcvzX/TCrivT//mszWvYw= 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 07.12.22 21:49, John Hubbard wrote: > On 12/7/22 12:30, 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(+) > > Looks good, with a couple of minor wording tweaks below that you might > consider folding in, but either way, > > Reviewed-by: John Hubbard > >> >> 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: > > "responsible for" > >> + * >> + * (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. > > mmap sem is sooo two years ago! :) > >> + * This is normally always the case, IOW we don't need to do anything > > "normally always" hurts my sense of logic. And "IOW" is for typing very quickly > in chats or email, not for long term documentation that is written rarely > and read many times. > >> + * special. > > So putting all that together, maybe: > > * (1) For private mappings: pmd unsharing is not possible, so holding the > * mmap_lock for either read or write is sufficient. Most callers already > * hold the mmap_lock, so normally, no special action is required. With that, Reviewed-by: David Hildenbrand -- Thanks, David / dhildenb