From nobody Thu Dec 18 06:14:35 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C4ED1C71134 for ; Fri, 25 Aug 2023 16:22:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343962AbjHYQVw (ORCPT ); Fri, 25 Aug 2023 12:21:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56636 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343951AbjHYQVX (ORCPT ); Fri, 25 Aug 2023 12:21:23 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 54A3719A0 for ; Fri, 25 Aug 2023 09:20:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1692980435; 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: in-reply-to:in-reply-to; bh=9YOqEsz0E9i9rWLFPxwqu5HEnoteHVcXSEYvs1ICBUo=; b=eYFGOgLcjmO5F/h71dm1jVdu2owLsYhltDlSrFSH43HjeVtVwpZk+HqIxM8s1MJP3v7rm2 uxGZ8Iyub8eInDHuYBMoNDbnenk6Gu2ToWp+wDEl+gqvrVdZm4QSi09x2P1c/Yd3Ga9Thl 18esP0FIp06jm9YlG0QT7x7/18U0Krk= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-553-AHXAlR9qNJG_uPLbCjlKdQ-1; Fri, 25 Aug 2023 12:20:30 -0400 X-MC-Unique: AHXAlR9qNJG_uPLbCjlKdQ-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 842BA185A78B; Fri, 25 Aug 2023 16:20:29 +0000 (UTC) Received: from dhcp-27-174.brq.redhat.com (unknown [10.45.225.136]) by smtp.corp.redhat.com (Postfix) with SMTP id 10352492C14; Fri, 25 Aug 2023 16:20:26 +0000 (UTC) Received: by dhcp-27-174.brq.redhat.com (nbSMTP-1.00) for uid 1000 oleg@redhat.com; Fri, 25 Aug 2023 18:19:43 +0200 (CEST) Date: Fri, 25 Aug 2023 18:19:39 +0200 From: Oleg Nesterov To: Andrew Morton , Yonghong Song Cc: "Eric W. Biederman" , Linus Torvalds , Daniel Borkmann , Kui-Feng Lee , Andrii Nakryiko , Martin KaFai Lau , bpf@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 1/6] bpf: task_group_seq_get_next: cleanup the usage of next_thread() Message-ID: <20230825161939.GA16859@redhat.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20230825161842.GA16750@redhat.com> User-Agent: Mutt/1.5.24 (2015-08-30) X-Scanned-By: MIMEDefang 3.1 on 10.11.54.10 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 1. find_pid_ns() + get_pid_task() under rcu_read_lock() guarantees that we can safely iterate the task->thread_group list. Even if this task exits right after get_pid_task() (or goto retry) and pid_alive() returns 0. Kill the unnecessary pid_alive() check. 2. next_thread() simply can't return NULL, kill the bogus "if (!next_task)" check. Signed-off-by: Oleg Nesterov Acked-by: Yonghong Song --- kernel/bpf/task_iter.c | 7 ------- 1 file changed, 7 deletions(-) diff --git a/kernel/bpf/task_iter.c b/kernel/bpf/task_iter.c index c4ab9d6cdbe9..4d1125108014 100644 --- a/kernel/bpf/task_iter.c +++ b/kernel/bpf/task_iter.c @@ -75,15 +75,8 @@ static struct task_struct *task_group_seq_get_next(struc= t bpf_iter_seq_task_comm return NULL; =20 retry: - if (!pid_alive(task)) { - put_task_struct(task); - return NULL; - } - next_task =3D next_thread(task); put_task_struct(task); - if (!next_task) - return NULL; =20 saved_tid =3D *tid; *tid =3D __task_pid_nr_ns(next_task, PIDTYPE_PID, common->ns); --=20 2.25.1.362.g51ebf55 From nobody Thu Dec 18 06:14:35 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6C594C3DA6F for ; Fri, 25 Aug 2023 16:22:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343957AbjHYQVv (ORCPT ); Fri, 25 Aug 2023 12:21:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56648 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343952AbjHYQVX (ORCPT ); Fri, 25 Aug 2023 12:21:23 -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 A969C1BD2 for ; Fri, 25 Aug 2023 09:20:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1692980438; 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: in-reply-to:in-reply-to; bh=0ZAYlH739l+imp77wdMvEIGsmJrTe5vS2BECbqXhogw=; b=EO11U5sm0sQvlZfC9wrx3GYWypBhj4F7xljWJrDRDRaBUvwUNEYzxrWHPIPNqhojh6q35/ 6G3WRaU2MFTn/Y/pJDDbhL4tW+U20gjPF/DpInShKil/xzUaeIvITv3wpK7HSowVR9qsAa 8aHuMQ4uiQGLrgSfBtl2A9GjxsPQJFE= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-134-qNskW97aOzyi2U7dJEyALg-1; Fri, 25 Aug 2023 12:20:34 -0400 X-MC-Unique: qNskW97aOzyi2U7dJEyALg-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 1F31485D060; Fri, 25 Aug 2023 16:20:33 +0000 (UTC) Received: from dhcp-27-174.brq.redhat.com (unknown [10.45.225.136]) by smtp.corp.redhat.com (Postfix) with SMTP id 9ECA52026D68; Fri, 25 Aug 2023 16:20:30 +0000 (UTC) Received: by dhcp-27-174.brq.redhat.com (nbSMTP-1.00) for uid 1000 oleg@redhat.com; Fri, 25 Aug 2023 18:19:46 +0200 (CEST) Date: Fri, 25 Aug 2023 18:19:43 +0200 From: Oleg Nesterov To: Andrew Morton , Yonghong Song Cc: "Eric W. Biederman" , Linus Torvalds , Daniel Borkmann , Kui-Feng Lee , Andrii Nakryiko , Martin KaFai Lau , bpf@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 2/6] bpf: task_group_seq_get_next: cleanup the usage of get/put_task_struct Message-ID: <20230825161943.GA16865@redhat.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20230825161842.GA16750@redhat.com> User-Agent: Mutt/1.5.24 (2015-08-30) X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" get_pid_task() makes no sense, the code does put_task_struct() soon after. Use find_task_by_pid_ns() instead of find_pid_ns + get_pid_task and kill put_task_struct(), this allows to do get_task_struct() only once before return. While at it, kill the unnecessary "if (!pid)" check in the "if (!*tid)" block, this matches the next usage of find_pid_ns() + get_pid_task() in this function. Signed-off-by: Oleg Nesterov Acked-by: Yonghong Song --- kernel/bpf/task_iter.c | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/kernel/bpf/task_iter.c b/kernel/bpf/task_iter.c index 4d1125108014..1589ec3faded 100644 --- a/kernel/bpf/task_iter.c +++ b/kernel/bpf/task_iter.c @@ -42,9 +42,6 @@ static struct task_struct *task_group_seq_get_next(struct= bpf_iter_seq_task_comm if (!*tid) { /* The first time, the iterator calls this function. */ pid =3D find_pid_ns(common->pid, common->ns); - if (!pid) - return NULL; - task =3D get_pid_task(pid, PIDTYPE_TGID); if (!task) return NULL; @@ -66,17 +63,12 @@ static struct task_struct *task_group_seq_get_next(stru= ct bpf_iter_seq_task_comm return task; } =20 - pid =3D find_pid_ns(common->pid_visiting, common->ns); - if (!pid) - return NULL; - - task =3D get_pid_task(pid, PIDTYPE_PID); + task =3D find_task_by_pid_ns(common->pid_visiting, common->ns); if (!task) return NULL; =20 retry: next_task =3D next_thread(task); - put_task_struct(task); =20 saved_tid =3D *tid; *tid =3D __task_pid_nr_ns(next_task, PIDTYPE_PID, common->ns); @@ -88,7 +80,6 @@ static struct task_struct *task_group_seq_get_next(struct= bpf_iter_seq_task_comm return NULL; } =20 - get_task_struct(next_task); common->pid_visiting =3D *tid; =20 if (skip_if_dup_files && task->files =3D=3D task->group_leader->files) { @@ -96,6 +87,7 @@ static struct task_struct *task_group_seq_get_next(struct= bpf_iter_seq_task_comm goto retry; } =20 + get_task_struct(next_task); return next_task; } =20 --=20 2.25.1.362.g51ebf55 From nobody Thu Dec 18 06:14:35 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id AF869C001DB for ; Fri, 25 Aug 2023 16:22:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343969AbjHYQVx (ORCPT ); Fri, 25 Aug 2023 12:21:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51606 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343988AbjHYQVa (ORCPT ); Fri, 25 Aug 2023 12:21:30 -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 3E1621FDB for ; Fri, 25 Aug 2023 09:20:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1692980443; 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: in-reply-to:in-reply-to; bh=y+lRSs+hmNEjHVxY8rBJ0LoRMNpoMiYd+1+hxT28KB4=; b=WGAPh8Ooo7emIE04AISXX+QBfOkFdYkxwBriOs/jfZDJ5LaVGSkuC7egHqNYM5LaDy9bVd pXj6XRwFWRjSqiRX4dNsJ8tXEK+hwpiWHFEfpab9giwj6BORRXoiLOz++ln7D2wbvwMV75 XanJstgqYUhxBOpqd2bBPycG3YtJBGY= Received: from mimecast-mx02.redhat.com (66.187.233.73 [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-528-4q0Mfw-NM6K2XxspvmH6lw-1; Fri, 25 Aug 2023 12:20:38 -0400 X-MC-Unique: 4q0Mfw-NM6K2XxspvmH6lw-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 7FDD11C02CB7; Fri, 25 Aug 2023 16:20:37 +0000 (UTC) Received: from dhcp-27-174.brq.redhat.com (unknown [10.45.225.136]) by smtp.corp.redhat.com (Postfix) with SMTP id 25ACF6B2B6; Fri, 25 Aug 2023 16:20:34 +0000 (UTC) Received: by dhcp-27-174.brq.redhat.com (nbSMTP-1.00) for uid 1000 oleg@redhat.com; Fri, 25 Aug 2023 18:19:51 +0200 (CEST) Date: Fri, 25 Aug 2023 18:19:47 +0200 From: Oleg Nesterov To: Andrew Morton , Yonghong Song Cc: "Eric W. Biederman" , Linus Torvalds , Daniel Borkmann , Kui-Feng Lee , Andrii Nakryiko , Martin KaFai Lau , bpf@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 3/6] bpf: task_group_seq_get_next: fix the skip_if_dup_files check Message-ID: <20230825161947.GA16871@redhat.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20230825161842.GA16750@redhat.com> User-Agent: Mutt/1.5.24 (2015-08-30) X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Unless I am notally confused it is wrong. We are going to return or skip next_task so we need to check next_task-files, not task->files. Signed-off-by: Oleg Nesterov Acked-by: Yonghong Song --- kernel/bpf/task_iter.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel/bpf/task_iter.c b/kernel/bpf/task_iter.c index 1589ec3faded..2264870ae3fc 100644 --- a/kernel/bpf/task_iter.c +++ b/kernel/bpf/task_iter.c @@ -82,7 +82,7 @@ static struct task_struct *task_group_seq_get_next(struct= bpf_iter_seq_task_comm =20 common->pid_visiting =3D *tid; =20 - if (skip_if_dup_files && task->files =3D=3D task->group_leader->files) { + if (skip_if_dup_files && next_task->files =3D=3D next_task->group_leader-= >files) { task =3D next_task; goto retry; } --=20 2.25.1.362.g51ebf55 From nobody Thu Dec 18 06:14:35 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id DC793C71133 for ; Fri, 25 Aug 2023 16:22:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343980AbjHYQVy (ORCPT ); Fri, 25 Aug 2023 12:21:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51608 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344004AbjHYQVe (ORCPT ); Fri, 25 Aug 2023 12:21:34 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A80111991 for ; Fri, 25 Aug 2023 09:20:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1692980444; 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: in-reply-to:in-reply-to; bh=k+LON5n1Edxrfcmnk444h4+WmPGNpJEbekR11bu5AwM=; b=QdCXm1cjge0862CxOmRBFiMQzNBlj2ecS68mxPPAox7/MRyMhQZOc6bu8w2lU1YPiPGl1P 2AQwk83+c5EiBHimkkk11Z19WwZcw3mHNPBiYUfex8/7KU57Db3E+AF9WUgfBaesD4XWVg 2sk7iMuNlskhWtnABA4yEYKOCsPP/eU= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-183-G5NOwX-QNNOTttWRdYHZcA-1; Fri, 25 Aug 2023 12:20:41 -0400 X-MC-Unique: G5NOwX-QNNOTttWRdYHZcA-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 92345802E5A; Fri, 25 Aug 2023 16:20:40 +0000 (UTC) Received: from dhcp-27-174.brq.redhat.com (unknown [10.45.225.136]) by smtp.corp.redhat.com (Postfix) with SMTP id 85C8740C2079; Fri, 25 Aug 2023 16:20:38 +0000 (UTC) Received: by dhcp-27-174.brq.redhat.com (nbSMTP-1.00) for uid 1000 oleg@redhat.com; Fri, 25 Aug 2023 18:19:54 +0200 (CEST) Date: Fri, 25 Aug 2023 18:19:51 +0200 From: Oleg Nesterov To: Andrew Morton , Yonghong Song Cc: "Eric W. Biederman" , Linus Torvalds , Daniel Borkmann , Kui-Feng Lee , Andrii Nakryiko , Martin KaFai Lau , bpf@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 4/6] bpf: task_group_seq_get_next: kill next_task Message-ID: <20230825161951.GA16878@redhat.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20230825161842.GA16750@redhat.com> User-Agent: Mutt/1.5.24 (2015-08-30) X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" It only adds the unnecessary confusion and compicates the "retry" code. Signed-off-by: Oleg Nesterov Acked-by: Yonghong Song --- kernel/bpf/task_iter.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/kernel/bpf/task_iter.c b/kernel/bpf/task_iter.c index 2264870ae3fc..f51f476ec679 100644 --- a/kernel/bpf/task_iter.c +++ b/kernel/bpf/task_iter.c @@ -35,7 +35,7 @@ static struct task_struct *task_group_seq_get_next(struct= bpf_iter_seq_task_comm u32 *tid, bool skip_if_dup_files) { - struct task_struct *task, *next_task; + struct task_struct *task; struct pid *pid; u32 saved_tid; =20 @@ -68,10 +68,10 @@ static struct task_struct *task_group_seq_get_next(stru= ct bpf_iter_seq_task_comm return NULL; =20 retry: - next_task =3D next_thread(task); + task =3D next_thread(task); =20 saved_tid =3D *tid; - *tid =3D __task_pid_nr_ns(next_task, PIDTYPE_PID, common->ns); + *tid =3D __task_pid_nr_ns(task, PIDTYPE_PID, common->ns); if (!*tid || *tid =3D=3D common->pid) { /* Run out of tasks of a process. The tasks of a * thread_group are linked as circular linked list. @@ -82,13 +82,11 @@ static struct task_struct *task_group_seq_get_next(stru= ct bpf_iter_seq_task_comm =20 common->pid_visiting =3D *tid; =20 - if (skip_if_dup_files && next_task->files =3D=3D next_task->group_leader-= >files) { - task =3D next_task; + if (skip_if_dup_files && task->files =3D=3D task->group_leader->files) goto retry; - } =20 - get_task_struct(next_task); - return next_task; + get_task_struct(task); + return task; } =20 static struct task_struct *task_seq_get_next(struct bpf_iter_seq_task_comm= on *common, --=20 2.25.1.362.g51ebf55 From nobody Thu Dec 18 06:14:35 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 08A91EE49A3 for ; Fri, 25 Aug 2023 16:22:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343995AbjHYQVz (ORCPT ); Fri, 25 Aug 2023 12:21:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35108 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344012AbjHYQVh (ORCPT ); Fri, 25 Aug 2023 12:21:37 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3A9342126 for ; Fri, 25 Aug 2023 09:20:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1692980450; 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: in-reply-to:in-reply-to; bh=DGZ49Q7hi6AstDObdejE3G09kyzhv7FaAvKHz09+bjg=; b=HjmQ7CSFcN3NLDbLMyuTxojuErkpZrmvXP0pBQOXFCx/5LsfxS39wrJjPetRB6AgHPyikZ MiFcVvBofR+ajwfv2zg9/MZrJF/7D2z5sDyfdlQEgz3CJWbnPHSX9SLFOlMAOqh/7CUHa6 Z642Ij7lv6G/4XDQUb8HSqqcn/ZgKY8= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-31-QVmpS7HjOWKaQKb9kBmnTg-1; Fri, 25 Aug 2023 12:20:46 -0400 X-MC-Unique: QVmpS7HjOWKaQKb9kBmnTg-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 643FF811726; Fri, 25 Aug 2023 16:20:45 +0000 (UTC) Received: from dhcp-27-174.brq.redhat.com (unknown [10.45.225.136]) by smtp.corp.redhat.com (Postfix) with SMTP id E4B672026D68; Fri, 25 Aug 2023 16:20:42 +0000 (UTC) Received: by dhcp-27-174.brq.redhat.com (nbSMTP-1.00) for uid 1000 oleg@redhat.com; Fri, 25 Aug 2023 18:19:58 +0200 (CEST) Date: Fri, 25 Aug 2023 18:19:54 +0200 From: Oleg Nesterov To: Andrew Morton , Yonghong Song Cc: "Eric W. Biederman" , Linus Torvalds , Daniel Borkmann , Kui-Feng Lee , Andrii Nakryiko , Martin KaFai Lau , bpf@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 5/6] bpf: task_group_seq_get_next: simplify the "next tid" logic Message-ID: <20230825161954.GA16886@redhat.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20230825161842.GA16750@redhat.com> User-Agent: Mutt/1.5.24 (2015-08-30) X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Kill saved_tid. It looks ugly to update *tid and then restore the previous value if __task_pid_nr_ns() returns 0. Change this code to update *tid and common->pid_visiting once before return. Signed-off-by: Oleg Nesterov Acked-by: Yonghong Song --- kernel/bpf/task_iter.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/kernel/bpf/task_iter.c b/kernel/bpf/task_iter.c index f51f476ec679..7473068ed313 100644 --- a/kernel/bpf/task_iter.c +++ b/kernel/bpf/task_iter.c @@ -37,7 +37,7 @@ static struct task_struct *task_group_seq_get_next(struct= bpf_iter_seq_task_comm { struct task_struct *task; struct pid *pid; - u32 saved_tid; + u32 next_tid; =20 if (!*tid) { /* The first time, the iterator calls this function. */ @@ -70,21 +70,18 @@ static struct task_struct *task_group_seq_get_next(stru= ct bpf_iter_seq_task_comm retry: task =3D next_thread(task); =20 - saved_tid =3D *tid; - *tid =3D __task_pid_nr_ns(task, PIDTYPE_PID, common->ns); - if (!*tid || *tid =3D=3D common->pid) { + next_tid =3D __task_pid_nr_ns(task, PIDTYPE_PID, common->ns); + if (!next_tid || next_tid =3D=3D common->pid) { /* Run out of tasks of a process. The tasks of a * thread_group are linked as circular linked list. */ - *tid =3D saved_tid; return NULL; } =20 - common->pid_visiting =3D *tid; - if (skip_if_dup_files && task->files =3D=3D task->group_leader->files) goto retry; =20 + *tid =3D common->pid_visiting =3D next_tid; get_task_struct(task); return task; } --=20 2.25.1.362.g51ebf55 From nobody Thu Dec 18 06:14:35 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id ED74CEE49A5 for ; Fri, 25 Aug 2023 16:22:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344001AbjHYQV4 (ORCPT ); Fri, 25 Aug 2023 12:21:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35154 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344022AbjHYQVo (ORCPT ); Fri, 25 Aug 2023 12:21:44 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 47576212B for ; Fri, 25 Aug 2023 09:20:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1692980453; 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: in-reply-to:in-reply-to; bh=YQBzsA/Z+wipgrDrsnQY+IF6MUsXkrkm4AfJgFKQYa8=; b=IE0pT17OSh0O2TS20Y3yvITR+LZ7JYzGaMfylrqQgq0FZ2EEtLS1tWM0k+2otNwXNow/Zi gwwpE7Ht1DNN78RFPEX1gNuuhwAJlpTHr3e/jO39zjK20+bJvn4sv3ZBqTRMvvtjCnmFkn xAUCDCfo78FO66ZSW6E9XZQfy8mQAbU= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-527-89Uwb9boOTuNfZZ-1GzX0A-1; Fri, 25 Aug 2023 12:20:49 -0400 X-MC-Unique: 89Uwb9boOTuNfZZ-1GzX0A-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id A7FBA811731; Fri, 25 Aug 2023 16:20:48 +0000 (UTC) Received: from dhcp-27-174.brq.redhat.com (unknown [10.45.225.136]) by smtp.corp.redhat.com (Postfix) with SMTP id 75A642166B27; Fri, 25 Aug 2023 16:20:46 +0000 (UTC) Received: by dhcp-27-174.brq.redhat.com (nbSMTP-1.00) for uid 1000 oleg@redhat.com; Fri, 25 Aug 2023 18:20:02 +0200 (CEST) Date: Fri, 25 Aug 2023 18:19:59 +0200 From: Oleg Nesterov To: Andrew Morton , Yonghong Song Cc: "Eric W. Biederman" , Linus Torvalds , Daniel Borkmann , Kui-Feng Lee , Andrii Nakryiko , Martin KaFai Lau , bpf@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 6/6] bpf: task_group_seq_get_next: use __next_thread() rather than next_thread() Message-ID: <20230825161959.GA16893@redhat.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20230825161842.GA16750@redhat.com> User-Agent: Mutt/1.5.24 (2015-08-30) X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Lockless use of next_thread() should be avoided, task_group_seq_get_next() is the last user, it too can return the group leader twice if it races with mt-thread exec which changes the group->leader's pid. Change the main loop to use __next_thread(), kill "next_tid =3D=3D common->= pid" check. __next_thread() can't loop forever, so we can also change this code to retry if next_tid =3D=3D 0. Signed-off-by: Oleg Nesterov --- kernel/bpf/task_iter.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/kernel/bpf/task_iter.c b/kernel/bpf/task_iter.c index 7473068ed313..8c847d91cdd9 100644 --- a/kernel/bpf/task_iter.c +++ b/kernel/bpf/task_iter.c @@ -68,15 +68,13 @@ static struct task_struct *task_group_seq_get_next(stru= ct bpf_iter_seq_task_comm return NULL; =20 retry: - task =3D next_thread(task); + task =3D __next_thread(task); + if (!task) + return NULL; =20 next_tid =3D __task_pid_nr_ns(task, PIDTYPE_PID, common->ns); - if (!next_tid || next_tid =3D=3D 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; =20 if (skip_if_dup_files && task->files =3D=3D task->group_leader->files) goto retry; --=20 2.25.1.362.g51ebf55