From patchwork Wed Dec 7 20:30:30 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 31025 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp393042wrr; Wed, 7 Dec 2022 12:35:27 -0800 (PST) X-Google-Smtp-Source: AA0mqf4WiHhUgB3cAIXkuYPfujSqhMZstgPTz7hX3bJ+a84s9fHBBBehNCXtYZVsNvuJAF3CwKuK X-Received: by 2002:a17:906:840e:b0:7c0:d94c:7384 with SMTP id n14-20020a170906840e00b007c0d94c7384mr16928122ejx.109.1670445327542; Wed, 07 Dec 2022 12:35:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670445327; cv=none; d=google.com; s=arc-20160816; b=Y5RJoh1ptVq1mI6vdYZ6dRHil6g5eYkT+de4DXN4PbO9medPLzpxuCaYzHdqcTfa+1 Hrs7SFvvmLRUvJ1poe1PO8POoxhoweR0Bs5hxGVMadYZiotKKJ2+klYThT9H0hglSsOd Pu7B3mFAQI6D5pgDa5juFNZ6FpbR87xQGjIDPr/AS9PL8kYcEsE+drFAMW5tEFSZabaj 3X6XZ8GM6KmP+aC/u3m7dQG4hfQMvS+9bmiKqdMkzoFJPwIaj8IkLg1c5IzY7FNpOLoy 3TsQP+1GhwxZ3D9qxcmYjp5208QM1VuXP59mxzd9oWkf4i+milAm+YRNTHB8WHxucNEF qt/Q== 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=PxW1+7FnblUXilUivNjEMBQoO6EpHq3+X52yjUcQpsM=; b=w62XN6164TKOBchKayYcfAXfvQ+FdFNIZ4N2CPAutR2BQ6nztESe/ivjzsuOcuqlFS xY4JBeZQC4MsGYKFmgIaq1DtFvZPAfpFBeljr0hgSxCsULECe6YoLM4knSglTYkS1zym DLaFWgz5ftehN0LPCo23W/krN0Ninve/L5DNy0+0KlsbXcfbR6C3Wkbh/11FPoAG82hK XvhCU5ZD0Z7X6nhBpEzgSZjEnGjj6Kc6CdW9JwpR49Pp9NEqtBpfvAEWzdoa+4eXV/iN LwA1nlWubUmsyWyjDq6cKfMU4dPfFL8g02KStBp/gObAk3OlL999M+j0+rABJZj6oxd4 wbZA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=O2SbdXyZ; 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 he18-20020a1709073d9200b00782e437a368si18456791ejc.160.2022.12.07.12.35.04; Wed, 07 Dec 2022 12:35:27 -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=O2SbdXyZ; 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 S229948AbiLGUcG (ORCPT + 99 others); Wed, 7 Dec 2022 15:32:06 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49200 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229885AbiLGUbw (ORCPT ); Wed, 7 Dec 2022 15:31:52 -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 5ED5130573 for ; Wed, 7 Dec 2022 12:30:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1670445051; 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=PxW1+7FnblUXilUivNjEMBQoO6EpHq3+X52yjUcQpsM=; b=O2SbdXyZpKklrTHYxm5bTPaPWW5Yow3P1uLvnYHlM97GmfxL+QD7RDrcDdWDdp5zGbBpxg VJwEUQmAxILNB0/ArXSZdY0iN6JbbOv5u4MwIF3TL/GXmrQBBpAI4oeKFD1G6LYBkF185r ZGq8t4C2ZaVvhCIR6H/oYcuWre2MJZo= Received: from mail-qt1-f200.google.com (mail-qt1-f200.google.com [209.85.160.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-664-vRZXXeIlPNW-QusS_hoOmw-1; Wed, 07 Dec 2022 15:30:48 -0500 X-MC-Unique: vRZXXeIlPNW-QusS_hoOmw-1 Received: by mail-qt1-f200.google.com with SMTP id cm12-20020a05622a250c00b003a521f66e8eso39653106qtb.17 for ; Wed, 07 Dec 2022 12:30:48 -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=PxW1+7FnblUXilUivNjEMBQoO6EpHq3+X52yjUcQpsM=; b=7s0vKwbZLDj+Z+EwORus8sT5GMdhxpqsPy9CuoB3iScVCeTbv+BuDorHGcQ61XWlMJ 27SjLBd/tx9G5XguPTSJ26SO8oW7VV79pTas3h529ELqEsZGvtXSUcrT8GREfAqT9Tvt s28PpTFrgT8JZgTXMUGsBM+PoFe2w6xL6DVT4LWIQh6KAtthoEhG8d0CmHFETljLPZX+ HaOqp1FbCw7Yg+IUYzPbT6hNQVxFYoAV4UdNNxKp4GfgF97YDCm9p0WjbCA93oDMLR5p nt5DYpTTB/M/GxifP0qb0pVE0pi8+KPk8RwCnZN33f3BgM3Lv3TE/1KwsAEHSYVssaij NzHQ== X-Gm-Message-State: ANoB5pl9675HQOBQAKTOSHqtWQqrbbxhwL5kimG1ZMPT2gbNl2GIZ5X2 9xQfXQQGZ/SHWDqF56aUqTh6+Rq5r2zn8+cE8rsBfzN17t5UnPdhT0g7hfu4lDYkhJr626xQNzR K348JMdD3Cr4iFI+9hzqmyMZz X-Received: by 2002:a05:622a:1b1c:b0:3a6:a205:741f with SMTP id bb28-20020a05622a1b1c00b003a6a205741fmr1433034qtb.32.1670445047602; Wed, 07 Dec 2022 12:30:47 -0800 (PST) X-Received: by 2002:a05:622a:1b1c:b0:3a6:a205:741f with SMTP id bb28-20020a05622a1b1c00b003a6a205741fmr1433023qtb.32.1670445047366; Wed, 07 Dec 2022 12:30:47 -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 dc53-20020a05620a523500b006fefa5f7fcesm855594qkb.10.2022.12.07.12.30.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Dec 2022 12:30:47 -0800 (PST) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Muchun Song , John Hubbard , Andrea Arcangeli , James Houghton , Jann Horn , Rik van Riel , Miaohe Lin , Andrew Morton , Mike Kravetz , peterx@redhat.com, David Hildenbrand , Nadav Amit Subject: [PATCH v2 06/10] mm/hugetlb: Make hugetlb_follow_page_mask() safe to pmd unshare Date: Wed, 7 Dec 2022 15:30:30 -0500 Message-Id: <20221207203034.650899-7-peterx@redhat.com> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20221207203034.650899-1-peterx@redhat.com> References: <20221207203034.650899-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?1751588879921088745?= X-GMAIL-MSGID: =?utf-8?q?1751588879921088745?= 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 Signed-off-by: Peter Xu Reviewed-by: John Hubbard --- mm/hugetlb.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 49f73677a418..3fbbd599d015 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -6226,9 +6226,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); @@ -6251,6 +6252,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; }