Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp5726733rwd; Mon, 5 Jun 2023 07:48:38 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7MIpOM3XJkOYmn6kOUkVWr5Ul5KYSy2mpdZwC8QsPaiLqkvkr5Ha5ls179oHY41o2rQahj X-Received: by 2002:a05:6a00:b41:b0:65e:d5da:b3c6 with SMTP id p1-20020a056a000b4100b0065ed5dab3c6mr655861pfo.7.1685976517724; Mon, 05 Jun 2023 07:48:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685976517; cv=none; d=google.com; s=arc-20160816; b=jOHTQ9bCa4jBYg4okVLytZeShfihkdS5N0WZWPjr/hPUhNyUComWiMVjEYqZfgkPqA KtSOFet5qb7KrOhLghTX92D4SVX4oCCMcp8jITaH2uJZkk6aIlgwcSp5h9LaT1/5hp21 yyTP+8/yEOLPAQ3g4BwFa6dbnzqYzFc64JcOlMLt9CiPR+CdzPV3KSFZWqGH3tXYU/zl YWLKGV8bvN7Fp+0DMktkVIEVzh+z/8PwPmmR+5Orfs1zoNHhch2uJXSFLvqbmAiSI1UD kbIXGLZZ/QklgvOlFnrLlD8KX4pbAmFu5P0U9bQP/VjIF10I19CySsmvIeLOQs8N71Na 6TnA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature; bh=DACW0kCzVuT5IFaBn71kjqbpg34q1clPenVlI/hF2Iw=; b=QTZvT3zkVdNr4xaVw8zEiUNlHJnSVcBI+8QoIWpfTb/fStn7K3ZNXfUZM8Fua2Ax/Z tMW5J890rT7qKWFkQVYjh4EQeGrGaa20TlM+Deu8pB4brMIWNGfFO76fYJL1qkp7O4eX HJD5TANORBJRmG9yrd6H5bmiy6en7rhe3lFeCKl9xCOZK5aE+PJkTEIJCwfC0QK3/yNl Fp4dxGgPCWjO0CZut46ppvpB+L658xQv6aHeU3+XIYqzzy+AXwWK5nzGVjdRDEfGel7E wQM4ccEIvmiKBdWnKOSWoJfeRn03lE/nMxVjs2b8aXkObqx+k7Yy/PbPWpal+jdRGGwu U06w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=f2sAlmbi; 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=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id d18-20020aa797b2000000b00654d446d357si3983175pfq.177.2023.06.05.07.48.22; Mon, 05 Jun 2023 07:48:37 -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=@redhat.com header.s=mimecast20190719 header.b=f2sAlmbi; 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=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231513AbjFEORP (ORCPT + 99 others); Mon, 5 Jun 2023 10:17:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44712 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233020AbjFEORF (ORCPT ); Mon, 5 Jun 2023 10:17:05 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5FD9910E3 for ; Mon, 5 Jun 2023 07:15:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1685974556; h=from:from: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:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=DACW0kCzVuT5IFaBn71kjqbpg34q1clPenVlI/hF2Iw=; b=f2sAlmbi3BUTXwH1e3mczMPQEDehfEhWqavh/t9C5Zml+E7qfflbEUCj3CtJZWshnQRsDv yK0RtHRS3MMpAJbGNPSrApmOaz1yDDj81rQnk3WRmzPKGbftJQZ2+NEA6UFqu1iGH1uZWM OUGSl6YbxHdwiHQqGJJSR2yvdZ49eAE= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-392-ogMX25KYN5epLTHPlLAQ2Q-1; Mon, 05 Jun 2023 10:15:18 -0400 X-MC-Unique: ogMX25KYN5epLTHPlLAQ2Q-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id BFE3F2A2AD53; Mon, 5 Jun 2023 14:12:26 +0000 (UTC) Received: from [10.22.10.186] (unknown [10.22.10.186]) by smtp.corp.redhat.com (Postfix) with ESMTP id 0B36E49BB98; Mon, 5 Jun 2023 14:12:26 +0000 (UTC) Message-ID: Date: Mon, 5 Jun 2023 10:12:25 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.7.1 Subject: Re: [PATCH] cgroup: fixed the cset refcnt leak when fork() failed Content-Language: en-US To: Zou Cao , linux-kernel@vger.kernel.org, tj@kernel.org Cc: cgroups@vger.kernel.org, lizefan.x@bytedance.com, hannes@cmpxchg.org, brauner@kernel.org, Zou Cao References: <20230605130444.1421-1-zoucao@kuaishou.com> From: Waiman Long In-Reply-To: <20230605130444.1421-1-zoucao@kuaishou.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.1 on 10.11.54.10 X-Spam-Status: No, score=-2.2 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable 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 6/5/23 09:04, Zou Cao wrote: > TeamID: B1486294 What is this? > > when fork, cset will be increased by commit "ef2c41cf38a7", the refcnt will > be decrease by child exit, but when failed in fork(), this refcnt will > be lost decrease in cgroup_cancel_fork as follow: > > copy_process > | > cgroup_can_fork // increase the css refcount > ...... > spin_lock_irq(&css_set_lock); > cset = task_css_setcurrent); > get_css_set(cset); > spin_unlock_irq&css_set_lock); > ...... The code quoted above is actually in cgroup_css_set_fork(). You may want to list it as well. I believe the problem is in         if (!(kargs->flags & CLONE_INTO_CGROUP)) {                 kargs->cset = cset;                 return 0;         } When CLONE_INTO_CGROUP isn't set, a reference to the current cset is taken. > | > goto cgroup_cancel_fork // if failed in copy_process > | > cgroup_cancel_fork // lost the decrease refcount if flag not CLONE_INTO_CGROUP > > Fixes: ef2c41cf38a7 ("clone3: allow spawning processes into cgroups") > Signed-off-by: Zou Cao > --- > kernel/cgroup/cgroup.c | 5 +++++ > 1 file changed, 5 insertions(+) > > diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c > index d18c2ef..5ecd706 100644 > --- a/kernel/cgroup/cgroup.c > +++ b/kernel/cgroup/cgroup.c > @@ -6284,6 +6284,11 @@ void cgroup_cancel_fork(struct task_struct *child, > if (ss->cancel_fork) > ss->cancel_fork(child, kargs->cset); > > + if (!(kargs->flags & CLONE_INTO_CGROUP) && > + kargs->cset) { > + put_css_set(kargs->cset); > + } > + I believe the out_revert error path of cgroup_can_fork() has a similar issue. Perhaps you may want to put the put_css_set() call in cgroup_css_set_put_fork(). > cgroup_css_set_put_fork(kargs); > } > Cheers, Longman