From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pf1-f194.google.com (mail-pf1-f194.google.com [209.85.210.194]) by dpdk.org (Postfix) with ESMTP id 520F35911 for ; Fri, 14 Sep 2018 17:30:30 +0200 (CEST) Received: by mail-pf1-f194.google.com with SMTP id x17-v6so4478843pfh.5 for ; Fri, 14 Sep 2018 08:30:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=networkplumber-org.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id; bh=k6PywAon4T7sx5r5xj6r19+kKO/5DpU1g0jJOlLaNt4=; b=zkx7MtxPHwPfj85+1ZGmPzot8gytrN9lYCmmJjk8+rNIOrqNJlPvbn5g7c8BcL8leu fK5vBJU6vc/7qT6wPX6WHmiWq2LlzkKoDss/E0pW+P+bBWfqRmUjDfm/+T8NnWGDctZs 6ASznhDQaoL4ATsx3CgAK1xmRnN94RHb7OlQCJW8P/2mL4m76sUeqg7c1486AVl8eKU4 U+SaXF3Al4XSmXEPAxLFe5aQeKN+TQt5Km7p2YBsAvsfVqU30cXyC6qHKYi/EYYfviJP ycyHeWlM+6uRdN3wWAoTJ3dWUwGuNn201CF+wXZZzsIHMuLRSaZLgLQMctf9WQ4mooo5 I4Jw== 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; bh=k6PywAon4T7sx5r5xj6r19+kKO/5DpU1g0jJOlLaNt4=; b=HYOO07ytVoJq4hFpueEdvQL9tvVqM6Y8qtp35mnQNTXs94zVi3ZCyjY6TWHlTDQRvM KC/3f5YEB289jjVtYxvB61Mj0uFkLftuPbE/G/HTbAaIvHI/nsTquSBWbP+PfaHVlfTK 29tK2MzPlnaUUpwQuitah9vFGSdJRgw4IopGbuRMjB6JcJTaUtLl+kvDqefNdVvDJCzv ARw1U7K/bA8s/IgX7sVvle1Zgcw4xxdMPsneaTw/6Rkeb6+WwwZMFIFHzEc/NFVkRfCM R7qtT0wauk58UmV2+ZJUjqb6YShi8OlObwv7a+WPYXhzq0WOd74oUM9WAf4xPWH9adv9 TFkQ== X-Gm-Message-State: APzg51CPbCOlNEeUDW2DascSAnhgN2eDzLdNqz51z/GG836yx1urf5UH FrvVyCE6V1M1nMLHRup5ORdyvsLAPCg= X-Google-Smtp-Source: ANB0Vdbrau7S73PAVfBcoXctbG0N8+740d5BFj16hZECDMYR2QKC3Set0e0k5XuqAhD65V8/77N0Ew== X-Received: by 2002:a63:455d:: with SMTP id u29-v6mr12234973pgk.342.1536939029016; Fri, 14 Sep 2018 08:30:29 -0700 (PDT) Received: from xeon-e3.lan (204-195-22-127.wavecable.com. [204.195.22.127]) by smtp.gmail.com with ESMTPSA id i5-v6sm10726686pfe.140.2018.09.14.08.30.28 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 14 Sep 2018 08:30:28 -0700 (PDT) From: Stephen Hemminger To: dev@dpdk.org Cc: Stephen Hemminger , Stephen Hemminger Date: Fri, 14 Sep 2018 08:30:19 -0700 Message-Id: <20180914153019.8983-1-stephen@networkplumber.org> X-Mailer: git-send-email 2.18.0 Subject: [dpdk-dev] [PATCH] igb_uio: fix refcount if open returns error X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 14 Sep 2018 15:30:30 -0000 This fixes the problem of reference count leak if igbuio_pci_enable_interrupts fails. Also, replace mutex and integer with a kernel atomic counter. This is standard pattern for kernel devices. Fixes: 19685d5aa79c ("igb_uio: allow multi-process access") Signed-off-by: Stephen Hemminger --- kernel/linux/igb_uio/igb_uio.c | 33 +++++++++++---------------------- 1 file changed, 11 insertions(+), 22 deletions(-) diff --git a/kernel/linux/igb_uio/igb_uio.c b/kernel/linux/igb_uio/igb_uio.c index 3398eacd3fcc..fede66cf2735 100644 --- a/kernel/linux/igb_uio/igb_uio.c +++ b/kernel/linux/igb_uio/igb_uio.c @@ -26,8 +26,7 @@ struct rte_uio_pci_dev { struct uio_info info; struct pci_dev *pdev; enum rte_intr_mode mode; - struct mutex lock; - int refcnt; + atomic_t refcnt; }; static int wc_activate; @@ -320,23 +319,19 @@ igbuio_pci_open(struct uio_info *info, struct inode *inode) struct pci_dev *dev = udev->pdev; int err; - mutex_lock(&udev->lock); - if (++udev->refcnt > 1) { - mutex_unlock(&udev->lock); + if (atomic_inc_return(&udev->refcnt) != 1) return 0; - } /* set bus master, which was cleared by the reset function */ pci_set_master(dev); /* enable interrupts */ err = igbuio_pci_enable_interrupts(udev); - mutex_unlock(&udev->lock); if (err) { + atomic_dec(&udev->refcnt); dev_err(&dev->dev, "Enable interrupt fails\n"); - return err; } - return 0; + return err; } static int @@ -345,19 +340,14 @@ igbuio_pci_release(struct uio_info *info, struct inode *inode) struct rte_uio_pci_dev *udev = info->priv; struct pci_dev *dev = udev->pdev; - mutex_lock(&udev->lock); - if (--udev->refcnt > 0) { - mutex_unlock(&udev->lock); - return 0; - } - - /* disable interrupts */ - igbuio_pci_disable_interrupts(udev); + if (atomic_dec_and_test(&udev->refcnt)) { + /* disable interrupts */ + igbuio_pci_disable_interrupts(udev); - /* stop the device from further DMA */ - pci_clear_master(dev); + /* stop the device from further DMA */ + pci_clear_master(dev); + } - mutex_unlock(&udev->lock); return 0; } @@ -489,7 +479,6 @@ igbuio_pci_probe(struct pci_dev *dev, const struct pci_device_id *id) if (!udev) return -ENOMEM; - mutex_init(&udev->lock); /* * enable device: ask low-level code to enable I/O and * memory @@ -529,6 +518,7 @@ igbuio_pci_probe(struct pci_dev *dev, const struct pci_device_id *id) udev->info.release = igbuio_pci_release; udev->info.priv = udev; udev->pdev = dev; + atomic_set(&udev->refcnt, 0); err = sysfs_create_group(&dev->dev.kobj, &dev_attr_grp); if (err != 0) @@ -580,7 +570,6 @@ igbuio_pci_remove(struct pci_dev *dev) { struct rte_uio_pci_dev *udev = pci_get_drvdata(dev); - mutex_destroy(&udev->lock); sysfs_remove_group(&dev->dev.kobj, &dev_attr_grp); uio_unregister_device(&udev->info); igbuio_pci_release_iomem(&udev->info); -- 2.18.0