From patchwork Fri Dec 9 17:00:57 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 31879 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp891155wrr; Fri, 9 Dec 2022 09:07:49 -0800 (PST) X-Google-Smtp-Source: AA0mqf5iKlzJv/LVRW2Xgb67a7bmKFDKuwQB9ppMf3qQbuHBbOKn5pyVbgmJnLMOX/0S4kwSMDhq X-Received: by 2002:a05:6a00:324b:b0:574:3cde:385a with SMTP id bn11-20020a056a00324b00b005743cde385amr5594486pfb.32.1670605668825; Fri, 09 Dec 2022 09:07:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670605668; cv=none; d=google.com; s=arc-20160816; b=dv3TRyPgUfSJ3wcjtMGXADCMsgeZJwhJtN4zkUfeaKq3vLgYyK77IBUiI8wfEi1HDk WWI4y2k0+J52FGh7SYLIollC8yPCZDhALyzHNDzeD31BeP9labo0epfd8KRswVX6xKz1 GdgrjsEVvkpUDbhZ8h63xVplF9Rpc8gqgcQ17/WssJ2wtV6esRg5QwhOKHdTvMMp+szp p+tX7JxLDyPmVj/AAWG4L78ns+f32kR+6rGanDeNCo2wAvWLBOO1dqW69b+ca1Bgt8/N qhkHR2wd3ag4sHpGzya8CDffoDubhy5X021iVu5AxdYffNXf5gq0hv7QiNLe9QZFKUKN VKzg== 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=QzeyNG+xisx8vPYcMzT01YLXY3V0nLFHTQTVfPkpFHw=; b=cj1TUmsNWffU1BwM5GeiVsJkSLCJ+arbpGk9l4eIbVqc5X/DC+ExGJSAoxnwZ1muNZ i9ycf3qUI+PHPJtOA7lznWTzTp2SLvl/7TKJ0hWjYkiVelGVmhWeavN/sckO2epMVYyu d+uSNqO47vnRjf1Y68IOLldN1y14GPZxyAAw0ab2GhmMIztFMNuXn+3TvvMu3JUViQiQ VEz3QlPwezwbMdB5pNbqKRhdQhwn9KXUWU+Rqbp9kqhF35/9zIX2OLrShbJJm4k+/D0X O+LQR1sPlWGGf1vhnoBQF9SP467sCL8IZ5qL/9J9dd/e0XVDp78Ozogv1EcECxYRNVcE giTg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=egPMxOEL; 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 bv5-20020a056a00414500b00574f3820920si2048250pfb.331.2022.12.09.09.07.33; Fri, 09 Dec 2022 09:07:48 -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=egPMxOEL; 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 S230011AbiLIRC6 (ORCPT + 99 others); Fri, 9 Dec 2022 12:02:58 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51638 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229861AbiLIRCY (ORCPT ); Fri, 9 Dec 2022 12:02:24 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A4D8A14D13 for ; Fri, 9 Dec 2022 09:01:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1670605285; 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=QzeyNG+xisx8vPYcMzT01YLXY3V0nLFHTQTVfPkpFHw=; b=egPMxOEL9VEKTZCHwGQlHUDVy1Qj4DNJlAz6zvnBvfNqmv0fU4nUSA7iVaxqxZka0v5AJ+ AfN1256Jb70mOjXsRX9X8bYTaRwr1cU/61/wSgo6pE5iGnvknIJ5GE28e1yA+ioEFxRITp bOi5JxY0p+Sb8rBg1+9IcXZbkuZZgc8= Received: from mail-ua1-f72.google.com (mail-ua1-f72.google.com [209.85.222.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-672-SWK1_QJZMOyDYZOZg4M2QQ-1; Fri, 09 Dec 2022 12:01:24 -0500 X-MC-Unique: SWK1_QJZMOyDYZOZg4M2QQ-1 Received: by mail-ua1-f72.google.com with SMTP id z44-20020a9f372f000000b00390af225beaso2088206uad.12 for ; Fri, 09 Dec 2022 09:01:24 -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=QzeyNG+xisx8vPYcMzT01YLXY3V0nLFHTQTVfPkpFHw=; b=FLdwDIQvnhZWV2UuoIYMU83iH0Qoo16E80/mF2zaZ+i0wAIeG73odeMFU09nIBg/q6 QHbFtEBl/IOHkPoEqaGatWIxXRNtVUvZWsDN9g2Yz1kPfBEW3nWHNmWq/OSvSjLvODlb JekakgLLUA3wLZEhcq0BbShEtjMI7pi4IKja8A66PqXfHoQ5w7aCXBjp4jKFPhzdy52Q RvYdVwwJG40MbV1d1YN0p4yOaBxgRg7j64SbP02+fLf0VsPQwCodR6ZKVRKIEo9dstm6 z8E0UhQF2sxKlavuSg/DdOtSLhsNOLuCBd28X2rFG8v7t+Ota59KYMZerhtgIvnDA8zF 7DOA== X-Gm-Message-State: ANoB5plNsYQcjtbTZyXNpvm5yapsnS/9e/1YfWN/vjt70PuR7wS/B7Ov v/xQFya8u7wWst7OEIRZwRJYCvNu4RbuswG3CvePoTkQKMhGAxaY+2OR6ftYluBR3YX1kjgGTyT pmebaBVjEG8YeoMyz515Ms8x3 X-Received: by 2002:a1f:ee4e:0:b0:3bd:f324:5500 with SMTP id m75-20020a1fee4e000000b003bdf3245500mr3198331vkh.2.1670605283871; Fri, 09 Dec 2022 09:01:23 -0800 (PST) X-Received: by 2002:a1f:ee4e:0:b0:3bd:f324:5500 with SMTP id m75-20020a1fee4e000000b003bdf3245500mr3198279vkh.2.1670605283615; Fri, 09 Dec 2022 09:01:23 -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.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Dec 2022 09:01:22 -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 6/9] mm/hugetlb: Make hugetlb_follow_page_mask() safe to pmd unshare Date: Fri, 9 Dec 2022 12:00:57 -0500 Message-Id: <20221209170100.973970-7-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?1751757009682189806?= X-GMAIL-MSGID: =?utf-8?q?1751757009682189806?= Since hugetlb_follow_page_mask() 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 | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 247702eb9f88..e3af347470ac 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -6227,9 +6227,10 @@ struct page *hugetlb_follow_page_mask(struct vm_area_struct *vma, if (WARN_ON_ONCE(flags & FOLL_PIN)) return NULL; + hugetlb_vma_lock_read(vma); pte = huge_pte_offset(mm, haddr, huge_page_size(h)); if (!pte) - return NULL; + goto out_unlock; ptl = huge_pte_lock(h, mm, pte); entry = huge_ptep_get(pte); @@ -6252,6 +6253,8 @@ struct page *hugetlb_follow_page_mask(struct vm_area_struct *vma, } out: spin_unlock(ptl); +out_unlock: + hugetlb_vma_unlock_read(vma); return page; }