From patchwork Fri Dec 16 15:52:23 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13075139 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 B3321C4332F for ; Fri, 16 Dec 2022 15:52:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 515B08E0007; Fri, 16 Dec 2022 10:52:31 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4C59C8E0002; Fri, 16 Dec 2022 10:52:31 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 27DC38E0007; Fri, 16 Dec 2022 10:52:31 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 06B4A8E0002 for ; Fri, 16 Dec 2022 10:52:31 -0500 (EST) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id BFE6880234 for ; Fri, 16 Dec 2022 15:52:30 +0000 (UTC) X-FDA: 80248611660.24.5A3421A Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf08.hostedemail.com (Postfix) with ESMTP id D2D9716001B for ; Fri, 16 Dec 2022 15:52:28 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=IHHOUX2z; spf=pass (imf08.hostedemail.com: domain of peterx@redhat.com designates 170.10.129.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=1671205948; 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=rIRGvziR/92Z54UoPp5pwcNYu+IBI1cm3v25qmGTr7U=; b=VHV1mG5KynerjSppZ9tZwr/fGyr12AXJgmnEMN8sdKFKW0Zx5uvIYQ3OsSnljSX6fk1XWx mVTZkpkAQlCv7hz2XWXL3pobCZBBNjU/JrMA4Khlzd4PX9ebEcnsJoQuqunoQOZ0TRvSNN B3oSi6O8AVbYKPN4vNjb0g9uQwPRaUw= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=IHHOUX2z; spf=pass (imf08.hostedemail.com: domain of peterx@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1671205948; a=rsa-sha256; cv=none; b=yQIX7cs7CAXFUr3FBOa/aB4imgbbsH62BoW+OXmBHbaUk/bSCAVFyBlLPUymsT+viSC7dO oHlZP9vdcJCK5RW76q3/DXAsdQcPxngv0JdEX/bs5nW7rMy86H18Y00pKYECV812Nv8ZAK gkEydoXjUeDFUX+9GO/yFGpqx/06AmA= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1671205948; 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=rIRGvziR/92Z54UoPp5pwcNYu+IBI1cm3v25qmGTr7U=; b=IHHOUX2zHuY7iyny7BCjYIhp5LfU+qUWgZi1FdSW1h1BZRgEfEAinmOa4XLVb+B7Rl1vEZ FL5q+N5VG/F5JnmJkIF4WpYL5MVHOKy71HHGa7n+J7JMVEhHvHTZ0R0dLJPgVK8R5lCIO7 +bE8Asm+Yoj2S05s6GNItt7FOp2r5B8= Received: from mail-qk1-f200.google.com (mail-qk1-f200.google.com [209.85.222.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-613-UJkRhB8LOcKi3989o7dI6A-1; Fri, 16 Dec 2022 10:52:27 -0500 X-MC-Unique: UJkRhB8LOcKi3989o7dI6A-1 Received: by mail-qk1-f200.google.com with SMTP id bq39-20020a05620a46a700b006ffd5db9fe9so2095651qkb.2 for ; Fri, 16 Dec 2022 07:52: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=rIRGvziR/92Z54UoPp5pwcNYu+IBI1cm3v25qmGTr7U=; b=Cmwxi1ojjQSeRz0gw6PPt+0UM39ZbzI2VIluVSFGEAN+PVVaRFOPtbUZ9Kwn0kA66f vm6KOjW9XjZSDxeiwIKmnvt1HVvP87z6c6nSAd69uPH18d0JLPX3moINOlNJrLVTmUf6 JzBx53EkDpnO1P24sh37HK4b+p1++Lj5jwS88vlBcsZMtaxsfMlNaJnrcGZTLFdrIMg9 Vb1GxRA6N1VVj/wz35q8FwH/GTppxh6WiS+EAEYpzEgMFo1TszqYYognMey7R25l39Xn q/Zsooa2gEV/nbcNVZHO47CDiITS6wKb7C8lHjS4bdGe0lJ4/i62pXOnBdudssGvetvd vAcA== X-Gm-Message-State: ANoB5pkgMiijhw3XTVoTPbGdiDxbljf07Ow90cbbnAJEpapUDpliKNjI 9atPHDzBHm9++brAmpgwGb0rDHiU+0ppTX2z8KT063Ss8Z2cuPuxfwErmgBiC01Mx0h0SnKcSD/ ZYdKG4coBWZg= X-Received: by 2002:ad4:4353:0:b0:4e8:c749:7f3a with SMTP id q19-20020ad44353000000b004e8c7497f3amr20525132qvs.50.1671205946729; Fri, 16 Dec 2022 07:52:26 -0800 (PST) X-Google-Smtp-Source: AA0mqf4PGgI1eYNhoIKqqtlFBB21vIxd73/3ENYwzCz1J5KG++K1lTB3/aO/vYUZa2W/DRv/E7u/0g== X-Received: by 2002:ad4:4353:0:b0:4e8:c749:7f3a with SMTP id q19-20020ad44353000000b004e8c7497f3amr20525095qvs.50.1671205946508; Fri, 16 Dec 2022 07:52:26 -0800 (PST) Received: from x1n.redhat.com (bras-base-aurron9127w-grc-45-70-31-26-132.dsl.bell.ca. [70.31.26.132]) by smtp.gmail.com with ESMTPSA id d136-20020ae9ef8e000000b006fef157c8aesm1673097qkg.36.2022.12.16.07.52.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Dec 2022 07:52:25 -0800 (PST) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: David Hildenbrand , John Hubbard , Muchun Song , Mike Kravetz , Nadav Amit , Andrea Arcangeli , Rik van Riel , peterx@redhat.com, Miaohe Lin , Jann Horn , James Houghton , Andrew Morton Subject: [PATCH v4 7/9] mm/hugetlb: Make follow_hugetlb_page() safe to pmd unshare Date: Fri, 16 Dec 2022 10:52:23 -0500 Message-Id: <20221216155223.2043727-1-peterx@redhat.com> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20221216155100.2043537-1-peterx@redhat.com> References: <20221216155100.2043537-1-peterx@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: D2D9716001B X-Rspam-User: X-Stat-Signature: ihcrw6xxntrcsufw3n4q8wuh9cp7eje9 X-HE-Tag: 1671205948-347998 X-HE-Meta: U2FsdGVkX1+Kq4jcLYXIadFEjDHrFpbUmuuC9sTfnonF/pTLzqdky2IQau+T0QGDxSpcUDKh2af4QYBnWncDOdcpMq0R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207sP4KTUpzvolYgF65XgHd16xUsu67RNnkfEXyNqp 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 0bf0abea388d..33fe73e1e589 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -6433,6 +6433,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 @@ -6457,6 +6458,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; } @@ -6478,6 +6480,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) @@ -6540,6 +6544,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; } @@ -6569,6 +6574,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; @@ -6580,6 +6586,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; /*