From patchwork Tue Jun 20 23:57:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13286506 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 DED14EB64D7 for ; Tue, 20 Jun 2023 23:57:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 54B718D0005; Tue, 20 Jun 2023 19:57:37 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4AD578D0001; Tue, 20 Jun 2023 19:57:37 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2FFEB8D0005; Tue, 20 Jun 2023 19:57:37 -0400 (EDT) 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 1B8898D0001 for ; Tue, 20 Jun 2023 19:57:37 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id E0EA6A062A for ; Tue, 20 Jun 2023 23:57:36 +0000 (UTC) X-FDA: 80924790912.07.B776BC7 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf18.hostedemail.com (Postfix) with ESMTP id 2F0691C0013 for ; Tue, 20 Jun 2023 23:57:34 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=P9ZGKNT+; spf=pass (imf18.hostedemail.com: domain of 37jySZAYKCNsPROBK8DLLDIB.9LJIFKRU-JJHS79H.LOD@flex--surenb.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=37jySZAYKCNsPROBK8DLLDIB.9LJIFKRU-JJHS79H.LOD@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=1687305455; 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=BGbrY/f1fvBNW9z8go6Nnz06u29111qaAGQndqYmZbI=; b=4f5SmV8wtY+g4vXqtjwG1j4pujeRN5TeNAoQbRk9spvfQOwcIlsdgDR6S0qCCdQ7JE3djk jImb040DXL38amKwLfWSiJvQPu2rQcUSzSfurmlwJf+5TDa67avfAYVC0Wf6EdCRsUjXcy MPhB7R6lkW7aeT0KwmEOmlrbUz19U9U= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1687305455; a=rsa-sha256; cv=none; b=6OBiSjKBvB4DkCYF50bHE0ePNT6LaY2w76D+swJ7MpbOyHt4iNelEe4E9gBGtAbl8UirVc KiiGQzqs5muNGuZ+g6Vp+v6xgxkHAp2vdzJoBNiXN4VXvafTpFCQznVIitV88Ieca6IQJL w7Ao+JzhL+ffmNYWFxmKoZE5QQigj1A= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=P9ZGKNT+; spf=pass (imf18.hostedemail.com: domain of 37jySZAYKCNsPROBK8DLLDIB.9LJIFKRU-JJHS79H.LOD@flex--surenb.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=37jySZAYKCNsPROBK8DLLDIB.9LJIFKRU-JJHS79H.LOD@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-bb2a7308f21so6657159276.2 for ; Tue, 20 Jun 2023 16:57:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1687305454; x=1689897454; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=BGbrY/f1fvBNW9z8go6Nnz06u29111qaAGQndqYmZbI=; b=P9ZGKNT+TEzfnNjgXfWsFMxrjrmo19InxA9Ltzw94eBOzcf0DrJjIadY39g5B1ARES UM8Tb66YAZoM1qvWyjNLmdfb7GBwdGln7qA5RgXbZWXGaZHvI0XHaH/HBL1QpJNOQgUm 6OloW5Gfvh2xzMIEA3ALlJsZDF0ta5gG6htWRcVitzaGC/HTclOzVZESltb69EPM+Vbp ulOrRqMr+3gQkWyLLTEaVyZRb1j645qYTNnqpW50MZl/4SibKrzjz7laJRB/LZnjYsyE XMOaY7sjsOrxZl8Y6VKGCVQZWc8BmHDWraN0TJszAdVqPx1QEoL6EPZTqJhDF1Ftewq7 eOYg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687305454; x=1689897454; 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=BGbrY/f1fvBNW9z8go6Nnz06u29111qaAGQndqYmZbI=; b=Pkc3UUHTVbziwgCCRbTNdrBDIhN4G3g4NZlNGgbiuD6PuB1veFndy/1QYhEe8DHarU +Nwa/Xf47/FDtVA2COq6wvErEmiBWFevbqecCRFw7sLEBkS0Frzw91oo1bibpJfS6jl3 PQ7+R5QQk9kpAmW565FiU4B5rRQLWlzwWurriA+ffheew1MHyLh/g7Kv0wEpNWk1Ik8j kYHJeg7+z1sQa8hcnCGgZpdCKpkfCgHghIqN4YRQH7wDvWsZ/ImUTMUm8c29XpMvVUzn rcu4QmWq46mJFE5vcg02FbJSH3TFZiXOhYU6A26oAbkBV8f8p0pSGweN6MGX1hOz1k3P EeOA== X-Gm-Message-State: AC+VfDxHynlOnydr6UnKOUulbKWJ/H8dcqPfJgcE/xiq2m+5DNThx9d/ sKOW7Op+2TSXDLV4+gFTclv/qpKDvck= X-Google-Smtp-Source: ACHHUZ6kw7oTpcpzUTFKAorHv5DtOn6LBqwlXR1p5RtO+KTrOjHZ+LXWbslSzGMAafSpF8zUkeChCRjPRTw= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:3f57:9854:e19:8906]) (user=surenb job=sendgmr) by 2002:a5b:bc9:0:b0:bad:41b:f343 with SMTP id c9-20020a5b0bc9000000b00bad041bf343mr5127082ybr.1.1687305454340; Tue, 20 Jun 2023 16:57:34 -0700 (PDT) Date: Tue, 20 Jun 2023 16:57:26 -0700 In-Reply-To: <20230620235726.3873043-1-surenb@google.com> Mime-Version: 1.0 References: <20230620235726.3873043-1-surenb@google.com> X-Mailer: git-send-email 2.41.0.162.gfafddb0af9-goog Message-ID: <20230620235726.3873043-3-surenb@google.com> Subject: [PATCH 3/3] mm: check for VMA being detached before destroying it From: Suren Baghdasaryan To: akpm@linux-foundation.org Cc: willy@infradead.org, torvalds@linuxfoundation.org, vegard.nossum@oracle.com, mpe@ellerman.id.au, Liam.Howlett@oracle.com, lrh2000@pku.edu.cn, mgorman@suse.de, linux-mm@kvack.org, linux-kernel@vger.kernel.org, kernel-team@android.com, surenb@google.com X-Stat-Signature: fe8uwfxxhtxosmuqarzp73hinwgp8sio X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 2F0691C0013 X-Rspam-User: X-HE-Tag: 1687305454-215222 X-HE-Meta: U2FsdGVkX1+2p0cHr9fXox52BXOWW8F3CGT5WEiBpKEUrh1xuhGNZnYACnkHjoJbw2ozsT7pV7AqT/t3CS0CnIr3fcKq2asr2ReueTfj2yg3xKNay2HGuKSYgaWtnjOQlM4JLbldiHccF7ZSLbOy7/j5Z7WrFYfxLiIoKFxx54kGY2KGGsHKmatSHUPOXuxZQ5iHjvke1yMVBtPpnumnKXM+eLNJwl10bl0TfmPEXDSj7fhcjAfVy+9rXEv00b95sPs3IiOiLgCwfpgw9sEJ0k9bqUd0L79Jtp4BHpna483zlF5+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kREJ5E1bqVNX33CSlYX0pJ/QJ3FfXXcd//023tY0Dj/UOrx8Ju4Lvf19gqbNtD/HK+9/1J7cUy4JMe8q8XMqvm7sq4TGcmwt2Ma/MYmbQnEFE3mxq43xUh/M+XUiGZduwpXEqa5B8yyxFlFoGNyrtwp3MKvoM5TpCiqEVuBoN6tRTaYKtCbrkaE9/ZBJYqqanXtHWINkPk3Ak+18xmuPlKSYrjhyPcc1NYknHOzHQ/MnKYseUOsEcnnbnMA63q4Hi8O+wWIhe+R5t 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: By the time VMA is freed it has to be detached with the exception of exit_mmap which is destroying the whole VMA tree. Enforce this requirement before freeing the VMA. exit_mmap in the only user calling __vm_area_free directly, therefore it won't trigger the new check. Change VMA initialization to mark new VMAs as detached and change that flag once the VMA is added into a tree. Suggested-by: Linus Torvalds Signed-off-by: Suren Baghdasaryan --- include/linux/mm.h | 4 ++-- kernel/fork.c | 2 ++ mm/internal.h | 1 + 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index 74e3033c9fc2..9a10fcdb134e 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -247,7 +247,7 @@ void setup_initial_init_mm(void *start_code, void *end_code, struct vm_area_struct *vm_area_alloc(struct mm_struct *); struct vm_area_struct *vm_area_dup(struct vm_area_struct *); void vm_area_free(struct vm_area_struct *); -/* Use only if VMA has no other users */ +/* Use only if VMA has no other users and might still be attached to a tree */ void __vm_area_free(struct vm_area_struct *vma); #ifndef CONFIG_MMU @@ -751,7 +751,7 @@ static inline void vma_init(struct vm_area_struct *vma, struct mm_struct *mm) vma->vm_mm = mm; vma->vm_ops = &dummy_vm_ops; INIT_LIST_HEAD(&vma->anon_vma_chain); - vma_mark_detached(vma, false); + vma->detached = true; vma_numab_state_init(vma); } diff --git a/kernel/fork.c b/kernel/fork.c index 41c964104b58..000fc429345c 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -540,6 +540,7 @@ static void vm_area_free_rcu_cb(struct rcu_head *head) /* The vma should not be locked while being destroyed. */ VM_BUG_ON_VMA(rwsem_is_locked(&vma->vm_lock->lock), vma); + WARN_ON_ONCE(!vma->detached); __vm_area_free(vma); } #endif @@ -549,6 +550,7 @@ void vm_area_free(struct vm_area_struct *vma) #ifdef CONFIG_PER_VMA_LOCK call_rcu(&vma->vm_rcu, vm_area_free_rcu_cb); #else + WARN_ON_ONCE(!vma->detached); __vm_area_free(vma); #endif } diff --git a/mm/internal.h b/mm/internal.h index 68410c6d97ac..728189e6c703 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -1068,6 +1068,7 @@ static inline void vma_iter_store(struct vma_iterator *vmi, vmi->mas.index = vma->vm_start; vmi->mas.last = vma->vm_end - 1; mas_store_prealloc(&vmi->mas, vma); + vma_mark_detached(vma, false); } static inline int vma_iter_store_gfp(struct vma_iterator *vmi,