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

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: 40a8c17aa770 ("ceph: Use sendmsg(MSG_SPLICE_PAGES) rather than sendpage")
Fixes: fa094ccae1e7 ("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>

authored by

David Howells and committed by
Jakub Kicinski
5da4d7b8 528a08bc

+107 -39
+37 -21
net/ceph/messenger_v1.c
··· 74 74 return r; 75 75 } 76 76 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 + 77 110 static void con_out_kvec_reset(struct ceph_connection *con) 78 111 { 79 112 BUG_ON(con->v1.out_skip); ··· 483 450 */ 484 451 crc = do_datacrc ? le32_to_cpu(msg->footer.data_crc) : 0; 485 452 while (cursor->total_resid) { 486 - struct bio_vec bvec; 487 - struct msghdr msghdr = { 488 - .msg_flags = MSG_SPLICE_PAGES, 489 - }; 490 453 struct page *page; 491 454 size_t page_offset; 492 455 size_t length; ··· 494 465 } 495 466 496 467 page = ceph_msg_data_next(cursor, &page_offset, &length); 497 - if (length != cursor->total_resid) 498 - msghdr.msg_flags |= MSG_MORE; 499 - 500 - bvec_set_page(&bvec, page, length, page_offset); 501 - iov_iter_bvec(&msghdr.msg_iter, ITER_SOURCE, &bvec, 1, length); 502 - 503 - ret = sock_sendmsg(con->sock, &msghdr); 468 + ret = ceph_tcp_sendpage(con->sock, page, page_offset, length, 469 + MSG_MORE); 504 470 if (ret <= 0) { 505 471 if (do_datacrc) 506 472 msg->footer.data_crc = cpu_to_le32(crc); ··· 525 501 */ 526 502 static int write_partial_skip(struct ceph_connection *con) 527 503 { 528 - struct bio_vec bvec; 529 - struct msghdr msghdr = { 530 - .msg_flags = MSG_SPLICE_PAGES | MSG_MORE, 531 - }; 532 504 int ret; 533 505 534 506 dout("%s %p %d left\n", __func__, con, con->v1.out_skip); 535 507 while (con->v1.out_skip > 0) { 536 508 size_t size = min(con->v1.out_skip, (int)PAGE_SIZE); 537 509 538 - if (size == con->v1.out_skip) 539 - msghdr.msg_flags &= ~MSG_MORE; 540 - bvec_set_page(&bvec, ZERO_PAGE(0), size, 0); 541 - iov_iter_bvec(&msghdr.msg_iter, ITER_SOURCE, &bvec, 1, size); 542 - 543 - ret = sock_sendmsg(con->sock, &msghdr); 510 + ret = ceph_tcp_sendpage(con->sock, ceph_zero_page, 0, size, 511 + MSG_MORE); 544 512 if (ret <= 0) 545 513 goto out; 546 514 con->v1.out_skip -= ret;
+70 -18
net/ceph/messenger_v2.c
··· 117 117 return ret; 118 118 } 119 119 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 + 120 185 /* 121 186 * Write as much as possible. The socket is expected to be corked, 122 187 * so we don't bother with MSG_MORE here. 123 188 * 124 189 * Return: 125 - * >0 - done, nothing (else) to write 190 + * 1 - done, nothing (else) to write 126 191 * 0 - socket is full, need to wait 127 192 * <0 - error 128 193 */ 129 194 static int ceph_tcp_send(struct ceph_connection *con) 130 195 { 131 - struct msghdr msg = { 132 - .msg_iter = con->v2.out_iter, 133 - .msg_flags = CEPH_MSG_FLAGS, 134 - }; 135 196 int ret; 136 - 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 197 143 198 dout("%s con %p have %zu try_sendpage %d\n", __func__, con, 144 199 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); 152 204 dout("%s con %p ret %d left %zu\n", __func__, con, ret, 153 205 iov_iter_count(&con->v2.out_iter)); 154 206 return ret;