From patchwork Tue Nov 29 19:35:25 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13059093 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 84794C4332F for ; Tue, 29 Nov 2022 19:35:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 565108E0006; Tue, 29 Nov 2022 14:35:47 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 427826B0080; Tue, 29 Nov 2022 14:35:47 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 203738E0006; Tue, 29 Nov 2022 14:35:47 -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 08FDA6B007E for ; Tue, 29 Nov 2022 14:35:47 -0500 (EST) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id AC6C1A0715 for ; Tue, 29 Nov 2022 19:35:46 +0000 (UTC) X-FDA: 80187484692.05.4FDF838 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf17.hostedemail.com (Postfix) with ESMTP id 5E58C40013 for ; Tue, 29 Nov 2022 19:35:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1669750545; 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=T6R1JTF+AaMAR6h+PxJxqsbJoPo1sRZuxu2H68hPMl4=; b=DrqQ7Pgygb/njglEWIVMov1qSfhA+MIJcOLBWCrFcNSZiKZVF6V3msJsC6T7/L6mvffl3Q iwdf3TbbHIHgYCq9ONZpspumV1hjTP0HrVS6cQgG/MwJyJJHdc0CDGqYldckiM9FrTuG7c u4Ol+A0nbxQxNn4o6hPkHbm3Be+5zGg= Received: from mail-qk1-f197.google.com (mail-qk1-f197.google.com [209.85.222.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-546-_ETZ0NXRPQSinrTyDfFUuQ-1; Tue, 29 Nov 2022 14:35:44 -0500 X-MC-Unique: _ETZ0NXRPQSinrTyDfFUuQ-1 Received: by mail-qk1-f197.google.com with SMTP id h8-20020a05620a284800b006b5c98f09fbso32092926qkp.21 for ; Tue, 29 Nov 2022 11:35:44 -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=T6R1JTF+AaMAR6h+PxJxqsbJoPo1sRZuxu2H68hPMl4=; b=w05izT+hftBfpKdsKcWJKZDU8Xbfg7kfIHH9/bgZUav/1+OiV3sQRFIDygXWo5VFun MK/SZV0IQeYT9L+BMmcqa/dmHIZ9FMVFVJkAq4TASxJfHO3nEzPHpSlSdWQ25rCay7Nk ogt8D+C4IJXORnX/E4wTVHK8q1AKWp1XCJZvAXNknJSqs1O1Inm1oIAXNFvjqtKYkSOM AjUIA89DgKP2NGilKPcW5Ud1XKFPmFKdNu9qq3c3u45FTN/Fy9+p9yLSwg9hnM+ADTJD 0Vtf1gjRAClNKIoEkU9h965wLSYn/52QVxCtpEDMbKJBePy+hXzOTG9VFmEKiNv6dtYB FZLA== X-Gm-Message-State: ANoB5pmJVxqk0QdjkfNYgnNwgShEB3a42jF1ouyeXLXfWCgJlrJdREVF ScuwJW745zB9BVlEQyLIOi8eaXfTJf9o8PW3EccG31BssKPyi1T658zoyzo4/UsKVYxy6mFqeBy kQtDjL2H5LXU7NdSSLs2VloOVpNqc1hsbUgkXXQHFk8kwu3fTOomp/nx4zh+F X-Received: by 2002:ac8:5511:0:b0:3a5:ae62:7b5a with SMTP id j17-20020ac85511000000b003a5ae627b5amr54714927qtq.595.1669750543801; Tue, 29 Nov 2022 11:35:43 -0800 (PST) X-Google-Smtp-Source: AA0mqf7OLspeAz8TgknZG0PdWjf/lNlOIdFM+L3EuhSvYh9tGOcnnSYMMTA7F6avxTBtXfjUbDS3oA== X-Received: by 2002:ac8:5511:0:b0:3a5:ae62:7b5a with SMTP id j17-20020ac85511000000b003a5ae627b5amr54714888qtq.595.1669750543457; Tue, 29 Nov 2022 11:35:43 -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.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Nov 2022 11:35:43 -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 09/10] mm/hugetlb: Make page_vma_mapped_walk() safe to pmd unshare Date: Tue, 29 Nov 2022 14:35:25 -0500 Message-Id: <20221129193526.3588187-10-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=1669750546; a=rsa-sha256; cv=none; b=WA9RVekicd7Xve78jMUWeBMA/e+5oOCzVQ4SSkZmAK7ZCX1vj3c/pAb4zCPyptCiulX2sa F+3ZULFLrBsWp5vVnx1o+5/jh1/BqWnD1li0aac893m5vAIDGBg7l0N/7pij4K65/kPFPX 7O+LT7vgH04VA1ggMP8zyrac36bl9Pw= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=DrqQ7Pgy; spf=pass (imf17.hostedemail.com: domain of peterx@redhat.com designates 170.10.133.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=1669750546; 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=T6R1JTF+AaMAR6h+PxJxqsbJoPo1sRZuxu2H68hPMl4=; b=1aLB4FZxEdvxlx7yr3U3rNKnQ7MzAP7jR7sDG51HIswp4t9pasiV+KAP/oomFkh5dEJHAu 9SmoxQjwODZPBBLTHnV7jsbYh98olNLcABXxWwjaP6qV+8paFKQcvOC3YrWA0fsBpSV4SK XtzwMY9Exc+zW8IizGzCM3l1st8Xt5s= X-Stat-Signature: fyg9zkeh3mi9hrzj6n5f6ba47jaorzk9 Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=DrqQ7Pgy; spf=pass (imf17.hostedemail.com: domain of peterx@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 5E58C40013 X-Rspam-User: X-HE-Tag: 1669750546-222877 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 page_vma_mapped_walk() 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 --- include/linux/rmap.h | 4 ++++ mm/page_vma_mapped.c | 5 ++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/include/linux/rmap.h b/include/linux/rmap.h index bd3504d11b15..a50d18bb86aa 100644 --- a/include/linux/rmap.h +++ b/include/linux/rmap.h @@ -13,6 +13,7 @@ #include #include #include +#include /* * The anon_vma heads a list of private "related" vmas, to scan if @@ -408,6 +409,9 @@ static inline void page_vma_mapped_walk_done(struct page_vma_mapped_walk *pvmw) pte_unmap(pvmw->pte); if (pvmw->ptl) spin_unlock(pvmw->ptl); + /* This needs to be after unlock of the spinlock */ + if (is_vm_hugetlb_page(pvmw->vma)) + hugetlb_vma_unlock_read(pvmw->vma); } bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw); diff --git a/mm/page_vma_mapped.c b/mm/page_vma_mapped.c index 93e13fc17d3c..f94ec78b54ff 100644 --- a/mm/page_vma_mapped.c +++ b/mm/page_vma_mapped.c @@ -169,10 +169,13 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) if (pvmw->pte) return not_found(pvmw); + hugetlb_vma_lock_read(vma); /* when pud is not present, pte will be NULL */ pvmw->pte = huge_pte_offset(mm, pvmw->address, size); - if (!pvmw->pte) + if (!pvmw->pte) { + hugetlb_vma_unlock_read(vma); return false; + } pvmw->ptl = huge_pte_lock(hstate, mm, pvmw->pte); if (!check_pte(pvmw))