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: 31881 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp892427wrr; Fri, 9 Dec 2022 09:10:09 -0800 (PST) X-Google-Smtp-Source: AA0mqf4pgFFi92JT15VbF2V3YVElm+aPnfAfixl+LTA0FrTkVm3055/JP70AlqdV+na8uyzg1jL2 X-Received: by 2002:a17:906:168a:b0:7c1:10b4:4742 with SMTP id s10-20020a170906168a00b007c110b44742mr5277691ejd.55.1670605809217; Fri, 09 Dec 2022 09:10:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670605809; cv=none; d=google.com; s=arc-20160816; b=obFumJk/HRfOjOVA+VH9vm8KggKUF3t5bwp4l+7trZGDaWdgRl0yQm7BfsHwMBG/cd z6vQW2uhvzimhbV7gAFg8bmyhLQWnuNmRGe3aiHMzqaWFdLcKmbRt+N2sR1hh/8Oae2r pC9Y1/S+8GZBX2FA3fZ8sKsFgRHXARfH+/glUj1qeQ+mlz9gOrqF4bD12PdZgiBdVO8z gy5Mptz77Ttu+xlJnYoradIkG/U3orkRx/xYOuCNk3K/wU+6EzzjAWeIRSK2ZAym3yrN y8DQuV2Nzf0NhONPavD/ySjpqGePfcuB8Td4/mPMiCjGW0kjRqcTC9qF7NWMjYVzDx2p eW5A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=BJ1c+DnQwwOjSKsDq4iN/7ttySyX/vWFxv5LWeLTqBE=; b=MSDP1GulMD2P9jTqstGWbAv9mbWOJGN57vewbVsheUeIzixn9XEAXBV+DYlwGY2PcU 9l3aAyONYbqOsWKDKJT2D6zKI6AM4uDXjnUAfEn4BqnR8QLvHIEOIN3lHer/EmchMRGk aetiKurnIJLG7r/HvZhJ7ezMIFluCnFU/m2DPWQ7XdcOBzP/9FtmozfzRq2HtueStyJ2 dyrRB7Z8CESA3T+3Xx4IBQTwb4BUY7BommfLRIcK+yxBlTNi6iAXYsEriksn+Aa+RWpF NQWbjnLRt6C54KIJ5mGWxvo2qsJSfVR7lFlHJeRW6aIZ/Qa30rCzTszsTpzUeCI9ric9 oZiA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=SJrjuxu3; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id hr40-20020a1709073fa800b0078034101c0esi201826ejc.978.2022.12.09.09.09.44; Fri, 09 Dec 2022 09:10:09 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=SJrjuxu3; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229631AbiLIRDO (ORCPT + 99 others); Fri, 9 Dec 2022 12:03:14 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51752 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229917AbiLIRC0 (ORCPT ); Fri, 9 Dec 2022 12:02:26 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9A0DD7DA55 for ; Fri, 9 Dec 2022 09:01:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1670605289; 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=SJrjuxu3NMB3kEkq8Qw3lRHS2W5ZxqyMZaaTxZ/nNmF4dXHjD8l5PEskCALrr3oNaMfmqR hqUfiH7/KYwnRlX3/zjjSDUdR9DaUIbPw4lYYM7D7V/2iPSiwkeRhPtEf43cuBicYV9tcl 0REDPNoemPNiL1aK5UqL8apMhQL8CB4= Received: from mail-oa1-f71.google.com (mail-oa1-f71.google.com [209.85.160.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-335-66wvnKmrNUyjYvEJ3uhGTA-1; Fri, 09 Dec 2022 12:01:26 -0500 X-MC-Unique: 66wvnKmrNUyjYvEJ3uhGTA-1 Received: by mail-oa1-f71.google.com with SMTP id 586e51a60fabf-1441866fa6cso117101fac.22 for ; Fri, 09 Dec 2022 09:01:26 -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=QL2wApHU6vwvO6lRJuJxFlOOfOAYP6yaSGV7ywQ7rnT8gAUhOc1iJs0+PDYzT5wBgv SXFlTPqTtqVKe6KWZGOEdF1LbPb7pdgkbMAoYZ2MR+Kuh/1q+UdbN+JymqdvZVsvn0BY raU7yscUfY7hBRL1YOVmkipx9xM3nEYtOHxjrTGShgNXHmVO7X7NkPOCrAbY/ceVyKWl 6td/E/6OrgXcPcTf3XY765lU/21ubrQsD/ZOGyzfAKHaioZfps2nSXLsxDb5gXGLm55m YMrnqJdGwIk7ggl7i02n+V49MWG7F3LYbnnLXIyUfrM9Km/xrFgehkQx+Pkf4FevkeJ1 rpIg== X-Gm-Message-State: ANoB5pkljGASLu669wKNCML9a6vatzxSS+mUlx7E+yyR3aYsUvSadGNq bS9KEOcKL2ChBI9CUDJbabgDBkOtiDdwZ9daCphk0BHXRi2jqNEqUyOX6u0jqGSBIajSCzdl+dz LcTea5ngOVZ23y7T6+o/ydyep X-Received: by 2002:a05:6870:2f0a:b0:141:fe19:d4d0 with SMTP id qj10-20020a0568702f0a00b00141fe19d4d0mr2655998oab.50.1670605285751; Fri, 09 Dec 2022 09:01:25 -0800 (PST) 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 Content-type: text/plain X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1751757157088399554?= X-GMAIL-MSGID: =?utf-8?q?1751757157088399554?= 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; /*