Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753307AbdLSPVA (ORCPT ); Tue, 19 Dec 2017 10:21:00 -0500 Received: from mail-cys01nam02on0052.outbound.protection.outlook.com ([104.47.37.52]:28054 "EHLO NAM02-CY1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1753130AbdLSPU4 (ORCPT ); Tue, 19 Dec 2017 10:20:56 -0500 Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=Tomasz.Nowicki@cavium.com; From: Tomasz Nowicki To: joro@8bytes.org, robin.murphy@arm.com, will.deacon@arm.com, lorenzo.pieralisi@arm.com, bhelgaas@google.com Cc: Jayachandran.Nair@cavium.com, Ganapatrao.Kulkarni@cavium.com, linux-kernel@vger.kernel.org, iommu@lists.linux-foundation.org, linux-arm-kernel@lists.infradead.org, linux-acpi@vger.kernel.org, linux-pci@vger.kernel.org, mw@semihalf.com, stable@vger.kernel.org, Tomasz Nowicki Subject: [PATCH V1 1/1] iommu: Make sure device's ID array elements are unique Date: Tue, 19 Dec 2017 16:20:21 +0100 Message-Id: <1513696821-32291-1-git-send-email-tomasz.nowicki@caviumnetworks.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1513696436-31834-1-git-send-email-tomasz.nowicki@caviumnetworks.com> References: <1513696436-31834-1-git-send-email-tomasz.nowicki@caviumnetworks.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [12.108.191.226] X-ClientProxiedBy: BN6PR16CA0009.namprd16.prod.outlook.com (2603:10b6:404:f5::19) To MWHPR0701MB3659.namprd07.prod.outlook.com (2603:10b6:301:7e::10) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: fa0f5dc0-cb2b-4eb0-0f15-08d546f417dd X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:(5600026)(4604075)(4534020)(4602075)(4627115)(201703031133081)(201702281549075)(2017052603307);SRVR:MWHPR0701MB3659; X-Microsoft-Exchange-Diagnostics: 1;MWHPR0701MB3659;3:mgUQeUS92izqrQe/yuc8IZ0dMhhuBRAKFieZC1mZILrTRzP4u8pzbH5q5eBhLWCz4D3MhlvY9ZJf09zwyiwVrVcJC1gkKAUX2iYrYoW6p5B5rHgsOIM/iiFKL50219+j9wh50vdJWWSN9S6+F56uxekzbsWNcHkP8TYto6RvEWS/C44FccfhqA9NdOhlV8FB5PvggeG24D5IUSHGwgJ+wsF/y79kae5K7aQV3kx08iBriIyz6nRzPV9eO6iscOQZ;25:DbawEsE3MZnQdvN+3QpvApL1l+2leGfiviH2id3O0egQIglGBBv5mN9q0GipE8Aq6yN1AeZVfjYD2LmhBtS+nHdzlwTDYWILz8dnweUVtNUF3TWEyyANI4576aSiFCM1EcAq8rz0c1CHzRc3llcpT4hbRSTRhUgQXHIyXVADO4H5dBADv+P0ApPbCMlDdQ2n9JMNgmo8D19I0ioIeqkYgGjNUjY7rLdRN1xAGkKJvrFcIn9/eZ3BU3KEgydvnvpQXXfure7HDj/GNYiceUfLRpRfmQQO81McJgt1LSVnLYhclQWqEMZO+5/46DmPK7FiE8Kb1TVJ8LZaUgl9mLGWuA==;31:/26dPg/JFrBat1YUPlXy9z6z+NkPrflqnV7lZpysNz4Vi6ax6TsdNM9GWoUyyCAF2bPo8zUBeatEa1TierQ3oJ7GDCENj3qAdK9HrkWxBkGdAUHPxV2WEwIzPaPxipjwqj2ufG2dzEJ7uVC2v034YWZ0O0S10SJH6M+8/TQAFMOf1FsyYPvLQd86FvlfYcWiXP1F8n1OH50Dq4nDWe0mbB5ZNvSm8yEi4GV8/T94rG0= X-MS-TrafficTypeDiagnostic: MWHPR0701MB3659: X-Microsoft-Exchange-Diagnostics: 1;MWHPR0701MB3659;20:WEY0SGNVFWDvM8lUBN4OsVKrMO8POXaoKtKn9YGh/uxMA7w2KgRaSIgrPifPrXSsfStcMDhXKIIU81yvLj422X+/zw6UsOpEE1Q531LZZDAtY+m6qljff8yTnQT0sUX11hBh0HBVT4ZxBC/cNeUFqC3lbsCSIBZhakqvdY0ihHo/FP3pKmWgdS29RJD4lvDu2Cfne/YVm6MPafgHkVhFLr2HuvGz/+LB1YtUQzIwIQR9ayaGvXxul8/HEbyE/ZSoxaq++HNynEwI8ghCDwLWgEqzMouX8pqCarn4qQPs7qmuUTdqwmC+GbkUGhmL8XJt0VZ4AcYxCedalhIytD3wqv62CtQ7jRLGfx19836oRx3w9iGBRbtxl1htu45DIi6RminNwx0Rpw4v8GOeOjNMyzp79Lmd5Wc28DkqgUnLBjQNrFpoJ5nsszq/k9qTj0Ny5ofm6AVsi8Comwg92lkPoLmzDIplETkVxl+9jO4Sfr/u9NY0hSm7WC0ShDlnEUtfEn1aUoA+N466BgAIMEnmO3+FvibUlHNxJoZZO6lHpIxlC+igL4QB5oZZxTxLgqK/3J6S2UYzP74MeFMR4uqS5Qc/WNwzVyf05Xzh/rTOvOo= X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(9452136761055)(131327999870524); X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(6040450)(2401047)(8121501046)(5005006)(3002001)(10201501046)(93006095)(3231023)(6041248)(20161123558100)(20161123560025)(20161123562025)(20161123564025)(201703131423075)(201702281528075)(201703061421075)(201703061406153)(20161123555025)(6072148)(201708071742011);SRVR:MWHPR0701MB3659;BCL:0;PCL:0;RULEID:(100000803101)(100110400095);SRVR:MWHPR0701MB3659; X-Microsoft-Exchange-Diagnostics: 1;MWHPR0701MB3659;4:BAXAXgkfl/ZUk8VZD2w4wUzI2OijBrJ9xMEP2EtDlAa5GohaoqupT9aglgjm7oN5xqqlYw26slLzosYMzWPVfw4SLafA8EvBv58cv8u3kWEI20vCmSLOUKV5U8f6gcL2Up4+7gUOzpfYjGW1pJuSt6u01s9AXsVo14wxJD5UhGOsLl7w+dDe4kdnSArVuqdq2TYmPPw+mhTY1lMsmUDFHu9kg94luKZMnGmcFf9rXc/D/TtiDJfGj7e4DiQGlkY7zWcVwLb8XMgSOW9v2vf4ekrCHKYV0G6z2qNJoEj3HauWRN9E6MjUaJ6epp5XEyUVCM0ZwQ0CwEcgt7IibsZQ3w1fdI1uz0p+Aw2d1SLb9z8= X-Forefront-PRVS: 052670E5A4 X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10009020)(346002)(396003)(366004)(376002)(39860400002)(199004)(189003)(6116002)(3846002)(316002)(66066001)(305945005)(16526018)(16586007)(7736002)(81166006)(81156014)(8676002)(47776003)(50466002)(7416002)(5660300001)(478600001)(25786009)(4326008)(72206003)(48376002)(2950100002)(42882006)(68736007)(6666003)(107886003)(53936002)(8936002)(106356001)(6512007)(76176011)(2906002)(6486002)(50226002)(97736004)(51416003)(6506007)(105586002)(52116002)(386003)(36756003)(575784001);DIR:OUT;SFP:1101;SCL:1;SRVR:MWHPR0701MB3659;H:desktop.semihalf.local;FPR:;SPF:None;PTR:InfoNoRecords;A:1;MX:1;LANG:en; X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1;MWHPR0701MB3659;23:phunRz5y5QbUMubLTivWuCXmvfUGchjlPNhn8o4?= =?us-ascii?Q?voq6BYBNyXCfpaB16LNROsmz3G/iv4W3Orr6fXXqjLlyMe1gbGvQ1aiObZdS?= =?us-ascii?Q?IkQQXUaQm5G0b0ACQGRJCgK1bE9dW7qbaz41Af2gKC/coKyebdAglnmn3P1f?= =?us-ascii?Q?c3QrcRWHwhFT71NOS0Z6F7RiDNpdtE+wey2bsuM0j86drrvYMwaCBsSfFZ8v?= =?us-ascii?Q?c8hRAX9lhxgRGybk9ObpHYWztrk3YtUAfdbpiAGsnLYuYCJUtxJvUoGJzYCw?= =?us-ascii?Q?t0DB6byTT/pZ8qyS4YctR0EojQmzHeeWPOxD9puK8xPvX17j+lofLskaLsIR?= =?us-ascii?Q?Lajy9PKcYp7zhKqrGR0JRhniIjR9m3MooAPYwFsIsNsFgZ6uRevVbYXYs0CC?= =?us-ascii?Q?f8t6OQIfRKPoVecPaw5M3jLs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DO9U4IH5+86XSiVC4+Yc+tI8gHUikXNdFw7MhOUO6syLi4S78dpb3177SGjmN74XafCBbgTnZjlsLnF1cSTDqg3ZaKrDhXe9Jb8pw1/l8X39sSELCaphqJbKmoB97tYu7feq+XdBAWLcoNd3jADgdCQScSdbjJpyOPHWKif/IRcbHNFpr/chLKIt/ihMA1EPA081I=;5:Wcl8V3Y68f5UjQEi4ra8Kfqh4WL5aH5XBnVnt2U4qZCNtDzllIUFWuOmnmNNFZaM5fnBNEG/yWyGgZHC9IHdlDIvH8/4gU9q614q5jDXnkm8wZ0Lj2w2cZBAKbKSOynAhbm3CuT7oh71ymG2fMEVyUeKwL04Qva+vaWJBi+cHEI=;24:nwt6FFGDEmo300SUgs5YCUMKRx6LsDdVZaiu5CFP2Dpi8Za4brD78vyxboEmT51rOscHXBQVSEKDcOoKGHiNuBLpZ+S6WybXd2xs7gs6eCc=;7:NGLXhEJe9ZtSHbHn6rT47r2T+NudzKazpt8IP/ifb/NyAslOdov+ZN0SwObfN0fm8Astl2vTm1aUllRktD0OachkUzibscFqz+sI6aDJImoXHkOZPvIrU6B0/rc9QcYgDF1MfsEOlO5M3uof+H71LT7MsHqwfBfOatUCkvTeAYe1WO4XKxWd/Em13P3zvlV1afvxYCV7fxzPO+ruuBHMqcP3UlShYCU013Ni3QfCIF6lhxbI8DNQkEdY8S0jqHuU SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-OriginatorOrg: caviumnetworks.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 Dec 2017 15:20:47.6152 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: fa0f5dc0-cb2b-4eb0-0f15-08d546f417dd X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 711e4ccf-2e9b-4bcf-a551-4094005b6194 X-MS-Exchange-Transport-CrossTenantHeadersStamped: MWHPR0701MB3659 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 1939 Lines: 63 While iterating over DMA aliases for a PCI device, for some rare cases (i.e. PCIe-to-PCI/X bridges) we may get exactly the same ID as initial child device. In turn, the same ID may get registered for a device multiple times. Eventually IOMMU driver may try to configure the same ID within domain multiple times too which for some IOMMU drivers is illegal and causes kernel panic. Rule out ID duplication prior to device ID array registration. CC: stable@vger.kernel.org # v4.14+ Signed-off-by: Tomasz Nowicki --- drivers/iommu/iommu.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 3de5c0b..9b2c138 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -1945,6 +1945,31 @@ void iommu_fwspec_free(struct device *dev) } EXPORT_SYMBOL_GPL(iommu_fwspec_free); +static void iommu_fwspec_remove_ids_dup(struct device *dev, u32 *ids, + int *num_ids) +{ + struct iommu_fwspec *fwspec = dev->iommu_fwspec; + int i, j, k, valid_ids = *num_ids; + + for (i = 0; i < valid_ids; i++) { + for (j = 0; j < fwspec->num_ids; j++) { + if (ids[i] != fwspec->ids[j]) + continue; + + dev_info(dev, "found 0x%x ID duplication, skipped\n", + ids[i]); + + for (k = i + 1; k < valid_ids; k++) + ids[k - 1] = ids[k]; + + valid_ids--; + break; + } + } + + *num_ids = valid_ids; +} + int iommu_fwspec_add_ids(struct device *dev, u32 *ids, int num_ids) { struct iommu_fwspec *fwspec = dev->iommu_fwspec; @@ -1954,6 +1979,9 @@ int iommu_fwspec_add_ids(struct device *dev, u32 *ids, int num_ids) if (!fwspec) return -EINVAL; + /* Rule out IDs already registered */ + iommu_fwspec_remove_ids_dup(dev, ids, &num_ids); + size = offsetof(struct iommu_fwspec, ids[fwspec->num_ids + num_ids]); if (size > sizeof(*fwspec)) { fwspec = krealloc(dev->iommu_fwspec, size, GFP_KERNEL); -- 2.7.4