From patchwork Tue Nov 17 05:44:08 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhenzhong Duan X-Patchwork-Id: 1401393 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=23.128.96.18; helo=vger.kernel.org; envelope-from=linux-pci-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20161025 header.b=jiHoeTH2; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 4CZvzb0tz6z9sSs for ; Tue, 17 Nov 2020 16:45:15 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726302AbgKQFo4 (ORCPT ); Tue, 17 Nov 2020 00:44:56 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45538 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725355AbgKQFoy (ORCPT ); Tue, 17 Nov 2020 00:44:54 -0500 Received: from mail-pg1-x542.google.com (mail-pg1-x542.google.com [IPv6:2607:f8b0:4864:20::542]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DD433C0613CF; Mon, 16 Nov 2020 21:44:53 -0800 (PST) Received: by mail-pg1-x542.google.com with SMTP id 34so12085875pgp.10; Mon, 16 Nov 2020 21:44:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=aM+Nm6mpUW3MCGzLuyldKTZgaVA5NUmu8kqnaySSbTA=; b=jiHoeTH2eVzDH2nroMQlXXEfsqWUlT8XzPEP5m3GuE9cJMD7d+qaWujwIVrgYF0gI9 /VyXhVAb+sduQ/t69Ze3j70EhmFxvhCQE2vAupTY2aFk6sDItUlGCr2ie8ti13GTfZj3 QsVxhh+0q/helyaH0W802fEacqAlaTct5lir81NQkfdvzlXXPbKZeMCuXgaHB8vv4A71 4gLMt4yIzMfPPHiM8cvCJwDmxyOtm4aBYcixN+5DB1bYbSp0FC0MUzB1xqlDmMKCYCLv R2n+OyOu9pnO4aI+w9PUsykVos8Ft+czGGakYfVS/8ClJn1ZIOwLvCYdUk8LxkmmCO2X ogDA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=aM+Nm6mpUW3MCGzLuyldKTZgaVA5NUmu8kqnaySSbTA=; b=NJaUmXXOv511wqVx0QnWUNeCj3d/iDccHAnzrsgVMQtvtMmaFJ/yyyngZCmc9+DKiy eBfTNnM1OoXjNieTDu1TdVO6s4W0tlmt6f6hOZJDutK1uPJ/U2SASVv7FFREYZNPiZ8F NciJ1QWXm0V7YfkiPAGUKaPnPJxYvzjii7Wsn8w5XSgrx/CS96qrHyF8hNfjvdJlySZB xn6vioAosGPC98NGxGgkvnRooqnCdEmvgFm7OBxsrWrJyO2KWBM1lW0/E0tGMCFFzUKx 8Ne5L0z0flrPGpfvUxLvoNPBlhgkXtu91H2VomPNvsvsrXbqzLMsk7oDrxQB/1fAkFnw jaNQ== X-Gm-Message-State: AOAM532MXsar4Bt7soJ8zTjmyAK9ZiL/FbxoEZIVyEMHUm8KVnXwxJw8 NDNudIXiYbG3elIPR4oPMF9agevHEUc= X-Google-Smtp-Source: ABdhPJwa+OrOSzwQxKXEOfmfS5BX/pQuhVXNnXY51685hG9SSZRcujXe79iUorqYOwh3EurcVdU1Nw== X-Received: by 2002:a63:a57:: with SMTP id z23mr2161811pgk.404.1605591893124; Mon, 16 Nov 2020 21:44:53 -0800 (PST) Received: from ZB-PF0YQ8ZU.360buyad.local ([137.116.162.235]) by smtp.gmail.com with ESMTPSA id m3sm20392462pfd.217.2020.11.16.21.44.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Nov 2020 21:44:52 -0800 (PST) From: Zhenzhong Duan To: linux-kernel@vger.kernel.org Cc: linux-pci@vger.kernel.org, bhelgaas@google.com, hch@infradead.org, alex.williamson@redhat.com, cohuck@redhat.com, Zhenzhong Duan Subject: [PATCH v3 1/2] PCI: move pci_match_device() ahead of new_id_store() Date: Tue, 17 Nov 2020 13:44:08 +0800 Message-Id: <20201117054409.3428-2-zhenzhong.duan@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201117054409.3428-1-zhenzhong.duan@gmail.com> References: <20201117054409.3428-1-zhenzhong.duan@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pci@vger.kernel.org Move pci_match_device() and it's dependencies (pci_match_id() and pci_device_id_any) ahead of new_id_store(). This is preparation work for calling pci_match_device() in new_id_store(). No functional changes. Signed-off-by: Zhenzhong Duan --- drivers/pci/pci-driver.c | 144 +++++++++++++++++++++++------------------------ 1 file changed, 72 insertions(+), 72 deletions(-) diff --git a/drivers/pci/pci-driver.c b/drivers/pci/pci-driver.c index 8b587fc..e928cfa 100644 --- a/drivers/pci/pci-driver.c +++ b/drivers/pci/pci-driver.c @@ -90,6 +90,78 @@ static void pci_free_dynids(struct pci_driver *drv) } /** + * pci_match_id - See if a pci device matches a given pci_id table + * @ids: array of PCI device id structures to search in + * @dev: the PCI device structure to match against. + * + * Used by a driver to check whether a PCI device present in the + * system is in its list of supported devices. Returns the matching + * pci_device_id structure or %NULL if there is no match. + * + * Deprecated, don't use this as it will not catch any dynamic ids + * that a driver might want to check for. + */ +const struct pci_device_id *pci_match_id(const struct pci_device_id *ids, + struct pci_dev *dev) +{ + if (ids) { + while (ids->vendor || ids->subvendor || ids->class_mask) { + if (pci_match_one_device(ids, dev)) + return ids; + ids++; + } + } + return NULL; +} +EXPORT_SYMBOL(pci_match_id); + +static const struct pci_device_id pci_device_id_any = { + .vendor = PCI_ANY_ID, + .device = PCI_ANY_ID, + .subvendor = PCI_ANY_ID, + .subdevice = PCI_ANY_ID, +}; + +/** + * pci_match_device - Tell if a PCI device structure has a matching PCI device id structure + * @drv: the PCI driver to match against + * @dev: the PCI device structure to match against + * + * Used by a driver to check whether a PCI device present in the + * system is in its list of supported devices. Returns the matching + * pci_device_id structure or %NULL if there is no match. + */ +static const struct pci_device_id *pci_match_device(struct pci_driver *drv, + struct pci_dev *dev) +{ + struct pci_dynid *dynid; + const struct pci_device_id *found_id = NULL; + + /* When driver_override is set, only bind to the matching driver */ + if (dev->driver_override && strcmp(dev->driver_override, drv->name)) + return NULL; + + /* Look at the dynamic ids first, before the static ones */ + spin_lock(&drv->dynids.lock); + list_for_each_entry(dynid, &drv->dynids.list, node) { + if (pci_match_one_device(&dynid->id, dev)) { + found_id = &dynid->id; + break; + } + } + spin_unlock(&drv->dynids.lock); + + if (!found_id) + found_id = pci_match_id(drv->id_table, dev); + + /* driver_override will always match, send a dummy id */ + if (!found_id && dev->driver_override) + found_id = &pci_device_id_any; + + return found_id; +} + +/** * store_new_id - sysfs frontend to pci_add_dynid() * @driver: target device driver * @buf: buffer for scanning device ID data @@ -208,78 +280,6 @@ static ssize_t remove_id_store(struct device_driver *driver, const char *buf, }; ATTRIBUTE_GROUPS(pci_drv); -/** - * pci_match_id - See if a pci device matches a given pci_id table - * @ids: array of PCI device id structures to search in - * @dev: the PCI device structure to match against. - * - * Used by a driver to check whether a PCI device present in the - * system is in its list of supported devices. Returns the matching - * pci_device_id structure or %NULL if there is no match. - * - * Deprecated, don't use this as it will not catch any dynamic ids - * that a driver might want to check for. - */ -const struct pci_device_id *pci_match_id(const struct pci_device_id *ids, - struct pci_dev *dev) -{ - if (ids) { - while (ids->vendor || ids->subvendor || ids->class_mask) { - if (pci_match_one_device(ids, dev)) - return ids; - ids++; - } - } - return NULL; -} -EXPORT_SYMBOL(pci_match_id); - -static const struct pci_device_id pci_device_id_any = { - .vendor = PCI_ANY_ID, - .device = PCI_ANY_ID, - .subvendor = PCI_ANY_ID, - .subdevice = PCI_ANY_ID, -}; - -/** - * pci_match_device - Tell if a PCI device structure has a matching PCI device id structure - * @drv: the PCI driver to match against - * @dev: the PCI device structure to match against - * - * Used by a driver to check whether a PCI device present in the - * system is in its list of supported devices. Returns the matching - * pci_device_id structure or %NULL if there is no match. - */ -static const struct pci_device_id *pci_match_device(struct pci_driver *drv, - struct pci_dev *dev) -{ - struct pci_dynid *dynid; - const struct pci_device_id *found_id = NULL; - - /* When driver_override is set, only bind to the matching driver */ - if (dev->driver_override && strcmp(dev->driver_override, drv->name)) - return NULL; - - /* Look at the dynamic ids first, before the static ones */ - spin_lock(&drv->dynids.lock); - list_for_each_entry(dynid, &drv->dynids.list, node) { - if (pci_match_one_device(&dynid->id, dev)) { - found_id = &dynid->id; - break; - } - } - spin_unlock(&drv->dynids.lock); - - if (!found_id) - found_id = pci_match_id(drv->id_table, dev); - - /* driver_override will always match, send a dummy id */ - if (!found_id && dev->driver_override) - found_id = &pci_device_id_any; - - return found_id; -} - struct drv_dev_and_id { struct pci_driver *drv; struct pci_dev *dev; From patchwork Tue Nov 17 05:44:09 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhenzhong Duan X-Patchwork-Id: 1401394 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=23.128.96.18; helo=vger.kernel.org; envelope-from=linux-pci-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20161025 header.b=TdPQIJxs; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 4CZvzb4yZzz9s0b for ; Tue, 17 Nov 2020 16:45:15 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726620AbgKQFo5 (ORCPT ); Tue, 17 Nov 2020 00:44:57 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45544 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725355AbgKQFo4 (ORCPT ); Tue, 17 Nov 2020 00:44:56 -0500 Received: from mail-pg1-x542.google.com (mail-pg1-x542.google.com [IPv6:2607:f8b0:4864:20::542]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 89DF1C0613CF; Mon, 16 Nov 2020 21:44:56 -0800 (PST) Received: by mail-pg1-x542.google.com with SMTP id i13so15354562pgm.9; Mon, 16 Nov 2020 21:44:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=lLOKhmR08M0B/4atKVerRPzwm0RJuJz/KvTr7deGbiY=; b=TdPQIJxsRBlp9yFp8khc/3gjrU3Rs8c9d2Qtnp7wxpbVCVXB75KctfQTgLpTISwXa7 y6iCySS9lFomqIruA5i1k68TPgFAvmiVGfQy+t1NFZgp8/op8S3pUmmShii8IAKj6Y+m EwgWFl0l9+a74b9AExu4CHf3IbeiTj04fOcl/cEl87nm9RGj6RL9+SvKgGNvthDRHOSz VrxRpgxc65/2j39GldFzQ748O5B4Qg8glAPi+TD4XdvfOcEv7E0GH6P/e7XNI23QVJto wlxvcaUKoHvotGlYpOyOV0ktYu+JP49iU5sH9IsO2BPq/DYBpvPULSaAyE6NrtkmfQLP XRFw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=lLOKhmR08M0B/4atKVerRPzwm0RJuJz/KvTr7deGbiY=; b=bUR9TaC11SzPDk0egfNb2K0qdMcmKAQXXIJ3gjAmAO88uLaQsvl3L0+52ORQ2Ioe8M 4sx1A19dng/aJtpbxlD5yTvfj8HX9qsXjuB8TYfesfzCMTk2LKKcAOzkEuitqyPKe1DH 4KXq7stkeIdOcFt2Mad24xMTu0C0xo6lgWRQHX8TqEwJJln6JOQ1SPMJoMYCk08Asrnq FqMwobHht8PPfJ3Fd1GdntIXg4RedYZajvpEe9IMGLESXTPr5dyq03gKpHuVoEbNjwcv eO+fEf8sDxe90eGraDAqpvbQ209hE8PN1crauJjBRyamxmLV4GHoIJXe4rUWh+1rdIMz ImOQ== X-Gm-Message-State: AOAM53320TAJCkz7qKWRMXqqEI7VqJN6p2mQ44RYkSE2D5MW41MgoGcl wdOnIdkzSJeQIzQuNscQL8wyP9Z279A= X-Google-Smtp-Source: ABdhPJzDvPSQytIfyRltrfuWFBufyW6jCZVITefa5VjDftT4j+qfDPY6Fg6uc26ISp8sldarztuUFQ== X-Received: by 2002:a63:5262:: with SMTP id s34mr2239785pgl.382.1605591895853; Mon, 16 Nov 2020 21:44:55 -0800 (PST) Received: from ZB-PF0YQ8ZU.360buyad.local ([137.116.162.235]) by smtp.gmail.com with ESMTPSA id m3sm20392462pfd.217.2020.11.16.21.44.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Nov 2020 21:44:55 -0800 (PST) From: Zhenzhong Duan To: linux-kernel@vger.kernel.org Cc: linux-pci@vger.kernel.org, bhelgaas@google.com, hch@infradead.org, alex.williamson@redhat.com, cohuck@redhat.com, Zhenzhong Duan Subject: [PATCH v3 2/2] PCI: avoid duplicate IDs in dynamic IDs list Date: Tue, 17 Nov 2020 13:44:09 +0800 Message-Id: <20201117054409.3428-3-zhenzhong.duan@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201117054409.3428-1-zhenzhong.duan@gmail.com> References: <20201117054409.3428-1-zhenzhong.duan@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pci@vger.kernel.org When a device ID data is writen to /sys/bus/pci/drivers/.../new_id, only static ID table is checked for duplicate and multiple dynamic ID entries of same kind are allowed to exist in a dynamic IDs list. This doesn't cause user-visible broken behavior, but not user friendly. remove_id_store() only remove one of the duplicate IDs, so if we add an ID several times, we would have to remove it the same number of times before it's completely gone. Fix it by calling pci_match_device() which checks both dynamic and static IDs to avoid inserting duplicate IDs in dynamic IDs list. After fix, it shows below result which is expected: echo "1af4 1041" > /sys/bus/pci/drivers/vfio-pci/new_id echo "1af4 1041" > /sys/bus/pci/drivers/vfio-pci/new_id -bash: echo: write error: File exists Signed-off-by: Zhenzhong Duan --- drivers/pci/pci-driver.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/pci/pci-driver.c b/drivers/pci/pci-driver.c index e928cfa..c4678d8 100644 --- a/drivers/pci/pci-driver.c +++ b/drivers/pci/pci-driver.c @@ -197,7 +197,7 @@ static ssize_t new_id_store(struct device_driver *driver, const char *buf, pdev->subsystem_device = subdevice; pdev->class = class; - if (pci_match_id(pdrv->id_table, pdev)) + if (pci_match_device(pdrv, pdev)) retval = -EEXIST; kfree(pdev);