From mboxrd@z Thu Jan 1 00:00:00 1970 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 0CBFFCD8C8E for ; Sat, 6 Jun 2026 15:08:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: Content-Type:In-Reply-To:From:References:Cc:To:Subject:MIME-Version:Date: Message-ID:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=z56ZvvEiHNN1gkYTK+8/eqIg8fgyCPdSo7V0PEkdypI=; b=AaYdQVsO8BefnQsW7AIjFBba+H oFP1mo9gumUFo9P4dhf567qMYjwTwrRXG7baAbhYeN9xnPZL/PMD8NcPN1JE5jt1fk86rUqMac+Jh 1EGjUYH1r1EDR/L3FFRrPE3/tNfY4usmAGbH+3F9AMoekYC6Cy/uy8h01KF87iQv6/QaaKzQhmqjo 6Ntws13OfUCdlN0H9rYmndm3h7eRSddm1m8GweAyz1sfcYapULmI9uwDuuhomZmSJ8n2VFFmR9bE8 0BtbGLTXDg85NZkjixvZqWVxsSz7QTzOyfziJdJXu4Gabzm8naMmnT2u/vKXOlS11EQgekuSbDFSb eQNOKKEA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.99.1 #2 (Red Hat Linux)) id 1wVsdR-00000001fvu-1cWW; Sat, 06 Jun 2026 15:08:29 +0000 Received: from mail-pf1-x433.google.com ([2607:f8b0:4864:20::433]) by bombadil.infradead.org with esmtps (Exim 4.99.1 #2 (Red Hat Linux)) id 1wVsdN-00000001fuy-3kXy for linux-arm-kernel@lists.infradead.org; Sat, 06 Jun 2026 15:08:28 +0000 Received: by mail-pf1-x433.google.com with SMTP id d2e1a72fcca58-842288702fbso1215538b3a.1 for ; Sat, 06 Jun 2026 08:08:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1780758505; x=1781363305; darn=lists.infradead.org; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=z56ZvvEiHNN1gkYTK+8/eqIg8fgyCPdSo7V0PEkdypI=; b=isJJ/eUf/9+hA7bUgwOO77yqs/1Nhj2M5jcp4Zzfb9ZgpD9LHdS/nlgbxMvgl+OZtZ CSSy76531z/2/ug+lPJ70qLZ1jBYeJ2eluxcFmtNNamPcpWPqGnJ79BON21GWXbtJ2a9 itFfe/GxbdUmbKW1H0puLo52r4mWMJClNQ31bqQveLZG2/MXZxnbEwxLzMU4zmGgNFXn YL0sjjNRJAg9EACUu3YSQW3K6hDrm6gLfdkbBGxLU1ovPjurrutENFrxvIm87hViJ76G mth7eXQI4qqPJ99sYGettlCBsxIKHe9G1qvO4MoAxFCRywlxtgsczKfLcTvnYIvK3H3Y 6I4A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780758505; x=1781363305; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=z56ZvvEiHNN1gkYTK+8/eqIg8fgyCPdSo7V0PEkdypI=; b=q3W+8pbWOu3YnyVQ5TBNMcdHIQ7cJZq0Lpa6YRgg4zlxKaRWoPN7Uqsu0A00bfaUmb vWHjK8pNdyhL5DDrGWX0PN5zkx4xvVi2uSpZgSTMzJU7+xjQ8aoCRuy0bTya9UQDEuIO coMfUu1U7qnYtJlxDWRVE88rIb9hAKEBojcPkz1eSxfCNkV/GdrSxuJahSd3ijBBDCkA JC7QuydbnJxSCK1uKKKyK2Ur2gMcrM4nI/EIJ4pwqGipxIyoiw0zCIlRiBZPa94FHgQA LnlZVB42LikDIICC6Dhr8F3reWEqAGOJKg3D5aKuGaXvsCETMyoTSdNc4+txS8Amxa+A 4xMw== X-Forwarded-Encrypted: i=1; AFNElJ8Ur/FXgcF5lCt2XqC64BdtV3nxMxUUDGj+eRY6WYJvxGVGQ1SlJ6P2DuD8QMh1R8uHELBUvfVKY7y0DBP10doS@lists.infradead.org X-Gm-Message-State: AOJu0Yy6gNiE+EdQxfV3TPOnpgm+bu3P11eB+3cAZSx8WVK+6UmzL6lt 1sxmvcBhKNcq1TF3VwYevlhC47kRvBpV4F49D2I5489fTk2eE9ih6al3 X-Gm-Gg: Acq92OFBHdnwCDGmXjMq9amMiT/3cpPKeP4qtYhC0J+7wJCfO+D9vRfZyWhOUdnIq/o DbISblcFEDP+OFwN6OQBIzkqHiRahJDMag4ydVc4EyuEAHVDQcRYcRzOiJBxK9LqvKfTBVBHIhQ iKaYF4/+aG2ukJ9mcv4o51RIRy+HkZxRyu/jK3sMh+TVbCItpfzaI5+ZLwyJu9tD/rigHU8/OTY iH4vQ4tTbp5sY9J5dEUugjQVftuN1dAYt9z8JPy/mX2nRukDHkLdL6aJna8uSLlfES5Ix3N4gjQ QDY+v3nmNOth2K33lE9W3HJt1Jcs3LOEXfsC420J33QHAkd8NFJxDBsFIJodztx/fecDZEUBHZc Xn2aUXo5D3RkZi4jGuuv0RKKLTqifoIqKr5rNRtTvEXYRDCmyqnq1VDIT51o5VHZv5+7ok9gqom pm6QgWQtGPouY5iONq/SRlsJzd3nBcqlmREe+DgV7pZGbwyRu2VdAO X-Received: by 2002:a05:6a00:2e1f:b0:842:5b63:610f with SMTP id d2e1a72fcca58-842b0f25497mr8656172b3a.4.1780758504899; Sat, 06 Jun 2026 08:08:24 -0700 (PDT) Received: from [192.168.1.111] ([223.122.38.120]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-84282494285sm12015997b3a.25.2026.06.06.08.08.19 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 06 Jun 2026 08:08:23 -0700 (PDT) Message-ID: <8861022e-97fa-45eb-99fa-7b56225a6423@gmail.com> Date: Sat, 6 Jun 2026 23:08:18 +0800 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 2/2] nvme-apple: Prevent tag collision across queues even if tag space is shared Content-Language: en-MW To: Sven Peter , Janne Grunau , Neal Gompa , Keith Busch , Jens Axboe , Christoph Hellwig , Sagi Grimberg Cc: asahi@lists.linux.dev, linux-arm-kernel@lists.infradead.org, linux-nvme@lists.infradead.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org, Yuriy Havrylyuk References: <20260606-prevent-tag-collision-t8015-v1-0-93ccf4eca550@gmail.com> <20260606-prevent-tag-collision-t8015-v1-2-93ccf4eca550@gmail.com> From: Nick Chan In-Reply-To: <20260606-prevent-tag-collision-t8015-v1-2-93ccf4eca550@gmail.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.9.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20260606_080825_944052_8B962E93 X-CRM114-Status: GOOD ( 29.49 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Nick Chan 於 2026/6/6 晚上9:25 寫道: > From: Yuriy Havrylyuk > > Apple NVMe controllers require tags of pending commands to not be shared > across admin and IO queues. However, on Apple A11 without linear SQ, it is > not possible for either queue to skip over some tags and must go from 0 to > the configured maximum before wrapping around. > > If a pending command tag is duplicated across queues, the firmware > crashes with: "duplicate tag error for tag N", with N being the tag. > > Instead of partitioning the tag space, which is not possible without > linear SQ, prevent tag collisions by keeping track of which tags are > currently in-flight across either queues, and return BLK_STS_RESOURCE to > temporaily block command submission when a collision would have occurred. > > Cc: stable@vger.kernel.org > Fixes: 04d8ecf37b5e ("nvme: apple: Add Apple A11 support") > Signed-off-by: Yuriy Havrylyuk > Co-developed-by: Nick Chan > Signed-off-by: Nick Chan > --- > drivers/nvme/host/apple.c | 65 +++++++++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 65 insertions(+) There are some issues with this version that make it not actually work, so a v2 will sent. > > diff --git a/drivers/nvme/host/apple.c b/drivers/nvme/host/apple.c > index c1115e27a0d6..6354edf27225 100644 > --- a/drivers/nvme/host/apple.c > +++ b/drivers/nvme/host/apple.c > @@ -203,6 +203,20 @@ struct apple_nvme { > > int irq; > spinlock_t lock; > + > + /* > + * Tags of pending commands must be unique across both Admin and IO > + * queue. However, on T8015, unlike T8103, without linear submission > + * queues, it is not possible for the either queue to skip some tags, > + * and both queues must go from 0 to their respective configured > + * maximum. > + * > + * Instead of reserving some tags for the admin queue, use a bitfield > + * to keep track of pending commands on either queue, and temporaily > + * block command submission by returning BLK_STS_RESOURCE until the > + * tag is freed on the other queue. > + */ > + unsigned long t8015_active_tags; > }; > > static_assert(sizeof(struct nvme_command) == 64); > @@ -290,6 +304,28 @@ static void apple_nvmmu_inval(struct apple_nvme_queue *q, unsigned int tag) > "NVMMU TCB invalidation failed\n"); > } > > +static bool apple_nvme_reserve_tag_t8015(struct apple_nvme *anv, > + struct nvme_command *cmd) > +{ > + u16 tag = nvme_tag_from_cid(cmd->common.command_id); > + > + if (WARN_ON_ONCE(tag >= BITS_PER_LONG)) > + return false; > + > + return !test_and_set_bit(tag, &anv->t8015_active_tags); > +} > + > +static void apple_nvme_release_tag_t8015(struct apple_nvme *anv, > + __u16 command_id) > +{ > + u16 tag = nvme_tag_from_cid(command_id); > + > + if (WARN_ON_ONCE(tag >= BITS_PER_LONG)) > + return; > + > + clear_bit(tag, &anv->t8015_active_tags); > +} > + > static void apple_nvme_submit_cmd_t8015(struct apple_nvme_queue *q, > struct nvme_command *cmd) > { > @@ -652,6 +688,8 @@ static inline void apple_nvme_update_cq_head(struct apple_nvme_queue *q) > static bool apple_nvme_poll_cq(struct apple_nvme_queue *q, > struct io_comp_batch *iob) > { > + struct apple_nvme *anv = queue_to_apple_nvme(q); > + unsigned long completed_tags = 0; > bool found = false; > > while (apple_nvme_cqe_pending(q)) { > @@ -664,11 +702,26 @@ static bool apple_nvme_poll_cq(struct apple_nvme_queue *q, > dma_rmb(); > apple_nvme_handle_cqe(q, iob, q->cq_head); > apple_nvme_update_cq_head(q); > + > + if (!anv->hw->has_lsq_nvmmu) { > + struct nvme_completion *cqe = &q->cqes[q->cq_head]; > + u16 tag = nvme_tag_from_cid(READ_ONCE(cqe->command_id)); Reading command ID here is too late since cq head has already been updated. > + > + if (!WARN_ON_ONCE(tag >= BITS_PER_LONG)) > + __set_bit(tag, &completed_tags); > + } > } > > if (found) > writel(q->cq_head, q->cq_db); > > + if (!anv->hw->has_lsq_nvmmu && completed_tags) { > + unsigned long tag_bit; > + > + for_each_set_bit(tag_bit, &completed_tags, BITS_PER_LONG) > + clear_bit(tag_bit, &anv->t8015_active_tags); > + } > + > return found; > } > > @@ -790,6 +843,12 @@ static blk_status_t apple_nvme_queue_rq(struct blk_mq_hw_ctx *hctx, > if (ret) > return ret; > > + if (!anv->hw->has_lsq_nvmmu && > + !apple_nvme_reserve_tag_t8015(anv, cmnd)) { > + ret = BLK_STS_RESOURCE; > + goto out_free_cmd; Note goto out_free_cmd here. > + } > + > if (blk_rq_nr_phys_segments(req)) { > ret = apple_nvme_map_data(anv, req, cmnd); > if (ret) > @@ -806,6 +865,9 @@ static blk_status_t apple_nvme_queue_rq(struct blk_mq_hw_ctx *hctx, > return BLK_STS_OK; > > out_free_cmd: > + if (!anv->hw->has_lsq_nvmmu) > + apple_nvme_release_tag_t8015(anv, cmnd->common.command_id); Combined with above this makes any attempted use of a in-use tag release that tag, making the workaround ineffective. (and allows nvme to still "work" if the tester is (un)lucky). > + > nvme_cleanup_cmd(req); > return ret; > } > @@ -1165,6 +1227,9 @@ static void apple_nvme_reset_work(struct work_struct *work) > if (ret) > goto out; > > + if (!anv->hw->has_lsq_nvmmu) > + WRITE_ONCE(anv->t8015_active_tags, 0); > + > dev_dbg(anv->dev, "Starting admin queue"); > apple_nvme_init_queue(&anv->adminq); > nvme_unquiesce_admin_queue(&anv->ctrl); > Best regards, Nick Chan