From patchwork Wed Nov 20 00:08:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13880638 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 E487DD6C2BA for ; Wed, 20 Nov 2024 00:08:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6AAEF6B0085; Tue, 19 Nov 2024 19:08:36 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 65CC26B0088; Tue, 19 Nov 2024 19:08:36 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 522A56B0089; Tue, 19 Nov 2024 19:08:36 -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 3570B6B0085 for ; Tue, 19 Nov 2024 19:08:36 -0500 (EST) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id E0EFC1C6FD6 for ; Wed, 20 Nov 2024 00:08:35 +0000 (UTC) X-FDA: 82804535562.12.1D0F31C Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf07.hostedemail.com (Postfix) with ESMTP id 265B340007 for ; Wed, 20 Nov 2024 00:07:23 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=AOzvSOy8; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf07.hostedemail.com: domain of 3gCg9ZwYKCAcz1yluinvvnsl.jvtspu14-ttr2hjr.vyn@flex--surenb.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3gCg9ZwYKCAcz1yluinvvnsl.jvtspu14-ttr2hjr.vyn@flex--surenb.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1732061247; a=rsa-sha256; cv=none; b=dW+4Pqn/nuP6IB77pw7d8dm7ZaV8qWu0KDF17tzazpzAlCmZ2PhzEBnDdtPuYa+N8/iTCt WJC04mn9YM4+LQN5Xe/jQXDtAW/We1jq1lZ82DnBeUI9zJ2kX87DnAkYOS8gclPhaQ45iJ bA/RGz4Bcizoz9cmmOqfpSX8VUkA4jw= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=AOzvSOy8; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf07.hostedemail.com: domain of 3gCg9ZwYKCAcz1yluinvvnsl.jvtspu14-ttr2hjr.vyn@flex--surenb.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3gCg9ZwYKCAcz1yluinvvnsl.jvtspu14-ttr2hjr.vyn@flex--surenb.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1732061247; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Ve1WLVFYOSXc3Opo5MHJNWqWiXorXcfFJ5bxi7J2prs=; b=4NObeW3y7JsNHTpfc8XGxry3HFRQx89i0+50X47X0IQJeuerRKhj14kRHYQS4dVKXw+qXH JTnMInicqINBUeipJC/9ZmJA62ggYKlD607Iol4v5MlPqP80mNyCxqPiL4L30fuG2jRQZj mowmuoy8ArHGadjWZEJ+gYtvtyYYnP4= Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e3826e5c0c0so2797315276.0 for ; Tue, 19 Nov 2024 16:08:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732061313; x=1732666113; darn=kvack.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=Ve1WLVFYOSXc3Opo5MHJNWqWiXorXcfFJ5bxi7J2prs=; b=AOzvSOy8hkS2JHlDIR2I7ZGwxNI3djXYe7xbytLgEGd9P38Wlxj5j+IWRCa8peKmcW ZXzR6hInwLJPEQmLwSMh1BHRHLHCQmNrSdlo3zWz5rxNuf+ydb0V7xUwJWpwWGoV7il8 Rg9eUcv+i9OX5y26pJn8zb5mjvLwwdOkY1vqGh6lM+fiyUfK7NCflOmnQXEBjO01fguQ oeji9BM3yKstuKJfkbOm6GbKcAQ5v+82x4Rs6u40HMSJn5BXr8vYnNhb6ihLZcJgytBH q3b7P85GPF9/1A77iaVexgX6h7VX82ptxONHuILzk2f5X9h3SKv7lCjRhQcOrYuMxZ5O sfyg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732061313; x=1732666113; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Ve1WLVFYOSXc3Opo5MHJNWqWiXorXcfFJ5bxi7J2prs=; b=BeCEIUj+bXdwOCfe7NBxW0ai22dxWG9erEgBjf7MLBZ1NjPWiKXycZlqyfXqb50Cp3 gmSss9PGgYdGb5V1AFuu10YDrfNtTbFhxS0yaWqYYPWlQlCkH57/JqIE4nfpo8KYljvi lPj/ixNCWg0v55OVBcdOxFwnCNCsDdrQsFLsbIaPftmou9VPtv5QuoLs4UYd3XPDLORP mKL+TFEhlkD6VzDu2n0MhljsQu7Dm3u+6C+MStRz4IenqrnFKCKISLKz+rIIxgGiBe1j jSlyoaz3Hu1leS/lqQ7+Dk7bP/E8bob0h7ZdyP0TON5R/CuGCKiCoNugJHVSz1SGbUse Iz8g== X-Forwarded-Encrypted: i=1; AJvYcCVNXt7Cy2pwHYQqzSm8e+tE+ajxHQ95SFP2guqMlkmrkmOT+XXPouH+Teh9w3iisrlspfOcAWNPug==@kvack.org X-Gm-Message-State: AOJu0Yyn3KYeoFsryQgXDyv+VIVVxiOidGvzY6JD367HUp7FKCxkeve3 PeaAZwuL5spRDX5KgF2YkToPZNsXLhmYjxI0TCYr957OFe05tb9sW40aWS/tsgWPKw2JfNmpOx/ Xnw== X-Google-Smtp-Source: AGHT+IEyLwXvDXLnYSqPv5azfkHnhcNjKGMYmPlWD8dNq1sLWn2jjgJONFBDd+BEsDvXmrivA7Y5d4vobs8= X-Received: from surenb-desktop.mtv.corp.google.com ([2a00:79e0:2e3f:8:af45:bbc1:fc75:e695]) (user=surenb job=sendgmr) by 2002:a25:aa22:0:b0:e2e:3401:ea0f with SMTP id 3f1490d57ef6-e38cb70b565mr451276.7.1732061312659; Tue, 19 Nov 2024 16:08:32 -0800 (PST) Date: Tue, 19 Nov 2024 16:08:22 -0800 In-Reply-To: <20241120000826.335387-1-surenb@google.com> Mime-Version: 1.0 References: <20241120000826.335387-1-surenb@google.com> X-Mailer: git-send-email 2.47.0.338.g60cca15819-goog Message-ID: <20241120000826.335387-2-surenb@google.com> Subject: [PATCH v4 1/5] mm: introduce vma_start_read_locked{_nested} helpers From: Suren Baghdasaryan To: akpm@linux-foundation.org Cc: willy@infradead.org, liam.howlett@oracle.com, lorenzo.stoakes@oracle.com, mhocko@suse.com, vbabka@suse.cz, hannes@cmpxchg.org, mjguzik@gmail.com, oliver.sang@intel.com, mgorman@techsingularity.net, david@redhat.com, peterx@redhat.com, oleg@redhat.com, dave@stgolabs.net, paulmck@kernel.org, brauner@kernel.org, dhowells@redhat.com, hdanton@sina.com, hughd@google.com, minchan@google.com, jannh@google.com, shakeel.butt@linux.dev, souravpanda@google.com, pasha.tatashin@soleen.com, corbet@lwn.net, linux-doc@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, kernel-team@android.com, surenb@google.com X-Rspam-User: X-Rspamd-Queue-Id: 265B340007 X-Rspamd-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FDCBVQ/3Fu4kBteO7JKeex+lmESuzz/LJ/HPUsigzY3EytOOHu/LLm2EIJANANHulfUwA8zfdiTj1ZkVZOpGA5G3hlpGr++xvbJhH1T8+SUwJOxkUeKbqA+EtG5NKoHNKU4Sdxr6nlQvP1rejgjlCuTPB7nDb1NkNl1aNlYhr4cr78ZP56AV2Wb01QyxYFjr8SwBadBA0WCWF2OaErp5gfRn/LDYAHW2cvIL29BnnfsRLYwYv56Ky7CX65CVkpSpOLwlTBRL42Z9VGuuAdbqY5E+t9oad/OKAfD6/GP/AZygwpRU5Ol2mwOUCit29OxDi65WK62jGq8nAF5y3RBQl8fla5llG+Zt9IVG/4++0U/chXD5529uZqo3Z4JzY3eJ9CmAykviE/9Xi6QiDp7xTOvXWZfcNpZuA5qbmQ5rLvkwzKyudQuFNJyp7TvbnLCwZwozVVyvNgJM3ydG1swg0MZyweFsQIPR25RRTX7XurPUDlgjhIziW1az7nnzMIHc0pNk3eS+VBMXPCpW4JCEQ2HHW8hW/XExJMvI2me1rsHmB++xy47IVHgvSIeNnci+LmixBe4G70oYBTvfiPzIZo9bxN6SP4sGLvaOTJXaEhpFeGp+T4+E2s8qahEEF6/BojNKPaAMr4euFq82u5TW77tymx5kh98e1o= 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: Introduce helper functions which can be used to read-lock a VMA when holding mmap_lock for read. Replace direct accesses to vma->vm_lock with these new helpers. Signed-off-by: Suren Baghdasaryan Reviewed-by: Lorenzo Stoakes Reviewed-by: Davidlohr Bueso --- include/linux/mm.h | 24 ++++++++++++++++++++++++ mm/userfaultfd.c | 22 +++++----------------- 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index fecd47239fa9..1ba2e480ae63 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -722,6 +722,30 @@ static inline bool vma_start_read(struct vm_area_struct *vma) return true; } +/* + * Use only while holding mmap read lock which guarantees that locking will not + * fail (nobody can concurrently write-lock the vma). vma_start_read() should + * not be used in such cases because it might fail due to mm_lock_seq overflow. + * This functionality is used to obtain vma read lock and drop the mmap read lock. + */ +static inline void vma_start_read_locked_nested(struct vm_area_struct *vma, int subclass) +{ + mmap_assert_locked(vma->vm_mm); + down_read_nested(&vma->vm_lock->lock, subclass); +} + +/* + * Use only while holding mmap read lock which guarantees that locking will not + * fail (nobody can concurrently write-lock the vma). vma_start_read() should + * not be used in such cases because it might fail due to mm_lock_seq overflow. + * This functionality is used to obtain vma read lock and drop the mmap read lock. + */ +static inline void vma_start_read_locked(struct vm_area_struct *vma) +{ + mmap_assert_locked(vma->vm_mm); + down_read(&vma->vm_lock->lock); +} + static inline void vma_end_read(struct vm_area_struct *vma) { rcu_read_lock(); /* keeps vma alive till the end of up_read */ diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c index 60a0be33766f..87db4b32b82a 100644 --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -84,16 +84,8 @@ static struct vm_area_struct *uffd_lock_vma(struct mm_struct *mm, mmap_read_lock(mm); vma = find_vma_and_prepare_anon(mm, address); - if (!IS_ERR(vma)) { - /* - * We cannot use vma_start_read() as it may fail due to - * false locked (see comment in vma_start_read()). We - * can avoid that by directly locking vm_lock under - * mmap_lock, which guarantees that nobody can lock the - * vma for write (vma_start_write()) under us. - */ - down_read(&vma->vm_lock->lock); - } + if (!IS_ERR(vma)) + vma_start_read_locked(vma); mmap_read_unlock(mm); return vma; @@ -1476,14 +1468,10 @@ static int uffd_move_lock(struct mm_struct *mm, mmap_read_lock(mm); err = find_vmas_mm_locked(mm, dst_start, src_start, dst_vmap, src_vmap); if (!err) { - /* - * See comment in uffd_lock_vma() as to why not using - * vma_start_read() here. - */ - down_read(&(*dst_vmap)->vm_lock->lock); + vma_start_read_locked(*dst_vmap); if (*dst_vmap != *src_vmap) - down_read_nested(&(*src_vmap)->vm_lock->lock, - SINGLE_DEPTH_NESTING); + vma_start_read_locked_nested(*src_vmap, + SINGLE_DEPTH_NESTING); } mmap_read_unlock(mm); return err;