Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

netconsole: Simplify send_fragmented_body()

Refactor send_fragmented_body() to use separate offset tracking for
msgbody, and extradata instead of complex conditional logic.
The previous implementation used boolean flags and calculated offsets
which made the code harder to follow.

The new implementation maintains independent offset counters
(msgbody_offset, extradata_offset) and processes each section
sequentially, making the data flow more straightforward and the code
easier to maintain.

This is a preparatory refactoring with no functional changes, which will
allow easily splitting extradata_complete into separate userdata and
sysdata buffers in the next patch.

Signed-off-by: Gustavo Luiz Duarte <gustavold@gmail.com>
Reviewed-by: Breno Leitao <leitao@debian.org>
Link: https://patch.msgid.link/20251119-netconsole_dynamic_extradata-v3-1-497ac3191707@meta.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>

authored by

Gustavo Luiz Duarte and committed by
Jakub Kicinski
7279b718 920fa394

+45 -64
+45 -64
drivers/net/netconsole.c
··· 1560 1560 } 1561 1561 1562 1562 static void send_fragmented_body(struct netconsole_target *nt, 1563 - const char *msgbody, int header_len, 1563 + const char *msgbody_ptr, int header_len, 1564 1564 int msgbody_len, int extradata_len) 1565 1565 { 1566 - int sent_extradata, preceding_bytes; 1567 - const char *extradata = NULL; 1568 - int body_len, offset = 0; 1566 + const char *extradata_ptr = NULL; 1567 + int data_len, data_sent = 0; 1568 + int extradata_offset = 0; 1569 + int msgbody_offset = 0; 1569 1570 1570 1571 #ifdef CONFIG_NETCONSOLE_DYNAMIC 1571 - extradata = nt->extradata_complete; 1572 + extradata_ptr = nt->extradata_complete; 1572 1573 #endif 1574 + if (WARN_ON_ONCE(!extradata_ptr && extradata_len != 0)) 1575 + return; 1573 1576 1574 - /* body_len represents the number of bytes that will be sent. This is 1577 + /* data_len represents the number of bytes that will be sent. This is 1575 1578 * bigger than MAX_PRINT_CHUNK, thus, it will be split in multiple 1576 1579 * packets 1577 1580 */ 1578 - body_len = msgbody_len + extradata_len; 1581 + data_len = msgbody_len + extradata_len; 1579 1582 1580 1583 /* In each iteration of the while loop below, we send a packet 1581 - * containing the header and a portion of the body. The body is 1584 + * containing the header and a portion of the data. The data is 1582 1585 * composed of two parts: msgbody and extradata. We keep track of how 1583 - * many bytes have been sent so far using the offset variable, which 1584 - * ranges from 0 to the total length of the body. 1586 + * many bytes have been sent so far using the data_sent variable, which 1587 + * ranges from 0 to the total bytes to be sent. 1585 1588 */ 1586 - while (offset < body_len) { 1587 - int this_header = header_len; 1588 - bool msgbody_written = false; 1589 - int this_offset = 0; 1589 + while (data_sent < data_len) { 1590 + int extradata_left = extradata_len - extradata_offset; 1591 + int msgbody_left = msgbody_len - msgbody_offset; 1592 + int buf_offset = 0; 1590 1593 int this_chunk = 0; 1591 1594 1592 - this_header += scnprintf(nt->buf + this_header, 1593 - MAX_PRINT_CHUNK - this_header, 1594 - ",ncfrag=%d/%d;", offset, 1595 - body_len); 1595 + /* header is already populated in nt->buf, just append to it */ 1596 + buf_offset = header_len; 1596 1597 1597 - /* Not all msgbody data has been written yet */ 1598 - if (offset < msgbody_len) { 1599 - this_chunk = min(msgbody_len - offset, 1600 - MAX_PRINT_CHUNK - this_header); 1601 - if (WARN_ON_ONCE(this_chunk <= 0)) 1602 - return; 1603 - memcpy(nt->buf + this_header, msgbody + offset, 1604 - this_chunk); 1605 - this_offset += this_chunk; 1598 + buf_offset += scnprintf(nt->buf + buf_offset, 1599 + MAX_PRINT_CHUNK - buf_offset, 1600 + ",ncfrag=%d/%d;", data_sent, 1601 + data_len); 1602 + 1603 + /* append msgbody first */ 1604 + this_chunk = min(msgbody_left, MAX_PRINT_CHUNK - buf_offset); 1605 + memcpy(nt->buf + buf_offset, msgbody_ptr + msgbody_offset, 1606 + this_chunk); 1607 + msgbody_offset += this_chunk; 1608 + buf_offset += this_chunk; 1609 + data_sent += this_chunk; 1610 + 1611 + /* after msgbody, append extradata */ 1612 + if (extradata_ptr && extradata_left) { 1613 + this_chunk = min(extradata_left, 1614 + MAX_PRINT_CHUNK - buf_offset); 1615 + memcpy(nt->buf + buf_offset, 1616 + extradata_ptr + extradata_offset, this_chunk); 1617 + extradata_offset += this_chunk; 1618 + buf_offset += this_chunk; 1619 + data_sent += this_chunk; 1606 1620 } 1607 1621 1608 - /* msgbody was finally written, either in the previous 1609 - * messages and/or in the current buf. Time to write 1610 - * the extradata. 1611 - */ 1612 - msgbody_written |= offset + this_offset >= msgbody_len; 1622 + /* if all is good, send the packet out */ 1623 + if (WARN_ON_ONCE(data_sent > data_len)) 1624 + return; 1613 1625 1614 - /* Msg body is fully written and there is pending extradata to 1615 - * write, append extradata in this chunk 1616 - */ 1617 - if (msgbody_written && offset + this_offset < body_len) { 1618 - /* Track how much user data was already sent. First 1619 - * time here, sent_userdata is zero 1620 - */ 1621 - sent_extradata = (offset + this_offset) - msgbody_len; 1622 - /* offset of bytes used in current buf */ 1623 - preceding_bytes = this_chunk + this_header; 1624 - 1625 - if (WARN_ON_ONCE(sent_extradata < 0)) 1626 - return; 1627 - 1628 - this_chunk = min(extradata_len - sent_extradata, 1629 - MAX_PRINT_CHUNK - preceding_bytes); 1630 - if (WARN_ON_ONCE(this_chunk < 0)) 1631 - /* this_chunk could be zero if all the previous 1632 - * message used all the buffer. This is not a 1633 - * problem, extradata will be sent in the next 1634 - * iteration 1635 - */ 1636 - return; 1637 - 1638 - memcpy(nt->buf + this_header + this_offset, 1639 - extradata + sent_extradata, 1640 - this_chunk); 1641 - this_offset += this_chunk; 1642 - } 1643 - 1644 - send_udp(nt, nt->buf, this_header + this_offset); 1645 - offset += this_offset; 1626 + send_udp(nt, nt->buf, buf_offset); 1646 1627 } 1647 1628 } 1648 1629