Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp2655093pxa; Mon, 17 Aug 2020 15:38:57 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz2vzCJ5l651yoXRp1TJ0/LcpzaSTwEeos3bjatvk6AwgcGWwVolJutjsQEhls1r4D3Hqqz X-Received: by 2002:a17:906:f914:: with SMTP id lc20mr17051407ejb.138.1597703936890; Mon, 17 Aug 2020 15:38:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597703936; cv=none; d=google.com; s=arc-20160816; b=nUbAT+1p952VlJOpn8LEVa25OQ0Zng8IQ6dG1yr9Uss43DO+hgEgSorigC1DeIK+yz LNN+SPwApjP/cGPEWkoXkwuEjKuCDwrvf6gHlDSR9X9wrBAXOOs7tYVayOItt2b3LsF9 uwIUuBb1oiS/j/zBD2NNee8nPbrmgTqOZ4SPRzGqXTuzE10D8epPLXFbL3OyKD4zTkXv dnCeD4r59KvcMh4FVOzXMwbGGOS6NHfd6dTVPeVyRCNNhOnjjJcFf66YJj8pDqeqtbCh /GIZL7DKyhOxGmO5eMayd6vBMFlTPvvfzb1wpcnsxf8v9+U4xvtt4pJ6NdxYxYGJ/O++ kJ2A== 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=4M+SqvbFCbpHweLH7HNXSWR8M1xyAJUCO2ffqRm8Jdw=; b=hd9SQLsxPvdzhJvgBh/dct53ERwpw6IdhGk3WdRKv786mGC0RmZIYvaFuVjPTXX07X a0LvLu6R8GTZEbdVZCt7ntGe+Gr8PYGecfyRpRqEDf4sTD1JoQgx//EGg2mgCS/OnyTD ut/WsAo97RnjpQxG3ijI9WiG4w0IQrmpaXwzPkTcDt56ATPYomQ7X5A7bWBZS7Om2Kma SniJZnZAMy1UwJhtmiX/q0NNQi3VxdoN2TBQ0Phwfke26clEZus1qIyj+0hxjV0q8vPv 6Cu0j1zUqFfJlLxUIBwu8RWICuxwHfN0cYoUrFZT5hu5Lla15BoSMjqhMVCoQN+GzNAr 0TTQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=z7+LVXIN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id i21si12074377eje.143.2020.08.17.15.38.33; Mon, 17 Aug 2020 15:38:56 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=z7+LVXIN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731083AbgHQRgw (ORCPT + 99 others); Mon, 17 Aug 2020 13:36:52 -0400 Received: from mail.kernel.org ([198.145.29.99]:60868 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729988AbgHQQR3 (ORCPT ); Mon, 17 Aug 2020 12:17:29 -0400 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 EB5A620657; Mon, 17 Aug 2020 16:17:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1597681048; bh=70U/j3EOCkHqW9olwUsw787MZO+sPdWvgTDRm3H4ins=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=z7+LVXINcGqIsiJlSlNvF1yQDLolgmSX8FyhEt273CcGjh49VyHpvlDRCiUEZArPd 1fbm+En/Z2/k6JF9/aO9G43Rvrc0qSQAEJToZi4f4o4A5xfsy+0JsoQ5+D9Gn8vTgu 16VKeGQFxkv5vOG5csV3OqBhRoTMUeow0NmIyi60= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Christian Eggers , Mark Brown Subject: [PATCH 4.19 160/168] spi: spidev: Align buffers for DMA Date: Mon, 17 Aug 2020 17:18:11 +0200 Message-Id: <20200817143741.663477319@linuxfoundation.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200817143733.692105228@linuxfoundation.org> References: <20200817143733.692105228@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: Christian Eggers commit aa9e862d7d5bcecd4dca9f39e8b684b93dd84ee7 upstream. Simply copying all xfers from userspace into one bounce buffer causes alignment problems if the SPI controller uses DMA. Ensure that all transfer data blocks within the rx and tx bounce buffers are aligned for DMA (according to ARCH_KMALLOC_MINALIGN). Alignment may increase the usage of the bounce buffers. In some cases, the buffers may need to be increased using the "bufsiz" module parameter. Signed-off-by: Christian Eggers Cc: stable@vger.kernel.org Link: https://lore.kernel.org/r/20200728100832.24788-1-ceggers@arri.de Signed-off-by: Mark Brown Signed-off-by: Greg Kroah-Hartman --- drivers/spi/spidev.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) --- a/drivers/spi/spidev.c +++ b/drivers/spi/spidev.c @@ -232,6 +232,11 @@ static int spidev_message(struct spidev_ for (n = n_xfers, k_tmp = k_xfers, u_tmp = u_xfers; n; n--, k_tmp++, u_tmp++) { + /* Ensure that also following allocations from rx_buf/tx_buf will meet + * DMA alignment requirements. + */ + unsigned int len_aligned = ALIGN(u_tmp->len, ARCH_KMALLOC_MINALIGN); + k_tmp->len = u_tmp->len; total += k_tmp->len; @@ -247,17 +252,17 @@ static int spidev_message(struct spidev_ if (u_tmp->rx_buf) { /* this transfer needs space in RX bounce buffer */ - rx_total += k_tmp->len; + rx_total += len_aligned; if (rx_total > bufsiz) { status = -EMSGSIZE; goto done; } k_tmp->rx_buf = rx_buf; - rx_buf += k_tmp->len; + rx_buf += len_aligned; } if (u_tmp->tx_buf) { /* this transfer needs space in TX bounce buffer */ - tx_total += k_tmp->len; + tx_total += len_aligned; if (tx_total > bufsiz) { status = -EMSGSIZE; goto done; @@ -267,7 +272,7 @@ static int spidev_message(struct spidev_ (uintptr_t) u_tmp->tx_buf, u_tmp->len)) goto done; - tx_buf += k_tmp->len; + tx_buf += len_aligned; } k_tmp->cs_change = !!u_tmp->cs_change; @@ -297,16 +302,16 @@ static int spidev_message(struct spidev_ goto done; /* copy any rx data out of bounce buffer */ - rx_buf = spidev->rx_buffer; - for (n = n_xfers, u_tmp = u_xfers; n; n--, u_tmp++) { + for (n = n_xfers, k_tmp = k_xfers, u_tmp = u_xfers; + n; + n--, k_tmp++, u_tmp++) { if (u_tmp->rx_buf) { if (copy_to_user((u8 __user *) - (uintptr_t) u_tmp->rx_buf, rx_buf, + (uintptr_t) u_tmp->rx_buf, k_tmp->rx_buf, u_tmp->len)) { status = -EFAULT; goto done; } - rx_buf += u_tmp->len; } } status = total;