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 X-Spam-Level: X-Spam-Status: No, score=-9.8 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4C933C3F68F for ; Sun, 29 Dec 2019 17:38:53 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 29202222C3 for ; Sun, 29 Dec 2019 17:38:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1577641133; bh=7ykI0aqLu3LkA3XNZUr9JslSzNOIdEvWlxpxjeaUA/I=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-ID:From; b=QZmXGEQALmfNc310ejK90App0AAJiBgAhm9qbtPTfN71+gOvIhuh/fjFGivN3uTP7 pcAj3XbU0Bwf8VkpxGMgdj6owS1l9WA9nDvuMJ3Z0kY0nrKcH736XDOASpIDbsB1lH Cha9OgFxrV3ecbSxHxQO8qRdhTP4WliSNNR+WvhM= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729524AbfL2Rds (ORCPT ); Sun, 29 Dec 2019 12:33:48 -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: stable-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: stable@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