Received: by 2002:a05:7412:3b8b:b0:fc:a2b0:25d7 with SMTP id nd11csp2724748rdb; Mon, 12 Feb 2024 14:41:44 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCViZT2LS1O3/JpB62uu5RP+Y7nsCRoRLLCLQH2tE1/vQ8btGT5emEyIXIZaSnJjs+qJMXuaY97iLRuyXhe/+ziyst43zpx93pqXGXJcDg== X-Google-Smtp-Source: AGHT+IFiAM8JVyULfUHrlCBVdjScOXQIKQ54piZAU7i6yO0KJ6iykoI4gMsHUzlPG2a/rtriwlX9 X-Received: by 2002:a62:aa0e:0:b0:6e0:ed60:9a27 with SMTP id e14-20020a62aa0e000000b006e0ed609a27mr1937761pff.7.1707777704300; Mon, 12 Feb 2024 14:41:44 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707777704; cv=pass; d=google.com; s=arc-20160816; b=0IMCsPMnNUvF6iK/tPe8H5nkSK/8rz5/J2anDt9KnDOylpHJ5CTw9av6jGXOuhbeRZ RxrouWvZyBp6zx8l2UV5IOTeVCK7VxY06bzbJOOg2s1dlkOkKUp7y14Q6XcWyInOlpDQ fpm6wmO31u6mWrnjSCiu1u3GWzCreqPMqFn3WlI8g10b0mjIZ4asnBtWcCNbrCUcRAv1 Sup2dDi4NLCTjgV6hqmIn/v+m3Qfsy+Bh5iuqSdTVIP5QbqV5TguLJ64tRxtLkxvD4Ne iS85VFT0MMwfKV2C3AOeu8yxFdVfwr9VY9/qbT4C5xRCQM/IbTtp8/3/E3HNLSJYo28T IfQA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=l9mO69PHSXpV4KszXXC4FO0K4MGQhMEHtnBuaX2b1wA=; fh=C0I0bbUW3j7aSMJhSx2JvukH2CF6XynkVLbEZjo+Kng=; b=upngWXwQ7eGYmrJYPMZSG0dL6qh8NPuuEivHdABNMOBxvNU9h92g6oiazXvJkXsSos 0rcMpm/q6jVfJ85fdFCQcw2ksaq3PSF5kZz90S0nz1oMhJGIOA5p0igog2PWLaudlDqS sRnUThtQKS/j22OGs/2p1qNzpJIjVjTGbrtDFs6T+WzRLCcuD58yu6xcdp6NpSVBy+p4 F4nDoqcjLwDm7VhcGfm0yofoC8Q3zhJSOx7ii3Y2QoCDZfzmHj1GfA+mw43hr64eJF4X oaewXLPyBdfa8Gjk29SdZG3xToDzkOXHwH2kuMXfhXCddBAi+7dUsrwbYNFqkhwFPf6g Q0og==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=xrILMhsZ; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-62485-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-62485-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=2; AJvYcCUBuFRw/71LeCJKoQ3+1tyQDLY2nFsD2NdbggxlCtmQ0EvF946u77aPlTn0chiHQqTKT9W7w1H1SThp5gxpDxwrV8IrV2zxZITWNWfguw== Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id o13-20020a62cd0d000000b006e0258617basi5576522pfg.29.2024.02.12.14.41.43 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Feb 2024 14:41:44 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-62485-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=xrILMhsZ; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-62485-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-62485-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 5C83AB25F3E for ; Mon, 12 Feb 2024 22:32:18 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D3BC84E1CF; Mon, 12 Feb 2024 22:31:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="xrILMhsZ" Received: from mail-wm1-f43.google.com (mail-wm1-f43.google.com [209.85.128.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0D4834E1CC for ; Mon, 12 Feb 2024 22:31:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.43 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707777071; cv=none; b=FjczA3yBOP5meaIRltX1GXuxdNOwdtDAutV4vprJ0abYDHSENI9rU4tHEK2Deo4J63LtQRkzUVHB5zxgd9ijyI4/epkv+YaNFiohxCbQnhkd8phoK0Qn7y0oIJpbIsHQFcFGGP41nIc9zsAwf/04btC21BdLL1+u17VOEmM7QVE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707777071; c=relaxed/simple; bh=OMjB46gs2wW9dar/91UoPpBUG+X9qus0cmG6eJvNhs8=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Content-Type; b=ek/CTdFXKBTHZg0qB902+sT6zBlGusN4OOYAglk+LA2sezy1g8DC5iQF5rFVbiRTUfsTRkLa/LGuTcBIH9c5py34gzhh5gWRWHPDpJ2QJgFN5AVB45fIZatLeEmbG/79mLdTgZujLx17GII5lyarz7AW7ZHZNaLzWZFaWfFEZ20= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=xrILMhsZ; arc=none smtp.client-ip=209.85.128.43 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=google.com Received: by mail-wm1-f43.google.com with SMTP id 5b1f17b1804b1-410ec928b83so7620385e9.2 for ; Mon, 12 Feb 2024 14:31:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707777068; x=1708381868; darn=vger.kernel.org; h=content-transfer-encoding:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=l9mO69PHSXpV4KszXXC4FO0K4MGQhMEHtnBuaX2b1wA=; b=xrILMhsZe+Zg2w/xQ5ese8QL9tTX2Vooc/yrQ4r2hBhmVFw/fF4nNeymXlW7gUjWYk mXS4Qo6mLJEBICQB1IBkpngVR0ivuoBznUpftvW4PHlIE6BNgpFZFu2rGfcuMSw4Y9kt OuKRe5cesh72hF2WaDSr0IurdSDESVpwrrojtTlfm3FbUQNE9JTWCFDGdaTl0N7BV4O0 fYnI+LO1tcan4GUOk0PzzdCqfXX3EGYI4fUUQ/AmOOXXc8xCgAG51j5OV/y3U400TC8f EXSR4h2ssiW6FCVu7VppeOEOssi3StexXTE4Ciu2Luo/5jdxFlysY1G7k25nSD6RR4FB zu5g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707777068; x=1708381868; h=content-transfer-encoding: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=l9mO69PHSXpV4KszXXC4FO0K4MGQhMEHtnBuaX2b1wA=; b=iFgt44aP3I9WbwJrvV82HbJlFZEkpL45Ix31MMrBvzu5PnnhwyzmzKV4lRKysd7aIB XyOLf8ANT7DxCyIdW73Pfib+0FYwsKeJpYUETvveinR/Ijs2uwt/9G1GfqHcM43YywSF MQ5vgehHccRduD8X6F05ZmvfkRk8lrtQL6YQ7kTu3dWWhzq5NgCgsrIcdrq+Gm7KjFUQ DT+zU039IuQcpXTw1fbWN9yf4coOWCFs6ntA9ZbLWTlfSmyFOUZHs1IXy6f/1QgtRbao n4qeGUv8q+hCwPLyqi3y27oX3xQ8AWWNHgsTW236q2cd4Zpo8V5DQRMPofEbVfBSa3ps 5Uew== X-Forwarded-Encrypted: i=1; AJvYcCW94mY56XQDNvz7CshuRBdSteMH6tM/MM29bOSoxkVlJogmqueVDJt6peWA6BUtOYMfF7R4lCnUe9b+q/Qt1zS6MzH1eB8a6ej3dOxQ X-Gm-Message-State: AOJu0YyWMm2/uLyUt1mP99Gfkv0SZd6Gdu9+pz5JqyoFT82lfiXp1miy 6A70CPkBQQkAeDtwbKbbM9lyVznuOVVJuvWbZwhUFZ0dp2/YGvT7kdYBCNHB1baR411qfMfUWih u6LgzUdYxVdCn8QGSGNaq4j/jeRSpnCZjomfxbrdwNYMO0OvYRQ74 X-Received: by 2002:adf:ed90:0:b0:33b:47d0:52ce with SMTP id c16-20020adfed90000000b0033b47d052cemr6385336wro.25.1707777068103; Mon, 12 Feb 2024 14:31:08 -0800 (PST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240208212204.2043140-1-lokeshgidra@google.com> <20240208212204.2043140-4-lokeshgidra@google.com> <20240209030654.lxh4krmxmiuszhab@revolver> <20240209190605.7gokzhg7afy7ibyf@revolver> <20240209193110.ltfdc6nolpoa2ccv@revolver> <20240212151959.vnpqzvpvztabxpiv@revolver> <20240212201134.fqys2zlixy4z565s@revolver> In-Reply-To: <20240212201134.fqys2zlixy4z565s@revolver> From: Lokesh Gidra Date: Mon, 12 Feb 2024 14:30:55 -0800 Message-ID: Subject: Re: [PATCH v4 3/3] userfaultfd: use per-vma locks in userfaultfd operations To: "Liam R. Howlett" , Lokesh Gidra , 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, jannh@google.com, kaleshsingh@google.com, ngeoffray@google.com, timmurray@google.com, rppt@kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Mon, Feb 12, 2024 at 12:11=E2=80=AFPM Liam R. Howlett wrote: > > * Lokesh Gidra [240212 13:08]: > > On Mon, Feb 12, 2024 at 7:20=E2=80=AFAM Liam R. Howlett wrote: > ... > > > > > > > > > The current implementation has a deadlock problem: > ... > > > > On contention you will now abort vs block. > > > > Is it? On contention mmap_read_trylock() will fail and we do the whole > > operation using lock_mm_and_find_vmas() which blocks on mmap_lock. Am > > I missing something? > > You are right, I missed the taking of the lock in the function call. > > > > > > > > } > > > > return 0; > > > > } > > > > > > > > Of course this would need defining lock_mm_and_find_vmas() regardle= ss > > > > of CONFIG_PER_VMA_LOCK. I can also remove the prepare_anon conditio= n > > > > in lock_vma(). > > > > > > You are adding a lot of complexity for a relatively rare case, which = is > > > probably not worth optimising. > > > > ... > > > > > Agreed on reduced complexity. But as Suren pointed out in one of his > > replies that lock_vma_under_rcu() may fail due to seq overflow. That's > > why lock_vma() uses vma_lookup() followed by direct down_read() on > > vma-lock. > > I'd rather see another function that doesn't care about anon (I think > src is special that way?), and avoid splitting the locking across > functions as much as possible. > Fair point about not splitting locking across functions. > > > IMHO what we need here is exactly lock_mm_and_find_vmas() > > and the code can be further simplified as follows: > > > > err =3D lock_mm_and_find_vmas(...); > > if (!err) { > > down_read(dst_vma...); > > if (dst_vma !=3D src_vma) > > down_read(src_vma....); > > mmap_read_unlock(mm); > > } > > return err; > > If we exactly needed lock_mm_and_find_vmas(), there wouldn't be three > lock/unlock calls depending on the return code. > > The fact that lock_mm_and_find_vmas() returns with the mm locked or > unlocked depending on the return code is not reducing the complexity of > this code. > > You could use a widget that does something with dst, and a different > widget that does something with src (if they are different). The dst > widget can be used for the lock_vma(), and in the > lock_mm_and_find_vmas(), while the src one can be used in this and the > lock_mm_and_find_vmas(). Neither widget would touch the locks. This way > you can build your functions that have the locking and unlocking > co-located (except the obvious necessity of holding the mmap_read lock > for the !per-vma case). > I think I have managed to minimize the code duplication while not complicating locking/unlocking. I have added a find_vmas_mm_locked() handler which, as the name suggests, expects mmap_lock is held and finds the two vmas and ensures anon_vma for dst_vma is populated. I call this handler from lock_mm_and_find_vmas() and find_and_lock_vmas() in the fallback case. I have also introduced a handler for finding dst_vma and preparing its anon_vma, which is used in lock_vma() and find_vmas_mm_locked(). Sounds good? > I've also thought of how you can name the abstraction in the functions: > use a 'prepare() and complete()' to find/lock and unlock what you need. > Might be worth exploring? If we fail to 'prepare()' then we don't need > to 'complete()', which means there won't be mismatched locking hanging > around. Maybe it's too late to change to this sort of thing, but I > thought I'd mention it. > Nice suggestion! But after (fortunately) finding the function names that are self-explanatory, dropping them seems like going in the wrong direction. Please let me know if you think this is a missing piece. I am open to incorporating this. > Thanks, > Liam