From nobody Sun Feb 8 06:04:46 2026 Received: from mail-qk1-f171.google.com (mail-qk1-f171.google.com [209.85.222.171]) (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 8FF38233D8E for ; Fri, 7 Feb 2025 05:45:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738907126; cv=none; b=G8cjpNY3jnAhql9n0CFz7xfADI3K8T4b0Q1db2rIgAxkzYGn3kv3uz1ExiHr+/R1J9701i5eA9PZKOMfpnxMUVhiF/Xi8NMXP/zEVXmRlUX9TYl0VCngrGkCpA7tUo8gYpKnCFxavJpzjNCkjpHG60veTLDfTn55PiXgee9qRVc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738907126; c=relaxed/simple; bh=TSqw6WTzkGxJ2g7v2P63DZ+KseEhuRtpFe8QcAD65c0=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=OkrULn+zYDYyVF0HtXzzy1R5UqZdlM4FxxBB2O9T7qm2PtOin6YWFYooBBR8eKmsHEg6aQJZlz8Py+IisjdG2e5kgJ6uBy5K4gfSX26EYw/InkceVNcM4czbCXqi11EWHbROvbG8+b1X6gxV8WiVS9gBszVDRV2KPRfg9Wb0uPQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=cloudflare.com; spf=pass smtp.mailfrom=cloudflare.com; dkim=pass (2048-bit key) header.d=cloudflare.com header.i=@cloudflare.com header.b=UHEPg4YV; arc=none smtp.client-ip=209.85.222.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=cloudflare.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=cloudflare.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=cloudflare.com header.i=@cloudflare.com header.b="UHEPg4YV" Received: by mail-qk1-f171.google.com with SMTP id af79cd13be357-7b6eeff1fdfso146684085a.2 for ; Thu, 06 Feb 2025 21:45:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloudflare.com; s=google09082023; t=1738907123; x=1739511923; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=BiL88Vl7FssHFuR4sgsTuWpUW3EZbuNKD07jWPDb1JA=; b=UHEPg4YVibcpTJ1mxaJRGLdhZvlvZ2EM/Z4sqsMsVLTNFTaB2r8peb7JBdIex/qYnw lK5vLlkMdjY+tqFSXvVOw2WSelcQ+KUb51Ho0dWExP4NnKMCFRdOAP7F+qX1hyyW4wD5 RIVDkAPbgqdMWSgeaDWSWU+IbwuFVEDj38tiSDNlZ5WuD1dC43q3doQNvVCaophJgEDN M7a9eyQma7FA72ofTx5y/50H6w0wtKVKm7p1pqzCXLnoMeddfD/uFzKnyPjSeklPNtgs eySYagC2xNcvtlDc6Vyk6pIUVuFpXtVHmsnM1iGYa5a2GZLgVGAhMRMThPKIqVqAJmvy jfZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738907123; x=1739511923; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=BiL88Vl7FssHFuR4sgsTuWpUW3EZbuNKD07jWPDb1JA=; b=PvDh2mnaznGE3Ilyu7p5AzJPtujBDMmR9CCosM814+yzcLWx0l6rLBBTEH/YAAhk/M WSR8yIsG4Q5EESecAXwH1X4mhYLf6uUgN9UYWakdRWYevBG2PXyJoO2liZlM3XvtW/AH aISE5TZNOHt0a5y/+6y06hqpqAMIKOn0ypYJwkNmUekRCSkRfjPQiJXChfUrfI0WCjDF RJEpVKPnP+FLTVNlwRFxomsywzKsMc3MEMvGjtnKh1vQnqWrpo2h7/ZfDBLPgBNm2Z3N 9pg0m0p9pnhcLx35Cbqse/9PbxTuVdwm1HX4+d6gfftXCrHL1q83ah5SdcPUY1CJUprg lCTg== X-Forwarded-Encrypted: i=1; AJvYcCW4qs7vS0b6TXyl0nGHvYnWV5+/wKuWnfhIcTRzJ8p95+LKWskGI9FtSDGBpVv6ym4P0RouZUUwpq4eSfM=@vger.kernel.org X-Gm-Message-State: AOJu0YxJjdVzXrYOucpU+MEaJG8ibKR9u8mBYaYr6MtNA8EhseaBF3vc d9/6eAs/Wdxiv4cWp8YuNp37bY5q7fIIRckQ7ifsVlyrtXSm84kOK006UB5rW10= X-Gm-Gg: ASbGncsQiNJ4JqfWCIulM2+Me4ripzKsXkvjDJP5/3DdhNbwLiJL+WOV9PR4wiXNr7t j4slbEvfC589Rn18LhrMQD4sr4TXICkGMuN1f0Bo9m5EPSwFVyK5Y2beiYh8bk2xvNnwzQ3eBmw vKup/wYzXEXpFOYK02fsii5oYkMSftV0NcTUMi3vjzVPvP0BMS2XxEXDgTEsluM8qDZG79yzArP fzovkE90c1HGLWf37KTImamCkrsr6ANQYfVJdBp3Oo2Kkk3og8DjS/A3m60XmGUOCT7/h453Vns vFw= X-Google-Smtp-Source: AGHT+IGZ3e/LBC9oFo92G9gFvFYL6g4CWsC0kkEIeD2dmJfNrUDCabnMPaFC52kKEkSHxzG2IanhUQ== X-Received: by 2002:a05:620a:258b:b0:7b6:e9be:97dc with SMTP id af79cd13be357-7c047ba498bmr301516785a.8.1738907123361; Thu, 06 Feb 2025 21:45:23 -0800 (PST) Received: from debian.debian ([2a09:bac5:79dd:25a5::3c0:2]) by smtp.gmail.com with ESMTPSA id af79cd13be357-7c041eb7f94sm147517385a.96.2025.02.06.21.45.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 Feb 2025 21:45:22 -0800 (PST) Date: Thu, 6 Feb 2025 21:45:20 -0800 From: Yan Zhai To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , John Fastabend , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Mykola Lysenko , Shuah Khan , Yan Zhai , Brian Vazquez , linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, kernel-team@cloudflare.com, Hou Tao Subject: [PATCH v2 bpf 2/2] selftests: bpf: test batch lookup on array of maps with holes Message-ID: <6e710c8034800cb14e7db2d77b2081222bcb247e.1738905497.git.yan@cloudflare.com> References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Iterating through array of maps may encounter non existing keys. The batch operation should not fail on when this happens. Signed-off-by: Yan Zhai Acked-by: Hou Tao --- .../bpf/map_tests/map_in_map_batch_ops.c | 62 +++++++++++++------ 1 file changed, 44 insertions(+), 18 deletions(-) diff --git a/tools/testing/selftests/bpf/map_tests/map_in_map_batch_ops.c b= /tools/testing/selftests/bpf/map_tests/map_in_map_batch_ops.c index 66191ae9863c..79c3ccadb962 100644 --- a/tools/testing/selftests/bpf/map_tests/map_in_map_batch_ops.c +++ b/tools/testing/selftests/bpf/map_tests/map_in_map_batch_ops.c @@ -120,11 +120,12 @@ static void validate_fetch_results(int outer_map_fd, =20 static void fetch_and_validate(int outer_map_fd, struct bpf_map_batch_opts *opts, - __u32 batch_size, bool delete_entries) + __u32 batch_size, bool delete_entries, + bool has_holes) { - __u32 *fetched_keys, *fetched_values, total_fetched =3D 0; - __u32 batch_key =3D 0, fetch_count, step_size; - int err, max_entries =3D OUTER_MAP_ENTRIES; + int err, max_entries =3D OUTER_MAP_ENTRIES - !!has_holes; + __u32 *fetched_keys, *fetched_values, total_fetched =3D 0, i; + __u32 batch_key =3D 0, fetch_count, step_size =3D batch_size; __u32 value_size =3D sizeof(__u32); =20 /* Total entries needs to be fetched */ @@ -134,9 +135,8 @@ static void fetch_and_validate(int outer_map_fd, "Memory allocation failed for fetched_keys or fetched_values", "error=3D%s\n", strerror(errno)); =20 - for (step_size =3D batch_size; - step_size <=3D max_entries; - step_size +=3D batch_size) { + /* hash map may not always return full batch */ + for (i =3D 0; i < OUTER_MAP_ENTRIES; i++) { fetch_count =3D step_size; err =3D delete_entries ? bpf_map_lookup_and_delete_batch(outer_map_fd, @@ -155,6 +155,7 @@ static void fetch_and_validate(int outer_map_fd, if (err && errno =3D=3D ENOSPC) { /* Fetch again with higher batch size */ total_fetched =3D 0; + step_size +=3D batch_size; continue; } =20 @@ -184,18 +185,19 @@ static void fetch_and_validate(int outer_map_fd, } =20 static void _map_in_map_batch_ops(enum bpf_map_type outer_map_type, - enum bpf_map_type inner_map_type) + enum bpf_map_type inner_map_type, + bool has_holes) { + __u32 max_entries =3D OUTER_MAP_ENTRIES - !!has_holes; __u32 *outer_map_keys, *inner_map_fds; - __u32 max_entries =3D OUTER_MAP_ENTRIES; LIBBPF_OPTS(bpf_map_batch_opts, opts); __u32 value_size =3D sizeof(__u32); int batch_size[2] =3D {5, 10}; __u32 map_index, op_index; int outer_map_fd, ret; =20 - outer_map_keys =3D calloc(max_entries, value_size); - inner_map_fds =3D calloc(max_entries, value_size); + outer_map_keys =3D calloc(OUTER_MAP_ENTRIES, value_size); + inner_map_fds =3D calloc(OUTER_MAP_ENTRIES, value_size); CHECK((!outer_map_keys || !inner_map_fds), "Memory allocation failed for outer_map_keys or inner_map_fds", "error=3D%s\n", strerror(errno)); @@ -209,6 +211,24 @@ static void _map_in_map_batch_ops(enum bpf_map_type ou= ter_map_type, ((outer_map_type =3D=3D BPF_MAP_TYPE_ARRAY_OF_MAPS) ? 9 : 1000) - map_index; =20 + /* This condition is only meaningful for array of maps. + * + * max_entries =3D=3D OUTER_MAP_ENTRIES - 1 if it is true. Say + * max_entries is short for n, then outer_map_keys looks like: + * + * [n, n-1, ... 2, 1] + * + * We change it to + * + * [n, n-1, ... 2, 0] + * + * So it will leave key 1 as a hole. It will serve to test the + * correctness when batch on an array: a "non-exist" key might be + * actually allocated and returned from key iteration. + */ + if (has_holes) + outer_map_keys[max_entries - 1]--; + /* batch operation - map_update */ ret =3D bpf_map_update_batch(outer_map_fd, outer_map_keys, inner_map_fds, &max_entries, &opts); @@ -219,15 +239,17 @@ static void _map_in_map_batch_ops(enum bpf_map_type o= uter_map_type, /* batch operation - map_lookup */ for (op_index =3D 0; op_index < 2; ++op_index) fetch_and_validate(outer_map_fd, &opts, - batch_size[op_index], false); + batch_size[op_index], false, + has_holes); =20 /* batch operation - map_lookup_delete */ if (outer_map_type =3D=3D BPF_MAP_TYPE_HASH_OF_MAPS) fetch_and_validate(outer_map_fd, &opts, - max_entries, true /*delete*/); + max_entries, true /*delete*/, + has_holes); =20 /* close all map fds */ - for (map_index =3D 0; map_index < max_entries; map_index++) + for (map_index =3D 0; map_index < OUTER_MAP_ENTRIES; map_index++) close(inner_map_fds[map_index]); close(outer_map_fd); =20 @@ -237,16 +259,20 @@ static void _map_in_map_batch_ops(enum bpf_map_type o= uter_map_type, =20 void test_map_in_map_batch_ops_array(void) { - _map_in_map_batch_ops(BPF_MAP_TYPE_ARRAY_OF_MAPS, BPF_MAP_TYPE_ARRAY); + _map_in_map_batch_ops(BPF_MAP_TYPE_ARRAY_OF_MAPS, BPF_MAP_TYPE_ARRAY, fal= se); printf("%s:PASS with inner ARRAY map\n", __func__); - _map_in_map_batch_ops(BPF_MAP_TYPE_ARRAY_OF_MAPS, BPF_MAP_TYPE_HASH); + _map_in_map_batch_ops(BPF_MAP_TYPE_ARRAY_OF_MAPS, BPF_MAP_TYPE_HASH, fals= e); printf("%s:PASS with inner HASH map\n", __func__); + _map_in_map_batch_ops(BPF_MAP_TYPE_ARRAY_OF_MAPS, BPF_MAP_TYPE_ARRAY, tru= e); + printf("%s:PASS with inner ARRAY map with holes\n", __func__); + _map_in_map_batch_ops(BPF_MAP_TYPE_ARRAY_OF_MAPS, BPF_MAP_TYPE_HASH, true= ); + printf("%s:PASS with inner HASH map with holes\n", __func__); } =20 void test_map_in_map_batch_ops_hash(void) { - _map_in_map_batch_ops(BPF_MAP_TYPE_HASH_OF_MAPS, BPF_MAP_TYPE_ARRAY); + _map_in_map_batch_ops(BPF_MAP_TYPE_HASH_OF_MAPS, BPF_MAP_TYPE_ARRAY, fals= e); printf("%s:PASS with inner ARRAY map\n", __func__); - _map_in_map_batch_ops(BPF_MAP_TYPE_HASH_OF_MAPS, BPF_MAP_TYPE_HASH); + _map_in_map_batch_ops(BPF_MAP_TYPE_HASH_OF_MAPS, BPF_MAP_TYPE_HASH, false= ); printf("%s:PASS with inner HASH map\n", __func__); } --=20 2.39.5