Skip to content

Commit 807ae7d

Browse files
committed
Merge branch 'bpf-sockmap-sg-api-fixes'
Prashant Bhole says: ==================== These patches fix sg api usage in sockmap. Previously sockmap didn't use sg_init_table(), which caused hitting BUG_ON in sg api, when CONFIG_DEBUG_SG is enabled v1: added sg_init_table() calls wherever needed. v2: - Patch1 adds new helper function in sg api. sg_init_marker() - Patch2 sg_init_marker() and sg_init_table() in appropriate places Backgroud: While reviewing v1, John Fastabend raised a valid point about unnecessary memset in sg_init_table() because sockmap uses sg table which embedded in a struct. As enclosing struct is zeroed out, there is unnecessary memset in sg_init_table. So Daniel Borkmann suggested to define another static inline function in scatterlist.h which only initializes sg_magic. Also this function will be called from sg_init_table. From this suggestion I defined a function sg_init_marker() which sets sg_magic and calls sg_mark_end() ==================== Signed-off-by: Daniel Borkmann <[email protected]>
2 parents 1379ef8 + 6ef6d84 commit 807ae7d

File tree

3 files changed

+27
-13
lines changed

3 files changed

+27
-13
lines changed

include/linux/scatterlist.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,24 @@ static inline void *sg_virt(struct scatterlist *sg)
248248
return page_address(sg_page(sg)) + sg->offset;
249249
}
250250

251+
/**
252+
* sg_init_marker - Initialize markers in sg table
253+
* @sgl: The SG table
254+
* @nents: Number of entries in table
255+
*
256+
**/
257+
static inline void sg_init_marker(struct scatterlist *sgl,
258+
unsigned int nents)
259+
{
260+
#ifdef CONFIG_DEBUG_SG
261+
unsigned int i;
262+
263+
for (i = 0; i < nents; i++)
264+
sgl[i].sg_magic = SG_MAGIC;
265+
#endif
266+
sg_mark_end(&sgl[nents - 1]);
267+
}
268+
251269
int sg_nents(struct scatterlist *sg);
252270
int sg_nents_for_len(struct scatterlist *sg, u64 len);
253271
struct scatterlist *sg_next(struct scatterlist *);

kernel/bpf/sockmap.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ static int bpf_tcp_push(struct sock *sk, int apply_bytes,
341341
md->sg_start++;
342342
if (md->sg_start == MAX_SKB_FRAGS)
343343
md->sg_start = 0;
344-
memset(sg, 0, sizeof(*sg));
344+
sg_init_table(sg, 1);
345345

346346
if (md->sg_start == md->sg_end)
347347
break;
@@ -843,7 +843,7 @@ static int bpf_tcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
843843
}
844844

845845
sg = md.sg_data;
846-
sg_init_table(sg, MAX_SKB_FRAGS);
846+
sg_init_marker(sg, MAX_SKB_FRAGS);
847847
rcu_read_unlock();
848848

849849
lock_sock(sk);
@@ -950,18 +950,21 @@ static int bpf_tcp_sendpage(struct sock *sk, struct page *page,
950950

951951
lock_sock(sk);
952952

953-
if (psock->cork_bytes)
953+
if (psock->cork_bytes) {
954954
m = psock->cork;
955-
else
955+
sg = &m->sg_data[m->sg_end];
956+
} else {
956957
m = &md;
958+
sg = m->sg_data;
959+
sg_init_marker(sg, MAX_SKB_FRAGS);
960+
}
957961

958962
/* Catch case where ring is full and sendpage is stalled. */
959963
if (unlikely(m->sg_end == m->sg_start &&
960964
m->sg_data[m->sg_end].length))
961965
goto out_err;
962966

963967
psock->sg_size += size;
964-
sg = &m->sg_data[m->sg_end];
965968
sg_set_page(sg, page, size, offset);
966969
get_page(page);
967970
m->sg_copy[m->sg_end] = true;

lib/scatterlist.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,7 @@ EXPORT_SYMBOL(sg_last);
132132
void sg_init_table(struct scatterlist *sgl, unsigned int nents)
133133
{
134134
memset(sgl, 0, sizeof(*sgl) * nents);
135-
#ifdef CONFIG_DEBUG_SG
136-
{
137-
unsigned int i;
138-
for (i = 0; i < nents; i++)
139-
sgl[i].sg_magic = SG_MAGIC;
140-
}
141-
#endif
142-
sg_mark_end(&sgl[nents - 1]);
135+
sg_init_marker(sgl, nents);
143136
}
144137
EXPORT_SYMBOL(sg_init_table);
145138

0 commit comments

Comments
 (0)