Received: by 2002:a05:7412:2a8a:b0:fc:a2b0:25d7 with SMTP id u10csp500136rdh; Wed, 7 Feb 2024 10:44:12 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWioQP4Qaiu9sW+xURHUyG1TfWfhN0T4M9S2CGjlhEp/IAVKCS6PwioEaXgVB5is4SiVmIfeeLOIE3dsqIUcQe5lWihi7UxTp2ocPiM2A== X-Google-Smtp-Source: AGHT+IFnlDAGJW2JMdAbSQyOoQxHvb4dqiThE9+H4qwTfLs+egxNzyyuRHBCGAEwKlnc6yupcH/U X-Received: by 2002:a05:6808:448d:b0:3bf:dd31:939b with SMTP id eq13-20020a056808448d00b003bfdd31939bmr8976990oib.0.1707331452214; Wed, 07 Feb 2024 10:44:12 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707331452; cv=pass; d=google.com; s=arc-20160816; b=XvmioXRLCRLR/M9P5h+xHQY5zjYvY3YUUT61gfk07jmSAzzKFbb9Q6JR17YWvWHn4s 1QHwoRAZbk6uDfTIZijWU4/JGORoH+F+UWwWWUW2YoJu+BfgGYvbFURT9t+W5mwFrfWP MDwqq5nT36I+HgBfxJuFUqlTTatjLqLTE+UXVR3RiPtctSeY0Rtp1qGvYczbIiy7/Zhg 1HHW1MhnHaZTBSUtg94N7gCXZhfjG3pD+T6nA+eezsutQp5QvFCIfuwLvpU0ML9/DNQv SEn5lJzrQIrAUxX3yKpf0uZKbjQIBLD8aITiPJea380LPn2x+s7wnyWK4ZEjJcXVlMxa QfAA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=VwU//eJ9yadzq/fsfDhiEtvVbtfHI98m1htYpt9Ipdk=; fh=K2QrRNG5xoiA575t7FyH5kjDpiojkg4+9LEB6EAArGE=; b=cvRXjZ9T0RrorfA9SVmsUlhZe7GB3c8QlNCG/0pscFKLJ6KFyph+GpI+innLfDqDxT WwbUlrHYYJEbvU/1T6jjJp7G2k7o3PN/GqMwEf5xnI++ktWXMxfZWB2Wclreh7W8B3pb LUl26SaaIaHwmBqowgZtTZbeEA6e/fT8+VJEuwv/ccJhKvqFZ15YMsQewKvWlf46pJUI NhXIXb5T53ox6+JOVngTrsLv3A9EunP/8rYNiL81TuDdEYDtELxn5H5QYcLJyUnN5AJl ABmpgQsSiUkhCWDufSY6LN7X62NbYvCYuak+hrcP7lyp7G1p7sK0cznEdEo9idPlRWuE xohA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="ee+/qVnN"; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-56922-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-56922-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com X-Forwarded-Encrypted: i=2; AJvYcCXPlZrQg+MrXjm0DW8qR3KoXCN9nuRoVw8OGbzzXd5J61CjQ8Kyto7EMqjnDgSkBG0Nq1lsAffEOk9EOmBN135q1ybKed5ThM9936u2tQ== Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id g12-20020ac85d4c000000b0042c371969d2si1716667qtx.512.2024.02.07.10.44.12 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Feb 2024 10:44:12 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-56922-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="ee+/qVnN"; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-56922-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-56922-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id E23D31C223D4 for ; Wed, 7 Feb 2024 18:44:11 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 11DF184FB6; Wed, 7 Feb 2024 18:42:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="ee+/qVnN" Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 77F3C86AC3 for ; Wed, 7 Feb 2024 18:42:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707331356; cv=none; b=Qi4ljkF/vrHX4nGpBDUrtvAH5KfZ/+ZvBs+3ObruDqt5/imlLDriE18fjdk5rWWXEvSnYBMpwgOZFOYZiUa60Oe2wcjoh5Wx2ZtwcG9uYFb+viRrNupSGKcK9pDyXbzieD/Z5awcRp3PrlWbwpPKFB6zRsGZYbaS5SNVqBxcdT4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707331356; c=relaxed/simple; bh=WxvH6FDX/eWNGMCI+SWHVfMnLqP3askGdu0ASe254/Y=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=dti5G61IYM7E/xP0blr6rXq8jHow9ioL3O1qFm/+sKT+dUgZIlL1MSqzUbIBGphb/gbBbAC3k1jn6magHNPt9VouvC+tKqh+ctQdJpzqY4TntEi1AySekKQrDZkxTwq2gbvX3EkWaaBde7iAVMeqkWP5Y3smT61/sXmoGAUTrt0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=ee+/qVnN; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1707331353; h=from:from:reply-to:subject:subject: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=VwU//eJ9yadzq/fsfDhiEtvVbtfHI98m1htYpt9Ipdk=; b=ee+/qVnNk8KVahdZouW+oiRq+/6BcPlD/19dTMp7FKf/ffDaQlzVIwY7lzY3yRKfdSKLwQ mSlf9lkl5x/41VPySBTQ8mJWuoicJinakPK3hXwGTmmtp9q0wlhFExOy2snv9o5hLpSCkq KwzjtSM8HjhAeHd1WaXxvKefl70x0Iw= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-686-m-6gGQ7ROWmYlHUy-tFPLQ-1; Wed, 07 Feb 2024 13:42:28 -0500 X-MC-Unique: m-6gGQ7ROWmYlHUy-tFPLQ-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 28C2C84AF81; Wed, 7 Feb 2024 18:42:28 +0000 (UTC) Received: from fedora-work.redhat.com (unknown [10.22.32.236]) by smtp.corp.redhat.com (Postfix) with ESMTP id AC90B4011FF1; Wed, 7 Feb 2024 18:42:27 +0000 (UTC) From: David Jeffery To: linux-kernel@vger.kernel.org Cc: linux-nvme@lists.infradead.org, linux-scsi@vger.kernel.org, Greg Kroah-Hartman , "Rafael J . Wysocki" , David Jeffery , Laurence Oberman Subject: [RFC PATCH 6/6] sd: async cache flush on shutdown Date: Wed, 7 Feb 2024 13:41:00 -0500 Message-ID: <20240207184100.18066-7-djeffery@redhat.com> In-Reply-To: <20240207184100.18066-1-djeffery@redhat.com> References: <20240207184100.18066-1-djeffery@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.2 Add async shutdown for the cache flush to the sd device by sending a SYNCHRONIZE_CACHE command asynchronously. If there is any sort of error, falls back to the synchronous sd_sync_cache() to try again and resolve any errors. Signed-off-by: David Jeffery Tested-by: Laurence Oberman --- drivers/scsi/sd.c | 66 ++++++++++++++++++++++++++++++++++++++++++----- drivers/scsi/sd.h | 2 ++ 2 files changed, 62 insertions(+), 6 deletions(-) diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c index 0833b3e6aa6e..f972310df76a 100644 --- a/drivers/scsi/sd.c +++ b/drivers/scsi/sd.c @@ -3838,23 +3838,64 @@ static int sd_start_stop_device(struct scsi_disk *sdkp, int start) return 0; } -/* - * Send a SYNCHRONIZE CACHE instruction down to the device through - * the normal SCSI command structure. Wait for the command to - * complete. - */ -static void sd_shutdown(struct device *dev) +static void sd_sync_cache_callback(struct scsi_cmnd *scmd, + struct scsi_exec_args *args) { + struct scsi_disk *sdkp; + + sdkp = container_of(args, struct scsi_disk, shutdown_params); + complete(&sdkp->shutdown_done); +} + +static void sd_async_shutdown_start(struct device *dev) { struct scsi_disk *sdkp = dev_get_drvdata(dev); + const int timeout = sdkp->device->request_queue->rq_timeout + * SD_FLUSH_TIMEOUT_MULTIPLIER; + int ret; if (!sdkp) return; /* this can happen */ + init_completion(&sdkp->shutdown_done); + sdkp->shutdown_params.callback = sd_sync_cache_callback; + if (pm_runtime_suspended(dev)) return; if (sdkp->WCE && sdkp->media_present) { + unsigned char cmd[16] = { 0 }; + sd_printk(KERN_NOTICE, sdkp, "Synchronizing SCSI cache\n"); + if (sdkp->device->use_16_for_sync) + cmd[0] = SYNCHRONIZE_CACHE_16; + else + cmd[0] = SYNCHRONIZE_CACHE; + + ret = scsi_execute_cmd_nowait(sdkp->device, cmd, REQ_OP_DRV_IN, + timeout, sdkp->max_retries, + &sdkp->shutdown_params); + if (!ret) + return; + sdkp->shutdown_params.result = ret; + } + /* no async I/O to do, go ahead and mark it complete */ + complete(&sdkp->shutdown_done); +} + +static void sd_async_shutdown_end(struct device *dev) +{ + struct scsi_disk *sdkp = dev_get_drvdata(dev); + + if (!sdkp) + return; + + if (pm_runtime_suspended(dev)) + return; + + wait_for_completion(&sdkp->shutdown_done); + + if (sdkp->WCE && sdkp->media_present && sdkp->shutdown_params.result) { + /* for any error with the async flush, retry as sync */ sd_sync_cache(sdkp); } @@ -3867,6 +3908,17 @@ static void sd_shutdown(struct device *dev) } } +/* + * Send a SYNCHRONIZE CACHE instruction down to the device through + * the normal SCSI command structure. Wait for the command to + * complete. + */ +static void sd_shutdown(struct device *dev) +{ + sd_async_shutdown_start(dev); + sd_async_shutdown_end(dev); +} + static inline bool sd_do_start_stop(struct scsi_device *sdev, bool runtime) { return (sdev->manage_system_start_stop && !runtime) || @@ -4003,6 +4055,8 @@ static struct scsi_driver sd_template = { .probe_type = PROBE_PREFER_ASYNCHRONOUS, .remove = sd_remove, .shutdown = sd_shutdown, + .async_shutdown_start = sd_async_shutdown_start, + .async_shutdown_end = sd_async_shutdown_end, .pm = &sd_pm_ops, }, .rescan = sd_rescan, diff --git a/drivers/scsi/sd.h b/drivers/scsi/sd.h index 409dda5350d1..7b5098211cec 100644 --- a/drivers/scsi/sd.h +++ b/drivers/scsi/sd.h @@ -91,6 +91,8 @@ struct scsi_disk { struct device disk_dev; struct gendisk *disk; struct opal_dev *opal_dev; + struct scsi_exec_args shutdown_params; + struct completion shutdown_done; #ifdef CONFIG_BLK_DEV_ZONED /* Updated during revalidation before the gendisk capacity is known. */ struct zoned_disk_info early_zone_info; -- 2.43.0