Received: by 2002:ac0:946b:0:0:0:0:0 with SMTP id j40csp2660907imj; Mon, 11 Feb 2019 06:37:56 -0800 (PST) X-Google-Smtp-Source: AHgI3IZb+nCeQvt0wetOTYVQi1dId4LOiDpRXI0ZhVTFbykRKAIFY00R8mrw8h1w3gmpyvuDCSr4 X-Received: by 2002:a63:9348:: with SMTP id w8mr12763089pgm.198.1549895876486; Mon, 11 Feb 2019 06:37:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1549895876; cv=none; d=google.com; s=arc-20160816; b=yA6TW2eUfgN2EeGNFh7tU+1eWtSxR9taQPbw/VRB2wwblDZK+f8Zn5LoWEsQtL5cjR 5CkYryxKP6eVR1PpN9NDV1XdsY3lVQJoGm2JcR9+WJ3SxcAqJBT9NQflgsUeDsg6AVqp K/h9hZvsBlniTy5vuVPlpfIZAA3yJCXZgYG7XAUwLQreoSMQR8W3Lgvc+6zcy8urRTXc VF4ZWLb7QESf76G7VZDfH5QNdPDP7Wp+NZurlQ3FMyaI6oZmoNhi0ERPkVre2y0KN5BG FcqXXGuGdZXMwDUPjDheHlRk7CWMO4i4nGnp6Iln6VuSx6JpWlcK/wUxLD6UyakJHYs4 0JCw== 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=hVOTVxYtkDPnw6nEmfoOERCSueQmTcXJkI/9UADS+ns=; b=TYHxlA+hQjJT6jit0Vv4/GcH2XROBqt8gAtVVhkOkCmL1inCgsGw4kagIuBxMu4gvm Q4rSS1RNBdJaC8fJGAhmhEeF0J+H0Eyscgq2yJqiNDKKSbfQvnaUsV6BWWtS+1SHFJZb GBs2hZLsX/h9aJVbZwLqSH4QnWMzFzO3PTr3GLXMrN13RoiWNp7WwxB/6LkIQy75wppK eBIUBfgMHJjYkXF+TTKpHm1ixJxJlG+MfsGcrA/6lS7q5vHFBjOJYEQnVqxDKeaIVIQl uf6madNHHP2hvcW/LX66yeawq5JlNA8eMl5jr1+f2LcHswDLNGAnYW4n1FKlZ9qkPXsf tLhw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=dJTwRxJ8; 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 61si10640767plz.117.2019.02.11.06.37.40; Mon, 11 Feb 2019 06:37:56 -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=dJTwRxJ8; 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 S1731338AbfBKOgw (ORCPT + 99 others); Mon, 11 Feb 2019 09:36:52 -0500 Received: from mail.kernel.org ([198.145.29.99]:46038 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731312AbfBKOgs (ORCPT ); Mon, 11 Feb 2019 09:36:48 -0500 Received: from localhost (5356596B.cm-6-7b.dynamic.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 440642081B; Mon, 11 Feb 2019 14:36:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1549895807; bh=aeS0HaRu88W588LAV7Pqmmwb43YV6Uw7p9Mbe1DbYDE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=dJTwRxJ8uS2fNa80obyzCnis+MV0J3bJNF1WWiG71rNqpz2uVFCoGBfNxMIq3qo0L 3d4BeVYrih9ApSlCw8Lxc6aDy7IINoIfZIPbjOj8PpDGQMr7EywhMwNdDyaFDU/F68 2JFIkaNb9pCoKd2OwKNRL667NbZgCxR2CkS3eTlM= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Zheng Xiang , Marc Zyngier Subject: [PATCH 4.20 329/352] irqchip/gic-v3-its: Plug allocation race for devices sharing a DevID Date: Mon, 11 Feb 2019 15:19:16 +0100 Message-Id: <20190211141907.813555844@linuxfoundation.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190211141846.543045703@linuxfoundation.org> References: <20190211141846.543045703@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review X-Patchwork-Hint: ignore 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 4.20-stable review patch. If anyone has any objections, please let me know. ------------------ From: Marc Zyngier commit 9791ec7df0e7b4d80706ccea8f24b6542f6059e9 upstream. On systems or VMs where multiple devices share a single DevID (because they sit behind a PCI bridge, or because the HW is broken in funky ways), we reuse the save its_device structure in order to reflect this. It turns out that there is a distinct lack of locking when looking up the its_device, and two device being probed concurrently can result in double allocations. That's obviously not nice. A solution for this is to have a per-ITS mutex that serializes device allocation. A similar issue exists on the freeing side, which can run concurrently with the allocation. On top of now taking the appropriate lock, we also make sure that a shared device is never freed, as we have no way to currently track the life cycle of such object. Reported-by: Zheng Xiang Tested-by: Zheng Xiang Cc: stable@vger.kernel.org Signed-off-by: Marc Zyngier Signed-off-by: Greg Kroah-Hartman --- drivers/irqchip/irq-gic-v3-its.c | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) --- a/drivers/irqchip/irq-gic-v3-its.c +++ b/drivers/irqchip/irq-gic-v3-its.c @@ -97,9 +97,14 @@ struct its_device; * The ITS structure - contains most of the infrastructure, with the * top-level MSI domain, the command queue, the collections, and the * list of devices writing to it. + * + * dev_alloc_lock has to be taken for device allocations, while the + * spinlock must be taken to parse data structures such as the device + * list. */ struct its_node { raw_spinlock_t lock; + struct mutex dev_alloc_lock; struct list_head entry; void __iomem *base; phys_addr_t phys_base; @@ -156,6 +161,7 @@ struct its_device { void *itt; u32 nr_ites; u32 device_id; + bool shared; }; static struct { @@ -2422,6 +2428,7 @@ static int its_msi_prepare(struct irq_do struct its_device *its_dev; struct msi_domain_info *msi_info; u32 dev_id; + int err = 0; /* * We ignore "dev" entierely, and rely on the dev_id that has @@ -2444,6 +2451,7 @@ static int its_msi_prepare(struct irq_do return -EINVAL; } + mutex_lock(&its->dev_alloc_lock); its_dev = its_find_device(its, dev_id); if (its_dev) { /* @@ -2451,18 +2459,22 @@ static int its_msi_prepare(struct irq_do * another alias (PCI bridge of some sort). No need to * create the device. */ + its_dev->shared = true; pr_debug("Reusing ITT for devID %x\n", dev_id); goto out; } its_dev = its_create_device(its, dev_id, nvec, true); - if (!its_dev) - return -ENOMEM; + if (!its_dev) { + err = -ENOMEM; + goto out; + } pr_debug("ITT %d entries, %d bits\n", nvec, ilog2(nvec)); out: + mutex_unlock(&its->dev_alloc_lock); info->scratchpad[0].ptr = its_dev; - return 0; + return err; } static struct msi_domain_ops its_msi_domain_ops = { @@ -2566,6 +2578,7 @@ static void its_irq_domain_free(struct i { struct irq_data *d = irq_domain_get_irq_data(domain, virq); struct its_device *its_dev = irq_data_get_irq_chip_data(d); + struct its_node *its = its_dev->its; int i; for (i = 0; i < nr_irqs; i++) { @@ -2580,8 +2593,14 @@ static void its_irq_domain_free(struct i irq_domain_reset_irq_data(data); } - /* If all interrupts have been freed, start mopping the floor */ - if (bitmap_empty(its_dev->event_map.lpi_map, + mutex_lock(&its->dev_alloc_lock); + + /* + * If all interrupts have been freed, start mopping the + * floor. This is conditionned on the device not being shared. + */ + if (!its_dev->shared && + bitmap_empty(its_dev->event_map.lpi_map, its_dev->event_map.nr_lpis)) { its_lpi_free(its_dev->event_map.lpi_map, its_dev->event_map.lpi_base, @@ -2593,6 +2612,8 @@ static void its_irq_domain_free(struct i its_free_device(its_dev); } + mutex_unlock(&its->dev_alloc_lock); + irq_domain_free_irqs_parent(domain, virq, nr_irqs); } @@ -3517,6 +3538,7 @@ static int __init its_probe_one(struct r } raw_spin_lock_init(&its->lock); + mutex_init(&its->dev_alloc_lock); INIT_LIST_HEAD(&its->entry); INIT_LIST_HEAD(&its->its_device_list); typer = gic_read_typer(its_base + GITS_TYPER);