From mboxrd@z Thu Jan 1 00:00:00 1970 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 95299C48297 for ; Tue, 6 Feb 2024 18:28:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EDB6C6B0072; Tue, 6 Feb 2024 13:28:03 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id EB2A36B0074; Tue, 6 Feb 2024 13:28:03 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D54966B0075; Tue, 6 Feb 2024 13:28:03 -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 C6D5A6B0072 for ; Tue, 6 Feb 2024 13:28:03 -0500 (EST) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 9DA7540B30 for ; Tue, 6 Feb 2024 18:28:03 +0000 (UTC) X-FDA: 81762213246.21.B5A32F5 Received: from mail-ed1-f51.google.com (mail-ed1-f51.google.com [209.85.208.51]) by imf22.hostedemail.com (Postfix) with ESMTP id D123CC0019 for ; Tue, 6 Feb 2024 18:28:01 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=sS+gEJ22; spf=pass (imf22.hostedemail.com: domain of jannh@google.com designates 209.85.208.51 as permitted sender) smtp.mailfrom=jannh@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1707244081; 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=mssWr8/tMwfYzRYqB7GAnBjReP7gos18KJNG0Y4GNVI=; b=rwqg5MP5DMcwOKU1cNLJ19G1mgEv6iKTZu4jdIR4j8ktYQ7oVNQxqj3DHCSLXhk85P9fC/ Zn4qUbNBAkX0pztLQp09yiUT0pk5k9618yj59rh6xNmvouluUwibV1NCmbrFU3KRom2Rz1 uwTtPceVH+GJ2BTWVdx8sDBL4imcdP4= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707244082; a=rsa-sha256; cv=none; b=dRyC1r/tRikwekBtEPaQUdSG6jDYJVbDgNeowE4PP8kwoo1V7nYe0Zbrpsnd6wmkFV3tUo qKOZyRx8xL18yPfn06AbZKrL3MP7ItBcmp/hpI+e9fPDAGpKuFxT6iDVnTMX8kDjfGAguc oni5OpkkGHCwoCNsI6Qhr+1t8R8QlP4= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=sS+gEJ22; spf=pass (imf22.hostedemail.com: domain of jannh@google.com designates 209.85.208.51 as permitted sender) smtp.mailfrom=jannh@google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-ed1-f51.google.com with SMTP id 4fb4d7f45d1cf-56012eeb755so1236a12.0 for ; Tue, 06 Feb 2024 10:28:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707244080; x=1707848880; darn=kvack.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=mssWr8/tMwfYzRYqB7GAnBjReP7gos18KJNG0Y4GNVI=; b=sS+gEJ22+ZSSCnAA3BAbmvEgcgQFfoaCV9z2bxIeytlBdkpX2X0KUrNXU2ySJDz41Y Ph/QRUvkF9/sxiYk7nX9HkqI6+EzHOnk3OzaD8Ta0GdTcgoMW70SEtVwhRuGXITy345b Gtn1ZVPmGpb5wgvyMIMyL8c5Sj/jGt6yfzUb6Ic2+6TkyLpja2DK7e0Q6kvk6Z7dsv82 LkqfqzRbGhPkmqoR5+tf725wD4ydvaH9MRIg+US5Sr5jKSxJ16fI9bz5DdlrvWEkh15M CvlNXt3fJO87yyW07hYq2fpD5xqiM1LhtCm0RlJmGHkVjSxILpqbAMFDCBCdyJ8bIzjV k/xQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707244080; x=1707848880; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=mssWr8/tMwfYzRYqB7GAnBjReP7gos18KJNG0Y4GNVI=; b=coh5PSUe+YG3UwpM0eZrSjMs5rhQUavMD1D5wRPXCfNw58ZvUdXT6IvtXbzXu6C7RJ rJDMuu9ekSrdySPEXa+WLhRo3hG4OyJPCBYGMz6t6LgfAenM8BHNdlGFCybhlXfJk0nO Bu/motRvPzkhGFKHbTrrEB0njcby7QkyNvmK0Qet3JL6+DftaAfPHCamu5JY9jalI3LP K3KvnfI50Ptwebe72dizNRT3rR5kuC+RqF8bK+xDyARcq7FpXgO+5NHz0+rR5RXewnN0 cd9Ri/SQKN4rGlUqT4B27QG4y8tyCvyE172OMTjw5bo2ggwtwirs4CJtuPCCqK6P08+C T1fQ== X-Gm-Message-State: AOJu0YxPE3yQHdE6l9AhtlVE2Xk5AyIVUB1/zpq168UWempmuwoJViAL u++SbkPc0a64JTwCtqR6TR3dvUFNBCWqlRSpDD0HTLoPbIaTsF2kfe6CO/EL5rIU2uNPFgZVncB bB0geYXb17A/DqLEIH908K136MEQJ0yvME3qLE2u+z0FodQzIzctL X-Google-Smtp-Source: AGHT+IGlFHjhMsclmD9laqfjKedcH6F5arabPzzT/YJDR4LP/EEGeTRmgXeW7NgKrSk3f6qUrTPuh0ae6i59DgzppUw= X-Received: by 2002:a50:ccd6:0:b0:560:1b3:970 with SMTP id b22-20020a50ccd6000000b0056001b30970mr7470edj.7.1707244080137; Tue, 06 Feb 2024 10:28:00 -0800 (PST) MIME-Version: 1.0 References: <20240206010919.1109005-1-lokeshgidra@google.com> <20240206010919.1109005-4-lokeshgidra@google.com> In-Reply-To: <20240206010919.1109005-4-lokeshgidra@google.com> From: Jann Horn Date: Tue, 6 Feb 2024 19:27:22 +0100 Message-ID: Subject: Re: [PATCH v3 3/3] userfaultfd: use per-vma locks in userfaultfd operations To: Lokesh Gidra Cc: akpm@linux-foundation.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, selinux@vger.kernel.org, surenb@google.com, kernel-team@android.com, aarcange@redhat.com, peterx@redhat.com, david@redhat.com, axelrasmussen@google.com, bgeffon@google.com, willy@infradead.org, kaleshsingh@google.com, ngeoffray@google.com, timmurray@google.com, rppt@kernel.org, Liam.Howlett@oracle.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Stat-Signature: iupcg86pftzwyg8q57rct4f55dugs9hc X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: D123CC0019 X-Rspam-User: X-HE-Tag: 1707244081-149001 X-HE-Meta: U2FsdGVkX1/UUv0dQEHoLdQC7ka7E/Q3VssbvDbuMc9dDYWmvinMe6bU/Topt1JKBMLmabVDX7mNlYm9pu2g1Dq1R3+oYA+p4TpBbd/HH8s2MFD689WQ7sOWN+jdaghQn9MMxRWye3pqyuO3+/V53WfGcxbgt1tTr9LtL72I00cIn2rU6UncKvZq3BvYh12ch1NrPLedu01eSOl2SjBNLeOZL5SQzW7fL8CNz8ny3PGUu+CiTxRn2b53gSkwZ8v6qLqbeRf9jXFkZprUH9Y+IxS3SqsUrC/FTIE1WP6DjdScbwPna42BW6MY73gqFo6YxpIDQ6c6cGW3uDfi7cUIN0SDSdTzac+f3rLDnsGUhA02o3LplKfawEO4vPohfserVuvMR7vV5xbVnsN8ygFLGJ6nykvFQNrnqsmS8bTAT3QpPIpbGkz+yumvVdtPklJwBqRuaMdqSbDpnOjPju/r43gJ0BR66IMSmEXeK0kewKNhdVcXSkL3oEX4mM8v/nD4urZoZjZ4etccfTVaiJqcPYbpiDRMHXlgrH+AFS0sV04spoAwOlSHc9ev1LIQhEjunLFN6yz9rkpKfIlhxxIrvjoByjN9JCL/iFi2p+MCemLL3LQ03X5jWAt3/N5yGAAj6DTQbnp6ERPXHAZAIeNxkxoo5Yr3tO9AOYGijLut0fEDcA8KXPu/qJ/mNpDHt8ByidtK7s5ZrUV7PwI37e1T2q8UnckGxikenAcQnulVR2lb600SVh/rYW1fQDr+JIsblrgRYTYdysGubdWih+Ni8qaKrJigHomZBCpzx0Wxa7YnILGPYGeScR0dYYEaSiB8lhAftccKmmDyc3t7n03NL86G8CPx+ipJxGN7zVETGzuxQW1s06riLldZew4Q/ysD/p4Rs5pNtDG+xlK6DQ1MtJZihgm7JQzy46EgaAtdT+aA6q4zMvch1wiqXcBz61jHRRU7R6d5ZOj8T5tAFXl DBZoOgQE aZvfELg2y/M+EF6nNqc+J865ofb8pdMVj8cVTepvLRdzpCGEKNlupyuJXxf2XprCiTRMCFQ1t3wzLgtV5H7qmhD9eR6+IIFA+5poxf1jvOheYD81QgzEeKY4en6wYViTgQaczGFT4X95isQhDaJ9g2Iv0VHS2rvwl9kkWIpw1I04dmw+EYfegfh5GVe4zhsvaTWmHP9DNUzV+5sOc8aBxzMphsN9Crxf4UxJ3AmTnXVccJ17gLgVqR6dnvOVk22HUfid6 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: List-Subscribe: List-Unsubscribe: On Tue, Feb 6, 2024 at 2:09=E2=80=AFAM Lokesh Gidra wrote: > All userfaultfd operations, except write-protect, opportunistically use > per-vma locks to lock vmas. On failure, attempt again inside mmap_lock > critical section. > > Write-protect operation requires mmap_lock as it iterates over multiple > vmas. > > Signed-off-by: Lokesh Gidra [...] > diff --git a/mm/memory.c b/mm/memory.c > index b05fd28dbce1..393ab3b0d6f3 100644 > --- a/mm/memory.c > +++ b/mm/memory.c [...] > +/* > + * lock_vma() - Lookup and lock VMA corresponding to @address. > + * @prepare_anon: If true, then prepare the VMA (if anonymous) with anon= _vma. > + * > + * Should be called without holding mmap_lock. VMA should be unlocked af= ter use > + * with unlock_vma(). > + * > + * Return: A locked VMA containing @address, NULL of no VMA is found, or > + * -ENOMEM if anon_vma couldn't be allocated. > + */ > +struct vm_area_struct *lock_vma(struct mm_struct *mm, > + unsigned long address, > + bool prepare_anon) > +{ > + struct vm_area_struct *vma; > + > + vma =3D lock_vma_under_rcu(mm, address); > + > + if (vma) > + return vma; > + > + mmap_read_lock(mm); > + vma =3D vma_lookup(mm, address); > + if (vma) { > + if (prepare_anon && vma_is_anonymous(vma) && > + anon_vma_prepare(vma)) > + vma =3D ERR_PTR(-ENOMEM); > + else > + vma_acquire_read_lock(vma); This new code only calls anon_vma_prepare() for VMAs where vma_is_anonymous() is true (meaning they are private anonymous). [...] > diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c > index 74aad0831e40..64e22e467e4f 100644 > --- a/mm/userfaultfd.c > +++ b/mm/userfaultfd.c > @@ -19,20 +19,25 @@ > #include > #include "internal.h" > > -static __always_inline > -struct vm_area_struct *find_dst_vma(struct mm_struct *dst_mm, > - unsigned long dst_start, > - unsigned long len) > +/* Search for VMA and make sure it is valid. */ > +static struct vm_area_struct *find_and_lock_dst_vma(struct mm_struct *ds= t_mm, > + unsigned long dst_sta= rt, > + unsigned long len) > { > - /* > - * Make sure that the dst range is both valid and fully within a > - * single existing vma. > - */ > struct vm_area_struct *dst_vma; > > - dst_vma =3D find_vma(dst_mm, dst_start); > - if (!range_in_vma(dst_vma, dst_start, dst_start + len)) > - return NULL; > + /* Ensure anon_vma is assigned for anonymous vma */ > + dst_vma =3D lock_vma(dst_mm, dst_start, true); lock_vma() is now used by find_and_lock_dst_vma(), which is used by mfill_atomic(). > + if (!dst_vma) > + return ERR_PTR(-ENOENT); > + > + if (PTR_ERR(dst_vma) =3D=3D -ENOMEM) > + return dst_vma; > + > + /* Make sure that the dst range is fully within dst_vma. */ > + if (dst_start + len > dst_vma->vm_end) > + goto out_unlock; > > /* > * Check the vma is registered in uffd, this is required to [...] > @@ -597,7 +599,15 @@ static __always_inline ssize_t mfill_atomic(struct u= serfaultfd_ctx *ctx, > copied =3D 0; > folio =3D NULL; > retry: > - mmap_read_lock(dst_mm); > + /* > + * Make sure the vma is not shared, that the dst range is > + * both valid and fully within a single existing vma. > + */ > + dst_vma =3D find_and_lock_dst_vma(dst_mm, dst_start, len); > + if (IS_ERR(dst_vma)) { > + err =3D PTR_ERR(dst_vma); > + goto out; > + } > > /* > * If memory mappings are changing because of non-cooperative > @@ -609,15 +619,6 @@ static __always_inline ssize_t mfill_atomic(struct u= serfaultfd_ctx *ctx, > if (atomic_read(&ctx->mmap_changing)) > goto out_unlock; > > - /* > - * Make sure the vma is not shared, that the dst range is > - * both valid and fully within a single existing vma. > - */ > - err =3D -ENOENT; > - dst_vma =3D find_dst_vma(dst_mm, dst_start, len); > - if (!dst_vma) > - goto out_unlock; > - > err =3D -EINVAL; > /* > * shmem_zero_setup is invoked in mmap for MAP_ANONYMOUS|MAP_SHAR= ED but > @@ -647,16 +648,6 @@ static __always_inline ssize_t mfill_atomic(struct u= serfaultfd_ctx *ctx, > uffd_flags_mode_is(flags, MFILL_ATOMIC_CONTINUE)) > goto out_unlock; > > - /* > - * Ensure the dst_vma has a anon_vma or this page > - * would get a NULL anon_vma when moved in the > - * dst_vma. > - */ > - err =3D -ENOMEM; > - if (!(dst_vma->vm_flags & VM_SHARED) && > - unlikely(anon_vma_prepare(dst_vma))) > - goto out_unlock; But the check mfill_atomic() used to do was different, it checked for VM_SH= ARED. Each VMA has one of these three types: 1. shared (marked by VM_SHARED; does not have an anon_vma) 2. private file-backed (needs to have anon_vma when storing PTEs) 3. private anonymous (what vma_is_anonymous() detects; needs to have anon_vma when storing PTEs) This old code would call anon_vma_prepare() for both private VMA types (which is correct). The new code only calls anon_vma_prepare() for private anonymous VMAs, not for private file-backed ones. I think this code will probably crash with a BUG_ON() in __folio_set_anon() if you try to use userfaultfd to insert a PTE into a private file-backed VMA of a shmem file. (Which you should be able to get by creating a file in /dev/shm/ and then mapping that file with mmap(NULL, , PROT_READ|PROT_WRITE, MAP_PRIVATE, , 0).)