From patchwork Tue Nov 29 19:35:21 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13059089 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 AB1E9C4332F for ; Tue, 29 Nov 2022 19:35:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1924C8E0001; Tue, 29 Nov 2022 14:35:42 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 11B186B0080; Tue, 29 Nov 2022 14:35:42 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DEB228E0001; Tue, 29 Nov 2022 14:35:41 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id C49B26B007E for ; Tue, 29 Nov 2022 14:35:41 -0500 (EST) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id A04B3C0B9D for ; Tue, 29 Nov 2022 19:35:41 +0000 (UTC) X-FDA: 80187484482.30.D99FDE3 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf13.hostedemail.com (Postfix) with ESMTP id 311462000D for ; Tue, 29 Nov 2022 19:35:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1669750540; 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=U2GgktAyXhDRBB8fn5rbcYoK/H0Sll/0lbzArKYhQzY=; b=HOXLYMVvhzztEaPIwe4aXANqPDSJyGxCzx5yIJu7YQPpDhqU0aG0azJHaxV0ifm3yaxoVL 8mbRB9uzCSomu6jCnX1j07MV+XwZJQDxFm+YjPzYngTDBW8WENpX0D26bXy8Y9GD+uImFl lbPn2T0j+HMv1vaT1/xMOYYTTCzOLd4= 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-488-iYJmZuj3NUuudXzOfc-Sqg-1; Tue, 29 Nov 2022 14:35:39 -0500 X-MC-Unique: iYJmZuj3NUuudXzOfc-Sqg-1 Received: by mail-qt1-f199.google.com with SMTP id bz20-20020a05622a1e9400b003a646e03748so22976960qtb.12 for ; Tue, 29 Nov 2022 11:35:39 -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=U2GgktAyXhDRBB8fn5rbcYoK/H0Sll/0lbzArKYhQzY=; b=KlIsmrDQxuVazXO86dkSWUSsnGYOvCZPjCuYcwyKwZ+2vqdnjD5pkPKbvMBpyaPdlQ sfMPZ+zGYfKkynwNAzzZpgecrLcYuU5IiCGem/fV2lmNCO3LKpUgevqYWvLmCjERfemn nBClb+MaLq/8BzAJSC1wC/XjFdSh7Ml1CEER3VxL5Piap1zSAGNMPo5yenwTAW6Fq8UZ p3gjK2cbhdfQAXAvHoe9sXGEeJ3FKl8EEu5fZUmgEMqCDpymVoSnWZJzuhpeag7qTE2a iyXVOEJ4xQDcImA8LIBDJ3XUm/8K4q6+qaRFTD9KddQjQwUiDpnaxOVgov7MU85jv0q2 4qdQ== X-Gm-Message-State: ANoB5pmgdZPBtu19PQgYXL1BeN40uU5NaeyJQrNruP+BmWUEwx75b9CQ recvqMZMFJ+ZAri54lL6P46kgxJasMDB3MWQXtj8uytzzR96TBpMsmtqVZVoX/4S1ASAjdhQ24q 7zkGPrdh3kYWwNPugelm7zvt3OajlgAhmhtB8wzKECuVjUffQ2LgMf1Jr0Uhy X-Received: by 2002:ad4:5a12:0:b0:4c6:cfb3:461f with SMTP id ei18-20020ad45a12000000b004c6cfb3461fmr29830227qvb.18.1669750538400; Tue, 29 Nov 2022 11:35:38 -0800 (PST) X-Google-Smtp-Source: AA0mqf5sUOLP+bu5vmUyeu4eR0d5tdrBlXwCznX/b9tlkmnKZ8SzN/XLnce+u+p7KI/YvBLKOKGVTQ== X-Received: by 2002:ad4:5a12:0:b0:4c6:cfb3:461f with SMTP id ei18-20020ad45a12000000b004c6cfb3461fmr29830188qvb.18.1669750538086; Tue, 29 Nov 2022 11:35:38 -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.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Nov 2022 11:35:37 -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 05/10] mm/hugetlb: Make userfaultfd_huge_must_wait() safe to pmd unshare Date: Tue, 29 Nov 2022 14:35:21 -0500 Message-Id: <20221129193526.3588187-6-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-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=HOXLYMVv; spf=pass (imf13.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-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1669750541; a=rsa-sha256; cv=none; b=1ln2iUkYIiQo0UVYlPQOPlXLcoziZGrJ4NKP6jy76OsLLRtEV0f6E1CHr4Csl2XvHCHaGd yfpQJPbjwd7Zyky2IrA82ABLrw/XtskEnDuqscc7Ce5tItW3jbYKDlfJ4RtbnA9vozSpTb /w/r5BVeK9qZ/KbVL9tzW6e8W4/EnBw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1669750541; 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=U2GgktAyXhDRBB8fn5rbcYoK/H0Sll/0lbzArKYhQzY=; b=soBIUWiq3t2Woie4Fa+xSzOMyR3y3aUAumeVxuHPoCn+fF/KuqLbu+xp3XqhvoKVByLctZ iBdmc1GADPWiHOjMomZqr1bX8zdSxbNC51s2MxTlbbPcSOLaezBOBPcd5Ydgq+ZsW81TWX ZZvZq+deaF4vaBSOFmii599fcfa+3dE= Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=HOXLYMVv; spf=pass (imf13.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: rspam02 X-Rspamd-Queue-Id: 311462000D X-Stat-Signature: fkkummurdci3u7irxk89rstj6nkwiapb X-Rspam-User: X-HE-Tag: 1669750540-816601 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. Signed-off-by: Peter Xu Reviewed-by: David Hildenbrand Reviewed-by: Mike Kravetz --- 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))) {