From patchwork Tue Nov 12 19:46:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13872894 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 CDE30D32D97 for ; Tue, 12 Nov 2024 19:46:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 538066B00C4; Tue, 12 Nov 2024 14:46:55 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4E5606B00CF; Tue, 12 Nov 2024 14:46:55 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 360286B00D0; Tue, 12 Nov 2024 14:46:55 -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 13E6C6B00C4 for ; Tue, 12 Nov 2024 14:46:55 -0500 (EST) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id BCCA8ABC06 for ; Tue, 12 Nov 2024 19:46:54 +0000 (UTC) X-FDA: 82778475066.18.BF1F1B4 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf25.hostedemail.com (Postfix) with ESMTP id 94FA7A000B for ; Tue, 12 Nov 2024 19:46:22 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=Ou2jJscd; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf25.hostedemail.com: domain of 3q7AzZwYKCBwKMJ6F38GG8D6.4GEDAFMP-EECN24C.GJ8@flex--surenb.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3q7AzZwYKCBwKMJ6F38GG8D6.4GEDAFMP-EECN24C.GJ8@flex--surenb.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731440684; a=rsa-sha256; cv=none; b=llxEujPbpfMWsWepoSFI9b3IkLB0r8EB+wHopVK5wBQP69r+gP39Fo06C9e4xGOwBBD0yJ d61WfjWqwSkpU4CbUvnhROj4TbHIlRZM1QIP2aKRb/1/4K5FunNscC7SUDvCUF0soJcKRS GgYceW163KMmGLmrNA4PLoxU9CmOMPk= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=Ou2jJscd; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf25.hostedemail.com: domain of 3q7AzZwYKCBwKMJ6F38GG8D6.4GEDAFMP-EECN24C.GJ8@flex--surenb.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3q7AzZwYKCBwKMJ6F38GG8D6.4GEDAFMP-EECN24C.GJ8@flex--surenb.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1731440684; 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=Z1CCbcAxwVITlach3ohAL8Fs+Rkv4PQfEREgrvS8KzA=; b=KIwVWzrdsqar45UzRW7CHaHfhAdQb38zgwUPJJMNxbEsrOCBonWXl1FruaZgJH3kULwfeG A8pIUMItzZrgtX+FStqs6JkWK2m97+t99RSW79sn7FfBYwqOTerMn5iWO5MpbxAIBv+g0B P9jlYBYUono27L6y5OMbyw/X5VJCoyk= Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e35e9d36da3so848505276.1 for ; Tue, 12 Nov 2024 11:46:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1731440812; x=1732045612; 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=Z1CCbcAxwVITlach3ohAL8Fs+Rkv4PQfEREgrvS8KzA=; b=Ou2jJscdR3h6yFgjyrZPnjjCco/JBuP0OG0jFKyH2ccvZTFM/F6kYqqaygqtKlW00U PbNK+Mvk0sqP5WYzehSi0xgSS8iVk1McJ34cQY0TTdTRvj0VMqtNjxL1yiXAFtX1K/mn vhtdaftmTyGLhtMv7itgSow3HxZNL13uqWZF6AKoDdI/b6GKGfrf+I4ClzRRLV3f3vKc ZnIIx3nS4xNRF2+suBRhTEF8hMrHY0hZ8eSxyeV21w8+Bdf2zW5RBpdL2/OVu2x9cdKA KsMSycJ1vcYyK8EjiH5i82seYAvG28Wmmb9kb947SBkIik5DKito/Jt6oMcsdKQm0XhS gTlw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731440812; x=1732045612; 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=Z1CCbcAxwVITlach3ohAL8Fs+Rkv4PQfEREgrvS8KzA=; b=D0o7W90jj0Ef1ka8dKHAvzGyqq/QTkzEDbH8q4SNKO4/gES9i9h/f5tQ7o9zbrNhOz Gx6oPXEV5Pc4Gyo3FPFpUlKNEMDkcYWLIT/sHqflH5qy7eLPWwPcBEEIQiuk2JIQMY6A i0gmefKgr6xWVW6eZXqpAOAeyWfjYtL8PCEI3g3URLJjqGXki6ylr9hGcGLoC0aTcT/e tWAtHXoZ4MRIzon3sjo2L/yiqyKc3MHcxZuYOWyRU18JL6uyG3u3FRNCCle7J8vH4l1i HJ27sBkD1pBjr2txpSZ0KkNUCYzDl+nXp7+A7vmIG9f0ijTDcOBwXJk6YsA3J+2tA6h5 SDrg== X-Forwarded-Encrypted: i=1; AJvYcCVq5XaVuuY3YHusZcyX1zOMd9DYsi02tzJZCEV89ilgqNxda0L3s4MYcUzsT5ZA0jMB8ZGRN2uPeA==@kvack.org X-Gm-Message-State: AOJu0Yw3lvce8t46rot3hmMzoeRvX/O4ZjT5Ki1ZgJsfGX8HWzghXXl1 kqFMUh7zZUDOrlFOsIwIi/yAwTYCtVPfTatyScb2gPLzK59bStQ1n5sV6Na9HhYv0Vx+eT6MNwF Ihw== X-Google-Smtp-Source: AGHT+IEq0LcdS3gaxVGqCSmSAjHrghcWgLVyn1pnLLC1ahL+sX7JpJH17PWyfqB3xOS3eIrVCs2OBxnyQWY= X-Received: from surenb-desktop.mtv.corp.google.com ([2a00:79e0:2e3f:8:cad5:5dc6:1298:ec42]) (user=surenb job=sendgmr) by 2002:a25:dc92:0:b0:e28:eaba:356a with SMTP id 3f1490d57ef6-e337f8f04e4mr71200276.9.1731440811893; Tue, 12 Nov 2024 11:46:51 -0800 (PST) Date: Tue, 12 Nov 2024 11:46:33 -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-4-surenb@google.com> Subject: [PATCH v2 3/5] mm: mark vma as detached until it's added into vma tree 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-Rspamd-Queue-Id: 94FA7A000B X-Stat-Signature: bxjt5qdbgz9cq8pgx6ox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WzRWT/60J/1c1226tsuwuAPZafiDJ/gNx7FLWN1iMKY6ZxzHxcAJp3zgBv2L42vwOYV4Rd2mOQ6VEEVKboaQTJUBqnLV5UIriuqvKRp5Gq21LSyVCKOlgziV2tgQ37hCpTJMT40dTUhIxOvKGUUbZY8oP1srNUgaqSni20sRYVfWZwGTFUvTzA3vUPRypv6e4FOPnXlEPzW23K32eK7WvgoUHHJSDIjdhVvLJumlvsnEhNR3k2yKTGMEZHeZglPJjV8JUoxlQXCcoFeaht74TeiBfvGcqaVACuC7v9uNfVFUBzDLP3RoFa9a1D7/PQXawfdc8d4I8mCEg6EaNJMWzPksE8UMAHncIaIzW9d26i3nE3FdisLzc1sW2aSL17KCTpopUBfn8rGwRIZcCobsevgHgShO/NWeNPo8XJWFWwZTektX2vEVLIP6Dr63AFq1fpCWpmXKkvQ1Hy+xZ7K5cgyXMW0pl8vmeIHlelHUPUFZYu+CU9A6z75B1xHWCnddJxcKopdPkYSF4FbBkWk= 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. Signed-off-by: Suren Baghdasaryan Reviewed-by: Lorenzo Stoakes --- include/linux/mm.h | 10 +++++++++- mm/memory.c | 2 +- mm/mmap.c | 2 ++ mm/nommu.c | 2 ++ mm/vma.c | 3 +++ tools/testing/vma/vma_internal.h | 3 ++- 6 files changed, 19 insertions(+), 3 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index a5eb0be3e351..245a85caf4c3 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -812,6 +812,11 @@ static inline void vma_mark_detached(struct vm_area_struct *vma, bool detached) vma->detached = detached; } +static inline bool is_vma_detached(struct vm_area_struct *vma) +{ + return vma->detached; +} + static inline void release_fault_lock(struct vm_fault *vmf) { if (vmf->flags & FAULT_FLAG_VMA_LOCK) @@ -874,7 +879,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); } diff --git a/mm/memory.c b/mm/memory.c index 209885a4134f..d0197a0c0996 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -6279,7 +6279,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/mmap.c b/mm/mmap.c index 386429f7db5a..1295c4cedaf4 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -1570,6 +1570,7 @@ static int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *vma, if (vma_iter_store_gfp(vmi, vma, GFP_KERNEL)) goto mas_store_fail; + vma_mark_detached(vma, false); mm->map_count++; validate_mm(mm); ksm_add_vma(vma); @@ -1890,6 +1891,7 @@ static struct vm_area_struct *__install_special_mapping( if (ret) goto out; + vma_mark_detached(vma, false); vm_stat_account(mm, vma->vm_flags, len >> PAGE_SHIFT); perf_event_mmap(vma); diff --git a/mm/nommu.c b/mm/nommu.c index 9cb6e99215e2..6afd5c2bd97d 100644 --- a/mm/nommu.c +++ b/mm/nommu.c @@ -1192,6 +1192,7 @@ unsigned long do_mmap(struct file *file, current->mm->map_count++; /* add the VMA to the tree */ vma_iter_store(&vmi, vma); + vma_mark_detached(vma, false); /* we flush the region from the icache only when the first executable * mapping of it is made */ @@ -1357,6 +1358,7 @@ static int split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma, setup_vma_to_mm(vma, mm); setup_vma_to_mm(new, mm); vma_iter_store(vmi, new); + vma_mark_detached(new, false); mm->map_count++; return 0; diff --git a/mm/vma.c b/mm/vma.c index 8a454a7bbc80..1426871fa6e0 100644 --- a/mm/vma.c +++ b/mm/vma.c @@ -275,6 +275,7 @@ static void vma_complete(struct vma_prepare *vp, struct vma_iterator *vmi, * (it may either follow vma or precede it). */ vma_iter_store(vmi, vp->insert); + vma_mark_detached(vp->insert, false); mm->map_count++; } @@ -1690,6 +1691,7 @@ int vma_link(struct mm_struct *mm, struct vm_area_struct *vma) vma_start_write(vma); vma_iter_store(&vmi, vma); + vma_mark_detached(vma, false); vma_link_file(vma); mm->map_count++; validate_mm(mm); @@ -2369,6 +2371,7 @@ static int __mmap_new_vma(struct mmap_state *map, struct vm_area_struct **vmap) /* Lock the VMA since it is modified after insertion into VMA tree */ vma_start_write(vma); vma_iter_store(vmi, vma); + vma_mark_detached(vma, false); map->mm->map_count++; vma_link_file(vma); diff --git a/tools/testing/vma/vma_internal.h b/tools/testing/vma/vma_internal.h index 1d9fc97b8e80..fdb60978821f 100644 --- a/tools/testing/vma/vma_internal.h +++ b/tools/testing/vma/vma_internal.h @@ -438,7 +438,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; } static inline struct vm_area_struct *vm_area_alloc(struct mm_struct *mm)