From patchwork Wed May 1 20:47:23 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: 13651119 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-ot1-f54.google.com (mail-ot1-f54.google.com [209.85.210.54]) (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 5832E1607BF for ; Wed, 1 May 2024 20:47:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714596462; cv=none; b=MpcilqP3vYVZd4cUFTYgBDVwVp7wwy5ZVbY8CnysHXsPCJGKE5lwF5p8uojXnONR8qTg7wClwWERT8ubgz2EDXLOYUzVCkIQK8+csWPlxmaKihI4D3x0asBa3YzBrGF0Swj9rj7ijUAcqzFObTkpXUeriHg1kT4tPFb9LuO7Vpk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714596462; c=relaxed/simple; bh=wh9ihHooBolmYD5Y0JBZxAzE+AhNKO2PCA+fwt2rxKo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=N4dfc3GiN0pRGKi7dipe8E/ktczCL/vt2LUZhhdqbqhkMD8iM5H9LZPsAWi9EdhYwvjsPBlUV0AJooTFlLnwo5xVxLVirkge6CnGFZXTskbBdMZo1Ra2K1rrW0W6C1dSCLzzxRJvOykN+7ckXxD3DsmXIaYb2OALHItC+IMmwKc= 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=WY80SS4m; arc=none smtp.client-ip=209.85.210.54 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="WY80SS4m" Received: by mail-ot1-f54.google.com with SMTP id 46e09a7af769-6ee4dcc4567so1889511a34.3 for ; Wed, 01 May 2024 13:47:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1714596460; x=1715201260; 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=YjHwE/uFFV7JIdhGZvBb7+FIgW4ZliZQGnsM23KacUk=; b=WY80SS4mk0hOyFEQfmSqXRpV/p/G4MAZyFy385+nQ+7lc8gaUO0FlOKC4JXvYi4zjW MM62hPygSPnVmpS9CaUkzYSxvyRSfBGoEhLpJv8cIDt7Gx8r2lfRTWRPK6fJw+g0JfSg iAaNCDkCL7ybtNfhLVbGx6qeymSYspwyqOaJaqoO8clrVUResNb08bgY8R6ku+0FUMsv YVgw/DTwl1ETzj0C/yOZzDONzLcuzoXsdFsnX+22Y3MgV0lmIyzEVoszc36nlM4Un6eU Yn6Tr+9uFNBA2ieRZspVji9E727ikEvV88OSmFDSOkpyaGOgEUHCk6X6IwLdMYUSVeek lZAw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714596460; x=1715201260; 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=YjHwE/uFFV7JIdhGZvBb7+FIgW4ZliZQGnsM23KacUk=; b=nRqykywOzBOivi6MEjzH0yzPleg8LY0t90YQp4qODRnGSJs+S22OVxu13xyy7NG/+Q LZZ7EDeYis8z3ggvFRi6rnNF5ipzAg8Vdso9td3HAC/CJmIaEdPbJTh/tCyvWJFIkkFA tdGcdpzlrTtImE2OYj1cjT3Iv93VFaP5B7GSaoE4SDx3HvVWJrl1XQyLO+krULzmXMQG 4YnYlKhESkRVGPTrHGCDWS18zXggwTX86qvkZlVrgH7PkYOOKH6GEkcIvfxozb+OL0vl j4JbP2bq435GoxiSDSUMMJjH542KR9INGcNmO+yJiNqhg5J8FMLWuFkyBGY2GXpOF/zg pSxQ== X-Gm-Message-State: AOJu0Yzk7KrD+HQIhQx+4ud42d/kFpEdrulhxV5KpsJ3SzZ0Xa2dtCMt HfOqX2x9VXqdEKrGSipoTJd5VKxuBauYaOrqKFdlBPUwdzbcH9h63kTkMw== X-Google-Smtp-Source: AGHT+IEHDgoeK9LSrPmMksqRWV4yGUH7qZhUiLVNYeLc8K2ko71uT1QFTXJYZgbL2eAAV7LLeK8AHQ== X-Received: by 2002:a05:6870:2006:b0:22a:5bae:9cd5 with SMTP id o6-20020a056870200600b0022a5bae9cd5mr4084319oab.48.1714596459776; Wed, 01 May 2024 13:47:39 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:22b9:2301:860f:eff6]) by smtp.gmail.com with ESMTPSA id rx17-20020a056871201100b002390714e903sm5744408oab.3.2024.05.01.13.47.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 May 2024 13:47:39 -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 v3 1/7] bpf: Remove unnecessary checks on the offset of btf_field. Date: Wed, 1 May 2024 13:47:23 -0700 Message-Id: <20240501204729.484085-2-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240501204729.484085-1-thinker.li@gmail.com> References: <20240501204729.484085-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 5d42db05315e..86d20433c10d 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -11638,7 +11638,7 @@ __process_kf_arg_ptr_to_graph_node(struct bpf_verifier_env *env, node_off = reg->off + reg->var_off.value; field = reg_find_field_offset(reg, node_off, node_field_type); - if (!field || field->offset != node_off) { + if (!field) { verbose(env, "%s not found at offset=%u\n", node_type_name, node_off); return -EINVAL; } From patchwork Wed May 1 20:47:24 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: 13651120 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-oa1-f43.google.com (mail-oa1-f43.google.com [209.85.160.43]) (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 4B21C163AA7 for ; Wed, 1 May 2024 20:47:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714596463; cv=none; b=Yszz/+BtvxVzO+lPoH4ERxpnPZ/UP0rOnIirLy0Ur0IMkTaZGjVtv3nMXQ5r8vkDseUypUcBeh/cx+m3ScsZPeQ9qeudoGZ9MPT0c/fPnDZHQtQg9gm9R4AsainBCgrZM7qYxqw65v6nLGzOKt+Ewm2Hh79Hbo6fvbcbL8CNj7w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714596463; c=relaxed/simple; bh=HR0BZ5/nRStZ4K4LeiV7Tlj8QBA0mggCUyHdcTcT5Xw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=OB70vE49bipJZKtRNAiyIkjiLo48E2LQKSf7w5fx5+TaJlLkHt97UR/FHWaFjuNN3YQuHGBvN9vzVPncFzWYCmTIQPomAzn5E7n61W1NWUlAL7Ix/vVXkLvBdwXyj7t4SJRRf75tP9L4sTsxJuMK8JWXDbOokNbft9MQIuRBoj8= 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=aQZ4dJMW; arc=none smtp.client-ip=209.85.160.43 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="aQZ4dJMW" Received: by mail-oa1-f43.google.com with SMTP id 586e51a60fabf-23d3afdcd71so800000fac.1 for ; Wed, 01 May 2024 13:47:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1714596461; x=1715201261; 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=DTl8z0Qr3BLTdPqJ2qKa7KSFvqh3z2sPFtoSfzE/nzg=; b=aQZ4dJMWGJnz3Kf6TUsR9nqlDvZvhOVTwAGeLpb+q9WUOrVK3UaDnXTtCx5bRitJry x7tyxTnzUgAIwsM5VM2pA70QtwFTDbuzwVRzDkXNxoTOn0hyJqc4N6y2Zpq09AKmckaC y4sqwYS8wUFNXVztHGBD4EnMPAUtcljwEBu6p38qXbhANSQbPZni0/+81R+3J+R0qvxF pETxhfRrg6J+eer88jlAfekQtx+N1j8QQhysYGsdH+Szn9n/ivQx9Uh47IckE6B193aI Y7mYLiOStBHyv9b2CWw88qZxyj5h0dvU4Qe2IpnhI352hw8o4Zpba6aJAYkxIErCNpD5 +hsQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714596461; x=1715201261; 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=DTl8z0Qr3BLTdPqJ2qKa7KSFvqh3z2sPFtoSfzE/nzg=; b=RYWfAxad3w4s9rDwCPHgD0oTwcZTpNPOzLNeWCPtUpenlVBcxBwORYPYG1gt2kwGv8 qWu8Y5otXvJ8cKaXBV6HGExMKHoFsrs+pUim77mM2VmG3BuPUDW1Deki/1YIiKI58ocr yWNhFOZ6Zz29GV7pJ+iiKT53PIfv3VO8ZXGfpVHxZ4EnWhKLmfjiUPPMZYgWfwve86Gf /pl01/nbJSOBmxqurZglOFCx9UflZZv2B6FIuYxce7z9Ng+3uRNOQncb/Esj1gxoUolF jsm+JsiRi+MwvZZg3Lm1xMR3ABe3E4Pv6m3qlPQwZvo9btODwlEucCZOQ5rt1h169A1k /nBA== X-Gm-Message-State: AOJu0YxV7nFrrXZk4Z+BpQFYCNO/P3GXx3CI8bjDC7G+TTpQAdyrwSaU Ie+OVHg/jEoK7hyssv0PwIoxxVkWvBDD/dWRghAN4GBrH4GL8brne1lsUw== X-Google-Smtp-Source: AGHT+IHqLo3ouAutLOPCNhiVXtpJ+tvThshANe481J75q/6XRcGGalmDRShVFtgx4MhpstIp5/QkMQ== X-Received: by 2002:a05:6870:558d:b0:23b:e418:d967 with SMTP id qj13-20020a056870558d00b0023be418d967mr3955360oac.12.1714596460904; Wed, 01 May 2024 13:47:40 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:22b9:2301:860f:eff6]) by smtp.gmail.com with ESMTPSA id rx17-20020a056871201100b002390714e903sm5744408oab.3.2024.05.01.13.47.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 May 2024 13:47:40 -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 v3 2/7] bpf: Remove unnecessary call to btf_field_type_size(). Date: Wed, 1 May 2024 13:47:24 -0700 Message-Id: <20240501204729.484085-3-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240501204729.484085-1-thinker.li@gmail.com> References: <20240501204729.484085-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 8291fbfd27b1..d4c3342e2027 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -6692,7 +6692,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 86d20433c10d..43144a6be45c 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -5448,7 +5448,7 @@ static int check_map_access(struct bpf_verifier_env *env, u32 regno, * this program. To check that [x1, x2) overlaps with [y1, y2), * it is sufficient to check x1 < y2 && y1 < x2. */ - if (reg->smin_value + off < p + btf_field_type_size(field->type) && + if (reg->smin_value + off < p + field->size && p < reg->umax_value + off + size) { switch (field->type) { case BPF_KPTR_UNREF: From patchwork Wed May 1 20:47:25 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: 13651121 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-oa1-f51.google.com (mail-oa1-f51.google.com [209.85.160.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 2EA621649CD for ; Wed, 1 May 2024 20:47:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714596464; cv=none; b=aNnLkLVIRAykAEVLr40ojzOFUpuoNZPBT4drA3ck89ee8swrmlHbNKwj57QOj7PuQs7DYfH+IRdckmuUXwnE1pkwGrI9jN/vmaRqTUkiCuAlRW6FDUaVTFvwB8mym1QAMex2elLmX2dTCnPnA0kbpfdog4IjzyGTNfJeeS+hqUo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714596464; c=relaxed/simple; bh=aOXBkWIp2RYw+AcZuT+hhsKn6B1qOAI2ah0gPNZgYXc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=lm4l69UJVlo2XxdG3XrvW15cIgoKq1dowuoK5a7OcESZCT7JW7aYIJL078MEdcftTEEjtxps61675cpkEUWoLz4XCuco00McsKN2RoCXrh2MtIx3ru4nMCJF4QcpGdZ28oVTPhzgi4DbfudT7g2KkNKoI4yhpN/59qi9p1lWL6w= 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=K2HaLQAm; arc=none smtp.client-ip=209.85.160.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="K2HaLQAm" Received: by mail-oa1-f51.google.com with SMTP id 586e51a60fabf-23d1c4c14ceso1132452fac.0 for ; Wed, 01 May 2024 13:47:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1714596462; x=1715201262; 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=ktDhAmiz3+1lqS9BxQJAmXedd1vY8D/++/LytiIo7rw=; b=K2HaLQAm5OkF9K8nW8OvMedDLmewmW/FowDk6cP377U+cpc03f+iR+tmDJu8Vff3U0 IOaBMofzN0GPyFYssfLJ1o65r74opbXkzHrsicfyUw2Yb9lOIQqGAv5fpClSNdOgRpZe 626jExB0N3arrVECGnr1kv70ARerNNDKXZE/zrj3Fu6gWGI2ysoxM/tLfmzX9bGv778M PTPH/oIuV9w+MgZpqT3Nu7970E9YlDs+hUe3OP0r6knnokvJ73/D0QFRNWK5zJ0KdlDQ TL0pzVEhc3lg6b4lvVvxHeiSnEn348Ht6b5TuLIqh7iGC2dAdBDplf5URrumyWnDnh1Z ga0A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714596462; x=1715201262; 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=ktDhAmiz3+1lqS9BxQJAmXedd1vY8D/++/LytiIo7rw=; b=r70eTdBXJ3/WJ2VZKJNBAqXCaNLzp69RDUFQKZ62r7E0Ok2LSTX4NAk62EELYSQtl5 1Vd4pJ58PpFtkseGJ6j3hc76dw5KaYo2NDuRvOSKLK+l54TeVbmJ/wjRgnPom5Eg0rZx ZIqCFZpsJ70+WEMD8iu0ZA65nHZy2y0kguh/wLCZo4daefo3OlVdwBv/aqJh7U151sXE jhtVj5gOOeuTPDYve0Wp8geOle9y1o82s6mClri4JPKlrHNwwELN5nYdehawvcjL40ak Bo1sv0OEUiC3dSwxdFFM0rOeXlwb7+vgPzzjQtHah20pnOK/K466fEykdY5emnNl7ZFU CqyQ== X-Gm-Message-State: AOJu0YxV8ocgjkFfumIFSwdxq1MzK2Zc530hBNOIImfgDw9Uhg8c7vpO YGpJ9o9Da4VEA51E8wCH91XasBsy3nBNNSjAEuAICCE4M/LOgmEFAjE0OQ== X-Google-Smtp-Source: AGHT+IFPMh4WxToTcjUH/zx3t/fkY4BuqahOzObw45cx38SM8NAQpApdUxChx6RdzvslAp/iiRuTSQ== X-Received: by 2002:a05:6871:412:b0:23c:6020:6733 with SMTP id d18-20020a056871041200b0023c60206733mr4294480oag.27.1714596461921; Wed, 01 May 2024 13:47:41 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:22b9:2301:860f:eff6]) by smtp.gmail.com with ESMTPSA id rx17-20020a056871201100b002390714e903sm5744408oab.3.2024.05.01.13.47.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 May 2024 13:47:41 -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 v3 3/7] bpf: create repeated fields for arrays. Date: Wed, 1 May 2024 13:47:25 -0700 Message-Id: <20240501204729.484085-4-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240501204729.484085-1-thinker.li@gmail.com> References: <20240501204729.484085-1-thinker.li@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net The verifier uses field information for certain special types, such as kptr, rbtree root, and list head. These types are treated differently. However, we did not previously support these types in arrays. This update examines arrays and duplicates field information the same number of times as the length of the array if the element type is one of the special types. Signed-off-by: Kui-Feng Lee --- kernel/bpf/btf.c | 81 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 76 insertions(+), 5 deletions(-) diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index d4c3342e2027..4a78cd28fab0 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -3493,6 +3493,41 @@ static int btf_get_field_type(const char *name, u32 field_mask, u32 *seen_mask, #undef field_mask_test_name +/* Repeat a field for a specified number of times. + * + * Copy and repeat a field for repeat_cnt + * times. The field is repeated by adding the offset of each field + * with + * (i + 1) * elem_size + * where i is the repeat index and elem_size is the size of the element. + */ +static int btf_repeat_field(struct btf_field_info *info, u32 field, + u32 repeat_cnt, u32 elem_size) +{ + u32 i; + u32 cur; + + /* Ensure not repeating fields that should not be repeated. */ + switch (info[field].type) { + case BPF_KPTR_UNREF: + case BPF_KPTR_REF: + case BPF_KPTR_PERCPU: + case BPF_LIST_HEAD: + case BPF_RB_ROOT: + break; + default: + return -EINVAL; + } + + cur = field + 1; + for (i = 0; i < repeat_cnt; i++) { + memcpy(&info[cur], &info[field], sizeof(info[0])); + info[cur++].off += (i + 1) * elem_size; + } + + return 0; +} + static int btf_find_struct_field(const struct btf *btf, const struct btf_type *t, u32 field_mask, struct btf_field_info *info, int info_cnt) @@ -3505,6 +3540,19 @@ static int btf_find_struct_field(const struct btf *btf, for_each_member(i, t, member) { const struct btf_type *member_type = btf_type_by_id(btf, member->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(member_type); j++) { + array = btf_array(member_type); + nelems *= array->nelems; + member_type = btf_type_by_id(btf, array->type); + } + if (nelems == 0) + continue; field_type = btf_get_field_type(__btf_name_by_offset(btf, member_type->name_off), field_mask, &seen_mask, &align, &sz); @@ -3556,9 +3604,14 @@ static int btf_find_struct_field(const struct btf *btf, if (ret == BTF_FIELD_IGNORE) continue; - if (idx >= info_cnt) + if (idx + nelems > info_cnt) return -E2BIG; - ++idx; + if (nelems > 1) { + ret = btf_repeat_field(info, idx, nelems - 1, sz); + if (ret < 0) + return ret; + } + idx += nelems; } return idx; } @@ -3575,6 +3628,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); @@ -3584,7 +3650,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; @@ -3624,9 +3690,14 @@ static int btf_find_datasec_var(const struct btf *btf, const struct btf_type *t, if (ret == BTF_FIELD_IGNORE) continue; - if (idx >= info_cnt) + if (idx + nelems > info_cnt) return -E2BIG; - ++idx; + if (nelems > 1) { + ret = btf_repeat_field(info, idx, nelems - 1, sz); + if (ret < 0) + return ret; + } + idx += nelems; } return idx; } From patchwork Wed May 1 20:47:26 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: 13651122 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-oa1-f54.google.com (mail-oa1-f54.google.com [209.85.160.54]) (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 F19A41649D5 for ; Wed, 1 May 2024 20:47:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714596465; cv=none; b=csXTk4kwCyu/7ukzzLP9DYixBZgcItC9OnDwIQwYUHUjkLWQpXTMi9IHBX9HSDbcnBDwZIWPXSi8e/1CA2eoeGgEUFv15gTQSbzG2pMkh8ntv4ZmJE1nmdf78a9Vrvr33gx3bjVH0F4CY7Wk3LSwLdpMgErjeQtetIIsv0H9nS4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714596465; c=relaxed/simple; bh=z0UqkRTALXxZhdkB4fYqH5hLwBNvnV4yPnOqyGhHMDc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=nXLgtuTBf9KUQRVrYC/Dij3bkAkegTTEBaEiKpN0DIJYA7c4WHWjCxmbtz/f+L82YhpIzkoQiQTuk4gS6OkI51FPuu/r+k5mz2JB0wIF8R7l/vyNWmbBtRPOsuCSJZbvcZu/0dqYcGAWBYxtGhiI6hIQJsBaXscig587z8Xyg+M= 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=X9BixgZ7; arc=none smtp.client-ip=209.85.160.54 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="X9BixgZ7" Received: by mail-oa1-f54.google.com with SMTP id 586e51a60fabf-23d4a716ee7so973677fac.2 for ; Wed, 01 May 2024 13:47:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1714596463; x=1715201263; 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=ebdVSdcEKl/+3KcoL/wBPwiUHNfirTRrd05AF7bSsRk=; b=X9BixgZ7AMYMUGT8joTxQbOcCqkpkFNMGSxrATbAJxpw3Q9y12aCIf+j0Lv1ZeZCoo VL8LGIK5FM/pOGG6Y58Y3KEmSRH5OFAJXl+ZUGmjDHOzsqCH2je3VaVdpDI57LNqb1aX WYxY0lGMCDqfRnACd0WwNlUbdz2Y2H7K8GYi9bYDHDTGCP1TZOr/LULaCwrEhOe1L0qa rjfEE20sA3dmCLwT8CKFIMquMkXTronBcTBKTxroylVsG1s/0DZAAkiXlCUTXMw2at1X e5L5PAWodqF0sTv7yAOAScyR5rkoaTUXbvTootBw2AZbEEEwWHq7MEdpcL5xrD0rVuH8 xQSw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714596463; x=1715201263; 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=ebdVSdcEKl/+3KcoL/wBPwiUHNfirTRrd05AF7bSsRk=; b=j4/9qttvlbsPQBAjCe8b7m1LbnDUwKZ3OTDn/uYnkkfPI5gZp+QHUPK0dQuPrKvrr9 9HznuUGB5kb6dJBMMTzoE/20LBpr0cCFMXgcdWQbwwVvc4+Aif0Em/whEX+Ix5TDXNal yhcUTdiTs1z1U+TL0zy5UJwY40mVgPSgLhZ8PAvblUbm3Sm0ZG+sPISs7HLbRvME6yY+ AMCM2frYu7glzhHp+6hExvw2N58GjCTQMjRApoh43TVIKQ9PzgjPHl8wkrzDQWoRKktZ RxImRgqRiDnC0d87PX/4N78QojyC/6KvGaRUwlcWmPeFUaIi1+EWfhRssZGz6s/h3P+V r/5Q== X-Gm-Message-State: AOJu0YwKX1QUP30xC0fregzLdsGv9sX3PWuWFsSk7dXbiKo4upOOaJEA 0DVi5EDuyMlmBh+c/EQJUujtazVZBE1k3mREAfY6Ye+CNFV7gjn2PuiDhg== X-Google-Smtp-Source: AGHT+IHUdzpUXApNP93wwy1J4ebhVnixlnegee3eS680rkSsMlnYpp/dr+eKyv0+4T0giiZQqnS2oA== X-Received: by 2002:a05:6871:4008:b0:22e:c4e7:8aba with SMTP id kx8-20020a056871400800b0022ec4e78abamr168986oab.47.1714596462893; Wed, 01 May 2024 13:47:42 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:22b9:2301:860f:eff6]) by smtp.gmail.com with ESMTPSA id rx17-20020a056871201100b002390714e903sm5744408oab.3.2024.05.01.13.47.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 May 2024 13:47:42 -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 v3 4/7] bpf: look into the types of the fields of a struct type recursively. Date: Wed, 1 May 2024 13:47:26 -0700 Message-Id: <20240501204729.484085-5-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240501204729.484085-1-thinker.li@gmail.com> References: <20240501204729.484085-1-thinker.li@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net The verifier has field information for specific special types, such as kptr, rbtree root, and list head. These types are handled differently. However, we did not previously examine the types of fields of a struct type variable. Field information records were not generated for the kptrs, rbtree roots, and linked_list heads that are not located at the outermost struct type of a variable. For example, struct A { struct task_struct __kptr * task; }; struct B { struct A mem_a; } struct B var_b; It did not examine "struct A" so as not to generate field information for the kptr in "struct A" for "var_b". This patch enables BPF programs to define fields of these special types in a struct type other than the direct type of a variable or in a struct type that is the type of a field in the value type of a map. Signed-off-by: Kui-Feng Lee --- kernel/bpf/btf.c | 118 +++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 98 insertions(+), 20 deletions(-) diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index 4a78cd28fab0..2ceff77b7e71 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -3493,41 +3493,83 @@ static int btf_get_field_type(const char *name, u32 field_mask, u32 *seen_mask, #undef field_mask_test_name -/* Repeat a field for a specified number of times. +/* Repeat a number of fields for a specified number of times. * - * Copy and repeat a field for repeat_cnt - * times. The field is repeated by adding the offset of each field + * Copy the fields starting from first_field and repeat them for repeat_cnt + * times. The fields are repeated by adding the offset of each field * with * (i + 1) * elem_size * where i is the repeat index and elem_size is the size of the element. */ -static int btf_repeat_field(struct btf_field_info *info, u32 field, - u32 repeat_cnt, u32 elem_size) +static int btf_repeat_fields(struct btf_field_info *info, u32 first_field, + u32 field_cnt, u32 repeat_cnt, u32 elem_size) { - u32 i; + u32 i, j; u32 cur; /* Ensure not repeating fields that should not be repeated. */ - switch (info[field].type) { - case BPF_KPTR_UNREF: - case BPF_KPTR_REF: - case BPF_KPTR_PERCPU: - case BPF_LIST_HEAD: - case BPF_RB_ROOT: - break; - default: - return -EINVAL; + for (i = 0; i < field_cnt; i++) { + switch (info[first_field + i].type) { + case BPF_KPTR_UNREF: + case BPF_KPTR_REF: + case BPF_KPTR_PERCPU: + case BPF_LIST_HEAD: + case BPF_RB_ROOT: + break; + default: + return -EINVAL; + } } - cur = field + 1; + cur = first_field + field_cnt; for (i = 0; i < repeat_cnt; i++) { - memcpy(&info[cur], &info[field], sizeof(info[0])); - info[cur++].off += (i + 1) * elem_size; + memcpy(&info[cur], &info[first_field], field_cnt * sizeof(info[0])); + for (j = 0; j < field_cnt; j++) + info[cur++].off += (i + 1) * elem_size; } return 0; } +static int btf_find_struct_field(const struct btf *btf, + const struct btf_type *t, u32 field_mask, + struct btf_field_info *info, int info_cnt); + +/* Find special fields in the struct type of a field. + * + * This function is used to find fields of special types that is not a + * global variable or a direct field of a struct type. It also handles the + * repetition if it is the element type of an array. + */ +static int btf_find_nested_struct(const struct btf *btf, const struct btf_type *t, + u32 off, u32 nelems, + u32 field_mask, struct btf_field_info *info, + int info_cnt) +{ + int ret, err, i; + + ret = btf_find_struct_field(btf, t, field_mask, info, info_cnt); + + if (ret <= 0) + return ret; + + /* Shift the offsets of the nested struct fields to the offsets + * related to the container. + */ + for (i = 0; i < ret; i++) + info[i].off += off; + + if (nelems > 1) { + err = btf_repeat_fields(info, 0, ret, nelems - 1, t->size); + if (err == 0) + ret *= nelems; + else + ret = err; + } + + return ret; +} + static int btf_find_struct_field(const struct btf *btf, const struct btf_type *t, u32 field_mask, struct btf_field_info *info, int info_cnt) @@ -3556,6 +3598,27 @@ static int btf_find_struct_field(const struct btf *btf, field_type = btf_get_field_type(__btf_name_by_offset(btf, member_type->name_off), field_mask, &seen_mask, &align, &sz); + /* Look into fields of struct types */ + if ((field_type == BPF_KPTR_REF || !field_type) && + __btf_type_is_struct(member_type)) { + /* For field_type == BPF_KPTR_REF, it is not + * necessary a kptr type. It can also be other + * types not special types handled here. However, + * it can not be a struct type if it is a kptr. + */ + off = __btf_member_bit_offset(t, member); + if (off % 8) + /* valid C code cannot generate such BTF */ + return -EINVAL; + off /= 8; + ret = btf_find_nested_struct(btf, member_type, off, nelems, field_mask, + &info[idx], info_cnt - idx); + if (ret < 0) + return ret; + idx += ret; + continue; + } + if (field_type == 0) continue; if (field_type < 0) @@ -3607,7 +3670,7 @@ static int btf_find_struct_field(const struct btf *btf, if (idx + nelems > info_cnt) return -E2BIG; if (nelems > 1) { - ret = btf_repeat_field(info, idx, nelems - 1, sz); + ret = btf_repeat_fields(info, idx, 1, nelems - 1, sz); if (ret < 0) return ret; } @@ -3644,6 +3707,21 @@ static int btf_find_datasec_var(const struct btf *btf, const struct btf_type *t, field_type = btf_get_field_type(__btf_name_by_offset(btf, var_type->name_off), field_mask, &seen_mask, &align, &sz); + /* Look into variables of struct types */ + if ((field_type == BPF_KPTR_REF || !field_type) && + __btf_type_is_struct(var_type)) { + sz = var_type->size; + if (vsi->size != sz * nelems) + continue; + off = vsi->offset; + ret = btf_find_nested_struct(btf, var_type, off, nelems, field_mask, + &info[idx], info_cnt - idx); + if (ret < 0) + return ret; + idx += ret; + continue; + } + if (field_type == 0) continue; if (field_type < 0) @@ -3693,7 +3771,7 @@ static int btf_find_datasec_var(const struct btf *btf, const struct btf_type *t, if (idx + nelems > info_cnt) return -E2BIG; if (nelems > 1) { - ret = btf_repeat_field(info, idx, nelems - 1, sz); + ret = btf_repeat_fields(info, idx, 1, nelems - 1, sz); if (ret < 0) return ret; } From patchwork Wed May 1 20:47:27 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: 13651123 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-oa1-f50.google.com (mail-oa1-f50.google.com [209.85.160.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 0012A165FA4 for ; Wed, 1 May 2024 20:47:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.50 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714596466; cv=none; b=fLwi3uiKEDt8zAHWcJVd25jZjtPNWMY4t+fjxCpn2MUIsC/0brFqOC9EttJjpCHAThn3Q+SkJjy1vqiGQtYvbZGO7hFY1sQd3eLC5b8d/xuYSS/zCQtycrYQeX0I8HobhVqeuu1sgwsFuIRR6+iXnphN0JGwbZffubJ/KOOSW7M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714596466; c=relaxed/simple; bh=Ma3Vu2MlOrX3+7gHJ0smej7Cz6EfF40UjlbMuQF7iyA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=eVV/Ie42CaDilFyUF5xofLwG5koW8zN5IlehkjvUvJe/ENKUq8JGC1qL7pC7VraMqAafOu8eXnU2Zsc96tQOljtxEneeEEaoYJc3mCUlEJeX1V5c2JfHKy3nGnO5/NkKAsbHltxyyEyHl7JTCuA1Hq75zTF2HjprMcZgYmHIrXo= 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=jL0WowwO; arc=none smtp.client-ip=209.85.160.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="jL0WowwO" Received: by mail-oa1-f50.google.com with SMTP id 586e51a60fabf-22edbef3b4eso1891662fac.3 for ; Wed, 01 May 2024 13:47:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1714596464; x=1715201264; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=WEkoRMBGu2GwDIk9DvRSHZhuDVYyTKX9czbFcw2JQz8=; b=jL0WowwO7/laJ2nGqaNgnCOCHFXbXIYSDXMBbYKf4EQBgPigiTtbdE8Xi4HghE+RhV gYDEdg3Teux/uX71yQIgXmRaausBABxtfNB7rHfInm/ONA2Z9k/+v/PSncWXsfFA57Pg WZYuFXMq2Q5UtkBVCok6XNxuj9pt5SXKXLVq5GW8cQ3NPcovA2zeEiC8JLRhnjfXcuNK ZqM/A7s0MAbYlQY04DA8Tn6PFuRtvHU1BdAKiEK+eclWgt5LmgZ/uaWP99s6HTDMddNw Hvb97XoQjwyQ20Uo8IzPPZhA5HisQTRxS5mZMvYRujcKBROTQs0leu3214v9TUEzlfSA 75sQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714596464; x=1715201264; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=WEkoRMBGu2GwDIk9DvRSHZhuDVYyTKX9czbFcw2JQz8=; b=lKYkrN6uu32zufCrWmSIOLkBGz1hVtpdo1I9Hy3yVueV4yu9LqEc0dqj8EP9S+O8An RXjiYVMAYJ3JGOUMuYZggkb5gk+ktaMTHTrvKen5y7UDqkH57zD/Ytl+DmQ5EClBjI6K gyahsFVlSdYEBQDeWhLfQazDH9BPrWSgVYhWxYKfVFgdPyZt7/4WnjiEQeZy3eT+inew weG2YkNd4cIZspeamOgZA/QAtYF5ro7ntfWLZg0ccB8xM0SyGYmYW35pfyvLYMXe2dfl gmymH/m0RffG98nrwmhmc5tjzOVbYxsoSJRRjbZoKwUP6aBUccbqsBzh3LAxE6d0NF8a m/jw== X-Gm-Message-State: AOJu0YzuaOYx39QiUc0nYeHqIY0ybTSw4q+D6mlHEeENheltwz3Xn8pA 6MVOFs/c4iWfXThI/Uc37MP/1vDt7OL+EXWGTj5i2SgKYL5wqpNNRfgBEQ== X-Google-Smtp-Source: AGHT+IEdaM9eOj/+odbXv4o26GzDCh7XQ5LU0aMWRPPvqRo3luvhZxM+MUc/QH08gxrQGSpaXSnq6g== X-Received: by 2002:a05:6870:d8d0:b0:239:52f9:7f15 with SMTP id of16-20020a056870d8d000b0023952f97f15mr4111212oac.26.1714596463828; Wed, 01 May 2024 13:47:43 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:22b9:2301:860f:eff6]) by smtp.gmail.com with ESMTPSA id rx17-20020a056871201100b002390714e903sm5744408oab.3.2024.05.01.13.47.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 May 2024 13:47:43 -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 v3 5/7] selftests/bpf: Test kptr arrays and kptrs in nested struct fields. Date: Wed, 1 May 2024 13:47:27 -0700 Message-Id: <20240501204729.484085-6-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240501204729.484085-1-thinker.li@gmail.com> References: <20240501204729.484085-1-thinker.li@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net Make sure that BPF programs can declare global kptr arrays and kptr fields in struct types that is the type of a global variable or the type of a nested descendant field in a global variable. An array with only one element is special case, that it treats the element like a non-array kptr field. Nested arrays are also tested to ensure they are handled properly. Signed-off-by: Kui-Feng Lee --- .../selftests/bpf/prog_tests/cpumask.c | 5 + .../selftests/bpf/progs/cpumask_success.c | 133 ++++++++++++++++++ 2 files changed, 138 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/cpumask.c b/tools/testing/selftests/bpf/prog_tests/cpumask.c index ecf89df78109..2570bd4b0cb2 100644 --- a/tools/testing/selftests/bpf/prog_tests/cpumask.c +++ b/tools/testing/selftests/bpf/prog_tests/cpumask.c @@ -18,6 +18,11 @@ static const char * const cpumask_success_testcases[] = { "test_insert_leave", "test_insert_remove_release", "test_global_mask_rcu", + "test_global_mask_array_one_rcu", + "test_global_mask_array_rcu", + "test_global_mask_array_l2_rcu", + "test_global_mask_nested_rcu", + "test_global_mask_nested_deep_rcu", "test_cpumask_weight", }; diff --git a/tools/testing/selftests/bpf/progs/cpumask_success.c b/tools/testing/selftests/bpf/progs/cpumask_success.c index 7a1e64c6c065..0b6383fa9958 100644 --- a/tools/testing/selftests/bpf/progs/cpumask_success.c +++ b/tools/testing/selftests/bpf/progs/cpumask_success.c @@ -12,6 +12,25 @@ char _license[] SEC("license") = "GPL"; int pid, nr_cpus; +struct kptr_nested { + struct bpf_cpumask __kptr * mask; +}; + +struct kptr_nested_mid { + int dummy; + struct kptr_nested m; +}; + +struct kptr_nested_deep { + struct kptr_nested_mid ptrs[2]; +}; + +private(MASK) static struct bpf_cpumask __kptr * global_mask_array[2]; +private(MASK) static struct bpf_cpumask __kptr * global_mask_array_l2[2][1]; +private(MASK) static struct bpf_cpumask __kptr * global_mask_array_one[1]; +private(MASK) static struct kptr_nested global_mask_nested[2]; +private(MASK) static struct kptr_nested_deep global_mask_nested_deep; + static bool is_test_task(void) { int cur_pid = bpf_get_current_pid_tgid() >> 32; @@ -460,6 +479,120 @@ int BPF_PROG(test_global_mask_rcu, struct task_struct *task, u64 clone_flags) return 0; } +SEC("tp_btf/task_newtask") +int BPF_PROG(test_global_mask_array_one_rcu, struct task_struct *task, u64 clone_flags) +{ + struct bpf_cpumask *local, *prev; + + if (!is_test_task()) + return 0; + + /* Kptr arrays with one element are special cased, being treated + * just like a single pointer. + */ + + local = create_cpumask(); + if (!local) + return 0; + + prev = bpf_kptr_xchg(&global_mask_array_one[0], local); + if (prev) { + bpf_cpumask_release(prev); + err = 3; + return 0; + } + + bpf_rcu_read_lock(); + local = global_mask_array_one[0]; + if (!local) { + err = 4; + bpf_rcu_read_unlock(); + return 0; + } + + bpf_rcu_read_unlock(); + + return 0; +} + +static int _global_mask_array_rcu(struct bpf_cpumask **mask0, + struct bpf_cpumask **mask1) +{ + struct bpf_cpumask *local; + + if (!is_test_task()) + return 0; + + /* Check if two kptrs in the array work and independently */ + + local = create_cpumask(); + if (!local) + return 0; + + bpf_rcu_read_lock(); + + local = bpf_kptr_xchg(mask0, local); + if (local) { + err = 1; + goto err_exit; + } + + /* [, NULL] */ + if (!*mask0 || *mask1) { + err = 2; + goto err_exit; + } + + local = create_cpumask(); + if (!local) { + err = 9; + goto err_exit; + } + + local = bpf_kptr_xchg(mask1, local); + if (local) { + err = 10; + goto err_exit; + } + + /* [, ] */ + if (!*mask0 || !*mask1 || *mask0 == *mask1) { + err = 11; + goto err_exit; + } + +err_exit: + if (local) + bpf_cpumask_release(local); + bpf_rcu_read_unlock(); + return 0; +} + +SEC("tp_btf/task_newtask") +int BPF_PROG(test_global_mask_array_rcu, struct task_struct *task, u64 clone_flags) +{ + return _global_mask_array_rcu(&global_mask_array[0], &global_mask_array[1]); +} + +SEC("tp_btf/task_newtask") +int BPF_PROG(test_global_mask_array_l2_rcu, struct task_struct *task, u64 clone_flags) +{ + return _global_mask_array_rcu(&global_mask_array_l2[0][0], &global_mask_array_l2[1][0]); +} + +SEC("tp_btf/task_newtask") +int BPF_PROG(test_global_mask_nested_rcu, struct task_struct *task, u64 clone_flags) +{ + return _global_mask_array_rcu(&global_mask_nested[0].mask, &global_mask_nested[1].mask); +} + +SEC("tp_btf/task_newtask") +int BPF_PROG(test_global_mask_nested_deep_rcu, struct task_struct *task, u64 clone_flags) +{ + return _global_mask_array_rcu(&global_mask_nested_deep.ptrs[0].m.mask, + &global_mask_nested_deep.ptrs[1].m.mask); +} + SEC("tp_btf/task_newtask") int BPF_PROG(test_cpumask_weight, struct task_struct *task, u64 clone_flags) { From patchwork Wed May 1 20:47:28 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: 13651124 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-oa1-f50.google.com (mail-oa1-f50.google.com [209.85.160.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 02B92165FB0 for ; Wed, 1 May 2024 20:47:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.50 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714596467; cv=none; b=XhteXeF+gM6tgMbC/hcBHm4oGcmuP1DkTLJJKirWjplg47iVaNNZHyeoUzV/kH0RuwFoeNZ90hlu3U6ZeuINJvlnadknqLX+Tyj2ctPwuN9zB7df8+IYiPXir9UjMMZ2qgHG3SaSxEI6PABYTb40MGxCtZdoFzyEYDiWXmal4ms= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714596467; c=relaxed/simple; bh=184HSHS8K1pDQhxH155teo7Hn+PjBBhq6NMvMs9ANKc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=RlOWiq8gIQ27wY9zIRmSQd+Mr14nWFzYQV7FpjngYBpyF6SRGFyaFp005dtWzvU2xHDbRkwOVbSgjC2ICdRwIOlBlleUNc2/IanBF7e0OG3xqVUe7DXp8MjehbVeHUTJdYj+gJACoOl6qNZPu4XOx06vFB+kXwlU2P5N2JHTgmQ= 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=IXMCKdeM; arc=none smtp.client-ip=209.85.160.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="IXMCKdeM" Received: by mail-oa1-f50.google.com with SMTP id 586e51a60fabf-23d15e31cf7so956981fac.1 for ; Wed, 01 May 2024 13:47:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1714596465; x=1715201265; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=EWwzY90xebc6YceOSeyo6ErhBa/XYlP1DhvcruTgZXw=; b=IXMCKdeMePR2L3SYcRJYPSBbOdJGXLFzQyYdO+agfcfmboG4JJvCuLzhhrPa6bgfik QnPMZZ3rXyrM9IA3LrMqJb+Fq722F0gjw0HjmqaAhs43emlQhr/rpqiMxOzJ5i3dheSz aTEzz+AEagNHgUUvOC8Ar4+AJ3QEtv731rVHOEnqN2C+2AFucrA3/fM1Ch7WT9oldia/ Tt7wo44pRdGK2O98Du0mxdC9CQBXeMso64C3JH4q6dWanikl4/xj9BX55M2iRGKeN3rK 7HZSb9TUVWKo5SLIKuxGZ/7apPot303zOC3HaTRO0wZabUdEz4dvaKAzm3HQZN7nkPOl 10xw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714596465; x=1715201265; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=EWwzY90xebc6YceOSeyo6ErhBa/XYlP1DhvcruTgZXw=; b=fnxF8AP4KpBPbhkFLqG1ySLDgYXxe1HKscinNygamt8GyYdeWHLj2AYQc/KbeqEoW4 pUQp8U0+1Vm7DoMg483NalHo9cP/P3eCVkdpQOq04mUS8dIWSQNgH/VJ+PaniO2sU7Lx ggxvy3nG1LjXonBnvnOoTngYG9jLHkeDQIQYNNFHiQpFrTW1zW8iSud4p2g4pAFMNKGc COZhBeaVoZER6ooUPgEpf1UsVeHKTLlHTYmfDL6ph+0ngmo0pmw4Ll9izXSgxTxFxOC9 6Ta6t6g2Cjw0nUCPmzIii2qXW1FnHdgx3pz0NLMGykxy7WfQ0LDX99opKOIZ6+egRcD4 dMhg== X-Gm-Message-State: AOJu0YwUhuB1QVAzM5AnrcA3TH9RsOJUL7eZkA9MbFKr7o/gMMwKIWz8 vpV3CNpxKH3h477JfxHnxvGS0X/QfQigRUsKB/zkX23XtTE65mSMWd8frA== X-Google-Smtp-Source: AGHT+IE86NAlfutGiZOh/NPmt/U2+N0G5LpnP/8BZuTBczR7b0bF02nZaaDvf1TfIcmwMaduYYN/FA== X-Received: by 2002:a05:6870:831e:b0:23c:739a:e6c5 with SMTP id p30-20020a056870831e00b0023c739ae6c5mr4061455oae.55.1714596464917; Wed, 01 May 2024 13:47:44 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:22b9:2301:860f:eff6]) by smtp.gmail.com with ESMTPSA id rx17-20020a056871201100b002390714e903sm5744408oab.3.2024.05.01.13.47.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 May 2024 13:47:44 -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 v3 6/7] selftests/bpf: Test global bpf_rb_root arrays and fields in nested struct types. Date: Wed, 1 May 2024 13:47:28 -0700 Message-Id: <20240501204729.484085-7-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240501204729.484085-1-thinker.li@gmail.com> References: <20240501204729.484085-1-thinker.li@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net Make sure global arrays of bpf_rb_root and fields of bpf_rb_root in nested struct types work correctly. Signed-off-by: Kui-Feng Lee --- .../testing/selftests/bpf/prog_tests/rbtree.c | 47 +++++++++++ tools/testing/selftests/bpf/progs/rbtree.c | 77 +++++++++++++++++++ 2 files changed, 124 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/rbtree.c b/tools/testing/selftests/bpf/prog_tests/rbtree.c index e9300c96607d..9818f06c97c5 100644 --- a/tools/testing/selftests/bpf/prog_tests/rbtree.c +++ b/tools/testing/selftests/bpf/prog_tests/rbtree.c @@ -31,6 +31,28 @@ static void test_rbtree_add_nodes(void) rbtree__destroy(skel); } +static void test_rbtree_add_nodes_nested(void) +{ + LIBBPF_OPTS(bpf_test_run_opts, opts, + .data_in = &pkt_v4, + .data_size_in = sizeof(pkt_v4), + .repeat = 1, + ); + struct rbtree *skel; + int ret; + + skel = rbtree__open_and_load(); + if (!ASSERT_OK_PTR(skel, "rbtree__open_and_load")) + return; + + ret = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.rbtree_add_nodes_nested), &opts); + ASSERT_OK(ret, "rbtree_add_nodes_nested run"); + ASSERT_OK(opts.retval, "rbtree_add_nodes_nested retval"); + ASSERT_EQ(skel->data->less_callback_ran, 1, "rbtree_add_nodes_nested less_callback_ran"); + + rbtree__destroy(skel); +} + static void test_rbtree_add_and_remove(void) { LIBBPF_OPTS(bpf_test_run_opts, opts, @@ -53,6 +75,27 @@ static void test_rbtree_add_and_remove(void) rbtree__destroy(skel); } +static void test_rbtree_add_and_remove_array(void) +{ + LIBBPF_OPTS(bpf_test_run_opts, opts, + .data_in = &pkt_v4, + .data_size_in = sizeof(pkt_v4), + .repeat = 1, + ); + struct rbtree *skel; + int ret; + + skel = rbtree__open_and_load(); + if (!ASSERT_OK_PTR(skel, "rbtree__open_and_load")) + return; + + ret = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.rbtree_add_and_remove_array), &opts); + ASSERT_OK(ret, "rbtree_add_and_remove_array"); + ASSERT_OK(opts.retval, "rbtree_add_and_remove_array retval"); + + rbtree__destroy(skel); +} + static void test_rbtree_first_and_remove(void) { LIBBPF_OPTS(bpf_test_run_opts, opts, @@ -104,8 +147,12 @@ void test_rbtree_success(void) { if (test__start_subtest("rbtree_add_nodes")) test_rbtree_add_nodes(); + if (test__start_subtest("rbtree_add_nodes_nested")) + test_rbtree_add_nodes_nested(); if (test__start_subtest("rbtree_add_and_remove")) test_rbtree_add_and_remove(); + if (test__start_subtest("rbtree_add_and_remove_array")) + test_rbtree_add_and_remove_array(); if (test__start_subtest("rbtree_first_and_remove")) test_rbtree_first_and_remove(); if (test__start_subtest("rbtree_api_release_aliasing")) diff --git a/tools/testing/selftests/bpf/progs/rbtree.c b/tools/testing/selftests/bpf/progs/rbtree.c index b09f4fffe57c..a3620c15c136 100644 --- a/tools/testing/selftests/bpf/progs/rbtree.c +++ b/tools/testing/selftests/bpf/progs/rbtree.c @@ -13,6 +13,15 @@ struct node_data { struct bpf_rb_node node; }; +struct root_nested_inner { + struct bpf_spin_lock glock; + struct bpf_rb_root root __contains(node_data, node); +}; + +struct root_nested { + struct root_nested_inner inner; +}; + long less_callback_ran = -1; long removed_key = -1; long first_data[2] = {-1, -1}; @@ -20,6 +29,9 @@ long first_data[2] = {-1, -1}; #define private(name) SEC(".data." #name) __hidden __attribute__((aligned(8))) private(A) struct bpf_spin_lock glock; private(A) struct bpf_rb_root groot __contains(node_data, node); +private(A) struct bpf_rb_root groot_array[2] __contains(node_data, node); +private(A) struct bpf_rb_root groot_array_one[1] __contains(node_data, node); +private(B) struct root_nested groot_nested; static bool less(struct bpf_rb_node *a, const struct bpf_rb_node *b) { @@ -71,6 +83,12 @@ long rbtree_add_nodes(void *ctx) return __add_three(&groot, &glock); } +SEC("tc") +long rbtree_add_nodes_nested(void *ctx) +{ + return __add_three(&groot_nested.inner.root, &groot_nested.inner.glock); +} + SEC("tc") long rbtree_add_and_remove(void *ctx) { @@ -109,6 +127,65 @@ long rbtree_add_and_remove(void *ctx) return 1; } +SEC("tc") +long rbtree_add_and_remove_array(void *ctx) +{ + struct bpf_rb_node *res1 = NULL, *res2 = NULL, *res3 = NULL; + struct node_data *nodes[3][2] = {{NULL, NULL}, {NULL, NULL}, {NULL, NULL}}; + struct node_data *n; + long k1 = -1, k2 = -1, k3 = -1; + int i, j; + + for (i = 0; i < 3; i++) { + for (j = 0; j < 2; j++) { + nodes[i][j] = bpf_obj_new(typeof(*nodes[i][j])); + if (!nodes[i][j]) + goto err_out; + nodes[i][j]->key = i * 2 + j; + } + } + + bpf_spin_lock(&glock); + for (i = 0; i < 2; i++) + for (j = 0; j < 2; j++) + bpf_rbtree_add(&groot_array[i], &nodes[i][j]->node, less); + for (j = 0; j < 2; j++) + bpf_rbtree_add(&groot_array_one[0], &nodes[2][j]->node, less); + res1 = bpf_rbtree_remove(&groot_array[0], &nodes[0][0]->node); + res2 = bpf_rbtree_remove(&groot_array[1], &nodes[1][0]->node); + res3 = bpf_rbtree_remove(&groot_array_one[0], &nodes[2][0]->node); + bpf_spin_unlock(&glock); + + if (res1) { + n = container_of(res1, struct node_data, node); + k1 = n->key; + bpf_obj_drop(n); + } + if (res2) { + n = container_of(res2, struct node_data, node); + k2 = n->key; + bpf_obj_drop(n); + } + if (res3) { + n = container_of(res3, struct node_data, node); + k3 = n->key; + bpf_obj_drop(n); + } + if (k1 != 0 || k2 != 2 || k3 != 4) + return 2; + + return 0; + +err_out: + for (i = 0; i < 3; i++) { + for (j = 0; j < 2; j++) { + if (nodes[i][j]) + bpf_obj_drop(nodes[i][j]); + } + } + return 1; +} + SEC("tc") long rbtree_first_and_remove(void *ctx) { From patchwork Wed May 1 20:47:29 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: 13651125 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-oa1-f46.google.com (mail-oa1-f46.google.com [209.85.160.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 0A376165FBA for ; Wed, 1 May 2024 20:47:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714596468; cv=none; b=RjnWHCysvElhVqUZYTg/HnGebkBsPdIqK3HBWBeEjE4KgUnzp3ZSqBB7op9SEqU9tFsB8ni6m5DxNNIuGT/AT+aM1mdn94YD8nu2DlgWENgcurF9iZ/P2QEiHKptueb2RESxpw7UPq9ZMFItBf6zkHI5chDIvE4msTXEOpkbATs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714596468; c=relaxed/simple; bh=T99aEuo7lB5fd7ns1ZsNRt0u99NUChuBc++AQ35NEN8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=rvY6ifmQxT+v6TZi+9sGCwqnOj1xYgyWQmU91ks+sm73A1bF5/+wu6r+l2jMME/2FFrGZU0Ne5/mM+0wKruDBZTKVuBF7vnPeXgWhljyAbvOpIo9ElUrK/QZpG/ov7yQ9qMC88Omk0mYWwpvCZDx46N8OaSmNVhnoZLgmWbJr+4= 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=CsSBfN/W; arc=none smtp.client-ip=209.85.160.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="CsSBfN/W" Received: by mail-oa1-f46.google.com with SMTP id 586e51a60fabf-23db0b5dd28so297090fac.2 for ; Wed, 01 May 2024 13:47:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1714596466; x=1715201266; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=2cZSvfqk5wcnoVZE269hw105nH6/qA5LuJG4//0qzg8=; b=CsSBfN/WMosPDe2nak9q7eOeTf0citnkVDySAlA/kVbDrrPtyFkH9BNF0XM1t6l8/0 ycNW64vIcGQ8Le7M4l8ywoh/CTKkpw22eIG9bSjS/OHl1Np9Lx2c8rD5ToDYC+QXn9hk yUeakN7e6GEhyjopEGZMAmmrIwdi8iLT7AzHUA+HqoRo2cWW9wwUn9ZbzSHkNDuLvj/X SY0XEh8D1AKksJWKpv3nAo1gp0hFqSOT8XVlvsEe36AJf3vjR1TD9YuUFux9G5J9j4x4 H7ZLYJGbt1ZxnTi/YSnvm4CLfIip4x2M2aS5C4t44ilsQ/T1wbG8Aa82fTcHYjT2+esG x+0Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714596466; x=1715201266; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=2cZSvfqk5wcnoVZE269hw105nH6/qA5LuJG4//0qzg8=; b=u0h101urKleWvlwUawsrSVvs7mD2zYAII3dSZH5Od6nWd2mJXVBUoDHqVp0LiQqKCg bdWR5Dvsth/G56Em0SkwqTa1aVSd5G6iw3blJOVmDW/nqb6aXT8OB4UV5DHYVn5JF82J qU0vYA7/jFzUc6ws+WeL9JIlccN7pzOwd5/R7VIsuSGP/z51nQ0jNXvkrX8BqlRjH6x5 HoH3gxaiR/eM5m+v4wqptP4nnFAS677g2Hw4MIjx2AP4Q9USZGtfX544AJykN+NY4syd G6ZUSszKiLsq55cFUTpHFuJaMtidPsc+0vwTZhkEu5fvKFN4JtMdlBHRw03FsL3YZHwf NE8w== X-Gm-Message-State: AOJu0YxI1hjol8WJppQeb1YG7HDU/SH8xHiHLyvouwtOPIqNP9bVidp0 S8mb8SAVcKh0Sw4/AcfOQqw/68sgzrq+Glj3zBytA1053tZ0W1tXMgaPMw== X-Google-Smtp-Source: AGHT+IHm53OVCpQlt7gK+2OhuRUYCeoOHZLhFK9j7WjuappLVokYXMsJ1NqzymqklIirMKbpZBRfdQ== X-Received: by 2002:a05:6871:810c:b0:221:9013:d783 with SMTP id sn12-20020a056871810c00b002219013d783mr156915oab.34.1714596465912; Wed, 01 May 2024 13:47:45 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:22b9:2301:860f:eff6]) by smtp.gmail.com with ESMTPSA id rx17-20020a056871201100b002390714e903sm5744408oab.3.2024.05.01.13.47.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 May 2024 13:47:45 -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 v3 7/7] selftests/bpf: Test global bpf_list_head arrays. Date: Wed, 1 May 2024 13:47:29 -0700 Message-Id: <20240501204729.484085-8-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240501204729.484085-1-thinker.li@gmail.com> References: <20240501204729.484085-1-thinker.li@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net Make sure global arrays of bpf_list_heads and fields of bpf_list_heads in nested struct types work correctly. Signed-off-by: Kui-Feng Lee --- .../selftests/bpf/prog_tests/linked_list.c | 12 ++++++ .../testing/selftests/bpf/progs/linked_list.c | 42 +++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/linked_list.c b/tools/testing/selftests/bpf/prog_tests/linked_list.c index 2fb89de63bd2..77d07e0a4a55 100644 --- a/tools/testing/selftests/bpf/prog_tests/linked_list.c +++ b/tools/testing/selftests/bpf/prog_tests/linked_list.c @@ -183,6 +183,18 @@ static void test_linked_list_success(int mode, bool leave_in_map) if (!leave_in_map) clear_fields(skel->maps.bss_A); + ret = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.global_list_push_pop_nested), &opts); + ASSERT_OK(ret, "global_list_push_pop_nested"); + ASSERT_OK(opts.retval, "global_list_push_pop_nested retval"); + if (!leave_in_map) + clear_fields(skel->maps.bss_A); + + ret = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.global_list_array_push_pop), &opts); + ASSERT_OK(ret, "global_list_array_push_pop"); + ASSERT_OK(opts.retval, "global_list_array_push_pop retval"); + if (!leave_in_map) + clear_fields(skel->maps.bss_A); + if (mode == PUSH_POP) goto end; diff --git a/tools/testing/selftests/bpf/progs/linked_list.c b/tools/testing/selftests/bpf/progs/linked_list.c index 26205ca80679..f69bf3e30321 100644 --- a/tools/testing/selftests/bpf/progs/linked_list.c +++ b/tools/testing/selftests/bpf/progs/linked_list.c @@ -11,6 +11,22 @@ #include "linked_list.h" +struct head_nested_inner { + struct bpf_spin_lock lock; + struct bpf_list_head head __contains(foo, node2); +}; + +struct head_nested { + int dummy; + struct head_nested_inner inner; +}; + +private(C) struct bpf_spin_lock glock_c; +private(C) struct bpf_list_head ghead_array[2] __contains(foo, node2); +private(C) struct bpf_list_head ghead_array_one[1] __contains(foo, node2); + +private(D) struct head_nested ghead_nested; + static __always_inline int list_push_pop(struct bpf_spin_lock *lock, struct bpf_list_head *head, bool leave_in_map) { @@ -309,6 +325,32 @@ int global_list_push_pop(void *ctx) return test_list_push_pop(&glock, &ghead); } +SEC("tc") +int global_list_push_pop_nested(void *ctx) +{ + return test_list_push_pop(&ghead_nested.inner.lock, &ghead_nested.inner.head); +} + +SEC("tc") +int global_list_array_push_pop(void *ctx) +{ + int r; + + r = test_list_push_pop(&glock_c, &ghead_array[0]); + if (r) + return r; + + r = test_list_push_pop(&glock_c, &ghead_array[1]); + if (r) + return r; + + /* Arrays with only one element is a special case, being treated + * just like a bpf_list_head variable by the verifier, not an + * array. + */ + return test_list_push_pop(&glock_c, &ghead_array_one[0]); +} + SEC("tc") int map_list_push_pop_multiple(void *ctx) {