Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp3070399rwb; Fri, 9 Dec 2022 09:33:44 -0800 (PST) X-Google-Smtp-Source: AA0mqf7QMOXHRJ44LCE4oPjDpyKvErKRKDU5kLwPwxwWlwTarie9JCH66DuCjMhcTjA5D+djbnbN X-Received: by 2002:a17:903:1357:b0:189:dc3:fc45 with SMTP id jl23-20020a170903135700b001890dc3fc45mr6155273plb.63.1670607224380; Fri, 09 Dec 2022 09:33:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670607224; cv=none; d=google.com; s=arc-20160816; b=YL0KQ/qGmulHDrdgF5ZGbpqD1gsvIfArg1p7jjfABnMmndUrippuw+1KlGgoOLTaOz vVOV9Zdum+6g+gI/Pcef2wTGN86sGAvh1VwlDMx2NGVPusOpsiLnpdrlUQtVZdb93Dj7 uA+qocJ+z3cUQ2TTLTJFu5U1YSc56t/0T7AlOrjIiEQghEJ0oSEUiJusMkP21OQ4PKda T4iF0/zKNHmmCnlE9K0NyJnq5RN3ohTJbwUmANRBFvNvHbtH+edu/5wa4UaHOgTsUwwL MN/7Evv+3xruV4gc0iv+q3BlC9SvJDs9STAGkl73Gtcs1daijfRjqiXiNW42OHeZFguV jCqQ== 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=aMLwOwXmHw3aEdY2dAtBVU7yezK1ZrIU2NcKyDsSofc=; b=kf97yNWBWaQkwwANFtS7F0nqMvYpHitGgyLfrZww/NkFMJteqmYRxIoRVCM1OpfCjO kruzlg9eh19Mn5lUzX2gV/MY6xDs6SFfSMhi15HMEXKd+YnYnzP1jhY8po3XMyDW5/Sl fzb6o7DL/85Bpp3dnEqbWc3lpm6yEaAGRdg+KChhUR6GBCuIpYFGgFgnF39T2oLlTzhi glWXSIspuXjwiiUVtH17LID0LA1T+7h/6XiIeaeQS2mQtz7ZH7IZLL438EGYFoNhWRoN yla4Yw0zKdtt4d1VV2UQ9yClEpsjky920lyDJpFNKCgdiKbulPOharsknZK7a3nWf+4L 5RfQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=ixxtqDBz; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q3-20020a17090311c300b00172cb948c68si2278216plh.227.2022.12.09.09.33.34; Fri, 09 Dec 2022 09:33:44 -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=ixxtqDBz; 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 S229985AbiLIRCu (ORCPT + 74 others); Fri, 9 Dec 2022 12:02:50 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51606 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229905AbiLIRCY (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 02C6679C8D for ; Fri, 9 Dec 2022 09:01:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1670605283; 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=aMLwOwXmHw3aEdY2dAtBVU7yezK1ZrIU2NcKyDsSofc=; b=ixxtqDBz3KXFU6W6Rd51ro51xrtpek0V/FAklQ2MBKrfiB1dpf37iOiLNQx5ztGtBOKXtj xdYP0FerDvl1v/XoqBOHNuLl4YcEGNJtsykIefg7Jttua5FdOUUHd09yftg2YzM92zAaVH +T7n6G3CBh5s10r9Mt56CMx5qu9IadQ= Received: from mail-oo1-f72.google.com (mail-oo1-f72.google.com [209.85.161.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-198--M-jSTl9NvmeHNcRCz_TsQ-1; Fri, 09 Dec 2022 12:01:21 -0500 X-MC-Unique: -M-jSTl9NvmeHNcRCz_TsQ-1 Received: by mail-oo1-f72.google.com with SMTP id d3-20020a4a9cc3000000b004a06af5f883so1569304ook.17 for ; Fri, 09 Dec 2022 09:01:21 -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=aMLwOwXmHw3aEdY2dAtBVU7yezK1ZrIU2NcKyDsSofc=; b=3z9tp4o7sWfhViG9o+mu5VPnmnCPdmD0Tk52eY75e18M8vUt2fnZVCQQSCGw7N2Js+ oLi5RTqvBJwLGFPGiBkhIkP4X0JN7erhQLCHjPfzpXROTIxL4P6oZ7d9C9t9fEhYrM9Y lln5u27DctxxiA04+wXVvrvQzl1mD4/tAvGAPv0Y6hG0jsZL1jUHPE5xYNTFN+nC8yWU has5wABEQCzv79lZc8vqCZAmbFcMfoTZaEEiFZ4ftVJ/s5kxdgOKBwhLWJ1nHofCO+z2 QqagrOx1yVVwMgSFZjCjqzQBlNG2F1KSCvICDBmb3hslR+oUTEiA+mHTGK0eOLBcCSY3 gnFA== X-Gm-Message-State: ANoB5pl4WqVuLQg0vb/BNApQKDuK8p6WggZDE1FNbaaKbptLL6ikmm8n sf+6Ja1rNpPzB78tDeZE335G2YhuX7HV1d5efkE+UMCvR4Jr7LXiVL97gL5hOgwGd6IObnOd6tw 6c+1r+F0Jz+5JznopTZ4pEoBV X-Received: by 2002:a9d:77c1:0:b0:66d:c8a2:b9b with SMTP id w1-20020a9d77c1000000b0066dc8a20b9bmr3276915otl.12.1670605281160; Fri, 09 Dec 2022 09:01:21 -0800 (PST) X-Received: by 2002:a9d:77c1:0:b0:66d:c8a2:b9b with SMTP id w1-20020a9d77c1000000b0066dc8a20b9bmr3276898otl.12.1670605280840; Fri, 09 Dec 2022 09:01:20 -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.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Dec 2022 09:01:20 -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 5/9] mm/hugetlb: Make userfaultfd_huge_must_wait() safe to pmd unshare Date: Fri, 9 Dec 2022 12:00:56 -0500 Message-Id: <20221209170100.973970-6-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 Content-Transfer-Encoding: 8bit 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 We can take the hugetlb walker lock, here taking vma lock directly. Reviewed-by: David Hildenbrand Reviewed-by: Mike Kravetz Reviewed-by: John Hubbard Signed-off-by: Peter Xu --- fs/userfaultfd.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c index 07c81ab3fd4d..969f4be967c6 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,15 @@ vm_fault_t handle_userfault(struct vm_fault *vmf, unsigned long reason) blocking_state = userfaultfd_get_blocking_state(vmf->flags); + /* + * Take the vma lock now, in order to safely call + * userfaultfd_huge_must_wait() later. Since acquiring the + * (sleepable) vma lock can modify the current task state, that + * must be before explicitly calling set_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 +517,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))) { -- 2.37.3