From nobody Mon Feb 9 00:54:42 2026 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.15]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9CC59225A38; Fri, 16 Jan 2026 12:58:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.15 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768568302; cv=none; b=j25f4oGcJxxSKGnDhWcVIeRqWPbDD7fX1Hjly3JIZpGEl4c1KqcHiyXOeShrTcxsdzGknUIebopDZftSlByENBWiwOUvjCZVz7AIhUhbkEwXG4qcDunefG93ie9Rw0dHlBhgk5R5gmle9vkAzx+ZEuJgBqVKsu01tH3U8eSDEac= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768568302; c=relaxed/simple; bh=UDXrYaw3rI48vBK1SRp/7YhpPlUFuB+2Nhwv4YDnGnw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=PiO98700RJGoz6vD9EHCOX9GRBsW3zUwfLsShMC6kNC2lUNqd16HUCOLlVXg3R82ocoEzl/TGMV5arYPd+Gx5z39F1tX4/xQpFRcWbAU5P0umkyagQe3A5Q1Jn/OuInPlS5pUXd5EYpxuN5iUFWi22Y0eXFzB7hywL6XbVjvZhM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=pass smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=IMS92Xb9; arc=none smtp.client-ip=198.175.65.15 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="IMS92Xb9" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1768568298; x=1800104298; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=UDXrYaw3rI48vBK1SRp/7YhpPlUFuB+2Nhwv4YDnGnw=; b=IMS92Xb98SDgsiN/jA+/cOPudS7yLqjRKhjgHGu9A86iqQZ/WPCspu10 NcqscoWiAV5OGdfejTdyzaQzaRuNcnjofJbL8rPjMCI471rZQ1yEdKYiJ BX+F4lpfXrhXaPr5DH4ekg+ko5DqaiD5Lc9YxuMsbK0IN19CmoGjmnR9F w9TKgTKnvaQNhjm/F2Hbeaj2rdyzsaiLeSAM+huxDmaPQ19IXprGTy2Hx muboNla3+SoBzBgwvzFythmyHOMTJ1yvI73wsuyyshFdRg3XpRaquucs0 sZrb0tV4742V9VHgXhNU23Y6YyozGVHEeXfTNO4OU0960PTIDVWih6sOn w==; X-CSE-ConnectionGUID: uSZj3yjTR0iAZOZUhfJHyw== X-CSE-MsgGUID: 24BL9NFoRYm+GvDQ25TH+g== X-IronPort-AV: E=McAfee;i="6800,10657,11673"; a="73511154" X-IronPort-AV: E=Sophos;i="6.21,231,1763452800"; d="scan'208";a="73511154" Received: from orviesa003.jf.intel.com ([10.64.159.143]) by orvoesa107.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Jan 2026 04:58:18 -0800 X-CSE-ConnectionGUID: hc6jFAjiR/2FaSiib9+J2Q== X-CSE-MsgGUID: mnUbxwsYT1qEmIwvmSPa/Q== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.21,231,1763452800"; d="scan'208";a="209371180" Received: from ijarvine-mobl1.ger.corp.intel.com (HELO localhost) ([10.245.245.178]) by ORVIESA003-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Jan 2026 04:58:14 -0800 From: =?UTF-8?q?Ilpo=20J=C3=A4rvinen?= To: "Jinhui Guo" , Keith Busch , "Anthony Pighin (Nokia)" , Alex Williamson , Jonathan Cameron , Bjorn Helgaas , linux-pci@vger.kernel.org, linux-kernel@vger.kernel.org Cc: =?UTF-8?q?Ilpo=20J=C3=A4rvinen?= Subject: [PATCH 3/3] PCI: Consolidate pci_bus/slot_lock/unlock/trylock() Date: Fri, 16 Jan 2026 14:57:42 +0200 Message-Id: <20260116125742.1890-4-ilpo.jarvinen@linux.intel.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20260116125742.1890-1-ilpo.jarvinen@linux.intel.com> References: <20260116125742.1890-1-ilpo.jarvinen@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable pci_bus/slot_lock/unlock/trylock() largely duplicate the bus iteration loop with variation only due to slot filter handling. The only differences in the loops is where the struct bus is found (directly in the argument vs in slot->bus) and whether slot filter is applied. Those difference are simple to handle using function parameters. Consolidate the bus iteration loop to one place by creating __pci_bus_{lock,unlock,trylock}() and call them from the non-underscore locking functions. Signed-off-by: Ilpo J=C3=A4rvinen --- drivers/pci/pci.c | 103 ++++++++++++++++++++++------------------------ 1 file changed, 49 insertions(+), 54 deletions(-) diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c index e1333539c7b7..622920c1529f 100644 --- a/drivers/pci/pci.c +++ b/drivers/pci/pci.c @@ -5242,13 +5242,18 @@ static bool pci_bus_resettable(struct pci_bus *bus) return true; } =20 +static void pci_bus_lock(struct pci_bus *bus); +static void pci_bus_unlock(struct pci_bus *bus); +static int pci_bus_trylock(struct pci_bus *bus); + /* Lock devices from the top of the tree down */ -static void pci_bus_lock(struct pci_bus *bus) +static void __pci_bus_lock(struct pci_bus *bus, struct pci_slot *slot) { struct pci_dev *dev; =20 - pci_dev_lock(bus->self); list_for_each_entry(dev, &bus->devices, bus_list) { + if (slot && (!dev->slot || dev->slot !=3D slot)) + continue; if (dev->subordinate) pci_bus_lock(dev->subordinate); else @@ -5257,28 +5262,28 @@ static void pci_bus_lock(struct pci_bus *bus) } =20 /* Unlock devices from the bottom of the tree up */ -static void pci_bus_unlock(struct pci_bus *bus) +static void __pci_bus_unlock(struct pci_bus *bus, struct pci_slot *slot) { struct pci_dev *dev; =20 list_for_each_entry(dev, &bus->devices, bus_list) { + if (slot && (!dev->slot || dev->slot !=3D slot)) + continue; if (dev->subordinate) pci_bus_unlock(dev->subordinate); else pci_dev_unlock(dev); } - pci_dev_unlock(bus->self); } =20 /* Return 1 on successful lock, 0 on contention */ -static int pci_bus_trylock(struct pci_bus *bus) +static int __pci_bus_trylock(struct pci_bus *bus, struct pci_slot *slot) { struct pci_dev *dev; =20 - if (!pci_dev_trylock(bus->self)) - return 0; - list_for_each_entry(dev, &bus->devices, bus_list) { + if (slot && (!dev->slot || dev->slot !=3D slot)) + continue; if (dev->subordinate) { if (!pci_bus_trylock(dev->subordinate)) goto unlock; @@ -5288,12 +5293,44 @@ static int pci_bus_trylock(struct pci_bus *bus) return 1; =20 unlock: - list_for_each_entry_continue_reverse(dev, &bus->devices, bus_list) { + list_for_each_entry_continue_reverse(dev, + &slot->bus->devices, bus_list) { + if (slot && (!dev->slot || dev->slot !=3D slot)) + continue; if (dev->subordinate) pci_bus_unlock(dev->subordinate); else pci_dev_unlock(dev); } + return 0; +} + +/* Lock devices from the top of the tree down */ +static void pci_bus_lock(struct pci_bus *bus) +{ + pci_dev_lock(bus->self); + __pci_bus_lock(bus, NULL); +} + +/* Unlock devices from the bottom of the tree up */ +static void pci_bus_unlock(struct pci_bus *bus) +{ + __pci_bus_unlock(bus, NULL); + pci_dev_unlock(bus->self); +} + +/* Return 1 on successful lock, 0 on contention */ +static int pci_bus_trylock(struct pci_bus *bus) +{ + if (!pci_dev_trylock(bus->self)) + return 0; + + if (!__pci_bus_trylock(bus, NULL)) + goto unlock; + + return 1; + +unlock: pci_dev_unlock(bus->self); return 0; } @@ -5321,61 +5358,19 @@ static bool pci_slot_resettable(struct pci_slot *sl= ot) /* Lock devices from the top of the tree down */ static void pci_slot_lock(struct pci_slot *slot) { - struct pci_dev *dev; - - list_for_each_entry(dev, &slot->bus->devices, bus_list) { - if (!dev->slot || dev->slot !=3D slot) - continue; - if (dev->subordinate) - pci_bus_lock(dev->subordinate); - else - pci_dev_lock(dev); - } + __pci_bus_lock(slot->bus, slot); } =20 /* Unlock devices from the bottom of the tree up */ static void pci_slot_unlock(struct pci_slot *slot) { - struct pci_dev *dev; - - list_for_each_entry(dev, &slot->bus->devices, bus_list) { - if (!dev->slot || dev->slot !=3D slot) - continue; - if (dev->subordinate) - pci_bus_unlock(dev->subordinate); - else - pci_dev_unlock(dev); - } + __pci_bus_unlock(slot->bus, slot); } =20 /* Return 1 on successful lock, 0 on contention */ static int pci_slot_trylock(struct pci_slot *slot) { - struct pci_dev *dev; - - list_for_each_entry(dev, &slot->bus->devices, bus_list) { - if (!dev->slot || dev->slot !=3D slot) - continue; - if (dev->subordinate) { - if (!pci_bus_trylock(dev->subordinate)) { - goto unlock; - } - } else if (!pci_dev_trylock(dev)) - goto unlock; - } - return 1; - -unlock: - list_for_each_entry_continue_reverse(dev, - &slot->bus->devices, bus_list) { - if (!dev->slot || dev->slot !=3D slot) - continue; - if (dev->subordinate) - pci_bus_unlock(dev->subordinate); - else - pci_dev_unlock(dev); - } - return 0; + return __pci_bus_trylock(slot->bus, slot); } =20 /* --=20 2.39.5