Received: by 2002:a05:6358:489b:b0:bb:da1:e618 with SMTP id x27csp2188084rwn; Fri, 9 Sep 2022 09:42:09 -0700 (PDT) X-Google-Smtp-Source: AA6agR6uY9io2s0cV4LjnZfbMgmT+NK3t1y4Is2U+f4Y1NNLIMsUCDzmAyDxYbUPu7uH1dzTCZPz X-Received: by 2002:a17:907:c08:b0:77a:9b71:222f with SMTP id ga8-20020a1709070c0800b0077a9b71222fmr797764ejc.739.1662741729440; Fri, 09 Sep 2022 09:42:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662741729; cv=none; d=google.com; s=arc-20160816; b=th9ddu9ROgSvfB5GHgwHg4I4OSL6MAWdWltASS5hoAu2lY43wixL+6+Llomkj3L2Bw ajkcBKuDYMaukTIyYEPiJdsik5TmVmfEV9QM5SyfNkHY8+rC1CK3ACoPZHBQTZ6PLxQ1 Y29pmcm4XwOnAKsTwIY1pcxlFDMTHZrzjz2MKcgivoyViZSx21g+6B9sBVPs5jSysw1O dfi6DfY6oYgTwxasazlkMqZ0p9NbiYb3qHgTIVM1+TwLycKQK7owEgMNRnV5KU9PKbAM QxRi9Kcca6HiTg7sK5jPXaoirlpJl5uXJzsnjSJQXgcMJlPvN7dccAY51ClaXK+6Kyys Uo0w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=2KJtyG0OrgeeUZ3tbEzNZEApZJ2MVSJ3tdGvELtllWA=; b=dBI9PaMvBx+ZeRD+SgcQQv1tu/fH+zcDyG5RLFsNoBeR0J0QqjLDw3xwrBb0dIYqql LwdWt+uRTQuTJ2caJL2psPCZzhes1CKZ+ULnvBM/BECbQIltvlqJTDiSUFhueCfkPwYQ pbUXZP2EWAzfHeR+XRBKtZHTcFpzQ+mgRXwHxKH8Zhcgj3HKcj7wHuYb+ujNdr5x5Hei FVB5XPV0DJ0SmuhLLM+oG4mXjHtovh98xkP5rllmn/mnBSn+cAmoL6F2pAG5ORykyGr2 QrRtkLQ6oT0fLOzfV6Dy4f6uZEhpI4cYXui2UPBM63nJ90qlYh7VjD36WLaMoI6PGzGT Dw3w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b="Vtzxz/3W"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w20-20020a056402071400b0043c1908e995si791056edx.113.2022.09.09.09.41.43; Fri, 09 Sep 2022 09:42:09 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b="Vtzxz/3W"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231238AbiIIQTc (ORCPT + 99 others); Fri, 9 Sep 2022 12:19:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60360 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231137AbiIIQT2 (ORCPT ); Fri, 9 Sep 2022 12:19:28 -0400 Received: from mail-yb1-xb34.google.com (mail-yb1-xb34.google.com [IPv6:2607:f8b0:4864:20::b34]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 51120224 for ; Fri, 9 Sep 2022 09:19:23 -0700 (PDT) Received: by mail-yb1-xb34.google.com with SMTP id g5so3382782ybg.11 for ; Fri, 09 Sep 2022 09:19:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date; bh=2KJtyG0OrgeeUZ3tbEzNZEApZJ2MVSJ3tdGvELtllWA=; b=Vtzxz/3WnYzehsvxEe5bYGVLVOYdFWrSTvZz1vALAWuaevC3U5WtB+f78lFCZF324w FiscSmbXyU+XOB2/F7lOYLYlIX8OuFWYPchdz5uYxmpm9O//MmQsACLWQpqvEvhHp78u Fqcicq6ClSVver2X4O9EIw1QxejGbytPeJEB/dU6IAFURZmla+3iGvObrXUwU6kzrvty vvKRbbaLhKfEVSQiX5d/rnqzs2MN5BTC2/L2bdEQkmQOw6LxzvEJ4w2N+ZccRlBLFUqE QBiGhcqj/PQWmuf5DYdZqpxP0dQ13hOo5+doY9eqgJ0sxS0rfuqgmlMiu7e7iSGtWTIp SMdw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date; bh=2KJtyG0OrgeeUZ3tbEzNZEApZJ2MVSJ3tdGvELtllWA=; b=rKrRU3MtWvDpo3wcA6PLB+0UYF5HvioQBMKPL+Ugq3liI6F7rZM6fuFhGhSBNknc1z cU+Qra071DxOwXFBP/YHz1wqBxxLhxLt7J25W1wt5nIaWwEYCttemtQ5fE70+cJattLV 73UMEd2YMAoCqf20rj61uIK7hyRNcNLcv1GMX6zbY31odeiEdh/TmHdNsa7bdvtcysgN n0IeeUTzhice3mGxmOW9ipSJSv1c4scU5x37mZT2XH5O0x/zgODdJu77qtwBpABCcJgp X8ELs8Eaky8GUitatV8TxwOsgVpLMhqA3vWvzNqjUBnh/RZyvnLWx8f27n2cJGvzvtpS oV8g== X-Gm-Message-State: ACgBeo335s1zXSuDcrTMc7GigIWSbRbEZVCUFrOS3BoU0KPli6Z7F4yR IwH7SlHn3RHjJRl0oxVF/e/RiKE9yb4rC6H3AH+QsQ== X-Received: by 2002:a05:6902:2d0:b0:694:d257:4c4b with SMTP id w16-20020a05690202d000b00694d2574c4bmr12464223ybh.316.1662740362348; Fri, 09 Sep 2022 09:19:22 -0700 (PDT) MIME-Version: 1.0 References: <20220901173516.702122-1-surenb@google.com> <20220901173516.702122-17-surenb@google.com> In-Reply-To: From: Suren Baghdasaryan Date: Fri, 9 Sep 2022 09:19:11 -0700 Message-ID: Subject: Re: [RFC PATCH RESEND 16/28] kernel/fork: assert no VMA readers during its destruction To: Laurent Dufour Cc: akpm@linux-foundation.org, michel@lespinasse.org, jglisse@google.com, mhocko@suse.com, vbabka@suse.cz, hannes@cmpxchg.org, mgorman@suse.de, dave@stgolabs.net, willy@infradead.org, liam.howlett@oracle.com, peterz@infradead.org, laurent.dufour@fr.ibm.com, paulmck@kernel.org, luto@kernel.org, songliubraving@fb.com, peterx@redhat.com, david@redhat.com, dhowells@redhat.com, hughd@google.com, bigeasy@linutronix.de, kent.overstreet@linux.dev, rientjes@google.com, axelrasmussen@google.com, joelaf@google.com, minchan@google.com, kernel-team@android.com, linux-mm@kvack.org, linux-arm-kernel@lists.infradead.org, linuxppc-dev@lists.ozlabs.org, x86@kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Sep 9, 2022 at 6:56 AM Laurent Dufour wrote= : > > Le 01/09/2022 =C3=A0 19:35, Suren Baghdasaryan a =C3=A9crit : > > Assert there are no holders of VMA lock for reading when it is about to= be > > destroyed. > > > > Signed-off-by: Suren Baghdasaryan > > --- > > include/linux/mm.h | 8 ++++++++ > > kernel/fork.c | 2 ++ > > 2 files changed, 10 insertions(+) > > > > diff --git a/include/linux/mm.h b/include/linux/mm.h > > index dc72be923e5b..0d9c1563c354 100644 > > --- a/include/linux/mm.h > > +++ b/include/linux/mm.h > > @@ -676,6 +676,13 @@ static inline void vma_assert_write_locked(struct = vm_area_struct *vma, int pos) > > VM_BUG_ON_VMA(vma->vm_lock_seq !=3D READ_ONCE(vma->vm_mm->mm_lock= _seq), vma); > > } > > > > +static inline void vma_assert_no_reader(struct vm_area_struct *vma) > > +{ > > + VM_BUG_ON_VMA(rwsem_is_locked(&vma->lock) && > > + vma->vm_lock_seq !=3D READ_ONCE(vma->vm_mm->mm_lock= _seq), > > + vma); > > +} > > + > > #else /* CONFIG_PER_VMA_LOCK */ > > > > static inline void vma_init_lock(struct vm_area_struct *vma) {} > > @@ -685,6 +692,7 @@ static inline bool vma_read_trylock(struct vm_area_= struct *vma) > > static inline void vma_read_unlock(struct vm_area_struct *vma) {} > > static inline void vma_assert_locked(struct vm_area_struct *vma) {} > > static inline void vma_assert_write_locked(struct vm_area_struct *vma,= int pos) {} > > +static inline void vma_assert_no_reader(struct vm_area_struct *vma) {} > > > > #endif /* CONFIG_PER_VMA_LOCK */ > > > > diff --git a/kernel/fork.c b/kernel/fork.c > > index 1872ad549fed..b443ba3a247a 100644 > > --- a/kernel/fork.c > > +++ b/kernel/fork.c > > @@ -487,6 +487,8 @@ static void __vm_area_free(struct rcu_head *head) > > { > > struct vm_area_struct *vma =3D container_of(head, struct vm_area_= struct, > > vm_rcu); > > + /* The vma should either have no lock holders or be write-locked.= */ > > + vma_assert_no_reader(vma); > > I'm wondering if this can be hit in the case the thread freeing a VMA is > preempted before incrementing the mm ref count, like this: > > VMA is about to be freed > write lock VMA > free vma -> call_rcu > .. > <--- thread preempted > rcu handler runs > rcu calls __vm_area_free() <<<<<< At this point the VMA is still write-locked (mm seq count hasn't been incremented yet), correct? If so then vma_assert_no_reader() will not assert because the second condition of VMA being write-locked is satisfied. Did I miss anything? > unlock mmap_lock and increase the mm seq count > > > > kmem_cache_free(vm_area_cachep, vma); > > } > > #endif >