From patchwork Fri Nov 18 01:10:18 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13047538 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 BCF6EC4332F for ; Fri, 18 Nov 2022 01:10:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9116E8E0006; Thu, 17 Nov 2022 20:10:39 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8C1C38E0005; Thu, 17 Nov 2022 20:10:39 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 714208E0006; Thu, 17 Nov 2022 20:10:39 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 632EC8E0005 for ; Thu, 17 Nov 2022 20:10:39 -0500 (EST) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 22F4CA0708 for ; Fri, 18 Nov 2022 01:10:39 +0000 (UTC) X-FDA: 80144782998.06.353B190 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf25.hostedemail.com (Postfix) with ESMTP id D4D84A000E for ; Fri, 18 Nov 2022 01:10:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1668733838; 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=ApQeGQkmbSMVPsr+Xdpa/9MFmm/79+V0L+f94Hc/CYj0TXFGgSB9PhlKS2vxxgoWXiA+e6 Jr1SVbYaIudklgmSqTZ2tDIcT0693TUHc1kqi6HBQQWb52OvLMD3xHTMxhkuBjfK2zQbIR h9EsczfXbLd4Yai/AGva9x8AURGvGT4= Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-146-WG-8xd0VMIWcnb4C2HMFLQ-1; Thu, 17 Nov 2022 20:10:37 -0500 X-MC-Unique: WG-8xd0VMIWcnb4C2HMFLQ-1 Received: by mail-qk1-f199.google.com with SMTP id q14-20020a05620a0d8e00b006ef0350dae1so4380221qkl.12 for ; Thu, 17 Nov 2022 17:10:37 -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=RDZ0px2fkupxFDrVcpYxnrw5LApJl5bW9CGjwNd5w2e4Df4WInJqwqO3oYbWttEtL5 gAwbahxToxYiczuQDnPaFHhpa5HVdbFIONuHYIaQ8DD+oWodfN8DMxNlgXzq7k0bXkbU CIJpQa/dxDFmWgcLin8IwqzS5Wg3PW66ghpJYAqEKkI94TSz5t4qRERZlLi7+MHXxqPN GDujIwGmHUTLdxf0Gpjus6iydKETlX1hoDlujy0L+T9BzgLyX52+FhXkunPBfqtFGUKO nyk7izHI4bpg7vsXRzh71hxtNb2meUKjdFIwyEeKJMtXoGOR7CemS5t+a2twpSuc6EH7 S2Xg== X-Gm-Message-State: ANoB5pnXbJADiFPGRc2Hw8xgaTkHkZgF/WosiK2Bp/FRnHzF1nCw+WPD 1bUaQrxINBrtTnHRK3olL2qkvDH/35I8N2YBaTVHDk3qIXv1gFjs3fLAzcV/1mDe8A+6rYjwVC3 v/ZxvBAZ39u5NSCfSA81Qd0fmo4NOBK1MXpw3E+OeHtdpfo/nrl9AhF/+w+AJ X-Received: by 2002:ac8:48ce:0:b0:3a5:65ac:9c8a with SMTP id l14-20020ac848ce000000b003a565ac9c8amr4698806qtr.457.1668733836753; Thu, 17 Nov 2022 17:10:36 -0800 (PST) X-Google-Smtp-Source: AA0mqf5rnz6DDTlt9Ny28N2rVHhXYOuDlhdIfEgZs3xq/IpEsL+2rvvX9NavYJjdTwg6ClUXgreJUw== X-Received: by 2002:ac8:48ce:0:b0:3a5:65ac:9c8a with SMTP id l14-20020ac848ce000000b003a565ac9c8amr4698782qtr.457.1668733836471; Thu, 17 Nov 2022 17:10:36 -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 u7-20020a05620a430700b006eed75805a2sm1491342qko.126.2022.11.17.17.10.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Nov 2022 17:10:35 -0800 (PST) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Rik van Riel , Muchun Song , Andrew Morton , peterx@redhat.com, James Houghton , Nadav Amit , Andrea Arcangeli , David Hildenbrand , Miaohe Lin , Mike Kravetz Subject: [PATCH RFC v2 05/12] mm/hugetlb: Make userfaultfd_huge_must_wait() safe to pmd unshare Date: Thu, 17 Nov 2022 20:10:18 -0500 Message-Id: <20221118011025.2178986-6-peterx@redhat.com> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20221118011025.2178986-1-peterx@redhat.com> References: <20221118011025.2178986-1-peterx@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-type: text/plain ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1668733838; a=rsa-sha256; cv=none; b=TAzVCLdYVDLxP00zK5xV2RHzqqyuElOqSvgbD2zd4yJEA6A9KbwEQS4jxz19uyfRMh3V03 Ni/0CBbjQK1vnrq7GV4ud9gHC4/Ui+4osNfcamEVsmfz5Avh3O+rWINxFgkC+AD5TIaPWw A/eMln1xr32rNKtPDvD2NlxSbPO05I8= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=ApQeGQkm; spf=pass (imf25.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-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1668733838; 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=e+q+VDp6lglVzRRLWt+CFs2P4Ri2ud6R83tN0PXXvjtArqPVBnSTzimbO7Vti/SMvqNxl7 W+DKiJ6qPFvRcS/vhu91IMcTBHqVLuicoHf+yk60HC4tFuQzSgGlQqJPHT3TKQZrDPCjqZ QYiSde1SGrMyaWbAFQqIcTNPTYwOYAo= X-Stat-Signature: cj7twnhfacg9za8ao9c7cckbr1cwy47j X-Rspamd-Queue-Id: D4D84A000E Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=ApQeGQkm; spf=pass (imf25.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-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1668733838-804482 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 --- 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))) {