Received: by 2002:a05:6358:a55:b0:ec:fcf4:3ecf with SMTP id 21csp2422256rwb; Sun, 15 Jan 2023 15:19:17 -0800 (PST) X-Google-Smtp-Source: AMrXdXvlipPZjuhb66DD/UZBDLCZ04bbqPtln20Fd3FuOqazrPVXnspuXyr/fcQswx0xeySxXVy3 X-Received: by 2002:a17:902:bd8d:b0:192:8cd1:5e79 with SMTP id q13-20020a170902bd8d00b001928cd15e79mr19905642pls.41.1673824757557; Sun, 15 Jan 2023 15:19:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673824757; cv=none; d=google.com; s=arc-20160816; b=DN1+EusOR1eIQYqs/iHx3XS2Rz/yFl+mmHoeAoqT0bwS5IWcc5b5vSfhU1mw5TDHEB n2ntAidfQ5Db0wyXvgxYxiu3Wjys06pMlAbOuIehYi4gTIJravmYISAEFOzPCe/DzeOA rDxajBt2gIkQgEsvrfKnJsMQOxeLy6f9eDaCyTtW05FzNuXgtc3whGHeL3I/zNfOkfee 3o/D29hYtMe+XrtbqbTCbcHGc9Ky/Z9ZlWkYIfQM7ck+9Cm331RasTm3Qq/vWIKWxJVy cg5YTgl1Cv7R9SbwV6AdZMe7hvbK6hiRiaxka84kG4PA+JzYiFLfrlTWBnASZXBoj72o Ph9w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=86dZ2QP+sHr0Jg65uUt9BrhUPIsdfEcbXYkErF8fpe8=; b=sqiB5hXOGz8Z8H5CFEYdsqp5AuYIQsQBh3acvA1sdQri4ckM3jnkkTMx7PynTmQ8NX 4B5+Lp0XbnVcnfD48rKRr/OBn13KuF4h34UvatFF2wkCo9dciAiMReQ3ONEPQe3H54zL 1Txxyhtj1yF0IlsTM5o7esmozTLPTIYPcJ/KcrmBlHyIKuoeFK5TQmN1VfbcBbZcNO9m kn/HXwY++InLablaTpMuTWpXunPCIrEeFH8sW+3pgwyt6Uf0J5aZpjL+adHbEQxl40OI 1fsGR6oN+LAyY8LcoMUqU9YrA7gRSBSjdXboXoDTYPMj0aTxRhK3DRnbk92jus8WZCI6 P+qA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b="Im/CuRNv"; 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 q9-20020a170902bd8900b00189efa12957si23795102pls.126.2023.01.15.15.19.06; Sun, 15 Jan 2023 15:19:17 -0800 (PST) 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="Im/CuRNv"; 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 S230354AbjAOXOZ (ORCPT + 52 others); Sun, 15 Jan 2023 18:14:25 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57918 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231356AbjAOXOX (ORCPT ); Sun, 15 Jan 2023 18:14:23 -0500 Received: from mail-vs1-xe2c.google.com (mail-vs1-xe2c.google.com [IPv6:2607:f8b0:4864:20::e2c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0DAD31E9DE for ; Sun, 15 Jan 2023 15:14:22 -0800 (PST) Received: by mail-vs1-xe2c.google.com with SMTP id d66so15054582vsd.9 for ; Sun, 15 Jan 2023 15:14:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=86dZ2QP+sHr0Jg65uUt9BrhUPIsdfEcbXYkErF8fpe8=; b=Im/CuRNvt2HbuN0YhQaoN3HBTTH8A9o3TzZnpRoJtWLjbUXAdbWA3NAongNzK79PcQ Qxnh3WpYwmU5TtOSHWPz/Oeme1I2UYZgMUIiWfQkgygOkRsnmwzw7X81+CaVEdCTjCt3 WONAceBrl3RKn2IjP5jU+1x7657QgRrlekQb+0y7IxVMRXCxva1JSK82jze7W4sTKL/h UlsoAl14g/RD8sFGVXcpDkcdeWj3irHzPoJrLjwoNanov4ou6A7LGeqsnzCKp8Zm24qv 6wHprB12URJWT+V9mQD33Z3WB/UUrYf93Ij0FH/KuPf66U1yFrPoLu8qA5YUuhcc+yaC 7F0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=86dZ2QP+sHr0Jg65uUt9BrhUPIsdfEcbXYkErF8fpe8=; b=IzzOCjbT0W4ac1E6sCJLq0N/s1aD+2f/mZtWRMRI5IYhMKUnY/cLulSH/vuMpGMvOO 2yyVw68CDxUerz/dI9v1W5P81HkTA58Ul/37j5WHrfFxKshxU82Qs5+TPz/O+/dzYUCI H9nW5qArYKR6W7xE0nE542C1AgAOiwsY6V8yLntV7+DBkZWRiwwXBdTpjQ7e51zWDj70 8mWXpoXKmHKy758rlbkuhcdn9ld+LSdk9qglPkypIwDNZK+UbTMWT1UxnVT1g7FJ/MQf 98v73mNAZ8ETXbZBhII+1CD+77wGvzzVOcgF/BgUg2tYr7ozS4Wac1nLj4Ch372BXt9z 38yw== X-Gm-Message-State: AFqh2kpS1ooieIl9lH6P+fXJJmsWgtTOzQQB7ktogr2P1ubw/JQ47xSh d3bF3K/dG3W7/vOEjTV+lFPp+fS5sTPXBorCH134qQ== X-Received: by 2002:a67:ecd4:0:b0:3ce:8835:de03 with SMTP id i20-20020a67ecd4000000b003ce8835de03mr7285245vsp.50.1673824460397; Sun, 15 Jan 2023 15:14:20 -0800 (PST) MIME-Version: 1.0 References: <20230115133330.28420-1-msizanoen@qtmlabs.xyz> <20230115134651.30028-1-msizanoen@qtmlabs.xyz> In-Reply-To: <20230115134651.30028-1-msizanoen@qtmlabs.xyz> From: Yu Zhao Date: Sun, 15 Jan 2023 16:13:44 -0700 Message-ID: Subject: Re: [PATCH v2] mm: do not try to migrate lru_gen if it's not associated with a memcg To: msizanoen1 Cc: Andrew Morton , stable@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" 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, 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 Sun, Jan 15, 2023 at 6:47 AM msizanoen1 wrote: > > In some cases, memory cgroup migration can be initiated by userspace > right after a process was created and right before `lru_gen_add_mm()` is > called (e.g. by some program watching a cgroup and moving away any > processes it detects[1]), which results in the following sequence of > WARNs followed by an Oops as the kernel attempts to perform a > `lru_gen_add_mm()` twice on the same `mm`: ... > Fix this by simply leaving the lru_gen alone if it has not been > associated with a memcg yet, as it should eventually be assigned to the > right cgroup anyway. > > [1]: https://gitlab.freedesktop.org/benzea/uresourced/-/blob/master/cgroupify/cgroupify.c > > v2: > Added stable cc tags > > Signed-off-by: N/A (patch should not be copyrightable) > Cc: stable@vger.kernel.org Thanks for the fix. Cc'ing stable is the right thing to do. The commit message and the comment styles could be easily adjusted to align with the guidelines. I don't think the N/A is acceptible though. I fully respect it if you wish to remain anonymous -- I can send a similar fix crediting you as the "anonymous user " who reported this bug. A bit of background on how I broke it: an old version I have on 4.15 calls lru_gen_add_mm() before cgroup_post_fork(), which excludes cgroup migrations by cgroup_threadgroup_rwsem. When I rebased it, I made lru_gen_add_mm() depend on task_lock for the synchronization with cgroup migrations -- the decoupling seemed (still seems) to make it less complicated -- but this is not safe unless we have the check below. > --- > mm/vmscan.c | 8 +++++++- > 1 file changed, 7 insertions(+), 1 deletion(-) > > diff --git a/mm/vmscan.c b/mm/vmscan.c > index bd6637fcd8f9..0cac40e7484c 100644 > --- a/mm/vmscan.c > +++ b/mm/vmscan.c > @@ -3323,13 +3323,19 @@ void lru_gen_migrate_mm(struct mm_struct *mm) > if (mem_cgroup_disabled()) > return; > > + /* This could happen if cgroup migration is invoked before the process > + * lru_gen is associated with a memcg (e.g. during process creation). > + * Simply ignore it in this case as the lru_gen will get assigned the > + * right cgroup later. */ > + if (!mm->lru_gen.memcg) > + return; > + > rcu_read_lock(); > memcg = mem_cgroup_from_task(task); > rcu_read_unlock(); > if (memcg == mm->lru_gen.memcg) > return; > > - VM_WARN_ON_ONCE(!mm->lru_gen.memcg); > VM_WARN_ON_ONCE(list_empty(&mm->lru_gen.list)); > > lru_gen_del_mm(mm);