From patchwork Tue Nov 29 19:35: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: 13059091 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 B9E61C4332F for ; Tue, 29 Nov 2022 19:35:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6C0BA8E0002; Tue, 29 Nov 2022 14:35:44 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 671176B0080; Tue, 29 Nov 2022 14:35:44 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 22AC28E0002; Tue, 29 Nov 2022 14:35:44 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 0F7D76B007E for ; Tue, 29 Nov 2022 14:35:44 -0500 (EST) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id B7BD1A04ED for ; Tue, 29 Nov 2022 19:35:43 +0000 (UTC) X-FDA: 80187484566.04.A727999 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf15.hostedemail.com (Postfix) with ESMTP id 5426AA000B for ; Tue, 29 Nov 2022 19:35:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1669750542; 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=wQQtMmya3oIPlns9VTTG82x3u2HOgOUNM9IGJVSq3FI=; b=hDzQzunNQivxT0zrqpMmGf4I/gtlG3d7uEDEn3Fb8TVriI8dqv/LdVDO8LiRhh8SGQawzY LsXF73Ebn/2SE6B8WrwfP3zfUL2Kp5CCmwdoDswZiuePEHDMqHDsVWeYhWZZciG16Ffgrn vQLK9PEFBBPjsvjBPMaeLLNwXSsq2ZM= Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-323-Adv-nwoIM7m9XNJ58lLKWA-1; Tue, 29 Nov 2022 14:35:41 -0500 X-MC-Unique: Adv-nwoIM7m9XNJ58lLKWA-1 Received: by mail-qk1-f199.google.com with SMTP id bk30-20020a05620a1a1e00b006fb2378c857so31469532qkb.18 for ; Tue, 29 Nov 2022 11:35:41 -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=wQQtMmya3oIPlns9VTTG82x3u2HOgOUNM9IGJVSq3FI=; b=xlv4499FbudocaeiORGifK9uX11VZ/91Uyf+N8NfqTZbwrkXQ175dkjP5PFpfDkA4k ODdm4q2NvXDCH5YI6H84RjFvcczSHCxNWMJtJhZTjt1fgm1L+gS5W6ESMZylvO4tSh1j TR5gsVayKsaJKGyMWk7MPMySHISON8+PE75jxPeeb4jACQ4OxOLWh81IorJpURGS+I6U g9+6KJyq2lV5WTXo9LntFMkLECcn9+Ol86YAz5fCT0SiCHRGvRh+D1DvVdU/+6nd3Dmq +gpq8hDwwH0Iqo5OlokXiS6OS5KN4hpkOkSjTw1wSN8Zp1rK+jjdqesTZ9HgAsOc1tbT uBgw== X-Gm-Message-State: ANoB5pmFmKuvckkaaGRgANAQ5+Drm/lJrgBg/PxBYaNj2E+LgfOV/oQZ j/5Wr5qL1/NhU+PmlEJOO+QhtUkfJRgi6OmRUwcJUpYpi8IAXeCxnvN43q5TVYMe3JdqtBTIYF/ AKp/XVk6vjipJRMRimpTqKGDhl/gDusCbFeuYyweKxKW39z1So9QGbqzNcJ4k X-Received: by 2002:a05:622a:4891:b0:3a5:280a:3c9c with SMTP id fc17-20020a05622a489100b003a5280a3c9cmr37951529qtb.282.1669750540896; Tue, 29 Nov 2022 11:35:40 -0800 (PST) X-Google-Smtp-Source: AA0mqf7D66AT3COOJUVWGxZ68VmKCBmOjMI4vwPiGugqyqG7QsmIEqLv5iYXBuBUQGMJ4PIx2eAMkQ== X-Received: by 2002:a05:622a:4891:b0:3a5:280a:3c9c with SMTP id fc17-20020a05622a489100b003a5280a3c9cmr37951490qtb.282.1669750540508; Tue, 29 Nov 2022 11:35:40 -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 n1-20020a05620a294100b006fa16fe93bbsm11313013qkp.15.2022.11.29.11.35.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Nov 2022 11:35:40 -0800 (PST) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: James Houghton , Jann Horn , peterx@redhat.com, Andrew Morton , Andrea Arcangeli , Rik van Riel , Nadav Amit , Miaohe Lin , Muchun Song , Mike Kravetz , David Hildenbrand Subject: [PATCH 07/10] mm/hugetlb: Make follow_hugetlb_page() safe to pmd unshare Date: Tue, 29 Nov 2022 14:35:23 -0500 Message-Id: <20221129193526.3588187-8-peterx@redhat.com> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20221129193526.3588187-1-peterx@redhat.com> References: <20221129193526.3588187-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=1669750543; a=rsa-sha256; cv=none; b=lvVwJl/wpQF/R9n9u3DOfcGyLy6nS0XLxSSMhqo1V8XU7wP7N29sOZRbuwgkzyH+wUlzAq LWqodYM6YXzGM5LURrxBcq48wxFwSbR0b+8lc6yqi9ESGpY6BNceZoBaEJ//F18sz7TLB3 nCA0WCloyWzcehCxR0Zzgo3f1Bksuic= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=hDzQzunN; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf15.hostedemail.com: domain of peterx@redhat.com designates 170.10.129.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=1669750543; 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=wQQtMmya3oIPlns9VTTG82x3u2HOgOUNM9IGJVSq3FI=; b=QDg+AHI6+hKMqwwmJmbJ1r7wK6xEqHOz1u0U7kO7bdn1lKT9ASH8OuPbwFgG6reHpZeVlp 33nbJXK/Ji7teP0PIDOz5OjWZBEIBU6sOIWjyQur9lKDmi4abXiICsfXiQ2hkFHNJC10DO kHZ5ZNOFnVFqnuWIX3hxFhbS27v5fAA= X-Rspamd-Queue-Id: 5426AA000B X-Rspam-User: Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=hDzQzunN; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf15.hostedemail.com: domain of peterx@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=peterx@redhat.com X-Rspamd-Server: rspam09 X-Stat-Signature: rw5d6ku8d51qz5eixqnptb9kzufif5no X-HE-Tag: 1669750543-6846 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. Signed-off-by: Peter Xu Acked-by: David Hildenbrand Reviewed-by: Mike Kravetz --- mm/hugetlb.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index d6bb1d22f1c4..df645a5824e3 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -6290,6 +6290,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 @@ -6314,6 +6315,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; } @@ -6335,6 +6337,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) @@ -6394,6 +6398,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; } @@ -6421,6 +6426,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; @@ -6432,6 +6438,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; /*