Received: by 2002:a05:7412:b101:b0:e2:908c:2ebd with SMTP id az1csp3216666rdb; Thu, 16 Nov 2023 03:47:06 -0800 (PST) X-Google-Smtp-Source: AGHT+IHYmwIsNDt2ZD9GIM/TCrBQwVmYtMWD9jq95UBDwwopmj51aC/dZT8T/fjkWvHmezJsRAKd X-Received: by 2002:a17:90b:4f41:b0:27d:63ae:f378 with SMTP id pj1-20020a17090b4f4100b0027d63aef378mr16374369pjb.42.1700135226028; Thu, 16 Nov 2023 03:47:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700135226; cv=none; d=google.com; s=arc-20160816; b=pR95EUq/oYlksiUMcOBgO1Ut8RGvjvCxV26kxZw4keU1JAZ7rv8QvgSl/PprUYQu11 VfiDsvTNFnpQZ7Dt0Dr2wGniFEg2SF76JECgsAX4+9lMLbR/Jnm3eZTZIvYJhtjmHna/ 4FN7M5b38zcdh3jCoJn0Knouv3qzOWlwx703Ebd2Nde3hiAOm1ibsaSzwB6VF/k/2qtI 5s1U2B4qYqLGu1B5JlZxrwrTYuihWAzgvNHeD7g3P1dpCzmfSUOGFQ4e+FXKVZDwZ+5P ztgA7HKZqrNAmiV9KUzHqTHokvRFBNp8By0thr1nQZrwD5tjXJUJJL+C9wT9N6OTThWX MA7A== 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:mime-version:date :dkim-signature:message-id; bh=ALy0ZOKzQLKlX8vKIj4p6bR47g5CJnR2bhwt15Sf1AE=; fh=EUA1TULZRuyn7Gp4Ul9WH7rjigYz9whpA+h2Fm6SoBQ=; b=fD7VhC9m9tOYYMCTWUiId56PukbnF0Gs+WQlsq9r0NUhY+PgBXvxqKDkO4hZLLG0X2 o0/QruyIrSfQfaMRFwGWTNgq5YRiRprDBwnPbvg8N7POe4XwTwvkIDmYhg7FzNFs64yc eCmWSGS+7EtRWEmtN6CIXEhB0WhEhk/qRbNHFcMuxQ8My5FlFvUkIp1kxrJeCTXgQEhE /vPRjdcYd+u1+nU/wkOFMBQmLAmRKIDe+tI3W0u+82M17C/gA+ZXfIB3NfLCMAEDJwLr MMX2wMz9V0x58ZrcKWCS4H/grKKnMG9b+Kwj1AvvK1Ay0amUC/VLRgudmyp4eZ2GXDBY b63A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=kVEPDUOl; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id s23-20020a17090a1c1700b0026810b074afsi1879471pjs.3.2023.11.16.03.47.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 Nov 2023 03:47:06 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=kVEPDUOl; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id 5075D80F696B; Thu, 16 Nov 2023 03:47:02 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345074AbjKPLqi (ORCPT + 99 others); Thu, 16 Nov 2023 06:46:38 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56334 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230160AbjKPLqh (ORCPT ); Thu, 16 Nov 2023 06:46:37 -0500 Received: from out-188.mta0.migadu.com (out-188.mta0.migadu.com [91.218.175.188]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 19D64181 for ; Thu, 16 Nov 2023 03:46:34 -0800 (PST) Message-ID: <28e179e1-c371-4212-9402-9fe3236e7b66@linux.dev> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1700135192; 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=ALy0ZOKzQLKlX8vKIj4p6bR47g5CJnR2bhwt15Sf1AE=; b=kVEPDUOlIKylq+XJWN9RKqCW+BiLljp+SElDWhBvu1scHc7+U8k309JrV1CjHHz/0Lgx7o BpCX/XGWUNPtEKGasEkoEqwEfzm0FFfYJXwp1mLYDPDspH3wQ/bxD2UI7FbaII7zUzcTcm rOpHU77aJXl4t8CJ1hAQ0uiNmCKOb+I= Date: Thu, 16 Nov 2023 06:46:24 -0500 MIME-Version: 1.0 Subject: Re: [PATCH 1/3] bpf: task_group_seq_get_next: use __next_thread() rather than next_thread() Content-Language: en-GB To: Oleg Nesterov Cc: Alexei Starovoitov , Chuyi Zhou , Daniel Borkmann , Kui-Feng Lee , linux-kernel@vger.kernel.org, bpf@vger.kernel.org References: <20231114163234.GA890@redhat.com> <34440ea4-3780-45e4-9e7c-1b36b535171b@linux.dev> <20231116093428.GA18748@redhat.com> X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Yonghong Song In-Reply-To: <20231116093428.GA18748@redhat.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Migadu-Flow: FLOW_OUT X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Thu, 16 Nov 2023 03:47:02 -0800 (PST) On 11/16/23 4:34 AM, Oleg Nesterov wrote: > On 11/15, Yonghong Song wrote: >> On 11/14/23 11:32 AM, Oleg Nesterov wrote: >>> @@ -70,15 +70,13 @@ static struct task_struct *task_group_seq_get_next(struct bpf_iter_seq_task_comm >>> return NULL; >>> retry: >>> - task = next_thread(task); >>> + task = __next_thread(task); >>> + if (!task) >>> + return NULL; >>> next_tid = __task_pid_nr_ns(task, PIDTYPE_PID, common->ns); >>> - if (!next_tid || next_tid == common->pid) { >>> - /* Run out of tasks of a process. The tasks of a >>> - * thread_group are linked as circular linked list. >>> - */ >>> - return NULL; >>> - } >>> + if (!next_tid) >>> + goto retry; >> Look at the code. Looks like next_tid should never be 0 > ... > >> pid_t __task_pid_nr_ns(struct task_struct *task, enum pid_type type, >> struct pid_namespace *ns) >> { >> pid_t nr = 0; >> >> rcu_read_lock(); >> if (!ns) >> ns = task_active_pid_ns(current); >> nr = pid_nr_ns(rcu_dereference(*task_pid_ptr(task, type)), ns); > ^^^^^^^^^^^^^^^^^^^^^^^^^ > > Please note that task_pid_ptr(task, type)) can return NULL if this > task has already exited and called detach_pid(). > > detach_pid() does __change_pid(task, type, NULL), please note the > > *pid_ptr = new; // NULL in this case > > assignment in __change_pid(). > > IOW. The problem is not that ns can change, the problem is that > task->thread_pid (and other pid links) can be NULL, and in this > case pid_nr_ns() returns zero. Thanks for explanation. I certainly missed race between task iterator and __change_pid(). Then the patch looks good to me. Acked-by: Yonghong Song > > > This code should be rewritten from the very beginning, it should > not rely on pid_nr. If nothing else common->pid and/or pid_visiting > can be reused. But currently my only concern is next_thread(). > >> Other than above, the change looks good to me. > Thanks for review! > > Oleg. >