[PATCH] nvmet-tcp: finish receiving before send back response if nvmet_req_init() failed.
Hou Pu
houpu.main at gmail.com
Tue Mar 16 05:24:34 GMT 2021
On 2021/3/16 1:32 AM, Sagi Grimberg wrote:
>
>> When receiving a pdu, if nvmet_req_init() failed (for example a ns is
>> not found), the queue->rcv_state could be moved to NVMET_TCP_RECV_DATA
>> by nvmet_tcp_handle_req_failure(). We should return 0 here to continue
>> to consume the possible remaining inline write out data in
>> nvmet_tcp_try_recv_one(). Otherwise, the response to this request would
>> be sent and iov would be freed. Next time in nvmet_tcp_try_recv_one(),
>> we would go to the receiving data phase and the iov is used again.
>>
>> A panic happend with a 5.4 kernel installed as below:
>
> Can you please try to reproduce this with upstream? and with latest
> stable 5.4? there have been some fixes in this area. We may need
> to help backported patches to stable if needed.
This could be reproduced on the latest stable (5.4.105). I tried with
upstream
(5.12-rc3), it could not be reproduced. But I thought the bug still
exist in upstream,
then I added the following changes to catch such use after problems.
diff --git a/drivers/nvme/target/tcp.c b/drivers/nvme/target/tcp.c
index 8b0485ada315..46847ccf4395 100644
--- a/drivers/nvme/target/tcp.c
+++ b/drivers/nvme/target/tcp.c
@@ -217,6 +217,16 @@ static inline void nvmet_tcp_put_cmd(struct
nvmet_tcp_cmd *cmd)
list_add_tail(&cmd->entry, &cmd->queue->free_list);
}
+static inline bool nvmet_tcp_cmd_freed(struct nvmet_tcp_cmd *cmd)
+{
+ /* When a cmd is first geted, list_del_init() is called, it could be
+ * used to tell if it is on free list
+ */
+ if (cmd->entry.next == &cmd->entry && cmd->entry.prev ==
&cmd->entry)
+ return false;
+ return true;
+}
+
static inline int queue_cpu(struct nvmet_tcp_queue *queue)
{
return queue->sock->sk->sk_incoming_cpu;
@@ -1088,6 +1098,9 @@ static int nvmet_tcp_try_recv_data(struct
nvmet_tcp_queue *queue)
struct nvmet_tcp_cmd *cmd = queue->cmd;
int ret;
+ if (nvmet_tcp_cmd_freed(cmd))
+ pr_err("cmd=%p which is freed is used again\n", cmd);
+
while (msg_data_left(&cmd->recv_msg)) {
ret = sock_recvmsg(cmd->queue->sock, &cmd->recv_msg,
cmd->recv_msg.msg_flags);
The dmesg shows:
[ 80.058535] nvmet_tcp: failed cmd 0000000074710fda id 83 opcode 1,
data_len: 1024
[ 80.060225] nvmet_tcp: cmd=00000000b470d96a which is freed is used again
[ 80.060903] nvmet_tcp: failed cmd 000000007d648e5e id 84 opcode 1,
data_len: 1024
[ 80.061377] nvmet_tcp: cmd=000000002f3194cb which is freed is used again
[ 80.061656] nvmet_tcp: failed cmd 0000000047cc966a id 85 opcode 2,
data_len: 1024
[ 80.066657] nvmet_tcp: failed cmd 000000005319c9b2 id 86 opcode 1,
data_len: 1024
[ 80.067325] nvmet_tcp: cmd=00000000245c70d6 which is freed is used again
So, I think the upstream also has this bug.
How to reproduce:
1. Runing fio from initiator:
fio -bs=1024 -filename=/dev/nvme1n1 -rw=randrw -direct=1
-ioengine=libaio -numjobs=4 -time_based=1 -runtime=600 -iodepth=1 -name=t
2. disable namespace 1 from target by :
echo 0 > /sys/kernel/config/nvmet/subsystems/mysub/namespaces/1/enable
[..]
>>
>> drivers/nvme/target/tcp.c | 2 +-
>> 1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/drivers/nvme/target/tcp.c b/drivers/nvme/target/tcp.c
>> index 8b0485ada315..da1c667e21ba 100644
>> --- a/drivers/nvme/target/tcp.c
>> +++ b/drivers/nvme/target/tcp.c
>> @@ -961,7 +961,7 @@ static int nvmet_tcp_done_recv_pdu(struct
>> nvmet_tcp_queue *queue)
>> le32_to_cpu(req->cmd->common.dptr.sgl.length));
>> nvmet_tcp_handle_req_failure(queue, queue->cmd, req);
>> - return -EAGAIN;
>> + return 0;
>
> What guarantees that you will actually have more to consume?
In my case, write 1024 bytes request need receive inline data.
After receive cmd pdu, the following inline data still need to be read
from the socket.
Thanks,
Hou
More information about the Linux-nvme
mailing list