From patchwork Mon Nov 11 20:55:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13871367 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 73C91D3ABF4 for ; Mon, 11 Nov 2024 20:55:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E2CB96B00D0; Mon, 11 Nov 2024 15:55:15 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D8CC26B00D1; Mon, 11 Nov 2024 15:55:15 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BB8F16B00D2; Mon, 11 Nov 2024 15:55:15 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 9848D6B00D1 for ; Mon, 11 Nov 2024 15:55:15 -0500 (EST) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 4C15A1C7230 for ; Mon, 11 Nov 2024 20:55:15 +0000 (UTC) X-FDA: 82775018088.21.9BE13B9 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf02.hostedemail.com (Postfix) with ESMTP id 4440C8001B for ; Mon, 11 Nov 2024 20:53:56 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=iC03jwuV; spf=pass (imf02.hostedemail.com: domain of 3MG8yZwYKCBkHJG3C05DD5A3.1DBA7CJM-BB9Kz19.DG5@flex--surenb.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3MG8yZwYKCBkHJG3C05DD5A3.1DBA7CJM-BB9Kz19.DG5@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=1731358459; 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=fTrDTuUPZ0AkCFSB2C5v7YkZ1U+virIStaXPWI+ocS/vYkQGpHR/sDCf42q2L61ov7q/XV 0l2ptGk5hUNixGIeRcB7XX3hy1U7Tl+TqSKnsCrBeixUt/6uHgf/y7G/Ug0bxVcIc7niW/ UGLuZw6b+aRBbuJH/f0Mt09n1MwSHOY= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731358459; a=rsa-sha256; cv=none; b=zHGhr7KwxyDzgQLORUVstPcCHhks/dQ0mwhWsvFl/Mr8JQqpt/YECYpMlOZC9qfZ8WDkEZ QR4r+WYChOH2SyUDUNQ3FxmWJfCxRHEtD+pIgRmZ9gi2cw6UBCN4SWjiW8w01PID4IBjDh W7S0m+R7eIY9mhMjTOFuUVRCkY63Ipo= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=iC03jwuV; spf=pass (imf02.hostedemail.com: domain of 3MG8yZwYKCBkHJG3C05DD5A3.1DBA7CJM-BB9Kz19.DG5@flex--surenb.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3MG8yZwYKCBkHJG3C05DD5A3.1DBA7CJM-BB9Kz19.DG5@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e28fdb4f35fso7296663276.0 for ; Mon, 11 Nov 2024 12:55:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1731358512; x=1731963312; 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=iC03jwuV3hW7J/8egjY52QJI9kWwodL6JT09xBbgB1/W3KiLdN/wU8xorGkwhJxKpA kH8LbOveqx3HeKk4HZrd/RaANP6ubdvKWldOpkMnLpccZKSRQVLw9CMgkbT+75FFpxDe EARD/2PO69xUa7KVOsrh3YIo6FU+s9KmEhVfkrVme6eHU6vE0FxTT3zXbHki5yBYFSuh 9t4Yvqm+N/aXMpJwgZ7ar5pnYXf9C3ifOJ65E432c+KL5GCUTg7oE+9oCf4tk6vL8MkG KHDQcuJCEI/E3od8r/yo2B0TcCxb3wtNT62vUJVLzLywo9fSzftdOqG0QCwxxW3xuYwh pXEA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731358512; x=1731963312; 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=MInlW8cnJACr5wbchayQaswRfSv3hmN0WJacOkGtyghwJsv0zrK0QruTuHXQgJEs+L 7OZNKD3X8IMcPZzKQ9e/cjCL3ZcKepR9msrViQnaJfCkkyHkwZFCQz+OcZ32wisDt+u2 oiKl8j9czHhtd/8jFsb+qvIxX6AwjzF9FfgczxQz6mLTbRG5CJRbnGkiANfPRtnFLIuG 35QbxKnR7sCb6P8CIvv7y7UWtwlr0mRBrVTCd4F+sbNyuIDD5QBr3SHKNBdLP9UFkyZ1 X8+FPEdQDn6sCMQCGfcsfSQ1xkgLyVz58XSPSFR3ySUvKUgMuDSd1JEySceEmEl4HzPC o5jg== X-Forwarded-Encrypted: i=1; AJvYcCXmYrN4OUw8tktxfCm451c9OMCkgvSjjZn7XOCesg6URRmCS3K6G8bQ8qpxTdF3GsfVYXSFtdKHXg==@kvack.org X-Gm-Message-State: AOJu0YwounXZgvvxmVUkr75lzw/vgzkoFy0TthMIY5z+ojRGMfjnJGcM z+D84squvhrq/BqD7xG7GvPsYO3jfBl73Zwu41pI61Di2YuLv/RFFuU3wqyzS3fg3EIs39MnBHd 4fg== X-Google-Smtp-Source: AGHT+IFtRvBKCg21ZonSdl0i6oAaNOlWcK8osbIXD1VUy6Zm+UF4tEaZT/nKAdEJtdzHD4gxQkKcsjXP+Js= X-Received: from surenb-desktop.mtv.corp.google.com ([2a00:79e0:2e3f:8:53af:d9fa:522d:99b1]) (user=surenb job=sendgmr) by 2002:a25:af4b:0:b0:e33:104c:fac8 with SMTP id 3f1490d57ef6-e337f8d7427mr11316276.7.1731358512081; Mon, 11 Nov 2024 12:55:12 -0800 (PST) Date: Mon, 11 Nov 2024 12:55:03 -0800 In-Reply-To: <20241111205506.3404479-1-surenb@google.com> Mime-Version: 1.0 References: <20241111205506.3404479-1-surenb@google.com> X-Mailer: git-send-email 2.47.0.277.g8800431eea-goog Message-ID: <20241111205506.3404479-2-surenb@google.com> Subject: [PATCH 1/4] 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: h5mnzs5jfyg5qgy9ak4crzrychc48mfq X-R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3sZxvGaq3KOKbaDyXJpz5pwOy1RF2/KebmdWHMoazARIEwwHCxkyPGUnLrS36gxlpU2Zl19mT7lVvxujxsTvS314COSakJR9dvi6Z4zs+x+WIE5NTtFRqsImStQ31YCodEUXBJQA6xWd+9fJi3+9pWlW7iVKWlD5URa3DIfUPgRK7f7BW/FV/ZBNHqMPmJhIR/kxl5mcBnty3xYgqvGt/Jikwhy2/ZfzDWQdmZI2jdkznzhmoUgHT1S26jyX+dzShAQ7eoPkICf+jSAFH11tk+LYXGhEFTFSJykxsm6I7SilTAY9pazJUyZ4U2HWLn53dVPHYPWBgZTVBCRYV4nTZKCHFLqPPsOvrE3O6EBLRBTSFGiDYqodWniyRJAn4Ls6YlZkYkOjmTv/hyEVpyxsK5lisDPVginSfrU74VUdqPY3DPHV1dDqE3lgseRFlBJmDP+x+fOT+gCK6WBF2rx4MCvTBKpyD8rccWFVeojzRe6THdmcnOHkAhx+ven/JOz+9fPUxxg74DANB7DDgtWvTW45464bd/iJfR300g5uhiwRGjRgFGDrS+rZch51WtgPrwAlWRA== 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;