Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp501824imm; Fri, 17 Aug 2018 01:32:07 -0700 (PDT) X-Google-Smtp-Source: AA+uWPweE0p58GeVHU1AXKFgoLUCjsdyyNoNoUP4khz5fdYBArJN46L5ljiQ/FB8ZlL1UlaaQkJr X-Received: by 2002:a17:902:7c89:: with SMTP id y9-v6mr32627076pll.187.1534494727284; Fri, 17 Aug 2018 01:32:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1534494727; cv=none; d=google.com; s=arc-20160816; b=sO8V0cgxsRLPbP4pKfJjl8gQ0a1wS7kWn870q6jYSdiFMfHooo+LBWoJHDnhVArIMm J+fv3YpGTnVGIHtreOEbqhBzlbhkt257lOMU6jENlSstuE4J2hchA6bWUIR329MCOqk5 eqDJaMHiCtGkppcJ7UNgWGEbCvkn8ne5yFt6JApqm/U4tsm63s46O54JSZ1uWr4k1n0u d9l7rFFVQhtqW1nFvw8YkXITjqZsWzUKQRpgXjl1zGrrwKpxz/rZNTjFZ12ae/xfeCTr qxaKsl5lM2bm2mJ1MgFNA59gAsv9D26FPIy5bHAVy0vyPnjyWoTcwBQgMZu6hAMTy7lW OR9Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:date:cc:to:from:subject:message-id :arc-authentication-results; bh=vyW8+MQSkuOD+s2fmc0nty4+wQpCAIUVn24VOvTgQu8=; b=SJFUQAevKv21WYbuXX5ofKXfT4JuG4r/5Tb4APzVpuxcCl3W9Jzxqj0Ou9rZ9SKPma KX/+Euo6QaMtV7MAF3Ju51zFdBKU5ru5CgBcn6kIgA12XPnw7zAEv4aj4rZnQqqHDtpq zQdXUX2olG+CicLsvFbmDfmPSiYfstcPVXG+NYR4rPui7c7HhPKe0nJM/j9RGHAYEBMi P426WH3cI97nGAo3hkIKdo+dGr3ll/YzvTin9adJP12y2vZtrGpTJtyeCO0g6cNPYVhT y2bq/NyaPwJtF/xm+ivg8YxaFZWrJDS3U3j3DRDpm3ZGYXyQw+RbrFFqPee2TUgH1q3u JvMw== 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 b13-v6si1652831plz.467.2018.08.17.01.31.51; Fri, 17 Aug 2018 01:32:07 -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 S1726755AbeHQLdQ (ORCPT + 99 others); Fri, 17 Aug 2018 07:33:16 -0400 Received: from gate.crashing.org ([63.228.1.57]:52184 "EHLO gate.crashing.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726546AbeHQLdQ (ORCPT ); Fri, 17 Aug 2018 07:33:16 -0400 Received: from localhost (localhost.localdomain [127.0.0.1]) by gate.crashing.org (8.14.1/8.14.1) with ESMTP id w7H8UGLq028341; Fri, 17 Aug 2018 03:30:18 -0500 Message-ID: <1ffafe01887e3b760f84488b06339aba64f586e5.camel@kernel.crashing.org> Subject: Re: [RFC PATCH 5/6] pci: Protect the enable/disable state of pci_dev using the state mutex From: Benjamin Herrenschmidt To: Marta Rybczynska Cc: Bjorn Helgaas , linux-pci@vger.kernel.org, Hari Vyas , Ray Jui , Srinath Mannam , Guenter Roeck , Jens Axboe , Lukas Wunner , Konstantin Khlebnikov , Pierre-Yves Kerbrat , linux-kernel@vger.kernel.org Date: Fri, 17 Aug 2018 18:30:16 +1000 In-Reply-To: <66577784.13494160.1534493390262.JavaMail.zimbra@kalray.eu> References: <20180817044902.31420-1-benh@kernel.crashing.org> <20180817044902.31420-6-benh@kernel.crashing.org> <66577784.13494160.1534493390262.JavaMail.zimbra@kalray.eu> Content-Type: text/plain; charset="UTF-8" X-Mailer: Evolution 3.28.5 (3.28.5-1.fc28) Mime-Version: 1.0 Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, 2018-08-17 at 10:09 +0200, Marta Rybczynska wrote: > > ----- On 17 Aug, 2018, at 06:49, Benjamin Herrenschmidt benh@kernel.crashing.org wrote: > > > This protects enable/disable operations using the state mutex to > > avoid races with, for example, concurrent enables on a bridge. > > > > The bus hierarchy is walked first before taking the lock to > > avoid lock nesting (though it would be ok if we ensured that > > we always nest them bottom-up, it is better to just avoid the > > issue alltogether, especially as we might find cases where > > we want to take it top-down later). > > > > Signed-off-by: Benjamin Herrenschmidt > > > > > > static void pci_enable_bridge(struct pci_dev *dev) > > { > > struct pci_dev *bridge; > > - int retval; > > + int retval, enabled; > > > > bridge = pci_upstream_bridge(dev); > > if (bridge) > > pci_enable_bridge(bridge); > > > > - if (pci_is_enabled(dev)) { > > - if (!dev->is_busmaster) > > - pci_set_master(dev); > > + /* Already enabled ? */ > > + pci_dev_state_lock(dev); > > + enabled = pci_is_enabled(dev); > > + if (enabled && !dev->is_busmaster) > > + pci_set_master(dev); > > + pci_dev_state_unlock(dev); > > + if (enabled) > > return; > > - } > > > > This looks complicated too me especially with the double locking. What do you > think about a function doing that check that used an unlocked version of > pcie_set_master? > > Like: > > dev_state_lock(dev); > enabled = pci_is_enabled(dev); > if (enabled && !dev->is_busmaster) > pci_set_master_unlocked(); > pci_dev_state_unlock(dev); > > BTW If I remember correctly the code today can set bus mastering multiple > times without checking if already done. I don't mind but I tend to dislike all those _unlocked() suffixes, I suppose my generation is typing adverse enough that we call these __something instead :) As for setting multiple times, yes pci_set_master() doesn't check but look at the "-" hunks of my patch, I'm not changing the existing test here. Not that it matters much, it's an optimization. In fact my original version just completely removed the whole lot and just unconditionally did pci_enable_device() + pci_set_master(), just ignoring the existing state :-) But I decided to keep the patch functionally equivalent so I added it back. Cheers, Ben.