-
Notifications
You must be signed in to change notification settings - Fork 146
Task local data #9315
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: bpf-next_base
Are you sure you want to change the base?
Task local data #9315
Conversation
Upstream branch: 0768e98 |
1c89975
to
2bf6e60
Compare
Upstream branch: 0768e98 |
32efc6b
to
b135040
Compare
2bf6e60
to
63bd3e4
Compare
Upstream branch: 0ee30d9 |
b135040
to
d32b562
Compare
63bd3e4
to
1c4f46e
Compare
Upstream branch: 0ee30d9 |
d32b562
to
3f9cb47
Compare
1c4f46e
to
b5a617e
Compare
Upstream branch: beb1097 |
3f9cb47
to
eeae154
Compare
b5a617e
to
ffefc6d
Compare
Task local data defines an abstract storage type for storing task- specific data (TLD). This patch provides user space and bpf implementation as header-only libraries for accessing task local data. Task local data is a bpf task local storage map with two UPTRs: 1) u_tld_metadata, shared by all tasks of the same process, consists of the total count of TLDs and an array of metadata of TLDs. A metadata of a TLD comprises the size and the name. The name is used to identify a specific TLD in bpf 2) u_tld_data points to a task-specific memory region for storing TLDs. Below are the core task local data API: User space BPF Define TLD TLD_DEFINE_KEY(), tld_create_key() - Get data tld_get_data() tld_get_data() A TLD is first defined by the user space with TLD_DEFINE_KEY() or tld_create_key(). TLD_DEFINE_KEY() defines a TLD statically and allocates just enough memory during initialization. tld_create_key() allows creating TLDs on the fly, but has a fix memory budget, TLD_DYN_DATA_SIZE. Internally, they all go through the metadata array to check if the TLD can be added. The total TLD size needs to fit into a page (limited by UPTR), and no two TLDs can have the same name. It also calculates the offset, the next available space in u_tld_data, by summing sizes of TLDs. If the TLD can be added, it increases the count using cmpxchg as there may be other concurrent tld_create_key(). After a successful cmpxchg, the last metadata slot now belongs to the calling thread and will be updated. tld_create_key() returns the offset encapsulated as a opaque object key to prevent user misuse. Then, user space can pass the key to tld_get_data() to get a pointer to the TLD. The pointer will remain valid for the lifetime of the thread. BPF programs can also locate the TLD by tld_get_data(), but with both name and key. The first time tld_get_data() is called, the name will be used to lookup the metadata. Then, the key will be saved to a task_local_data map, tld_keys_map. Subsequent call to tld_get_data() will use the key to quickly locate the data. User space task local data library uses a light way approach to ensure thread safety (i.e., atomic operation + compiler and memory barriers). While a metadata is being updated, other threads may also try to read it. To prevent them from seeing incomplete data, metadata::size is used to signal the completion of the update, where 0 means the update is still ongoing. Threads will wait until seeing a non-zero size to read a metadata. Signed-off-by: Amery Hung <[email protected]>
Test basic operations of task local data with valid and invalid tld_create_key(). For invalid calls, make sure they return the right error code and check that the TLDs are not inserted by running tld_get_data(" value_not_exists") on the bpf side. The call should a null pointer. For valid calls, first make sure the TLDs are created by calling tld_get_data() on the bpf side. The call should return a valid pointer. Finally, verify that the TLDs are indeed task-specific (i.e., their addresses do not overlap) with multiple user threads. This done by writing values unique to each thread, reading them from both user space and bpf, and checking if the value read back matches the value written. Signed-off-by: Amery Hung <[email protected]>
Test thread-safety of tld_create_key(). Since tld_create_key() does not rely on locks but memory barriers and atomic operations to protect the shared metadata, the thread-safety of the function is non-trivial. Make sure concurrent tld_key_create(), both valid and invalid, can not race and corrupt metatada, which may leads to TLDs not being thread- specific or duplicate TLDs with the same name. Signed-off-by: Amery Hung <[email protected]>
Upstream branch: 42be23e |
eeae154
to
166f1e1
Compare
Pull request for series with
subject: Task local data
version: 6
url: https://patchwork.kernel.org/project/netdevbpf/list/?series=983442