From patchwork Wed May 8 06:32:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kui-Feng Lee X-Patchwork-Id: 13658112 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-oi1-f175.google.com (mail-oi1-f175.google.com [209.85.167.175]) (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 E623E384 for ; Wed, 8 May 2024 06:32:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715149946; cv=none; b=bDzlsBfO1Wws5YwL6QdoGbfco1vUGnGpIjNjzZRNent+ddPJafdssBxPzyRSMo/GFrncagk3Um/uq/sAgsshikW89YKyV9uzpgehV4cdDf1UuypxGOJD448/PPtl7Xh9M6LPJ1QKd+rWrMopqVeZZe3DO0o2R+nEmKueKv04dY0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715149946; c=relaxed/simple; bh=pCN0gUblbq3sf5AdhXjHdOKajyp+ZRRHTuC/nrcTzpo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=tB5sDF7BsQdD33qgv9QGnTfg9kn9gd8XWSFZExVpRzLfUVt0bgjfy1whoL+mOcHe5Fp1fkImxDzOFSbtEtC33jeynVfVZ7mBAh8mGGQuRum2OpJYYbZN2oZAVIBcZ3RakOeRzw1zw6T0nKU7MDJRXQjJ1FPXe87LDlgDzzpZ5IY= 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=M+y3fz24; arc=none smtp.client-ip=209.85.167.175 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="M+y3fz24" Received: by mail-oi1-f175.google.com with SMTP id 5614622812f47-3c989d4e838so62923b6e.1 for ; Tue, 07 May 2024 23:32:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1715149944; x=1715754744; 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=ITUVE3cgCYC2CFtIbPVKh4YbIS+9WyVA1G9yycG1a2M=; b=M+y3fz24FmaYeQGTtquuJHogFDBmfZ0rZhPldppaeqGMiDAVxoP2QhKMAszD4nYlm/ PIFOTf2LK+IXx9TQLfUF4Buu918LkLtkm/zH3KZD6hJ1iImCGziEnko+ntRxsuebH44N f6gLrmkReVViZM5tNDtB8PdwbUxSEYsiudn9+7YTRN7NFWdEI8CeM0OdglKHumX5IePu oUipEUPvQoPBzBVC8TFvJJ0u940HyEqvCvTyBeyanvc/RMN3LfFbb3OZ2fBtFIRYy8o5 DojfVPuAVxNHVDF9VbMDG1UJtrH3LfYWgwKol0G/7fGh39LTZK4IFnBR6vheXe1YBFNG 2mTA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715149944; x=1715754744; 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=ITUVE3cgCYC2CFtIbPVKh4YbIS+9WyVA1G9yycG1a2M=; b=F5Epf0OWxRHfU7WVTWeFAJtmoYoUHmgC8SJxYmZNLV228TLHRYYTbP1eIM4Ma7O0wC cxzlGM/gWTiYr/jkJ0iyE2l56Udo2khYqviXmaDD/noKzMd3BXbGklsNyA/f5u5GhiS5 FLUC/Th1nBnv6S+Cg4joeFlO8Tg5es/+PJspEeRu1dOFvh84GBa31IA3EPIAG/DfFyr1 xGrx2sXBPLOe35Sw+Dswfy5mWIXTzyU5Pab/8frWAhdV5OxQ1+mZyMR7C8Ie57sEf+zY 2ep6T2RrVJCVfFonDIRfT0xf2szOZA4b8H4CtLNOynK3U1ifVLTrGW5woM4lYppmQhKz usVg== X-Gm-Message-State: AOJu0YwKlH6tZa0AtI/WMcv0K4TWyvPOS6wYU97b0m34lCnRYazcSsk0 MO/Zg9OCJXcZiZv8h1NWTO4G/iPqm7/qeTrGXbjUXg/OmwSmJivdz+hRHA== X-Google-Smtp-Source: AGHT+IHFLvtGS8t5Mrst9RSiZpgJVeQ/FFJ9Obe4fIMx0jCLzrxNr0PKwAGpSsahsLr9pj2Ka4xmuA== X-Received: by 2002:a05:6808:18c:b0:3c9:63d1:6fde with SMTP id 5614622812f47-3c9852fb1a0mr1718372b6e.37.1715149943765; Tue, 07 May 2024 23:32:23 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:28e:823a:cbf2:fea6]) by smtp.gmail.com with ESMTPSA id z22-20020a056808029600b003c9729ac86dsm841371oic.11.2024.05.07.23.32.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 May 2024 23:32:23 -0700 (PDT) From: Kui-Feng Lee To: bpf@vger.kernel.org, ast@kernel.org, martin.lau@linux.dev, song@kernel.org, kernel-team@meta.com, andrii@kernel.org Cc: sinquersw@gmail.com, kuifeng@meta.com, Kui-Feng Lee , Eduard Zingerman Subject: [PATCH bpf-next v4 1/9] bpf: Remove unnecessary checks on the offset of btf_field. Date: Tue, 7 May 2024 23:32:10 -0700 Message-Id: <20240508063218.2806447-2-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240508063218.2806447-1-thinker.li@gmail.com> References: <20240508063218.2806447-1-thinker.li@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 reg_find_field_offset() always return a btf_field with a matching offset value. Checking the offset of the returned btf_field is unnecessary. Acked-by: Eduard Zingerman Signed-off-by: Kui-Feng Lee --- kernel/bpf/verifier.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 7360f04f9ec7..b731d00cf1ae 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -11640,7 +11640,7 @@ __process_kf_arg_ptr_to_graph_node(struct bpf_verifier_env *env, node_off = reg->off + reg->var_off.value; field = reg_find_field_offset(reg, node_off, node_field_type); - if (!field || field->offset != node_off) { + if (!field) { verbose(env, "%s not found at offset=%u\n", node_type_name, node_off); return -EINVAL; } From patchwork Wed May 8 06:32:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kui-Feng Lee X-Patchwork-Id: 13658114 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-oi1-f170.google.com (mail-oi1-f170.google.com [209.85.167.170]) (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 6FFE21DA53 for ; Wed, 8 May 2024 06:32:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715149948; cv=none; b=CmhFnVQbCFZg55MjcP7vsWWTOV+Y55RlQkZmDwOTuXU143X7FADY2KdjZ5Qmb0/QM+zpuR8QlW4ghAFaFFjdINh5C3MRcRilyhdIa3+ml+BQ2JUdLWT6Cf5EJ0kA5B/e5S2CMZw3kq5KH+kdyTy/P3+TLuXGrMGPv1rbOiQGJWs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715149948; c=relaxed/simple; bh=kCgappnLf09hMPvITmn+nEcPndSFxpBnw/V0ZLEwpSw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=lFy/MLamIpEXsCEu7/zzBDh453jkqgRhZcrQuy06Xsnqm8+li80Wl/iOkXpvGGUB3SLwOwDrUsvjVmG9EK3s3fSop2mlnugZnnSZR38VU4mqgW7XrJhbim3jQ7Pg3PBZk3Bcrd0gSaQOPiDnWpUpARwB0F4Eao0hEoOSn5/nUho= 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=KgCNhATp; arc=none smtp.client-ip=209.85.167.170 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="KgCNhATp" Received: by mail-oi1-f170.google.com with SMTP id 5614622812f47-3c96a556006so1776543b6e.0 for ; Tue, 07 May 2024 23:32:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1715149946; x=1715754746; 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=1OxWvA/tKSbMBMe3nvCIcsidoEQ4eab8W4E/mkVKEqI=; b=KgCNhATpSdTGhuHmlXWjTIwykEMRuzBOrmfHeCClVadsuZnJNagTnwrEqXhfc7y7um nph1pfuPWq0npw87Bd5N/iht6krCW5dntxlbnOwUajuFLSTbLhMQrE66S0VlZPstXrz8 ikeWcHgZ3OhHa0y2wkTfZmR2Iry+OaUmIFmjL5EOy2SJlZvp4c1dQPj4+L16ckgdQiG3 0FBN6JiRf26hPOIQciXoaV4nA3k7oc8GfY4ZfFmogwSERvp1olEvrzcaJtlcawG/ANjO e1lJFgb5AJn1gYhqDHcor8SlQgbWFosvzs+fsohrGayU/E+N62NjzYsT1yzyGGoCnjf/ Dhpw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715149946; x=1715754746; 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=1OxWvA/tKSbMBMe3nvCIcsidoEQ4eab8W4E/mkVKEqI=; b=qSx9MxCgBXoZwphYQBbBIhfTjs2AVfP8jJBPo16ihpPRDb6GGrtLJVFvPUuYjdBeVY 1WT4N/eYBd2mTG0wbToue/vhrmAO9uTf6NoRs7+KJX6cSsSP46VQ9ydJeaOH6jQ55uVF VcdfwaBMI9Q3ayW8L0tcStC40s5KYpPu2kfDo8F4yq7vw4+ydoeN2VF28MMhWWSIC3ZV UY69zxAmfRqJMsovskFdREC93h29JuJfYh1R50usC1hHet87Ef0wsXgjty5us31b6Ejn FvNa4/g1G3hG3DE030uEhGix/3NAmK65VXK+Mv2dpTcd7GefVVbMnWOLHqCZxjw1WFt9 rJAQ== X-Gm-Message-State: AOJu0Ywa7VqqhR5YvBUX/IYCYDcpJGAL2f/uO4A+phUtf//R+OX5T5Ir +jwsmQvX13NqfDiQMcldHfN6vMQpsT6KNL1qoirFNUrEP1DVSLEv8ezGiA== X-Google-Smtp-Source: AGHT+IF90pAxB3YJxxTWq+57ldHWg48onuV6MiWM/a1tzln77FE0tFtKR7WIgW6mVPUp6szCTzVVZA== X-Received: by 2002:aca:1311:0:b0:3c9:6009:87d5 with SMTP id 5614622812f47-3c9852fd64emr1623391b6e.42.1715149944830; Tue, 07 May 2024 23:32:24 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:28e:823a:cbf2:fea6]) by smtp.gmail.com with ESMTPSA id z22-20020a056808029600b003c9729ac86dsm841371oic.11.2024.05.07.23.32.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 May 2024 23:32:24 -0700 (PDT) From: Kui-Feng Lee To: bpf@vger.kernel.org, ast@kernel.org, martin.lau@linux.dev, song@kernel.org, kernel-team@meta.com, andrii@kernel.org Cc: sinquersw@gmail.com, kuifeng@meta.com, Kui-Feng Lee , Eduard Zingerman Subject: [PATCH bpf-next v4 2/9] bpf: Remove unnecessary call to btf_field_type_size(). Date: Tue, 7 May 2024 23:32:11 -0700 Message-Id: <20240508063218.2806447-3-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240508063218.2806447-1-thinker.li@gmail.com> References: <20240508063218.2806447-1-thinker.li@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 field->size has been initialized by bpf_parse_fields() with the value returned by btf_field_type_size(). Use it instead of calling btf_field_type_size() again. Acked-by: Eduard Zingerman Signed-off-by: Kui-Feng Lee --- kernel/bpf/btf.c | 2 +- kernel/bpf/verifier.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index 821063660d9f..226138bd139a 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -6693,7 +6693,7 @@ int btf_struct_access(struct bpf_verifier_log *log, for (i = 0; i < rec->cnt; i++) { struct btf_field *field = &rec->fields[i]; u32 offset = field->offset; - if (off < offset + btf_field_type_size(field->type) && offset < off + size) { + if (off < offset + field->size && offset < off + size) { bpf_log(log, "direct access to %s is disallowed\n", btf_field_type_name(field->type)); diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index b731d00cf1ae..94eeb1366a65 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -5448,7 +5448,7 @@ static int check_map_access(struct bpf_verifier_env *env, u32 regno, * this program. To check that [x1, x2) overlaps with [y1, y2), * it is sufficient to check x1 < y2 && y1 < x2. */ - if (reg->smin_value + off < p + btf_field_type_size(field->type) && + if (reg->smin_value + off < p + field->size && p < reg->umax_value + off + size) { switch (field->type) { case BPF_KPTR_UNREF: From patchwork Wed May 8 06:32:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kui-Feng Lee X-Patchwork-Id: 13658113 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-oi1-f181.google.com (mail-oi1-f181.google.com [209.85.167.181]) (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 DBDDA1A269 for ; Wed, 8 May 2024 06:32:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715149948; cv=none; b=tiqvw8RX1eDI1vhNzwDX2wyNIAM1whKHk5lERRhu7o6KZ4QbD7Y07Sv5KQT7ab+T4x6LZflKdxTDw7LFj+8jLoPxOogVnB4NVvgY6g3/E53gHWe5ApxIkNj2IF4otiN0Jawxmp4D6NANayUg6MhIWlZRe7ISANCAhJ3Ab2qdQ1w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715149948; c=relaxed/simple; bh=6znW+NE8zwp4bgpiun5+PGm3xlcUg+KGbPtaqm1Ye3w=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=SAq9BqyhRmQrcrfApB5Qna8z4+Z1eEi2rTbp4bBGSIShPFbiayen+e8mVnavhuCjjMz8+fjI74u3UW/ewEhwGPzwKrlc+dsAy4XMPNcFbal42ukb7SZsSR+bbYBerfafFM+C24QoXlEphJ6cdKcsoZCM7iHJu5cLY5Rx9+72UuI= 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=GwE2EfN1; arc=none smtp.client-ip=209.85.167.181 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="GwE2EfN1" Received: by mail-oi1-f181.google.com with SMTP id 5614622812f47-3c969fa8fd2so1856905b6e.1 for ; Tue, 07 May 2024 23:32:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1715149946; x=1715754746; 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=y1a3kDiHecRAIVMu8M6ylST2Yj/4mbIS/q1HrQnEk5I=; b=GwE2EfN1e/Rgl3KNy3UH0FllRb0aoDRGoG2UXSci7l3znz6GerJFA6o250gqKT2Nbq 58lSPvS52Fv9dgfDrH/OmtbzpVCw5TQMYLPBEBOTirqb90cvxRJ9KZjsNiBD4PqjNz8R FpD8lqmLra10V/3ZfJW0BHOH9037kxv2bMmfrCgTzRBpX7e58AyWuHM8rrbwMv1cWwUz I5I9VeXnsALiPi0nnK7kgn4jWEakmte3cMM3yPVen+0m02BgKPxF2ng8dxaWqKH5AlnK Dw+JepCxaH15qITU/6ndvbfnRvOLcri2O9QQyxm6t9axIw0KduQNq2i83jwzJQ2K44f3 PhZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715149946; x=1715754746; 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=y1a3kDiHecRAIVMu8M6ylST2Yj/4mbIS/q1HrQnEk5I=; b=WdFPr+osni//eJazfZJcnXsBK7aW7d30Xo/lL6PXFEMVlw8uUqZA54o4qoYne+arPk YZHVgItmHMldUeR1PLTM1rGbrXU8XG1LqgpNSXGyKyzHE3xZQSeVDkP1W4YCs1NRDOBA 8jAVwQGuAfz+IQgiCNd+qbu8/FiRxv+wEz41KZtZmEeWhbfqonWyJtgwr5jVc9UxrfyB It8VYq4oQsPDyAI3U6AGf3arqqLW5CPWgf/XLknuSxclZ+lygYReKcBztVrWHBvJNwD7 nOehepJ0UZpTFR7x5jxcDtqcBhiIwcQnaIJqNmf7UdMcipECDatJ45TZg/mQ6k1iyi9i /4Fg== X-Gm-Message-State: AOJu0YyQDGNJ1YITL2twkJES8+6ZyviLSj5NzcD2jdaFsA6axdv6HL5k PhuPg7x1hCwZYo0qvVmp1ZmyPZeUwsktCv6271+FDvAQWBLn1DkjR9anWA== X-Google-Smtp-Source: AGHT+IHxrtLuAxtRcqS+opnA2Jumv6yW1HQVzYB8v3VMttf62IW+PmHin/MBbOTc6VcNg2m9KbsOsg== X-Received: by 2002:a05:6808:2a8e:b0:3c8:47a3:3cb6 with SMTP id 5614622812f47-3c9852bd825mr2083676b6e.21.1715149945872; Tue, 07 May 2024 23:32:25 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:28e:823a:cbf2:fea6]) by smtp.gmail.com with ESMTPSA id z22-20020a056808029600b003c9729ac86dsm841371oic.11.2024.05.07.23.32.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 May 2024 23:32:25 -0700 (PDT) From: Kui-Feng Lee To: bpf@vger.kernel.org, ast@kernel.org, martin.lau@linux.dev, song@kernel.org, kernel-team@meta.com, andrii@kernel.org Cc: sinquersw@gmail.com, kuifeng@meta.com, Kui-Feng Lee Subject: [PATCH bpf-next v4 3/9] bpf: refactor btf_find_struct_field() and btf_find_datasec_var(). Date: Tue, 7 May 2024 23:32:12 -0700 Message-Id: <20240508063218.2806447-4-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240508063218.2806447-1-thinker.li@gmail.com> References: <20240508063218.2806447-1-thinker.li@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 Move common code of the two functions to btf_find_field_one(). Signed-off-by: Kui-Feng Lee Acked-by: Eduard Zingerman --- kernel/bpf/btf.c | 180 +++++++++++++++++++++-------------------------- 1 file changed, 79 insertions(+), 101 deletions(-) diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index 226138bd139a..2ce61c3a7e28 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -3494,72 +3494,95 @@ static int btf_get_field_type(const char *name, u32 field_mask, u32 *seen_mask, #undef field_mask_test_name +static int btf_find_field_one(const struct btf *btf, + const struct btf_type *var, + const struct btf_type *var_type, + int var_idx, + u32 off, u32 expected_size, + u32 field_mask, u32 *seen_mask, + struct btf_field_info *info, int info_cnt) +{ + int ret, align, sz, field_type; + struct btf_field_info tmp; + + field_type = btf_get_field_type(__btf_name_by_offset(btf, var_type->name_off), + field_mask, seen_mask, &align, &sz); + if (field_type == 0) + return 0; + if (field_type < 0) + return field_type; + + if (expected_size && expected_size != sz) + return 0; + if (off % align) + return 0; + + switch (field_type) { + case BPF_SPIN_LOCK: + case BPF_TIMER: + case BPF_WORKQUEUE: + case BPF_LIST_NODE: + case BPF_RB_NODE: + case BPF_REFCOUNT: + ret = btf_find_struct(btf, var_type, off, sz, field_type, + info_cnt ? &info[0] : &tmp); + if (ret < 0) + return ret; + break; + case BPF_KPTR_UNREF: + case BPF_KPTR_REF: + case BPF_KPTR_PERCPU: + ret = btf_find_kptr(btf, var_type, off, sz, + info_cnt ? &info[0] : &tmp); + if (ret < 0) + return ret; + break; + case BPF_LIST_HEAD: + case BPF_RB_ROOT: + ret = btf_find_graph_root(btf, var, var_type, + var_idx, off, sz, + info_cnt ? &info[0] : &tmp, + field_type); + if (ret < 0) + return ret; + break; + default: + return -EFAULT; + } + + if (ret == BTF_FIELD_IGNORE) + return 0; + if (!info_cnt) + return -E2BIG; + + return 1; +} + static int btf_find_struct_field(const struct btf *btf, const struct btf_type *t, u32 field_mask, struct btf_field_info *info, int info_cnt) { - int ret, idx = 0, align, sz, field_type; + int ret, idx = 0; const struct btf_member *member; - struct btf_field_info tmp; u32 i, off, seen_mask = 0; for_each_member(i, t, member) { const struct btf_type *member_type = btf_type_by_id(btf, member->type); - field_type = btf_get_field_type(__btf_name_by_offset(btf, member_type->name_off), - field_mask, &seen_mask, &align, &sz); - if (field_type == 0) - continue; - if (field_type < 0) - return field_type; - off = __btf_member_bit_offset(t, member); if (off % 8) /* valid C code cannot generate such BTF */ return -EINVAL; off /= 8; - if (off % align) - continue; - - switch (field_type) { - case BPF_SPIN_LOCK: - case BPF_TIMER: - case BPF_WORKQUEUE: - case BPF_LIST_NODE: - case BPF_RB_NODE: - case BPF_REFCOUNT: - ret = btf_find_struct(btf, member_type, off, sz, field_type, - idx < info_cnt ? &info[idx] : &tmp); - if (ret < 0) - return ret; - break; - case BPF_KPTR_UNREF: - case BPF_KPTR_REF: - case BPF_KPTR_PERCPU: - ret = btf_find_kptr(btf, member_type, off, sz, - idx < info_cnt ? &info[idx] : &tmp); - if (ret < 0) - return ret; - break; - case BPF_LIST_HEAD: - case BPF_RB_ROOT: - ret = btf_find_graph_root(btf, t, member_type, - i, off, sz, - idx < info_cnt ? &info[idx] : &tmp, - field_type); - if (ret < 0) - return ret; - break; - default: - return -EFAULT; - } - if (ret == BTF_FIELD_IGNORE) - continue; - if (idx >= info_cnt) - return -E2BIG; - ++idx; + ret = btf_find_field_one(btf, t, member_type, i, + off, 0, + field_mask, &seen_mask, + &info[idx], info_cnt - idx); + if (ret < 0) + return ret; + idx += ret; } return idx; } @@ -3568,66 +3591,21 @@ static int btf_find_datasec_var(const struct btf *btf, const struct btf_type *t, u32 field_mask, struct btf_field_info *info, int info_cnt) { - int ret, idx = 0, align, sz, field_type; + int ret, idx = 0; const struct btf_var_secinfo *vsi; - struct btf_field_info tmp; u32 i, off, seen_mask = 0; for_each_vsi(i, t, vsi) { const struct btf_type *var = btf_type_by_id(btf, vsi->type); const struct btf_type *var_type = btf_type_by_id(btf, var->type); - field_type = btf_get_field_type(__btf_name_by_offset(btf, var_type->name_off), - field_mask, &seen_mask, &align, &sz); - if (field_type == 0) - continue; - if (field_type < 0) - return field_type; - off = vsi->offset; - if (vsi->size != sz) - continue; - if (off % align) - continue; - - switch (field_type) { - case BPF_SPIN_LOCK: - case BPF_TIMER: - case BPF_WORKQUEUE: - case BPF_LIST_NODE: - case BPF_RB_NODE: - case BPF_REFCOUNT: - ret = btf_find_struct(btf, var_type, off, sz, field_type, - idx < info_cnt ? &info[idx] : &tmp); - if (ret < 0) - return ret; - break; - case BPF_KPTR_UNREF: - case BPF_KPTR_REF: - case BPF_KPTR_PERCPU: - ret = btf_find_kptr(btf, var_type, off, sz, - idx < info_cnt ? &info[idx] : &tmp); - if (ret < 0) - return ret; - break; - case BPF_LIST_HEAD: - case BPF_RB_ROOT: - ret = btf_find_graph_root(btf, var, var_type, - -1, off, sz, - idx < info_cnt ? &info[idx] : &tmp, - field_type); - if (ret < 0) - return ret; - break; - default: - return -EFAULT; - } - - if (ret == BTF_FIELD_IGNORE) - continue; - if (idx >= info_cnt) - return -E2BIG; - ++idx; + ret = btf_find_field_one(btf, var, var_type, -1, off, vsi->size, + field_mask, &seen_mask, + &info[idx], info_cnt - idx); + if (ret < 0) + return ret; + idx += ret; } return idx; } From patchwork Wed May 8 06:32:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kui-Feng Lee X-Patchwork-Id: 13658115 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-oi1-f179.google.com (mail-oi1-f179.google.com [209.85.167.179]) (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 2DCA9210E9 for ; Wed, 8 May 2024 06:32:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715149950; cv=none; b=XwhU3Uwt99fZB7fBVvZU/gxKMfB/ymZdnUbQQO2W+ccV4YXaGJfDY+Yh6uIpeLgBH/nz8QixJcF8QcZDOfaMQGONv+JZBufbfs2vUaSAs64XNIukb4MrDWUGkKQ3SaaVJtNXVfVCpAmKSS6f4mXayDb4vdbLVz2j+LtU+qowD5Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715149950; c=relaxed/simple; bh=f+PxVLjc+66TcKUvZRUTfOmyFfg8SYJG6hUBE43pnGU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=KRjwgHqJxPo4UcEf3M2yIev37ZCNGpH9e8FbmuCJ9QdJZ/ZVhEWvmXBM9GMFJ5ClnBAmgcc7bRxojAkK2gWnea3hQZGCSdJZdOkavMvTJawwCrJhrZ+ugUF3Q9l/Sv3EKdCE0ADl/E09FfdvTQhnUScV7gvV66YK7xP7qWuuUEw= 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=KSlUbI00; arc=none smtp.client-ip=209.85.167.179 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="KSlUbI00" Received: by mail-oi1-f179.google.com with SMTP id 5614622812f47-3c5ee4ce695so979749b6e.0 for ; Tue, 07 May 2024 23:32:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1715149948; x=1715754748; 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=A4r8v2MRLTFL20Ck49ByWH0xPmhJ1y/H0HI2kjXGJB0=; b=KSlUbI00K0jNYqLGXFhstZ9XqfvtIuNINXycWcAzBqb7w1avnmAIkBaOIhI1nmXvcE 2RqUTVqPGCDcKs6N6MZSkUY0K0YvKW/O7vsjykFoeo0TUc4NpaOL1m3awGHHStPzgpoX ut3YqdaX5ussJaFSFy5qymlSXykKMl/ClbXCY0NYW6AfPWZSldHVrn/EfdaOX/6jHcNg keJQUHedQVQeFloTVDqsx/rvqsgA/dgh0dUC6ELdH9dw7zO4OaegjO4lNM4Z/CIZNXGY ghKoH83RdryZ2aF4NQcAcy8+ge9lEB9JnznGPt1E7NnvRIEgzc0H+1S8rhcO+PXj2I7a JGYA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715149948; x=1715754748; 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=A4r8v2MRLTFL20Ck49ByWH0xPmhJ1y/H0HI2kjXGJB0=; b=pmSrqys50w0CDbidIyOaK6srLq/KjtGaQr0b/t/AB5H2FMg3TZhppUKqwYdH/Zi3j+ jWNVsm//p36EhdPPPIXly58TDx/lNVIhFtFfMY4AFhnFWEv8by0g6G61+TKm8Ml62V85 b7BzZ1wz6U2t/fT0e+QpKeAWFP8S1NLAVNrtvQ08Odt2Dqf3XCljpKITSCXk3+Rk0WtC eXOYhutBu84XichL7xICeKbanrZRJaqp9HdGcqNBvNtXSb1b+pQUg0q/RUAJI9KwbnAF xkHi8Se1fGcCKeszfcfsoTor7hXWK1sBAs8zaP5MkNfK/LnXprWaIiSDxH9qfCw4tVrL 5/JQ== X-Gm-Message-State: AOJu0Yz7Q/yiM9zx4XZJQGMNc1lRKuCUidTNiczdzf846PG2Ghxjc/cg Ck3o8GfcWV/u7KZHHhl80tv2+4T7VykaicJyRHcsiclJl8HW2e9EX5ye/Q== X-Google-Smtp-Source: AGHT+IF/ZXbcT6c+m/WClPs8yRavwldCkDnTuQ8loFnZy+M/oWW2k2RjuKwQ3cractU9j2LBAGpAEQ== X-Received: by 2002:a54:4411:0:b0:3c8:2bb4:2288 with SMTP id 5614622812f47-3c98529859bmr1675168b6e.2.1715149948033; Tue, 07 May 2024 23:32:28 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:28e:823a:cbf2:fea6]) by smtp.gmail.com with ESMTPSA id z22-20020a056808029600b003c9729ac86dsm841371oic.11.2024.05.07.23.32.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 May 2024 23:32:27 -0700 (PDT) From: Kui-Feng Lee To: bpf@vger.kernel.org, ast@kernel.org, martin.lau@linux.dev, song@kernel.org, kernel-team@meta.com, andrii@kernel.org Cc: sinquersw@gmail.com, kuifeng@meta.com, Kui-Feng Lee Subject: [PATCH bpf-next v4 4/9] bpf: create repeated fields for arrays. Date: Tue, 7 May 2024 23:32:13 -0700 Message-Id: <20240508063218.2806447-5-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240508063218.2806447-1-thinker.li@gmail.com> References: <20240508063218.2806447-1-thinker.li@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 The verifier uses field information for certain special types, such as kptr, rbtree root, and list head. These types are treated differently. However, we did not previously support these types in arrays. This update examines arrays and duplicates field information the same number of times as the length of the array if the element type is one of the special types. Signed-off-by: Kui-Feng Lee Acked-by: Eduard Zingerman --- kernel/bpf/btf.c | 60 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 56 insertions(+), 4 deletions(-) diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index 2ce61c3a7e28..633c3e037cef 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -3494,6 +3494,41 @@ static int btf_get_field_type(const char *name, u32 field_mask, u32 *seen_mask, #undef field_mask_test_name +/* Repeat a field for a specified number of times. + * + * Copy and repeat the first field for repeat_cnt + * times. The field is repeated by adding the offset of each field + * with + * (i + 1) * elem_size + * where i is the repeat index and elem_size is the size of an element. + */ +static int btf_repeat_field(struct btf_field_info *info, + u32 repeat_cnt, u32 elem_size) +{ + u32 i; + u32 cur; + + /* Ensure not repeating fields that should not be repeated. */ + switch (info[0].type) { + case BPF_KPTR_UNREF: + case BPF_KPTR_REF: + case BPF_KPTR_PERCPU: + case BPF_LIST_HEAD: + case BPF_RB_ROOT: + break; + default: + return -EINVAL; + } + + cur = 1; + for (i = 0; i < repeat_cnt; i++) { + memcpy(&info[cur], &info[0], sizeof(info[0])); + info[cur++].off += (i + 1) * elem_size; + } + + return 0; +} + static int btf_find_field_one(const struct btf *btf, const struct btf_type *var, const struct btf_type *var_type, @@ -3504,6 +3539,19 @@ static int btf_find_field_one(const struct btf *btf, { int ret, align, sz, field_type; struct btf_field_info tmp; + const struct btf_array *array; + u32 i, nelems = 1; + + /* Walk into array types to find the element type and the number of + * elements in the (flattened) array. + */ + for (i = 0; i < MAX_RESOLVE_DEPTH && btf_type_is_array(var_type); i++) { + array = btf_array(var_type); + nelems *= array->nelems; + var_type = btf_type_by_id(btf, array->type); + } + if (nelems == 0) + return 0; field_type = btf_get_field_type(__btf_name_by_offset(btf, var_type->name_off), field_mask, seen_mask, &align, &sz); @@ -3512,7 +3560,7 @@ static int btf_find_field_one(const struct btf *btf, if (field_type < 0) return field_type; - if (expected_size && expected_size != sz) + if (expected_size && expected_size != sz * nelems) return 0; if (off % align) return 0; @@ -3552,10 +3600,14 @@ static int btf_find_field_one(const struct btf *btf, if (ret == BTF_FIELD_IGNORE) return 0; - if (!info_cnt) + if (nelems > info_cnt) return -E2BIG; - - return 1; + if (nelems > 1) { + ret = btf_repeat_field(info, nelems - 1, sz); + if (ret < 0) + return ret; + } + return nelems; } static int btf_find_struct_field(const struct btf *btf, From patchwork Wed May 8 06:32:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kui-Feng Lee X-Patchwork-Id: 13658116 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-oi1-f177.google.com (mail-oi1-f177.google.com [209.85.167.177]) (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 8467D1DA53 for ; Wed, 8 May 2024 06:32:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715149952; cv=none; b=bR4h7fcKq6mBzjdYuJTamHY+6GvFyvK/IROlxUaI4FioZXOIo83JYtK6b5Fe7Kxb8rFw6Kj4aKUfOnclKPW7SS2UAbsvvCTrcnOViF2a70oUAivsJEVA0dePks3AjS65Yv+a0gBGWzBH02HIB7AglDg0VqDvQuKN1/ZNZXlusfc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715149952; c=relaxed/simple; bh=IukWxncW71pOWKuXy6XwK0xo/mkiKGE5IHNqY5ILuKg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=em/0j+F5Pemzeqdffqjdi+YcT/wFBLsW2V2wNorWHL2xG50e4NgnVd3jFQyJY2WRDo+iuJ4fTfV7CHi9jzVBJ4pBJFvlp7oMCWC+jaCBhSbyCdl54LBIDFS16uxsj+bmnb2xmgPFR5OSteHUErw42lQj8qbSStCsPslVe0uQ6qU= 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=Xf8enLOj; arc=none smtp.client-ip=209.85.167.177 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="Xf8enLOj" Received: by mail-oi1-f177.google.com with SMTP id 5614622812f47-3c96d8bff27so228609b6e.0 for ; Tue, 07 May 2024 23:32:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1715149949; x=1715754749; 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=qMHj8IuUdPM0tOmYXvWLbeITQEfrgALpkMswxvMZiwc=; b=Xf8enLOjH4a/2pihQTJDP4fe2YryexPIJSQXB9+tyI4vAeDAOOKoJHHrCroagwgvy9 g0NO9SMRp1xHaYVeh3DPTizOnU7e7xS172P4N22XfS8diFZchK3B8v++PulHddUnoxfn Cz2stn2NP5CApblupccQcLyfrihIz67kJrSHO+2z1x3StFfbLFwP4vgJ00LIAPcdgkgn pLOAHufsorgOY/+p7tYpgoslgzmfU9JvyP//qw8YyKqtC4dsHcgwlyxM6vqz+jRn/CWy vH0OMwNMz3kz5XeTJCxRqbsAEOSrzTQndVNzrShjOuLG2FPehovOg4njuLYMLhuLmazE 7C8g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715149949; x=1715754749; 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=qMHj8IuUdPM0tOmYXvWLbeITQEfrgALpkMswxvMZiwc=; b=b9T6Q4wbSleXDR0eOaVMxdjxza2RIFhOgCFapy85NXbmuEYlfIn/uy4lHbGqzlwhaH IkxoZ/5RXwCz3JDgUf4+EpXmPJZ+lLhLLP4wXOIOfjw5IpP1LvPzvKCue7m8OEJep41m AbDv8Woe49dlqv0Dt3lxm/22xj+45nST+iqHm8dZE+61mNUbW2fEKUwFcXHxQYXhD8uj 7Z/nLgfWoROpyc+2F6xkvcktP6KV9qNK+5H18eGYuHyIWIvHg3setwr89sFZb/fXBFRq W/fBKzOpiO7S6Ba+dOBFFfF+7oz2eMIzKdnHuC7yjcPMlj/Dx7UCTkJMaFqL9tQVBG0n RS0w== X-Gm-Message-State: AOJu0YzKxU66hjlm0KeEVBOTCx7hql1Ja5cm8wycpGEpqncDcMkoZRbE ofZFF+axtZ0zKBUrO5FY+sHMHSNgQR+/rJdPyYJDhusMaSWy4QW6kozM2A== X-Google-Smtp-Source: AGHT+IFF+nh9MHmVqxyX6v9J1xkNli6ZnPfROimbxUVoPTmtVObywp/5f2EYYDx/mISGYvsCS8sRhA== X-Received: by 2002:a05:6808:2988:b0:3c9:691b:dcee with SMTP id 5614622812f47-3c984b6a9aamr808328b6e.7.1715149949105; Tue, 07 May 2024 23:32:29 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:28e:823a:cbf2:fea6]) by smtp.gmail.com with ESMTPSA id z22-20020a056808029600b003c9729ac86dsm841371oic.11.2024.05.07.23.32.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 May 2024 23:32:28 -0700 (PDT) From: Kui-Feng Lee To: bpf@vger.kernel.org, ast@kernel.org, martin.lau@linux.dev, song@kernel.org, kernel-team@meta.com, andrii@kernel.org Cc: sinquersw@gmail.com, kuifeng@meta.com, Kui-Feng Lee Subject: [PATCH bpf-next v4 5/9] bpf: look into the types of the fields of a struct type recursively. Date: Tue, 7 May 2024 23:32:14 -0700 Message-Id: <20240508063218.2806447-6-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240508063218.2806447-1-thinker.li@gmail.com> References: <20240508063218.2806447-1-thinker.li@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 The verifier has field information for specific special types, such as kptr, rbtree root, and list head. These types are handled differently. However, we did not previously examine the types of fields of a struct type variable. Field information records were not generated for the kptrs, rbtree roots, and linked_list heads that are not located at the outermost struct type of a variable. For example, struct A { struct task_struct __kptr * task; }; struct B { struct A mem_a; } struct B var_b; It did not examine "struct A" so as not to generate field information for the kptr in "struct A" for "var_b". This patch enables BPF programs to define fields of these special types in a struct type other than the direct type of a variable or in a struct type that is the type of a field in the value type of a map. Signed-off-by: Kui-Feng Lee Acked-by: Eduard Zingerman --- kernel/bpf/btf.c | 93 +++++++++++++++++++++++++++++++++++++----------- 1 file changed, 73 insertions(+), 20 deletions(-) diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index 633c3e037cef..bbda24299be2 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -3494,41 +3494,83 @@ static int btf_get_field_type(const char *name, u32 field_mask, u32 *seen_mask, #undef field_mask_test_name -/* Repeat a field for a specified number of times. +/* Repeat a number of fields for a specified number of times. * - * Copy and repeat the first field for repeat_cnt - * times. The field is repeated by adding the offset of each field - * with + * Copy the fields starting from the first field and repeat them for + * repeat_cnt times. The fields are repeated by adding the offset of each + * field with * (i + 1) * elem_size * where i is the repeat index and elem_size is the size of an element. */ -static int btf_repeat_field(struct btf_field_info *info, - u32 repeat_cnt, u32 elem_size) +static int btf_repeat_fields(struct btf_field_info *info, + u32 field_cnt, u32 repeat_cnt, u32 elem_size) { - u32 i; + u32 i, j; u32 cur; /* Ensure not repeating fields that should not be repeated. */ - switch (info[0].type) { - case BPF_KPTR_UNREF: - case BPF_KPTR_REF: - case BPF_KPTR_PERCPU: - case BPF_LIST_HEAD: - case BPF_RB_ROOT: - break; - default: - return -EINVAL; + for (i = 0; i < field_cnt; i++) { + switch (info[i].type) { + case BPF_KPTR_UNREF: + case BPF_KPTR_REF: + case BPF_KPTR_PERCPU: + case BPF_LIST_HEAD: + case BPF_RB_ROOT: + break; + default: + return -EINVAL; + } } - cur = 1; + cur = field_cnt; for (i = 0; i < repeat_cnt; i++) { - memcpy(&info[cur], &info[0], sizeof(info[0])); - info[cur++].off += (i + 1) * elem_size; + memcpy(&info[cur], &info[0], field_cnt * sizeof(info[0])); + for (j = 0; j < field_cnt; j++) + info[cur++].off += (i + 1) * elem_size; } return 0; } +static int btf_find_struct_field(const struct btf *btf, + const struct btf_type *t, u32 field_mask, + struct btf_field_info *info, int info_cnt); + +/* Find special fields in the struct type of a field. + * + * This function is used to find fields of special types that is not a + * global variable or a direct field of a struct type. It also handles the + * repetition if it is the element type of an array. + */ +static int btf_find_nested_struct(const struct btf *btf, const struct btf_type *t, + u32 off, u32 nelems, + u32 field_mask, struct btf_field_info *info, + int info_cnt) +{ + int ret, err, i; + + ret = btf_find_struct_field(btf, t, field_mask, info, info_cnt); + + if (ret <= 0) + return ret; + + /* Shift the offsets of the nested struct fields to the offsets + * related to the container. + */ + for (i = 0; i < ret; i++) + info[i].off += off; + + if (nelems > 1) { + err = btf_repeat_fields(info, ret, nelems - 1, t->size); + if (err == 0) + ret *= nelems; + else + ret = err; + } + + return ret; +} + static int btf_find_field_one(const struct btf *btf, const struct btf_type *var, const struct btf_type *var_type, @@ -3555,6 +3597,17 @@ static int btf_find_field_one(const struct btf *btf, field_type = btf_get_field_type(__btf_name_by_offset(btf, var_type->name_off), field_mask, seen_mask, &align, &sz); + /* Look into variables of struct types */ + if ((field_type == BPF_KPTR_REF || !field_type) && + __btf_type_is_struct(var_type)) { + sz = var_type->size; + if (expected_size && expected_size != sz * nelems) + return 0; + ret = btf_find_nested_struct(btf, var_type, off, nelems, field_mask, + &info[0], info_cnt); + return ret; + } + if (field_type == 0) return 0; if (field_type < 0) @@ -3603,7 +3656,7 @@ static int btf_find_field_one(const struct btf *btf, if (nelems > info_cnt) return -E2BIG; if (nelems > 1) { - ret = btf_repeat_field(info, nelems - 1, sz); + ret = btf_repeat_fields(info, 1, nelems - 1, sz); if (ret < 0) return ret; } From patchwork Wed May 8 06:32:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kui-Feng Lee X-Patchwork-Id: 13658117 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-oi1-f181.google.com (mail-oi1-f181.google.com [209.85.167.181]) (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 7C16C1E53F for ; Wed, 8 May 2024 06:32:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715149953; cv=none; b=FsVY4cmwhv4EmREUwCjYC1fl3wL0y9JuOdOwLPiknqAUBdZeULAAAadcQvauIK3k5+wDfi1HAEHKh6aeYi+QoOPlyHAJezI/evegzTe465GAA03RJ6p0/gf93d9Hr7m+qASPmNHGAWVuCykCR0MV1Ome1EqSUPBp8hJ2rJsKBJE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715149953; c=relaxed/simple; bh=LpM0WjX+tDainc6yVUwiMyxOVwf2I1EDoa4gC6TSX28=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=SZiZRsmNRmkOmMLJ8lnkTwajUjVVfR2JmEKYXxYmDe4OQbN0+SSm2anPhCpekk53bHuVh/L7LIUBeRlRvXIBBkpjuBh9jhvD48CCw+miSfEAVLUi+y2kjhANX7UG+d0prfggPyiCSSg9KV7FuFjofYYiRTro1XQxEl59vn+MuAg= 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=MIo3tnVf; arc=none smtp.client-ip=209.85.167.181 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="MIo3tnVf" Received: by mail-oi1-f181.google.com with SMTP id 5614622812f47-3c963880aecso1535707b6e.3 for ; Tue, 07 May 2024 23:32:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1715149950; x=1715754750; 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=eGCAxMWCfi60a3tw+PnorEYjKcG5uTIVemeKayT5RIo=; b=MIo3tnVfOlhrpefraBDN5MhD8H8xTTFBACHFrKxS0RZaXbLonciML5e5LU2o5OCSY3 4lk+HzZnNHqCW+KH04rqJUd4ktH44i9M1qXixekITxcGAM/buQrcKviSp6G4sMujpHrm EQJybScQXfdf2l9uLXhbky21dFhPWWotWKbPtanRKVpicKTJQsUDs5DeZe3N/GrwTjzt dgQEKo1RVwSaeUl8hta8ws9TqRP9Rj2Dv6vQimlonT3Pe0eLfKeYLuc4S7asae/zwV9M 6zMWOpSrXFhKLDKunJaeN/2Z42mRwMbAB/3rh8yf96VbqooYu9aBbHTGJb3py+kTavah WmrA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715149950; x=1715754750; 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=eGCAxMWCfi60a3tw+PnorEYjKcG5uTIVemeKayT5RIo=; b=lnZZya1kJ+ZlKRr0JSGnJMrFKrbKdPHBO3952aRjVssh4gKommp3MUPYYVexZZou/t C+WIWaj61mBkvB3oXnYDVlzo6KQRKUF1o68ixuvy1iCc5iDgRjQVrofU3q2WxKYXuNwX r3YyZ/BvMFl+ADDn8yF7SmEenTpzxAQTbDPMPJQs6p+scMLaDmXBi4iUdVmwL6vuAUo9 EVxOva95NTMJVYdWeNW9Y6Ky8qwq+SsfCldJSgIErAD5R9PjYeJaywUn3aU9Ki7lufEn CjfhvCUx3NcN3v0ehuDmh48rXQ77R7LQkprjSRA3wolYNRsNvGsthfvP71cAYKy5Cklp J+Ag== X-Gm-Message-State: AOJu0YyPEvknaboWHxsY09NUqebLr161DZ7YbbK8hJ2x+3UYILKo6bNY rJ2zyEBhnzR9/1PQrlif8ftQMiiwnjdpWSmS5lAtIs7Jh4RS4seWXNRqtg== X-Google-Smtp-Source: AGHT+IGBMZRWdnyXIaNGMMcC/AieXODTPoY91Wqfr8ro436ML/0njKraxI09E7LPCQJTXxdVT59gJA== X-Received: by 2002:a05:6808:1a1c:b0:3c9:6ba0:e98c with SMTP id 5614622812f47-3c98532a51fmr2056412b6e.52.1715149950384; Tue, 07 May 2024 23:32:30 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:28e:823a:cbf2:fea6]) by smtp.gmail.com with ESMTPSA id z22-20020a056808029600b003c9729ac86dsm841371oic.11.2024.05.07.23.32.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 May 2024 23:32:29 -0700 (PDT) From: Kui-Feng Lee To: bpf@vger.kernel.org, ast@kernel.org, martin.lau@linux.dev, song@kernel.org, kernel-team@meta.com, andrii@kernel.org Cc: sinquersw@gmail.com, kuifeng@meta.com, Kui-Feng Lee Subject: [PATCH bpf-next v4 6/9] bpf: limit the number of levels of a nested struct type. Date: Tue, 7 May 2024 23:32:15 -0700 Message-Id: <20240508063218.2806447-7-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240508063218.2806447-1-thinker.li@gmail.com> References: <20240508063218.2806447-1-thinker.li@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 Limit the number of levels looking into struct types to avoid running out of stack space. Signed-off-by: Kui-Feng Lee --- kernel/bpf/btf.c | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index bbda24299be2..b4d6a6959b21 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -3534,7 +3534,8 @@ static int btf_repeat_fields(struct btf_field_info *info, static int btf_find_struct_field(const struct btf *btf, const struct btf_type *t, u32 field_mask, - struct btf_field_info *info, int info_cnt); + struct btf_field_info *info, int info_cnt, + u32 level); /* Find special fields in the struct type of a field. * @@ -3545,11 +3546,15 @@ static int btf_find_struct_field(const struct btf *btf, static int btf_find_nested_struct(const struct btf *btf, const struct btf_type *t, u32 off, u32 nelems, u32 field_mask, struct btf_field_info *info, - int info_cnt) + int info_cnt, u32 level) { int ret, err, i; - ret = btf_find_struct_field(btf, t, field_mask, info, info_cnt); + level++; + if (level >= MAX_RESOLVE_DEPTH) + return -E2BIG; + + ret = btf_find_struct_field(btf, t, field_mask, info, info_cnt, level); if (ret <= 0) return ret; @@ -3577,7 +3582,8 @@ static int btf_find_field_one(const struct btf *btf, int var_idx, u32 off, u32 expected_size, u32 field_mask, u32 *seen_mask, - struct btf_field_info *info, int info_cnt) + struct btf_field_info *info, int info_cnt, + u32 level) { int ret, align, sz, field_type; struct btf_field_info tmp; @@ -3604,7 +3610,7 @@ static int btf_find_field_one(const struct btf *btf, if (expected_size && expected_size != sz * nelems) return 0; ret = btf_find_nested_struct(btf, var_type, off, nelems, field_mask, - &info[0], info_cnt); + &info[0], info_cnt, level); return ret; } @@ -3665,7 +3671,8 @@ static int btf_find_field_one(const struct btf *btf, static int btf_find_struct_field(const struct btf *btf, const struct btf_type *t, u32 field_mask, - struct btf_field_info *info, int info_cnt) + struct btf_field_info *info, int info_cnt, + u32 level) { int ret, idx = 0; const struct btf_member *member; @@ -3684,7 +3691,7 @@ static int btf_find_struct_field(const struct btf *btf, ret = btf_find_field_one(btf, t, member_type, i, off, 0, field_mask, &seen_mask, - &info[idx], info_cnt - idx); + &info[idx], info_cnt - idx, level); if (ret < 0) return ret; idx += ret; @@ -3694,7 +3701,7 @@ static int btf_find_struct_field(const struct btf *btf, static int btf_find_datasec_var(const struct btf *btf, const struct btf_type *t, u32 field_mask, struct btf_field_info *info, - int info_cnt) + int info_cnt, u32 level) { int ret, idx = 0; const struct btf_var_secinfo *vsi; @@ -3707,7 +3714,8 @@ static int btf_find_datasec_var(const struct btf *btf, const struct btf_type *t, off = vsi->offset; ret = btf_find_field_one(btf, var, var_type, -1, off, vsi->size, field_mask, &seen_mask, - &info[idx], info_cnt - idx); + &info[idx], info_cnt - idx, + level); if (ret < 0) return ret; idx += ret; @@ -3720,9 +3728,9 @@ static int btf_find_field(const struct btf *btf, const struct btf_type *t, int info_cnt) { if (__btf_type_is_struct(t)) - return btf_find_struct_field(btf, t, field_mask, info, info_cnt); + return btf_find_struct_field(btf, t, field_mask, info, info_cnt, 0); else if (btf_type_is_datasec(t)) - return btf_find_datasec_var(btf, t, field_mask, info, info_cnt); + return btf_find_datasec_var(btf, t, field_mask, info, info_cnt, 0); return -EINVAL; } From patchwork Wed May 8 06:32:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kui-Feng Lee X-Patchwork-Id: 13658118 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-oi1-f178.google.com (mail-oi1-f178.google.com [209.85.167.178]) (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 AFCAF2263E for ; Wed, 8 May 2024 06:32:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715149954; cv=none; b=AKpnT/iJ2RdQolu0/79GbS8TAHWAPRqib5BnS8Y/1ypXx2THo9VjNo9SQRwUP6URR+2tZ+nDIHMqvOSd8FDhqyCC/sg/86duZtr7TPq0BSco/8Qga6yMkrnik3KjywIWM36FXD01lQBjDjp0aXvVZZ+XJSDyPxJ0Jbks9IjV4Aw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715149954; c=relaxed/simple; bh=Ma3Vu2MlOrX3+7gHJ0smej7Cz6EfF40UjlbMuQF7iyA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=YPQ8iHOru9zHpO8hhnVXZwhcFb39O6D0nVxn7JLQfQY3BmH+1ECqsFcFpS6XHK2/8ZsoBmTp0Z4BTEwapRNC2ih2suuJXKryDGtzmdypxtTuhneDxKsTGbSVxk8PS7VkhrP0uU2C5Kffyh3F8psuaYpD3DwjXYOfrnDK/4boJAU= 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=fWzrCIqZ; arc=none smtp.client-ip=209.85.167.178 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="fWzrCIqZ" Received: by mail-oi1-f178.google.com with SMTP id 5614622812f47-3c96a556006so1776572b6e.0 for ; Tue, 07 May 2024 23:32:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1715149951; x=1715754751; 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=WEkoRMBGu2GwDIk9DvRSHZhuDVYyTKX9czbFcw2JQz8=; b=fWzrCIqZif9U8wamrp2xPXEu/rrZV+rtlvg5e0DfHLtzmqSOqLahADmFtdqarcJa8N nWiBiEfH1eoyiJzFJCmVlCXRFcc54sbxkRGpd1/I0nx5C/i26ml7SvE15r6tsoqqQvNX GA+qHB7XOkxjOcMw6QWlAQCuBg5ykkU1M+sW5P4yM/CVxndlNU9PjU9OlP/arefiS4ki ZZnxHyY4Rv7bDIx66Sjo5S7BKwIdV4wxTTMCpeblaFOxok01Nx7r9kct4AS9/CyDUGXh dizU1zLJi2uRpxjxvbwuUWWXatC6MxDXwX3lL0ehU7b79GUpVw1EicU5gKQY8jCJ3a0w vgSg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715149951; x=1715754751; 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=WEkoRMBGu2GwDIk9DvRSHZhuDVYyTKX9czbFcw2JQz8=; b=BNiJveDQ553R0PSdhJVgeM1XPkQ58lVfRv1ULdPqy9ogkajlsyHUMXmWs8oDTCSXM9 AmvHtb99rnjYaJ8tELVzUboR+wRJMCml4z1WsuD+pBq5UBF93Y2f5lFTOFUfhpsB7jH8 gmQ1M99l8gYL8BSCu2fPXSNNUQbqRtF0iVicjR59vPnsj0WljE9zJl4cfuHyFaBy9fZx 65s/SrKfdZWz4VPznIm4l/GMhu0NTdGAjrcC68pGITiU99K9MjOmGRvJoIh9KftWqh53 +fAQDimE9vvzRoFxdLie+bTxYf5RYIaL/Ph578K5CXEjFQEDz5A394cG1tcmIiuP/f7A dPQw== X-Gm-Message-State: AOJu0Yw34UdiIrjMc0FFQqGB0Ft6eCfcbht2NlJUAKJsC5Fu+USOo3UP aLnz3jxFEGRy9J/PXeociTW8V6DfnedTDB+Ybaw+jKcUwys2FlTI9EHQVw== X-Google-Smtp-Source: AGHT+IGEJ82LSRWlA/x8CLxWXmIVpaFO1IO1TsScpC04a+CNSDKnlQeSc5rsxFXwUwyaoISZuySy2Q== X-Received: by 2002:aca:1e17:0:b0:3c8:575f:4135 with SMTP id 5614622812f47-3c9852977a1mr1868417b6e.7.1715149951681; Tue, 07 May 2024 23:32:31 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:28e:823a:cbf2:fea6]) by smtp.gmail.com with ESMTPSA id z22-20020a056808029600b003c9729ac86dsm841371oic.11.2024.05.07.23.32.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 May 2024 23:32:31 -0700 (PDT) From: Kui-Feng Lee To: bpf@vger.kernel.org, ast@kernel.org, martin.lau@linux.dev, song@kernel.org, kernel-team@meta.com, andrii@kernel.org Cc: sinquersw@gmail.com, kuifeng@meta.com, Kui-Feng Lee Subject: [PATCH bpf-next v4 7/9] selftests/bpf: Test kptr arrays and kptrs in nested struct fields. Date: Tue, 7 May 2024 23:32:16 -0700 Message-Id: <20240508063218.2806447-8-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240508063218.2806447-1-thinker.li@gmail.com> References: <20240508063218.2806447-1-thinker.li@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 Make sure that BPF programs can declare global kptr arrays and kptr fields in struct types that is the type of a global variable or the type of a nested descendant field in a global variable. An array with only one element is special case, that it treats the element like a non-array kptr field. Nested arrays are also tested to ensure they are handled properly. Signed-off-by: Kui-Feng Lee --- .../selftests/bpf/prog_tests/cpumask.c | 5 + .../selftests/bpf/progs/cpumask_success.c | 133 ++++++++++++++++++ 2 files changed, 138 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/cpumask.c b/tools/testing/selftests/bpf/prog_tests/cpumask.c index ecf89df78109..2570bd4b0cb2 100644 --- a/tools/testing/selftests/bpf/prog_tests/cpumask.c +++ b/tools/testing/selftests/bpf/prog_tests/cpumask.c @@ -18,6 +18,11 @@ static const char * const cpumask_success_testcases[] = { "test_insert_leave", "test_insert_remove_release", "test_global_mask_rcu", + "test_global_mask_array_one_rcu", + "test_global_mask_array_rcu", + "test_global_mask_array_l2_rcu", + "test_global_mask_nested_rcu", + "test_global_mask_nested_deep_rcu", "test_cpumask_weight", }; diff --git a/tools/testing/selftests/bpf/progs/cpumask_success.c b/tools/testing/selftests/bpf/progs/cpumask_success.c index 7a1e64c6c065..0b6383fa9958 100644 --- a/tools/testing/selftests/bpf/progs/cpumask_success.c +++ b/tools/testing/selftests/bpf/progs/cpumask_success.c @@ -12,6 +12,25 @@ char _license[] SEC("license") = "GPL"; int pid, nr_cpus; +struct kptr_nested { + struct bpf_cpumask __kptr * mask; +}; + +struct kptr_nested_mid { + int dummy; + struct kptr_nested m; +}; + +struct kptr_nested_deep { + struct kptr_nested_mid ptrs[2]; +}; + +private(MASK) static struct bpf_cpumask __kptr * global_mask_array[2]; +private(MASK) static struct bpf_cpumask __kptr * global_mask_array_l2[2][1]; +private(MASK) static struct bpf_cpumask __kptr * global_mask_array_one[1]; +private(MASK) static struct kptr_nested global_mask_nested[2]; +private(MASK) static struct kptr_nested_deep global_mask_nested_deep; + static bool is_test_task(void) { int cur_pid = bpf_get_current_pid_tgid() >> 32; @@ -460,6 +479,120 @@ int BPF_PROG(test_global_mask_rcu, struct task_struct *task, u64 clone_flags) return 0; } +SEC("tp_btf/task_newtask") +int BPF_PROG(test_global_mask_array_one_rcu, struct task_struct *task, u64 clone_flags) +{ + struct bpf_cpumask *local, *prev; + + if (!is_test_task()) + return 0; + + /* Kptr arrays with one element are special cased, being treated + * just like a single pointer. + */ + + local = create_cpumask(); + if (!local) + return 0; + + prev = bpf_kptr_xchg(&global_mask_array_one[0], local); + if (prev) { + bpf_cpumask_release(prev); + err = 3; + return 0; + } + + bpf_rcu_read_lock(); + local = global_mask_array_one[0]; + if (!local) { + err = 4; + bpf_rcu_read_unlock(); + return 0; + } + + bpf_rcu_read_unlock(); + + return 0; +} + +static int _global_mask_array_rcu(struct bpf_cpumask **mask0, + struct bpf_cpumask **mask1) +{ + struct bpf_cpumask *local; + + if (!is_test_task()) + return 0; + + /* Check if two kptrs in the array work and independently */ + + local = create_cpumask(); + if (!local) + return 0; + + bpf_rcu_read_lock(); + + local = bpf_kptr_xchg(mask0, local); + if (local) { + err = 1; + goto err_exit; + } + + /* [, NULL] */ + if (!*mask0 || *mask1) { + err = 2; + goto err_exit; + } + + local = create_cpumask(); + if (!local) { + err = 9; + goto err_exit; + } + + local = bpf_kptr_xchg(mask1, local); + if (local) { + err = 10; + goto err_exit; + } + + /* [, ] */ + if (!*mask0 || !*mask1 || *mask0 == *mask1) { + err = 11; + goto err_exit; + } + +err_exit: + if (local) + bpf_cpumask_release(local); + bpf_rcu_read_unlock(); + return 0; +} + +SEC("tp_btf/task_newtask") +int BPF_PROG(test_global_mask_array_rcu, struct task_struct *task, u64 clone_flags) +{ + return _global_mask_array_rcu(&global_mask_array[0], &global_mask_array[1]); +} + +SEC("tp_btf/task_newtask") +int BPF_PROG(test_global_mask_array_l2_rcu, struct task_struct *task, u64 clone_flags) +{ + return _global_mask_array_rcu(&global_mask_array_l2[0][0], &global_mask_array_l2[1][0]); +} + +SEC("tp_btf/task_newtask") +int BPF_PROG(test_global_mask_nested_rcu, struct task_struct *task, u64 clone_flags) +{ + return _global_mask_array_rcu(&global_mask_nested[0].mask, &global_mask_nested[1].mask); +} + +SEC("tp_btf/task_newtask") +int BPF_PROG(test_global_mask_nested_deep_rcu, struct task_struct *task, u64 clone_flags) +{ + return _global_mask_array_rcu(&global_mask_nested_deep.ptrs[0].m.mask, + &global_mask_nested_deep.ptrs[1].m.mask); +} + SEC("tp_btf/task_newtask") int BPF_PROG(test_cpumask_weight, struct task_struct *task, u64 clone_flags) { From patchwork Wed May 8 06:32:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kui-Feng Lee X-Patchwork-Id: 13658119 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-oi1-f172.google.com (mail-oi1-f172.google.com [209.85.167.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 EB798846B for ; Wed, 8 May 2024 06:32:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715149955; cv=none; b=Oq1/cy3reK8Kkuil5fPDrygOZjwdMeBn6hCrFvUywdSjYTwHYVlIKljoD79V07Af/4Ze2ozfBIik4Kgvs8LkVu7cUNNZo2sgwgS7WqfNER6YrjxhVsil2ep3ZuH3so07E0VapKar9/cBodxP1JYO27gteSkLU7XeJQQzLUAtH6w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715149955; c=relaxed/simple; bh=184HSHS8K1pDQhxH155teo7Hn+PjBBhq6NMvMs9ANKc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=tgYgBZsoM/Yce+y1FmbBUDkRDOocEGrqlK6j1kGz+bMjh3eWQ2pg4UZuZLxj8AY7jOpbKWZE9mQqwK/07dRruO9NYn6SSxyYIJev3B8MuzUq8lJbKeF3eQnc/yQajo0IoabWG/rKGzc/6RxLOAG1kNG3MGmwFMP6IIjVr5It9Bs= 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=eGAarL3+; arc=none smtp.client-ip=209.85.167.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="eGAarL3+" Received: by mail-oi1-f172.google.com with SMTP id 5614622812f47-3c70d8bb618so2010186b6e.2 for ; Tue, 07 May 2024 23:32:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1715149953; x=1715754753; 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=EWwzY90xebc6YceOSeyo6ErhBa/XYlP1DhvcruTgZXw=; b=eGAarL3+mq/fBTGvohXjo1rZP37BCfr8syPIZyh3Aeam9CRIoE8ztrmDGlzmIncaqI glxSXsE0+zX4eeK1eR/Br1OLVp6XRvzuKTTnVabhfXufMZNI5HSwvylWvEAMPnGbaYLV bgbnfXeFunCbegupW+O2djJvMGcjYDk8pfitG60BekbS7ehirikNX/KnWQ7xRl20dkuf lJvv9pKgrTS7kxNb2ok9zPmJmxDkafRzllZuswimDcE24TZB6PK6JNlZykfKB0Wy7iDv p4Qz4wP+sfjkLe6M07W6sSqfSWNjMC7B9VbTcMKK4824UHWSOm6UA+QKVoh0lyPuE0TS bL0A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715149953; x=1715754753; 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=EWwzY90xebc6YceOSeyo6ErhBa/XYlP1DhvcruTgZXw=; b=jIfJx40GwrfueAHst1UUTz0joy8/6+pI0A93wGlQG5ckrnScQvuiml3W+RTD/FXx6U qbs0qs6ipJFCH20/yeRzcZWprgYsS5VWCjQLGPLzl/HKmdk6gAgFESqVEbOJZLv+fI6d ANRQHoowxDW2+jxFdMlU0wniMAYgxmU/MVYjbjJD7d16/z8ll++SKa0xt4Pxrcnyv2fy K11spssWHqxYX6nfHP2wfqFgDqIKr7mF2kJIOMfeOvHGgtH3vDytYV5kBcxbTwxrwum8 8rRduSsOhU0eLRr6OQrqUlO94JygM+dHdm/HR4hsablfKfoS69qnNPQ9ZIL91HLSitsv n6WA== X-Gm-Message-State: AOJu0Yx4o9UNki4PdijQFwh3lXSM6o82LZBaoYLHTLirFYNGwWrnA+vv yIcefSijdCf+98WQUwsa2r5k6jJ9fXptizrzeOFeQYtdfBlIRiWBM2W3iQ== X-Google-Smtp-Source: AGHT+IEqZGeAJG42DNrLfY70WxDB2l2QXn8waka6PjhOv7YXke/0n9ihHvq6qiEawoPT6Fa27zX5Kg== X-Received: by 2002:aca:1916:0:b0:3c8:69b6:9b78 with SMTP id 5614622812f47-3c9852cbe30mr2004580b6e.29.1715149952780; Tue, 07 May 2024 23:32:32 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:28e:823a:cbf2:fea6]) by smtp.gmail.com with ESMTPSA id z22-20020a056808029600b003c9729ac86dsm841371oic.11.2024.05.07.23.32.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 May 2024 23:32:32 -0700 (PDT) From: Kui-Feng Lee To: bpf@vger.kernel.org, ast@kernel.org, martin.lau@linux.dev, song@kernel.org, kernel-team@meta.com, andrii@kernel.org Cc: sinquersw@gmail.com, kuifeng@meta.com, Kui-Feng Lee Subject: [PATCH bpf-next v4 8/9] selftests/bpf: Test global bpf_rb_root arrays and fields in nested struct types. Date: Tue, 7 May 2024 23:32:17 -0700 Message-Id: <20240508063218.2806447-9-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240508063218.2806447-1-thinker.li@gmail.com> References: <20240508063218.2806447-1-thinker.li@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 Make sure global arrays of bpf_rb_root and fields of bpf_rb_root in nested struct types work correctly. Signed-off-by: Kui-Feng Lee --- .../testing/selftests/bpf/prog_tests/rbtree.c | 47 +++++++++++ tools/testing/selftests/bpf/progs/rbtree.c | 77 +++++++++++++++++++ 2 files changed, 124 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/rbtree.c b/tools/testing/selftests/bpf/prog_tests/rbtree.c index e9300c96607d..9818f06c97c5 100644 --- a/tools/testing/selftests/bpf/prog_tests/rbtree.c +++ b/tools/testing/selftests/bpf/prog_tests/rbtree.c @@ -31,6 +31,28 @@ static void test_rbtree_add_nodes(void) rbtree__destroy(skel); } +static void test_rbtree_add_nodes_nested(void) +{ + LIBBPF_OPTS(bpf_test_run_opts, opts, + .data_in = &pkt_v4, + .data_size_in = sizeof(pkt_v4), + .repeat = 1, + ); + struct rbtree *skel; + int ret; + + skel = rbtree__open_and_load(); + if (!ASSERT_OK_PTR(skel, "rbtree__open_and_load")) + return; + + ret = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.rbtree_add_nodes_nested), &opts); + ASSERT_OK(ret, "rbtree_add_nodes_nested run"); + ASSERT_OK(opts.retval, "rbtree_add_nodes_nested retval"); + ASSERT_EQ(skel->data->less_callback_ran, 1, "rbtree_add_nodes_nested less_callback_ran"); + + rbtree__destroy(skel); +} + static void test_rbtree_add_and_remove(void) { LIBBPF_OPTS(bpf_test_run_opts, opts, @@ -53,6 +75,27 @@ static void test_rbtree_add_and_remove(void) rbtree__destroy(skel); } +static void test_rbtree_add_and_remove_array(void) +{ + LIBBPF_OPTS(bpf_test_run_opts, opts, + .data_in = &pkt_v4, + .data_size_in = sizeof(pkt_v4), + .repeat = 1, + ); + struct rbtree *skel; + int ret; + + skel = rbtree__open_and_load(); + if (!ASSERT_OK_PTR(skel, "rbtree__open_and_load")) + return; + + ret = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.rbtree_add_and_remove_array), &opts); + ASSERT_OK(ret, "rbtree_add_and_remove_array"); + ASSERT_OK(opts.retval, "rbtree_add_and_remove_array retval"); + + rbtree__destroy(skel); +} + static void test_rbtree_first_and_remove(void) { LIBBPF_OPTS(bpf_test_run_opts, opts, @@ -104,8 +147,12 @@ void test_rbtree_success(void) { if (test__start_subtest("rbtree_add_nodes")) test_rbtree_add_nodes(); + if (test__start_subtest("rbtree_add_nodes_nested")) + test_rbtree_add_nodes_nested(); if (test__start_subtest("rbtree_add_and_remove")) test_rbtree_add_and_remove(); + if (test__start_subtest("rbtree_add_and_remove_array")) + test_rbtree_add_and_remove_array(); if (test__start_subtest("rbtree_first_and_remove")) test_rbtree_first_and_remove(); if (test__start_subtest("rbtree_api_release_aliasing")) diff --git a/tools/testing/selftests/bpf/progs/rbtree.c b/tools/testing/selftests/bpf/progs/rbtree.c index b09f4fffe57c..a3620c15c136 100644 --- a/tools/testing/selftests/bpf/progs/rbtree.c +++ b/tools/testing/selftests/bpf/progs/rbtree.c @@ -13,6 +13,15 @@ struct node_data { struct bpf_rb_node node; }; +struct root_nested_inner { + struct bpf_spin_lock glock; + struct bpf_rb_root root __contains(node_data, node); +}; + +struct root_nested { + struct root_nested_inner inner; +}; + long less_callback_ran = -1; long removed_key = -1; long first_data[2] = {-1, -1}; @@ -20,6 +29,9 @@ long first_data[2] = {-1, -1}; #define private(name) SEC(".data." #name) __hidden __attribute__((aligned(8))) private(A) struct bpf_spin_lock glock; private(A) struct bpf_rb_root groot __contains(node_data, node); +private(A) struct bpf_rb_root groot_array[2] __contains(node_data, node); +private(A) struct bpf_rb_root groot_array_one[1] __contains(node_data, node); +private(B) struct root_nested groot_nested; static bool less(struct bpf_rb_node *a, const struct bpf_rb_node *b) { @@ -71,6 +83,12 @@ long rbtree_add_nodes(void *ctx) return __add_three(&groot, &glock); } +SEC("tc") +long rbtree_add_nodes_nested(void *ctx) +{ + return __add_three(&groot_nested.inner.root, &groot_nested.inner.glock); +} + SEC("tc") long rbtree_add_and_remove(void *ctx) { @@ -109,6 +127,65 @@ long rbtree_add_and_remove(void *ctx) return 1; } +SEC("tc") +long rbtree_add_and_remove_array(void *ctx) +{ + struct bpf_rb_node *res1 = NULL, *res2 = NULL, *res3 = NULL; + struct node_data *nodes[3][2] = {{NULL, NULL}, {NULL, NULL}, {NULL, NULL}}; + struct node_data *n; + long k1 = -1, k2 = -1, k3 = -1; + int i, j; + + for (i = 0; i < 3; i++) { + for (j = 0; j < 2; j++) { + nodes[i][j] = bpf_obj_new(typeof(*nodes[i][j])); + if (!nodes[i][j]) + goto err_out; + nodes[i][j]->key = i * 2 + j; + } + } + + bpf_spin_lock(&glock); + for (i = 0; i < 2; i++) + for (j = 0; j < 2; j++) + bpf_rbtree_add(&groot_array[i], &nodes[i][j]->node, less); + for (j = 0; j < 2; j++) + bpf_rbtree_add(&groot_array_one[0], &nodes[2][j]->node, less); + res1 = bpf_rbtree_remove(&groot_array[0], &nodes[0][0]->node); + res2 = bpf_rbtree_remove(&groot_array[1], &nodes[1][0]->node); + res3 = bpf_rbtree_remove(&groot_array_one[0], &nodes[2][0]->node); + bpf_spin_unlock(&glock); + + if (res1) { + n = container_of(res1, struct node_data, node); + k1 = n->key; + bpf_obj_drop(n); + } + if (res2) { + n = container_of(res2, struct node_data, node); + k2 = n->key; + bpf_obj_drop(n); + } + if (res3) { + n = container_of(res3, struct node_data, node); + k3 = n->key; + bpf_obj_drop(n); + } + if (k1 != 0 || k2 != 2 || k3 != 4) + return 2; + + return 0; + +err_out: + for (i = 0; i < 3; i++) { + for (j = 0; j < 2; j++) { + if (nodes[i][j]) + bpf_obj_drop(nodes[i][j]); + } + } + return 1; +} + SEC("tc") long rbtree_first_and_remove(void *ctx) { From patchwork Wed May 8 06:32:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kui-Feng Lee X-Patchwork-Id: 13658120 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-oi1-f177.google.com (mail-oi1-f177.google.com [209.85.167.177]) (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 1A94D2263E for ; Wed, 8 May 2024 06:32:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715149956; cv=none; b=o+uSR6MS7gEDoVr8FzlouI68i2iCxLJQxRfwMxsN5SFEl/YVtmLJ4eyDdXFsrj27z0loKf9CfCtW/uoj0glpdiMpTBUAcfYMXPZ4we+58VVl4Ij2YWyUkh2INanBcD9CeGUUEje3/h10ork7Gh8+P1DTBGzyL2aKIrwkWn+Chhg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715149956; c=relaxed/simple; bh=T99aEuo7lB5fd7ns1ZsNRt0u99NUChuBc++AQ35NEN8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=BMl+1SSmKgwE4KyppNDSuGz0MUXx+gJsZ1+fr0l2EAZTGxZ2CVGNY/PUciPfG6L3QnsDOL41sYvl0aJv4LAhwJJenD/TxbU0RhjlDZPqZdjyKFBgEdjnu70emHE9U3E1IaFI1jEGpXcjP1J3GuSC9dh7pFBjfZUckceDA3hYURQ= 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=CR/Zu3Ma; arc=none smtp.client-ip=209.85.167.177 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="CR/Zu3Ma" Received: by mail-oi1-f177.google.com with SMTP id 5614622812f47-3c9539a7d70so203370b6e.0 for ; Tue, 07 May 2024 23:32:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1715149954; x=1715754754; 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=2cZSvfqk5wcnoVZE269hw105nH6/qA5LuJG4//0qzg8=; b=CR/Zu3Mabsj0K2qlkyrRdf1VGdGas3F4ij/gg59Pq2LBVPdRrUauAKdu70195qhom0 E9De/1YEIX97Cshz9zZkqDWJsAKXz87ZWE+fzoGVwtFrUw6s1MrZaH4Je/mrBmFfehAg wnPOnbiza9FuwXAvJXzF9EW2iVhFcwWCOx3BwCNxgUm/Y9O0hm9uRHU3P+VVEJhWQWvu dWlmnPRnKtYxXxhpd6GH6QwWuqhqU9ERzqMoVJqfH9d40yEeyXYZnsOsBPRW1OJM/hEl /UbHJiPjhjBtbZJlpc/w+/20fNNdAUSnK/pp7RGZK9oclPX5FeXYBaP04WrGtkbkxip+ r5Iw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715149954; x=1715754754; 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=2cZSvfqk5wcnoVZE269hw105nH6/qA5LuJG4//0qzg8=; b=n9BA2boTOZxXnoLsCeOy4viBE5mOYTwMfBtIUdMKt572qp0vZBw/iCJ5axYQSOzk1/ MbiYPE2kCUQw4hoonHkFRmxZ7s7sTbfNDNafnbkQ94v7fnGCVa9C4whXGdWJ0C796v/Q USX3t26z6b+wlrHfyN9449YABuaZr2pFgfXmnyri440heGZFzDDQ0KoIMuZy3EJCkpyq GocQmXmtvCBWqBThTOoYGkVntu39ZzvAl9xmCq/Z/mKnYG9CJSlQRhSLnynOvu2QseIc jzZniObeK+meJtXtLDUoURlcjJ0jYQAo3CXdMUO3VL5ZqbfX/TiWRohHAZzLoXyXCnlf /Lzg== X-Gm-Message-State: AOJu0Yzuo7/Y3zoHAVeozzZBBKmSn547+AK1M/nrCsxFFxy1aGiRi50u EhgFz+ZDj6La1HkjEPVMBeBkAdR4O1CJZJOVt0WLmEoTzQQGD0a5ntZPqA== X-Google-Smtp-Source: AGHT+IGasM8subQBEBfInGyLHbHY0lLFI9GqFYuYcl3j2VUN9u7JoaU2UleOJnQvHC4HIdGdxMGLcw== X-Received: by 2002:a05:6808:2746:b0:3c9:6a82:4ffc with SMTP id 5614622812f47-3c984b72431mr708507b6e.12.1715149953883; Tue, 07 May 2024 23:32:33 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:28e:823a:cbf2:fea6]) by smtp.gmail.com with ESMTPSA id z22-20020a056808029600b003c9729ac86dsm841371oic.11.2024.05.07.23.32.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 May 2024 23:32:33 -0700 (PDT) From: Kui-Feng Lee To: bpf@vger.kernel.org, ast@kernel.org, martin.lau@linux.dev, song@kernel.org, kernel-team@meta.com, andrii@kernel.org Cc: sinquersw@gmail.com, kuifeng@meta.com, Kui-Feng Lee Subject: [PATCH bpf-next v4 9/9] selftests/bpf: Test global bpf_list_head arrays. Date: Tue, 7 May 2024 23:32:18 -0700 Message-Id: <20240508063218.2806447-10-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240508063218.2806447-1-thinker.li@gmail.com> References: <20240508063218.2806447-1-thinker.li@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 Make sure global arrays of bpf_list_heads and fields of bpf_list_heads in nested struct types work correctly. Signed-off-by: Kui-Feng Lee --- .../selftests/bpf/prog_tests/linked_list.c | 12 ++++++ .../testing/selftests/bpf/progs/linked_list.c | 42 +++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/linked_list.c b/tools/testing/selftests/bpf/prog_tests/linked_list.c index 2fb89de63bd2..77d07e0a4a55 100644 --- a/tools/testing/selftests/bpf/prog_tests/linked_list.c +++ b/tools/testing/selftests/bpf/prog_tests/linked_list.c @@ -183,6 +183,18 @@ static void test_linked_list_success(int mode, bool leave_in_map) if (!leave_in_map) clear_fields(skel->maps.bss_A); + ret = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.global_list_push_pop_nested), &opts); + ASSERT_OK(ret, "global_list_push_pop_nested"); + ASSERT_OK(opts.retval, "global_list_push_pop_nested retval"); + if (!leave_in_map) + clear_fields(skel->maps.bss_A); + + ret = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.global_list_array_push_pop), &opts); + ASSERT_OK(ret, "global_list_array_push_pop"); + ASSERT_OK(opts.retval, "global_list_array_push_pop retval"); + if (!leave_in_map) + clear_fields(skel->maps.bss_A); + if (mode == PUSH_POP) goto end; diff --git a/tools/testing/selftests/bpf/progs/linked_list.c b/tools/testing/selftests/bpf/progs/linked_list.c index 26205ca80679..f69bf3e30321 100644 --- a/tools/testing/selftests/bpf/progs/linked_list.c +++ b/tools/testing/selftests/bpf/progs/linked_list.c @@ -11,6 +11,22 @@ #include "linked_list.h" +struct head_nested_inner { + struct bpf_spin_lock lock; + struct bpf_list_head head __contains(foo, node2); +}; + +struct head_nested { + int dummy; + struct head_nested_inner inner; +}; + +private(C) struct bpf_spin_lock glock_c; +private(C) struct bpf_list_head ghead_array[2] __contains(foo, node2); +private(C) struct bpf_list_head ghead_array_one[1] __contains(foo, node2); + +private(D) struct head_nested ghead_nested; + static __always_inline int list_push_pop(struct bpf_spin_lock *lock, struct bpf_list_head *head, bool leave_in_map) { @@ -309,6 +325,32 @@ int global_list_push_pop(void *ctx) return test_list_push_pop(&glock, &ghead); } +SEC("tc") +int global_list_push_pop_nested(void *ctx) +{ + return test_list_push_pop(&ghead_nested.inner.lock, &ghead_nested.inner.head); +} + +SEC("tc") +int global_list_array_push_pop(void *ctx) +{ + int r; + + r = test_list_push_pop(&glock_c, &ghead_array[0]); + if (r) + return r; + + r = test_list_push_pop(&glock_c, &ghead_array[1]); + if (r) + return r; + + /* Arrays with only one element is a special case, being treated + * just like a bpf_list_head variable by the verifier, not an + * array. + */ + return test_list_push_pop(&glock_c, &ghead_array_one[0]); +} + SEC("tc") int map_list_push_pop_multiple(void *ctx) {