Skip to content

Commit 5da4d7b

Browse files
dhowellskuba-moo
authored andcommitted
libceph: Partially revert changes to support MSG_SPLICE_PAGES
Fix the mishandling of MSG_DONTWAIT and also reinstates the per-page checking of the source pages (which might have come from a DIO write by userspace) by partially reverting the changes to support MSG_SPLICE_PAGES and doing things a little differently. In messenger_v1: (1) The ceph_tcp_sendpage() is resurrected and the callers reverted to use that. (2) The callers now pass MSG_MORE unconditionally. Previously, they were passing in MSG_MORE|MSG_SENDPAGE_NOTLAST and then degrading that to just MSG_MORE on the last call to ->sendpage(). (3) Make ceph_tcp_sendpage() a wrapper around sendmsg() rather than sendpage(), setting MSG_SPLICE_PAGES if sendpage_ok() returns true on the page. In messenger_v2: (4) Bring back do_try_sendpage() and make the callers use that. (5) Make do_try_sendpage() use sendmsg() for both cases and set MSG_SPLICE_PAGES if sendpage_ok() is set. Fixes: 40a8c17 ("ceph: Use sendmsg(MSG_SPLICE_PAGES) rather than sendpage") Fixes: fa094cc ("ceph: Use sendmsg(MSG_SPLICE_PAGES) rather than sendpage()") Reported-by: Ilya Dryomov <idryomov@gmail.com> Link: https://lore.kernel.org/r/CAOi1vP9vjLfk3W+AJFeexC93jqPaPUn2dD_4NrzxwoZTbYfOnw@mail.gmail.com/ Link: https://lore.kernel.org/r/CAOi1vP_Bn918j24S94MuGyn+Gxk212btw7yWeDrRcW1U8pc_BA@mail.gmail.com/ Signed-off-by: David Howells <dhowells@redhat.com> cc: Xiubo Li <xiubli@redhat.com> cc: Jeff Layton <jlayton@kernel.org> cc: Jens Axboe <axboe@kernel.dk> cc: Matthew Wilcox <willy@infradead.org> Link: https://lore.kernel.org/r/3101881.1687801973@warthog.procyon.org.uk/ # v1 Link: https://lore.kernel.org/r/3111635.1687813501@warthog.procyon.org.uk/ # v2 Reviewed-by: Ilya Dryomov <idryomov@gmail.com> Link: https://lore.kernel.org/r/3199652.1687873788@warthog.procyon.org.uk Signed-off-by: Jakub Kicinski <kuba@kernel.org>
1 parent 528a08b commit 5da4d7b

2 files changed

Lines changed: 107 additions & 39 deletions

File tree

net/ceph/messenger_v1.c

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,39 @@ static int ceph_tcp_sendmsg(struct socket *sock, struct kvec *iov,
7474
return r;
7575
}
7676

77+
/*
78+
* @more: MSG_MORE or 0.
79+
*/
80+
static int ceph_tcp_sendpage(struct socket *sock, struct page *page,
81+
int offset, size_t size, int more)
82+
{
83+
struct msghdr msg = {
84+
.msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL | more,
85+
};
86+
struct bio_vec bvec;
87+
int ret;
88+
89+
/*
90+
* MSG_SPLICE_PAGES cannot properly handle pages with page_count == 0,
91+
* we need to fall back to sendmsg if that's the case.
92+
*
93+
* Same goes for slab pages: skb_can_coalesce() allows
94+
* coalescing neighboring slab objects into a single frag which
95+
* triggers one of hardened usercopy checks.
96+
*/
97+
if (sendpage_ok(page))
98+
msg.msg_flags |= MSG_SPLICE_PAGES;
99+
100+
bvec_set_page(&bvec, page, size, offset);
101+
iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, &bvec, 1, size);
102+
103+
ret = sock_sendmsg(sock, &msg);
104+
if (ret == -EAGAIN)
105+
ret = 0;
106+
107+
return ret;
108+
}
109+
77110
static void con_out_kvec_reset(struct ceph_connection *con)
78111
{
79112
BUG_ON(con->v1.out_skip);
@@ -450,10 +483,6 @@ static int write_partial_message_data(struct ceph_connection *con)
450483
*/
451484
crc = do_datacrc ? le32_to_cpu(msg->footer.data_crc) : 0;
452485
while (cursor->total_resid) {
453-
struct bio_vec bvec;
454-
struct msghdr msghdr = {
455-
.msg_flags = MSG_SPLICE_PAGES,
456-
};
457486
struct page *page;
458487
size_t page_offset;
459488
size_t length;
@@ -465,13 +494,8 @@ static int write_partial_message_data(struct ceph_connection *con)
465494
}
466495

467496
page = ceph_msg_data_next(cursor, &page_offset, &length);
468-
if (length != cursor->total_resid)
469-
msghdr.msg_flags |= MSG_MORE;
470-
471-
bvec_set_page(&bvec, page, length, page_offset);
472-
iov_iter_bvec(&msghdr.msg_iter, ITER_SOURCE, &bvec, 1, length);
473-
474-
ret = sock_sendmsg(con->sock, &msghdr);
497+
ret = ceph_tcp_sendpage(con->sock, page, page_offset, length,
498+
MSG_MORE);
475499
if (ret <= 0) {
476500
if (do_datacrc)
477501
msg->footer.data_crc = cpu_to_le32(crc);
@@ -501,22 +525,14 @@ static int write_partial_message_data(struct ceph_connection *con)
501525
*/
502526
static int write_partial_skip(struct ceph_connection *con)
503527
{
504-
struct bio_vec bvec;
505-
struct msghdr msghdr = {
506-
.msg_flags = MSG_SPLICE_PAGES | MSG_MORE,
507-
};
508528
int ret;
509529

510530
dout("%s %p %d left\n", __func__, con, con->v1.out_skip);
511531
while (con->v1.out_skip > 0) {
512532
size_t size = min(con->v1.out_skip, (int)PAGE_SIZE);
513533

514-
if (size == con->v1.out_skip)
515-
msghdr.msg_flags &= ~MSG_MORE;
516-
bvec_set_page(&bvec, ZERO_PAGE(0), size, 0);
517-
iov_iter_bvec(&msghdr.msg_iter, ITER_SOURCE, &bvec, 1, size);
518-
519-
ret = sock_sendmsg(con->sock, &msghdr);
534+
ret = ceph_tcp_sendpage(con->sock, ceph_zero_page, 0, size,
535+
MSG_MORE);
520536
if (ret <= 0)
521537
goto out;
522538
con->v1.out_skip -= ret;

net/ceph/messenger_v2.c

Lines changed: 70 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -117,38 +117,90 @@ static int ceph_tcp_recv(struct ceph_connection *con)
117117
return ret;
118118
}
119119

120+
static int do_sendmsg(struct socket *sock, struct iov_iter *it)
121+
{
122+
struct msghdr msg = { .msg_flags = CEPH_MSG_FLAGS };
123+
int ret;
124+
125+
msg.msg_iter = *it;
126+
while (iov_iter_count(it)) {
127+
ret = sock_sendmsg(sock, &msg);
128+
if (ret <= 0) {
129+
if (ret == -EAGAIN)
130+
ret = 0;
131+
return ret;
132+
}
133+
134+
iov_iter_advance(it, ret);
135+
}
136+
137+
WARN_ON(msg_data_left(&msg));
138+
return 1;
139+
}
140+
141+
static int do_try_sendpage(struct socket *sock, struct iov_iter *it)
142+
{
143+
struct msghdr msg = { .msg_flags = CEPH_MSG_FLAGS };
144+
struct bio_vec bv;
145+
int ret;
146+
147+
if (WARN_ON(!iov_iter_is_bvec(it)))
148+
return -EINVAL;
149+
150+
while (iov_iter_count(it)) {
151+
/* iov_iter_iovec() for ITER_BVEC */
152+
bvec_set_page(&bv, it->bvec->bv_page,
153+
min(iov_iter_count(it),
154+
it->bvec->bv_len - it->iov_offset),
155+
it->bvec->bv_offset + it->iov_offset);
156+
157+
/*
158+
* MSG_SPLICE_PAGES cannot properly handle pages with
159+
* page_count == 0, we need to fall back to sendmsg if
160+
* that's the case.
161+
*
162+
* Same goes for slab pages: skb_can_coalesce() allows
163+
* coalescing neighboring slab objects into a single frag
164+
* which triggers one of hardened usercopy checks.
165+
*/
166+
if (sendpage_ok(bv.bv_page))
167+
msg.msg_flags |= MSG_SPLICE_PAGES;
168+
else
169+
msg.msg_flags &= ~MSG_SPLICE_PAGES;
170+
171+
iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, &bv, 1, bv.bv_len);
172+
ret = sock_sendmsg(sock, &msg);
173+
if (ret <= 0) {
174+
if (ret == -EAGAIN)
175+
ret = 0;
176+
return ret;
177+
}
178+
179+
iov_iter_advance(it, ret);
180+
}
181+
182+
return 1;
183+
}
184+
120185
/*
121186
* Write as much as possible. The socket is expected to be corked,
122187
* so we don't bother with MSG_MORE here.
123188
*
124189
* Return:
125-
* >0 - done, nothing (else) to write
190+
* 1 - done, nothing (else) to write
126191
* 0 - socket is full, need to wait
127192
* <0 - error
128193
*/
129194
static int ceph_tcp_send(struct ceph_connection *con)
130195
{
131-
struct msghdr msg = {
132-
.msg_iter = con->v2.out_iter,
133-
.msg_flags = CEPH_MSG_FLAGS,
134-
};
135196
int ret;
136197

137-
if (WARN_ON(!iov_iter_is_bvec(&con->v2.out_iter)))
138-
return -EINVAL;
139-
140-
if (con->v2.out_iter_sendpage)
141-
msg.msg_flags |= MSG_SPLICE_PAGES;
142-
143198
dout("%s con %p have %zu try_sendpage %d\n", __func__, con,
144199
iov_iter_count(&con->v2.out_iter), con->v2.out_iter_sendpage);
145-
146-
ret = sock_sendmsg(con->sock, &msg);
147-
if (ret > 0)
148-
iov_iter_advance(&con->v2.out_iter, ret);
149-
else if (ret == -EAGAIN)
150-
ret = 0;
151-
200+
if (con->v2.out_iter_sendpage)
201+
ret = do_try_sendpage(con->sock, &con->v2.out_iter);
202+
else
203+
ret = do_sendmsg(con->sock, &con->v2.out_iter);
152204
dout("%s con %p ret %d left %zu\n", __func__, con, ret,
153205
iov_iter_count(&con->v2.out_iter));
154206
return ret;

0 commit comments

Comments
 (0)