From patchwork Thu Jan 9 02:30:12 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13931810 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 1250BE7719A for ; Thu, 9 Jan 2025 02:30:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1DE776B0093; Wed, 8 Jan 2025 21:30:39 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 165766B0096; Wed, 8 Jan 2025 21:30:39 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E84826B0098; Wed, 8 Jan 2025 21:30:38 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id BBCD36B0093 for ; Wed, 8 Jan 2025 21:30:38 -0500 (EST) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 5FF84121702 for ; Thu, 9 Jan 2025 02:30:38 +0000 (UTC) X-FDA: 82986334956.24.9EDCEEE Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) by imf03.hostedemail.com (Postfix) with ESMTP id 8361920007 for ; Thu, 9 Jan 2025 02:30:36 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=NJ6QhQqT; spf=pass (imf03.hostedemail.com: domain of 3yzR_ZwYKCHQkmjWfTYggYdW.Ugedafmp-eecnSUc.gjY@flex--surenb.bounces.google.com designates 209.85.216.74 as permitted sender) smtp.mailfrom=3yzR_ZwYKCHQkmjWfTYggYdW.Ugedafmp-eecnSUc.gjY@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=1736389836; 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=mca0wUeRB8q4/QThhp2wCRFx+30mwBcyzZfjxbePC2g=; b=lLPViirS1/t4XGrlyHyHT+QN7rhgKuFwPyFafwG6m7NS8FYmQEmhKDDGNbYfP2/rWrbgE6 C0nVgOEyzZB7DvJ2kw15zoJq+79vHD3O/cojS7Lh2Cd2V0AT1aBueoUYCKlfzF+bd/HKUk Bzdcen0sid2DJWxaE2GyM7RFHsDEL2c= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1736389836; a=rsa-sha256; cv=none; b=sHCO7qhRQ6cxkTRtc/DeBHeXp2YAs/3AdwMXX2tCGp7gpTPlPCX79NzAK+UsQYtt8aRpU7 RhhwCleDvdvk9iauUQB7xml4i3KvKaN9sbxmdf+5y6W2rHkN4sZ2dwPx4EKdYMIzKkFqd3 amCa1HfN9CmpaRX9Pb+J4BdNoX/iD7E= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=NJ6QhQqT; spf=pass (imf03.hostedemail.com: domain of 3yzR_ZwYKCHQkmjWfTYggYdW.Ugedafmp-eecnSUc.gjY@flex--surenb.bounces.google.com designates 209.85.216.74 as permitted sender) smtp.mailfrom=3yzR_ZwYKCHQkmjWfTYggYdW.Ugedafmp-eecnSUc.gjY@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2ee6dccd3c9so748641a91.3 for ; Wed, 08 Jan 2025 18:30:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736389835; x=1736994635; 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=mca0wUeRB8q4/QThhp2wCRFx+30mwBcyzZfjxbePC2g=; b=NJ6QhQqTE5u0msGn5IO73l9+FdBGEa0kXWsp4lVpXx0+d/7RtHRt+jRNPbbBkyme2O Kb74AgEkIblDLNio+r49XpAv3bq6r6+1tc3vFtK4izhh6qyV9yDiCBXf2YPBz/0E43s0 rGDiZaqT+UhZKOUUIgfBKvAlsPYfYEW4vR6JLSa67Ufe0v/t77RSGMpUyJGeTNFUdAcb 27swQ1Q2QGCHjLrPvsyt0aiUtJmrPiLNp1uydXdACw8yMuddmvlCvZAPuvavQ9R99+CL rjtYTi/ZaEdEh/QpSoeZ/mMOzYlDU9XwCvJGIspKDHC9t9SP6oWAO2LNbAMx3vO2zB39 djtA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736389835; x=1736994635; 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=mca0wUeRB8q4/QThhp2wCRFx+30mwBcyzZfjxbePC2g=; b=xOEaGnKs6mYO6/+S3ixxCaukdAjmkt/pCOQVp8/SYW60ikTvSLsD8t2eQjSvJ0+u58 RXutXpxH60H7J7iPHpdfcpqdiZdRLefrBYVSmtZYXF/Tf1SSPx+AeBENayJYcnzpqk3R fkG71cdPysAv6L9RM1i6XizUrsKRDZXTKpQ9BV0uAOfYc7KjnMmxK/6UaEt7N88Do2Pq mZho5/yiqjtScvwu7/QEwttwtv7C7FpFSHtGLXj2Iw8zW8U7a3NHd3vY0GsCgHPkQKB/ gGzH9ELJWmy139Bg9wJWxKv3LVyXmEfihLOKtxGJBoD0fpoPy5x8U8usH00gIEPRJVzn CFaw== X-Forwarded-Encrypted: i=1; AJvYcCVixItwgXzWjJjATBdJ8mx9l9h+WGaidq7aGF+DIZK5b1ijrxpcsBRSZ2x8KwE7fp338e8s/cIVJQ==@kvack.org X-Gm-Message-State: AOJu0Yx0RuhTHqqCGB4Aw0DPTf5KMZi5x+6k7AZKFCwMpqZxq9EC3ptP +Nx2uW8OFRWJss5JpyaRzDvy79/Yy92/uH/zNxiJwS6/WB5WjDjoNuzlUi8EQFeFQ8AR3j6ax5d oag== X-Google-Smtp-Source: AGHT+IEIh41j0AtPPhIvxBJK6ISgVLxnQVreV/6d3lvpOJLUFoaWqoL/DFcaBwvey98OKSYtXZrpcJIDmp8= X-Received: from pjbsi3.prod.google.com ([2002:a17:90b:5283:b0:2ea:6b84:3849]) (user=surenb job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:4c4f:b0:2ee:ee77:227c with SMTP id 98e67ed59e1d1-2f548f102d1mr7321068a91.3.1736389835294; Wed, 08 Jan 2025 18:30:35 -0800 (PST) Date: Wed, 8 Jan 2025 18:30:12 -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-4-surenb@google.com> Subject: [PATCH v8 03/16] mm: mark vma as detached until it's added into vma tree 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-Queue-Id: 8361920007 X-Rspam-User: X-Rspamd-Server: rspam07 X-Stat-Signature: gekjf871okao94om9hffrtf1wuhkjuek X-HE-Tag: 1736389836-358687 X-HE-Meta: U2FsdGVkX19UYUJdR8sPcTAWGaiePzaBh/0E1bsPv5G19HB0A/m8QTFgaf+yfoZDdelMbXkZCTRZZrG8+ju/TXukk4NrAB3TAwat6msNC7mNDWpl40DhpI7vTHbuC+lMgtoSLnTLaFwTbNQoI/gK6IboxuyNpnMCuju28zc4C8rqggu21Wmrd8F6RTV0YlknAReneoG9//bHOnNWcS6JXmUMA29cVBlJCo5LO8gl41uxmgLrdiOfMQ+fc/t/BNbnLYMUtrPRhrcxNkWDlvYMQDdmNPOkaAzRhhYk35huZbD42rvROcB6fz2HaXNQzRnxhj3YRXjMR+gLyKDXbwAPkxXfTbVm321lQUACAZz867fvqWx1iYxFt+1TInsnD9ZEogvfggaBsqZuIPw0Nc87GDsFLkX+o+sAvl9ew+Ao40n/R2286bd+ihrVWeb8FTl1XbUuaHt2BhT17Sg4P7F/zhfB5OIuPFzTX3Fw9wwNAtvSqo3Z4+8KDuKKuA8jYQH8c0ndR+Ywk1QVUC42IKpk8h0V7rQtzfZqyWlFL10DRDXU1AYh4TmCmzqwmO5cLaeQ7tEAUVyBucYgDulxibvdXNg3uKLKGuEI6P5H7RIrb+VMt2ZHlZCnu9jpLEtpGCUmL+y99eYLnujtDO3dnJDUWSGBM41F2Sr7S6OpJj7QrCX7XBF1ysTX+0Oy2Z+p+Gp97NxmEStZKLWiEfA4n91KToYLfwL5Rwdlbc2Z2CwBy4bVuCZetvufFrEgSI5zdqDNrfsHGD0GubbFFMolKqp62UfuV6lGTqQvR1cmUiV+lGpotkmx9etM1Wzra3n3wgQR1tkz1GEXEEqFU+RmS1wftZ3wV1QHZiz9WGNT7R0C6FE/tXXm/KXoTdXU6Mpx761P4Jgeb+4usRIbafUIE8aQR4eSv0lkvNe/Xk6V1nI1I5xgzWQbbeaI+mfVVSpnUTDduMxHbNuaEHie4bQ7ilG l2kyeb0W o1EBOuG4Brz57FSF2eLF6dMOUSFQBotLhwAfsY7AaYp7zyTGqodNUe0OJk/079HgChopB3DDMsSCzga/HLkUvGQo3f0rw/+Bbk6hnYHl/6qSNjgDhYfV+cHWzNarT4WHh34PWpBctrq4C2cYhJsKwGaTPTrTjXGDtfy6JhG6+4xc4QXZlAriL5ko6GxaFSIIqSRUpMIgjTB9+U/fanJ73+zhFblSOsuurCIPlWpnrdtoGgjf2wbR3ziXeTtxK1hKfeepjNkR+n8xo2T61c4yr6nsblI3bWqq/6zx5/C8K3mqwLlHA1r8CD/jlg8rorqllZFI0ZXTFTNeub/XSvwVaUr0p8fUjeFq67nTcIHPrHvo1FbhuFxg1WKOiiaVoAy68GydR9wu/wmcPCEdpjdrArESGWqJ4Vpynb+FO9AjUAaqicCviLlcERYLR0FeCkn0hklNLOv3s0VbTCm5btE0SRWDLia/NeAuaGj3NZ8NGR/KdaA8XZsTvMGTqha85d9QxtxAe+SI9ZVgEmg8YM3Q7FjS1T0Bn2CmDq5eeue8c0pH0gOBUVrtvRjBwlQytyV8wYm+F3lb8o6B5Czu0U4VV9XD74+VhTdywLbfNsyfd78dZUA0Bvca1d14Y8K2qjavE2eNXCBrKNx42Yj8= 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: Current implementation does not set detached flag when a VMA is first allocated. This does not represent the real state of the VMA, which is detached until it is added into mm's VMA tree. Fix this by marking new VMAs as detached and resetting detached flag only after VMA is added into a tree. Introduce vma_mark_attached() to make the API more readable and to simplify possible future cleanup when vma->vm_mm might be used to indicate detached vma and vma_mark_attached() will need an additional mm parameter. Signed-off-by: Suren Baghdasaryan Reviewed-by: Shakeel Butt Reviewed-by: Lorenzo Stoakes Reviewed-by: Vlastimil Babka Reviewed-by: Liam R. Howlett --- include/linux/mm.h | 27 ++++++++++++++++++++------- kernel/fork.c | 4 ++++ mm/memory.c | 2 +- mm/vma.c | 6 +++--- mm/vma.h | 2 ++ tools/testing/vma/vma_internal.h | 17 ++++++++++++----- 6 files changed, 42 insertions(+), 16 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index 920e5ddd77cc..a9d8dd5745f7 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -821,12 +821,21 @@ static inline void vma_assert_locked(struct vm_area_struct *vma) vma_assert_write_locked(vma); } -static inline void vma_mark_detached(struct vm_area_struct *vma, bool detached) +static inline void vma_mark_attached(struct vm_area_struct *vma) +{ + vma->detached = false; +} + +static inline void vma_mark_detached(struct vm_area_struct *vma) { /* When detaching vma should be write-locked */ - if (detached) - vma_assert_write_locked(vma); - vma->detached = detached; + vma_assert_write_locked(vma); + vma->detached = true; +} + +static inline bool is_vma_detached(struct vm_area_struct *vma) +{ + return vma->detached; } static inline void release_fault_lock(struct vm_fault *vmf) @@ -857,8 +866,8 @@ static inline void vma_end_read(struct vm_area_struct *vma) {} static inline void vma_start_write(struct vm_area_struct *vma) {} static inline void vma_assert_write_locked(struct vm_area_struct *vma) { mmap_assert_write_locked(vma->vm_mm); } -static inline void vma_mark_detached(struct vm_area_struct *vma, - bool detached) {} +static inline void vma_mark_attached(struct vm_area_struct *vma) {} +static inline void vma_mark_detached(struct vm_area_struct *vma) {} static inline struct vm_area_struct *lock_vma_under_rcu(struct mm_struct *mm, unsigned long address) @@ -891,7 +900,10 @@ static inline void vma_init(struct vm_area_struct *vma, struct mm_struct *mm) vma->vm_mm = mm; vma->vm_ops = &vma_dummy_vm_ops; INIT_LIST_HEAD(&vma->anon_vma_chain); - vma_mark_detached(vma, false); +#ifdef CONFIG_PER_VMA_LOCK + /* vma is not locked, can't use vma_mark_detached() */ + vma->detached = true; +#endif vma_numab_state_init(vma); vma_lock_init(vma); } @@ -1086,6 +1098,7 @@ static inline int vma_iter_bulk_store(struct vma_iterator *vmi, if (unlikely(mas_is_err(&vmi->mas))) return -ENOMEM; + vma_mark_attached(vma); return 0; } diff --git a/kernel/fork.c b/kernel/fork.c index 40a8e615499f..f2f9e7b427ad 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -465,6 +465,10 @@ struct vm_area_struct *vm_area_dup(struct vm_area_struct *orig) data_race(memcpy(new, orig, sizeof(*new))); vma_lock_init(new); INIT_LIST_HEAD(&new->anon_vma_chain); +#ifdef CONFIG_PER_VMA_LOCK + /* vma is not locked, can't use vma_mark_detached() */ + new->detached = true; +#endif vma_numab_state_init(new); dup_anon_vma_name(orig, new); diff --git a/mm/memory.c b/mm/memory.c index 1342d451b1bd..105b99064ce5 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -6391,7 +6391,7 @@ struct vm_area_struct *lock_vma_under_rcu(struct mm_struct *mm, goto inval; /* Check if the VMA got isolated after we found it */ - if (vma->detached) { + if (is_vma_detached(vma)) { vma_end_read(vma); count_vm_vma_lock_event(VMA_LOCK_MISS); /* The area was replaced with another one */ diff --git a/mm/vma.c b/mm/vma.c index af1d549b179c..d603494e69d7 100644 --- a/mm/vma.c +++ b/mm/vma.c @@ -327,7 +327,7 @@ static void vma_complete(struct vma_prepare *vp, struct vma_iterator *vmi, if (vp->remove) { again: - vma_mark_detached(vp->remove, true); + vma_mark_detached(vp->remove); if (vp->file) { uprobe_munmap(vp->remove, vp->remove->vm_start, vp->remove->vm_end); @@ -1221,7 +1221,7 @@ static void reattach_vmas(struct ma_state *mas_detach) mas_set(mas_detach, 0); mas_for_each(mas_detach, vma, ULONG_MAX) - vma_mark_detached(vma, false); + vma_mark_attached(vma); __mt_destroy(mas_detach->tree); } @@ -1296,7 +1296,7 @@ static int vms_gather_munmap_vmas(struct vma_munmap_struct *vms, if (error) goto munmap_gather_failed; - vma_mark_detached(next, true); + vma_mark_detached(next); nrpages = vma_pages(next); vms->nr_pages += nrpages; diff --git a/mm/vma.h b/mm/vma.h index a2e8710b8c47..2a2668de8d2c 100644 --- a/mm/vma.h +++ b/mm/vma.h @@ -157,6 +157,7 @@ static inline int vma_iter_store_gfp(struct vma_iterator *vmi, if (unlikely(mas_is_err(&vmi->mas))) return -ENOMEM; + vma_mark_attached(vma); return 0; } @@ -389,6 +390,7 @@ static inline void vma_iter_store(struct vma_iterator *vmi, __mas_set_range(&vmi->mas, vma->vm_start, vma->vm_end - 1); mas_store_prealloc(&vmi->mas, vma); + vma_mark_attached(vma); } static inline unsigned long vma_iter_addr(struct vma_iterator *vmi) diff --git a/tools/testing/vma/vma_internal.h b/tools/testing/vma/vma_internal.h index 96aeb28c81f9..47c8b03ffbbd 100644 --- a/tools/testing/vma/vma_internal.h +++ b/tools/testing/vma/vma_internal.h @@ -469,13 +469,17 @@ static inline void vma_lock_init(struct vm_area_struct *vma) vma->vm_lock_seq = UINT_MAX; } +static inline void vma_mark_attached(struct vm_area_struct *vma) +{ + vma->detached = false; +} + static inline void vma_assert_write_locked(struct vm_area_struct *); -static inline void vma_mark_detached(struct vm_area_struct *vma, bool detached) +static inline void vma_mark_detached(struct vm_area_struct *vma) { /* When detaching vma should be write-locked */ - if (detached) - vma_assert_write_locked(vma); - vma->detached = detached; + vma_assert_write_locked(vma); + vma->detached = true; } extern const struct vm_operations_struct vma_dummy_vm_ops; @@ -488,7 +492,8 @@ static inline void vma_init(struct vm_area_struct *vma, struct mm_struct *mm) vma->vm_mm = mm; vma->vm_ops = &vma_dummy_vm_ops; INIT_LIST_HEAD(&vma->anon_vma_chain); - vma_mark_detached(vma, false); + /* vma is not locked, can't use vma_mark_detached() */ + vma->detached = true; vma_lock_init(vma); } @@ -514,6 +519,8 @@ static inline struct vm_area_struct *vm_area_dup(struct vm_area_struct *orig) memcpy(new, orig, sizeof(*new)); vma_lock_init(new); INIT_LIST_HEAD(&new->anon_vma_chain); + /* vma is not locked, can't use vma_mark_detached() */ + new->detached = true; return new; }