Received: by 2002:a25:683:0:0:0:0:0 with SMTP id 125csp689881ybg; Mon, 1 Jun 2020 11:45:53 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwzPmer2vzQIbfxDAZ1leeRUUynWlUXwGGESJ12gVziAimbaVesCMX5BmfLwVMtgdPvgsUl X-Received: by 2002:a05:6402:1558:: with SMTP id p24mr22275908edx.193.1591037153734; Mon, 01 Jun 2020 11:45:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1591037153; cv=none; d=google.com; s=arc-20160816; b=eAR9/bXjpkVajlFSdDknmYr7XLEdDBjJ0xFnQgtAn0Ep/TrpKjW//0DY3jzRXGwHPM 0BLqfT7R0pQzDvmSM4IsnaOlnBGYiUm6g4F8CIVAbuPptpH96JaeGAhJdyDUl2sAosQ9 PUFtqQQFwGxNiNhaKvrNVoB39V7n4hZdk6UZRCyG/JEfsnJq1mlP3r16UuO206XhR4+4 oiFpFyGSBzlHFQv7ExEbNDDh6P20PI/3V1Ja+bDyZJz6vOOTwukzZgQoAp3lkva+byTT aJbElLDkCsTLp293AP4XRa610okgGCtxiqUhrxIx/17HcZOCexxp8ITcH6a2cM3yAKgz 4r4Q== 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=08ghkQw7t3QjnVNm5kFN4mLGta7bTJscIcK9I5VNnlA=; b=FdtlaVI1WBm8IRrZkhofGI8rmxD+1vjmb8tEQEF0fS9UmhS00i2mAzAuioUG5LcUks 18keMpQULdzErjGy4OjTbsOP5B+kE/oSeVqmDIHhDWqIPkOtpAgvv6lndc0NpoaKdDvm 6LAtwcrMOU/GkMOckEsxBwUgS87fugcH2uIqZ7Mb+u/lI9nl/dQcNP0aohoB5tmjWiyN oDK1y8qIiGFGc6GA1Qkv+ujqPtUuMNNFWTS+mXbI3pQlAYuryuJeVKUGrIzgISxWKGRr QFcj6AoizIcf++WTjxXs1tVDU+p/oqplUhB1LZ33rso/LOPBmQCbvyitzk4d4fqFSKUQ xYkw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=ESlMMDwG; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id r9si132407eds.223.2020.06.01.11.45.31; Mon, 01 Jun 2020 11:45:53 -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=ESlMMDwG; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731416AbgFASmm (ORCPT + 99 others); Mon, 1 Jun 2020 14:42:42 -0400 Received: from mail.kernel.org ([198.145.29.99]:58710 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731119AbgFASLc (ORCPT ); Mon, 1 Jun 2020 14:11:32 -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 314C62065C; Mon, 1 Jun 2020 18:11:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1591035091; bh=N0l1RPeo1SlGImHH28ykJupX+PqrhocXisBt8WWswmY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ESlMMDwG59zYqQNI6XfhP/zdCqyBunuY23gsrgfP9dA2onk22ucPOw+E3M1YLu6zd A4wIvph07RiHzSCzCRYMZRy/uQ7lEq70oLgmcQfvLh4z3jM5Dz4l/GdFfIHFmW1o/I DGWToGdcVtdF2k9PtTkx2R6Iy7x/FjXFeXOi2yAE= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Alan Jenkins , Robin Murphy , Alexander Dahl , Borislav Petkov Subject: [PATCH 5.4 110/142] x86/dma: Fix max PFN arithmetic overflow on 32 bit systems Date: Mon, 1 Jun 2020 19:54:28 +0200 Message-Id: <20200601174049.319622345@linuxfoundation.org> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200601174037.904070960@linuxfoundation.org> References: <20200601174037.904070960@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: Alexander Dahl commit 88743470668ef5eb6b7ba9e0f99888e5999bf172 upstream. The intermediate result of the old term (4UL * 1024 * 1024 * 1024) is 4 294 967 296 or 0x100000000 which is no problem on 64 bit systems. The patch does not change the later overall result of 0x100000 for MAX_DMA32_PFN (after it has been shifted by PAGE_SHIFT). The new calculation yields the same result, but does not require 64 bit arithmetic. On 32 bit systems the old calculation suffers from an arithmetic overflow in that intermediate term in braces: 4UL aka unsigned long int is 4 byte wide and an arithmetic overflow happens (the 0x100000000 does not fit in 4 bytes), the in braces result is truncated to zero, the following right shift does not alter that, so MAX_DMA32_PFN evaluates to 0 on 32 bit systems. That wrong value is a problem in a comparision against MAX_DMA32_PFN in the init code for swiotlb in pci_swiotlb_detect_4gb() to decide if swiotlb should be active. That comparison yields the opposite result, when compiling on 32 bit systems. This was not possible before 1b7e03ef7570 ("x86, NUMA: Enable emulation on 32bit too") when that MAX_DMA32_PFN was first made visible to x86_32 (and which landed in v3.0). In practice this wasn't a problem, unless CONFIG_SWIOTLB is active on x86-32. However if one has set CONFIG_IOMMU_INTEL, since c5a5dc4cbbf4 ("iommu/vt-d: Don't switch off swiotlb if bounce page is used") there's a dependency on CONFIG_SWIOTLB, which was not necessarily active before. That landed in v5.4, where we noticed it in the fli4l Linux distribution. We have CONFIG_IOMMU_INTEL active on both 32 and 64 bit kernel configs there (I could not find out why, so let's just say historical reasons). The effect is at boot time 64 MiB (default size) were allocated for bounce buffers now, which is a noticeable amount of memory on small systems like pcengines ALIX 2D3 with 256 MiB memory, which are still frequently used as home routers. We noticed this effect when migrating from kernel v4.19 (LTS) to v5.4 (LTS) in fli4l and got that kernel messages for example: Linux version 5.4.22 (buildroot@buildroot) (gcc version 7.3.0 (Buildroot 2018.02.8)) #1 SMP Mon Nov 26 23:40:00 CET 2018 … Memory: 183484K/261756K available (4594K kernel code, 393K rwdata, 1660K rodata, 536K init, 456K bss , 78272K reserved, 0K cma-reserved, 0K highmem) … PCI-DMA: Using software bounce buffering for IO (SWIOTLB) software IO TLB: mapped [mem 0x0bb78000-0x0fb78000] (64MB) The initial analysis and the suggested fix was done by user 'sourcejedi' at stackoverflow and explicitly marked as GPLv2 for inclusion in the Linux kernel: https://unix.stackexchange.com/a/520525/50007 The new calculation, which does not suffer from that overflow, is the same as for arch/mips now as suggested by Robin Murphy. The fix was tested by fli4l users on round about two dozen different systems, including both 32 and 64 bit archs, bare metal and virtualized machines. [ bp: Massage commit message. ] Fixes: 1b7e03ef7570 ("x86, NUMA: Enable emulation on 32bit too") Reported-by: Alan Jenkins Suggested-by: Robin Murphy Signed-off-by: Alexander Dahl Signed-off-by: Borislav Petkov Reviewed-by: Greg Kroah-Hartman Cc: stable@vger.kernel.org Link: https://unix.stackexchange.com/q/520065/50007 Link: https://web.nettworks.org/bugs/browse/FFL-2560 Link: https://lkml.kernel.org/r/20200526175749.20742-1-post@lespocky.de Signed-off-by: Greg Kroah-Hartman --- arch/x86/include/asm/dma.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) --- a/arch/x86/include/asm/dma.h +++ b/arch/x86/include/asm/dma.h @@ -74,7 +74,7 @@ #define MAX_DMA_PFN ((16UL * 1024 * 1024) >> PAGE_SHIFT) /* 4GB broken PCI/AGP hardware bus master zone */ -#define MAX_DMA32_PFN ((4UL * 1024 * 1024 * 1024) >> PAGE_SHIFT) +#define MAX_DMA32_PFN (1UL << (32 - PAGE_SHIFT)) #ifdef CONFIG_X86_32 /* The maximum address that we can perform a DMA transfer to on this platform */