From patchwork Fri Apr 12 21:08:04 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: 13628323 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-oa1-f42.google.com (mail-oa1-f42.google.com [209.85.160.42]) (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 26116153592 for ; Fri, 12 Apr 2024 21:08:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712956101; cv=none; b=pNxcxdWdhC9S5Iywv4Myiu+D2k11/O4DTrqRRtjtU9c74I20yzKcVV1yd8NIHZDiFoZ6q/UG0r4chV8PlzRgu28DXk53wbrxhukK3Q2pbTTRlotk45pXm2dyTEyltkugoG88wTGXnWhpbFwLDaVYm6uDUjRfzUT+EGAFWGgaMyM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712956101; c=relaxed/simple; bh=G+Jrm7+AOL2oUoXNz8kcAREXelsRshTV6bWcfElhI1U=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=qZy2emrFLm/Hpem7ejgdLXrxQ0gC8uM7h+uDSshOPle6hhsIqWWkZNQtbVIScCzuZGsoEBV+dKdY5rd4BqxoTeTaBr3i4lf5lGWlwkoydP3MbTZKkP8YJnTD2iNvL9mVVBHtO6UT88nKZmms0fwh1SelvI6PydSpmrhX5yR3Hz8= 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=PVcpX8qA; arc=none smtp.client-ip=209.85.160.42 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="PVcpX8qA" Received: by mail-oa1-f42.google.com with SMTP id 586e51a60fabf-233f389a61eso275421fac.3 for ; Fri, 12 Apr 2024 14:08:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712956099; x=1713560899; 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=3B8ubEPEvjOe4glCCPznp6yUG163aAy+zIl8t3hudPM=; b=PVcpX8qAHoFN7wIvA7BQ/QQG2Qc7oV7DlCwDi5ju1ZJ4pEn75GvUSFxOsfRqPVe4Te RL/AKIzJoS9mcp/DpbA+qAtSS5fmRyYJq6KdG5oyytERuY9VlsRuBufxoAU0bnQh2npk yfQ5ZOR8FhCvls3GWDNXjou1gTAbEDCyHZeMuVfJJhV1CbSWXfSUZZpda7XrCqx/lI/R UrXtKUR6tN9/1IR/RaS7kzDKiOepqah52kGyvr+A0d123RIy55NxunvZC8T0u8ecdeEC UuDDeCpiR+BcoWFl+Spuo5KCYG3fd3DH74rslMi5TzEc9W+WZAvfWNvLbRgXYr+nY2Pp l9hQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712956099; x=1713560899; 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=3B8ubEPEvjOe4glCCPznp6yUG163aAy+zIl8t3hudPM=; b=S6j32Y3sv9o2forAyR9O+vudlO1pA/fKs7itrZCabmlGL6i6F3DfesuLLT3K1CQYGg AniFzBT3QTnqQXytNjZCwytoaxM1jx/n2Seuv0a0mzhNU7fe8zOHquWtspJLXzB/CZfA ioMi1tk79IcPw9kVmf8guc8YrXAMk4ulnpoTJYiHC0ikmQfWK72pB+RlxrToOUQrkwmr FG3NHxbE7g4dgtllXv3vAF0XUdtnGYz/yDx5P4sxXbuFL7WnIAJMcqerbR+rUyJSkXIT LxLCxhwOwU3b6xtxv5hHGrfUi7ov4Igk5aKw0gLDXp6ZfOF/YzPuhkEhqPQpdz7dYhHX o8Tw== X-Gm-Message-State: AOJu0YyKPm6aEH23lU4Rr9xO53+p4i6CiUbRmJ0XOn39T3N3RscwGNdS fMgj6kChtbv2rmZVKkGVFXtbgNY8cKsk9MGyM111SRGCxrnartoG2RG/wg== X-Google-Smtp-Source: AGHT+IHxzUQBwxHjWwi28sAKQqt8LA62wJ3YLyl75A0YWfjg/rpbkkCyGn41o8uJPHe3L+CsrK4taw== X-Received: by 2002:a05:6871:7382:b0:233:b5b3:7610 with SMTP id na2-20020a056871738200b00233b5b37610mr3523734oac.51.1712956098746; Fri, 12 Apr 2024 14:08:18 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:a1a1:7d97:cada:fa46]) by smtp.gmail.com with ESMTPSA id pk22-20020a056871d21600b002334685aedbsm1015117oac.11.2024.04.12.14.08.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Apr 2024 14:08:18 -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 v2 01/11] bpf: Remove unnecessary checks on the offset of btf_field. Date: Fri, 12 Apr 2024 14:08:04 -0700 Message-Id: <20240412210814.603377-2-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240412210814.603377-1-thinker.li@gmail.com> References: <20240412210814.603377-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 2aad6d90550f..0d44940c12d2 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -11529,7 +11529,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 Fri Apr 12 21:08:05 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: 13628324 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-oa1-f41.google.com (mail-oa1-f41.google.com [209.85.160.41]) (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 544F3153BC7 for ; Fri, 12 Apr 2024 21:08:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712956103; cv=none; b=n5hAW59rc33N6pqujQaFQkZ9m2+a2b7jkoVcRNbUj4dOHv91wP7ACOPvOdz90VW/P5OnAq7LILbk8Gmhk/PSDdfGCeZObYML6OL3kMBiYhACVG9brnswiKWiiRGDhtlyL3HyppdPfOKmgksPnRgH3Zil+qqWJj32iJTa87Qz96E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712956103; c=relaxed/simple; bh=QRgI8tt1lXF2UVb1h2GidAFomEe46UYdNaLGIMYmRPE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=b2gHpMn5nuh/PADuu37Yd+IJtWns+tCJTHbEFBNL1MfPg9EF/cWdHxz+5ipjQhdEQhXdUlNkTaFaV670HGK7xyFhCYzd2mW86c0/R3byRiT89j+Noqj2SvFCqN/s1I5Y1WH+w54Nw3mzQlPtJM6J3hD1D/v8WZoLwpbybR/kYxQ= 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=gK8oU/Af; arc=none smtp.client-ip=209.85.160.41 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="gK8oU/Af" Received: by mail-oa1-f41.google.com with SMTP id 586e51a60fabf-22f32226947so776987fac.2 for ; Fri, 12 Apr 2024 14:08:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712956100; x=1713560900; 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=ZJpX3nqLTwHBotbGdz4JsE9nEMIz3OLoljTf3Ghn4kQ=; b=gK8oU/AfrGixfS+MW5E8hwBkjOxFAqwzjNE1Ek7RUal+IyN4ByUyVl8l/ek4dcMUHM 0vTP/WkmEvgSFRAzSxfmEAWf3a4/6uuzNkCwcsVgG15yYW99CdwTb8AK/px282au2BfU xtezp/bXuSMHCk4M9Gyj6J1ZRArHKbQSefzoCOeAfWlK94rMhzjPsShI03SJfFSCnMpF hfZcH+01ivv2NodoxrS2X35vPh1groaTMZ1dHcc26OC9Qwm/zepKducqh81fQ+wKdI2I 2boyrQ648ungzkH2w7W4MLLsnl+RJZkWzP9h8NOLZzcKrmUkKsNCeWVXp6EZabnPu/cw /bog== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712956100; x=1713560900; 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=ZJpX3nqLTwHBotbGdz4JsE9nEMIz3OLoljTf3Ghn4kQ=; b=t01Mf5K8HYLFY/pVMIbb3dlqSyl65rJBEYPe2xuN571oJtmTfvMwr/2B0SgtBk8X5w NbR4u4HdT1kV1NiqW7R+igJlq52S/D3k1boSCQElCR18SgWHxsX/fjyR8qG680LhQUIY t2KPzey4SkR/mL/h8H1RyPTP64oveqWfwy3Rk27LVkkRsnNip/6TUAHHDnW34RXxKSLR tskxJ6F7uRfzo7FnvvijwfoPFF5cvRaJkivUAs6wcXqorpSNzZtV0gS9hfSo8+y2xlPC QcNknpDbWDToCiz7qxFCmO79az7Ohrj3gyXgC4w7Q4KaDOZaOVDIJL6uoA2H9V0ike6Q MFIw== X-Gm-Message-State: AOJu0YwNPTwUyChMZfR42FsjoXVdkUG7WsJX6mnzQbXaGANrOowawFEM aJVkYurEPhDFTrymtIvOQvAcHV5nNWSP7Pu+toDfojsruqME92d3YPs/OQ== X-Google-Smtp-Source: AGHT+IH7uSmDp/2RQXo33A8u6U8PdWClcve5apByXhesDOZjOSKJnx7VyO3p9o3YSN85hnT2eZrOxg== X-Received: by 2002:a05:6870:1650:b0:22e:ed14:3e3d with SMTP id c16-20020a056870165000b0022eed143e3dmr3992691oae.33.1712956099998; Fri, 12 Apr 2024 14:08:19 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:a1a1:7d97:cada:fa46]) by smtp.gmail.com with ESMTPSA id pk22-20020a056871d21600b002334685aedbsm1015117oac.11.2024.04.12.14.08.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Apr 2024 14:08:19 -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 v2 02/11] bpf: Remove unnecessary call to btf_field_type_size(). Date: Fri, 12 Apr 2024 14:08:05 -0700 Message-Id: <20240412210814.603377-3-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240412210814.603377-1-thinker.li@gmail.com> References: <20240412210814.603377-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 90c4a32d89ff..e71ea78a4db9 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -6672,7 +6672,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 0d44940c12d2..86adacc5f76c 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -5432,7 +5432,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 Fri Apr 12 21:08:06 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: 13628325 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-oa1-f45.google.com (mail-oa1-f45.google.com [209.85.160.45]) (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 7C93F154441 for ; Fri, 12 Apr 2024 21:08:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712956104; cv=none; b=hQLmGFnkXSIXbGNCG5TsizHO6Ep2wnPi2Mse8HEDqtnVMtwo3SkBHWYZhjIaAnKDxEYSmu2I1U6BRuophYk6GKvhNvf5bzLZFMW4USF8ZKs6jsNE/Ezhk3A17zOGNr3Q46hIEy1pSYG3Ldn60xfsMsGNhx2hRHwmnTg0LU8omYw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712956104; c=relaxed/simple; bh=WAQo3zeuYTuwBDnZIKOUNP3Zlaq6IdsaiiH7yQ3arVc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=pZmL+7LdFyavZ7Udnt/5MYtsa+hQdkxU6DfsXv4PNQ2mC2B+omQPsPt/FJ+bQDWzYBiBMxdUP5Cw3H4NmiivSBETtyZeak/1VYnDH0ZJ8BWpKAUId7ZkvGjCDVLQElhY/ySH5ZxDuMetUb2xymcEoe6fhkocoBjmM9pnM/VUlAk= 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=jbaiV6lc; arc=none smtp.client-ip=209.85.160.45 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="jbaiV6lc" Received: by mail-oa1-f45.google.com with SMTP id 586e51a60fabf-22ecc103220so802524fac.2 for ; Fri, 12 Apr 2024 14:08:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712956101; x=1713560901; 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=T4evj/sl1m/knLQ1ldzpUWvsY/xm9y/3A4+Z0ZfNuB0=; b=jbaiV6lcxEu38gQWJC3LHa6LjkCA3+/e/McQ35uHr9W1SsRp44sHe6OTahdOuVNYEH 0fLpSQduPNKSabdz4nE7x4KwJT2ojCXBrUt2gU2klowqgFCm6siqYzaOzlE/8YW9kIIj EgDFuHoMVnEw6q1cGd3triJ0APYNSQt6WI1ad/qMWYBIOZaTxCKJAYk9KPRbYygLBpNr /Eiz+Dqyn0qLVYEP0xatVX02tAzJsXZirW52KUsaPqC8O9y9kquLEyGiAAgMjbFCMv9/ T0iFHCooJyvtodUBr6oqFC4jghSk/sl9Usq2g1MAOB0RoFM8d3XuHCaG3lon6HEE8vph sdwA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712956101; x=1713560901; 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=T4evj/sl1m/knLQ1ldzpUWvsY/xm9y/3A4+Z0ZfNuB0=; b=jToDrYj5XUUx0jQ5iQd3SMs60Dhv4J7kgeB8HpUGNOQRq0+W7cQ+wvORYSoKgMyRSN IYPWAnQvVkBp6AZDR4gejvGoY7YgyP4o2wa0r5uHW6T/N9fd3PTgx0Sn1mRJVWqQciFI qZ05L9Ppor/Tvq8x6MM/8twF+S7eKsZtyE0Cc7pwq7LagFTrx0gc3/E3Xv3tH8V+x3th s64OSG4efbap/hrn2qOQkz7VmxjtAdnVv8dwQGrKUVJj/lvNtw9S4o+1bezaYaS9pdMz IbH5P6UI3WbQbxVPSqUDdt+nOGdKHhhCmlHDw8mMWqPrzsyfx7zWIPu4OkvFs5mFi4qD MonA== X-Gm-Message-State: AOJu0Yzus5GIQGjPM4S8uxh84kBR2Wuhk5kdkNhxeqsopCrKboFrXbea Ts6/DrpYEkEyTytacJcNvOqNOfdJnKhp0G7STeGsmmXOPjCvmPiUxMjTaA== X-Google-Smtp-Source: AGHT+IF9jzcAAL/L0BcZki8qmn5P0Ux7DfTHYwIL0W4Eu5YFrM43iHJvO81pJmpzXAamuHtnCBgR6Q== X-Received: by 2002:a05:6870:7694:b0:22e:d61d:a53c with SMTP id dx20-20020a056870769400b0022ed61da53cmr2481737oab.51.1712956101195; Fri, 12 Apr 2024 14:08:21 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:a1a1:7d97:cada:fa46]) by smtp.gmail.com with ESMTPSA id pk22-20020a056871d21600b002334685aedbsm1015117oac.11.2024.04.12.14.08.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Apr 2024 14:08:20 -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 v2 03/11] bpf: Add nelems to struct btf_field_info and btf_field. Date: Fri, 12 Apr 2024 14:08:06 -0700 Message-Id: <20240412210814.603377-4-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240412210814.603377-1-thinker.li@gmail.com> References: <20240412210814.603377-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 To support global field arrays for bpf_rb_root, bpf_list_head, and kptr, the nelems (number of elements) has been added to btf_field_info and btf_field. The value of nelems is the length of a field array. Nested arrays are flatten to get the length. If a field is not an array, the value of nelems should be 1. In the other word, you can not distinguish an array with only one element from a field with the same type as array's element. However, it is not a problem with the help of the offset and size in btf_field. field->size will be the size of the array if it is. The value of nelems of btf_field is always 1 to deactivate arrays for now, but the nelems of btf_field_info has been updated as the number of elements for data sections. A later patch will activate it. Signed-off-by: Kui-Feng Lee --- include/linux/bpf.h | 1 + kernel/bpf/btf.c | 24 +++++++++++++++++++++--- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 5034c1b4ded7..cab479925dfd 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -226,6 +226,7 @@ struct btf_field_graph_root { struct btf_field { u32 offset; u32 size; + u32 nelems; enum btf_field_type type; union { struct btf_field_kptr kptr; diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index e71ea78a4db9..6fb482789f8e 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -3289,6 +3289,7 @@ enum { struct btf_field_info { enum btf_field_type type; u32 off; + u32 nelems; union { struct { u32 type_id; @@ -3548,6 +3549,7 @@ static int btf_find_struct_field(const struct btf *btf, continue; if (idx >= info_cnt) return -E2BIG; + info[idx].nelems = 1; ++idx; } return idx; @@ -3565,6 +3567,19 @@ static int btf_find_datasec_var(const struct btf *btf, const struct btf_type *t, 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); + const struct btf_array *array; + u32 j, nelems = 1; + + /* Walk into array types to find the element type and the + * number of elements in the (flattened) array. + */ + for (j = 0; j < MAX_RESOLVE_DEPTH && btf_type_is_array(var_type); j++) { + array = btf_array(var_type); + nelems *= array->nelems; + var_type = btf_type_by_id(btf, array->type); + } + if (nelems == 0) + continue; field_type = btf_get_field_type(__btf_name_by_offset(btf, var_type->name_off), field_mask, &seen_mask, &align, &sz); @@ -3574,7 +3589,7 @@ static int btf_find_datasec_var(const struct btf *btf, const struct btf_type *t, return field_type; off = vsi->offset; - if (vsi->size != sz) + if (vsi->size != sz * nelems) continue; if (off % align) continue; @@ -3582,9 +3597,11 @@ static int btf_find_datasec_var(const struct btf *btf, const struct btf_type *t, switch (field_type) { case BPF_SPIN_LOCK: case BPF_TIMER: + case BPF_REFCOUNT: case BPF_LIST_NODE: case BPF_RB_NODE: - case BPF_REFCOUNT: + if (nelems != 1) + continue; ret = btf_find_struct(btf, var_type, off, sz, field_type, idx < info_cnt ? &info[idx] : &tmp); if (ret < 0) @@ -3615,7 +3632,7 @@ static int btf_find_datasec_var(const struct btf *btf, const struct btf_type *t, continue; if (idx >= info_cnt) return -E2BIG; - ++idx; + info[idx++].nelems = nelems; } return idx; } @@ -3834,6 +3851,7 @@ struct btf_record *btf_parse_fields(const struct btf *btf, const struct btf_type rec->fields[i].offset = info_arr[i].off; rec->fields[i].type = info_arr[i].type; rec->fields[i].size = field_type_size; + rec->fields[i].nelems = 1; switch (info_arr[i].type) { case BPF_SPIN_LOCK: From patchwork Fri Apr 12 21:08:07 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: 13628326 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-oa1-f44.google.com (mail-oa1-f44.google.com [209.85.160.44]) (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 B3E301552E3 for ; Fri, 12 Apr 2024 21:08:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.44 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712956105; cv=none; b=dEDtSEppjz8omS5ApJVU4THxYjfDs0u/khLRSa/iGxPbiJWInVQEixT/24Oi7mVMdVBdBxMR0sbwiwzdAJFXi9oHnVThC2SL+SLf15p+dfRO0qwmHPhVyE5CXdCxe6liFxBUNN8VvNMXpSVXbnrgMKSSFvyzeqqGcNOKijvH05U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712956105; c=relaxed/simple; bh=FP5sOiCtSPy9Dqg+lcJupXzhrFQfzTjbtbEhG8RfKHs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=SNRRbhXmJLcvtQ1TFa8p8uaumSOYA0o4utMtc+gY/c09aeFNz4g6gOR41w+86pqvPZvO+iYXYeKJl/gCrjRe1LIoTEDQbVCGRXzOyTZ4lwOasPaFNbZ/Mak94XEU/Zb6k50dLzrdedJCAF7uInKiyw9IOk0Tqp9D3QT3MTvyb0s= 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=iNLIwFqC; arc=none smtp.client-ip=209.85.160.44 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="iNLIwFqC" Received: by mail-oa1-f44.google.com with SMTP id 586e51a60fabf-221e1910c3bso708673fac.1 for ; Fri, 12 Apr 2024 14:08:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712956102; x=1713560902; 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=Lpihkz25ih9HzbbtbdX+Qgomtg80aTKQZhPcenojhIQ=; b=iNLIwFqC8sIFZwJNEvdZkr7vlC/dxnLqrD3pVzMwg1v6slkTOVaXmk2Q3nyZjG66eg 4TD7bWM7ZSUN5JsxWFlrQVwyRXfYnClvnHF38Yn5262dlBQB+PHEDGCXp2f1mIXpQaCe MBPM31s9w23/5GjpF5cilAsWpY5FZcCqwmzd4oQx0s6A9vBWE/2CYofBRWBIUytucX0/ 92Fwyi5nQ+evvuxDLCg2ITmBHs+n3zkIlXmu5gzYzGLg8kD0EGgYEtj/zfxj643tvJW1 HmQ9r9uCUw08ELSk6ydn2t0PxDDhHn4LQYIo27QWCJiyn7XsS2ZdS9Iy2z2Eda4+1rp5 GV/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712956102; x=1713560902; 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=Lpihkz25ih9HzbbtbdX+Qgomtg80aTKQZhPcenojhIQ=; b=b/+BTK9Nz3rdiHU5USv7cFf9FnYu9JAP7u3ONK6kjm6ymDNFMMsI2LkbdOkzoW0bwr CI2G7m790uIGruuvihUSWOINNqMksuOAeusJS7+vGQolEgy2BbTvwl+0+cdVM9w0O4LP KdZgKDVCnpEPB2l2hNN2SvBvgb4wkGIyeo60xFIvQ+cGELpY2xcByNGE/LHe2UW46lG/ ZX+FuGVqTG0j7kUU4SSWP2liTL+hAPZc7ZoFZfjC1EWhZ/SvPsty69Gvyi502R4tS5Ib 82oSXCEPat3UKbINvB8HiQiDkJ/804SyaDeETWzhiGSC+/KLnc3j/p8zoewNBgFeb8QH vtQQ== X-Gm-Message-State: AOJu0YwekZVDYczXqG1AIE24SAti9CTStpienG6/ZOT/gW2dHhKm5ktG tm96u6aFGGvdOQ15sKCmOABzT+QeQtJFBZR5ureHUr1//BwlfqRu44D8ag== X-Google-Smtp-Source: AGHT+IGOdmpgwp+HHb7oyeuZCjl5N/vlMbHTBqGU2fbZIlG1cnFaZfYmHFe1nfzLy/1TtQTdSDtFtg== X-Received: by 2002:a05:6871:5d2:b0:22e:9c5a:233e with SMTP id v18-20020a05687105d200b0022e9c5a233emr3937086oan.43.1712956102384; Fri, 12 Apr 2024 14:08:22 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:a1a1:7d97:cada:fa46]) by smtp.gmail.com with ESMTPSA id pk22-20020a056871d21600b002334685aedbsm1015117oac.11.2024.04.12.14.08.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Apr 2024 14:08:21 -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 v2 04/11] bpf: initialize/free array of btf_field(s). Date: Fri, 12 Apr 2024 14:08:07 -0700 Message-Id: <20240412210814.603377-5-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240412210814.603377-1-thinker.li@gmail.com> References: <20240412210814.603377-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 Initialize and free each element in a btf_field array based on the values of nelems and size in btf_field. The value of nelems is the length of the flatten array for nested arrays. Signed-off-by: Kui-Feng Lee --- include/linux/bpf.h | 7 +++++++ kernel/bpf/syscall.c | 39 ++++++++++++++++++++++++--------------- 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index cab479925dfd..b25dd498b737 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -390,6 +390,9 @@ static inline u32 btf_field_type_align(enum btf_field_type type) static inline void bpf_obj_init_field(const struct btf_field *field, void *addr) { + u32 elem_size; + int i; + memset(addr, 0, field->size); switch (field->type) { @@ -400,6 +403,10 @@ static inline void bpf_obj_init_field(const struct btf_field *field, void *addr) RB_CLEAR_NODE((struct rb_node *)addr); break; case BPF_LIST_HEAD: + elem_size = field->size / field->nelems; + for (i = 0; i < field->nelems; i++, addr += elem_size) + INIT_LIST_HEAD((struct list_head *)addr); + break; case BPF_LIST_NODE: INIT_LIST_HEAD((struct list_head *)addr); break; diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 7d392ec83655..cdabb673d358 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -672,6 +672,8 @@ void bpf_obj_free_fields(const struct btf_record *rec, void *obj) const struct btf_field *field = &fields[i]; void *field_ptr = obj + field->offset; void *xchgd_field; + u32 elem_size = field->size / field->nelems; + int j; switch (fields[i].type) { case BPF_SPIN_LOCK: @@ -680,35 +682,42 @@ void bpf_obj_free_fields(const struct btf_record *rec, void *obj) bpf_timer_cancel_and_free(field_ptr); break; case BPF_KPTR_UNREF: - WRITE_ONCE(*(u64 *)field_ptr, 0); + for (j = 0; j < field->nelems; j++, field_ptr += elem_size) + WRITE_ONCE(*(u64 *)field_ptr, 0); break; case BPF_KPTR_REF: case BPF_KPTR_PERCPU: - xchgd_field = (void *)xchg((unsigned long *)field_ptr, 0); - if (!xchgd_field) - break; - - if (!btf_is_kernel(field->kptr.btf)) { + if (!btf_is_kernel(field->kptr.btf)) pointee_struct_meta = btf_find_struct_meta(field->kptr.btf, field->kptr.btf_id); - migrate_disable(); - __bpf_obj_drop_impl(xchgd_field, pointee_struct_meta ? - pointee_struct_meta->record : NULL, - fields[i].type == BPF_KPTR_PERCPU); - migrate_enable(); - } else { - field->kptr.dtor(xchgd_field); + + for (j = 0; j < field->nelems; j++, field_ptr += elem_size) { + xchgd_field = (void *)xchg((unsigned long *)field_ptr, 0); + if (!xchgd_field) + continue; + + if (!btf_is_kernel(field->kptr.btf)) { + migrate_disable(); + __bpf_obj_drop_impl(xchgd_field, pointee_struct_meta ? + pointee_struct_meta->record : NULL, + fields[i].type == BPF_KPTR_PERCPU); + migrate_enable(); + } else { + field->kptr.dtor(xchgd_field); + } } break; case BPF_LIST_HEAD: if (WARN_ON_ONCE(rec->spin_lock_off < 0)) continue; - bpf_list_head_free(field, field_ptr, obj + rec->spin_lock_off); + for (j = 0; j < field->nelems; j++, field_ptr += elem_size) + bpf_list_head_free(field, field_ptr, obj + rec->spin_lock_off); break; case BPF_RB_ROOT: if (WARN_ON_ONCE(rec->spin_lock_off < 0)) continue; - bpf_rb_root_free(field, field_ptr, obj + rec->spin_lock_off); + for (j = 0; j < field->nelems; j++, field_ptr += elem_size) + bpf_rb_root_free(field, field_ptr, obj + rec->spin_lock_off); break; case BPF_LIST_NODE: case BPF_RB_NODE: From patchwork Fri Apr 12 21:08:08 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: 13628327 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-oa1-f45.google.com (mail-oa1-f45.google.com [209.85.160.45]) (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 8AA7115532C for ; Fri, 12 Apr 2024 21:08:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712956106; cv=none; b=HTpTiWv/A91VzL8llG7UttYLpYGXAOSqdyZ5NmT5lL70PDIOfw+zS3PnImjg4t8x7WRmPFHGTguzdKpALTLB+RFaEEjYUahH9eZcvzB/zsEuvZR3R/DO9DhVhnIHTAOgALr8TOi4vrq8Pw8/BV/nI5vQ6RXvvrm5+ej2egWcfeg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712956106; c=relaxed/simple; bh=cegdf/GB4TUTwqgkozn+YfKCbNRABusidh9A0N5MHTM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=A+FIpdTA9sToTzNEVXU5PcjJBlHZurQVpTTm7wXXqvHgu4bItbUXWUpf650gVp4xaC0wf9qb0zbv7KRzsY1V8hgHpv7ZnQES6xXQokX0f3ppWXKwGXffcUnQSDLnHFOePkDuBLLOwV1LaNNMLfsjOqvI6R7bo1sDW8ObpDi+aCI= 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=XYhqpqaH; arc=none smtp.client-ip=209.85.160.45 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="XYhqpqaH" Received: by mail-oa1-f45.google.com with SMTP id 586e51a60fabf-232f2b86e4fso729280fac.3 for ; Fri, 12 Apr 2024 14:08:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712956104; x=1713560904; 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=IVe3JxZE6Z55O1rHODqgNOrrk4iw+fYt2hDBvI/KiNI=; b=XYhqpqaHGeYJD8hOvxdDR6E4a0vK5qOeE3g3YUzMOaNxAWON+ts/LUhaGrzUBA2r7L UWMu5BorCN/uauOvjQs3Kgk8LWeiRHMU3nKWLYQ2Fgdd4heMvb5YayE6pPCc75EuXZMx 7AZW2WTVLSxBolBDL6GpTl5Vgwd9LoP5HFSGyQj5yHjGiBaxFG/WVz8qlAtEX86K3a5U v1yCDzC9VlEHfr9et4BTYnCPnsuk0NWAtQwYL3EtV3MtUEIslx9Gh3SrI05wPBM5bwFA vEw5WDhcX/i+Cz9NBhJ0HY0bbjefo06i9n9dBSdrGVTcgLQFCpM1i8vNdEboVJggfYV4 lUfw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712956104; x=1713560904; 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=IVe3JxZE6Z55O1rHODqgNOrrk4iw+fYt2hDBvI/KiNI=; b=kT/GzSIxrtwYu6rwDX+ZX+qOG9j9tT1qMptn5dMFCSgeWXfx8GbR4gyu392xZlkyUJ PMsuHhVQ925gfkbADMH5CnLgNI9M2NrNdtfRf5AnyZWPfHWS8FDYxP5PX4WQ/NRpxFAG hGIvmeoKZkhc3UkbwfdW++gmC+p8o+2um7gJc6uEqKzRLx7Ti9fh8RJ78F8FO5V1nsSb WawdxFTqBV9NBMqoH3UwjkCLQrssEl4h56zfkYanxnYz0dOR8p+nMsapSaDm3PBlqSmd zzAmj8jOJpKDVR2g6k+9bUKc+G3l3S7mq+a+sq9z6TCnfTwD4471Ekt0qUo+6xJyq7zy Z/DA== X-Gm-Message-State: AOJu0YzAoHOo8DtKOcyq86pZUmIU4Bmz9puwy5Qx1JvHe9zuqTKFkBp0 UuqKZc98TgZthuMBpTEouRQ3vfrKh6s3AH/BKAd4Fsu1Dcast+PEGh2w9g== X-Google-Smtp-Source: AGHT+IHj/ZwEkeY8x7KAPCerj3zFIxD7vhBayAVU3X5KLeb8/MAwtmWBAiQlYrRKNXM0BGyewo3CUg== X-Received: by 2002:a05:6870:ec90:b0:22e:b7d3:fbb3 with SMTP id eo16-20020a056870ec9000b0022eb7d3fbb3mr4518245oab.4.1712956104140; Fri, 12 Apr 2024 14:08:24 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:a1a1:7d97:cada:fa46]) by smtp.gmail.com with ESMTPSA id pk22-20020a056871d21600b002334685aedbsm1015117oac.11.2024.04.12.14.08.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Apr 2024 14:08: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 Subject: [PATCH bpf-next v2 05/11] bpf: Find btf_field with the knowledge of arrays. Date: Fri, 12 Apr 2024 14:08:08 -0700 Message-Id: <20240412210814.603377-6-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240412210814.603377-1-thinker.li@gmail.com> References: <20240412210814.603377-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 Modify btf_record_find() so that it locates a btf_field by comparing the provided offset with the offset of elements, instead of the offset of the entire array, in the case where a btf_field represents an array. This update is crucial for accommodating btf_field arrays in upcoming patches. Signed-off-by: Kui-Feng Lee --- kernel/bpf/syscall.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index cdabb673d358..1c8a9bc00d17 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -516,11 +516,16 @@ int bpf_map_alloc_pages(const struct bpf_map *map, gfp_t gfp, int nid, static int btf_field_cmp(const void *a, const void *b) { const struct btf_field *f1 = a, *f2 = b; + int gt = 1, lt = -1; + if (f2->nelems == 0) { + swap(f1, f2); + swap(gt, lt); + } if (f1->offset < f2->offset) - return -1; - else if (f1->offset > f2->offset) - return 1; + return lt; + else if (f1->offset >= f2->offset + f2->size) + return gt; return 0; } @@ -528,12 +533,19 @@ struct btf_field *btf_record_find(const struct btf_record *rec, u32 offset, u32 field_mask) { struct btf_field *field; + struct btf_field key = { + .offset = offset, + .size = 0, /* as a label for this key */ + }; if (IS_ERR_OR_NULL(rec) || !(rec->field_mask & field_mask)) return NULL; - field = bsearch(&offset, rec->fields, rec->cnt, sizeof(rec->fields[0]), btf_field_cmp); + field = bsearch(&key, rec->fields, rec->cnt, sizeof(rec->fields[0]), btf_field_cmp); if (!field || !(field->type & field_mask)) return NULL; + if ((offset - field->offset) % (field->size / field->nelems)) + /* not aligned with an array element */ + return NULL; return field; } From patchwork Fri Apr 12 21:08:09 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: 13628328 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-ot1-f50.google.com (mail-ot1-f50.google.com [209.85.210.50]) (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 4018C1514D1 for ; Fri, 12 Apr 2024 21:08:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.50 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712956107; cv=none; b=hV0Ou53O4gWieidjnDflP9pJiMlyNvRurtDAOxBxG+0VJHSsmkyBUh+SjfTjVWBp+5TgHif12lbkdLXgz+v/1ETJ1QYTMp2vxCZUwMcoi7Cj5lsgDKWe1kgaWF91ECjROiBFvLZX2ZzgTSUWOw8jfmHSGJ1nWctWu94gQEvY5zA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712956107; c=relaxed/simple; bh=6XkZ/1FOu1LupX3nFqllD0vWnsEEY8AZzNuJu9Jpyno=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=JRgf4tHDHKMvmwkBaxDyE5tnANCfEO1a6Pgk4q/kHaSPFY7fHBoUBUQNSOi/k+a6YvJ3tZu2S2jN8Derunph5YK5G4yghI5BX32mhJw8SKpvGvddpDd4KzLRsCMb8qWKdcUE17EoYKpbkSB7Wsjy17bmBnnBqDjuPc6Cw6rklj4= 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=KWdhvUUP; arc=none smtp.client-ip=209.85.210.50 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="KWdhvUUP" Received: by mail-ot1-f50.google.com with SMTP id 46e09a7af769-6ea26393116so1038873a34.0 for ; Fri, 12 Apr 2024 14:08:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712956105; x=1713560905; 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=/GqBNkXmzywsDFSvjJFp1U9/X7HRQVzzRiLFpr4/tl0=; b=KWdhvUUPYufJRZSshUv6DK4E+OsSsBmgTN+q4XRTa9mF83mzpN61hdlDiOs929w7iy Z3aom95xZag08NBGOlndw0szWNQG22n4Y6oevOfbVZVd48c5xy/w41f6bITXBrRudefF Lm5q7QFSHrYrsS2tU49Hp1vLCrwdh9SMYrgfbSBvazu3CpgNZ9diF2DzKepmdwuj+5iG luN9SVGiT9o4meT2SAeIB//bQiB6DpQo9wNd6nSAOblKum5SaVPXv9edQgCfHgBYQChz WdFE5j6xNDeJPvSUVk8aDNxEwbp5jW9KbW3Ynqx3vEg2toRA8qfRdPYvM/OThYfAOMQd O3Pg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712956105; x=1713560905; 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=/GqBNkXmzywsDFSvjJFp1U9/X7HRQVzzRiLFpr4/tl0=; b=l5fgri8vVmXpQ6xi+TFHajITE6O6CKtI+uKz07c90W5X2ZzWRfzw7MR6mKJEk6f1qs ekmyTxooUcEm5Z/IvOR3UqiF/CwMHRTocPJWQMIWM1G9b9W8194JqZpeyBof06/gujom eyiNFD6Pu1N01rWaDoIpQCi+OHnVfK2/aroxKkpM8yT+DkhMbmMwimzvjjVCCCiRqKai Yr8CyEJjbUeXqK3ALRrgx0xiWtrWeARijaH88OHmwneW0Zl+kmulfCgy6Ta0O4J47kk1 tB/uAyHy9LKuSmLup/pGg+Q/mBhhiuLUD20Sc2nsVsvQfukKYtdQRhQRf39ToRPXfeMg TMbw== X-Gm-Message-State: AOJu0YwYww9vaGHmMeHwZAzmiBZDWZdoGdUYPeVZRjdnLUemgGTsShHH sB0wCp725WIuIwV09o6B82SsDITsLzfS2w0cVd746mG9srTGwzPf3xUmCw== X-Google-Smtp-Source: AGHT+IEhHLKC6KS7Hu6QXMbUMnzOptJZHkgr9CQu/gWT/lI95mCVYXv3I6ZLBDx2/7Vo18a2uqW2Xg== X-Received: by 2002:a05:6871:799e:b0:22a:8e5:941 with SMTP id pb30-20020a056871799e00b0022a08e50941mr4003131oac.52.1712956105227; Fri, 12 Apr 2024 14:08:25 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:a1a1:7d97:cada:fa46]) by smtp.gmail.com with ESMTPSA id pk22-20020a056871d21600b002334685aedbsm1015117oac.11.2024.04.12.14.08.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Apr 2024 14:08: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 Subject: [PATCH bpf-next v2 06/11] bpf: check_map_access() with the knowledge of arrays. Date: Fri, 12 Apr 2024 14:08:09 -0700 Message-Id: <20240412210814.603377-7-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240412210814.603377-1-thinker.li@gmail.com> References: <20240412210814.603377-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 Ensure that accessing a map aligns with an element if the corresponding btf_field, if there is, represents an array. Without this change, an access would need to align with the beginning of an array, otherwise it would be rejected. Signed-off-by: Kui-Feng Lee --- kernel/bpf/verifier.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 86adacc5f76c..302aad33a7f4 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -5426,7 +5426,7 @@ static int check_map_access(struct bpf_verifier_env *env, u32 regno, rec = map->record; for (i = 0; i < rec->cnt; i++) { struct btf_field *field = &rec->fields[i]; - u32 p = field->offset; + u32 p = field->offset, var_p, elem_size; /* If any part of a field can be touched by load/store, reject * this program. To check that [x1, x2) overlaps with [y1, y2), @@ -5446,7 +5446,10 @@ static int check_map_access(struct bpf_verifier_env *env, u32 regno, verbose(env, "kptr access cannot have variable offset\n"); return -EACCES; } - if (p != off + reg->var_off.value) { + var_p = off + reg->var_off.value; + elem_size = field->size / field->nelems; + if (var_p < p || var_p >= p + field->size || + (var_p - p) % elem_size) { verbose(env, "kptr access misaligned expected=%u off=%llu\n", p, off + reg->var_off.value); return -EACCES; From patchwork Fri Apr 12 21:08: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: 13628329 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-oo1-f46.google.com (mail-oo1-f46.google.com [209.85.161.46]) (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 ABC1C1514DC for ; Fri, 12 Apr 2024 21:08:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.161.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712956109; cv=none; b=o5/RDCj7UJb/qiV74vgOa8FtnJcbfPL8lpq7i6BZWfpaIxsHju4pDWCtWMljYQ/5+mLtOd1ADnf4z6oYotNU4VHderW6twkegqDS8y4T71Tk4UdKtcFJ04BJtpvAJ89cnnuQnRpsR+wM89X84OdenN7OqOy0pA5AuWslUHZ0m0A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712956109; c=relaxed/simple; bh=Ez+l1VleCaf3f4r1vE+m+duR7QZfwt8rVoI9ub1KDEk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=rbr1623Ef8U/GcMIJMfBRv92drGXi4uM22c8SPQVrlzFhTFthFy+5dh8oCYdW9GcPPztyH8SVcQ/Q7ghIV74VRLsYNF0po7SM+PoCsAP43lGTw3Qs+lgDWPD25RFH6ciMqQm50VLJuqWs3K0n7mY4E7SZWzTnR6NsUZKTfmWIGY= 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=i9ph+h6q; arc=none smtp.client-ip=209.85.161.46 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="i9ph+h6q" Received: by mail-oo1-f46.google.com with SMTP id 006d021491bc7-5aa1e9527d1so868055eaf.1 for ; Fri, 12 Apr 2024 14:08:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712956106; x=1713560906; 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=hSxlDb9lcWN1aGaSHOqa6Mi308SXNva/SP10QJMLung=; b=i9ph+h6qoBb1Xr1/KIVMZzWdyn3YmfyxG45dffEZ3iSej6IMpl8U+mHzhnrje1HbjX lA9FtAaesiDhLaeJxf6Rmbd3/zuuXNK0mRTm0maN2qqzJrZcyC6Bh6iJE8gAqkhWmnwV gjSoOnBB235uYNJiOiOb1sRbUqjudL4cVBiwXwYI3zKA3mNRKSpbWPewlFf7LPCwnH1W cZpHvBFNaJeL9O4O5ffPSEBAUU3ECsIRGCLjdpR81q+piX+lRg+xxPi+lD13JblB82nS zL592qSUwBz+l6l515PI1/+oDh3SmGO8d2uLY6u59w0URYwI559THOQ5ua1y3VCopTQm qI3g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712956106; x=1713560906; 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=hSxlDb9lcWN1aGaSHOqa6Mi308SXNva/SP10QJMLung=; b=GdkClkDclAKECzYSRwPzfpZZT8hxgC6Pwwjgr0WpamqzIFwvHdFmZQJvr8Vupwk5YP jyHIXi4oSk3Cobrw/X2fUx7h8kcIIt5HKSNFBabOgYeMBLhbjhgZASFoSC54bYEG9EyT aEfmTyA873lcT6HCqIu10+ogicGwizuDp7okrh1K67XLtBcHdJkwkXLizZUgGxDnhN1W Qx5hFad56P/AFiSxjbU8lG8Yf6G72/CpeQjpNU8tYx2+mn0Mp5ka86Pp04lWl7HgRFzP ehM8nddNS1gk4G8obAZOfY77m2zZdjyxusV7lgicuoOI57zUkOiWiLRT24ggdhH7M+k/ KEyw== X-Gm-Message-State: AOJu0YzbTLKwjMQJrDX+jCGBHI3Wpc/mJNZA7gUQqvQo2I1bv+CM9atn k4TQP4kBcWBBebjs5z6S6pPvHrG4Ag3yr1AHpSnjOH48/Txd3WB0zbFjCQ== X-Google-Smtp-Source: AGHT+IHt0J1iJlgEHFlORG2unlRI4XSXyqLa3oUS2uKS/LnZGZLrE8gvmkwshgET6Wt6FiSWOdO0VA== X-Received: by 2002:a05:6870:f21a:b0:229:f035:f5b2 with SMTP id t26-20020a056870f21a00b00229f035f5b2mr4632853oao.17.1712956106267; Fri, 12 Apr 2024 14:08:26 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:a1a1:7d97:cada:fa46]) by smtp.gmail.com with ESMTPSA id pk22-20020a056871d21600b002334685aedbsm1015117oac.11.2024.04.12.14.08.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Apr 2024 14:08: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 , Eduard Zingerman Subject: [PATCH bpf-next v2 07/11] bpf: check_map_kptr_access() compute the offset from the reg state. Date: Fri, 12 Apr 2024 14:08:10 -0700 Message-Id: <20240412210814.603377-8-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240412210814.603377-1-thinker.li@gmail.com> References: <20240412210814.603377-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 Previously, check_map_kptr_access() assumed that the accessed offset was identical to the offset in the btf_field. However, once field array is supported, the accessed offset no longer matches the offset in the bpf_field. It may refer to an element in an array while the offset in the bpf_field refers to the beginning of the array. To handle arrays, it computes the offset from the reg state instead. Acked-by: Eduard Zingerman Signed-off-by: Kui-Feng Lee --- kernel/bpf/verifier.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 302aad33a7f4..67b89d4ea1ba 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -5349,18 +5349,19 @@ static u32 btf_ld_kptr_type(struct bpf_verifier_env *env, struct btf_field *kptr } static int check_map_kptr_access(struct bpf_verifier_env *env, u32 regno, - int value_regno, int insn_idx, + u32 offset, int value_regno, int insn_idx, struct btf_field *kptr_field) { struct bpf_insn *insn = &env->prog->insnsi[insn_idx]; int class = BPF_CLASS(insn->code); - struct bpf_reg_state *val_reg; + struct bpf_reg_state *val_reg, *reg; /* Things we already checked for in check_map_access and caller: * - Reject cases where variable offset may touch kptr * - size of access (must be BPF_DW) * - tnum_is_const(reg->var_off) - * - kptr_field->offset == off + reg->var_off.value + * - kptr_field->offset + kptr_field->size * i / kptr_field->nelems + * == off + reg->var_off.value where n is an index into the array */ /* Only BPF_[LDX,STX,ST] | BPF_MEM | BPF_DW is supported */ if (BPF_MODE(insn->code) != BPF_MEM) { @@ -5393,8 +5394,9 @@ static int check_map_kptr_access(struct bpf_verifier_env *env, u32 regno, return -EACCES; } else if (class == BPF_ST) { if (insn->imm) { - verbose(env, "BPF_ST imm must be 0 when storing to kptr at off=%u\n", - kptr_field->offset); + reg = reg_state(env, regno); + verbose(env, "BPF_ST imm must be 0 when storing to kptr at off=%llu\n", + reg->var_off.value + offset); return -EACCES; } } else { @@ -6784,7 +6786,8 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regn kptr_field = btf_record_find(reg->map_ptr->record, off + reg->var_off.value, BPF_KPTR); if (kptr_field) { - err = check_map_kptr_access(env, regno, value_regno, insn_idx, kptr_field); + err = check_map_kptr_access(env, regno, off, value_regno, + insn_idx, kptr_field); } else if (t == BPF_READ && value_regno >= 0) { struct bpf_map *map = reg->map_ptr; From patchwork Fri Apr 12 21:08: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: 13628330 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-ot1-f44.google.com (mail-ot1-f44.google.com [209.85.210.44]) (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 4BCF4155337 for ; Fri, 12 Apr 2024 21:08:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.44 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712956109; cv=none; b=IMLMvC9ty+P4bSNJMQDNRhTdabT8D3tB5S7C7m09yECL3B4pqfIbK6v2yrOb8jATk5PKLWqGRwO7VuLumDIgrbnz3G8LIUpsjDLUMTWwTd0zGxs18jGAINAQsnFea3WEAFnV2nUxH5oDr4HQSpSJLJVjJXmBmseYLg4S9Beyj8U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712956109; c=relaxed/simple; bh=FnltRKvuB2ibpSeRZADOoapJF5TuPlRP9dDsHIkkVuI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ZUFhlRfzR8Wo9dvgk88T4F0xbed+GQooJm7GbAE3By4mhTONy98n94fCq6VnCiTZx7ciE9WG8z6i5+V+jh31LH/TyK0uXSPZ+pIRXZxT8UduNQyyo09BDQuWY/wfQcn7rpD/y5T/J7zUDIKCtQPfnlloNRwCIuz8pLSkhPPt7SM= 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=cDTR8xmC; arc=none smtp.client-ip=209.85.210.44 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="cDTR8xmC" Received: by mail-ot1-f44.google.com with SMTP id 46e09a7af769-6eb6b6cdb6dso78274a34.1 for ; Fri, 12 Apr 2024 14:08:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712956107; x=1713560907; 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=/GnXXeZfEMkA7zk9KrCCtAwm0v9n/2vjIg5x5unW63M=; b=cDTR8xmCigZxfoqnfmJQeHiLrZJLRCKI0wHWUTi4+astkohAhf5GnQrU6ea2VRhmfc p8UZdEUN9drfHRbe4Ifl9Jm1Rggq2LjzidkizRGsAPBgf4EhbVNMc9nNIZPW7ZEMst1S 52pzo0LyKe6rBMUTpBFuqpdZk00H/JcdTyQtUigw/1PD97XS0qsHy4wNuMwXG21qXmR2 4rKXWGfx7dOfIjEO/cS7IKZfccB7lg5zgHXW5tZ5aGk/Nt51ez4wIS9p9CMFxvKhCu8u rbzNVDgODseCFY1eT6LIlFYC8gqIBeMo3SPihK/m89db/2wL5naRSUzGfVZIsyGwk/JD 4KJQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712956107; x=1713560907; 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=/GnXXeZfEMkA7zk9KrCCtAwm0v9n/2vjIg5x5unW63M=; b=d+N8YkVS/IbAQRR2VXoXaAg0v/EWf2dgbTLLdss4bZ9Nw2J8YGF9nHVnkpqQJyNJ89 /EIO8n9kARQ1EN98d2i6T8fQ851PS1g0TafPlbeQ7mr5nWTVM0vijzw3HcP3XWPG0X6H SSq3hzMkxaXfZckHCruLYzxwl/YtF8sxFXsgwXch4J/BADPK7yTUkkPW4nkX/itM1gcS x9pQgj4z+UW0gsGmJT3o5/murO7fghm8iR3kOuINW+FCkgKvGamjbB6iuT0hyYfck0Kp 3hz0rPi0FBRsP8B8u3a742fk2n6KXUyYloJeRctIi7BZuxiugR/KA2CUfVh7D2Xae2fB 19sw== X-Gm-Message-State: AOJu0YyEzdf7oFqPsfADu0D/qyQl/0rKO4c9dImDfcXhm8yMmLWMucM2 KzQvAa/Q5NtK0cHcSh7klEVaKoegOENRXi4EWAPtbM3Cjxay4HW9gufAKw== X-Google-Smtp-Source: AGHT+IEI4tV2NzvY1tYsuoHwXQ/bzP1kBy0uppsch9CuEtyqhSOq9HOVzaYjGVUlu2/E1aJ6t6L9Wg== X-Received: by 2002:a05:6870:b4aa:b0:22e:ddd1:4641 with SMTP id y42-20020a056870b4aa00b0022eddd14641mr3879065oap.35.1712956107288; Fri, 12 Apr 2024 14:08:27 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:a1a1:7d97:cada:fa46]) by smtp.gmail.com with ESMTPSA id pk22-20020a056871d21600b002334685aedbsm1015117oac.11.2024.04.12.14.08.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Apr 2024 14:08:26 -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 v2 08/11] bpf: Enable and verify btf_field arrays. Date: Fri, 12 Apr 2024 14:08:11 -0700 Message-Id: <20240412210814.603377-9-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240412210814.603377-1-thinker.li@gmail.com> References: <20240412210814.603377-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 Set the value of "nelems" of a btf_field with the length of the flattened array if the btf_field represents an array. The "size" is the size of the entire array rather than the size of an individual element. Once "nelems" and "size" reflects the length and size of arrays respectively, it allows BPF programs to easily declare multiple kptr, btf_rb_root, or btf_list_head in a global array. Signed-off-by: Kui-Feng Lee --- kernel/bpf/btf.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index 6fb482789f8e..ae17d3996843 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -3835,7 +3835,7 @@ struct btf_record *btf_parse_fields(const struct btf *btf, const struct btf_type rec->timer_off = -EINVAL; rec->refcount_off = -EINVAL; for (i = 0; i < cnt; i++) { - field_type_size = btf_field_type_size(info_arr[i].type); + field_type_size = btf_field_type_size(info_arr[i].type) * info_arr[i].nelems; if (info_arr[i].off + field_type_size > value_size) { WARN_ONCE(1, "verifier bug off %d size %d", info_arr[i].off, value_size); ret = -EFAULT; @@ -3851,7 +3851,7 @@ struct btf_record *btf_parse_fields(const struct btf *btf, const struct btf_type rec->fields[i].offset = info_arr[i].off; rec->fields[i].type = info_arr[i].type; rec->fields[i].size = field_type_size; - rec->fields[i].nelems = 1; + rec->fields[i].nelems = info_arr[i].nelems; switch (info_arr[i].type) { case BPF_SPIN_LOCK: From patchwork Fri Apr 12 21:08: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: 13628331 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-oa1-f47.google.com (mail-oa1-f47.google.com [209.85.160.47]) (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 5E5B31514E3 for ; Fri, 12 Apr 2024 21:08:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712956110; cv=none; b=M2qVjH2+8PQaM+Fspht/rCdNd1memRRi+KZj8Yia3R+sQUw6JlZK8I+QpCZ8fWSYJOqLT6oykSBBZ5t8yq8DrcPVBY6jvWrZ2mXaNTSA5tZSdfa+X67Z4asquuTDlKVksX3U58VIXrT/xjAsYgnYpg6gIiuNyXPXMp+XbnJgCaA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712956110; c=relaxed/simple; bh=DypjCVY43yhA5fiSymfCfD24cW9KiO9HvaU5WeMouKs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=mel0wQscHeaN5UmtpVKc/9TIXzpjWxpTjnnLDBH9MUtS3xvR5QuRWVRndqut/ms5P5m195qPIek1tN6TDbAT7JLKB642yeV/enBp8m1z/vn5tgLUdItnihI7DU/CdzoQr3OF0SL8u8vd81ExjhW16UnVAceEgEoNPFEQvKcefec= 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=VNDzOdIk; arc=none smtp.client-ip=209.85.160.47 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="VNDzOdIk" Received: by mail-oa1-f47.google.com with SMTP id 586e51a60fabf-23335730db1so690692fac.2 for ; Fri, 12 Apr 2024 14:08:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712956108; x=1713560908; 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=xCXoxIZdu3IbIezcXli4tuWerfrPR0dwNVTGMo9uTDs=; b=VNDzOdIkq+x9mGeRLERSTSFIgh2OzF+t3arbOxZo+/wO4DDuBw2zRKGKf+y0TuqpsQ rtyznMNsQ10EDKMqXx6buUvjFz7NNdoV8RJ5qgtBbU6MM04jWTZkyw3N1G5FhbMzobHU UZEuHMOfWNP1AP+I1iQSAU/509xqOGjJjbXhJhwiTqrizt5fFYUYuQuxXSoCv5N7ZLvv HPUk+p3J7miV1pcka74VEr0n/DQE6PufALLwjcf6psbFpwEBBMFPIJoBvPraxTwygtV6 H1nnf5gaLhSFdLGs+W0WWKuwUMFP0aKqq7FCNwzyy1PYodX4//PqUDYk678EDg53JUd2 iGFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712956108; x=1713560908; 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=xCXoxIZdu3IbIezcXli4tuWerfrPR0dwNVTGMo9uTDs=; b=MgpypTdc29GjoGXhdADCQBzOSwZcpH/lxhaZHjFC8XSNsTX5az5YBxlwT0Y+jAt28X dwobtgAZf9ZxNwg6jYGPZz7Xjln9Ki7yrvpHsV0E78zOJy72RZx8O1xPQxIbHvPCgGW6 O8ZXhVuIHVzXjmY6sMRtK2TfLY7A+bIwB8XXs1ycSuEUHkpFEK3KPN2NvHIaEC/68ct2 KfTWWre8nw4UE8s7W0xtowDAJFTyZx4RjuxvU1ZP2i05bfBRnGF87LQAdXX7SIhiRbml dD/0xiw5BqpzYk+fyGGfNBvntADWIAB2Nn8u6yU3pfIOcTqzsryZzvYh/VXEE4ovhtSc +Ahg== X-Gm-Message-State: AOJu0YwbK2kh4GmvLTlRDvXwQTG2C6ZYokPpSogxZ3tFcD5xZfnEVvNh 8bi6RkXZPPPj/eN1gSRxTY/aG7r7fV8Rm8gtzOEgmKBBOM2Q07mKKVW58g== X-Google-Smtp-Source: AGHT+IFJmX4GGhdtvZJs8ItRxeeTrOdyyAooH2GE3Fy6oyarkGCwZlktl6YEUjvyzwlefXk7LDWn9Q== X-Received: by 2002:a05:6870:e40b:b0:22e:c8ea:ca06 with SMTP id n11-20020a056870e40b00b0022ec8eaca06mr4444199oag.17.1712956108336; Fri, 12 Apr 2024 14:08:28 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:a1a1:7d97:cada:fa46]) by smtp.gmail.com with ESMTPSA id pk22-20020a056871d21600b002334685aedbsm1015117oac.11.2024.04.12.14.08.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Apr 2024 14:08: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 v2 09/11] selftests/bpf: Test global kptr arrays. Date: Fri, 12 Apr 2024 14:08:12 -0700 Message-Id: <20240412210814.603377-10-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240412210814.603377-1-thinker.li@gmail.com> References: <20240412210814.603377-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. 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 | 3 + .../selftests/bpf/progs/cpumask_success.c | 147 ++++++++++++++++++ 2 files changed, 150 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/cpumask.c b/tools/testing/selftests/bpf/prog_tests/cpumask.c index ecf89df78109..bba601e235f6 100644 --- a/tools/testing/selftests/bpf/prog_tests/cpumask.c +++ b/tools/testing/selftests/bpf/prog_tests/cpumask.c @@ -18,6 +18,9 @@ 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_cpumask_weight", }; diff --git a/tools/testing/selftests/bpf/progs/cpumask_success.c b/tools/testing/selftests/bpf/progs/cpumask_success.c index 7a1e64c6c065..9d76d85680d7 100644 --- a/tools/testing/selftests/bpf/progs/cpumask_success.c +++ b/tools/testing/selftests/bpf/progs/cpumask_success.c @@ -11,6 +11,9 @@ char _license[] SEC("license") = "GPL"; int pid, nr_cpus; +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]; static bool is_test_task(void) { @@ -460,6 +463,150 @@ 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; +} + +SEC("tp_btf/task_newtask") +int BPF_PROG(test_global_mask_array_rcu, struct task_struct *task, u64 clone_flags) +{ + 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(&global_mask_array[0], local); + if (local) { + err = 1; + goto err_exit; + } + + /* global_mask_array => [, NULL] */ + if (!global_mask_array[0] || global_mask_array[1]) { + err = 2; + goto err_exit; + } + + local = create_cpumask(); + if (!local) { + err = 9; + goto err_exit; + } + + local = bpf_kptr_xchg(&global_mask_array[1], local); + if (local) { + err = 10; + goto err_exit; + } + + /* global_mask_array => [, ] */ + if (!global_mask_array[0] || !global_mask_array[1] || + global_mask_array[0] == global_mask_array[1]) { + 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_l2_rcu, struct task_struct *task, u64 clone_flags) +{ + 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(&global_mask_array_l2[0][0], local); + if (local) { + err = 1; + goto err_exit; + } + + /* global_mask_array => [[], [NULL]] */ + if (!global_mask_array_l2[0][0] || global_mask_array_l2[1][0]) { + err = 2; + goto err_exit; + } + + local = create_cpumask(); + if (!local) { + err = 9; + goto err_exit; + } + + local = bpf_kptr_xchg(&global_mask_array_l2[1][0], local); + if (local) { + err = 10; + goto err_exit; + } + + /* global_mask_array => [[], []] */ + if (!global_mask_array_l2[0][0] || !global_mask_array_l2[1][0] || + global_mask_array_l2[0][0] == global_mask_array_l2[1][0]) { + 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_cpumask_weight, struct task_struct *task, u64 clone_flags) { From patchwork Fri Apr 12 21:08: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: 13628332 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-oa1-f52.google.com (mail-oa1-f52.google.com [209.85.160.52]) (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 7CECB155337 for ; Fri, 12 Apr 2024 21:08:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712956111; cv=none; b=IGQpwcUKkzNVggma414Q5tdzkgu9omMN5vyEGlnFFtu/ISZzDWL6zb7i+XGqLG4PqJ7zuygpsnNfk/HEYBay1MTxQvBGuiAy06+6t5CQL4SHChJmQP53KYad232Uz6bCpEnwKGK6aUuP8QJGIMWm32wYAqT6Pe0pyxNtoM6Rplo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712956111; c=relaxed/simple; bh=XCOrX2UCM/jR2fC0SWffLjWE7UO0dOeu2CDXbUUZo2M=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=k5FFKgf933LxeGBZ4DCzZMizfICA5CUS+aw6aEdFhECo9HjoHdPgb5wmou3ruGKJXwDXYXyA7GIyrlK5sjZtrsCGRYNZNCm4ZBnBlh4s1W5YMHWxztD8Go9nPYB9GoMLMcxmhkkqW7ZpO9HwXim+ya6UIOLuVuZpAA6VGP2MNF8= 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=FYrLXuvv; arc=none smtp.client-ip=209.85.160.52 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="FYrLXuvv" Received: by mail-oa1-f52.google.com with SMTP id 586e51a60fabf-22f32226947so777060fac.2 for ; Fri, 12 Apr 2024 14:08:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712956109; x=1713560909; 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=f51USvNE8y445K4bfiKnnRxSA5H877NAgdsZGb73FMA=; b=FYrLXuvvCb5Arnjf5EcIPTkF1BUEerdFJlIf8njjeJXzuwcorIcpmKanlIzT6XD5dO MsuMHLpzumbYQhWBOFbOVJK6TPPbXbAGgxdFhzrb/CV5AeSmrPCHZZz2vCcXi8R4wlm/ 6ekDzbFBpmTE29ZlckIGouj8LoZehA9yME9+g9oAcG3i/we0y44HQ4yvVRIocHimzPrx +TY8UbYtLYtGLoTchY1iwG6RrrhAD7rauzwOhbpMnfr3on1ydBepuXupCmVbJh+Iwp6D iLvPEYfLkgohu9HXiwX3U1Srhdu4LkehlKN7bJwtQSTNr521bRgmuixM6X5ZmBd1XISi aFJw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712956109; x=1713560909; 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=f51USvNE8y445K4bfiKnnRxSA5H877NAgdsZGb73FMA=; b=jy3j1psjGBSl0se2IwAkbRH3qhHOw2aZAMWEsFaG7442vXTW76uW3bYcBo/qRpnNa6 ufz8PgihxUrMY0n7lHB2Lmx9DmrjSEXFEizcCoXz8D8XfAodk93FESaXFvGFDzhxfA/m 87InudCNKJl7dvwHlxr2EAOhmSPao+ZpZo2dt4DWxd4h4ExrE4iLpWY465EgW4fggIUb d+gfmRxvveHV3+KCzjs9OAHgspn9MQchTsx4QyLJ8j8V2HSSZdQTlhc34WvKI5qcQz2Q hAKs/9U/z+ZFKau1RuyICOiiY83EqesspdW8+6lgi0hwiTTIJDh6ngulzpqAqb99sRjv M/3Q== X-Gm-Message-State: AOJu0YzZsaKU0Ep6EEFtS066skvxjmsnk1obKvwD7ov421TFZAeUfjLA 6y0H01adkfFnw9/qlWS6HtxIqZxeTj5elQMzYfllienncGPIEgH56fBodA== X-Google-Smtp-Source: AGHT+IGNw+Dgl/KiSIoJrpdmd8TTaVCci7JQ6e5Kc+9Q7EjW5TJY6/bggY/EIR5PvgmOC8fw1PsKrg== X-Received: by 2002:a05:6870:390c:b0:222:4cb3:4e79 with SMTP id b12-20020a056870390c00b002224cb34e79mr4057449oap.12.1712956109379; Fri, 12 Apr 2024 14:08:29 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:a1a1:7d97:cada:fa46]) by smtp.gmail.com with ESMTPSA id pk22-20020a056871d21600b002334685aedbsm1015117oac.11.2024.04.12.14.08.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Apr 2024 14:08: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 v2 10/11] selftests/bpf: Test global bpf_rb_root arrays. Date: Fri, 12 Apr 2024 14:08:13 -0700 Message-Id: <20240412210814.603377-11-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240412210814.603377-1-thinker.li@gmail.com> References: <20240412210814.603377-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 work correctly. Signed-off-by: Kui-Feng Lee --- .../testing/selftests/bpf/prog_tests/rbtree.c | 23 +++++++ tools/testing/selftests/bpf/progs/rbtree.c | 63 +++++++++++++++++++ 2 files changed, 86 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/rbtree.c b/tools/testing/selftests/bpf/prog_tests/rbtree.c index e9300c96607d..399d61b8a9a4 100644 --- a/tools/testing/selftests/bpf/prog_tests/rbtree.c +++ b/tools/testing/selftests/bpf/prog_tests/rbtree.c @@ -53,6 +53,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, @@ -106,6 +127,8 @@ void test_rbtree_success(void) test_rbtree_add_nodes(); 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..6bf7285944d6 100644 --- a/tools/testing/selftests/bpf/progs/rbtree.c +++ b/tools/testing/selftests/bpf/progs/rbtree.c @@ -20,6 +20,8 @@ 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); static bool less(struct bpf_rb_node *a, const struct bpf_rb_node *b) { @@ -109,6 +111,67 @@ 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) + return 2; + if (k3 != 4) + return 3; + + 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 Fri Apr 12 21:08: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: 13628333 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-oa1-f48.google.com (mail-oa1-f48.google.com [209.85.160.48]) (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 92B151514E3 for ; Fri, 12 Apr 2024 21:08:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.48 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712956112; cv=none; b=ioa0XaTOVD92u0UjTgVBrB12E81H2R3rydeE/9jd2sl3mLKLP2OePexnlJqpbhqo1vZbHl2Tu3SaQst3G3yqkYnbkfWVRbzZ1Ctp6RDYWAf2C+AvGwwGs3GI7I4E6CwpNM6HKae50mtv0NP0GBx7/PNK1MYNKtgF0VKFNhxuHhU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712956112; c=relaxed/simple; bh=WqjYgyHey8ZaGJ0KCr/4HIlllNyG29jbp0/EBTx83R0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=WioVZWtvrQJdWl0t5RLjMBpAFVeePNf/8r5WbAbuKd/WVMLQ6XGlVjGvx0QAMd2nvUOBvbBVPyFw8JgUCSsN0hbovKrMt0ZCbOjvFhAp5kNkgM58JnSt5gZAGvOt9eHr5BcYYmFZnL97UVVJP++JcjZhOvYFHzjCz1lPH6uOz3k= 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=QD1vY0BI; arc=none smtp.client-ip=209.85.160.48 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="QD1vY0BI" Received: by mail-oa1-f48.google.com with SMTP id 586e51a60fabf-22f746c56a2so636487fac.0 for ; Fri, 12 Apr 2024 14:08:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712956110; x=1713560910; 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=A0q/ZdC0G1jUqK369GNantm3vlcpUDTGm2krIxfZLzk=; b=QD1vY0BI2kCH/Sd/mp2Eh2UkOhbtUOuEHtlcjqPheQoLrVO2Sz5aGMIGaJxhMlEsq9 VWA1kDstlF+W9rC77y48mVE/VqBTBkzVdXI2NtkjW9QIE7lMRYl3TFQldHsh+jio+ecy a+ooVxkrtesueP2OTSe+24wHkU6rHgkt9T5w9MINQfig67RPkQta6QqMX00oYCOCj/yv vwSsAQcO9b0jKXLAnupmS9jUJVkLQpJVbfIXefdaMKVhD1saaVdhn4JCWJZzTsyhN+cn Yro4+msL/DQC0mX9fnzVn0Q2FWT2mf73I2RhQ+B+JGK9DXAcBb3PI4HEtkqgRgl2VHKO lUKg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712956110; x=1713560910; 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=A0q/ZdC0G1jUqK369GNantm3vlcpUDTGm2krIxfZLzk=; b=cOhIZOEhds5sBOGyQkokvLMsQZ0mNK3MnVYo8ycJI9parEWNRmr0JeCCGx0UqRaF6b MXbbQXxSTdIjSpRbK+qQejcyrcGtB7IKda6okvXK5VK8XqrUm5u82amHEiZHiEdQjJyl 787JsPoj+/p8z+fK+m7O6Ij8Nre98ds1IFXZYS4VxGAlJf9t03rbWVXcHMjRrwJnGw/O xvGUIXSXslHXRPy/UbrajDz+JEQJC0qzhDF0PvnWBvm1wTQPsCYVD7P5m911Jf6y5gmR s7RPnowLU9zR6xGga/8mJPmk5WlBvjQcXIZGERwDv6SoEAYiODawCFzxgnCCqrHqUi8X 6Nzg== X-Gm-Message-State: AOJu0YxXtPZvN0KfBNDxjf1BXO1YsCdAyBsw7Ld7UOTqpgYpFRVEbZAX b7fkmMr364FlYb5NQKGecf2mPGi3e7r3zVJJRFl4jVfl/xTL82h3otelfw== X-Google-Smtp-Source: AGHT+IHYpfXIb6pRPPbxLB+uBcpqQIeXmYipspbOInfWVzjG36yvtZv1Io3uMvw3jxJCEICwxLGc9A== X-Received: by 2002:a05:6871:a583:b0:232:f991:8fc4 with SMTP id wd3-20020a056871a58300b00232f9918fc4mr4193406oab.46.1712956110467; Fri, 12 Apr 2024 14:08:30 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:a1a1:7d97:cada:fa46]) by smtp.gmail.com with ESMTPSA id pk22-20020a056871d21600b002334685aedbsm1015117oac.11.2024.04.12.14.08.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Apr 2024 14:08:30 -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 v2 11/11] selftests/bpf: Test global bpf_list_head arrays. Date: Fri, 12 Apr 2024 14:08:14 -0700 Message-Id: <20240412210814.603377-12-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240412210814.603377-1-thinker.li@gmail.com> References: <20240412210814.603377-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_head(s) work correctly. Signed-off-by: Kui-Feng Lee --- .../selftests/bpf/prog_tests/linked_list.c | 6 +++++ .../testing/selftests/bpf/progs/linked_list.c | 24 +++++++++++++++++++ 2 files changed, 30 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..0f0ccee688c5 100644 --- a/tools/testing/selftests/bpf/prog_tests/linked_list.c +++ b/tools/testing/selftests/bpf/prog_tests/linked_list.c @@ -183,6 +183,12 @@ 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_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..7c203b5367a7 100644 --- a/tools/testing/selftests/bpf/progs/linked_list.c +++ b/tools/testing/selftests/bpf/progs/linked_list.c @@ -11,6 +11,10 @@ #include "linked_list.h" +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); + static __always_inline int list_push_pop(struct bpf_spin_lock *lock, struct bpf_list_head *head, bool leave_in_map) { @@ -309,6 +313,26 @@ int global_list_push_pop(void *ctx) return test_list_push_pop(&glock, &ghead); } +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) {