Skip to content

Commit 75c2aeb

Browse files
jori-nordiccarlescufi
authored andcommitted
Bluetooth: L2CAP: stop stealing buffers from SDU pool
It seems like a nice idea at first, but leads to hard-to-debug situations for the application. The previous behavior can be implemented by the app by defining `alloc_seg` and allocating from the same pool as `buf`. Signed-off-by: Jonathan Rico <[email protected]>
1 parent 7b42e36 commit 75c2aeb

File tree

3 files changed

+16
-12
lines changed

3 files changed

+16
-12
lines changed

doc/releases/migration-guide-3.6.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,10 @@ Bluetooth
241241
Any pointer to a UUID must be prefixed with `const`, otherwise there will be a compilation warning.
242242
For example change ``struct bt_uuid *uuid = BT_UUID_DECLARE_16(xx)`` to
243243
``const struct bt_uuid *uuid = BT_UUID_DECLARE_16(xx)``. (:github:`66136`)
244+
* The :c:func:`bt_l2cap_chan_send` API no longer allocates buffers from the same pool as its `buf`
245+
parameter when segmenting SDUs into PDUs. In order to reproduce the previous behavior, the
246+
application should register the `alloc_seg` channel callback and allocate from the same pool as
247+
`buf`.
244248

245249
* Mesh
246250

include/zephyr/bluetooth/l2cap.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -587,9 +587,10 @@ int bt_l2cap_chan_disconnect(struct bt_l2cap_chan *chan);
587587
* If the application is reserving the bytes it should use the
588588
* BT_L2CAP_BUF_SIZE() helper to correctly size the buffers for the for the
589589
* outgoing buffer pool.
590-
* When segmenting an L2CAP SDU into L2CAP PDUs the stack will first attempt
591-
* to allocate buffers from the original buffer pool of the L2CAP SDU before
592-
* using the stacks own buffer pool.
590+
* When segmenting an L2CAP SDU into L2CAP PDUs the stack will first attempt to
591+
* allocate buffers from the channel's `alloc_seg` callback and will fallback
592+
* on the stack's global buffer pool (sized
593+
* @kconfig{CONFIG_BT_L2CAP_TX_BUF_COUNT}).
593594
*
594595
* @note Buffer ownership is transferred to the stack in case of success, in
595596
* case of an error the caller retains the ownership of the buffer.

subsys/bluetooth/host/l2cap.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1818,25 +1818,24 @@ static void le_disconn_rsp(struct bt_l2cap *l2cap, uint8_t ident,
18181818

18191819
static inline struct net_buf *l2cap_alloc_seg(struct net_buf *buf, struct bt_l2cap_le_chan *ch)
18201820
{
1821-
struct net_buf_pool *pool = net_buf_pool_get(buf->pool_id);
1822-
struct net_buf *seg;
1821+
struct net_buf *seg = NULL;
18231822

1824-
/* Use the dedicated segment callback if registered */
1823+
/* Use the user-defined allocator */
18251824
if (ch->chan.ops->alloc_seg) {
18261825
seg = ch->chan.ops->alloc_seg(&ch->chan);
18271826
__ASSERT_NO_MSG(seg);
1828-
} else {
1829-
/* Try to use original pool if possible */
1830-
seg = net_buf_alloc(pool, K_NO_WAIT);
1827+
}
1828+
1829+
/* Fallback to using global connection tx pool */
1830+
if (!seg) {
1831+
seg = bt_l2cap_create_pdu_timeout(NULL, 0, K_NO_WAIT);
18311832
}
18321833

18331834
if (seg) {
18341835
net_buf_reserve(seg, BT_L2CAP_CHAN_SEND_RESERVE);
1835-
return seg;
18361836
}
18371837

1838-
/* Fallback to using global connection tx pool */
1839-
return bt_l2cap_create_pdu_timeout(NULL, 0, K_NO_WAIT);
1838+
return seg;
18401839
}
18411840

18421841
static struct net_buf *l2cap_chan_create_seg(struct bt_l2cap_le_chan *ch,

0 commit comments

Comments
 (0)