From patchwork Wed Apr 10 00:41:40 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: 13623327 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-oo1-f51.google.com (mail-oo1-f51.google.com [209.85.161.51]) (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 71F00387 for ; Wed, 10 Apr 2024 00:41:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.161.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712709717; cv=none; b=bugZCZkKvFEAG9nBDA+QRlQ3zZ5SNdGTVeIHXTN4TEkKJ72qhJfAfx+kGMS1Um6seHPZXQR0naS3qVG2ZUdB0PAalovsLyJ9Mti/GAVWCWmIuWdDsM4JB+kYHVY23pdNQdALnqVO2HPZ7st1vDn0tS0wOto8K6KZdbRGYs+a3l4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712709717; c=relaxed/simple; bh=Yi0PF2Xp4EiAjyXNPIst0YEunHuP+bb4DkipOWjFu/8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=REHVGRU1wHqBl5GGCJUYN5K30wJM0MAQPSWidQ79N8MuN4cE3jvpp+3nDgqCHhw1n7/8qHU+bej5IWX9QUB2QYm5xQ5P4hOn0GOtsIn79sUF+3IiNdnnKH4uyIzFKQa7YFjfRL7DU+WL8zfpp76MdhbB1C5vf66aiRjKkN0yF4c= 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=VvVDvELW; arc=none smtp.client-ip=209.85.161.51 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="VvVDvELW" Received: by mail-oo1-f51.google.com with SMTP id 006d021491bc7-5aa22ebd048so2784903eaf.1 for ; Tue, 09 Apr 2024 17:41:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712709715; x=1713314515; 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=rwcJqrsJ+5ydXSsOGrm2hlF/YDtofk5BUbh6bX9mJA8=; b=VvVDvELWTSutaVSYkcH07ZYBShAH0/8AiNuexrqVNy4P96YLvBNHjNUnGYZgXWgTNJ joCI8qnAddVn+J7EdAY80wEVJEFomfKlXhBwNJmI73O1HxkQRUyD7JYHzg6+Xsx00Leo Ltg5MjPPwagxw015MWwZI88QEmEcYcqa649Sjf59iNChVWyi9kGAoF641qGdIBuqTnYl K/waA9tyPOv1ViCmWZb9VXo4iXinjJmwP9FVGNIZOGv7yHzIt6aPxP5AwzTq87ABAot+ U3kr02N59eQCzcmZvlC3VI8bGI2u3DSvKx8fnDwF7I2jzmotXD6dsHvHHx8YgeG/bGAR UwjA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712709715; x=1713314515; 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=rwcJqrsJ+5ydXSsOGrm2hlF/YDtofk5BUbh6bX9mJA8=; b=b2UYzn0HLGNmxqY4OlgXUpcb96xIa03by8KhV0b2FO+MA1kr+J83fAEmLbrAYHA1p2 4zGo9J9aDsCx2BRjvH+7Wgs3EU9ulI69QDCo51+yjG0Wma6dzN6ZFzy8S+kAOiC7R1Yt Eg+eXd6nKdp4VzAJr5BZNhOm+h9EWX6VB5b4j8GSjeoHnG/K87GWsfU6RZyT6TVZi/mr 9OBknJbOkMqMiONGIc6TKUKpg1ZMVjRShH4Eq2Fau/ZmfDj0az76h3FEzAbxCnKageVc f5UB0e86iNFiiY2PWM9AsFvyxDcDrzxPWyieEtJGvzG/CTenJFzUog9QFtMnOxchL+te NbKA== X-Gm-Message-State: AOJu0YzsZGjpMzUxHg5SjFDvCczixhiUB6NJD1ZOzsZFId/jWpHQq/JY Nj8xcZRl7x2Lv8AxksWDFJnDWx4lr90U5MftnDTqMk8mOkA7NjBp8JdYLoE6 X-Google-Smtp-Source: AGHT+IFqKDvPeqmDYoKYwjPPftep1kf5gbRW7s2QYBu1o7fCa8hYs2Lxvr9yc3qzSqZB8DJZk1SdHw== X-Received: by 2002:a05:6808:638d:b0:3c5:ee83:5205 with SMTP id ec13-20020a056808638d00b003c5ee835205mr1129668oib.37.1712709715036; Tue, 09 Apr 2024 17:41:55 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:d330:d0dc:41bd:be5b]) by smtp.gmail.com with ESMTPSA id bf10-20020a056808190a00b003c5fbfe3ac3sm505124oib.21.2024.04.09.17.41.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Apr 2024 17:41:54 -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 01/11] bpf: Remove unnecessary checks on the offset of btf_field. Date: Tue, 9 Apr 2024 17:41:40 -0700 Message-Id: <20240410004150.2917641-2-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240410004150.2917641-1-thinker.li@gmail.com> References: <20240410004150.2917641-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. Signed-off-by: Kui-Feng Lee Acked-by: Eduard Zingerman --- 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 Wed Apr 10 00:41:41 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: 13623328 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 5FAAD38F for ; Wed, 10 Apr 2024 00:41:57 +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=1712709718; cv=none; b=fRDmMvpw3Gq/l7yh/03bTfhgUvtbuJh635GLjUq7N3fHVI5X0xHg3dDxQdCGKddjp7gM1MLbYI9Rl+hled7lNXD3lmX1ZAl7iOo2C5gfRtFnlYvmlmEj72jFcdc6VR/KnbSqAI/hhJsLT3tNs/kLkbQEbHDdV87oQwFbi0KsT/4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712709718; c=relaxed/simple; bh=ez44W3aeluU84egFnXW3H2uRN0yUjOERuV+O0SQ+Z7I=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ofyhvqD7VhmhfFbgABCz2QgQI31kBDVa6HdJmFcP017in3h5jqvW+ggNqrP4sIn59q5IRLRDJzfomZEtdnN3cVzcy9tsfLOwlE/RT2LDfBUWAczm+tGG0HsAVqRs6NmkDW+U0qDoimxrvpLkCXULAmYVsyDJYNhPBhIeMT1fcrk= 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=cLu7jKU9; 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="cLu7jKU9" Received: by mail-oi1-f177.google.com with SMTP id 5614622812f47-3c5d9bec755so3021967b6e.2 for ; Tue, 09 Apr 2024 17:41:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712709716; x=1713314516; 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=7y04/wrtZsljnjAE2JVt4GSsZDYcg6TqZhVLlNiRQsU=; b=cLu7jKU9ZWMAwEFPZULF5EJzPgGPe4k52Pukl1AvyufHNZ9N/1c/4iUM+dFhu2sw75 eRv4uHamckEi1ppq3h43rQf6byDKVcsCcgJjZNHLJB2GpmistHxEQrhdtDOoPLKp7DKr 0ygMYKRu0nmUuLks5EX+BqWT6lekC0Pl0IF9gNnFzPIi1+kS5pMUwP2EWbEAxT30JRa8 iUOBHD/cmrNQHDGgn+iYN9m1DeTnMCo4FTPvZqn6BHY7qArXHHzVFfsavKFwxIzgIgDf QP2YUtHHeqJmWtD1lbS64u1FhbGgE0WbOvbT0eM4S6dcucriDRyXJQ7mQ/obfpVkY1M3 gMJg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712709716; x=1713314516; 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=7y04/wrtZsljnjAE2JVt4GSsZDYcg6TqZhVLlNiRQsU=; b=YxzCL7O+8O0hRzcdOYtAAg9w51KwiRTVXKd21I4mcsANudhwJu9MEwpL0rrIfVEUEs DbM9JbubM9YdE7enGij+Ka6Jp9MKyFwPbwvla3JAvqGBUxCO9J6VMepoKxp8vkieirq7 bRuJ7aNWiHhnVdCDhmC8O5NYtPaEpYAfczqcvZbiN+xLNUfjNIrtyCzajwCtiO9tENay UCJMq/wfmq1cVT1y3qcjFFayxN1yThJUNf2RBuKV6UgJaj2rMKXAAppG965qHT3+CJXQ xSbQ3paOMqgUBAgj8N3Suu1V/hsQtLgH+g1nNW1aoJWkDlDxlox0+esSgrpZY+KWc52U kkFA== X-Gm-Message-State: AOJu0YwYlr0p9zmWty6v0TcUpbnPV5wSLxu8divQzEmk6EYsUZZYLPPS Giunxxgdd89tOto9eErT086nCN8VwmXZg3IS1cawimzKbnqJRIfDKmc00ss6 X-Google-Smtp-Source: AGHT+IH5n16Ri1UjUsMYwlttYgxM6aix9E0MpOXd5zkDv28LHe15W8haqxoc6+v5fc53Rt40dOdlXg== X-Received: by 2002:a05:6808:1144:b0:3c1:e8d4:e16f with SMTP id u4-20020a056808114400b003c1e8d4e16fmr1474891oiu.24.1712709716069; Tue, 09 Apr 2024 17:41:56 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:d330:d0dc:41bd:be5b]) by smtp.gmail.com with ESMTPSA id bf10-20020a056808190a00b003c5fbfe3ac3sm505124oib.21.2024.04.09.17.41.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Apr 2024 17:41:55 -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 02/11] bpf: Remove unnecessary call to btf_field_type_size(). Date: Tue, 9 Apr 2024 17:41:41 -0700 Message-Id: <20240410004150.2917641-3-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240410004150.2917641-1-thinker.li@gmail.com> References: <20240410004150.2917641-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. Signed-off-by: Kui-Feng Lee Acked-by: Eduard Zingerman --- 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 Wed Apr 10 00:41:42 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: 13623329 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-oi1-f180.google.com (mail-oi1-f180.google.com [209.85.167.180]) (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 6345364A for ; Wed, 10 Apr 2024 00:41:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712709719; cv=none; b=tjsFUfVm9exCjVPw9t3mjalzqvlgxtpaZJZd+ugqDS9p7LcSUgBt//IR8YcpmQSb2rDQ53sVlSgE5thOLeNreymx+L3Jrqg1VDRQAKm9Zqw9hMCqjeHepQS7DJkedOxKoVi8B0hxrf2/Q/RQN3jTXT0TvyCl9bcvOYiULHmHKH8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712709719; c=relaxed/simple; bh=vJnGMV9m02pWXOm3WPiZ6qA+dpqSfgOz+UF6/SJT8dQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=AqCnluCDVlcYmnV+UJy8BJh/HkglIv0LuIuGPds7PoYm69efYNvXfIWCZddIR3j/mzX7U7iX+R0G0NZO8yvcpvV6KodXLJEycp7AwP/kuzTPrWVoMCCNe2OLHwXuZnN4d7cAsFnex3TD94ijLlob3FbQnIuv8zO5YQLuXuTtXj8= 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=mC5KmXT+; arc=none smtp.client-ip=209.85.167.180 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="mC5KmXT+" Received: by mail-oi1-f180.google.com with SMTP id 5614622812f47-3c4f23d23d9so3690984b6e.3 for ; Tue, 09 Apr 2024 17:41:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712709717; x=1713314517; 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=5sTdDquWYFj1Nz0bN4Jj16pF/LNQ7Xc6jrYCY8FQPyg=; b=mC5KmXT+DW7SMOALdLVx9CWBbeVg3ie8cnqaWtRZGLwckvNcbRbMSx7YV9xjMwFYAs t5yDgbyvhBi42OVy2yqfDDmtCMPW6wOoN1YtAXrd9LEux9LbMMWt0AjVDPmw5M1vyx7s WlrFcwGrpCyuhnzltybUeeZOoKlXkwYZfJmLWXOxQvDu5fAS4kAdJ9JW/sxa3h9SsV0j m+Qha3bUoYYrMCq/EI1gPVj0IkykO715f3P6FRv601T6+fOnkSp6If76eA5qTOOJCaZO qzpSPWXu64JmVBjiNuvCPinirs1UKegTzuNPLE/ZnwmEp4qVoU8eir9sc75X3ecdvsm/ vVwQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712709717; x=1713314517; 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=5sTdDquWYFj1Nz0bN4Jj16pF/LNQ7Xc6jrYCY8FQPyg=; b=GGxyr9QnSm1+oUs3jV1YxnvBOJPzhDAivj3rLyj4zDjJv1IXE50vkaH0flsaDWcpH4 Mn7/6UzjAajV30BySOnRvdrw8WoMNN4aaUezBjhJViUSGwBDQ3ZMbQvueB4V0YrlT661 7dLpaO/lf1xy8uKGIpFV9MJlforZBTop6URU52tSr9Lq4W+YlZAVgpjAdUcZjMN6XMCA GiwnDIxRtFaaQrIsNojEOXp660fhoXFFpSN/voaMVwyYynhAG5jIMLzHeaP7B3Q3xZIY 0JlWOGRH6fJZ6Kut61HuWZt1tT/Ww50BeMMUDKBByhth1XGwukDm+i7ziUIDcT29dCLW dFxA== X-Gm-Message-State: AOJu0YyfQEIxEo3iW97LIbref6Wv+Qmz8lg2/NDT65LlhNPgUbQOwAv+ UM5PbMXr1ZhjP64duwSDnq6dP8F1nqi7xjjdpaN3iUjJ90+ac2vkArEcsdLK X-Google-Smtp-Source: AGHT+IEtcoRuNW2deHaPI8yW2nxZHv8KjsDTUko7rEaQpqspEwdcZCSkKGjUT8BNS9uW6TBeqNWL+g== X-Received: by 2002:a05:6808:2a5c:b0:3c3:c0e5:8d47 with SMTP id fa28-20020a0568082a5c00b003c3c0e58d47mr1166453oib.31.1712709717153; Tue, 09 Apr 2024 17:41:57 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:d330:d0dc:41bd:be5b]) by smtp.gmail.com with ESMTPSA id bf10-20020a056808190a00b003c5fbfe3ac3sm505124oib.21.2024.04.09.17.41.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Apr 2024 17:41:56 -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 03/11] bpf: Add nelems to struct btf_field_info and btf_field. Date: Tue, 9 Apr 2024 17:41:42 -0700 Message-Id: <20240410004150.2917641-4-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240410004150.2917641-1-thinker.li@gmail.com> References: <20240410004150.2917641-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 | 25 ++++++++++++++++++++++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 62762390c93d..f397ccdc6d4b 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..831073285ef2 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,12 @@ 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: + if (nelems != 1) + continue; + fallthrough; 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) @@ -3615,7 +3633,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 +3852,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 Wed Apr 10 00:41:43 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: 13623330 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 62931EC2 for ; Wed, 10 Apr 2024 00:41:59 +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=1712709720; cv=none; b=o/G3sb0NPktT0ZHlSvvKyFO9hwygrc9Xrlz/FHNys08+fsH2f2pCRRPTYer1CVs7Tp2CImMDy/wtyI/19jS7XTYgUbgv/UBje/1eKtUtBBOBmrU58ucCJYrweUzuRCqeJk1YUTk+7b4WXhQtvFj2KaNt2m9eVn199RuhgPIvj9k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712709720; c=relaxed/simple; bh=phr5fzU13/yD+OD2OqdLQym/DIXjKDKCgTubLtxOQj4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=iPkMddl/kLc9wLtJj41IFk0saDQgeQQeB04AJdj+2bVCuNKI91h4gBeoILQmJxaO/FF0moYfFOxK5v5tsfLwzoAyqCgozzDti0jjEPRBSI07+cmtII73XejZ179kW550Fk7lGtTV4gRutLThZue43kUv3eIy8BYE2u3T//wL6cw= 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=RrKoFGM9; 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="RrKoFGM9" Received: by mail-oi1-f172.google.com with SMTP id 5614622812f47-3c3aeef1385so3510565b6e.3 for ; Tue, 09 Apr 2024 17:41:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712709718; x=1713314518; 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=/FYAFbUvqMWEFSTQq3pdH/nIomxwhfiPGGthCbFBfE4=; b=RrKoFGM9LZG7kPtsegKaYbAxF+HGAtJhgdWYNXo8kvni/jNBiBQoxVbUlqgquTfSVR x3Rq/ZQbIiDJkFcbHdPRRgeN0HUy/BrW0Mi1zrKvjOMl7dCuHzRou9qWUH9qhsM3yXoe 5pwlVED1NSaJ8wfXNU9B8DxQYtX/SWgoOvYE4cjy0+RxMOFaNMNPA2SEYP7K2upa6YS0 xiLLlLITaZULU6vtuRWDjCVtcFPQbteJcbnlvcE8I8Fm7qIgoDuhIKKN5CzKz3+cpKbT LiR56vD5L3lHL4DKKLCZRiHtcxgsBBCXqWgjcrb/XbTxZt6ydLE4DBN6c8WzjcbB+b0r oYlg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712709718; x=1713314518; 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=/FYAFbUvqMWEFSTQq3pdH/nIomxwhfiPGGthCbFBfE4=; b=kB2aqrYfX8RwV4LDTnRLhuzNT00jqDSyP6HR29IT4dkR9MnQukzzcK8+rRq3atjlIV KYlNtp/CC3sFeMuLzQpQIjTc+Tu+4K4B9jXMGyzK+syUq2zMGkZwjpk0dwvcfQMvTenn GB2aEOoDnwfce6XBkm1t9uwymZcnJZ4bFt+tRQSwmV07ODkmv8wiMtLSfAtLvmJJwzOf hqU19E38OS+5Zh0X2TMKrs0tu3QHJ4vxwto4xQRCCZB9TfjFLRJqa6GLnZh+IOd3GG4P WQ3kPz8zCLqfauPtcAubai46YifCzHvTftNC+cGu8JoP+gD0KKUn3TYRE/XwUdVTYMi9 yUxA== X-Gm-Message-State: AOJu0YwECo3WIXQuLGYdSR9oN66vHxe8oeVyr/cUvVL9IGQDKuRHsVqR R/vCI8nPP0NfrT92/x2uXSWnyVvFCV5w/Z2RkG37Av9R1b3j9eakvnCB1GTM X-Google-Smtp-Source: AGHT+IH5puw4V85rCxgQpODJ0ep/jqvoMzwRF01l9v5PIqT5+yBLr1ce1mBotTeEGAi/I4ycoLKrLA== X-Received: by 2002:aca:1309:0:b0:3c5:e551:2770 with SMTP id e9-20020aca1309000000b003c5e5512770mr1045094oii.31.1712709718246; Tue, 09 Apr 2024 17:41:58 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:d330:d0dc:41bd:be5b]) by smtp.gmail.com with ESMTPSA id bf10-20020a056808190a00b003c5fbfe3ac3sm505124oib.21.2024.04.09.17.41.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Apr 2024 17:41:57 -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 04/11] bpf: check_map_kptr_access() compute the offset from the reg state. Date: Tue, 9 Apr 2024 17:41:43 -0700 Message-Id: <20240410004150.2917641-5-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240410004150.2917641-1-thinker.li@gmail.com> References: <20240410004150.2917641-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. Signed-off-by: Kui-Feng Lee Acked-by: Eduard Zingerman --- 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 86adacc5f76c..34e43220c6f0 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 { @@ -6781,7 +6783,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 Wed Apr 10 00:41:44 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: 13623331 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-ot1-f47.google.com (mail-ot1-f47.google.com [209.85.210.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 831BA15A4 for ; Wed, 10 Apr 2024 00:42:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712709722; cv=none; b=F+cl7+A0qRiFe02OpTkPivJdR5bMrmbHnVQdYeRdPIQEF/pE3oYobmstvV27noXW8bZ120NkXuTOMRZhLRJ8Z35VPLX1ot70qQIU0ES+x6MIoDUL264lnVgvpZK2wHk5a9If+GZTaS57THfq76fi4FSaKhZwejscbiCyrsqeDzI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712709722; c=relaxed/simple; bh=PQiaphJS8sGSmmNhxpua2lWHZd+JTm/QCi4UQVFxKFg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=YN2Z8gVNRWO8jK2pjh9/o4BlARNz1BHjGvdKmA09H1Q8DLZ2VOr17xcDOEjlLXVVT2+XqXd1IavBCm2T9Dhtk5kc95Fl8jZiPyaMjkwaa6c3lY0ajIm8633lwuGYaohWNFZhKER+zbREQBYcG2zTCx7w/zR8yf+QNMU/XyqbAPw= 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=MmRpo+xH; arc=none smtp.client-ip=209.85.210.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="MmRpo+xH" Received: by mail-ot1-f47.google.com with SMTP id 46e09a7af769-6ea29cf24c6so588760a34.3 for ; Tue, 09 Apr 2024 17:42:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712709719; x=1713314519; 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=pe+0CtEDtN75wD1CKRy11z0iaVOyi7eddI7dtEcIGAc=; b=MmRpo+xHEMvaPadmsyuYg7KlZG4SBen8so3sx4YoJb31dUNlEAKqMhwGwoo6dH5Zq+ fUO/N+piC+k0p4sE7WaQEM8tq1oWZOByQB/HLt0t1osyt48oPWiS4tzDo/EZagVy/sza kxTaoTHNeMwQN2zOO0eb1xpFnKiwz2tO/FCU+JhQ84ddCgazCh8OfqWA+x7C/Rbf3jPr xYC7Ihu3+14y6JEyQWK3CAegT3J4NmZC9ojkuNMGFfhbBJ3lpoYfkKmjjpxdGeOZj2sg 7H3/MeEvi8VwY/y3zltuOt/GB2BuYXSp7Hn9QEEj+nfJIi35Nsv0BsMGHxuh9Zi/pkst n08g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712709719; x=1713314519; 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=pe+0CtEDtN75wD1CKRy11z0iaVOyi7eddI7dtEcIGAc=; b=Zg/CvhCXx3T+vDfrS+DeRLebBYOQliAfRy+JBH6BiDKnMDxTWThV2hZeH2qDikPYI5 kaQqsyrB5kGD6fcgtqRNgs+7HjU1uUIgFNY0/14ow8mUNVXrZObuamuFaF2DsK5HKPuj g5uZaHdhU33PFL7ZWclkp1PzX6FXeb058UGpqmAcsZUUHJ0ZuJYSFG86TXblxFayOZlL QFgRZ1qJXjGMNfx0Jz9/sx0ijTzSkSfiUL1BFPf01eu53/uhyT8a5t+6SJAPpK1/AuQJ rBoKHmwpAC65gHtwhg1PCyJVzMP0IW7+Tt9narxD3mmkrCBJpAcO3TbYFIYnAENc5nxG dsSg== X-Gm-Message-State: AOJu0YzaktW9RI1VJx7Le5ksJg6vI/u9kHyRk174FVs8QaD1z6ruO8k+ 98TY0QO6LHsMfScnCjWw5qYYmTgnE9bvO5O4HkO21zaUjPNKwr8PcM3E3+sa X-Google-Smtp-Source: AGHT+IF+xTEkLU7qoDvOjQlEitZ6Gqck16Q9egHRXgwUpFfWodXr45ywiM2Vw4VmOCtqYIEPGoYRlQ== X-Received: by 2002:a05:6808:1da:b0:3c5:da62:f60b with SMTP id x26-20020a05680801da00b003c5da62f60bmr1135793oic.33.1712709719290; Tue, 09 Apr 2024 17:41:59 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:d330:d0dc:41bd:be5b]) by smtp.gmail.com with ESMTPSA id bf10-20020a056808190a00b003c5fbfe3ac3sm505124oib.21.2024.04.09.17.41.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Apr 2024 17:41:58 -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 05/11] bpf: initialize/free array of btf_field(s). Date: Tue, 9 Apr 2024 17:41:44 -0700 Message-Id: <20240410004150.2917641-6-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240410004150.2917641-1-thinker.li@gmail.com> References: <20240410004150.2917641-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 f397ccdc6d4b..ee53dcd14bd4 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 e44c276e8617..543ff0d944e8 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 Wed Apr 10 00:41:45 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: 13623332 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 551CC1C2D for ; Wed, 10 Apr 2024 00:42:01 +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=1712709722; cv=none; b=EZ0Gn29+cCVE1G3vJ+1nB8JthFZQKQCc0HbNSmo0ENivV9hYvnp91ILohpHUrl48lBczwrtNbyywxU39NoFOoXy5SbAXw04Ips4G9XENGfNTTwMrVVREKe5QQe48tZjGaacmSf+1KcZCyEaU9C0TIwLhXp4xM64k36ah2FtG7S4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712709722; c=relaxed/simple; bh=duJrRbrDbuvEkA+s0FVFEg8myFmF/kzDbiyzFeuepgM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=nEV3T3GZvkxh72EyKDmEvYcVh5JJ9VKq+jUzhdXIaZ4TlxtVNzCXz6iIUtB9fmv8lRdm9rZ01/JTMqLPRPQ1Np+y5IPMxuGGqwkCe4/9gAmjyxbW/s50/fDwgWJD5xqmj0z7rzlaER997/BeQvV0VvJql/IPwXQ18RLf21aJVrA= 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=dnFKG6WM; 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="dnFKG6WM" Received: by mail-ot1-f44.google.com with SMTP id 46e09a7af769-6ea260f51b8so814250a34.3 for ; Tue, 09 Apr 2024 17:42:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712709720; x=1713314520; 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=bF8KcRRkTQwJsCZ7FycwHfjPvjuQ14LUR/nslkAUd2o=; b=dnFKG6WMa/JZppoyl2n2Brd7sTPH6vNKOwfPXtWyDLiq67eqToOzSKu5lUXdZ7l7bl gyWL24WvGvpqGQqTEYNgE8bptGvaaQWvHnh1qvmQK74ZLI9u1S/JkKwVCi2nG3RviT3M IO3puOkGoLXOpQs6z601rGf8d0MdJzEdBanqYcYHZEw1jBXMeVC+r99od4lR6fpGST+s o/qyxG6FBPZxWBch1V1VUZ6uJCZBUaIWjVioW1ZyXGDmVzL50hOonttELTGdcJwxf1mN r1moRP4J6jhqu0jkiSGxCt1xEBRi3iZyLS5x270pVOOjQgarkIK0SNuK84PxbT42tZyQ p0LQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712709720; x=1713314520; 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=bF8KcRRkTQwJsCZ7FycwHfjPvjuQ14LUR/nslkAUd2o=; b=S34SH1ByeHGwE1UO05ueKkpEIctrLW281sJgtwMXvz+jBlIOq3T++yrA/nHrCCBlnk LtJjGht+uDH4xJJwcsAjmHHXwI3xfL/vfjSEr0AUKwSdaGjxS+Y0JMLNXq8jqnGxLqhP ZBS1BGexu4OaLkMXY0B8RzxKsQAPhBgbJaMQbzAvery+yGDGmdF3nsXCkg3/zEsL5pdF R8G0/7lhu2Wryb0w25voSdGsOupYKE9IaHmU+cPUI391KJAlQ5GA1dJFC6GOXTq2JfiW L8e11khGdKBNsSARpCvAHgkdSQcd6DNaaUwAzlTxKeDdJM8Y5RXnRDc1QJEG7UCkNFdU rU+g== X-Gm-Message-State: AOJu0YzPK1wepcbQRF435nEE9mus9imoWuldrDFJ2DZC2oVxv+QUmPlt bNJn9GQnOpvN8z4NAJH3r3FjhM6iOhudkuDAd6m71Gfz/COTouBDb/4BEvqh X-Google-Smtp-Source: AGHT+IFjedtfU0iz62u11MLt33WWqzh+27w2Lnn4fd56Lnj45EJJ3Hi6w4s/2/7L20ipjp7OYXW29A== X-Received: by 2002:a05:6808:20f:b0:3c5:f9eb:6b26 with SMTP id l15-20020a056808020f00b003c5f9eb6b26mr1160623oie.19.1712709720298; Tue, 09 Apr 2024 17:42:00 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:d330:d0dc:41bd:be5b]) by smtp.gmail.com with ESMTPSA id bf10-20020a056808190a00b003c5fbfe3ac3sm505124oib.21.2024.04.09.17.41.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Apr 2024 17:42:00 -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 06/11] bpf: Find btf_field with the knowledge of arrays. Date: Tue, 9 Apr 2024 17:41:45 -0700 Message-Id: <20240410004150.2917641-7-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240410004150.2917641-1-thinker.li@gmail.com> References: <20240410004150.2917641-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 btf_record_find() find the btf_field for an offset by comparing the offset with the offset of each element, rather than the offset of the entire array, if a btf_field represents an array. It is important to have support for btf_field arrays in the future. Signed-off-by: Kui-Feng Lee --- kernel/bpf/syscall.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 543ff0d944e8..1a37731e632a 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -516,11 +516,18 @@ 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; + if ((f1->offset - f2->offset) % (f2->size / f2->nelems)) + return gt; return 0; } @@ -528,10 +535,14 @@ 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; return field; From patchwork Wed Apr 10 00:41:46 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: 13623333 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-ot1-f42.google.com (mail-ot1-f42.google.com [209.85.210.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 9A15B10F1 for ; Wed, 10 Apr 2024 00:42:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712709724; cv=none; b=EkQ4ZQjSDR7Aex2ktv2tkO+8CyVnW4OG//6xXQaeIYhWa1xqpZ/ItwnEzVFQlPezSeJeY9WhOlzyL7wGOrMeZJo/fHBFvxHtWyTEk+jewtZycl7g+oNBTtHxREWuAcfnGW8AxbnBdqPdDPFswZNP5cpDPNwIXGTKk0p5nnINGgY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712709724; c=relaxed/simple; bh=kYrFWFA7WneZwDQwTjX3ZgpxnvRnZ9c+6S58UYo6m/I=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=tNBYC71Yy4tFta+gW+ynvz3ayEbAqY33YQRpsp6LBmQkd2TAGJSx+lXkHhyWupbSMFCZOLcaKgxZUeVy8N3W/h5L+H6A04+K6aM8JCnoLGQEdCnCXB32qW17fONGCg+NZu4IBWhZHS4EHO2fd+uiuaqpvIFVL6dzs6048MsmYzE= 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=AwIuzb4T; arc=none smtp.client-ip=209.85.210.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="AwIuzb4T" Received: by mail-ot1-f42.google.com with SMTP id 46e09a7af769-6ea0fd9ca89so1895605a34.3 for ; Tue, 09 Apr 2024 17:42:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712709721; x=1713314521; 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=4AqF5wMXF6A3WrrYWyUoEcyLqdmX7zmChx1pN+rJKiQ=; b=AwIuzb4TmPdv4Cp7wbMtPmF04MycQKCFYy9wOtrxBQEUrYaYdmzK2P5DGgvl28QIN9 qr7PrjYeImIRRyie39uYvIL8Fv8btCpQIyiUqM5/OhR68Ueoq2og2fEsSPS6W/ETD0w7 1ifIk4GBydAZesyia/YbppRT4eHV0591As6WPE17NQfpIdytmgIWWp9uHr9T/LJ2N6XV Zb+AEwhwXbr3lz3+TaHHqKG4c+1eDzz0O0pyhoqz1waGVk1wTXCzyerqKKhxWOwF0Ols eaip7qDsMANaQ+aZp/XbZguBEiqWxv45mmGY62fPOo1pOLRHTgTNMdVB2D+dMfjSMPu5 uGCQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712709721; x=1713314521; 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=4AqF5wMXF6A3WrrYWyUoEcyLqdmX7zmChx1pN+rJKiQ=; b=s+MmT6EJ/cioXlBeAgP0gyamSlATCc7yP07tR9ldghs5AtW+Wzwc2+san50c8InAVp Bnu4ec6Stmq8jRiqFz1I1WDyFrwD0mx8elM9VYzXoPLvg0vevhmRsvM3TjFbCdQUaUlq PplsEA5pQaUKAW/RbtVWFlZOmairqyNCmlWzCcYRySFxvEmBZaBxsDc0up5H6vEGlwnR A2nmzpRq2NepRJVzZRc14aSRNmoI1duLqJviRIclM7Ch2H4Gw4O3jRlIs7O1EJDjanX2 FlC6ADdXDLs6OVPf4YN6+1vGAKmhdxFob49RSPzH+1hUEQAilUrUAKNXS/Q7HR5tRwFn P8NA== X-Gm-Message-State: AOJu0YyfPgKsnSPUY8ln4iiTvmKxN+cyNbuzBapT6bk8wlMJdyl9LXd0 6+p2aPX68LNd1DH/rr1i+AELM00Y799McDaPJaKgCxg5GMMKJN6Eqk+IYWVs X-Google-Smtp-Source: AGHT+IE/AKpq5VO5YFQaddE5yZOpEH3kZrdSJEDele1gKiaZzBlvFVd/2H9Jp1x7b1CVEvtffKL/UQ== X-Received: by 2002:a05:6808:9af:b0:3c5:f100:d559 with SMTP id e15-20020a05680809af00b003c5f100d559mr1178607oig.10.1712709721253; Tue, 09 Apr 2024 17:42:01 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:d330:d0dc:41bd:be5b]) by smtp.gmail.com with ESMTPSA id bf10-20020a056808190a00b003c5fbfe3ac3sm505124oib.21.2024.04.09.17.42.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Apr 2024 17:42:01 -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 07/11] bpf: check_map_access() with the knowledge of arrays. Date: Tue, 9 Apr 2024 17:41:46 -0700 Message-Id: <20240410004150.2917641-8-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240410004150.2917641-1-thinker.li@gmail.com> References: <20240410004150.2917641-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. It would be necessary for an access to be aligned with the beginning of an array if we didn't make this change. Any access to elements other than the first one 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 34e43220c6f0..67b89d4ea1ba 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -5428,7 +5428,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), @@ -5448,7 +5448,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 Wed Apr 10 00:41:47 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: 13623334 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 879E44A12 for ; Wed, 10 Apr 2024 00:42:03 +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=1712709725; cv=none; b=oAEb+rRmmp/OaHZmCjw5wMVvP1pQUNGsASAojzr58b2zjXfozJiTUhD3/z85NsmDaFILkOvsXew8oKWIb3MobCmT8WYNOmjEVpqSY7/Sudeh/JAdy+eTkaoI3IUvoE1zAW43wTNYY/t+VZxdf9LQhRPvxpU7m3ZUIIDfrzoJPVg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712709725; c=relaxed/simple; bh=augahCc3Ewhq83kRindLxC6HqChOA1Yri6+rDpGyt6s=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=FLhIf6k1u14doVuyxfstnVmCq8mOSyxUOk9OjexAZv8OOEZMyTCMDjg18SUi8yaSN8XFfBs62gSZW+sTSquQSgUAU1uQppVKQwXwXzM2apCWxxLuC5VVIDse6ZlI4NmOdgxYL9rIEX6CY3WpBqijPWlKoLOa6wzKVDHGulECa/Y= 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=FsxKWJFu; 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="FsxKWJFu" Received: by mail-oi1-f181.google.com with SMTP id 5614622812f47-3c6088df378so138712b6e.1 for ; Tue, 09 Apr 2024 17:42:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712709722; x=1713314522; 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=jfJ7M5RkuH9cscJgQnxAJ7D57q0+30Pxa+NQ2gc1KoQ=; b=FsxKWJFuYCB4ASc6vsM9hA6dIJZlPSKfdHR4h6g9Vrto0H0/TCVQSnbTd39k8m8ZJz Oil83ihKkKAPuBMVdBxlPiIUDbR1JpA4U4bhDdQbSg+Nn1aogGSk/1Cmn+Kgz/cy63y2 qeSCrHl1dbZLdIUCDmtz3B85GLMcR+zk3q8rHaJu4p+Wk4mxeqQcqog/0lVKq0UsEPTA keoGPLT3Bti4I8NsUqNmzB+ggN4EjBB6sGOzT9atRJnW0CXmPJBIYtlLj0ktIN6CchJp Ug3pu0oICmLh/cW+hWOY+J8/tzY8XusEzFLUIFzWlo1foxOtN0IdwhC5TnsX3zqF5QL4 EtiA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712709722; x=1713314522; 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=jfJ7M5RkuH9cscJgQnxAJ7D57q0+30Pxa+NQ2gc1KoQ=; b=hLEjQVKGFFM/yyxGh9zrzDmeDxUK8BocQmASh28aWhW/2HTuF6Q5w5HwDRuNLtIMlw 6rZ5hYauv/UrRpOU0O7OvPW0GH8X/ketPVVwwIeZqB0r0Op+4xvZE8kR3ahUWN00VhLS G2FSIhwXaXhN6ycFtXO0zopWVVxKLVunqjJ4M53EtNvU38xuQWVDFhW6QlOn+YCw8D/J 3LYWX7gJEaTQJlgdPggWCROb5amritx56F7Ujv5HBD9ozKDZB2/Q20EKU87xVwhTYrew LbbgIysQDUrbrwAtbPEtKoTFgEFPNNgrQschuE08cV/6bKSMNKyMBJGCF/sVruCXy8dK cP2A== X-Gm-Message-State: AOJu0YzC6OAPmQcpjORFJQZYli3JWUvyH5SLzZXenUxd8WLQQXn3KfNw TQ22KMVcyMx2Ehqr1PjuF94vTLr1DuObS0MOPaU08lTVUt6TYHvGsaI/V81/ X-Google-Smtp-Source: AGHT+IHJHzwrtQhOEAbDvvz9wR6LtJeUsxcPiFSwhIvtuD98y64gN7OoHjZ82uBd4ETf3d0hkP1Elg== X-Received: by 2002:aca:2113:0:b0:3c6:a0b:929e with SMTP id 19-20020aca2113000000b003c60a0b929emr572219oiz.47.1712709722275; Tue, 09 Apr 2024 17:42:02 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:d330:d0dc:41bd:be5b]) by smtp.gmail.com with ESMTPSA id bf10-20020a056808190a00b003c5fbfe3ac3sm505124oib.21.2024.04.09.17.42.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Apr 2024 17:42:02 -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 08/11] bpf: Enable and verify btf_field arrays. Date: Tue, 9 Apr 2024 17:41:47 -0700 Message-Id: <20240410004150.2917641-9-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240410004150.2917641-1-thinker.li@gmail.com> References: <20240410004150.2917641-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 831073285ef2..d228360ee1c6 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -3836,7 +3836,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; @@ -3852,7 +3852,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 Wed Apr 10 00:41:48 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: 13623335 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-oi1-f169.google.com (mail-oi1-f169.google.com [209.85.167.169]) (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 881FC1C2D for ; Wed, 10 Apr 2024 00:42:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712709726; cv=none; b=NHXZhiQTMzPxUR8LXBWOkyL/2uORVB0wHNY64x93a5QUCtC0wmdoaM5r1cveE/4GBNUeexioxzO92quWCHEuIHuUxPSlnzNoruhKrKYzT6/psUHiT8p11vHuuT0kST6ZYj4o0OmghxLwruj8LWvJf8hEWUcIBV6iG94psxVQxXg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712709726; c=relaxed/simple; bh=DypjCVY43yhA5fiSymfCfD24cW9KiO9HvaU5WeMouKs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=mXFVxLfnNeljUN3B+Qgysk3PAUgFtJAS9jvEgIigzssMuxKrIK5gpMJTn0P++EnY/ozL8bo8Fb3MyhKMQxXgvX6gakZ3eKml/OJFe04yVXM7ptRJ+MgrsrHDywf05ptmZuCKBSz70U5O17exfxGF2AS1M9yO90HwfogTxygyzX4= 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=lV7CZFVC; arc=none smtp.client-ip=209.85.167.169 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="lV7CZFVC" Received: by mail-oi1-f169.google.com with SMTP id 5614622812f47-3c3d2d0e86dso2553056b6e.2 for ; Tue, 09 Apr 2024 17:42:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712709723; x=1713314523; 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=lV7CZFVCiNAdYYjw4SGXVN87bRs4wqrUeAhbKdtg71H/VHIQBmNcF1SyDEq4g6R/HG i5IDHZKrt5Av+qnwrG0LZnCT7rDjVNwUAC5FSCw48qy/Veojm88+Lt7L7W0uxZ5nB0nI tjTHjWy4gSeKW35dFHO5ah4USmBdB+2lNIpj7TNuYNA/qDKh89fTRaFRtFyXBeyTcfDh JkEEmDPddc4dfSXR556z6ukNX4BmmzuaEJAIEk2L1b1Dn0GQFv5CYNNvHtVNif5kp43Q ++wGg2kJCS8CPzh69f4+Bz/bn8oBKLKL60qd15eV11FIQGLiJKUXGhd3InLurnPog/pJ WnbA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712709723; x=1713314523; 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=BNe9sElzhsC4xaRxOvQ0f69tTQ6/x4SDQwXGRiSWk2X1td0h3QzeNE9a4pzJHljGbV DmXH8Y8aiKeIRCeAyyBL27AIbjdkPJComkW0MP3B9HhEhoz6FPFeisaZrASbhabnoiu/ S6Jg1q0jMtklkhUoutriH73CyHRWJW3KpQrOTZNeMKI/Ny0BJXz4+IohgDG95o9HlMoV AfoOBggilk1QiRdP4qJjwUE6OYcsyS4f1yBzFxBooE92A81RcGMvXEurYOFlsKI0/4wQ bl/Zn083CbJm5M/HMb+XGsJAibSk6ry6BcOckSRdLz2Df8yLFU4XGq4pp5k0d3zAhmAW 8Guw== X-Gm-Message-State: AOJu0YxuHuCqB7WqWfdEMxLyz7Ed0xHQM8Es4boiZLRIjTUFRrCwsLEC GZcVnaVYRo183hcGmAsSs07R8ak/KlQ7RY4QUVxTna3PnWImahhcQK5HaANc X-Google-Smtp-Source: AGHT+IGnVOHmpS5mgkpWD3BGDO34/rvCjq5eDlNltxWbIkW79D0e5R9dbgGW7qV9H3Bgvu07KNdkbA== X-Received: by 2002:a05:6808:144c:b0:3c6:78d:a5f0 with SMTP id x12-20020a056808144c00b003c6078da5f0mr1331929oiv.6.1712709723405; Tue, 09 Apr 2024 17:42:03 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:d330:d0dc:41bd:be5b]) by smtp.gmail.com with ESMTPSA id bf10-20020a056808190a00b003c5fbfe3ac3sm505124oib.21.2024.04.09.17.42.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Apr 2024 17:42:03 -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 09/11] selftests/bpf: Test global kptr arrays. Date: Tue, 9 Apr 2024 17:41:48 -0700 Message-Id: <20240410004150.2917641-10-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240410004150.2917641-1-thinker.li@gmail.com> References: <20240410004150.2917641-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 Wed Apr 10 00:41:49 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: 13623336 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-ot1-f46.google.com (mail-ot1-f46.google.com [209.85.210.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 839EA4A1B for ; Wed, 10 Apr 2024 00:42:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712709727; cv=none; b=tlyXDizTOsEjoCAQdWZbBbQXhQQ1go14z0zSk+mifNMQfnZHIxcZmwzt+GyiFPgMURiZ6orR7V0SlwL5QDAFBg4IuIuRrCZyaX/FUwjKYp/UCng1HXGPPyCtlqCbUDI8VPIFnSjmbXEpMYtBzidvBCxUByvmghFGVNTFrJBadjk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712709727; c=relaxed/simple; bh=XCOrX2UCM/jR2fC0SWffLjWE7UO0dOeu2CDXbUUZo2M=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=eo/JMrMQ9ethpewu8XLvtGVBzRrk8u8C16oqWbrpB7Nk62AHxKxE6Fd/vbIc4oR65jWBibte3IkALkQl/aoQ7KHj4cQW1oIUYHSny2N/wLhgdt2lOzhmQshAZh6amE4lJZT99Ns2ST9WfG1+S+sOdupAUGM2Xc1NHxKTqS8eP4I= 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=gZQmNVu7; arc=none smtp.client-ip=209.85.210.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="gZQmNVu7" Received: by mail-ot1-f46.google.com with SMTP id 46e09a7af769-6ea26393116so875946a34.0 for ; Tue, 09 Apr 2024 17:42:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712709724; x=1713314524; 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=gZQmNVu7mo4PX3op5KYgaqLLfrikhqjX5VdfrlvmJs6LDNN8qXU9ENvLGz6tO2pLEN ML7nM+FFFaHSOl1QHSMc3xmGaPiQL4F4ywYyOjwMXphKW7hHjLtnHoF+R4WZmdGLV+ZP QqxTHvdB7wne0s+ZZzxzauJKlMZ44r/rCvkrnd9mUH/kxNDYkj00WnEss8MIprXdxdGb g0huYWc40DwQ6M0s8bXkTQqTgkpvvVdiKZDyTuIGuMMsG67ExzBXIaFckrRRP+XgXhla ocheicN2lc/zWP0MJ+yNyS1gjiaFCT2VdxkNiDPGdPVOQSRhumOaCHddIkYvd2qfCpFj kSZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712709724; x=1713314524; 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=e4qAjy+fmECyXE5kkTDlVxcDHLLlLIZdjrH6AqVM1mScPpO3pJ85U4f0m0V6ytTpnE 1DgnDtAOwkz0551WjvmdOiPrSoqDf0jyEFTQ+35CNGSUjyEBr6/UEL7nEdLxvRIJUpay gs0jR7Aw29nfz6r2GWXLWpbC9Lnq3FAdwu1tWveU/fxE7SexBpZ5NUF7i16uzxetMk5e +RBei+8J/u+E83VkN7d/l38MFJfYNWwoQp1+BDDCByH4za+ZA9anS9dmDQj2+ny2BR73 R0YsugJfxFrEMNlNpumz8zlY8uDj3GaJLmSr3G7SUiNxnSEzqvHOpzfpDMtBSPjigEgK uUKA== X-Gm-Message-State: AOJu0YzV24WL1AJSSltzS0VEOPDU777GYrPVeM48NUdYrJR4ZAeBN1Zl 2Rig82Kva9KyH4e/zdXyqYaPR+UNr1GoY0YlitYhZQ3vRENYb6JlOwOdTMep X-Google-Smtp-Source: AGHT+IHxDT9CBUimrEpuY+ifeQ7oQlRowvqzccIPLM3eGPf8KAWS/kJRFqbR1bo7rBc8iqWe6c3zxA== X-Received: by 2002:a05:6808:18f:b0:3c5:fcfa:c268 with SMTP id w15-20020a056808018f00b003c5fcfac268mr1241716oic.29.1712709724438; Tue, 09 Apr 2024 17:42:04 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:d330:d0dc:41bd:be5b]) by smtp.gmail.com with ESMTPSA id bf10-20020a056808190a00b003c5fbfe3ac3sm505124oib.21.2024.04.09.17.42.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Apr 2024 17:42:04 -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 10/11] selftests/bpf: Test global bpf_rb_root arrays. Date: Tue, 9 Apr 2024 17:41:49 -0700 Message-Id: <20240410004150.2917641-11-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240410004150.2917641-1-thinker.li@gmail.com> References: <20240410004150.2917641-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 Wed Apr 10 00:41:50 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: 13623337 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-ot1-f51.google.com (mail-ot1-f51.google.com [209.85.210.51]) (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 C73324A12 for ; Wed, 10 Apr 2024 00:42:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712709728; cv=none; b=BUklloZwOvikHZlymS1+1Plwa6frsGa/5LdLNqkD8yeOFokXi+q/ELXkwY3g3mxGYGRZruiR7hIdYwP6xja9/8KXkF3QfgyL2i3IDYQ7+p2YWo2oVXYmo7Kzma7TQkS8rOXaL1HwhSbgdc6MZrzsCrGR+i6W0rnpKaIrhuvcvJU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712709728; c=relaxed/simple; bh=WqjYgyHey8ZaGJ0KCr/4HIlllNyG29jbp0/EBTx83R0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=SrwbR09vu8uoLy3ge2JSoMqqIWMOL7INlOJycrpdx0T1oUfhVxl+AGN88evspxFpYhIuKE8evR9ZFrpxLcH1TRN1LLKGHIhkb3WOlzbtqDLzVv1azt3lPioKqPuLcyZmnylArZdJQFbz9mVEPgqhGT9LSDXu9X+OwWam5BTe+Lc= 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=J4+fPJUX; arc=none smtp.client-ip=209.85.210.51 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="J4+fPJUX" Received: by mail-ot1-f51.google.com with SMTP id 46e09a7af769-6ea23720da9so750867a34.2 for ; Tue, 09 Apr 2024 17:42:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712709725; x=1713314525; 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=J4+fPJUXekrC57c+Ak0El/dcV2jy2OXO9/OidO7AG2vqMI/hX4sbMFr6ghDrVcQoFI mJ/RgfWofHkyE2inPwDNq7iw9mY6qnecpRDS83gRJjrGmuQ/HKbzyRhr79QgBBXDYD3J KM24VO4eszADjj6fVEbxGmbbwpXsQW/gEtPVJEz7OKHJRRsV4gYmTw2f+eQ/9ufGTuIp c5vWDhgbFhDB5Xwn+oDXTM4yD4pyDiaze2NJZuc8etl3MijX1maT+oPPLlqQUwM/AEIe RRkhm8ar+ib3MnZJgXzotFPT4TpHi3E55t1ZBxljco3dHM9IkQEzd66iXzDRol4CSW+y RBdg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712709725; x=1713314525; 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=UAMsddGf/xRtqWtq8niNAx9EYi8SW5WjxFzvmZQeUjYXAVkAC4dGCukzZwwPM2LI2g nGZ+eou2ritYRg+6LwjBhLLKEmKUIkHKC1ws6GucZcm5kLTSiZKOuvBjpQSElKrboXGt gw+ASQmsH6s9i6GBPgDkdLEzMpjCAUAHcA+raUrEKAh5ODpOWamEtJ43oJf6jqSPMP0C 4DJgmHmf7rUmt3dbLgmVagCDzcPX/+CII2IfXaM5dJnFzUsRIH48EsuhrOQZ/+WPz65F Ad2QtOBcnwhRNtJpSGLgL5kioMzxXK0szPzf7FXfn4epxrm3IexphjuT84nfJjK9p3Kn VbpQ== X-Gm-Message-State: AOJu0YzYZkTRTKnskbihoqGPVj74FlcdyuS7m+dMz44Pz1KFJb0IpmuP KwEQeg2dRLVXU9/Ttmdc1A5hgF/zv+xFKtNkUxYMKB4iUANWf8tk8es/GbkK X-Google-Smtp-Source: AGHT+IFYFs/0QuOmeCNIJvy3KxWbeiotDFof06gYvVkTk6NA6k9CpKpEnzF1QyAwULpO7VJKRMomDg== X-Received: by 2002:a05:6808:4b:b0:3c5:fca4:17a5 with SMTP id v11-20020a056808004b00b003c5fca417a5mr1070729oic.2.1712709725419; Tue, 09 Apr 2024 17:42:05 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:d330:d0dc:41bd:be5b]) by smtp.gmail.com with ESMTPSA id bf10-20020a056808190a00b003c5fbfe3ac3sm505124oib.21.2024.04.09.17.42.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Apr 2024 17:42:05 -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 11/11] selftests/bpf: Test global bpf_list_head arrays. Date: Tue, 9 Apr 2024 17:41:50 -0700 Message-Id: <20240410004150.2917641-12-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240410004150.2917641-1-thinker.li@gmail.com> References: <20240410004150.2917641-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) {