Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp2213639imm; Mon, 28 May 2018 04:04:59 -0700 (PDT) X-Google-Smtp-Source: AB8JxZpSk1Hqr+CtE0ZvJLL/Ua4aSd1Re9jZqfkG7TQhSa1MscuY8tGrvBHqCqpENPFX4aO2hfQU X-Received: by 2002:a17:902:5602:: with SMTP id h2-v6mr13417894pli.115.1527505499750; Mon, 28 May 2018 04:04:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527505499; cv=none; d=google.com; s=arc-20160816; b=e2rDkAvUF6xW8fx2kpknjwbIlndsmwTOsEz4WLJwHNNkpgBRN31zapAKOOyNlNJQr2 6lIlro6qc+MV5Vz563QCFAUF3Na6Q2ZWQNyRw4meF6cWqVM1POmgXMaRZzsKGawkbsy/ NVD8gbVheSWK7IwHBEKc10H6zdMKtgMBxfC6AeI3yBDgBgVZgxXMy6m2cK3Udqnzfy1d IeXYHJgrhWeEEhcmfEO+X1h+7DRNDfPk3rzq8PI3rr3VQ2xHsTxCliUfSoTQp7rjr3Jm KZeV3+Lp/3NHrM/zHC0S790N31aCXTZGCvAUctxcdssznb8n6QBlJRlaNT47ccJaY1LM H1Zw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from:dkim-signature :arc-authentication-results; bh=ZhBjN9k1SFrqbMgdeE3rrsIyUNGmKAhCBY7LJ0biRuo=; b=mXz5l4poR09AJM8UuC0zi7gdBt4mIWeDm6n/CsdLDOTuJMU7OpaDYm++AWIZIjSL3p m5K+gyJUk83hq8PhNgsiIAtm1h6yPStlpNOD/PYxvR0tl7IwUq7Z3m/c29z/3Mvfp6p+ yx4Xfhg15ZfjrR7/mEDQ+ed+He1zN3m9QmzjCHg/cZbzBRHajqcfkbiYXxxiQLLjP5RH bboE+taq3jwqOB8tPiWH+Q9rMOofN7Z6DPDqyRt1nUHQ+/Z0tVPI6jHQ1XdH+A/FlCxH rT6xnVu4CbX9NP0AE8IUqE7qDfgxNRrVkyYX5eY8rRUOPzqe/a0pvZxBKKsdi4FZrqI8 BKJA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=rsOE7KHw; 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 b59-v6si29981289plc.335.2018.05.28.04.04.44; Mon, 28 May 2018 04:04:59 -0700 (PDT) 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=rsOE7KHw; 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 S1423039AbeE1LEZ (ORCPT + 99 others); Mon, 28 May 2018 07:04:25 -0400 Received: from mail.kernel.org ([198.145.29.99]:51462 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1164993AbeE1LEW (ORCPT ); Mon, 28 May 2018 07:04:22 -0400 Received: from localhost (LFbn-1-12247-202.w90-92.abo.wanadoo.fr [90.92.61.202]) (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 DDED3208A1; Mon, 28 May 2018 11:04:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1527505461; bh=qQkynfUDaG95ACkPXoJ1XkeGvGFZ2nYgkdBrxIH+h7A=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rsOE7KHwjVMvPFmVrYrVtuZhbnjBRHAWuV/U00gW1phlWVL2LgXx7wB+xc1MPr8XC LJFZyb0l6oE0UO8dUpZge+SS0dR450oKVQNTy96nsdha2Y9F/9K+mRo538NgYQu9zD ePvNR2LAO2RQlM6yMK3aqha6b7MYMsmwvWPh+SlA= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, NeilBrown , Ralf Baechle , Paul Burton , linux-mips@linux-mips.org, James Hogan Subject: [PATCH 4.16 003/272] MIPS: c-r4k: Fix data corruption related to cache coherence Date: Mon, 28 May 2018 12:00:36 +0200 Message-Id: <20180528100240.501786137@linuxfoundation.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180528100240.256525891@linuxfoundation.org> References: <20180528100240.256525891@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.16-stable review patch. If anyone has any objections, please let me know. ------------------ From: NeilBrown commit 55a2aa08b3af519a9693f99cdf7fa6d8b62d9f65 upstream. When DMA will be performed to a MIPS32 1004K CPS, the L1-cache for the range needs to be flushed and invalidated first. The code currently takes one of two approaches. 1/ If the range is less than the size of the dcache, then HIT type requests flush/invalidate cache lines for the particular addresses. HIT-type requests a globalised by the CPS so this is safe on SMP. 2/ If the range is larger than the size of dcache, then INDEX type requests flush/invalidate the whole cache. INDEX type requests affect the local cache only. CPS does not propagate them in any way. So this invalidation is not safe on SMP CPS systems. Data corruption due to '2' can quite easily be demonstrated by repeatedly "echo 3 > /proc/sys/vm/drop_caches" and then sha1sum a file that is several times the size of available memory. Dropping caches means that large contiguous extents (large than dcache) are more likely. This was not a problem before Linux-4.8 because option 2 was never used if CONFIG_MIPS_CPS was defined. The commit which removed that apparently didn't appreciate the full consequence of the change. We could, in theory, globalize the INDEX based flush by sending an IPI to other cores. These cache invalidation routines can be called with interrupts disabled and synchronous IPI require interrupts to be enabled. Asynchronous IPI may not trigger writeback soon enough. So we cannot use IPI in practice. We can already test if IPI would be needed for an INDEX operation with r4k_op_needs_ipi(R4K_INDEX). If this is true then we mustn't try the INDEX approach as we cannot use IPI. If this is false (e.g. when there is only one core and hence one L1 cache) then it is safe to use the INDEX approach without IPI. This patch avoids options 2 if r4k_op_needs_ipi(R4K_INDEX), and so eliminates the corruption. Fixes: c00ab4896ed5 ("MIPS: Remove cpu_has_safe_index_cacheops") Signed-off-by: NeilBrown Cc: Ralf Baechle Cc: Paul Burton Cc: linux-mips@linux-mips.org Cc: # 4.8+ Patchwork: https://patchwork.linux-mips.org/patch/19259/ Signed-off-by: James Hogan Signed-off-by: Greg Kroah-Hartman --- arch/mips/mm/c-r4k.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) --- a/arch/mips/mm/c-r4k.c +++ b/arch/mips/mm/c-r4k.c @@ -851,9 +851,12 @@ static void r4k_dma_cache_wback_inv(unsi /* * Either no secondary cache or the available caches don't have the * subset property so we have to flush the primary caches - * explicitly + * explicitly. + * If we would need IPI to perform an INDEX-type operation, then + * we have to use the HIT-type alternative as IPI cannot be used + * here due to interrupts possibly being disabled. */ - if (size >= dcache_size) { + if (!r4k_op_needs_ipi(R4K_INDEX) && size >= dcache_size) { r4k_blast_dcache(); } else { R4600_HIT_CACHEOP_WAR_IMPL; @@ -890,7 +893,7 @@ static void r4k_dma_cache_inv(unsigned l return; } - if (size >= dcache_size) { + if (!r4k_op_needs_ipi(R4K_INDEX) && size >= dcache_size) { r4k_blast_dcache(); } else { R4600_HIT_CACHEOP_WAR_IMPL;