Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp30459pxb; Wed, 30 Mar 2022 22:03:33 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzXn5k+ynuZxysql49xtdeK8/C2pwhV0qDRoY96KMS407Nmrk6v55eyTyPuNmEZWSYysJCl X-Received: by 2002:a17:903:2446:b0:154:3bb0:7ba0 with SMTP id l6-20020a170903244600b001543bb07ba0mr3579357pls.38.1648703012997; Wed, 30 Mar 2022 22:03:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1648703012; cv=none; d=google.com; s=arc-20160816; b=ILputZDyeOuj7jX+eljHzTZyKIk3QmVEgL8ukF3RVRYWx/dzCpgZCTi6slbWyS4vVs WNOdp5AgPtaFtXFw4vFLT9gKVH4Jw5vPTSO+gA5jdj3bIsx/4BLoO/Mvs7GZZz9xhlaK /DYPi45SiUDHQEqb0Ndk3mfwQHDCnoviC7BgPHRMXsYkqqXVWHpJKcgZXc2JrctcyJtA g3EBOB4yDp3N86Zpebmj+4tKfq145VUIvFDkkYV1+5G4EFtdAVnrKGxvl9MhA+Vq1k0t WwZL8M0e9tkgCGSDucM968kUG/yoO3P4/qodU+wag/yHIWWzI2TKu81Z5CC2WwwB19kN Mvng== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=jVK+rPz1l8INE77n/a/sr5WmVf8asOT/OJ7Xo4hsUL4=; b=yvTUK3I5F6EutKw8aQ0tDQlMEg7idWlwEL13v3u9O94z4JPFcJLCHHzo6W7Cfdob3x 0Aq3cCTLfE/xcoHhsPYQfzvDaQizWhrOE3eYrFL4qkszX7XAdAe0bAsnAltIqnMOHjWF F5te/INqRJdEVs+nIXKwaKeRW3/JGHnPoMy2ICVl4tZyu9UtFtjui4RrKHZyWT0IgV69 m4w8BxlKGwGAl6UFNG2PNH8SCOptHdo5/+twccCYotfmM5k1NYir2yO9P1oQUUu00HL6 FbfhTiC+8bnCyefD4nM4wnahOkZANY+WgNRPdFAC0N3UQL6GDNFDG5jlCvNPJwmGVSBJ T9Yg== ARC-Authentication-Results: i=1; mx.google.com; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id z24-20020a63e558000000b003816043ef13si22362836pgj.264.2022.03.30.22.03.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 30 Mar 2022 22:03:32 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 1E345265E85; Wed, 30 Mar 2022 20:38:58 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343905AbiCaCIZ (ORCPT + 99 others); Wed, 30 Mar 2022 22:08:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43326 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1352466AbiCaCIX (ORCPT ); Wed, 30 Mar 2022 22:08:23 -0400 Received: from out30-45.freemail.mail.aliyun.com (out30-45.freemail.mail.aliyun.com [115.124.30.45]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E3344652FB for ; Wed, 30 Mar 2022 19:06:34 -0700 (PDT) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R651e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=e01e04426;MF=yaohongbo@linux.alibaba.com;NM=1;PH=DS;RN=5;SR=0;TI=SMTPD_---0V8gVAU6_1648692384; Received: from localhost(mailfrom:yaohongbo@linux.alibaba.com fp:SMTPD_---0V8gVAU6_1648692384) by smtp.aliyun-inc.com(127.0.0.1); Thu, 31 Mar 2022 10:06:32 +0800 From: Yao Hongbo To: mst@redhat.com, gregkh@linuxfoundation.org Cc: yaohongbo@linux.alibaba.com, linux-kernel@vger.kernel.org, alikernel-developer@linux.alibaba.com Subject: [PATCH] uio/uio_pci_generic: Introduce refcnt on open/release Date: Thu, 31 Mar 2022 10:06:24 +0800 Message-Id: <1648692384-109967-1-git-send-email-yaohongbo@linux.alibaba.com> X-Mailer: git-send-email 1.8.3.1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RDNS_NONE, SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE,UNPARSEABLE_RELAY autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org If two userspace programs both open the PCI UIO fd, when one of the program exits uncleanly, the other will cause IO hang due to bus-mastering disabled. It's a common usage for spdk/dpdk to use UIO. So, introduce refcnt to avoid such problems. Fixes: 865a11f("uio/uio_pci_generic: Disable bus-mastering on release") Reported-by: Xiu Yang Signed-off-by: Yao Hongbo Reviewed-by: Baolin Wang --- drivers/uio/uio_pci_generic.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/drivers/uio/uio_pci_generic.c b/drivers/uio/uio_pci_generic.c index e03f9b5..8add2cf 100644 --- a/drivers/uio/uio_pci_generic.c +++ b/drivers/uio/uio_pci_generic.c @@ -31,6 +31,7 @@ struct uio_pci_generic_dev { struct uio_info info; struct pci_dev *pdev; + atomic_t refcnt; }; static inline struct uio_pci_generic_dev * @@ -39,6 +40,14 @@ struct uio_pci_generic_dev { return container_of(info, struct uio_pci_generic_dev, info); } +static int open(struct uio_info *info, struct inode *inode) +{ + struct uio_pci_generic_dev *gdev = to_uio_pci_generic_dev(info); + + atomic_inc(&gdev->refcnt); + return 0; +} + static int release(struct uio_info *info, struct inode *inode) { struct uio_pci_generic_dev *gdev = to_uio_pci_generic_dev(info); @@ -51,7 +60,9 @@ static int release(struct uio_info *info, struct inode *inode)  * Note that there's a non-zero chance doing this will wedge the device  * at least until reset. */ - pci_clear_master(gdev->pdev); + if (atomic_dec_and_test(&gdev->refcnt)) + pci_clear_master(gdev->pdev); + return 0; } @@ -92,8 +103,11 @@ static int probe(struct pci_dev *pdev, gdev->info.name = "uio_pci_generic"; gdev->info.version = DRIVER_VERSION; + gdev->info.open = open; gdev->info.release = release; gdev->pdev = pdev; + atomic_set(&gdev->refcnt, 0); + if (pdev->irq && (pdev->irq != IRQ_NOTCONNECTED)) { gdev->info.irq = pdev->irq; gdev->info.irq_flags = IRQF_SHARED; -- 1.8.3.1