From nobody Fri Dec 19 15:59:32 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BA7BEC6FA8F for ; Tue, 29 Aug 2023 09:14:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234438AbjH2JOQ (ORCPT ); Tue, 29 Aug 2023 05:14:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51684 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234489AbjH2JNr (ORCPT ); Tue, 29 Aug 2023 05:13:47 -0400 Received: from smtp-out1.suse.de (smtp-out1.suse.de [IPv6:2001:67c:2178:6::1c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 56A441B1 for ; Tue, 29 Aug 2023 02:13:40 -0700 (PDT) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id B273121865; Tue, 29 Aug 2023 09:13:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1693300418; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=NG1Qwe/WQzn5azjwmrwbfCg0wFPF7HJfxfYNGD1R4lU=; b=C1n8p0O15bSrHcTHtAA8ZZk0+fz+QOSAdWYxSuzrh8NIP2darW85anPSxaRzYpacGMUE/P 822CXr5PZ1kDfbrU1Ok/tMFc/Ybs7JD8YH/VxzBgRK/1pI533Jgt+iN6YwXpM68Zk4Q/cR 02g3b394hZC78jQ/srcKAlV3bTWQabs= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1693300418; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=NG1Qwe/WQzn5azjwmrwbfCg0wFPF7HJfxfYNGD1R4lU=; b=oiGZnWY4IpTAhpK4Mm7to+YCbWtJFgxB/AC8JG+HM1yFE1En13jRpTI/rbuUkCADLBdJTN S0ucFGfSxeNmdTDg== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id A19A813301; Tue, 29 Aug 2023 09:13:38 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id 2yVbJ8K27WSwUwAAMHmgww (envelope-from ); Tue, 29 Aug 2023 09:13:38 +0000 From: Daniel Wagner To: linux-nvme@lists.infradead.org Cc: linux-kernel@vger.kernel.org, Hannes Reinecke , Sagi Grimberg , Jason Gunthorpe , James Smart , Chaitanya Kulkarni , Christoph Hellwig , Daniel Wagner Subject: [RFC v1 1/4] nvmet-trace: avoid dereferencing pointer too early Date: Tue, 29 Aug 2023 11:13:46 +0200 Message-ID: <20230829091350.16156-2-dwagner@suse.de> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230829091350.16156-1-dwagner@suse.de> References: <20230829091350.16156-1-dwagner@suse.de> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The first command issued from the host to the target is the fabrics connect command. At this point, neither the target queue nor the controller have been allocated. But we already try to trace this command in nvmet_req_init. Reported by KASAN. Signed-off-by: Daniel Wagner Reviewed-by: Hannes Reinecke --- drivers/nvme/target/trace.c | 6 +++--- drivers/nvme/target/trace.h | 24 +++++++++++++----------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/drivers/nvme/target/trace.h b/drivers/nvme/target/trace.h index 6109b3806b12..6997bd7e45cf 100644 --- a/drivers/nvme/target/trace.h +++ b/drivers/nvme/target/trace.h @@ -32,18 +32,20 @@ const char *nvmet_trace_parse_fabrics_cmd(struct trace_= seq *p, u8 fctype, nvmet_trace_parse_nvm_cmd(p, opcode, cdw10) : \ nvmet_trace_parse_admin_cmd(p, opcode, cdw10))) =20 -const char *nvmet_trace_ctrl_name(struct trace_seq *p, struct nvmet_ctrl *= ctrl); -#define __print_ctrl_name(ctrl) \ - nvmet_trace_ctrl_name(p, ctrl) +const char *nvmet_trace_ctrl_id(struct trace_seq *p, u16 ctrl_id); +#define __print_ctrl_id(ctrl_id) \ + nvmet_trace_ctrl_id(p, ctrl_id) =20 const char *nvmet_trace_disk_name(struct trace_seq *p, char *name); #define __print_disk_name(name) \ nvmet_trace_disk_name(p, name) =20 #ifndef TRACE_HEADER_MULTI_READ -static inline struct nvmet_ctrl *nvmet_req_to_ctrl(struct nvmet_req *req) +static inline u16 nvmet_req_to_ctrl_id(struct nvmet_req *req) { - return req->sq->ctrl; + if (!req->sq || !req->sq->ctrl) + return 0; + return req->sq->ctrl->cntlid; } =20 static inline void __assign_req_name(char *name, struct nvmet_req *req) @@ -63,7 +65,7 @@ TRACE_EVENT(nvmet_req_init, TP_ARGS(req, cmd), TP_STRUCT__entry( __field(struct nvme_command *, cmd) - __field(struct nvmet_ctrl *, ctrl) + __field(u16, ctrl_id) __array(char, disk, DISK_NAME_LEN) __field(int, qid) __field(u16, cid) @@ -76,7 +78,7 @@ TRACE_EVENT(nvmet_req_init, ), TP_fast_assign( __entry->cmd =3D cmd; - __entry->ctrl =3D nvmet_req_to_ctrl(req); + __entry->ctrl_id =3D nvmet_req_to_ctrl_id(req); __assign_req_name(__entry->disk, req); __entry->qid =3D req->sq->qid; __entry->cid =3D cmd->common.command_id; @@ -90,7 +92,7 @@ TRACE_EVENT(nvmet_req_init, ), TP_printk("nvmet%s: %sqid=3D%d, cmdid=3D%u, nsid=3D%u, flags=3D%#x, " "meta=3D%#llx, cmd=3D(%s, %s)", - __print_ctrl_name(__entry->ctrl), + __print_ctrl_id(__entry->ctrl_id), __print_disk_name(__entry->disk), __entry->qid, __entry->cid, __entry->nsid, __entry->flags, __entry->metadata, @@ -104,7 +106,7 @@ TRACE_EVENT(nvmet_req_complete, TP_PROTO(struct nvmet_req *req), TP_ARGS(req), TP_STRUCT__entry( - __field(struct nvmet_ctrl *, ctrl) + __field(u16, ctrl_id) __array(char, disk, DISK_NAME_LEN) __field(int, qid) __field(int, cid) @@ -112,7 +114,7 @@ TRACE_EVENT(nvmet_req_complete, __field(u16, status) ), TP_fast_assign( - __entry->ctrl =3D nvmet_req_to_ctrl(req); + __entry->ctrl_id =3D nvmet_req_to_ctrl_id(req); __entry->qid =3D req->cq->qid; __entry->cid =3D req->cqe->command_id; __entry->result =3D le64_to_cpu(req->cqe->result.u64); @@ -120,7 +122,7 @@ TRACE_EVENT(nvmet_req_complete, __assign_req_name(__entry->disk, req); ), TP_printk("nvmet%s: %sqid=3D%d, cmdid=3D%u, res=3D%#llx, status=3D%#x", - __print_ctrl_name(__entry->ctrl), + __print_ctrl_id(__entry->ctrl_id), __print_disk_name(__entry->disk), __entry->qid, __entry->cid, __entry->result, __entry->status) =20 --=20 2.41.0