Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp3329398imm; Tue, 29 May 2018 05:30:07 -0700 (PDT) X-Google-Smtp-Source: AB8JxZoG1+GyYxnTMw79W3xkPGzCRUo2Zd/cipWyJkrdSEEPIH6oD2Be+6EHxE4t2bjl4BwaQQqd X-Received: by 2002:a17:902:8207:: with SMTP id x7-v6mr17473660pln.100.1527597007575; Tue, 29 May 2018 05:30:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527597007; cv=none; d=google.com; s=arc-20160816; b=Xuip3aMn824WeyhEwV+xp7xuyqwSl8jJ/Qu463tudNS7Yah9i25MU2Qa0K74HI4cO0 h3JfgognULJ+mu46BItQbQt0RgdLJXC5pJ1G3Whb36x5YkkNpP2RyNayS+T7hM/Ke+kY Djgc/elDEjYLbBkmBTcUXOFXlzftYxnJFV/hce4kSYT1TN6tHVFDgpvex30o9a78vp8g 4uLDYkqMwpy3ix0ZBqX4rwCaERhLTagZq510NvmZQYUGdOh8Qfc90ec2QiTDBQaO+INk DlyABxui5e1+4uRxPBlzglVAAsViu68HaSvrrGTWw2ZGvd+lo6pqhDPQVZ/ZXbi0DWet xUxg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:message-id:date:subject:cc :to:from:arc-authentication-results; bh=aWrt6E9jv6A8X2ZfiIgoQ8i5Fk2dYgvn7nGdoZpscN4=; b=tbsdhpKzZ6kjxVkJ6USrr+YEzLbCYWqZnWyO+FBS59HyeeZFMJI65nWa6Uz7iBw7k4 uWhCN7Ts0jEiBMR2cqeawN5R+Na89+Blq30LDe6c1Iz/6xjNAlcfkuBQXvTxHCMI9hCP JYGk82VSlncMqUqV3uHHBNDhgXQPKVKAjuNB1HeFH/CmdPZGyUPeRWztttqffUlmOQ1G KvS3ke/+stIkZLRSeh+opAyrxi0C7LyTcELAyvuARnSWXPVamHCK8bvDe/MLmlGsngX8 AVZoU4AyqniqMnEMAAYN0NRq1NP17Dy5Tpw3R9Ia76TNk6ko6SZd7Mylr5NoopjPp4Bd HRmw== ARC-Authentication-Results: i=1; mx.google.com; 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 z1-v6si31826769plb.558.2018.05.29.05.29.53; Tue, 29 May 2018 05:30:07 -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; 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 S933774AbeE2M3S (ORCPT + 99 others); Tue, 29 May 2018 08:29:18 -0400 Received: from szxga05-in.huawei.com ([45.249.212.191]:8202 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S933697AbeE2M3O (ORCPT ); Tue, 29 May 2018 08:29:14 -0400 Received: from DGGEMS405-HUB.china.huawei.com (unknown [172.30.72.58]) by Forcepoint Email with ESMTP id AB7206FE1263A; Tue, 29 May 2018 20:28:59 +0800 (CST) Received: from linux-ibm.site (10.175.102.37) by DGGEMS405-HUB.china.huawei.com (10.3.19.205) with Microsoft SMTP Server id 14.3.382.0; Tue, 29 May 2018 20:28:54 +0800 From: Yisheng Xie To: , , , , , , , CC: , , , Yisheng Xie Subject: [PATCH v3 1/2] PCI: Avoid panic when PCI IO resource's size is not page aligned Date: Tue, 29 May 2018 20:18:18 +0800 Message-ID: <1527596299-57567-1-git-send-email-xieyisheng1@huawei.com> X-Mailer: git-send-email 1.7.12.4 MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.175.102.37] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Zhou reported a bug on Hisilicon arm64 D06 platform with 64KB page size: [ 2.470908] kernel BUG at lib/ioremap.c:72! [ 2.475079] Internal error: Oops - BUG: 0 [#1] PREEMPT SMP [ 2.480551] Modules linked in: [ 2.483594] CPU: 1 PID: 1 Comm: swapper/0 Not tainted 4.16.0-rc7-00062-g0b41260-dirty #23 [ 2.491756] Hardware name: Huawei D06/D06, BIOS Hisilicon D06 UEFI Nemo 2.0 RC0 - B120 03/23/2018 [ 2.500614] pstate: 80c00009 (Nzcv daif +PAN +UAO) [ 2.505395] pc : ioremap_page_range+0x268/0x36c [ 2.509912] lr : pci_remap_iospace+0xe4/0x100 [...] [ 2.603733] Call trace: [ 2.606168] ioremap_page_range+0x268/0x36c [ 2.610337] pci_remap_iospace+0xe4/0x100 [ 2.614334] acpi_pci_probe_root_resources+0x1d4/0x214 [ 2.619460] pci_acpi_root_prepare_resources+0x18/0xa8 [ 2.624585] acpi_pci_root_create+0x98/0x214 [ 2.628843] pci_acpi_scan_root+0x124/0x20c [ 2.633013] acpi_pci_root_add+0x224/0x494 [ 2.637096] acpi_bus_attach+0xf8/0x200 [ 2.640918] acpi_bus_attach+0x98/0x200 [ 2.644740] acpi_bus_attach+0x98/0x200 [ 2.648562] acpi_bus_scan+0x48/0x9c [ 2.652125] acpi_scan_init+0x104/0x268 [ 2.655948] acpi_init+0x308/0x374 [ 2.659337] do_one_initcall+0x48/0x14c [ 2.663160] kernel_init_freeable+0x19c/0x250 [ 2.667504] kernel_init+0x10/0x100 [ 2.670979] ret_from_fork+0x10/0x18 The cause is the size of PCI IO resource is 32KB, which is 4K aligned but not 64KB aligned, however, ioremap_page_range() request the range as page aligned or it will trigger a BUG_ON() on ioremap_pte_range() it calls, as ioremap_pte_range increase the addr by PAGE_SIZE, which makes addr != end until trigger BUG_ON, if its incoming end is not page aligned. More detail trace is as following: ioremap_page_range -> ioremap_p4d_range -> ioremap_p4d_range -> ioremap_pud_range -> ioremap_pmd_range -> ioremap_pte_range This patch avoid panic by return -EINVAL if vaddr or resource size is not page aligned. Reported-by: Zhou Wang Tested-by: Xiaojun Tan Signed-off-by: Yisheng Xie --- v3: - pci_remap_iospace() sanitize its arguments instead - per Rafael v2: - Let the caller of ioremap_page_range() align the request by PAGE_SIZE - per Toshi drivers/pci/pci.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c index dbfe7c4..0eb0381 100644 --- a/drivers/pci/pci.c +++ b/drivers/pci/pci.c @@ -3544,6 +3544,9 @@ int pci_remap_iospace(const struct resource *res, phys_addr_t phys_addr) if (res->end > IO_SPACE_LIMIT) return -EINVAL; + if (!PAGE_ALIGNED(vaddr) || !PAGE_ALIGNED(resource_size(res))) + return -EINVAL; + return ioremap_page_range(vaddr, vaddr + resource_size(res), phys_addr, pgprot_device(PAGE_KERNEL)); #else -- 1.7.12.4