From patchwork Sun Nov 17 08:09:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13877796 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 02220D68BF2 for ; Sun, 17 Nov 2024 08:09:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 66D559C0018; Sun, 17 Nov 2024 03:09:42 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 61C596B00C3; Sun, 17 Nov 2024 03:09:42 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 471939C0018; Sun, 17 Nov 2024 03:09:42 -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 272626B00C1 for ; Sun, 17 Nov 2024 03:09:42 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 9ADB21A0165 for ; Sun, 17 Nov 2024 08:09:41 +0000 (UTC) X-FDA: 82794861828.15.51E27EA Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by imf18.hostedemail.com (Postfix) with ESMTP id A73501C0003 for ; Sun, 17 Nov 2024 08:09:18 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=L5LGEs2o; spf=pass (imf18.hostedemail.com: domain of 3wqQ5ZwYKCDMhjgTcQVddVaT.RdbaXcjm-bbZkPRZ.dgV@flex--surenb.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3wqQ5ZwYKCDMhjgTcQVddVaT.RdbaXcjm-bbZkPRZ.dgV@flex--surenb.bounces.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=1731830921; 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=CZkyrFrgAwaR5MxHWX7P6WpGLYaVbagX6YvpJwNn5k0=; b=aLDXQ5QzXrcQAXLFqGM2QAGZkEMu2sYxZyq9kcskzcYJ7K+n2keJwGgGDU77YvQI+qlqhn YPBv8Y1nfEqmtcYXLyTI44Z8wizOwOxQW3JLk3QJoNChjQkpM9ZSF2G/DgYMgus4oklXWn O7IdvuwZ9cwu9P7cYTAY4FsQ1L6RRWw= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731830921; a=rsa-sha256; cv=none; b=x9oM61Zz3rG6db4bchJnP5eeWjP3fJIQc7+ikDMBR3cQkLk8Uylk+wdQcPFb4mYsuT5iFZ va4vm79+UajqjkxRCCsJv8Uy1Or1bc1HBJm0/q1ERp6jj96jV4lgmRwnWauYwqD5fuhCCn vJMigHEdJKeqlGIbb2VVpUE+HE3y358= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=L5LGEs2o; spf=pass (imf18.hostedemail.com: domain of 3wqQ5ZwYKCDMhjgTcQVddVaT.RdbaXcjm-bbZkPRZ.dgV@flex--surenb.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3wqQ5ZwYKCDMhjgTcQVddVaT.RdbaXcjm-bbZkPRZ.dgV@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e3886f4cee2so770679276.0 for ; Sun, 17 Nov 2024 00:09:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1731830979; x=1732435779; 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=CZkyrFrgAwaR5MxHWX7P6WpGLYaVbagX6YvpJwNn5k0=; b=L5LGEs2oIHuhsUbE9c5/S4F0fY5zICJvhO9gUVR5tKC4UOzP6ge0yHsgU8b5zSHPyu W990Fsg9SiAjPIs8Exa3LYVBKyyTTvubxLNSgvNCUuP0yLDRU983zKnKN06xFxZ5ZQix YiDpa7UkdqmqW950yPU//5HM5xkqT6FcqrnK3I2gLwCALLfHF0cZEARgCcZxxRFqUllS sCYN6x2EGQC1cYJ0NEMeJRkOPFY6qF1tMIW5l0ZE/VHy+NGjQvigaEqxogcucAsOkQhL AxhxlJCm56eGg6VeJJv26XuGwrN6Qq9jWOOQ1/wfg4CE8q0kXyIpFhDfRyuDnhIzbS/2 l5kw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731830979; x=1732435779; 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=CZkyrFrgAwaR5MxHWX7P6WpGLYaVbagX6YvpJwNn5k0=; b=OTW57LF1FHLRRB+dFN9lW8gwowYIUs2aK45oSLijIXexmzOpxkOpCC8ZmYdUPoBkna ZvT76REeMl5COAEgmmVvGFCgZSiot8MPw4hBUTgEeIZP84FyCYl9PAKVlIdMcixYrPY2 yOhm2HA/8XnnyisJheuO7tIR0BJb7CvELQi+UHxv70MsLfTmUcPl4ej4TCiNCt+pyE9W LI9gb3IBHuy90RQvtK+w6tMMgCv7U47sYbksGL+oR9MDSwZzjWvtbhvT3s64GGnSu8iR JT78PObC/LL0J5ePhzHQS6HObNy5rQ4xUt12dj5f9LVXk+spCNHDR0ihe7jjgoHX7ent 8ZJA== X-Forwarded-Encrypted: i=1; AJvYcCW8TlCCezFVtG/eFkqaU5ncIBbpaysSQWoA1kMgARdA9F63nCMNTo/0zRTG9P1k2OAyj0RWK2zKKQ==@kvack.org X-Gm-Message-State: AOJu0Yz+X98rRfKzh+B3FEXjAxAY6fyp3dH4/1EunO3rrZPHxSoTWkPK U5lM1DFRGMYRiYwUkAMqzJIOI8mn25Er7CWkRTIH1WeQYHtNJAYtEjxcxBKwen5Jcx9ByXdzrmh /cA== X-Google-Smtp-Source: AGHT+IH7Nd8pXV7CscbKL6Oe02lMMMIInGW3q+Det1fKSUjvn9CktAIAKXjQIOt60hBNRgC9PV8ouBWRm4Y= X-Received: from surenb-desktop.mtv.corp.google.com ([2a00:79e0:2e3f:8:bafc:6633:f766:6415]) (user=surenb job=sendgmr) by 2002:a25:9d85:0:b0:e2e:317a:d599 with SMTP id 3f1490d57ef6-e3824ac0065mr340301276.2.1731830978836; Sun, 17 Nov 2024 00:09:38 -0800 (PST) Date: Sun, 17 Nov 2024 00:09:27 -0800 In-Reply-To: <20241117080931.600731-1-surenb@google.com> Mime-Version: 1.0 References: <20241117080931.600731-1-surenb@google.com> X-Mailer: git-send-email 2.47.0.338.g60cca15819-goog Message-ID: <20241117080931.600731-2-surenb@google.com> Subject: [PATCH v3 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-Stat-Signature: tnmr4q68r9du8wgcu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CIL69 3zUlqmu+A+6ESgHAOQ5tsO6fCGcz1DhBmVVGk0v77NR0eo1mkiYDVxxRAxdpwrRQCJTYXcKtFXwwdLiARSCgdTU5Bb7smqu3vUFgtNIuGU0YbI1L9/Nd5kJDySvaNja7cDKrREc/H/ttUbkJLfgG0hWO+jrdujNiv6/Qd0GYoc37kzS5LCXdlSU1782nGLI+DZMXb1HSmM5SzektmyfGTY/8F9zg9JcF1Tmw8JYZfXZNw2Gk4zIVNkizVt4kgUohpGFQK/3L9YvQN+KprB1pw01vbmxak+gAHgsVWy/x75y9o3HMJthtr7tYecYaTCS0nLYKP0bJgKAPQi4Q1/zVC1MCaPhq6APMzQGpn3w1SZpblAOhnF2CNg/MRu4PE1X7Cg9MeBDCp1tXweuPlmYmZX9OvnfXMSUpH2NqZjtEjUpwv3n6neWNrWWVip2vrpCm6ohDdE7XqNGPdA60uQtl9Lm+TjsbxtoJujcCQ53lj/vk6Zb+E1ED1c3ZBbFBrkubkI8G4DlCqtgtITSXNTU3/Pld45lvGifld4TWV1OkzT+q2Bn8vDpDOFy461qOyk9NuKV4OGtrdqBm6+oGOPe+k4VHJqA== 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;