From patchwork Thu Jan 9 02:30:10 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13931808 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 77ECFE7719A for ; Thu, 9 Jan 2025 02:30:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 005916B0089; Wed, 8 Jan 2025 21:30:35 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id ED02B6B008A; Wed, 8 Jan 2025 21:30:34 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CFCDC6B008C; Wed, 8 Jan 2025 21:30:34 -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 A71FA6B0089 for ; Wed, 8 Jan 2025 21:30:34 -0500 (EST) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 32EA6141607 for ; Thu, 9 Jan 2025 02:30:34 +0000 (UTC) X-FDA: 82986334788.30.FD9CC29 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) by imf14.hostedemail.com (Postfix) with ESMTP id 57D81100002 for ; Thu, 9 Jan 2025 02:30:32 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=TVI400mt; spf=pass (imf14.hostedemail.com: domain of 3xjR_ZwYKCG8fheRaOTbbTYR.PbZYVahk-ZZXiNPX.beT@flex--surenb.bounces.google.com designates 209.85.216.73 as permitted sender) smtp.mailfrom=3xjR_ZwYKCG8fheRaOTbbTYR.PbZYVahk-ZZXiNPX.beT@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=1736389832; 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=L2pdotIvyMZQNnGmgTfsGf0xKAYx/XKZavKJSJfbFgY=; b=EngorJgFA49apk4g4X2iODX9wG15bWQpq6UpEerug1AJ6uFG0XQ3XcQdKvYVOmvc6p3Ei0 gy+fJ8JhBXstlx2/1dmq1OtOWY/Zr2+/LPxyFD9H4F6J/cfGWwtAZYNWLv1P5NjxTi0aWj Pgphln9Ei2fUQBVanNJqAu5Mfu2i1V4= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1736389832; a=rsa-sha256; cv=none; b=qBcn4XC/OyPx7S7zhj/H5tqgS7R2UL7k3EvU4IznCeDyVJrDjEPDH5+Wq4tm3+oJaZYeYr hMi7TLtgzeIpIZDb52q5mTpAuJfDGdd42BCP8z2wK2+O1S8jywZtkLY1JqCTEBcRyCXIjQ rDJS6q2tvFV0MNw/03rH890ICaJUiX0= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=TVI400mt; spf=pass (imf14.hostedemail.com: domain of 3xjR_ZwYKCG8fheRaOTbbTYR.PbZYVahk-ZZXiNPX.beT@flex--surenb.bounces.google.com designates 209.85.216.73 as permitted sender) smtp.mailfrom=3xjR_ZwYKCG8fheRaOTbbTYR.PbZYVahk-ZZXiNPX.beT@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2ee6dccd3c9so748490a91.3 for ; Wed, 08 Jan 2025 18:30:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736389831; x=1736994631; 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=L2pdotIvyMZQNnGmgTfsGf0xKAYx/XKZavKJSJfbFgY=; b=TVI400mt5eTyM3PkAICAOTzt0RM2wdM0XMWpdOp+f3ldDZt7WoJisolVYjNzYkU8Al lHDDeiqQ9mpqivBzycXURMiAcjyPsvlvQ3sMxCV2YkwfieK9Zok/Vz2p1PCCJDBYuhnb uUdQ/HNenSISXA7FxU4jzIpSaEiZt5FUy9D8jTA1ZRu2hw0eAi6EDjjau48+psYm6uMC RSQRnpZccrBCNbz/eYyjEQplbXEK5XUYs+tQSQ2zZhcQkXDAf7coWoQ2v8vI0U+oAfvm 2wUVznxcdW5SyllAsKxgcJImAtQ6YV9fTQ1hJ/7D5CnGhb/jWPTpuC3Dut9wAg18FUnj 43LQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736389831; x=1736994631; 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=L2pdotIvyMZQNnGmgTfsGf0xKAYx/XKZavKJSJfbFgY=; b=f6qQLBdDEiU9HAL5G3NC+6nGbkC2SABkp+zl440uZ/jt9e04HjaUbOoLON9VJ2ylZh fdxLunKNnJicLhl7c3NhTlN+n7765YDO+1MTlS4zVOZO55i4ZPMU/Oz0w6MOp4rk93Xp jeUTFwY+eXA12hrahsYT2qijzzCATUV0B0de9iGZ6MsWtfdWhkpu2SPGnyvDINiEtCYI Yhx3WYl/BgZOxlT82Du3fJzVne7Ya2v+WBxb5wMqGPaA7rGtgXUOVij9Roo0cbu5uejf uMohq5KJ8vxMX9YXl1yY0VigQsQI7slFpUWJG1f2Lr8hZ6EFpufRwB18O5y8TgisqPcc fLtw== X-Forwarded-Encrypted: i=1; AJvYcCXfiHONEwQYJBC+ZftdH3C0jPgE+huAd+5pzadocc6rvouShImpJ0cToMjWIFszwPdbIHBQW6kipw==@kvack.org X-Gm-Message-State: AOJu0Yw2Kwhf50JXnDHycFhRkfqVBy0YLBdXhkQaLj1ZpEWFg0QHkl1M 7MNbDN28gDKzcwLBmhHDmBbKkuGq6fqH9FKa0Vp49eQFNSdd+hZG25v1R0wRgX+igIkQ22R/Xaj qEQ== X-Google-Smtp-Source: AGHT+IHWgi2ldN3a83aKZv2dDntq/xf/7vQGhigjGpu6Kf8lGzADe54dCpxgONH1NIDSqnEDQ0UO7ZABvv0= X-Received: from pjbnb3.prod.google.com ([2002:a17:90b:35c3:b0:2ee:3128:390f]) (user=surenb job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:2748:b0:2f1:30c8:6e75 with SMTP id 98e67ed59e1d1-2f5490e89e0mr6525677a91.32.1736389830966; Wed, 08 Jan 2025 18:30:30 -0800 (PST) Date: Wed, 8 Jan 2025 18:30:10 -0800 In-Reply-To: <20250109023025.2242447-1-surenb@google.com> Mime-Version: 1.0 References: <20250109023025.2242447-1-surenb@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20250109023025.2242447-2-surenb@google.com> Subject: [PATCH v8 01/16] mm: introduce vma_start_read_locked{_nested} helpers From: Suren Baghdasaryan To: akpm@linux-foundation.org Cc: peterz@infradead.org, 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, lokeshgidra@google.com, minchan@google.com, jannh@google.com, shakeel.butt@linux.dev, souravpanda@google.com, pasha.tatashin@soleen.com, klarasmodin@gmail.com, richard.weiyang@gmail.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, "Liam R. Howlett" X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 57D81100002 X-Stat-Signature: zmauy35dabaomxm4n53sqadpneynuwg6 X-Rspam-User: X-HE-Tag: 1736389832-101360 X-HE-Meta: U2FsdGVkX19dRvze2aS1v0oMjXQ6+lEbRCXmBewb+pV2MtYIjbb4om3sJm77GPI1SCpvsIVyen0hJWf9zfVWTNCZRcY+uYwlC7sYt8ff7LHLqfKQJxSgHqpzgw0ULaaMorWcaljdYE6l2sAGAKmp0Fdmswi/1VN2mu8rHmI4/B35FwNVngUYm+KEpasNFNR0BhOldwg8amUBGxvQg1T72zIJ4GpmFi3EmJR8lMbU1VckL1mKOEYwmIlBuCU5UsTMY0JPWnuiZ6/qYVpOTDS5+cob7vms9h+cR/65HFB/p8hcIpeU155JhX0lozdcJR6YWGRFf/1PxtDrCQ95hM0ZiOoZBw04esob1qjcWBZx3LJwUUYlLEFmlFeYlseZTAwjK/8Pi5CmpoXgpgufFG0bHu4K0V4H837sXSG9A2duFkKv30yw/Ekd+96/ajQA+Ooq3qB1OABVBWR1JILsWR/rk1WyWx7c9s6GU3lkEQlHbaD5swd/KaRx+wDNbyYaEowBj2ni1z0YpSRzPz02sdIWi6vI2tSEbIyjBHYNSeIR8DXfo4+uvr/0l3LY0t2JXtkrI8OXILgUqbq9SejHIYSYjOdUs3iQbGjO+yEgEAusaGlvBWnhCfZt+cmVuykf9cSXmoO9xNF4N3kdjQ/zwGXq14akO6F0uh7r+pzrsrwSXcTynxlUeAEknVswPCVVcEWz3a6ZDWNzXTQM+ZRWC+w1OycWrvpst4Tx8gF0/MVnrrk6XUZdzwokODXxH9J9VO70Y2YsDRbhfMSVwz9vyVhsErkTe+Yiki0P8ZdWF6G3l7xndVa4FuGmhpF+SAdhQgtTavzJp8hAdfytAhFIGAi7egaPwpjwLkXqBgL4JFHHl2cLflcTrBhAfSBEpC3ARRXeUUP5Oai9dTyBG76uE0HcuvCG91f2dfZdQ9rNP6Cowqtj0+khRWDNcBpU0UKYkHUOgSGNPtsPDf+rbb/OG3W xgSKwIbW ekBgQ1OXyANaNbK7n0y0QJNIL7hAfkwF9KqmTGHwW8PY/7j1OTl8qh7rM5apMNqfpPywcfJ63IeDN+sqJXgKKoU47ZPfVF4YjttbAwP92dKMeXC1O1e97BE5EuaRvcXbgJnP/5noZbBSbi9gmAir6W2u2PTyaKWP/snXf00BwCuDdwKC/8YTkyJZuLCxDDgFVAnkP/MvSiAQCc8WlmQoGNwFPvLSeU/0H0hiB2M9EPZCpFTixcmM+318Mwnr+B/y9DB21G94oQTJCQ9VKnm+utDwa5UwNjq44Fgp9E6rBD5KcJTLRkqe9B8idN4CpQpWZPrW+H4XnQ9IyJJQpN8H4hjv4QvvieVXiT4sHn4Dg00RqZjFOf2H9YCwS8H/I/+cSS6Lw2X14bwnUIrhcrZ0Jxgi/J/cnxy5h3nU5TC0RpnY8dh9EuYmLW3vUgA4U/9iBQqhh2VTSiZyZ2SWtbklkZsvrqXlT2vXXTCOZ8ihCHM4nfj16YM+Pqgav256in67n3Ul5yHvIkV04cLhrimFX+CdWEGNtcO312onWh5Kq971/SwcMeV0hTRmCmvenmyf4ygk2sscnm9/b7Rn8lLI5NRnSoyCn4QTC8hTgVgDtbudTHBPUpBNbdIbYVbkDW1xn10776R1aPTrU2QB6yOLTvKT8WP0JEudht8UzM7w+hXBf6JMf3N1kNRSPd5u2kqFN0UKkN3ypqwPKy3TJhigLBiMZtVlFffz8h6UZ2u/wJ9dhGqU= 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 Reviewed-by: Shakeel Butt Reviewed-by: Vlastimil Babka Reviewed-by: Liam R. Howlett --- 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 57b9e4dc4724..b040376ee81f 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -735,6 +735,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 11b7eb3c8a28..a03c6f1ceb9e 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; @@ -1490,14 +1482,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;