Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp1664812imm; Thu, 27 Sep 2018 00:03:54 -0700 (PDT) X-Google-Smtp-Source: ACcGV60SOEEh4HhZmq0LRAlOjKYsqWnxAiRKmVmY2ZCb/7EEa7clF/X0N40FYWJCvysnlWGY+xJx X-Received: by 2002:a63:1c1b:: with SMTP id c27-v6mr8570502pgc.351.1538031834774; Thu, 27 Sep 2018 00:03:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1538031834; cv=none; d=google.com; s=arc-20160816; b=uK5/y5AGd5fcGd5BKK6kTuK0YQk3BzcXMxXNCRvt9i6Tj+tWD8ISHlinkfsKP+HRs0 U84ZUUzSk5Pz/XL/7N4jG3KFmZAkQtlNaPZefNs1oej0dsQb36PbgpH+YPoy+XB72vlG PUqjTSt4y+7N3NK/CtDPToeaZomqjH3w23dZ0rkwM94R23vKn1lQOJMTNZEaZ7Jj3Yf0 bVdvoXcdTObhMZVYVgFWb857YVWLkfiB2mrNa2l130AyGV50i5c+tLtyvT7Z7sWD8gYG SGfhuVlZ5A1oQ1CcfPzDhP6KNqFPhvin1eKUplpqbNm46bib9B1KHKPGQYHWUsQreJUO utjw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=JvmDydbaMBH/V3NZLm+3fqKq/TRXR2sk5jesgi6AD1E=; b=k796Njlb/cf3+nvvAynkKXKeMyY0B0DaMqSL4MT+JSMAGUW5Edc84ERyVoDmz/Hsqr 5q1khsbFHAENtVbDwjzDXKHxoq7MVWAu+RkcMCZLKLSStgVSMM8pbHyWFMjKi15UBPhS 9/JkOO35c+XKejInrGDoeBASuiIcsZJRVtez1VRjCvTtv44au18AsQLn6Ny8/VAGW7Hv dmlctuVZ2P/PZhzoCvWzjaaA5fcavkvPUfUoHsHQkXA4I0P0oi0huDHzUFboejMFz91F kBIFnoU+xtlgG+UqbDM89eBacGN7ZMiJylVpSs2fZ3tK+K724KpaXMeBECCvMpxn5Nox DxYQ== 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 l14-v6si1210024pgi.34.2018.09.27.00.03.38; Thu, 27 Sep 2018 00:03:54 -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 S1727131AbeI0NUQ (ORCPT + 99 others); Thu, 27 Sep 2018 09:20:16 -0400 Received: from bmailout2.hostsharing.net ([83.223.90.240]:35633 "EHLO bmailout2.hostsharing.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726962AbeI0NUQ (ORCPT ); Thu, 27 Sep 2018 09:20:16 -0400 Received: from h08.hostsharing.net (h08.hostsharing.net [83.223.95.28]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "*.hostsharing.net", Issuer "COMODO RSA Domain Validation Secure Server CA" (not verified)) by bmailout2.hostsharing.net (Postfix) with ESMTPS id EC5262800A26C; Thu, 27 Sep 2018 09:03:27 +0200 (CEST) Received: by h08.hostsharing.net (Postfix, from userid 100393) id 810E2CC5B1; Thu, 27 Sep 2018 09:03:27 +0200 (CEST) Date: Thu, 27 Sep 2018 09:03:27 +0200 From: Lukas Wunner To: Bjorn Helgaas Cc: Suganath Prabu S , linux-scsi@vger.kernel.org, linux-pci@vger.kernel.org, andy.shevchenko@gmail.com, Sathya.Prakash@broadcom.com, sreekanth.reddy@broadcom.com, linux-kernel@vger.kernel.org Subject: Re: [PATCH v4 1/6] mpt3sas: Introduce mpt3sas_base_pci_device_is_available Message-ID: <20180927070327.wvl5glb4c7gtjoa3@wunner.de> References: <1537935759-14754-1-git-send-email-suganath-prabu.subramani@broadcom.com> <1537935759-14754-2-git-send-email-suganath-prabu.subramani@broadcom.com> <20180926213241.GI28024@bhelgaas-glaptop.roam.corp.google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20180926213241.GI28024@bhelgaas-glaptop.roam.corp.google.com> User-Agent: NeoMutt/20170113 (1.7.2) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Sep 26, 2018 at 04:32:41PM -0500, Bjorn Helgaas wrote: > On Wed, Sep 26, 2018 at 09:52:34AM +0530, Suganath Prabu S wrote: > > @@ -6853,6 +6872,13 @@ mpt3sas_wait_for_commands_to_complete(struct MPT3SAS_ADAPTER *ioc) > > > > ioc->pending_io_count = 0; > > > > + if (!mpt3sas_base_pci_device_is_available(ioc)) { > > + pr_err(MPT3SAS_FMT > > + "%s: pci error recovery reset or pci device unplug occurred\n", > > + ioc->name, __func__); > > + return; > > + } > > + > > ioc_state = mpt3sas_base_get_iocstate(ioc, 0); > > This is a good example of why I don't like pci_device_is_present(): it > is fundamentally racy and gives a false sense of security. Here we > *think* we're making the code safer, but in fact we could have this > sequence: > > mpt3sas_base_pci_device_is_available() # returns true > # device is removed > ioc_state = mpt3sas_base_get_iocstate() > > In this case the readl() inside mpt3sas_base_get_iocstate() will > probably return 0xffffffff data, and we assume that's valid and > continue on our merry way, pretending that "ioc_state" makes sense > when it really doesn't. The function does the following: ioc_state = mpt3sas_base_get_iocstate(ioc, 0); if ((ioc_state & MPI2_IOC_STATE_MASK) != MPI2_IOC_STATE_OPERATIONAL) return; where MPI2_IOC_STATE_MASK is 0xF0000000 and MPI2_IOC_STATE_OPERATIONAL is 0x20000000. If the device is removed after the call to mpt3sas_base_pci_device_is_available(), the result of the bitwise "and" operation would be 0xF0000000, which is unequal to 0x20000000. Hence this looks safe. I agree that pci_device_is_present() (and the pci_dev_is_disconnected() it calls) must be used judiciously, but here it seems to have been done correctly. One thing to be aware of is that a return value of "true" from pci_dev_is_disconnected() is definitive and can be trusted. On the other hand a return value of "false" is more like a fuzzy "likely not disconnected, but can't give any guarantees". So the boolean return value is kind of the problem here. Boolean logic doesn't really fit these "definitive if true, not definitive if false" semantics. However being able to get the definitive answer in the disconnected case is valuable: pciehp is the only entity that can determine surprise removal authoritatively and unambiguously (albeit with a latency). All the other tools that we have at our disposal don't have that quality: E.g. checking the Vendor ID is ambiguous because it returns a valid value if a device was quickly replaced with another one. Also, all ones may be returned in the case of an Uncorrectable Error, but the device may revert to valid responses if the error can be recovered. (Please correct me if I'm wrong.) Thanks, Lukas