From patchwork Mon Jun 13 21:44:40 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bart Van Assche X-Patchwork-Id: 581609 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 E5A45C433EF for ; Mon, 13 Jun 2022 21:45:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344997AbiFMVpM (ORCPT ); Mon, 13 Jun 2022 17:45:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51416 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234131AbiFMVpL (ORCPT ); Mon, 13 Jun 2022 17:45:11 -0400 Received: from mail-pg1-f170.google.com (mail-pg1-f170.google.com [209.85.215.170]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2BFE463F0 for ; Mon, 13 Jun 2022 14:45:10 -0700 (PDT) Received: by mail-pg1-f170.google.com with SMTP id 129so6753376pgc.2 for ; Mon, 13 Jun 2022 14:45:10 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=UhfEfvl0r2NBkOBg8gADR1ZqMrK/zdzBIc2zKDop9kQ=; b=oW0i61EltoXgdpmWwXuReWslQOvP72gcdeXw+VCx2Wi7VMwJeG+BFH+aUVMzbDJopY 0IVZrwxPZKUiFhLOib+4JKKgAz3DOlArNoKmeC909+fp7MnGee/CLOt1wHbYbhaiFE+5 baShUUJYQNDfa3IIrw7v+FWaRtwpZn/d4aPuOabk3MNZQo0BBJcTNnwSL4h7bfR942yz LLCL27YlsCpWJL2mZ7Vy4s1oABbLzElEmo7ZLjgofZmK98tB1B5hyLiGnyFC0JGDcD+p +m57F2zQtwaQWGlMV1IkTU9MQN+gTkuorafveBSNMHy7appZvF+Kxfm4/ZC2RL3Ykn9S 3vbg== X-Gm-Message-State: AOAM530PMQG3s+rbDeG22c4mnVa2Us+Z0q3bbw2eZxpVLnVj+TH1Kmmm qTn2/doxuXO/G50K3Bxou3Y= X-Google-Smtp-Source: ABdhPJx95L5Cc9IBcxxTvtWukROJsgaLinWfX20ecOD0DuiTKzI95Uoqg9OxtHl4WWKqhmNhikuK9g== X-Received: by 2002:a63:67c3:0:b0:3fe:465:bfa2 with SMTP id b186-20020a6367c3000000b003fe0465bfa2mr1472427pgc.12.1655156709455; Mon, 13 Jun 2022 14:45:09 -0700 (PDT) Received: from bvanassche-linux.mtv.corp.google.com ([2620:15c:211:201:6862:a290:1a09:5af5]) by smtp.gmail.com with ESMTPSA id ji2-20020a170903324200b001622c377c3esm5585833plb.117.2022.06.13.14.45.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Jun 2022 14:45:08 -0700 (PDT) From: Bart Van Assche To: "Martin K . Petersen" Cc: Jaegeuk Kim , Adrian Hunter , linux-scsi@vger.kernel.org, Bart Van Assche , "James E.J. Bottomley" , Bean Huo , Avri Altman , Daejun Park Subject: [PATCH v2 1/3] scsi: ufs: Simplify ufshcd_clear_cmd() Date: Mon, 13 Jun 2022 14:44:40 -0700 Message-Id: <20220613214442.212466-2-bvanassche@acm.org> X-Mailer: git-send-email 2.36.1.476.g0c4daa206d-goog In-Reply-To: <20220613214442.212466-1-bvanassche@acm.org> References: <20220613214442.212466-1-bvanassche@acm.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-scsi@vger.kernel.org Remove the local variable 'err'. This patch does not change any functionality. Signed-off-by: Bart Van Assche --- drivers/ufs/core/ufshcd.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/drivers/ufs/core/ufshcd.c b/drivers/ufs/core/ufshcd.c index bb6cbd514a69..4ed2f6c29e80 100644 --- a/drivers/ufs/core/ufshcd.c +++ b/drivers/ufs/core/ufshcd.c @@ -2868,7 +2868,6 @@ static int ufshcd_compose_dev_cmd(struct ufs_hba *hba, static int ufshcd_clear_cmd(struct ufs_hba *hba, int tag) { - int err = 0; unsigned long flags; u32 mask = 1 << tag; @@ -2881,11 +2880,8 @@ ufshcd_clear_cmd(struct ufs_hba *hba, int tag) * wait for h/w to clear corresponding bit in door-bell. * max. wait is 1 sec. */ - err = ufshcd_wait_for_register(hba, - REG_UTP_TRANSFER_REQ_DOOR_BELL, - mask, ~mask, 1000, 1000); - - return err; + return ufshcd_wait_for_register(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL, + mask, ~mask, 1000, 1000); } static int From patchwork Mon Jun 13 21:44:41 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Bart Van Assche X-Patchwork-Id: 582193 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 2CB8EC43334 for ; Mon, 13 Jun 2022 21:45:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348225AbiFMVpS (ORCPT ); Mon, 13 Jun 2022 17:45:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51542 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239450AbiFMVpR (ORCPT ); Mon, 13 Jun 2022 17:45:17 -0400 Received: from mail-pj1-f51.google.com (mail-pj1-f51.google.com [209.85.216.51]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D08A46419 for ; Mon, 13 Jun 2022 14:45:16 -0700 (PDT) Received: by mail-pj1-f51.google.com with SMTP id cx11so6813173pjb.1 for ; Mon, 13 Jun 2022 14:45:16 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Y1xiiMs+jvGJqSGFqyS+k2VD1/618zcR4FkPID0+MWU=; b=jMBESOjaKUq3WPe8XF20SD5xzyLE1KGdeGQqjC7TEV2CK5SN4YwgGVm6RYmmQLZkrw NjT+1cWZKpB4x9JhIGW2TzJ92dpx9oVdgBqqT1pqPoGLZC/lJP/xlEUNq8u34e2Rg+l/ w4sL0B4s+zT1HhN1tY6/yyA+snUiNv6Z1kXs21biT96aI+K6R88vQuAPY24bhYHy0eDC 3ocNPwFQ2LYdYfXvnJuQUM+UlfeHEVtIuN+hTO6ZwnAAkyZgS2Nw4XAbmJB7RGKvj3Zj 7dO7mhYdrIwtVGHTdC+rh+o7nemgjxqVz0hX+rQIY4JN8iReQtf1aPYKOJFcAiXBR4e1 f74A== X-Gm-Message-State: AOAM531Cz2tk+cLIIIY4zh08BcrwEU8zydnH9lvMDh7cv/SdhhbYSpZl 06AM7y6MkNjx3f93fJ3IeD8= X-Google-Smtp-Source: AGRyM1ukJyw7oexegs3Vi/+T/NFaxTjwldoEEa+F9HoQFLgzgbn4llDCdLJn0nCStWld68hzRENK9Q== X-Received: by 2002:a17:902:d707:b0:168:9a6a:936d with SMTP id w7-20020a170902d70700b001689a6a936dmr1135221ply.92.1655156716230; Mon, 13 Jun 2022 14:45:16 -0700 (PDT) Received: from bvanassche-linux.mtv.corp.google.com ([2620:15c:211:201:6862:a290:1a09:5af5]) by smtp.gmail.com with ESMTPSA id ji2-20020a170903324200b001622c377c3esm5585833plb.117.2022.06.13.14.45.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Jun 2022 14:45:15 -0700 (PDT) From: Bart Van Assche To: "Martin K . Petersen" Cc: Jaegeuk Kim , Adrian Hunter , linux-scsi@vger.kernel.org, Bart Van Assche , "James E.J. Bottomley" , Bean Huo , Avri Altman , Daejun Park Subject: [PATCH v2 2/3] scsi: ufs: Support clearing multiple commands at once Date: Mon, 13 Jun 2022 14:44:41 -0700 Message-Id: <20220613214442.212466-3-bvanassche@acm.org> X-Mailer: git-send-email 2.36.1.476.g0c4daa206d-goog In-Reply-To: <20220613214442.212466-1-bvanassche@acm.org> References: <20220613214442.212466-1-bvanassche@acm.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-scsi@vger.kernel.org Modify ufshcd_clear_cmd() such that it supports clearing multiple commands at once instead of one command at a time. This change will be used in a later patch to reduce the time spent in the reset handler. Signed-off-by: Bart Van Assche --- drivers/ufs/core/ufshcd.c | 42 ++++++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/drivers/ufs/core/ufshcd.c b/drivers/ufs/core/ufshcd.c index 4ed2f6c29e80..b7120c5f0920 100644 --- a/drivers/ufs/core/ufshcd.c +++ b/drivers/ufs/core/ufshcd.c @@ -748,17 +748,28 @@ static enum utp_ocs ufshcd_get_tr_ocs(struct ufshcd_lrb *lrbp) } /** - * ufshcd_utrl_clear - Clear a bit in UTRLCLR register + * ufshcd_utrl_clear() - Clear requests from the controller request list. * @hba: per adapter instance - * @pos: position of the bit to be cleared + * @mask: mask with one bit set for each request to be cleared */ -static inline void ufshcd_utrl_clear(struct ufs_hba *hba, u32 pos) +static inline void ufshcd_utrl_clear(struct ufs_hba *hba, u32 mask) { if (hba->quirks & UFSHCI_QUIRK_BROKEN_REQ_LIST_CLR) - ufshcd_writel(hba, (1 << pos), REG_UTP_TRANSFER_REQ_LIST_CLEAR); - else - ufshcd_writel(hba, ~(1 << pos), - REG_UTP_TRANSFER_REQ_LIST_CLEAR); + mask = ~mask; + /* + * From the UFSHCI specification: "UTP Transfer Request List CLear + * Register (UTRLCLR): This field is bit significant. Each bit + * corresponds to a slot in the UTP Transfer Request List, where bit 0 + * corresponds to request slot 0. A bit in this field is set to ‘0’ + * by host software to indicate to the host controller that a transfer + * request slot is cleared. The host controller + * shall free up any resources associated to the request slot + * immediately, and shall set the associated bit in UTRLDBR to ‘0’. The + * host software indicates no change to request slots by setting the + * associated bits in this field to ‘1’. Bits in this field shall only + * be set ‘1’ or ‘0’ by host software when UTRLRSR is set to ‘1’." + */ + ufshcd_writel(hba, ~mask, REG_UTP_TRANSFER_REQ_LIST_CLEAR); } /** @@ -2865,15 +2876,18 @@ static int ufshcd_compose_dev_cmd(struct ufs_hba *hba, return ufshcd_compose_devman_upiu(hba, lrbp); } -static int -ufshcd_clear_cmd(struct ufs_hba *hba, int tag) +/* + * Clear all the requests from the controller for which a bit has been set in + * @mask and wait until the controller confirms that these requests have been + * cleared. + */ +static int ufshcd_clear_cmds(struct ufs_hba *hba, u32 mask) { unsigned long flags; - u32 mask = 1 << tag; /* clear outstanding transaction before retry */ spin_lock_irqsave(hba->host->host_lock, flags); - ufshcd_utrl_clear(hba, tag); + ufshcd_utrl_clear(hba, mask); spin_unlock_irqrestore(hba->host->host_lock, flags); /* @@ -2961,7 +2975,7 @@ static int ufshcd_wait_for_dev_cmd(struct ufs_hba *hba, err = -ETIMEDOUT; dev_dbg(hba->dev, "%s: dev_cmd request timedout, tag %d\n", __func__, lrbp->task_tag); - if (!ufshcd_clear_cmd(hba, lrbp->task_tag)) + if (!ufshcd_clear_cmds(hba, 1U << lrbp->task_tag)) /* successfully cleared the command, retry if needed */ err = -EAGAIN; /* @@ -6989,7 +7003,7 @@ static int ufshcd_eh_device_reset_handler(struct scsi_cmnd *cmd) /* clear the commands that were pending for corresponding LUN */ for_each_set_bit(pos, &hba->outstanding_reqs, hba->nutrs) { if (hba->lrb[pos].lun == lun) { - err = ufshcd_clear_cmd(hba, pos); + err = ufshcd_clear_cmds(hba, 1U << pos); if (err) break; __ufshcd_transfer_req_compl(hba, 1U << pos); @@ -7091,7 +7105,7 @@ static int ufshcd_try_to_abort_task(struct ufs_hba *hba, int tag) goto out; } - err = ufshcd_clear_cmd(hba, tag); + err = ufshcd_clear_cmds(hba, 1U << tag); if (err) dev_err(hba->dev, "%s: Failed clearing cmd at tag %d, err %d\n", __func__, tag, err); From patchwork Mon Jun 13 21:44:42 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bart Van Assche X-Patchwork-Id: 581608 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 83515C43334 for ; Mon, 13 Jun 2022 21:45:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239622AbiFMVpg (ORCPT ); Mon, 13 Jun 2022 17:45:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51614 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234131AbiFMVpY (ORCPT ); Mon, 13 Jun 2022 17:45:24 -0400 Received: from mail-pg1-f172.google.com (mail-pg1-f172.google.com [209.85.215.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6D3456463 for ; Mon, 13 Jun 2022 14:45:23 -0700 (PDT) Received: by mail-pg1-f172.google.com with SMTP id l4so5664340pgh.13 for ; Mon, 13 Jun 2022 14:45:23 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=RlJQWaQtzKYLaJO2jTeTrMzOBITTjl5zGC+qrwjiNv8=; b=aJG3SVTv5sfpHy/LS/WGevNE+PzKJbkunqUK2jcbuB3X5n4RQH/2bdCPVXy680poA0 bn33jIW8SJWWD6MD0tToe34lLz/itsNYaC/4dghGqDAlPToUXxVthBRy0A+Fz0TgptjN OpkQ6EXveRFjdkW9mV2ReGTlywV3gyhEHhqFkicMbEkZVdGR/vWlTcIclKJM9oECVLFP BsWtyxhLEGUBt3hpxTn/L9g/P7bbaV5N58zWADYl6KUP80xqe2HhACeLhoSNaKUmJMij CGnU+6iSOFpi0t/ypgqLKEPfJ1wcWfp4g+Qw2OXolNWOFtIVzcxHsl11i9/OJ2dH0wQd vfwg== X-Gm-Message-State: AOAM530jXZ0UsVO3wpJGoAfNXUTjkcikkHRkeVXoqJaidAKJ9P0729tJ O4wBTI9tIqf6kKC6rhEU1RE= X-Google-Smtp-Source: ABdhPJxMgl8HkMzIxO4Kh66eF6Z15nP+OhNOFpkG1cEwQzxW6Etx/rMk0HqCQDOxT3fpg92mgC5aLA== X-Received: by 2002:a05:6a00:10d3:b0:4fe:5d:75c8 with SMTP id d19-20020a056a0010d300b004fe005d75c8mr1364549pfu.6.1655156722845; Mon, 13 Jun 2022 14:45:22 -0700 (PDT) Received: from bvanassche-linux.mtv.corp.google.com ([2620:15c:211:201:6862:a290:1a09:5af5]) by smtp.gmail.com with ESMTPSA id ji2-20020a170903324200b001622c377c3esm5585833plb.117.2022.06.13.14.45.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Jun 2022 14:45:22 -0700 (PDT) From: Bart Van Assche To: "Martin K . Petersen" Cc: Jaegeuk Kim , Adrian Hunter , linux-scsi@vger.kernel.org, Bart Van Assche , "James E.J. Bottomley" , Bean Huo , Avri Altman Subject: [PATCH v2 3/3] scsi: ufs: Fix a race between the interrupt handler and the reset handler Date: Mon, 13 Jun 2022 14:44:42 -0700 Message-Id: <20220613214442.212466-4-bvanassche@acm.org> X-Mailer: git-send-email 2.36.1.476.g0c4daa206d-goog In-Reply-To: <20220613214442.212466-1-bvanassche@acm.org> References: <20220613214442.212466-1-bvanassche@acm.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-scsi@vger.kernel.org Prevent that both the interrupt handler and the reset handler try to complete a request at the same time. This patch is the result of an analysis of the following crash: Unable to handle kernel NULL pointer dereference at virtual address 0000000000000120 CPU: 0 PID: 0 Comm: swapper/0 Tainted: G OE 5.10.107-android13-4-00051-g1e48e8970cca-ab8664745 #1 pc : ufshcd_release_scsi_cmd+0x30/0x46c lr : __ufshcd_transfer_req_compl+0x4fc/0x9c0 Call trace: ufshcd_release_scsi_cmd+0x30/0x46c __ufshcd_transfer_req_compl+0x4fc/0x9c0 ufshcd_poll+0xf0/0x208 ufshcd_sl_intr+0xb8/0xf0 ufshcd_intr+0x168/0x2f4 __handle_irq_event_percpu+0xa0/0x30c handle_irq_event+0x84/0x178 handle_fasteoi_irq+0x150/0x2e8 __handle_domain_irq+0x114/0x1e4 gic_handle_irq.31846+0x58/0x300 el1_irq+0xe4/0x1c0 cpuidle_enter_state+0x3ac/0x8c4 do_idle+0x2fc/0x55c cpu_startup_entry+0x84/0x90 kernel_init+0x0/0x310 start_kernel+0x0/0x608 start_kernel+0x4ec/0x608 Signed-off-by: Bart Van Assche --- drivers/ufs/core/ufshcd.c | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/drivers/ufs/core/ufshcd.c b/drivers/ufs/core/ufshcd.c index b7120c5f0920..ff3abda91bb6 100644 --- a/drivers/ufs/core/ufshcd.c +++ b/drivers/ufs/core/ufshcd.c @@ -6975,14 +6975,14 @@ int ufshcd_exec_raw_upiu_cmd(struct ufs_hba *hba, } /** - * ufshcd_eh_device_reset_handler - device reset handler registered to - * scsi layer. + * ufshcd_eh_device_reset_handler() - Reset a single logical unit. * @cmd: SCSI command pointer * * Returns SUCCESS/FAILED */ static int ufshcd_eh_device_reset_handler(struct scsi_cmnd *cmd) { + unsigned long flags, pending_reqs = 0, not_cleared = 0; struct Scsi_Host *host; struct ufs_hba *hba; u32 pos; @@ -7001,14 +7001,24 @@ static int ufshcd_eh_device_reset_handler(struct scsi_cmnd *cmd) } /* clear the commands that were pending for corresponding LUN */ - for_each_set_bit(pos, &hba->outstanding_reqs, hba->nutrs) { - if (hba->lrb[pos].lun == lun) { - err = ufshcd_clear_cmds(hba, 1U << pos); - if (err) - break; - __ufshcd_transfer_req_compl(hba, 1U << pos); - } + spin_lock_irqsave(&hba->outstanding_lock, flags); + for_each_set_bit(pos, &hba->outstanding_reqs, hba->nutrs) + if (hba->lrb[pos].lun == lun) + __set_bit(pos, &pending_reqs); + hba->outstanding_reqs &= ~pending_reqs; + spin_unlock_irqrestore(&hba->outstanding_lock, flags); + + if (ufshcd_clear_cmds(hba, pending_reqs) < 0) { + spin_lock_irqsave(&hba->outstanding_lock, flags); + not_cleared = pending_reqs & + ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL); + hba->outstanding_reqs |= not_cleared; + spin_unlock_irqrestore(&hba->outstanding_lock, flags); + + dev_err(hba->dev, "%s: failed to clear requests %#lx\n", + __func__, not_cleared); } + __ufshcd_transfer_req_compl(hba, pending_reqs & ~not_cleared); out: hba->req_abort_count = 0;