From patchwork Wed Dec 7 20:30:29 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13067582 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 7D183C63708 for ; Wed, 7 Dec 2022 20:30:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 96DA48E0009; Wed, 7 Dec 2022 15:30:49 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 91F2A8E0005; Wed, 7 Dec 2022 15:30:49 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 721568E0009; Wed, 7 Dec 2022 15:30:49 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 3A43E8E0005 for ; Wed, 7 Dec 2022 15:30:49 -0500 (EST) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 12F84A0F0F for ; Wed, 7 Dec 2022 20:30:49 +0000 (UTC) X-FDA: 80216653818.22.F09B4E6 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf30.hostedemail.com (Postfix) with ESMTP id 927B680016 for ; Wed, 7 Dec 2022 20:30:48 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=EuFYZeWa; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf30.hostedemail.com: domain of peterx@redhat.com designates 170.10.133.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=1670445048; 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=v+Krxeq6Jc+PcaZJFIdSBIisnuJapI9S7rvolPGgBf8=; b=fyCRDaISRKGxX685IiXtCfGmBlO+wvW4sjDtI3oR0U3oqw4cbmtiUiStRaOXEd68WxDLlV S+b5iw19jEIvtfg02ABYK1qW5CQN8bdP7oMMLENewvl3iOSolPsbEHX+N0AJTPkmCLpjoA xWeDbYChIpg5Jni2SxuOk3i/zJ/AkUs= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=EuFYZeWa; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf30.hostedemail.com: domain of peterx@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=peterx@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1670445048; a=rsa-sha256; cv=none; b=Pnc8Z0nWd2e73HeVzt9bNyC5TjoC5Ee/gh8taCa3xx8XOs0ib5c4S/5J1YnIeTSj2SGfbu JkyVxqx3LFTvEkl7lb7QsTexCiqn1NYQqg9lP2VA5n2aOfJMQEThKIDF3/moFqppDE80nt dY/qXvujcgm8aDxiJo5sMD9fwU/EKcQ= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1670445048; 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=v+Krxeq6Jc+PcaZJFIdSBIisnuJapI9S7rvolPGgBf8=; b=EuFYZeWa63kT2UHXBI0WtvoxqGG46FM31/eM35yXM7vEVGDjg9j5vfYZLXn5E/M5v0T92N gbUhSxEB0IMWtAAtdwIcyYTmgvlylRIq746wf8x34Am5gq+26TDlARFoIWmyYPWr7chjf2 8J5OSW+QonL++ghxpvH68u0mAPthb04= Received: from mail-qt1-f199.google.com (mail-qt1-f199.google.com [209.85.160.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-224-rb2SeheMN5-0kxxqmHSLLQ-1; Wed, 07 Dec 2022 15:30:47 -0500 X-MC-Unique: rb2SeheMN5-0kxxqmHSLLQ-1 Received: by mail-qt1-f199.google.com with SMTP id ay12-20020a05622a228c00b003a52bd33749so38181988qtb.8 for ; Wed, 07 Dec 2022 12:30:47 -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=v+Krxeq6Jc+PcaZJFIdSBIisnuJapI9S7rvolPGgBf8=; b=uxxza7YbZVf3dCnws+96I6gnxkhfsIa9A4j5pqjbDLD6Zs0m6G/0ICkMwF2vs2ALkt eg7AzYzN7GecAeKIxFOw89Ri0WJGpNgmvhI7beAHtYjl5YJRRQGckXmE8ImNgX/7vAQr iRhAHx6Tisq2cF4xUrMp5rxb4544L+3HZZInbqMJCpOyeU69dx7CY0qacYLscGV3eYbh oMpPGnx/O7CvEUO1hS+IcmAduWeBYx68e+8Uvm1RJZW86EHwlal3GQzFZfmm8z4FSIAU 2K007g0xuLLVjPH7+6ICACs46iUgcCVkP0SGAv43laB35Ue7jaMkOpSgq9sAJVQ198iH Gdew== X-Gm-Message-State: ANoB5pmppLEGQ1Ug1+rFeUcwLSR/vnQZmkKufheuX7aUIwTVe5CbQi6E 3eTt87zZi7ucnkDSpUQ0fsvSNalsOoP9JXkoPqxJf20XsTmY8SqNZJyQxp52jG9DO7ITcYaYgZl 7Z4qL1aH0IsNklYtPVueEYqyfCESl+ldDIJizoOpk+FDQtfzVaK7Pb48SyT6L X-Received: by 2002:ad4:4049:0:b0:4c6:e720:39ff with SMTP id r9-20020ad44049000000b004c6e72039ffmr1560036qvp.28.1670445046289; Wed, 07 Dec 2022 12:30:46 -0800 (PST) X-Google-Smtp-Source: AA0mqf4HMr/hq5EZlF5E6QU5x135bvh3KiJsjh1/wzJyvKRsivrnjQIaSzqnKC1C9heD+L0fqJzg4A== X-Received: by 2002:ad4:4049:0:b0:4c6:e720:39ff with SMTP id r9-20020ad44049000000b004c6e72039ffmr1560015qvp.28.1670445045854; Wed, 07 Dec 2022 12:30:45 -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.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Dec 2022 12:30:45 -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 05/10] mm/hugetlb: Make userfaultfd_huge_must_wait() safe to pmd unshare Date: Wed, 7 Dec 2022 15:30:29 -0500 Message-Id: <20221207203034.650899-6-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 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-type: text/plain X-Spamd-Result: default: False [3.85 / 9.00]; BAYES_HAM(-5.25)[98.33%]; SORBS_IRL_BL(3.00)[209.85.160.199:received]; R_MISSING_CHARSET(2.50)[]; SUSPICIOUS_RECIPS(1.50)[]; SUBJECT_HAS_UNDERSCORES(1.00)[]; MID_CONTAINS_FROM(1.00)[]; RCVD_NO_TLS_LAST(0.10)[]; MIME_GOOD(-0.10)[text/plain]; BAD_REP_POLICIES(0.10)[]; PREVIOUSLY_DELIVERED(0.00)[linux-mm@kvack.org]; ARC_NA(0.00)[]; FROM_HAS_DN(0.00)[]; RCPT_COUNT_TWELVE(0.00)[14]; FROM_EQ_ENVFROM(0.00)[]; R_DKIM_ALLOW(0.00)[redhat.com:s=mimecast20190719]; MIME_TRACE(0.00)[0:+]; TAGGED_RCPT(0.00)[]; DMARC_POLICY_ALLOW(0.00)[redhat.com,none]; TO_MATCH_ENVRCPT_SOME(0.00)[]; ARC_SIGNED(0.00)[hostedemail.com:s=arc-20220608:i=1]; DKIM_TRACE(0.00)[redhat.com:+]; R_SPF_ALLOW(0.00)[+ip4:170.10.133.0/24]; RCVD_COUNT_THREE(0.00)[4]; TO_DN_SOME(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[] X-Rspamd-Queue-Id: 927B680016 X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: 51ag5om8n5yadzjhayidcgzaf9q8ibod X-HE-Tag: 1670445048-426255 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: We can take the hugetlb walker lock, here taking vma lock directly. Reviewed-by: David Hildenbrand Reviewed-by: Mike Kravetz Signed-off-by: Peter Xu Reviewed-by: John Hubbard --- fs/userfaultfd.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c index 07c81ab3fd4d..a602f008dde5 100644 --- a/fs/userfaultfd.c +++ b/fs/userfaultfd.c @@ -376,7 +376,8 @@ static inline unsigned int userfaultfd_get_blocking_state(unsigned int flags) */ vm_fault_t handle_userfault(struct vm_fault *vmf, unsigned long reason) { - struct mm_struct *mm = vmf->vma->vm_mm; + struct vm_area_struct *vma = vmf->vma; + struct mm_struct *mm = vma->vm_mm; struct userfaultfd_ctx *ctx; struct userfaultfd_wait_queue uwq; vm_fault_t ret = VM_FAULT_SIGBUS; @@ -403,7 +404,7 @@ vm_fault_t handle_userfault(struct vm_fault *vmf, unsigned long reason) */ mmap_assert_locked(mm); - ctx = vmf->vma->vm_userfaultfd_ctx.ctx; + ctx = vma->vm_userfaultfd_ctx.ctx; if (!ctx) goto out; @@ -493,6 +494,13 @@ vm_fault_t handle_userfault(struct vm_fault *vmf, unsigned long reason) blocking_state = userfaultfd_get_blocking_state(vmf->flags); + /* + * This stablizes pgtable for hugetlb on e.g. pmd unsharing. Need + * to be before setting current state. + */ + if (is_vm_hugetlb_page(vma)) + hugetlb_vma_lock_read(vma); + spin_lock_irq(&ctx->fault_pending_wqh.lock); /* * After the __add_wait_queue the uwq is visible to userland @@ -507,13 +515,15 @@ vm_fault_t handle_userfault(struct vm_fault *vmf, unsigned long reason) set_current_state(blocking_state); spin_unlock_irq(&ctx->fault_pending_wqh.lock); - if (!is_vm_hugetlb_page(vmf->vma)) + if (!is_vm_hugetlb_page(vma)) must_wait = userfaultfd_must_wait(ctx, vmf->address, vmf->flags, reason); else - must_wait = userfaultfd_huge_must_wait(ctx, vmf->vma, + must_wait = userfaultfd_huge_must_wait(ctx, vma, vmf->address, vmf->flags, reason); + if (is_vm_hugetlb_page(vma)) + hugetlb_vma_unlock_read(vma); mmap_read_unlock(mm); if (likely(must_wait && !READ_ONCE(ctx->released))) {