From patchwork Sat Jan 11 04:25:48 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13935767 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 6ED54E7719C for ; Sat, 11 Jan 2025 04:26:14 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0111B6B0083; Fri, 10 Jan 2025 23:26:14 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id F02106B0085; Fri, 10 Jan 2025 23:26:13 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D7BE36B0088; Fri, 10 Jan 2025 23:26:13 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id B45B66B0083 for ; Fri, 10 Jan 2025 23:26:13 -0500 (EST) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 6411FA1092 for ; Sat, 11 Jan 2025 04:26:13 +0000 (UTC) X-FDA: 82993883826.18.EFB32A2 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) by imf01.hostedemail.com (Postfix) with ESMTP id 837F940006 for ; Sat, 11 Jan 2025 04:26:11 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=KEqKck12; spf=pass (imf01.hostedemail.com: domain of 34vKBZwYKCBMBDAx6uz77z4x.v75416DG-553Etv3.7Az@flex--surenb.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=34vKBZwYKCBMBDAx6uz77z4x.v75416DG-553Etv3.7Az@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=1736569571; 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=1I/dUK+s3BXYUQU4Ou354w8mERlKGBrfGL48eT2fYzA=; b=OxVj2niB01T7M3cV73zf9O//iq3YYHU2zrPAXzqXiR5MsWLv0MhiuNfHsxh0JXlKT/A0Vh qBqOaNuVcWvh1H0zZV7a+pYqNc+bkSjWDzL+N/VWcQ3o/z3917/EeYpDOZMEC9ieRqXLGm LB7acqde1Qc+39C7Dn0OKkn6FZRKdW0= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=KEqKck12; spf=pass (imf01.hostedemail.com: domain of 34vKBZwYKCBMBDAx6uz77z4x.v75416DG-553Etv3.7Az@flex--surenb.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=34vKBZwYKCBMBDAx6uz77z4x.v75416DG-553Etv3.7Az@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1736569571; a=rsa-sha256; cv=none; b=HNcU1FaFRgNWNMmMly9H7O81w02FcGEK3hUumm/7TmfYoNRUiJ+Fl6lneiJ4J6cEsw/5io nKhXgnqSSzwsQW3pHNiwiLyK6uIk90/lAej9h3aSeX2VRoGWx0+iy5kV8LsSsDqBkMp7fL RZlDV+7iTIuU8LMLY6PfyzurSVU+ZIw= Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-21640607349so58336975ad.0 for ; Fri, 10 Jan 2025 20:26:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736569570; x=1737174370; 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=1I/dUK+s3BXYUQU4Ou354w8mERlKGBrfGL48eT2fYzA=; b=KEqKck12co6FWbvlNzdmITnNpguBq5ftLoA9MOVJsMnDUH2p34P0a2FZ84DioS+M9w xJdE7B7/qwVbmJQRXI8mA8tb9YddhcXy1rq/Jyb80fvgimE7Tomfl2pogAvtQLcMFeq8 3wMjtbnhSCJ00NfxMr09Ebc09jyElGZkkgmqpBx+fI4yhc3iygMjPmCrEka3Aegha+/u qNb5O2NRSN0LHZ6cCa5m3U2YMRKagqCuwQCwX3DgNZUWLOKIx2U/GLoxlIyqlAwbZv8w 9N0J6m2K+/bXXGlsm4UgmUH9ucVz17DEUPpKpFraT8VahhNigzGLnNt8Dlke+vd1mTkG Ie0A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736569570; x=1737174370; 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=1I/dUK+s3BXYUQU4Ou354w8mERlKGBrfGL48eT2fYzA=; b=xBu4+YrhYEhgaq+ogbewvmH9EI8adZeRVIWbaGBlsf+M1CRWD6XMqevfzdvkOGfKkw U0BqdJ7iXaoZbcm1trTFcX/0trdGmehm2P/EVQ/6puk77Pk7ZCC+mTXFxoWOrrRHf4Om RfWLOIjWrZukc/emuKlXZvS1PgCw5Hr0VxXWfWuLzfixQh4J3XYdkxr/NMjwwiLgZ1R1 JGJFpbLX11tpCXSArhHvL7Knv5mjA+N2nf4jYrfK/xVscvwLfbH//c2DhGSrh6MfrC9m gBzbyvdsp/RD5CVGsOLA7lt2GrwBxjmmxkjN4qRDgiaQE7rxzrTV/F8RH/sG9qRBZd6v UL3w== X-Forwarded-Encrypted: i=1; AJvYcCXJtUhn9eeUA13Di409vK4vTO817s8vA6FLtVAJvVPGKLOTJoqf6p11m1a6CDdj89OUTzVbvk6Y8w==@kvack.org X-Gm-Message-State: AOJu0Yxt8AfH5HTYV/HPvAtRL5/RAaVI9azx8hBfW26LimqPQpotHLYs VOhokVZ5nbj193PT9Vh7LpOQ2THenR8Tve97oMJZZ7EWSqWXIT4DT4pDbZz/1Xah/AweRd9jFp0 odQ== X-Google-Smtp-Source: AGHT+IH+ZTrdGgYL0EsT9nxpjvST8Wb4PiTOAlOCLTZpVYAikP8aBq9n9KO+/zLUPpIwa+3cew7Rdi532wM= X-Received: from plrf18.prod.google.com ([2002:a17:902:ab92:b0:216:61ba:610]) (user=surenb job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:d58a:b0:216:6c77:7bbb with SMTP id d9443c01a7336-21a83f573ccmr189000015ad.17.1736569570248; Fri, 10 Jan 2025 20:26:10 -0800 (PST) Date: Fri, 10 Jan 2025 20:25:48 -0800 In-Reply-To: <20250111042604.3230628-1-surenb@google.com> Mime-Version: 1.0 References: <20250111042604.3230628-1-surenb@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20250111042604.3230628-2-surenb@google.com> Subject: [PATCH v9 01/17] 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, david.laight.linux@gmail.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, "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VQVNJNxwiMsKJULh/eTyjNx7OvA1M B1bSyDV5 FQ9JRlZmet5Fp3FzGaAJWUVa36mOlxhuGDlMgjfz3Z/2rpXMVPBcdqywK47TJbznDg6WkPEI+vAWrhiTASbU5Te2I9q7RiYwF+CGX65v2leegyR9lu2ILS5VqZp/R2YnB9Sbku2DxqSMU90IKX+h2ap5BV1f5r0iuzJd5JNuIquZ9D4C7l8T/gYYMcilrkGW42V2HqeH1C56y4LGa5R3SiSJ89OsFq/r7Z31plhwP1X0BFxx2vLY9kUkvI5ghCRdim6D24bLdIzfa3/9NDKuDuflTTlJ6jzNxIEZ0P02B6A4xTQEkypNpOdecFRYvy8QFwa7O+hMgou6w5SIeoxfb5oKWblG9+REGTX7upfCUP7w8MfL2d7Tfmm/A3kOERaLcpY1+xxweTBBUL5+zmFR92V4VkjUEBBvzQE/MZ//7449FTaN7E0i4xsNU9Mmm54CBsIjTnYLFGDXh0lkTeIjSieRr62toX9wuZ0TVjqjePLqizCvL0BOpXy0hXWM9V8X3OhDkQODyV/kSPPFQU0FgoXtyG0Qv6Sk8KFTI1ZMTxLRw3oFlYdKmRJaIyz+/idrZb7t9YUZSOZF2DOYwP5d17YVDXm1p8MUrCQkGyrKFmYc08A6IbXtXRt5glnoibIMDvKKQTLgMhNJnABNnaP4ckGoe1LVbl2nKdpGmGGFb+EnhWg53ZBzR7oAxALEV51k0qnllolvZmcVxlflrPkZUYEtj+yhEAwdA2i58sxhxH3XkSKM= 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 8483e09aeb2c..1c0250c187f6 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 af3dfc3633db..4527c385935b 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; @@ -1491,14 +1483,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;