From patchwork Fri Dec 6 22:51:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13897897 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 5D7C2E7717B for ; Fri, 6 Dec 2024 22:52:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C53F16B0307; Fri, 6 Dec 2024 17:52:12 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C05706B0309; Fri, 6 Dec 2024 17:52:12 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AA6F76B030A; Fri, 6 Dec 2024 17:52:12 -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 8D4846B0307 for ; Fri, 6 Dec 2024 17:52:12 -0500 (EST) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 3B22AA0875 for ; Fri, 6 Dec 2024 22:52:12 +0000 (UTC) X-FDA: 82866033558.23.2962683 Received: from mail-oa1-f73.google.com (mail-oa1-f73.google.com [209.85.160.73]) by imf20.hostedemail.com (Postfix) with ESMTP id A53071C0003 for ; Fri, 6 Dec 2024 22:51:51 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=d0HOi8Ik; spf=pass (imf20.hostedemail.com: domain of 3GYBTZwYKCKgacZMVJOWWOTM.KWUTQVcf-UUSdIKS.WZO@flex--surenb.bounces.google.com designates 209.85.160.73 as permitted sender) smtp.mailfrom=3GYBTZwYKCKgacZMVJOWWOTM.KWUTQVcf-UUSdIKS.WZO@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=1733525512; 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=2XVlPHpEZtu9xIw4Qap8alaO3+SGpEYwVQX0iy/v7kQ=; b=nV4L6DZcNUNkVEFUl7uzGhq6HIB8YXNAF3vEtCdQawMNW+7A0fwTDX7Ee2bowQauhVjipN a3G8dHbRj0l5SzajYPwHjW+HOo34xviHqZ/RAXTM8K4VUf7ikvYTgdpt1vGJ9JKfcE1auH rzOuuSuklVEqOJ0WcsNWP+qdCcYhvQs= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1733525512; a=rsa-sha256; cv=none; b=77QPRSwWRetMHVv99OJCE0t2QJAHLxbWBYOvjruz13+R7Gg23KRLWJX+ZjwFSSkqGMFujD n64kGnoYD3VvLYtqqD/7dS4xdICuJxwKB4iCoWe8cGfX0WyGvTvtWmlSTD6waUTn2JFTE/ R0C/dyRVkS9bXL0q7ob1L4ODdZ94nWg= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=d0HOi8Ik; spf=pass (imf20.hostedemail.com: domain of 3GYBTZwYKCKgacZMVJOWWOTM.KWUTQVcf-UUSdIKS.WZO@flex--surenb.bounces.google.com designates 209.85.160.73 as permitted sender) smtp.mailfrom=3GYBTZwYKCKgacZMVJOWWOTM.KWUTQVcf-UUSdIKS.WZO@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-oa1-f73.google.com with SMTP id 586e51a60fabf-2975e172b3bso2318112fac.1 for ; Fri, 06 Dec 2024 14:52:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1733525529; x=1734130329; 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=2XVlPHpEZtu9xIw4Qap8alaO3+SGpEYwVQX0iy/v7kQ=; b=d0HOi8IkjUGZPQy6Qk3yXbY2AtET9l0uQBbkHXFS3cWkq4JfYec5kQOjrIvFzcTKsR Yic1VMGmJuLVxm7uPtrm9WsMSSbRr1OZrB7qs1mcdl9a+EOlTqXgD6HivlMQEXz/L5S1 2hcEgWagi8huo/yV0tPfF2UPp72Lj0XDX3a2/u/6RKxNLJVZJUsSiuXxamRza3vJSTwW pMbirYIn4fqEc0yH/lsTTz24LOC2w1NAr8laCFysnINNpokLxBo5K+fx/G0vzgfB106Q Wff9AUCOWDj8dBklXaVFDAj5KWBtZsTeExsNtJGnvi3tBcklqIxFuZnJrGoepPl4o8al lA2g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733525529; x=1734130329; 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=2XVlPHpEZtu9xIw4Qap8alaO3+SGpEYwVQX0iy/v7kQ=; b=vQCo24uhtj4wKFpvgSiMCXbkepUWTVgOKzUDKddEgxMmgYMwH818WKym0n3VJBg5ly Qg/+14Cscg1N3u2WAvcoN9RwTyK+kxrj94sdy9OiMjSF0Qp8nQH2u/Etc3dGEnMPk6Mv r9aFnGSj/JVGbdINiYQCwmyJwE+XwYAoYj6OWiR0hHNLK/nFbTHOY3tdIAUU91daCdt2 sbH+n1cdXklRw5xXVkZdkGvp/sMdR9ZaZ2rmFbEOL3nec7cFKjZy9sug3iQRo8yfaeMz 0SB5g9mBEL5OvEDuWunEZJqWvar14CYeGjSRPHP3gsgJxBfDHYHtrJoyut+w3FbAjRRt /OKw== X-Forwarded-Encrypted: i=1; AJvYcCWtyX5CyCpraCKJJzE1IgyMWbIDgALSvWQb5zLhI2cCo51+kEu+Tppty1X8LDxfLyHqGttc3SKcWQ==@kvack.org X-Gm-Message-State: AOJu0YxOh2zdXX9K9tiUpXxrCUOpyFVl4mxxjqq7IRK5GaHqMz/MP9+x pO2nuXabkkulbQv4fQbAiIwZJzjor/4SEKP3E+i3oPGIqaBZ7HTRRiBJIo8Q7OKWYwCYCAMDPtO vDw== X-Google-Smtp-Source: AGHT+IE3XhAvDScGGmsUTdbbULESWT7yEme/wNWVflifCM9618rjWWcRcIta7LlU8v32EA39xFZwUHCJlPc= X-Received: from oacpd2.prod.google.com ([2002:a05:6871:7a82:b0:297:2483:5994]) (user=surenb job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6808:e8c:b0:3ea:5161:f71 with SMTP id 5614622812f47-3eb19ce52a7mr4865569b6e.20.1733525529219; Fri, 06 Dec 2024 14:52:09 -0800 (PST) Date: Fri, 6 Dec 2024 14:51:58 -0800 In-Reply-To: <20241206225204.4008261-1-surenb@google.com> Mime-Version: 1.0 References: <20241206225204.4008261-1-surenb@google.com> X-Mailer: git-send-email 2.47.0.338.g60cca15819-goog Message-ID: <20241206225204.4008261-2-surenb@google.com> Subject: [PATCH v5 1/6] 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-Rspamd-Queue-Id: A53071C0003 X-Rspam-User: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YMefL+9WQ9CP569PEV2/NwCWqvs7UDtLaye6epQYRs8pp2wDmqnfwRNJORTbou8CKB/YywCX17pqluSD1GV3g2POPuG4XDkxbuBjjOU7nV15lx7d4P3XyZUY+9p3tIrq45pSbq3gNObY6ucUCDMFAUrcEo6Ii1MbVy9wHiTBcMZxRJHL50MKL7h7G7Ke+qZ7Cl4o1pHqo7K1o1uF12dgrb+CYNRnSMoSjP+HgIrKtkyU48nND/yvlWWqJyeJEZTUyQy0TFOD/TFNzgmo6aIcqPq7rIRmXRhBVO/H/dt9bmFZpWNhpK8/q920fVR6IfRcFNBySGzc/riopeKbK1gthxuOB0hx3O7itoprDkn48Q7nuUfirHdNFKiEAUi0kce1s8bKzF4E4usDOLfiAMmVFLGwgBNZF6qZHvbGPgmKKWIHGeWGqK7zDbFG49kLBJ8aQLvRt/0eefUypSf5VztEMm+qMbuDTN/WlPr039RPFi598aoqzOAUkBFT/CyR67eBGJ/ZLHkFuzevaMqdteEJwz7GMpPlBgPyBij1PmJpQuwZWmQUFcfS95s9P8adZHUD8KbIDtphyO7JI8aTDBvgWtkQNEZfMSXwc8qFczp3PtfLWz+ij1WJxGMdV4AEFJvo3ftEXhsjOb5TMmi2Y5fNG8LInIYG1ebqxhyVe4Wo= 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 --- 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 187e42339d8e..c4a001972223 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -734,6 +734,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 8e16dc290ddf..bc9a66ec6a6e 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;