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 54D12C4829A for ; Tue, 13 Feb 2024 18:25:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DDC388D000F; Tue, 13 Feb 2024 13:25:43 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D8B508D0001; Tue, 13 Feb 2024 13:25:43 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C53178D000F; Tue, 13 Feb 2024 13:25:43 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id B55DE8D0001 for ; Tue, 13 Feb 2024 13:25:43 -0500 (EST) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id ED1BD1605B9 for ; Tue, 13 Feb 2024 18:25:42 +0000 (UTC) X-FDA: 81787608924.28.76DA4CE Received: from mail-yb1-f175.google.com (mail-yb1-f175.google.com [209.85.219.175]) by imf05.hostedemail.com (Postfix) with ESMTP id 29543100015 for ; Tue, 13 Feb 2024 18:25:40 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=hAVNK8Fy; spf=pass (imf05.hostedemail.com: domain of surenb@google.com designates 209.85.219.175 as permitted sender) smtp.mailfrom=surenb@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=1707848741; 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=zAYeUGx8AxWS5HRIkW2e+QEILi32w1ZSQIfACi9TajU=; b=pu55OelXhode15kcMgSKeQGQ+61IDq4BuI6jRh/BxIeM8WDOAKagOZdldnfYQoBU7PcO9n 6IqgFQZbrfDLXc7ONkEBxBGk1xtJuWP3Cxma4tUBL32IRIWFVRrEWr4a5exRY161EAtld9 CgiMEm3htgpL4EFnzMSRiA8IENt4tuM= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=hAVNK8Fy; spf=pass (imf05.hostedemail.com: domain of surenb@google.com designates 209.85.219.175 as permitted sender) smtp.mailfrom=surenb@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707848741; a=rsa-sha256; cv=none; b=CjPhNluFniFOc5tR6N/baAPymznaAHiRAD0Ym2QDW7da6bbTrSRhrhJUvFtuwd8vDQk314 i9lR0yuzvrMufaULpMfFwVJFQfRx8JzvJXI1CtBtGOLDMVFHnNPzhqowQ1/3yIiBihOM6s mdPPG7shZrlqLJ1KicgXoyTA4MVD1KY= Received: by mail-yb1-f175.google.com with SMTP id 3f1490d57ef6-dc236729a2bso1122790276.0 for ; Tue, 13 Feb 2024 10:25:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707848740; x=1708453540; 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=zAYeUGx8AxWS5HRIkW2e+QEILi32w1ZSQIfACi9TajU=; b=hAVNK8FyHmm0GmKMPMWpHUcn3IiZjTFMIuRTFIP63M0tftc5AO/bAK/oIe48D7Rk+u itzihPJ9NHR8R/1s1NT+Gdcpo9rhphbjYojyWkoU6NWvDyL2sUxQegnTr0Q1jTj541J1 fPm51rBNOZ5G1hm+edA8iyKgq1mSzSPBdIewakqN1wojs5SU07phqKmBd0HxjX+Jc2tT VGx2V8VHYJOkxHPS+tfAfW7+UZyTcLfYc/WXeyivTI2xDVuED4Dttj75ARYDVblo2BTo F8WuZdSG4TF6TYmPmPGCbdOJpXkorV0aTfcL9dpyMkLoFg16ptP4vU+1mq/9MbEdKPCq 5j2w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707848740; x=1708453540; 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=zAYeUGx8AxWS5HRIkW2e+QEILi32w1ZSQIfACi9TajU=; b=k720/InroJNlCWxsm9ZLEUHBooVrVfKibQD0T/yjrsC/zLsWTd/u5Guk6+3HPHRkyB KsphL7mGLiGVV1BDfEGu8b+vyuqmXIDR72VSUDJgA9VJb6xA0bqlyg3g+5jQ+HTV246F ROivBIVZKXT4+s99E/lNA4us2NuUkT2vGZsmwz+mrBvrj7xjMJuE5SvVF0NHZj0hGEJT hwpMQF17Zfi3mHRmQCT0kYxnQu4LoXfoF2tZn7N/29B5H6JAdTjQE7hVT+K/3P7/LxyS 6q0Xx1iTxSMMtwSuYhz38OMtVP2tcFCWrMn4vNMqXbQ7WNm97smkMKC3ilkN4X0zZPJd HkgQ== X-Forwarded-Encrypted: i=1; AJvYcCXmoT+wAO9PENMApJBQiGDyXZqiuLgTf/98UtIUhKsdVAGI/wFoc9DW81BKvLm2k8Ss+uXInHzJQ6S5bbrC4PHCP/E= X-Gm-Message-State: AOJu0Yxm+fYSQTMk6N0jjRHGTRnm2qyC/JmLuQOLUzRNOCXKdYMQl5eK 5e2xnxPkUrhjRHzdMm3ydda0806HwQTvJb9Eb4UAnNaAbI/4tQtoXLJ5hNRfspgpHIh0siiaH2t L58M5CsWgtNXpXAdDHNLP8ZlJ9sP7LQWTRtQy X-Google-Smtp-Source: AGHT+IGdRu8XKjUJKRkKvDcznUTg4skBmOiJkJhaDQ64RKBRaLdu5QQmvMpAyevdDiDk4nn6WgNwUyj1RoTIjp+I6YU= X-Received: by 2002:a25:5f09:0:b0:dcc:6e60:7024 with SMTP id t9-20020a255f09000000b00dcc6e607024mr48458ybb.45.1707848738732; Tue, 13 Feb 2024 10:25:38 -0800 (PST) MIME-Version: 1.0 References: <20240213001920.3551772-1-lokeshgidra@google.com> <20240213001920.3551772-4-lokeshgidra@google.com> <20240213033307.zbhrpjigco7vl56z@revolver> <20240213170609.s3queephdyxzrz7j@revolver> In-Reply-To: From: Suren Baghdasaryan Date: Tue, 13 Feb 2024 10:25:26 -0800 Message-ID: Subject: Re: [PATCH v5 3/3] userfaultfd: use per-vma locks in userfaultfd operations To: Lokesh Gidra Cc: "Liam R. Howlett" , akpm@linux-foundation.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, selinux@vger.kernel.org, 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 X-Rspamd-Queue-Id: 29543100015 X-Rspam-User: X-Stat-Signature: kcguqqfepitui6fhn6ettgugj6jqgxde X-Rspamd-Server: rspam01 X-HE-Tag: 1707848740-310995 X-HE-Meta: U2FsdGVkX1/mLwhfJHKRWAsynBqFGqOu9wqtU+I181iCgZJGTxryEBrrDVaV79YQhGTQsoLhjtRhNokg4eoKTb6lGvoZO2LyO/Zjv1hGwkvxdAuXuXzL815vq+AeUPJ/y4uqi7a68QPkWyOjqdN667qyLbxQUiC1X+eNCt/PfxW2RgcOULrZ/A460Eq1Qxz8g1FMQgVV97X+YNjbHDnOKZlZ+V+vw4Xxs6iyEvkbLYYwaHEs24Di69Xq5kSQXYOm+2guNvgh8ViZL3ITpy/saIBIkBKV3G4Cl2sCkndgffhLQSEd48RQFNCVbFYSzuaGItIaQDtjWzHjXNPIsV3YQtPZripCDPr1yeE1X3MAnaLVuKWkZjv34s1gUulvcX42U/9QrJQt1hlO75EsxhK5DaI+AcF7E886U3iEmwlJMmt/gvpL07XJ3kvWJfgb/+tm5w0ZfWGm9AxeQmINowCkCsNF+orA+Eh/9YffvOmjXu9W12YPeRskeKvIxWkvMIFKxU0NZpIbucQHvxaYxa/j4oUfVsE0WAghDiOgs7/JrOn8q0xGr9p18pFB2CktTmfLbcYv2FfFlVrGxN7lHh7Yx8HDMT+uTcXPuq9Xs+uYVnwysD3lXLF4fXpho2go1aYRhlJZiFIIJzut48u7SSFGC0h5iXpOWxdQMw3t5yt052I9ch95tWhOOYw3/YSo7Ldi9r5CLW1KyUykRJPSqcOsm4k5k0CfcSb49giOp+Ex9QfDW0r5sZtbDcFKKOEQS0ZqS3J3ErFyw03F+sx4N1eXJ8TJIqwpVtQKc2/5puOw3uIpofZUzKOLA+QP32aLbvFYTV832unECf24NIexR5H6uhpw3kEcL276gf5zqmI58iBKyfLKVGpmoPKZj1vjk8pP1GO84cwn4g4t4JmiJQEj3HoBXpgRE8Dx2E7BdwhQSSpn7g1GB01uRC//Km5Gh+6WTZGrgdU+OOTwsVVnjgt zukeVPGZ Qq7MZQkbebvlI/jEtSfEGrqRGMueit/a9yp77jqxc4BcnozzvXpTrHvfcVbJqQYYa4T/ZxpiuqjiNPol1wemB21ccH2+jJDnFlwvhkD9IaVlfZcZFlXWDc7FlzB0iti1NysW6c1E3qnETUx7XGjoCsBMmWU1U45jmwbatPtWmiZjToEUG9n8yY1himG9MWG7KW+xrAyyryq+cPa/eHFrH2H1abThgb/j9dt9ev2OEJYFzCFr90AsZzPXu6R/NSPnjiA0tpvK3KG0J5PU7JvTZxwqUDA== 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 13, 2024 at 10:14=E2=80=AFAM Lokesh Gidra wrote: > > On Tue, Feb 13, 2024 at 9:06=E2=80=AFAM Liam R. Howlett wrote: > > > > * Lokesh Gidra [240213 06:25]: > > > On Mon, Feb 12, 2024 at 7:33=E2=80=AFPM Liam R. Howlett wrote: > > > > > > > > * Lokesh Gidra [240212 19:19]: > > > > > All userfaultfd operations, except write-protect, opportunistical= ly 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 mu= ltiple > > > > > vmas. > > > > > > > > > > Signed-off-by: Lokesh Gidra > > > > > --- > > > > > fs/userfaultfd.c | 13 +- > > > > > include/linux/userfaultfd_k.h | 5 +- > > > > > mm/userfaultfd.c | 392 ++++++++++++++++++++++++++--= ------ > > > > > 3 files changed, 312 insertions(+), 98 deletions(-) > > > > > > > > > ... > > > > I just remembered an issue with the mmap tree that exists today that yo= u > > needs to be accounted for in this change. > > > > If you hit a NULL VMA, you need to fall back to the mmap_lock() scenari= o > > today. > > Unless I'm missing something, isn't that already handled in the patch? > We get the VMA outside mmap_lock critical section only via > lock_vma_under_rcu() (in lock_vma() and find_and_lock_vmas()) and in > both cases if we get NULL in return, we retry in mmap_lock critical > section with vma_lookup(). Wouldn't that suffice? I think that case is handled correctly by lock_vma(). Sorry for coming back a bit late. The overall patch looks quite good but the all these #ifdef CONFIG_PER_VMA_LOCK seem unnecessary to me. Why find_and_lock_vmas() and lock_mm_and_find_vmas() be called the same name (find_and_lock_vmas()) and in one case it would lock only the VMA and in the other case it takes mmap_lock? Similarly unlock_vma() would in one case unlock the VMA and in the other drop the mmap_lock? That would remove all these #ifdefs from the code. Maybe this was already discussed? > > > > This is a necessity to avoid a race of removal/replacement of a VMA in > > the mmap(MAP_FIXED) case. In this case, we munmap() prior to mmap()'in= g > > an area - which means you could see a NULL when there never should have > > been a null. > > > > Although this would be exceedingly rare, you need to handle this case. > > > > Sorry I missed this earlier, > > Liam