Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp13461639ybl; Sun, 29 Dec 2019 12:25:25 -0800 (PST) X-Google-Smtp-Source: APXvYqxuVGPX/uvpHPJ77r+CiFkdQ0i5Ic6nU6THn+quHsMnbRu29uhRyAV1JB8wjcq4CNWOBXTk X-Received: by 2002:a05:6830:3014:: with SMTP id a20mr44986343otn.350.1577651125014; Sun, 29 Dec 2019 12:25:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1577651125; cv=none; d=google.com; s=arc-20160816; b=IOeB4JA/KVNyV7006KxGLIcsTPlCAl9h0O0Pc3FtUVigtdSvkQOBH1/phyoBNMVCTI +MKldVR8uMThp4DomiwGb/fstp3FjkbO0icNF1BUzdR4N9+EHzV0sLKPlDtcmlz+NDH4 6WOko7yM4mXNL4mOnBPbWlBvJSyHaC6S8NJP0sVLgVQRgXVFObhQiKB8nq7cbdI3u9uU kfEdk9Mck7/JPkrHD8MJNMSP6d4TjvbixllYd9gFIX5RA42LT3wU0LOdtG7V2LRY+6T4 K9vGOKYl0gSY1FAknI6dTvXWvQHxbF66MM7ju/lDI/77Ol3/4EL1V3CyaVqt/ro3sVyl trQA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=c1xwqwL8MZAVtsV8H31ZfVGm5QbjAlRyWOTrv/k4sF0=; b=syGj3FXS3wB/0X/OYxoFt9s1+7sgNcs3fRcnfLXudaOq77HsmR8u/xn+FBn7nM0w9A llKg0r5tFlo5ZwtTFNv1JBAVZbsoWIRn0mCQppMJO8YY4CpHIyw8WlSsmm65AuFzYTmz MCg/3ljxYbMZhB2e/gD4YgC+nQMKrVxZGmJPVQgu8ezpJ8312Z5MdIXqmoQb3NDcFMp8 zWWHI+ndjBeFVNMa4X3mY+URX8kp/cgtVKZitgRmvwy36FQ8JbrjoCgem6y54ZXfelUb UiOzhe+FAlPOqxxihD0e2J1fy6aG57DTSAdkgbTvZx94roWzwM+Md3HTvrHDu5ED3M78 Tu7A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=yS0CCV+a; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id r4si331084otq.188.2019.12.29.12.25.12; Sun, 29 Dec 2019 12:25:24 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=yS0CCV+a; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729315AbfL2Rdv (ORCPT + 99 others); Sun, 29 Dec 2019 12:33:51 -0500 Received: from mail.kernel.org ([198.145.29.99]:36018 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729521AbfL2Rds (ORCPT ); Sun, 29 Dec 2019 12:33:48 -0500 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 4935520722; Sun, 29 Dec 2019 17:33:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1577640827; bh=7ykI0aqLu3LkA3XNZUr9JslSzNOIdEvWlxpxjeaUA/I=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=yS0CCV+aL/76UfMwNGrmj3ePjcEowETifFV1fk9pTlHgzNIrXRMGGZnRFb3fG8EAy jd3wscvmaHq92vSu0dZgd3iuSK5Ii/NzxXuWbfkNajXv4kiHEKXXAbUnFzsqwTxN7E FJqQ/DGPPu/bxrz+gr4RupbvJ/H3fJgrHZuwgsXw= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Eduard Hasenleithner , Keith Busch , Sasha Levin Subject: [PATCH 4.19 155/219] nvme: Discard workaround for non-conformant devices Date: Sun, 29 Dec 2019 18:19:17 +0100 Message-Id: <20191229162531.968286289@linuxfoundation.org> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20191229162508.458551679@linuxfoundation.org> References: <20191229162508.458551679@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Eduard Hasenleithner [ Upstream commit 530436c45ef2e446c12538a400e465929a0b3ade ] Users observe IOMMU related errors when performing discard on nvme from non-compliant nvme devices reading beyond the end of the DMA mapped ranges to discard. Two different variants of this behavior have been observed: SM22XX controllers round up the read size to a multiple of 512 bytes, and Phison E12 unconditionally reads the maximum discard size allowed by the spec (256 segments or 4kB). Make nvme_setup_discard unconditionally allocate the maximum DSM buffer so the driver DMA maps a memory range that will always succeed. Link: https://bugzilla.kernel.org/show_bug.cgi?id=202665 many Signed-off-by: Eduard Hasenleithner [changelog, use existing define, kernel coding style] Signed-off-by: Keith Busch Signed-off-by: Sasha Levin --- drivers/nvme/host/core.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/drivers/nvme/host/core.c b/drivers/nvme/host/core.c index b2d9bd564960..b7bd89b3b2f9 100644 --- a/drivers/nvme/host/core.c +++ b/drivers/nvme/host/core.c @@ -551,8 +551,14 @@ static blk_status_t nvme_setup_discard(struct nvme_ns *ns, struct request *req, struct nvme_dsm_range *range; struct bio *bio; - range = kmalloc_array(segments, sizeof(*range), - GFP_ATOMIC | __GFP_NOWARN); + /* + * Some devices do not consider the DSM 'Number of Ranges' field when + * determining how much data to DMA. Always allocate memory for maximum + * number of segments to prevent device reading beyond end of buffer. + */ + static const size_t alloc_size = sizeof(*range) * NVME_DSM_MAX_RANGES; + + range = kzalloc(alloc_size, GFP_ATOMIC | __GFP_NOWARN); if (!range) { /* * If we fail allocation our range, fallback to the controller @@ -593,7 +599,7 @@ static blk_status_t nvme_setup_discard(struct nvme_ns *ns, struct request *req, req->special_vec.bv_page = virt_to_page(range); req->special_vec.bv_offset = offset_in_page(range); - req->special_vec.bv_len = sizeof(*range) * segments; + req->special_vec.bv_len = alloc_size; req->rq_flags |= RQF_SPECIAL_PAYLOAD; return BLK_STS_OK; -- 2.20.1