From patchwork Fri May 18 16:56:24 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 10411481 X-Patchwork-Delegate: bhelgaas@google.com Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 636A0602C2 for ; Fri, 18 May 2018 16:56:43 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 53876289BA for ; Fri, 18 May 2018 16:56:43 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4831028A00; Fri, 18 May 2018 16:56:43 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C7308289BA for ; Fri, 18 May 2018 16:56:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752462AbeERQ4l (ORCPT ); Fri, 18 May 2018 12:56:41 -0400 Received: from bombadil.infradead.org ([198.137.202.133]:51362 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752459AbeERQ4g (ORCPT ); Fri, 18 May 2018 12:56:36 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20170209; h=Message-Id:Date:Subject:Cc:To:From: Sender:Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=6I51zFIEKQOyvzT7m98JkP7rTVC2ef6pb/RKaoi5I/0=; b=hU17uZnNBlrblDOgf/t/lKjLp P7KRQZtCS918e/cUnQ6D1tpRsefGvosb1icbqwdSwRX5RnsDA6mahcdXV3IVLz07oiQlDl9sHGeKj Mv/KnM6+S4p+stsYAaVPXnUaL5n0xjSfyv4SHBm/OWWPbfdcklHhiscnC2JpLrH/fadltmg68r4dz YT9ovqqG3/WdExSv/1B/LAaYxI5LHJy2gha9WLC4Jnb5wkNbbhNXcOCJC6lYs3aYeeMQKkcW90q6L 4Y0rgalUocFB7f/gr/TC0c/wrMtcKn7qW5PNPHQRVfEYXgeGKCyC1402fwzMOA84IHQRdSVpb+tOL tCiT5/p+w==; Received: from 80-109-164-210.cable.dynamic.surfer.at ([80.109.164.210] helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.90_1 #2 (Red Hat Linux)) id 1fJig7-0006iI-Cz; Fri, 18 May 2018 16:56:27 +0000 From: Christoph Hellwig To: bhelgaas@google.com Cc: arjan@linux.intel.com, keith.busch@linux.intel.com, jthumshirn@suse.de, ming.lei@redhat.com, linux-pci@vger.kernel.org, linux-nvme@lists.infradead.org Subject: [PATCH] PCI/sysfs: do not allow enabling/disabling a device with a driver Date: Fri, 18 May 2018 18:56:24 +0200 Message-Id: <20180518165624.13993-1-hch@lst.de> X-Mailer: git-send-email 2.17.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html Sender: linux-pci-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pci@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Manpulating the enable_cnt behind the back of the driver will wreck complete havok with the kernel state, so disallow it. Signed-off-by: Christoph Hellwig Acked-by: Keith Busch Reviewed-by: Johannes Thumshirn --- drivers/pci/pci-sysfs.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c index 366d93af051d..788a200fb2dc 100644 --- a/drivers/pci/pci-sysfs.c +++ b/drivers/pci/pci-sysfs.c @@ -288,13 +288,16 @@ static ssize_t enable_store(struct device *dev, struct device_attribute *attr, if (!capable(CAP_SYS_ADMIN)) return -EPERM; - if (!val) { - if (pci_is_enabled(pdev)) - pci_disable_device(pdev); - else - result = -EIO; - } else + device_lock(dev); + if (dev->driver) + result = -EBUSY; + else if (val) result = pci_enable_device(pdev); + else if (pci_is_enabled(pdev)) + pci_disable_device(pdev); + else + result = -EIO; + device_unlock(dev); return result < 0 ? result : count; }