Received: by 2002:a05:7412:b995:b0:f9:9502:5bb8 with SMTP id it21csp6142061rdb; Mon, 1 Jan 2024 10:13:06 -0800 (PST) X-Google-Smtp-Source: AGHT+IHDMNt08Uq8Tn+mwCJAx3bpk55YrBUd03FTfY7aTULrGNVt5HVSE91tTEUKzW/dT0RN6dC3 X-Received: by 2002:a17:903:32ca:b0:1d4:be59:f8cf with SMTP id i10-20020a17090332ca00b001d4be59f8cfmr1413147plr.21.1704132786548; Mon, 01 Jan 2024 10:13:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704132786; cv=none; d=google.com; s=arc-20160816; b=rwmOEzC0XZyeoZ+u1Ao+ObA+T7bkYYsEZEEYZEciIgm5hHVZULoYo1imtGXlUqVlpI azmWtiG34gJDupn9eRVv/HR+ku/D0wcCS4ooa8yV8nbyxxkKkfTLaEW0hff/f/rScpQx QFxQRFH4omMYqQ+9TsMJdEJoBHv6H1whHMTXORkY7u0Mn6x3GdpUUuNkt2+1PosmeNG0 cffTJQb9FqQL40EM6+DhvdoSn4dEMc9zTXLmO284inzY4VOvHv4jSVkrxLWmLP2C9j2p e2uWGnGgXfiORtiL81L5QgM33kysA2fhEBAUISsYpOaqhIzgmnf0eqCQto2N7qXw5OIM YBlA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=mime-version:list-unsubscribe:list-subscribe:list-id:precedence :references:message-id:in-reply-to:subject:cc:to:from:date :dkim-signature; bh=U9hbfknPkTKna/AqnptoBWdevmCr2DzXjYJjppTeD4k=; fh=KpuXO1Rhu2XnaR7ZqrrNqdjSxNDpF9ksF9Af/TJfAQI=; b=Npp8M+fAYnc7sIhdHRSKAyDvK5oFDkHG9AcnXosWq92RnaFr3gp/8knpXlPbi8iEmP RtHxR9DDIlcX+oSI4Ywx8np6BRdw2/vihSKwwQnwXvw4TIn/adBrN3Lw2jX/f+xxYOCR rQPTsI/jVyQj1UHAUstoI5uSZkdCfoP7/dnPjl+C0Uq5nugeMGJRqPJmUxOPyw70ixTw a/Pc1+GGbV0jriyXrm4oi74p/GgqEhEt41NGhDcaEqr0uUub430KRjxHAJ9DOQRbZw/3 XasCuQkH/r4GootzacFb/hcCN5RcvoJbaAMPV/YGKQFsdO3SxZwKnSxCC3W2iyADN1Rj aWrw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=BmwUdS1r; spf=pass (google.com: domain of linux-kernel+bounces-13925-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-13925-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id p19-20020a170902b09300b001d45090a764si13049822plr.186.2024.01.01.10.13.06 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Jan 2024 10:13:06 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-13925-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=BmwUdS1r; spf=pass (google.com: domain of linux-kernel+bounces-13925-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-13925-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id F3302B2118C for ; Mon, 1 Jan 2024 18:13:02 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9DB19BE5F; Mon, 1 Jan 2024 18:12:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="BmwUdS1r" X-Original-To: linux-kernel@vger.kernel.org Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.11]) (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 F0618BA43; Mon, 1 Jan 2024 18:12:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1704132771; x=1735668771; h=date:from:to:cc:subject:in-reply-to:message-id: references:mime-version; bh=xaOMzq2s3eMU8oWUt36+JE+ktUuXJd1kcZHmLKD8vJI=; b=BmwUdS1rM8IZ6hUsmxrZmb7FKZC+9XHUt5exe1WT7UQNEHEJ1qKlzPwm VnKxoPB3eIsdK9bo7Y30KVXVURRRkz5h80Ap9GRD0/JLPUCYLFKUEuZ5X IM0g/VVjY964UZ0nRhvSA1a0qLTU6Z1Pz5VfYDOFoMkHqVB5lv/P//XSv VTZMGge0sIPA26B1IbmPFVSwko9PdOC4u8jKCv8eUtCylLTzpTM8lF0+U uSdNwqgwifBX35kPAm3gFgGVamoy8MGLECbqRX3weWAs4eikMhj6Hei7O 9xtST45XkGXzihSMIS6MJJS4Fw3qMZ8hyeGymAPTv9HFywSxa0f2PvaH4 A==; X-IronPort-AV: E=McAfee;i="6600,9927,10940"; a="3626573" X-IronPort-AV: E=Sophos;i="6.04,322,1695711600"; d="scan'208";a="3626573" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmvoesa105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Jan 2024 10:12:49 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10940"; a="870035694" X-IronPort-AV: E=Sophos;i="6.04,322,1695711600"; d="scan'208";a="870035694" Received: from amazouz-mobl.ger.corp.intel.com ([10.251.210.158]) by fmsmga003-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Jan 2024 10:12:45 -0800 Date: Mon, 1 Jan 2024 20:12:43 +0200 (EET) From: =?ISO-8859-15?Q?Ilpo_J=E4rvinen?= To: Lukas Wunner cc: linux-pci@vger.kernel.org, Bjorn Helgaas , Lorenzo Pieralisi , Rob Herring , Krzysztof Wilczy??ski , Alexandru Gagniuc , Krishna chaitanya chundru , Srinivas Pandruvada , "Rafael J . Wysocki" , linux-pm@vger.kernel.org, Bjorn Helgaas , LKML , Alex Deucher , Daniel Lezcano , Amit Kucheria , Zhang Rui Subject: Re: [PATCH v3 08/10] PCI/bwctrl: Add "controller" part into PCIe bwctrl In-Reply-To: <20231230184905.GA6104@wunner.de> Message-ID: References: <20230929115723.7864-1-ilpo.jarvinen@linux.intel.com> <20230929115723.7864-9-ilpo.jarvinen@linux.intel.com> <20231230184905.GA6104@wunner.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="8323329-900971598-1704132769=:7866" This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools. --8323329-900971598-1704132769=:7866 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8BIT On Sat, 30 Dec 2023, Lukas Wunner wrote: > On Fri, Sep 29, 2023 at 02:57:21PM +0300, Ilpo J?rvinen wrote: > > > > --- a/drivers/pci/pcie/bwctrl.c > > +static u16 speed2lnkctl2(enum pci_bus_speed speed) > > +{ > > + static const u16 speed_conv[] = { > > + [PCIE_SPEED_2_5GT] = PCI_EXP_LNKCTL2_TLS_2_5GT, > > + [PCIE_SPEED_5_0GT] = PCI_EXP_LNKCTL2_TLS_5_0GT, > > + [PCIE_SPEED_8_0GT] = PCI_EXP_LNKCTL2_TLS_8_0GT, > > + [PCIE_SPEED_16_0GT] = PCI_EXP_LNKCTL2_TLS_16_0GT, > > + [PCIE_SPEED_32_0GT] = PCI_EXP_LNKCTL2_TLS_32_0GT, > > + [PCIE_SPEED_64_0GT] = PCI_EXP_LNKCTL2_TLS_64_0GT, > > + }; > > Looks like this could be a u8 array to save a little bit of memory. > > Also, this seems to duplicate pcie_link_speed[] defined in probe.c. It's not the same. pcie_link_speed[] is indexed by a different thing unless you also suggest I should do index minus a number? There are plenty of arithmetics possible when converting between the types but the existing converions functions don't seem to take advantage of those properties so I've been a bit hesitant to add such assumptions myself. I suppose I used u16 because the reg is u16 but you're of course correct that the values don't take up more than u8. > > +static int bwctrl_select_speed(struct pcie_device *srv, enum pci_bus_speed *speed) > > +{ > > + struct pci_bus *bus = srv->port->subordinate; > > + u8 speeds, dev_speeds; > > + int i; > > + > > + if (*speed > PCIE_LNKCAP2_SLS2SPEED(bus->pcie_bus_speeds)) > > + return -EINVAL; > > + > > + dev_speeds = READ_ONCE(bus->pcie_dev_speeds); > > Hm, why is the compiler barrier needed? It's probably an overkill but there could be a checker which finds this read is not protected by anything while the value could get updated concurrectly (there's probably already such checker as I've seen patches to data races found with some tool). I suppose the alternative would be to mark the data race being harmless (because if endpoint removal clears pcie_dev_speeds, bwctrl will be pretty moot). I just chose to use READ_ONCE() that prevents rereading the same value later in this function making the function behave consistently regardless of what occurs parallel to it with the endpoints. If the value selected cannot be set because of endpoint no longer being there, the other parts of the code will detect that. So if I just add a comment to this line why there's the data race and keep it as is? > > + /* Only the lowest speed can be set when there are no devices */ > > + if (!dev_speeds) > > + dev_speeds = PCI_EXP_LNKCAP2_SLS_2_5GB; > > Maybe move this to patch [06/10], i.e. set dev->bus->pcie_dev_speeds to > PCI_EXP_LNKCAP2_SLS_2_5GB on removal (instead of 0). Okay, I'll set it to 2_5GB on init and removal. > > + speeds = bus->pcie_bus_speeds & dev_speeds; > > + i = BIT(fls(speeds)); > > + while (i >= PCI_EXP_LNKCAP2_SLS_2_5GB) { > > + enum pci_bus_speed candidate; > > + > > + if (speeds & i) { > > + candidate = PCIE_LNKCAP2_SLS2SPEED(i); > > + if (candidate <= *speed) { > > + *speed = candidate; > > + return 0; > > + } > > + } > > + i >>= 1; > > + } > > Can't we just do something like > > supported_speeds = bus->pcie_bus_speeds & dev_speeds; > desired_speeds = GEN_MASK(pcie_link_speed[*speed], 1); > *speed = BIT(fls(supported_speeds & desired_speeds)); > > and thus avoid the loop altogether? Yes, I can change to loopless version. I'll try to create functions for the speed format conversions though rather than open coding them into the logic. > > @@ -91,16 +242,32 @@ static int pcie_bandwidth_notification_probe(struct pcie_device *srv) > > if (ret) > > return ret; > > > > + data = kzalloc(sizeof(*data), GFP_KERNEL); > > + if (!data) { > > + ret = -ENOMEM; > > + goto free_irq; > > + } > > + mutex_init(&data->set_speed_mutex); > > + set_service_data(srv, data); > > + > > I think you should move that further up so that you allocate and populate > the data struct before requesting the IRQ. Otherwise if BIOS has already > enabled link bandwith notification for some reason, the IRQ handler might > be invoked without the data struct being allocated. Sure, I don't know why I missed that possibility. -- i. --8323329-900971598-1704132769=:7866--