From patchwork Sun Oct 30 21:30:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13025223 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 811F4FA3743 for ; Sun, 30 Oct 2022 21:30:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 22EC380007; Sun, 30 Oct 2022 17:30:51 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1B9BF6B0074; Sun, 30 Oct 2022 17:30:51 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 031FC80007; Sun, 30 Oct 2022 17:30:50 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id E5A6F6B0071 for ; Sun, 30 Oct 2022 17:30:50 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id AD875A0B52 for ; Sun, 30 Oct 2022 21:30:50 +0000 (UTC) X-FDA: 80078910660.07.F2F6A91 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf04.hostedemail.com (Postfix) with ESMTP id 4D4034002E for ; Sun, 30 Oct 2022 21:30:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1667165449; 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=MbdbMcNyE2RWt/K55AkSFMkKO4f3Jk1aXGE/HUSEbTg=; b=WWeMO2p3pXRJSKZ0YpYUfsaE8cijgITzNdHQLflllfQNHb1Zyt+3w3GbvixkP96ACef93D wib2wSM21kadKl3T29la0z8RMbVFr7ULR6+INFwOu9yBUXOwHpXzyWFvwPYIfao7GXiegp NeWgg5Z65H2JVFm+AhqYkIiVsC6LuhA= Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-232-XJjXpi7GMQezIjec9P468w-1; Sun, 30 Oct 2022 17:30:48 -0400 X-MC-Unique: XJjXpi7GMQezIjec9P468w-1 Received: by mail-qk1-f198.google.com with SMTP id bj1-20020a05620a190100b006fa12a05188so3848546qkb.4 for ; Sun, 30 Oct 2022 14:30:48 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=MbdbMcNyE2RWt/K55AkSFMkKO4f3Jk1aXGE/HUSEbTg=; b=GqtFW/zJYtcH/GtTjC6U5ZD2MOgZb3nj1QJhgOSgh+p7thS2r13Hu+W4sV79bgg8z4 QrPiXw4vfOW8pMIHHQQ216/owLC3K2htvZR8xhBb5TNydKD5Hl9sNqnAe/10Nx3pHEN0 tKP8tMDwVkV/BUlovA/YlX8GiM2OdapL/14eKIxWhZAm0WuyOfHmkQ88j0fU/ohXKwCC 4w8z9fViWROl7AI+ZXeA8QN5+hum1ZmzMa9wy+e+DWp1qhj7UjLV2UgUzkOYDSRprVbE pI+U1f7PcC1ilLnzv02Yy+YK2Gx93VtekVaOe0scS18jkul7CXYpPQuUo3TPM1UjF/Z6 Eelg== X-Gm-Message-State: ACrzQf0FqlVZmRd31yA2v1vx2y/RtbNoOEFgTdBV7ESknrEEYM2rJQXS Xpf8lTjYXxpBXR0D0KxQV2DFsFUGHUUHzDoyMCYKUYB0EuI2kOn39fOEBnWW/GzT4XuxqferNDi gS8kHAwPqIZU/PMfY2f7Pr8HqkEorGXJB4uT1KdIIKi9D0REmjXKYqd9npGwD X-Received: by 2002:a0c:e44d:0:b0:4bb:6814:cacc with SMTP id d13-20020a0ce44d000000b004bb6814caccmr8623265qvm.73.1667165448138; Sun, 30 Oct 2022 14:30:48 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6jOrHvdqiTcpqwmdfHOEF5EeLgusoteeQUtfIu8VXZPch/zK/jAZabG/q9ulUqzuJxcqfFYA== X-Received: by 2002:a0c:e44d:0:b0:4bb:6814:cacc with SMTP id d13-20020a0ce44d000000b004bb6814caccmr8623237qvm.73.1667165447749; Sun, 30 Oct 2022 14:30:47 -0700 (PDT) Received: from x1n.redhat.com (bras-base-aurron9127w-grc-46-70-31-27-79.dsl.bell.ca. [70.31.27.79]) by smtp.gmail.com with ESMTPSA id bk20-20020a05620a1a1400b006f3e6933bacsm3482823qkb.113.2022.10.30.14.30.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 30 Oct 2022 14:30:47 -0700 (PDT) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: James Houghton , Mike Kravetz , David Hildenbrand , Andrea Arcangeli , Rik van Riel , peterx@redhat.com, Andrew Morton , Muchun Song , Miaohe Lin , Nadav Amit Subject: [PATCH RFC 10/10] mm/hugetlb: Comment at rest huge_pte_offset() places Date: Sun, 30 Oct 2022 17:30:45 -0400 Message-Id: <20221030213045.335680-1-peterx@redhat.com> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20221030212929.335473-1-peterx@redhat.com> References: <20221030212929.335473-1-peterx@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-type: text/plain ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1667165450; a=rsa-sha256; cv=none; b=Ir/nPLwjBGF7TUloGzRHofK7vWpbUktwZVFoNWa4j/d67N8dlYTgWyz4qmcsa+9eCGtSg2 m/i+3Eb1DhNTDSHwr5B5gqjrP+NE+n98Bf2ZN9+eXOX6sfjEJPZYY9Mm+T9mxlxL3/Ro/L TyxcaJxYbwq8uloYNkWNo1mgcXW/9aE= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=WWeMO2p3; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf04.hostedemail.com: domain of peterx@redhat.com designates 170.10.133.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=1667165450; 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=MbdbMcNyE2RWt/K55AkSFMkKO4f3Jk1aXGE/HUSEbTg=; b=L3UQF12b7DqbKMwVlF9loIw4m+PcMZjqZBfSLb954+sdLOZ9dVxtRIt03jw1ClVbmVahMd UytYWCb3JaVmuaW7n3TQgAfL3d+nN4Yq+z54+RDFVsLsov7mJ8f7yd+efSdCSL0wcl+4mW bRSz+bIMEIOZAJ1quu0LQdfkQT3Rlmo= X-Rspam-User: Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=WWeMO2p3; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf04.hostedemail.com: domain of peterx@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=peterx@redhat.com X-Stat-Signature: xdo6otodctaub1qgq73akciqex8q8zft X-Rspamd-Queue-Id: 4D4034002E X-Rspamd-Server: rspam06 X-HE-Tag: 1667165450-358850 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: This makes sure that we're covering all the existing huge_pte_offset() callers and mention why they are safe regarding to pmd unsharing. Signed-off-by: Peter Xu --- mm/hugetlb.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 6d336d286394..270bfc578115 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -4822,6 +4822,7 @@ int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src, last_addr_mask = hugetlb_mask_last_page(h); for (addr = src_vma->vm_start; addr < src_vma->vm_end; addr += sz) { spinlock_t *src_ptl, *dst_ptl; + /* With vma lock held, safe without RCU */ src_pte = huge_pte_offset(src, addr, sz); if (!src_pte) { addr |= last_addr_mask; @@ -5026,6 +5027,7 @@ int move_hugetlb_page_tables(struct vm_area_struct *vma, hugetlb_vma_lock_write(vma); i_mmap_lock_write(mapping); for (; old_addr < old_end; old_addr += sz, new_addr += sz) { + /* With vma lock held, safe without RCU */ src_pte = huge_pte_offset(mm, old_addr, sz); if (!src_pte) { old_addr |= last_addr_mask; @@ -5097,6 +5099,7 @@ static void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct last_addr_mask = hugetlb_mask_last_page(h); address = start; for (; address < end; address += sz) { + /* With vma lock held, safe without RCU */ ptep = huge_pte_offset(mm, address, sz); if (!ptep) { address |= last_addr_mask; @@ -5402,6 +5405,7 @@ static vm_fault_t hugetlb_wp(struct mm_struct *mm, struct vm_area_struct *vma, mutex_lock(&hugetlb_fault_mutex_table[hash]); hugetlb_vma_lock_read(vma); spin_lock(ptl); + /* With vma lock held, safe without RCU */ ptep = huge_pte_offset(mm, haddr, huge_page_size(h)); if (likely(ptep && pte_same(huge_ptep_get(ptep), pte))) @@ -5440,6 +5444,7 @@ static vm_fault_t hugetlb_wp(struct mm_struct *mm, struct vm_area_struct *vma, * before the page tables are altered */ spin_lock(ptl); + /* With vma lock (and even pgtable lock) held, safe without RCU */ ptep = huge_pte_offset(mm, haddr, huge_page_size(h)); if (likely(ptep && pte_same(huge_ptep_get(ptep), pte))) { /* Break COW or unshare */ @@ -6511,6 +6516,7 @@ unsigned long hugetlb_change_protection(struct vm_area_struct *vma, last_addr_mask = hugetlb_mask_last_page(h); for (; address < end; address += psize) { spinlock_t *ptl; + /* With vma lock held, safe without RCU */ ptep = huge_pte_offset(mm, address, psize); if (!ptep) { address |= last_addr_mask; @@ -7060,7 +7066,14 @@ pte_t *huge_pmd_share(struct mm_struct *mm, struct vm_area_struct *vma, saddr = page_table_shareable(svma, vma, addr, idx); if (saddr) { + /* + * huge_pmd_share() (or say its solo caller, + * huge_pte_alloc()) always takes the hugetlb vma + * lock, so it's always safe to walk the pgtable of + * the process, even without RCU. + */ spte = huge_pte_offset(svma->vm_mm, saddr, + vma_mmu_pagesize(svma)); if (spte) { get_page(virt_to_page(spte)); @@ -7420,6 +7433,7 @@ void hugetlb_unshare_all_pmds(struct vm_area_struct *vma) hugetlb_vma_lock_write(vma); i_mmap_lock_write(vma->vm_file->f_mapping); for (address = start; address < end; address += PUD_SIZE) { + /* With vma lock held, safe without RCU */ ptep = huge_pte_offset(mm, address, sz); if (!ptep) continue;