Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp429633imu; Wed, 12 Dec 2018 20:40:17 -0800 (PST) X-Google-Smtp-Source: AFSGD/W/AonHokpERULbcTfqKslasA2tthJ+kDoPQFMhvGP/b7VL+aUARoEYaNqcb20HxIVxLvQo X-Received: by 2002:a17:902:704b:: with SMTP id h11mr22525851plt.157.1544676017796; Wed, 12 Dec 2018 20:40:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1544676017; cv=none; d=google.com; s=arc-20160816; b=ND4qWTfEOTbNIgDy0jw9onz7d4QtFbxnfAP5Mvz8bQ8M/XAuSH1AO+bVcmauSiDEHH oPPG3yALoa3+FBCLAaViYKsczJhm/ub9K5KL+cbZMfSlCL4iMlk5VqAmd17WU/44nGCu EAYiQsleOl7zdo1Y1qQPA5QTfvYLoUuI03sdkYyHdOLWA+fi2g0f9hRWLR0z1GRHkl6P VqBstL3Bw90h4LAaG1kdSHFZ8Di9YfB8nmjMvL5BmDZ9sUOV2d9/RH7YHOtU64C0xftT m5nZzhynCoPgYj+BEP/Y7zE+UuRfE+VnpMvr6/wFFTvoC1EWZm0sQSPMZNbnOoopiXz3 EOWA== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=TR8X6nnj33utKK2e5nhn5m5fUjmWUubCdDA9syBziCk=; b=OW1URlC4aXpz+wXkz1f/BrpomNItaug8GU7xCFHB6IKDZsFGh15WNCVLUN/DCimz5e 5/aEu3nTBOhebNlRsrgmy36hT7tcJkw4fsnlDUBcRfh9BiVPDBiuMlQX4Bx7jfh61yKY pD+MBQIxyEGEVJPFap4LvSJ3jLnlBMPcDbLnZlfpx08eiFO4/bI9Gvi1rFknl1M96ycq pSiteRkPaFs6LKS7ZkuIe64uCfSk1aiBFXe//9cxRKun8A3o+TVfGpegAgln5XqB55qG z0N9G4hUSM5A1Xcdh0BDsk8Q6sw+H8zNSILrD+TSILCBp+JD308IGAnZKVFsNVLArZY0 H4Ug== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=dKHP0cva; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id n24si632930pgv.119.2018.12.12.20.40.03; Wed, 12 Dec 2018 20:40:17 -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=dKHP0cva; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729164AbeLMEjJ (ORCPT + 99 others); Wed, 12 Dec 2018 23:39:09 -0500 Received: from mail.kernel.org ([198.145.29.99]:45590 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727758AbeLMEco (ORCPT ); Wed, 12 Dec 2018 23:32:44 -0500 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id E3D3F20880; Thu, 13 Dec 2018 04:32:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1544675563; bh=0JQlf9lnpUVeeXwAygIuD/6K6JoE6jf2iX7HY821o7k=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=dKHP0cvafZhLMy+dObsNRrwiqby8qL155p/CTbZkdn13hV4IpAqNqnRqPSUSckh4p l0RPWtT/v4xsLDHVjhPgvMAelDDKRE1AasRALboBfUgx/W+LW533HxL9Ucwpm0KugX 7KgwCIqDb0B2qcg8w7sJOO1PRQ/Ya+t9+dJCBktQ= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Chris Cole , Russell King , Sasha Levin Subject: [PATCH AUTOSEL 4.9 25/34] ARM: 8814/1: mm: improve/fix ARM v7_dma_inv_range() unaligned address handling Date: Wed, 12 Dec 2018 23:31:51 -0500 Message-Id: <20181213043200.76295-25-sashal@kernel.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181213043200.76295-1-sashal@kernel.org> References: <20181213043200.76295-1-sashal@kernel.org> MIME-Version: 1.0 X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Chris Cole [ Upstream commit a1208f6a822ac29933e772ef1f637c5d67838da9 ] This patch addresses possible memory corruption when v7_dma_inv_range(start_address, end_address) address parameters are not aligned to whole cache lines. This function issues "invalidate" cache management operations to all cache lines from start_address (inclusive) to end_address (exclusive). When start_address and/or end_address are not aligned, the start and/or end cache lines are first issued "clean & invalidate" operation. The assumption is this is done to ensure that any dirty data addresses outside the address range (but part of the first or last cache lines) are cleaned/flushed so that data is not lost, which could happen if just an invalidate is issued. The problem is that these first/last partial cache lines are issued "clean & invalidate" and then "invalidate". This second "invalidate" is not required and worse can cause "lost" writes to addresses outside the address range but part of the cache line. If another component writes to its part of the cache line between the "clean & invalidate" and "invalidate" operations, the write can get lost. This fix is to remove the extra "invalidate" operation when unaligned addressed are used. A kernel module is available that has a stress test to reproduce the issue and a unit test of the updated v7_dma_inv_range(). It can be downloaded from http://ftp.sageembedded.com/outgoing/linux/cache-test-20181107.tgz. v7_dma_inv_range() is call by dmac_[un]map_area(addr, len, direction) when the direction is DMA_FROM_DEVICE. One can (I believe) successfully argue that DMA from a device to main memory should use buffers aligned to cache line size, because the "clean & invalidate" might overwrite data that the device just wrote using DMA. But if a driver does use unaligned buffers, at least this fix will prevent memory corruption outside the buffer. Signed-off-by: Chris Cole Signed-off-by: Russell King Signed-off-by: Sasha Levin --- arch/arm/mm/cache-v7.S | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/arch/arm/mm/cache-v7.S b/arch/arm/mm/cache-v7.S index a134d8a13d00..11d699af30ed 100644 --- a/arch/arm/mm/cache-v7.S +++ b/arch/arm/mm/cache-v7.S @@ -359,14 +359,16 @@ v7_dma_inv_range: ALT_UP(W(nop)) #endif mcrne p15, 0, r0, c7, c14, 1 @ clean & invalidate D / U line + addne r0, r0, r2 tst r1, r3 bic r1, r1, r3 mcrne p15, 0, r1, c7, c14, 1 @ clean & invalidate D / U line -1: - mcr p15, 0, r0, c7, c6, 1 @ invalidate D / U line - add r0, r0, r2 cmp r0, r1 +1: + mcrlo p15, 0, r0, c7, c6, 1 @ invalidate D / U line + addlo r0, r0, r2 + cmplo r0, r1 blo 1b dsb st ret lr -- 2.19.1