[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20250422-afabre-traits-010-rfc2-v2-6-92bcc6b146c9@arthurfabre.com>
Date: Tue, 22 Apr 2025 15:23:35 +0200
From: Arthur Fabre <arthur@...hurfabre.com>
To: netdev@...r.kernel.org, bpf@...r.kernel.org
Cc: jakub@...udflare.com, hawk@...nel.org, yan@...udflare.com,
jbrandeburg@...udflare.com, thoiland@...hat.com, lbiancon@...hat.com,
ast@...nel.org, kuba@...nel.org, edumazet@...gle.com,
Arthur Fabre <arthur@...hurfabre.com>
Subject: [PATCH RFC bpf-next v2 06/17] trait: Replace memcpy calls with
inline copies
When copying trait values to or from the caller, the size isn't a
constant so memcpy() ends up being a function call.
Replace it with an inline implementation that only handles the sizes we
support.
We store values "packed", so they won't necessarily be 4 or 8 byte
aligned.
Setting and getting traits is roughly ~40% faster.
Signed-off-by: Arthur Fabre <arthur@...hurfabre.com>
---
include/net/trait.h | 21 ++++++++++++++-------
1 file changed, 14 insertions(+), 7 deletions(-)
diff --git a/include/net/trait.h b/include/net/trait.h
index af42c1ad2416d5c38631f1f0305ef9fefe43bd87..4013351549731c4e3bede211dbe9fbe651556dc9 100644
--- a/include/net/trait.h
+++ b/include/net/trait.h
@@ -7,6 +7,7 @@
#include <linux/errno.h>
#include <linux/string.h>
#include <linux/bitops.h>
+#include <linux/unaligned.h>
/* Traits are a very limited KV store, with:
* - 64 keys (0-63).
@@ -144,23 +145,21 @@ int trait_set(void *traits, void *hard_end, u64 key, const void *val, u64 len, u
memmove(traits + off + len, traits + off, traits_size(traits) - off);
}
- /* Set our value. */
- memcpy(traits + off, val, len);
-
- /* Store our length in header. */
u64 encode_len = 0;
-
switch (len) {
case 0:
encode_len = 1;
break;
case 4:
+ put_unaligned(*(u32 *)val, (u32 *)(traits + off));
encode_len = 2;
break;
case 8:
+ put_unaligned(*(u64 *)val, (u64 *)(traits + off));
encode_len = 3;
break;
}
+
h->high |= (encode_len >> 1) << key;
h->low |= (encode_len & 1) << key;
return 0;
@@ -195,7 +194,7 @@ int trait_is_set(void *traits, u64 key)
* @val_len: The length of val.
*
* Return:
- * * %>0 - Actual size of value.
+ * * %>=0 - Actual size of value.
* * %-EINVAL - Key or length invalid.
* * %-ENOENT - Key has not been set with trait_set() previously.
* * %-ENOSPC - Val is not big enough to hold stored value.
@@ -221,7 +220,15 @@ int trait_get(void *traits, u64 key, void *val, u64 val_len)
if (real_len > val_len)
return -ENOSPC;
- memcpy(val, traits + off, real_len);
+ switch (real_len) {
+ case 4:
+ *(u32 *)val = get_unaligned((u32 *)(traits + off));
+ break;
+ case 8:
+ *(u64 *)val = get_unaligned((u64 *)(traits + off));
+ break;
+ }
+
return real_len;
}
--
2.43.0
Powered by blists - more mailing lists