From patchwork Tue Nov 12 19:46:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13872892 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 14E10D32D97 for ; Tue, 12 Nov 2024 19:46:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9C9F26B00B6; Tue, 12 Nov 2024 14:46:46 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 97AEB6B00B7; Tue, 12 Nov 2024 14:46:46 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 81AA96B00B9; Tue, 12 Nov 2024 14:46:46 -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 5AAAE6B00B6 for ; Tue, 12 Nov 2024 14:46:46 -0500 (EST) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id D883B1A05C2 for ; Tue, 12 Nov 2024 19:46:45 +0000 (UTC) X-FDA: 82778474772.04.0D58927 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf16.hostedemail.com (Postfix) with ESMTP id 48C3A18000B for ; Tue, 12 Nov 2024 19:46:02 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=TEpaduYC; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf16.hostedemail.com: domain of 3o7AzZwYKCBQCEBy7v08805y.w86527EH-664Fuw4.8B0@flex--surenb.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3o7AzZwYKCBQCEBy7v08805y.w86527EH-664Fuw4.8B0@flex--surenb.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731440627; a=rsa-sha256; cv=none; b=4R7ABvZMVY8RxK6h6yTCZ3PhRx8mzNvt2vi5rQxhIMwfkihGfzkMRgtIZ5vi42yfC/aBZe vbdREVja3CL0edT3FXgXbZY0g2rKSW4f5M3gXNcZbyga1n9eONF0JqI/rMpuhEnut3rO5i zt+mMEPpy5nTlf7WxpkNbzPew6wjBmY= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=TEpaduYC; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf16.hostedemail.com: domain of 3o7AzZwYKCBQCEBy7v08805y.w86527EH-664Fuw4.8B0@flex--surenb.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3o7AzZwYKCBQCEBy7v08805y.w86527EH-664Fuw4.8B0@flex--surenb.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1731440627; 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=c6L2TsxMW3xdptYp+SxjhxiQb9Rn3imOm+Hs2oG6vZc=; b=i69qkW8/NH/8K9MUF+LrYoMEsSwvWNHzHSzHEPx1ak57S8PqfMf2qWLYsgMAmVmy7Hjt0C nvOFfeMitL8zRz5WIhvkEAgutH5+TzqRPOO8Ds++3Ht3OhzypdBotVNM+bgsFaAJoVSK9X wM0ZaDPZIxmZcrOrrSMV9mOyjFB/4X0= Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6eaae8b12bfso96127557b3.3 for ; Tue, 12 Nov 2024 11:46:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1731440803; x=1732045603; 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=c6L2TsxMW3xdptYp+SxjhxiQb9Rn3imOm+Hs2oG6vZc=; b=TEpaduYCrZv3qq7pCqlLlBnJmpUTA2+Ml2HINeIzU3tGTguPLsO9GS97GTCRGIVzJm xHYESgJso94Gp4Fyx+2Av7KIjMcvGLxlztYniKiQGxIFnw93mxaGVzPzXWt0KHGCstbt NZSEA03OwEl02w51Np5MJ+i56Z0r0Nb2Jcg6sDfM9fm0xdKfnec6QWrQS/82wxroiiQN bPnaGlB3Ii1wa6q9FX7X6H+EFKZPajiL1CyjgXy2BkG3iYCX58Xg8PCl+6n9pBGFz29b f2Kxvcc3lSlLlQu/n4e5ap/8gfkTAf2TLj9hvzLi9qXR9FBQ7dLLO/5AhoKfK/eEPxSL yX0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731440803; x=1732045603; 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=c6L2TsxMW3xdptYp+SxjhxiQb9Rn3imOm+Hs2oG6vZc=; b=fJ1cMYo15i2P7UyrCr6CWeaZ59FMt0+ga6yIPJLRdlXuu7JE4aw6N00GU4RGY6iEQA XRD1HdAhpNFe9DgJDyhTfJ6JvKoXi3PC1vtT75BJ/eNSSyPPsE9Uq0yyYkR/bPCCzO0S W5Os93GRFEWRPU7yorxaSl4RlqLXyaknKAcadfn4Hj1U+Y5NYr0O1nZOH/dU4ErLgKtx OdnqYij30bTEvIG5l/VFbV0VgQspx+d84VSKRUVMJVxTvXaMy4l7q6W7oAKht48pI/6Y 4ubltndpg8tfrApq5nnBMAaS7YxnTkaX1s1KvDWbKt+I/D8+XDJLA3nehltRMEDfBJ8a VDYg== X-Forwarded-Encrypted: i=1; AJvYcCVJe7YrCsw6Fw0GEKL++FriQzcuW5tHg8i1J4IaubwSEqr83ssSPbIG8Nmv/UzUboMCDP4ibrdf0Q==@kvack.org X-Gm-Message-State: AOJu0YwzKwvyfRCEpQJzvhRGqBpcOTNLgu6/1UCOVZr692qWXV6Cb0Pr /+xKsHQARTlWeQZM4v3vkKbG6VBNGn1XuWXqUh0sShbO4S0zg5XlBIgM5OhtkovJXlFVCQSpkjM pnA== X-Google-Smtp-Source: AGHT+IH3ogaKA/6SIpqGiWEzmqcQ3qZ/Nx5QjPItSCKNvMM699jDmgRp92hUZbCEx3dlegBqK4JNX36Bnsw= X-Received: from surenb-desktop.mtv.corp.google.com ([2a00:79e0:2e3f:8:cad5:5dc6:1298:ec42]) (user=surenb job=sendgmr) by 2002:a05:690c:6e88:b0:6e2:371f:4aef with SMTP id 00721157ae682-6ecb344400cmr3697b3.3.1731440803073; Tue, 12 Nov 2024 11:46:43 -0800 (PST) Date: Tue, 12 Nov 2024 11:46:31 -0800 In-Reply-To: <20241112194635.444146-1-surenb@google.com> Mime-Version: 1.0 References: <20241112194635.444146-1-surenb@google.com> X-Mailer: git-send-email 2.47.0.277.g8800431eea-goog Message-ID: <20241112194635.444146-2-surenb@google.com> Subject: [PATCH v2 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, linux-mm@kvack.org, linux-kernel@vger.kernel.org, kernel-team@android.com, surenb@google.com X-Stat-Signature: 5mdb7tfzkyhujmbd9q3oquooyof7uu14 X-Rspamd-Queue-Id: 48C3A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GPxCK/SJIAri4w81bovQNMsHK8KMn/lRKRmOXLa69qh9YFpWou6Fb0z/nJJYPR3bqc3m073VyQg8xD+YzXCT0DaEqMMIss1Pc3qXzqjjyD3RoiHTcvP+IXswosqmS8njtxA+ofkKC6aexK+DnQOhEa3f+DwXR7JNDng9vdoquKq43hChIB0WK/9qhDDmOwEfmJb5AaPbLS3TtZOnsoANCYv4V4PPsWHudzWIayKZohm9jqD6eHOdW5xhZJ0YVQgnfp8XdXrFlDaZlzphFy9gCoDjubnrlAPzV8eEP5BEEaEdzqFg0Nxa+AI3RgD/g5WgzYj4oBVAox6ckZ6rnlzEftwAwu/UJOlC29JOFeWVp0AVHlGqnruf2rhVXVukM81xJwYvt5Nk6wDCLjssTZa205t7tc0hdmkyR0/Tm4YZSlyqc/gUd48O6E8YS2KSnqfO7h0YQpEAnHKRbHIyRvr7A0m0gcbITnl2wlE682Q93jWGOSHVa9+rnV6rSE8/DJnc52PSG1Lk6Zaip427kS9xNUThMVSB8GM7mYpXYxqulNIVXeF7cpCNNQ== 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 --- include/linux/mm.h | 20 ++++++++++++++++++++ mm/userfaultfd.c | 14 ++++++-------- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index fecd47239fa9..01ce619f3d17 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -722,6 +722,26 @@ static inline bool vma_start_read(struct vm_area_struct *vma) return true; } +/* + * Use only while holding mmap_read_lock which guarantees that nobody can lock + * the vma for write (vma_start_write()) from under us. + */ +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 nobody can lock + * the vma for write (vma_start_write()) from under us. + */ +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..55019c11b5a8 100644 --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -86,13 +86,11 @@ static struct vm_area_struct *uffd_lock_vma(struct mm_struct *mm, vma = find_vma_and_prepare_anon(mm, address); if (!IS_ERR(vma)) { /* + * While holding mmap_lock we can't fail * 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. + * false locked (see comment in vma_start_read()). */ - down_read(&vma->vm_lock->lock); + vma_start_read_locked(vma); } mmap_read_unlock(mm); @@ -1480,10 +1478,10 @@ static int uffd_move_lock(struct mm_struct *mm, * 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;