From patchwork Thu Feb 8 09:09:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13549495 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BFA5D6A02A for ; Thu, 8 Feb 2024 09:09:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707383380; cv=none; b=IsJ31Anu/srYWhijdFhOvOyLWXpDiLbV4U8eiO1rM4TkMharsrgH4d351uJpgmkcI+vwQtYOjUsGBcN+wQj813w6c+/65xc/Kuk+yyxm+9QRpRSquyv4gNwN09Drfvwa9yspcZf2EFhqximUeD75d+pvaAV+6rrt8JMR2LK+gl4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707383380; c=relaxed/simple; bh=0Xg8D3thkvvTg+XTPkXUNVMk3MvQtOboB6WznyesL7w=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=l7IFeAXpe9rkQysmbCYyD1F1LhAMjQq81qIsZ2gwz1MWYg2cqLMQRfY+bENHzdVFd9AuBiMIwO3Nq7NI8Pk6p8WKLoe/+Y3rurrQvJaPVnfv3f6djK5IMxcdFMHXr18ANMLfTrEjebLMcFXPxS/o7sQ8XJoEcxwXVgBf4rDRzvQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=UVgRbRvX; arc=none smtp.client-ip=209.85.214.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="UVgRbRvX" Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-1d7858a469aso12831725ad.2 for ; Thu, 08 Feb 2024 01:09:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1707383378; x=1707988178; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=JCpdk8A8Md1t87+15YVAWFKDKY6a11dgmE/s4ip1vTM=; b=UVgRbRvXz0n4/cohCXquRDQ5ULHSkxNYYKgcECwhe0RnGtcI8xOF8Q7UqZqXoxQr4Z p9f+uj4A1OAKPdOyqhmj4u7U7w0S0Sl5+R55HHnFxBxL1oWI7omujAMN0L9FZQywZXR0 kMm9iiF8Ar7hiqRhHt6ajFD0LD82dJrLqYvkIwOh/OkUGe0gjneuKs/mCx1a+EDhWzDv u2DZh9DnnNDtp/I1pAGrqLjt/e0L6KJvgA34/SukZP+JCf4q8B1yWSe9R2FZqCtgz8Aw lWQlGelc8vTH3MdYto1P3MDz8PDPnE4b7TvKplM5yBrBtsRAPjDOJ3+EqyYYbh4+AWIm oITQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707383378; x=1707988178; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=JCpdk8A8Md1t87+15YVAWFKDKY6a11dgmE/s4ip1vTM=; b=b7MPno7YaaonY65M8RLdj/ESD6fslGkSjBMA9yyH+TyuIsd3PROKmgb338qGCsTdPt 2IsPKOg6f0VyPLW3vIq9r07wB5juHI2GtgzB8oxaDHpu5PvzmIB7wvzhIhDbgc4pgeiE 2tB9/9oe00rEKzVi/QrCyF0517lVn+cnIBUKg7SsUK5NlFFjFmt1unBESVDjPKsLQXaD XSaHV3VIv9GVcYx3SWuEctA4+JOrb8yqbQLYDUSrQ9vuV1nAelGnHEVy435nBPFKfpCy 7FowHnzWMfQwmF7lzAwDYm30KTzsXSvnw4TgAblFY61P13UgRu8GzIUHP5Mv6vAGngPA w1ng== X-Gm-Message-State: AOJu0YxGLf/xc171KxsoqoYCMy5DpFGLqn6cqy5Jto035xmTCDb1JMrQ SZyR7ctqOeaUVJc0NNbdQf5WJULmWoseIKzrDTg9J6MaSNGq1WCf X-Google-Smtp-Source: AGHT+IEdzvjrujz6FgX01DHlL8gAEn5weJnMx5wxnLdUUDADYL+fTVRNKB8wMNeINVQDJoVcMY8Ijw== X-Received: by 2002:a17:90b:20d:b0:296:a529:4d5d with SMTP id fy13-20020a17090b020d00b00296a5294d5dmr5588826pjb.39.1707383377928; Thu, 08 Feb 2024 01:09:37 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCUytfFkHBfoKZ/N/IlBI9mcDCrSMxcnk5VqXR7CnbxpCzKRHNdSufLDSzto9ay16YlUplByG7F9HQfOiQo5pE37kgoUUKcL/EahAogfc+YonGVn+w+i9J0qZEysGT+vzQfi7Ox64kQUTkzQ5lJFN6IsJ3TGkheDv/j84ButBjimFSaUJY9k/fzK2jGsnSsYhL898VRlk3zwOBSnWNr686RUohC1TSxKBfQR3d+DzC8ZvfgrXMFqErnAUv6ZxE2gELTDTRPdDwjrUPE4F5SdkqWs/7azLr2ToJeIHWU0AkL1xiFegGnHB72lve/JvqIRs892xu4fz7aL+lk+wyEH3Odxo+9LKloSLNo9J4l1f0v1WzfIvjzSwu1uFxV7gJlndVI0iMtePrxIxp6tJBpMkdUPEpw93U6pKX/KUhd0 Received: from localhost.localdomain ([39.144.103.18]) by smtp.gmail.com with ESMTPSA id gg18-20020a17090b0a1200b0029685873233sm952361pjb.45.2024.02.08.01.09.26 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 08 Feb 2024 01:09:37 -0800 (PST) From: Yafang Shao To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, andrii@kernel.org, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org Cc: bpf@vger.kernel.org, Yafang Shao , Chuyi Zhou Subject: [PATCH bpf-next 1/3] bpf: Fix an issue due to uninitialized bpf_iter_task Date: Thu, 8 Feb 2024 17:09:04 +0800 Message-Id: <20240208090906.56337-2-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240208090906.56337-1-laoar.shao@gmail.com> References: <20240208090906.56337-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net Failure to initialize it->pos, coupled with the presence of an invalid value in the flags variable, can lead to it->pos referencing an invalid task, potentially resulting in a kernel panic. To mitigate this risk, it's crucial to ensure proper initialization of it->pos to 0. Fixes: c68a78ffe2cb ("bpf: Introduce task open coded iterator kfuncs") Signed-off-by: Yafang Shao Cc: Chuyi Zhou Acked-by: Yonghong Song --- kernel/bpf/task_iter.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/kernel/bpf/task_iter.c b/kernel/bpf/task_iter.c index e5c3500443c6..ec4e97c61eef 100644 --- a/kernel/bpf/task_iter.c +++ b/kernel/bpf/task_iter.c @@ -978,6 +978,8 @@ __bpf_kfunc int bpf_iter_task_new(struct bpf_iter_task *it, BUILD_BUG_ON(__alignof__(struct bpf_iter_task_kern) != __alignof__(struct bpf_iter_task)); + kit->pos = NULL; + switch (flags) { case BPF_TASK_ITER_ALL_THREADS: case BPF_TASK_ITER_ALL_PROCS: From patchwork Thu Feb 8 09:09:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13549496 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-pg1-f172.google.com (mail-pg1-f172.google.com [209.85.215.172]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A44746931C for ; Thu, 8 Feb 2024 09:09:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707383393; cv=none; b=bkJVt6VqrTlinvMvemtyr3T1HIOyCc55w5gO9uWAgom7FECynol7C/7QTWqV64Tmdbhsw9S5QOnFpHfIDLxLgWaeQ6JJrqFG7v7s0xdp5DiN+itIYcTxtl3pr5ioSf7P4ZE6MgjuwTXu3/UY337hqzRjQS3XHujMBcPywSUjp8w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707383393; c=relaxed/simple; bh=UgSHNW8lssCl1G9AZjbvSGCwOQyWHiD28zM9BeFYB5k=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=XAfFJ7wurWbNgnlLcWJXUotVkpb8Fsmb6Jhc2T2b9pFlWMVHmw72VeBHmnneTnmgQyCFzBgjvjKe4yfBRvNvegSt38e6nYgJITXLcwmJif39OpDuhtbNFwvQ3/vtI0K55NjPrn/PDEDvWy3bA3Uwm3ZLB6K0lJD4iYZhjKz1KHU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=iqzC83I4; arc=none smtp.client-ip=209.85.215.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="iqzC83I4" Received: by mail-pg1-f172.google.com with SMTP id 41be03b00d2f7-5d8df34835aso533109a12.0 for ; Thu, 08 Feb 2024 01:09:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1707383391; x=1707988191; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=YloBOO91rudoAeMOs6kzXhUv6qEy0P5+n2VgX0+i3jU=; b=iqzC83I4+TvyscLMBM3tkEq1vTMzq/49g72hkBjDtKydDexy1ZOQT3Wl98fWl1obay wTgXQzy8i1tqItX69ZeAypbiPEyRy/T0oIsHeKONOJ520ypVE903GzPgLxl+WstedVn/ K8R86WAbd2fif8RDUi701MHq7eLWjTZvUvUgFXPcHIAPqA7I4iFbW+4+5/sdGTM86eGy 75RcZ7VXRpBx1XH0pNR6Bzs5W/S/bU2dDA5tS/6f5/+7CzhUwv/4AGikqnR95207qxvD Myv7U3ZpP6embgnn//CLB+ZUQ3PVEasBC9Lr3/oVYtxuOHld8WUQJAqs5TftzN76IEYk Xkmw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707383391; x=1707988191; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=YloBOO91rudoAeMOs6kzXhUv6qEy0P5+n2VgX0+i3jU=; b=ptxXNlXOIFXKqyimcvrWx1w/iQbgu1ewKvfBqzjxVxLHk0l0LK0mulwmaquaUTcRoE w0l9BLNw/5rz8Q8gHrpqKSTqS1dRDrSq/TGleG3dc32xT7qReDChHTV+aCvh+/hpUr6k Umu+Vy+4sh7gC32lbNygagcxoOev1+SYUN4x8c+l8wl7tvNpNfnGRMAPyBGjUPI1whxN St9AQRmly5S2kOGYSOwubfH9F0U8ycu0BguERwGBoEKFBMA8VnFCK6I9hoAfXbW5hNSd IXj6BERjTXtEHLkmtpFpklULT27CfphlP14RRel5C0TxOOMcV9QeywAeNBormxPfBQUn M2lw== X-Gm-Message-State: AOJu0Yw97UionbhOrj/X8pc4Ih+MafbKOkdriQtIKnpkek6QJTXyRYmH ynPHpXvs9hYj/PeWmrPtKyrwJ80abhp33F5UfMXOGPAMVnFFYAq9 X-Google-Smtp-Source: AGHT+IE/mr2Gzl5E5wsnkIlFJyIFMkFDTK7ALA1ZbzjDVl5XynftVF0AsvDOSflbXyZIkJAsm/BufA== X-Received: by 2002:a17:90a:e2ca:b0:296:1fc8:5e66 with SMTP id fr10-20020a17090ae2ca00b002961fc85e66mr3252644pjb.6.1707383390736; Thu, 08 Feb 2024 01:09:50 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCVkj4V24dSIQVZyAUpnRKbSXSm/IOk2Q9lCnujpmHaiv2OUPFa9udHASLd6ahGzXn7s+kVfp7yqx5zzY/kVApD0TpyTbkHnHPkww5M5xxu0xASBrMDvye/hF4H1eazhKBFA/5vT3Xt/ZChs7GJBdyyMKzW3YrFpPMBl0ZUjf5QmJo9JSQH1NuCGcKdCUIl9Es3e4HjxCmWwFU/ybFE7UM6BPjXGE2WWfwYG2l1l4CM1pmZAup6V75bcerxR9aOl8ZILYbJBTVj91OJ4PRvSwdgx1z+6NZtJQGY4yXJW7bevWDbGc7e34cz5mKYBkN9euHYol2EoMKirsCKR0ekB31izQR705D2Mk4ReWheSdOmjfLzXwfFFzQNEKe5lOijGs5hK8QdeycJ/2XG+4wIZHtIX2ulrs9v2PNPKP6VI Received: from localhost.localdomain ([39.144.103.18]) by smtp.gmail.com with ESMTPSA id gg18-20020a17090b0a1200b0029685873233sm952361pjb.45.2024.02.08.01.09.38 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 08 Feb 2024 01:09:50 -0800 (PST) From: Yafang Shao To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, andrii@kernel.org, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org Cc: bpf@vger.kernel.org, Yafang Shao , Chuyi Zhou Subject: [PATCH bpf-next 2/3] selftests/bpf: Add negtive test cases for task iter Date: Thu, 8 Feb 2024 17:09:05 +0800 Message-Id: <20240208090906.56337-3-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240208090906.56337-1-laoar.shao@gmail.com> References: <20240208090906.56337-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net Incorporate a test case to assess the handling of invalid flags or task__nullable parameters passed to bpf_iter_task_new(). Prior to the preceding commit, this scenario could potentially trigger a kernel panic. However, with the previous commit, this test case is expected to function correctly. Signed-off-by: Yafang Shao Cc: Chuyi Zhou --- tools/testing/selftests/bpf/prog_tests/iters.c | 1 + tools/testing/selftests/bpf/progs/iters_task.c | 12 +++++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/bpf/prog_tests/iters.c b/tools/testing/selftests/bpf/prog_tests/iters.c index bf84d4a1d9ae..3c440370c1f0 100644 --- a/tools/testing/selftests/bpf/prog_tests/iters.c +++ b/tools/testing/selftests/bpf/prog_tests/iters.c @@ -193,6 +193,7 @@ static void subtest_task_iters(void) ASSERT_EQ(skel->bss->procs_cnt, 1, "procs_cnt"); ASSERT_EQ(skel->bss->threads_cnt, thread_num + 1, "threads_cnt"); ASSERT_EQ(skel->bss->proc_threads_cnt, thread_num + 1, "proc_threads_cnt"); + ASSERT_EQ(skel->bss->invalid_cnt, 0, "invalid_cnt"); pthread_mutex_unlock(&do_nothing_mutex); for (int i = 0; i < thread_num; i++) ASSERT_OK(pthread_join(thread_ids[i], &ret), "pthread_join"); diff --git a/tools/testing/selftests/bpf/progs/iters_task.c b/tools/testing/selftests/bpf/progs/iters_task.c index c9b4055cd410..e4d53e40ff20 100644 --- a/tools/testing/selftests/bpf/progs/iters_task.c +++ b/tools/testing/selftests/bpf/progs/iters_task.c @@ -10,7 +10,7 @@ char _license[] SEC("license") = "GPL"; pid_t target_pid; -int procs_cnt, threads_cnt, proc_threads_cnt; +int procs_cnt, threads_cnt, proc_threads_cnt, invalid_cnt; void bpf_rcu_read_lock(void) __ksym; void bpf_rcu_read_unlock(void) __ksym; @@ -26,6 +26,16 @@ int iter_task_for_each_sleep(void *ctx) procs_cnt = threads_cnt = proc_threads_cnt = 0; bpf_rcu_read_lock(); + bpf_for_each(task, pos, NULL, ~0U) { + /* Below instructions shouldn't be executed for invalid flags */ + invalid_cnt++; + } + + bpf_for_each(task, pos, NULL, BPF_TASK_ITER_PROC_THREADS) { + /* Below instructions shouldn't be executed for invalid task__nullable */ + invalid_cnt++; + } + bpf_for_each(task, pos, NULL, BPF_TASK_ITER_ALL_PROCS) if (pos->pid == target_pid) procs_cnt++; From patchwork Thu Feb 8 09:09:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13549497 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9B8CE6BB25 for ; Thu, 8 Feb 2024 09:10:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707383403; cv=none; b=lk4rDNJ33Q7v7Oiv98vXWzrtqVZZvUOjFbIG/ZPfIky4qQeUZxfP2NI0TsIsS0mDsz++wk3XnjSqgk1DvezSIDOVUsuTsD88PC+ra1/KUhTvre8x03FlUwXDVGu2VvlaUjH1Io+0uPkiwXR86WQKkFIiq854WG2ZQbFY719qci8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707383403; c=relaxed/simple; bh=7YAviJtne8Q1HbC8GlxM7WH84w5CNQljmhiywgGWsvI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=obmKwQxcf1X2gGwZGDmshG6K06GfIoTLV5lmZOr6j2hhUWMkZsHW+sgaobI7NyJCs7qAiJizHwli79dylAJMQA0COuo+ckL9KRhryAW0A0UZbKd5AwEiUOsRXfqljyalDft3xOPUg2ZLfClgOrE1OYhVOZSesaBxCQ/U/XgqRV0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=EsVvG76H; arc=none smtp.client-ip=209.85.214.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="EsVvG76H" Received: by mail-pl1-f172.google.com with SMTP id d9443c01a7336-1d8ef977f1eso12761665ad.0 for ; Thu, 08 Feb 2024 01:10:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1707383401; x=1707988201; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=6vxqu4vefOu8/xhw0fEkde3w8WIGJp/rlWw1+Z3g4yo=; b=EsVvG76Hy4gjbo/ucNEdHzhFGJruwueHSYI+5aCyH8E6YmVrX6Fjksq7j7kODUUB49 PT+08jyU2D5iyr5Gf22YZvivKwWQOPA6eMFtc1aQPCU2UfMPhZlLuymYmryxPffnXJGM 9E2ySdzbOHELuXEgwrCU7jvuoXBmeyjgZSU01YsomPaTxxLP6fJ5TsCEu9we0eOyc/dU aO25qDXlls4D7garLgnYqaXHJpildFFFYT3NXIPfjx+Z3C3kmnZMvGpz14iwVPa07QMF 5KATivTc6MAzbqhLxvvqBB2lzzVPXn6EedYDcAcUXRsgSD5Mcb0EbO9GXHjiXRK3zA1i IkNw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707383401; x=1707988201; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=6vxqu4vefOu8/xhw0fEkde3w8WIGJp/rlWw1+Z3g4yo=; b=fVl6FDUuCpCZzVv3CJDyHRDLVe3hUWSTbVshuvHPTX9oF2fSv4W+SKj1idcSI8vrAc gn/HjkFmVzptmdCErMesPn01p1D5/VzF7rOqTwcOtMv8ljGcXM0Q6BRPOvNKsm+CDEpW nMVgY3ka2ID1ylw4SVhXv31T+l3gW89kWXtbFhBa0owk2Mxa5fHzF+ScOTLC9rklEada IuMstq8Dgmg/pynLKmDbtNv7A4vJucAvuVZTs41tJh+NfK5qJ+6VrZnn8eDJGvM0/DsJ IdYlb5bOE4Uh6HD7hJSwJTgOMo6lN/Q61KY3e8aC1UC066kz9KRSWfiRUhfw8yTuwLdw 9vFQ== X-Gm-Message-State: AOJu0YyoYWZ9NxiZsxQfi9AtTiatiOB8LuEopw1Adh8pahCufDNG5off Dvst0RDq6UKIKJ1EAj1uSDngwLf2L0fdFIa3ezmN0W1Dd0yVR0Fw X-Google-Smtp-Source: AGHT+IEBATmBuDtAuOjeLyNCcfuk5Db6S2fWzuR05WgWsZm7kJVpwzP67J+am5K+Eke1/Un0h5iiJA== X-Received: by 2002:a17:90b:80e:b0:296:3a5:6fb8 with SMTP id bk14-20020a17090b080e00b0029603a56fb8mr5191505pjb.25.1707383400802; Thu, 08 Feb 2024 01:10:00 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCUGcECXuq4V49UanEMwJrBXkcMDoGbiF2w4X35SDmpjzwL2Aom52416v4Fa3QAQ8UI858rRAdMPEiTySiiMf1pcivVpx99WfNGoKmNR9efVV1xw2Lp2ftmfnmLEYqG7E4s1OHxU9CPv12vLGYCuZoPr55DKXAsWMCVWTKUVZIsxskwL0Pgxzq8Dwn7P2+u8A1PCwKKWeJOu7yrCOMrb1pxcimvK4OvphE/eL0f8ThPJn6VqKpgIpkirhCHOoQHdlt6aQe4Yk8QzRJBsScphOJG6JTB3ATOWKtZas6pifRfdjFSZyWoOnKJresWlxaKjY5wxwmUCGYmLviGt5fkykYmjXKF+n7HfC8rGFUA27r8T6idiNi1kbbPIxcIJ+PmezW4Q0Kw= Received: from localhost.localdomain ([39.144.103.18]) by smtp.gmail.com with ESMTPSA id gg18-20020a17090b0a1200b0029685873233sm952361pjb.45.2024.02.08.01.09.51 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 08 Feb 2024 01:10:00 -0800 (PST) From: Yafang Shao To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, andrii@kernel.org, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org Cc: bpf@vger.kernel.org, Yafang Shao Subject: [PATCH bpf-next 3/3] libbpf: Check the return value of bpf_iter__new() Date: Thu, 8 Feb 2024 17:09:06 +0800 Message-Id: <20240208090906.56337-4-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240208090906.56337-1-laoar.shao@gmail.com> References: <20240208090906.56337-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net On success, bpf_iter__new() return 0. On failure, it returns ERR. We'd better check the return value of it. Signed-off-by: Yafang Shao --- tools/lib/bpf/bpf_helpers.h | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/tools/lib/bpf/bpf_helpers.h b/tools/lib/bpf/bpf_helpers.h index 79eaa581be98..2cd2428e3bc6 100644 --- a/tools/lib/bpf/bpf_helpers.h +++ b/tools/lib/bpf/bpf_helpers.h @@ -133,6 +133,15 @@ # define __bpf_unreachable() __builtin_trap() #endif +#ifndef __must_check +#define __must_check __attribute__((warn_unused_result)) +#endif + +static inline void * __must_check ERR_PTR(long error) +{ + return (void *) error; +} + /* * Helper function to perform a tail call with a constant/immediate map slot. */ @@ -340,14 +349,13 @@ extern void bpf_iter_num_destroy(struct bpf_iter_num *it) __weak __ksym; /* initialize and define destructor */ \ struct bpf_iter_##type ___it __attribute__((aligned(8), /* enforce, just in case */, \ cleanup(bpf_iter_##type##_destroy))), \ - /* ___p pointer is just to call bpf_iter_##type##_new() *once* to init ___it */ \ *___p __attribute__((unused)) = ( \ - bpf_iter_##type##_new(&___it, ##args), \ /* this is a workaround for Clang bug: it currently doesn't emit BTF */ \ /* for bpf_iter_##type##_destroy() when used from cleanup() attribute */ \ - (void)bpf_iter_##type##_destroy, (void *)0); \ + (void)bpf_iter_##type##_destroy, \ + ERR_PTR(bpf_iter_##type##_new(&___it, ##args))); \ /* iteration and termination check */ \ - (((cur) = bpf_iter_##type##_next(&___it))); \ + ((!___p) && ((cur) = bpf_iter_##type##_next(&___it))); \ ) #endif /* bpf_for_each */