Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp368759yba; Fri, 26 Apr 2019 01:14:45 -0700 (PDT) X-Google-Smtp-Source: APXvYqz5blvFzkqELabp3Zn96WGTqH/sWa2TlqbpIKHt0TwiUlvt8+KzyZ5UPQ+lLbGv+lDV+Upj X-Received: by 2002:a62:a515:: with SMTP id v21mr46272562pfm.41.1556266484944; Fri, 26 Apr 2019 01:14:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1556266484; cv=none; d=google.com; s=arc-20160816; b=IU3MH0I+H+P7+XjjLLgTYA42DxaGmLPE0bZN+RqD0JPp5CX0eypKbUw0TWAjVWnBi3 gRp2D71Y8xhONSos01byNwzcTP8wu2Z6XkZHMChxFgUfxceQbnJuTFAdMyw/HyIAjn6y dxz3aQIUdE231wp/R3+96vxcykneHUahtc8DObCEyHzhd3DPvCPg/g450r1BUzRJsfX1 xjjWuF70CAxo5r7SFCBwfwnaMm4Hix032pkQSqAyHsi33SobUv9aLSTcyiCJrdrl/WjZ lnggEfuSgE9+h2PJrN3U0WEDJ1nwNocqCd8XcK1xj4ejdX++MqADnWxgZwjwCbIzZCQh TnmQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-language :content-transfer-encoding:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject; bh=bdNJc48Cnx+63PDCz+wnpVU1h1f1W9YHqWRJkzwn57g=; b=ddCqL6lSLhUwj10JT1c3+2NYk0Fj3aTDY+3GZSDBc6Yk9vOGnYh9DznsbpVBHQDhpQ QjWuN10uHqzBid8Q5JoL2IYB5vmel9d5wQVU28pQ0x0SOH5flKbd9Xn945be2GhgTXlD bKyebrfFG3S8IWFKWDBDWeTSwhn7Aw3TP01VHjBSi2zfSJt1rVzSDWIYDvSmmREc6wR3 rtJsFl4xbWCdCbbKg/5XsBgStBAXHklv3pA/wVHBw+PrY6b9aLJ9T83Y70FLucGwomor 42rTnpSRjT4U/2s9cWOzI9vsdLwzce+vgAKvhcbGjbv1HJdcodRHsN1LZJuZvbKPbSA3 HTOw== ARC-Authentication-Results: i=1; mx.google.com; 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 d1si12898756pgv.242.2019.04.26.01.14.29; Fri, 26 Apr 2019 01:14:44 -0700 (PDT) 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; 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 S1726040AbfDZIMU (ORCPT + 99 others); Fri, 26 Apr 2019 04:12:20 -0400 Received: from p-mail-ext.rd.orange.com ([161.106.1.9]:48304 "EHLO p-mail-ext.rd.orange.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725854AbfDZIMT (ORCPT ); Fri, 26 Apr 2019 04:12:19 -0400 Received: from p-mail-ext.rd.orange.com (localhost [127.0.0.1]) by localhost (Postfix) with SMTP id E5CD45616B8; Fri, 26 Apr 2019 10:12:01 +0200 (CEST) Received: from p-mail-int.rd.francetelecom.fr (p-mail-int.rd.francetelecom.fr [10.192.117.12]) by p-mail-ext.rd.orange.com (Postfix) with ESMTP id DA50F5616A9; Fri, 26 Apr 2019 10:11:53 +0200 (CEST) Received: from p-mail-int.rd.francetelecom.fr (localhost.localdomain [127.0.0.1]) by localhost (Postfix) with SMTP id E230018169F; Fri, 26 Apr 2019 10:11:54 +0200 (CEST) Received: from [10.193.71.88] (yd-CZC9059FTQ.rd.francetelecom.fr [10.193.71.88]) by p-mail-int.rd.francetelecom.fr (Postfix) with ESMTP id 8EC9718152B; Fri, 26 Apr 2019 10:11:54 +0200 (CEST) Subject: Re: [PATCH] PCI/IOV: update num_VFs earlier To: Bjorn Helgaas Cc: linux-pci@vger.kernel.org, linux-kernel@vger.kernel.org References: <20190329080058.21736-1-pierre.cregut@orange.com> <20190405223300.GD159318@google.com> From: CREGUT Pierre IMT/OLN Message-ID: <744273fd-8045-7527-ad29-fa19adf6d015@orange.com> Date: Fri, 26 Apr 2019 10:11:54 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.6.1 MIME-Version: 1.0 In-Reply-To: <20190405223300.GD159318@google.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US X-PMX-Version: 6.4.6.2792898, Antispam-Engine: 2.7.2.2107409, Antispam-Data: 2019.4.26.80616, AntiVirus-Engine: 5.60.0, AntiVirus-Data: 2019.4.26.5600000 X-PMX-Spam: Gauge=IIIIIIII, Probability=8%, Report=' MULTIPLE_RCPTS 0.1, HTML_00_01 0.05, HTML_00_10 0.05, BODY_SIZE_4000_4999 0, BODY_SIZE_5000_LESS 0, BODY_SIZE_7000_LESS 0, FROM_NAME_PHRASE 0, IN_REP_TO 0, LEGITIMATE_SIGNS 0, MSG_THREAD 0, MULTIPLE_REAL_RCPTS 0, REFERENCES 0, SINGLE_URI_IN_BODY 0, URI_WITH_PATH_ONLY 0, __ANY_URI 0, __BOUNCE_CHALLENGE_SUBJ 0, __BOUNCE_NDR_SUBJ_EXEMPT 0, __CP_URI_IN_BODY 0, __CT 0, __CTE 0, __CT_TEXT_PLAIN 0, __DQ_NEG_HEUR 0, __DQ_NEG_IP 0, __FORWARDED_MSG 0, __FRAUD_MONEY_CURRENCY 0, __FRAUD_MONEY_CURRENCY_DOLLAR 0, __HAS_CC_HDR 0, __HAS_FROM 0, __HAS_MSGID 0, __HAS_REFERENCES 0, __HTTPS_URI 0, __IN_REP_TO 0, __MIME_TEXT_ONLY 0, __MIME_TEXT_P 0, __MIME_TEXT_P1 0, __MIME_VERSION 0, __MOZILLA_USER_AGENT 0, __MULTIPLE_RCPTS_CC_X2 0, __NO_HTML_TAG_RAW 0, __PHISH_SPEAR_SUBJ_PREDICATE 0, __REFERENCES 0, __SANE_MSGID 0, __SINGLE_URI_TEXT 0, __SUBJ_ALPHA_END 0, __SUBJ_ALPHA_NEGATE 0, __SUBJ_REPLY 0, __TO_MALFORMED_2 0, __TO_NAME 0, __TO_NAME_DIFF_FROM_ACC 0, __TO_REAL_NAMES 0, __URI_IN_BODY 0, __URI_NOT_IMG 0, __URI_NO_WWW 0, __URI_NS , __URI_WITH_PATH 0, __USER_AGENT 0' Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org I also initially thought that kobject_uevent generated the netlink event but this is not the case. This is generated by the specific driver in use. For the Intel i40e driver, this is the call to i40e_do_reset_safe in i40e_pci_sriov_configure that sends the event. It is followed by i40e_pci_sriov_enable that calls i40e_alloc_vfs that finally calls the generic pci_enable_sriov function. So the proposed patch works well for the i40e driver (x710 cards) because the update to num_VFs is fast enough to be committed before the event is received. It may not work with other cards. The same is true for the zero value and there is no guarantee for other cards. The clean solution would be to lock the device in sriov_numvfs_show. I guess that there are good reasons why locks have been avoided in sysfs getter functions so let us explore other approaches. We can either return a "not settled" value (-1) or (probably better) do not return a value but an error (-EAGAIN returned by the show function). To distinguish this "not settled" situation we can either: * overload the meaning of num_VFs (eg make it negative) but it is an unsigned short. * add a bool to pci_sriov struct (rather simple but modifies a well established structure). * use the fact that not_settled => device is locked and use mutex_is_locked as an over approximation. The later is not perfect but requires minimal changes to sriov_numvfs_show:  if (mutex_is_locked(&dev->mutex)) return -EAGAIN; In all cases, the device could be locked or the boolean set just after the test. But I don't think there is a case where causality would be violated.Thank you in advance for your recommendations. I will update the patch according to your instructions. Le 06/04/2019 à 00:33, Bjorn Helgaas a écrit : > On Fri, Mar 29, 2019 at 09:00:58AM +0100, Pierre Crégut wrote: >> Ensure that iov->num_VFs is set before a netlink message is sent >> when the number of VFs is changed. Only the path for num_VFs > 0 >> is affected. The path for num_VFs = 0 is already correct. >> >> Monitoring programs can relie on netlink messages to track interface >> change and query their state in /sys. But when sriov_numvfs is set to a >> positive value, the netlink message is sent before the value is available >> in sysfs. The value read after the message is received is always zero. > Thanks, Pierre! Can you clue me in on where exactly the connection > from sriov_enable() to netlink is? > > I see one side of the race is with sriov_numvfs_show(), but I don't > know where the netlink message is sent. Is that connected with the > kobject_uevent(KOBJ_CHANGE)? > > One thing this would help with is figuring out exactly how *much* > earlier we need to set iov->num_VFs. It looks like the current patch > sets it before we actually enable the VFs, so a user could read > /sys/.../sriov_numvfs and get the wrong value. Of course, that's > unavoidable; the question is whether it's OK to get the new value > *before* it actually takes effect, or whether we want to return a > stale value until after it takes effect. > >> Link: https://bugzilla.kernel.org/show_bug.cgi?id=202991 >> Signed-off-by: Pierre Crégut >> --- >> note: the behaviour can be tested with the following shell script also >> available on the bugzilla (d being the phy device name): >> >> ip monitor dev $d | grep --line-buffered "^[0-9]*:" | \ >> while read line; do cat /sys/class/net/$d/device/sriov_numvfs; done >> >> drivers/pci/iov.c | 3 ++- >> 1 file changed, 2 insertions(+), 1 deletion(-) >> >> diff --git a/drivers/pci/iov.c b/drivers/pci/iov.c >> index 3aa115ed3a65..a9655c10e87f 100644 >> --- a/drivers/pci/iov.c >> +++ b/drivers/pci/iov.c >> @@ -351,6 +351,7 @@ static int sriov_enable(struct pci_dev *dev, int nr_virtfn) >> goto err_pcibios; >> } >> >> + iov->num_VFs = nr_virtfn; >> pci_iov_set_numvfs(dev, nr_virtfn); >> iov->ctrl |= PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE; >> pci_cfg_access_lock(dev); >> @@ -363,7 +364,6 @@ static int sriov_enable(struct pci_dev *dev, int nr_virtfn) >> goto err_pcibios; >> >> kobject_uevent(&dev->dev.kobj, KOBJ_CHANGE); >> - iov->num_VFs = nr_virtfn; >> >> return 0; >> >> @@ -379,6 +379,7 @@ static int sriov_enable(struct pci_dev *dev, int nr_virtfn) >> if (iov->link != dev->devfn) >> sysfs_remove_link(&dev->dev.kobj, "dep_link"); >> >> + iov->num_VFs = 0; >> pci_iov_set_numvfs(dev, 0); >> return rc; >> } >> -- >> 2.17.1 >>