From patchwork Mon Sep 18 04:22:54 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Huacai Chen X-Patchwork-Id: 9955473 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id C1D7360385 for ; Mon, 18 Sep 2017 04:21:16 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B4E49206E2 for ; Mon, 18 Sep 2017 04:21:16 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A9B9B28A98; Mon, 18 Sep 2017 04:21:16 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3A159206E2 for ; Mon, 18 Sep 2017 04:21:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751462AbdIREVP (ORCPT ); Mon, 18 Sep 2017 00:21:15 -0400 Received: from smtpbgbr2.qq.com ([54.207.22.56]:33651 "EHLO smtpbgbr2.qq.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750732AbdIREVP (ORCPT ); Mon, 18 Sep 2017 00:21:15 -0400 X-QQ-mid: bizesmtp5t1505708464tep8a1lc0 Received: from software.domain.org (unknown [222.92.8.142]) by esmtp4.qq.com (ESMTP) with id ; Mon, 18 Sep 2017 12:21:01 +0800 (CST) X-QQ-SSF: 01100000008000F0FNF0B00A0000000 X-QQ-FEAT: 9MsTBLS6yXEe4cZb9QXvtHpR7RhWVqW1PjKaKOYdzqMbRF/IfhRY5U2rYd2GG gxC42qlKLbg6LM33k7qfQDQS//tTa8OxS6WuGYhgWhGezAlvVAjPCEooCZNbMtRb05vPBBN SIkHLTX+uzsXF+b5696N+pOyO7YrVXwUhmsYxSpzGpBkj5QVuS1YG8gHeeac88OCIsxTi27 sgPk3psYO06Tk9eUspV32wTN6HKDNdapOmC/BkbNq2Vex4YL4gIOmLQ0W0tWW5NSluQlOMw JJfu275eTY2MqI7zNvALqN6OVQsWczx/ZY0SHZylFNLkyauDdoQW5uH/0= X-QQ-GoodBg: 0 From: Huacai Chen To: "James E . J . Bottomley" Cc: "Martin K . Petersen" , Andrew Morton , Fuxin Zhang , linux-scsi@vger.kernel.org, linux-kernel@vger.kernel.org, Huacai Chen , stable@vger.kernel.org Subject: [PATCH V5 3/3] scsi: Align queue to ARCH_DMA_MINALIGN in non-coherent DMA mode Date: Mon, 18 Sep 2017 12:22:54 +0800 Message-Id: <1505708574-5281-1-git-send-email-chenhc@lemote.com> X-Mailer: git-send-email 2.7.0 X-QQ-SENDSIZE: 520 Feedback-ID: bizesmtp:lemote.com:qybgforeign:qybgforeign1 X-QQ-Bgrelay: 1 Sender: linux-scsi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-scsi@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP In non-coherent DMA mode, kernel uses cache flushing operations to maintain I/O coherency, so scsi's block queue should be aligned to ARCH_DMA_MINALIGN. Otherwise, it will cause data corruption, at least on MIPS: Step 1, dma_map_single Step 2, cache_invalidate (no writeback) Step 3, dma_from_device Step 4, dma_unmap_single If a DMA buffer and a kernel structure share a same cache line, and if the kernel structure has dirty data, cache_invalidate (no writeback) will cause data lost. Cc: stable@vger.kernel.org Signed-off-by: Huacai Chen --- drivers/scsi/scsi_lib.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/drivers/scsi/scsi_lib.c b/drivers/scsi/scsi_lib.c index 9cf6a80..d5059b9 100644 --- a/drivers/scsi/scsi_lib.c +++ b/drivers/scsi/scsi_lib.c @@ -2132,11 +2132,14 @@ void __scsi_init_queue(struct Scsi_Host *shost, struct request_queue *q) q->limits.cluster = 0; /* - * set a reasonable default alignment on word boundaries: the - * host and device may alter it using + * set a reasonable default alignment on word/cacheline boundaries: + * the host and device may alter it using * blk_queue_update_dma_alignment() later. */ - blk_queue_dma_alignment(q, 0x03); + if (device_is_coherent(dev)) + blk_queue_dma_alignment(q, 0x04 - 1); + else + blk_queue_dma_alignment(q, dma_get_cache_alignment() - 1); } EXPORT_SYMBOL_GPL(__scsi_init_queue);