Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp3379137pxb; Mon, 4 Apr 2022 15:08:32 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxbGUiZsDe017wpodmZp/olmjrr7XL3wbMNhapzW+FRZAJz4doz0ADPRtrtSkLeZaHbWj7k X-Received: by 2002:ae9:e411:0:b0:67e:616f:400a with SMTP id q17-20020ae9e411000000b0067e616f400amr278474qkc.645.1649110112465; Mon, 04 Apr 2022 15:08:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649110112; cv=none; d=google.com; s=arc-20160816; b=OF8JjustLJWffr6gcwmZGd6sP2sDGHE0faatq2OaQKhcjvdD2f9E/QGCdTS5rwmi/N q9B7NCNgWJg0oQizQNE4VQkD0hglf/dBomBBELmuhlJayH430gRag/UnoqQoY4gaoKHN 2EzZbF21IoAqt8NknSGtO58TTQRfJdPzVkNUws++NbOZqYVvWnzt1pGs93gXkeB3+fIq ZFxxwuFbGMsoW33vvBdsfDDRXYoBRJjEl/rPKjSOWnS3SiwRlQ3MGLuCDdEKVG6uER2a zVCgaRyM1rPxfrUGSVwjX3b8iJFkWcTu0kkLp4xtB3birpft/wZzCR17J5uwgKojDWEJ dI+g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:subject:user-agent:mime-version:date:message-id; bh=o0Hw4JG7Hrihf/MGhWw+3c/XC0x5w9oEvBwHvt7d86o=; b=Mnw2WWIAF7xyJVzltgtxqbAnNfefyZHm6klMWXqyBi8f1tMA/x16saNrocnf3vGYVL 3AKD7X12eZB1lTdyd2OCGYKEmiQ/0VaKEud0h+NLq4PlcPg+1ULyiWeEb35bTzZIn0GZ lNRhURsbNnqqLfF+PhrOxr78PCwTJ8V8jXwqlRbggVpB0xcix1443uafo0uYoFyox6J9 K+q/XA1bytlsJRUyAw7mgcXIwYkO2pN/o6Oy5n/e38+7L0rnifFf+CsKxERJJwhlGJM5 BzoADkKaqiIpQ0u4h/NcLH6HrOJuWQrgOd2uD5bMXjXNvk7NY4NiQ/PFAvEiFm4A6rzp cE9A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id cg7-20020a05622a408700b002ebb4c6cc38si1711952qtb.498.2022.04.04.15.08.17; Mon, 04 Apr 2022 15:08:32 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244493AbiDADY6 (ORCPT + 99 others); Thu, 31 Mar 2022 23:24:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41494 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244491AbiDADY4 (ORCPT ); Thu, 31 Mar 2022 23:24:56 -0400 Received: from out30-43.freemail.mail.aliyun.com (out30-43.freemail.mail.aliyun.com [115.124.30.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B8B8425E32B for ; Thu, 31 Mar 2022 20:23:07 -0700 (PDT) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R181e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=e01e04400;MF=yaohongbo@linux.alibaba.com;NM=1;PH=DS;RN=7;SR=0;TI=SMTPD_---0V8mkkFC_1648783383; Received: from 30.225.24.82(mailfrom:yaohongbo@linux.alibaba.com fp:SMTPD_---0V8mkkFC_1648783383) by smtp.aliyun-inc.com(127.0.0.1); Fri, 01 Apr 2022 11:23:04 +0800 Message-ID: Date: Fri, 1 Apr 2022 11:23:01 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:91.0) Gecko/20100101 Thunderbird/91.6.1 Subject: Re: [PATCH RESEND] uio/uio_pci_generic: Introduce refcnt on open/release To: "Michael S. Tsirkin" Cc: gregkh@linuxfoundation.org, linux-kernel@vger.kernel.org, alikernel-developer@linux.alibaba.com, gang.cao@intel.com, shouqu.ssq@alibaba-inc.com, yangxiu.yx@alibaba-inc.com References: <1648693432-129409-1-git-send-email-yaohongbo@linux.alibaba.com> <20220331162952-mutt-send-email-mst@kernel.org> From: Yao Hongbo In-Reply-To: <20220331162952-mutt-send-email-mst@kernel.org> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-9.9 required=5.0 tests=BAYES_00, ENV_AND_HDR_SPF_MATCH,NICE_REPLY_A,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,UNPARSEABLE_RELAY,USER_IN_DEF_SPF_WL autolearn=ham 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 + cc. 在 2022/4/1 上午4:30, Michael S. Tsirkin 写道: > On Thu, Mar 31, 2022 at 10:23:52AM +0800, Yao Hongbo wrote: >> 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. > > With two programs poking at the same device, how is this ever > supposed to work even while both are alive? > >> 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 >> --- >> 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