From patchwork Fri Dec 9 17:00:58 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13070019 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 22D03C4332F for ; Fri, 9 Dec 2022 17:01:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B35518E000A; Fri, 9 Dec 2022 12:01:34 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id ABEE48E0001; Fri, 9 Dec 2022 12:01:34 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 938468E000A; Fri, 9 Dec 2022 12:01:34 -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 8245A8E0001 for ; Fri, 9 Dec 2022 12:01:34 -0500 (EST) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 3D7E81413B4 for ; Fri, 9 Dec 2022 17:01:34 +0000 (UTC) X-FDA: 80223384108.24.B7773A9 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf25.hostedemail.com (Postfix) with ESMTP id F07A2A0031 for ; Fri, 9 Dec 2022 17:01:31 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=WeMlD0vR; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf25.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=1670605292; 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=BJ1c+DnQwwOjSKsDq4iN/7ttySyX/vWFxv5LWeLTqBE=; b=VTGiVIjxh3S2LqG+8uqTtZXEXoCBjoGHbzKOXGbR6VgaiOknU1rwBrZ24wW0wp1IafNkn3 58rpYxxdYOD+kwFvadTBV5sf5CWws0139cA9uH/UcON8Lr0JusSSJ1usbTVlF6tbzlbY38 hrFgFE5yGjekImf/QHnuPtLc90SjWY8= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=WeMlD0vR; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf25.hostedemail.com: domain of peterx@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=peterx@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1670605292; a=rsa-sha256; cv=none; b=ekwRA298gnNy1dSOMS2DD+1VQLPfHDx+EdFyJ7XeLQ58aW6p/Npw5hzBrmR4Jsdko1K5dj A30lCQbfOuYdLBOoWryl1SqdtFnQ2VekOOaLXOkcHahqt25MiGMI8nvITeblvc/4Ql7w1J jte8/r+ymQc5tQ/QPZ7mrkLVvrtrAtg= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1670605291; 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=BJ1c+DnQwwOjSKsDq4iN/7ttySyX/vWFxv5LWeLTqBE=; b=WeMlD0vR4fwYR8sl/Td4qOC0OB9oB7/VVFoYTrOTYM7H6c63oM1AVExwD/KQFzXFXM6rHU mdsZN4uTVPFd/aZgluEEtptQgrry9GPfi3CZLy56BSf+epJdetgSwmv/NLlkowFI/ldnRf kK76FrQ0A8IIr28Vz4qriWW9ew40LdM= Received: from mail-oa1-f69.google.com (mail-oa1-f69.google.com [209.85.160.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-140-x9MavlnvNmWDjmG8KfBldA-1; Fri, 09 Dec 2022 12:01:27 -0500 X-MC-Unique: x9MavlnvNmWDjmG8KfBldA-1 Received: by mail-oa1-f69.google.com with SMTP id 586e51a60fabf-144da30bb39so140790fac.10 for ; Fri, 09 Dec 2022 09:01:27 -0800 (PST) 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=BJ1c+DnQwwOjSKsDq4iN/7ttySyX/vWFxv5LWeLTqBE=; b=TEh4smQ+xNNdngO1KlXY2xoqXqh9BZ2/zgXQsTj6MS7BgJoS0bzv83ZjzjunIVEbJ0 h7oFmUl0wR4BTUPKyuv8AMoGNaQ2hJ/qZfSKJaNhAaCY0gTG0KmBiKmsmKaHpLUc+FgW PYnFd01+wWDGFNg5mlK5PybcRwLc+F1Y4UgQKgpi7Zt8VJxNejUjJjwweg7gA+leQevL y6fQJnL7ok6v0kKs6PwQfKI005iABGCI3+lRMeRcdhJgmSS1DHMSiOpVtLMZoj2lg0nS 4bmiBF8ZpdoqNi9/G9/qBtFYrYzIF1QB1xQZNcgq+ELUhE1yoXTW18ALmHiEJ2uYxfe5 hvFA== X-Gm-Message-State: ANoB5pmHulMYt25pYb+8+SPzh7biz0X2AP7YNliquyL+soln9vvzmL5Q fa2BJF9SxC2Nn8sg5kkkwK2Z8RTSgnsh5990J7n+s0mFIFTtawWh78MzY0mT+qRM+dl5ST6NJWx hwLGmr4bEcLUiIucbLoEHb7TBHsExpBI2JevSLD/dlrAG/lxUzQDVCoaYBKRF X-Received: by 2002:a05:6870:2f0a:b0:141:fe19:d4d0 with SMTP id qj10-20020a0568702f0a00b00141fe19d4d0mr2656015oab.50.1670605285802; Fri, 09 Dec 2022 09:01:25 -0800 (PST) X-Google-Smtp-Source: AA0mqf67Y/YxUkSaXiObMUPvZ9/FDlwGcy+0g+8APbf7404nsBoydGf3R0e/VdazPQe3Ssti4e+l/Q== X-Received: by 2002:a05:6870:2f0a:b0:141:fe19:d4d0 with SMTP id qj10-20020a0568702f0a00b00141fe19d4d0mr2655965oab.50.1670605285082; Fri, 09 Dec 2022 09:01:25 -0800 (PST) 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 q7-20020a05620a0d8700b006cf38fd659asm178907qkl.103.2022.12.09.09.01.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Dec 2022 09:01:24 -0800 (PST) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Andrew Morton , Miaohe Lin , David Hildenbrand , Nadav Amit , peterx@redhat.com, Andrea Arcangeli , Jann Horn , John Hubbard , Mike Kravetz , James Houghton , Rik van Riel , Muchun Song Subject: [PATCH v3 7/9] mm/hugetlb: Make follow_hugetlb_page() safe to pmd unshare Date: Fri, 9 Dec 2022 12:00:58 -0500 Message-Id: <20221209170100.973970-8-peterx@redhat.com> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20221209170100.973970-1-peterx@redhat.com> References: <20221209170100.973970-1-peterx@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-type: text/plain X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: F07A2A0031 X-Stat-Signature: xc3acyg4hh9ahdwxkehy5o6eqmqqqhht X-HE-Tag: 1670605291-10991 X-HE-Meta: U2FsdGVkX18uxpfhmkoJBP2CYPk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yf2cu4h43SjJNdshnj+GSClZM1autJ7wqmn16mH7sJ+vO2tHJJjAveU0MQBWrqxvwuseZCrfrYnHJEt8lVcwZQtpcuHUUwsNXShRrYWh8Eoo9MEX2alOMhSkTwwfRd3I1y6FXBTk8JueA1FS/f2LVMSBlNNcNMFqBY= 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: Since follow_hugetlb_page() walks the pgtable, it needs the vma lock to make sure the pgtable page will not be freed concurrently. Acked-by: David Hildenbrand Reviewed-by: Mike Kravetz Reviewed-by: John Hubbard Signed-off-by: Peter Xu --- mm/hugetlb.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index e3af347470ac..9d8bb6508288 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -6285,6 +6285,7 @@ long follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma, break; } + hugetlb_vma_lock_read(vma); /* * Some archs (sparc64, sh*) have multiple pte_ts to * each hugepage. We have to make sure we get the @@ -6309,6 +6310,7 @@ long follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma, !hugetlbfs_pagecache_present(h, vma, vaddr)) { if (pte) spin_unlock(ptl); + hugetlb_vma_unlock_read(vma); remainder = 0; break; } @@ -6330,6 +6332,8 @@ long follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma, if (pte) spin_unlock(ptl); + hugetlb_vma_unlock_read(vma); + if (flags & FOLL_WRITE) fault_flags |= FAULT_FLAG_WRITE; else if (unshare) @@ -6389,6 +6393,7 @@ long follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma, remainder -= pages_per_huge_page(h); i += pages_per_huge_page(h); spin_unlock(ptl); + hugetlb_vma_unlock_read(vma); continue; } @@ -6416,6 +6421,7 @@ long follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma, if (WARN_ON_ONCE(!try_grab_folio(pages[i], refs, flags))) { spin_unlock(ptl); + hugetlb_vma_unlock_read(vma); remainder = 0; err = -ENOMEM; break; @@ -6427,6 +6433,7 @@ long follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma, i += refs; spin_unlock(ptl); + hugetlb_vma_unlock_read(vma); } *nr_pages = remainder; /*