-
Notifications
You must be signed in to change notification settings - Fork 8.2k
drivers: eswifi: Fix memory buffer allocation in off_read_work #76483
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -133,33 +133,40 @@ static void eswifi_off_read_work(struct k_work *work) | |
|
|
||
| __select_socket(eswifi, socket->index); | ||
|
|
||
| /* Verify if we can allocate a rx packet before reading data to prevent leaks */ | ||
| pkt = net_pkt_rx_alloc_with_buffer(eswifi->iface, 1460, | ||
| AF_UNSPEC, 0, K_NO_WAIT); | ||
| if (!pkt) { | ||
| LOG_ERR("Cannot allocate rx packet"); | ||
| goto done; | ||
| } | ||
|
|
||
| len = __read_data(eswifi, 1460, &data); /* 1460 is max size */ | ||
| if (len < 0) { | ||
| __stop_socket(eswifi, socket); | ||
|
|
||
| if (socket->recv_cb) { | ||
| /* send EOF (null pkt) */ | ||
| net_pkt_unref(pkt); | ||
| pkt = NULL; | ||
|
||
| goto do_recv_cb; | ||
| } | ||
| } | ||
|
|
||
| if (!len || !socket->recv_cb) { | ||
| net_pkt_unref(pkt); | ||
| goto done; | ||
| } | ||
|
|
||
| LOG_DBG("payload sz = %d", len); | ||
|
|
||
| pkt = net_pkt_rx_alloc_with_buffer(eswifi->iface, len, | ||
| AF_UNSPEC, 0, K_NO_WAIT); | ||
| if (!pkt) { | ||
| LOG_ERR("Cannot allocate rx packet"); | ||
| goto done; | ||
| } | ||
|
|
||
| if (net_pkt_write(pkt, data, len) < 0) { | ||
| LOG_WRN("Incomplete buffer copy"); | ||
| } | ||
|
|
||
| /* Resize the packet */ | ||
| net_pkt_trim_buffer(pkt); | ||
|
||
|
|
||
| net_pkt_cursor_init(pkt); | ||
|
|
||
| do_recv_cb: | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So this means that we always try to allocate the "full" buffer regardless of how much data there is, this could be wasting lot of net_buf's.
Is it possible to figure out how much data there is to read (without actually reading it), and then allocate just that amount?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Absolutely, I’ll check tomorrow if there is an API to check the size of the pending packet or if we can resize the buffer we have allocated. Thanks !
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, I pushed a new commit to fix the issue of allocation. My strategy is to allocate the buffer with MTU, then read and then resize with the API net_pkt_update_length() so that we allocate the returned length of read function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I think I did a mistake, net_pkt_update_length() only modify length attribute of net_pkt, I'll try to resize the pkt by creating a new pkt to the exact size thanks to read return and then copy the old pkt into the new pkt and then unref the old pkt