ice: lighter locking for PTP time reading
Subsystems (3)
v3 cover ice: lighter locking for PTP time reading
This series removes the use of the heavy-weight PTP hardware semaphore in the gettimex64 path. Instead, serialization of access to the time register is done using a host-side spinlock. The timer hardware is shared between PFs on the PCI adapter, so the spinlock must be shared between ice_pf instances too.
Replacing the PTP hardware semaphore entirely with a mutex is also possible and you can see it done in my git branch[1], but I am not posting those patches yet to keep the scope of this series limited.
[1] https://gitlab.com/mschmidt2/linux/-/commits/ice-ptp-host-side-lock-9
v3:
- Longer variable name ("a" -> "adapter").
- Propagate xarray error in ice_adapter_get with ERR_PTR.
- Added kernel-doc comments for ice_adapter_{get,put}.
v2:
- Patch 1: Rely on xarray's own lock. (Suggested by Jiri Pirko)
- Patch 2: Do not use *_irqsave with ptp_gltsyn_time_lock, as it's used only in process contexts.
Michal Schmidt (3):
ice: add ice_adapter for shared data across PFs on the same NIC
ice: avoid the PTP hardware semaphore in gettimex64 path
ice: fold ice_ptp_read_time into ice_ptp_gettimex64
drivers/net/ethernet/intel/ice/Makefile | 3 +- drivers/net/ethernet/intel/ice/ice.h | 2 + drivers/net/ethernet/intel/ice/ice_adapter.c | 109 +++++++++++++++++++ drivers/net/ethernet/intel/ice/ice_adapter.h | 28 +++++ drivers/net/ethernet/intel/ice/ice_main.c | 8 ++ drivers/net/ethernet/intel/ice/ice_ptp.c | 33 +----- drivers/net/ethernet/intel/ice/ice_ptp_hw.c | 3 + 7 files changed, 156 insertions(+), 30 deletions(-) create mode 100644 drivers/net/ethernet/intel/ice/ice_adapter.c create mode 100644 drivers/net/ethernet/intel/ice/ice_adapter.h
Signature · 1 line
2.43.2
PATCH v3 1/3 ice: add ice_adapter for shared data across PFs on the same NIC
There is a need for synchronization between ice PFs on the same physical adapter.
Add a "struct ice_adapter" for holding data shared between PFs of the same multifunction PCI device. The struct is refcounted - each ice_pf holds a reference to it.
Its first use will be for PTP. I expect it will be useful also to improve the ugliness that is ice_prot_id_tbl.
--- drivers/net/ethernet/intel/ice/Makefile | 3 +- drivers/net/ethernet/intel/ice/ice.h | 2 + drivers/net/ethernet/intel/ice/ice_adapter.c | 107 +++++++++++++++++++ drivers/net/ethernet/intel/ice/ice_adapter.h | 22 ++++ drivers/net/ethernet/intel/ice/ice_main.c | 8 ++ 5 files changed, 141 insertions(+), 1 deletion(-) create mode 100644 drivers/net/ethernet/intel/ice/ice_adapter.c create mode 100644 drivers/net/ethernet/intel/ice/ice_adapter.h
drivers/net/ethernet/intel/ice/Makefile +2 -1
@@ -36,7 +36,8 @@ ice-y := ice_main.o \ ice_repr.o \ ice_tc_lib.o \ ice_fwlog.o \ - ice_debugfs.o + ice_debugfs.o \ + ice_adapter.o ice-$(CONFIG_PCI_IOV) += \ ice_sriov.o \ ice_virtchnl.o \
drivers/net/ethernet/intel/ice/ice.h +2 -0
@@ -77,6 +77,7 @@#include "ice_gnss.h" #include "ice_irq.h" #include "ice_dpll.h" +#include "ice_adapter.h" #define ICE_BAR0 0#define ICE_REQ_DESC_MULTIPLE 32
@@ -544,6 +545,7 @@ struct ice_agg_node {struct ice_pf {struct pci_dev *pdev; +struct ice_adapter *adapter;struct devlink_region *nvm_region;struct devlink_region *sram_region;
drivers/net/ethernet/intel/ice/ice_adapter.c new file +107 -0
107 lines not on this page · raw message
drivers/net/ethernet/intel/ice/ice_adapter.h new file +22 -0
@@ -0,0 +1,22 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* SPDX-FileCopyrightText: Copyright Red Hat */ + +#ifndef _ICE_ADAPTER_H_ +#define _ICE_ADAPTER_H_ + +#include <linux/refcount_types.h> + +struct pci_dev ; + +/** +* struct ice_adapter - PCI adapter resources shared across PFs +* @refcount: Reference count. struct ice_pf objects hold the references. +*/ +struct ice_adapter { +refcount_t refcount; +}; + +struct ice_adapter *ice_adapter_get (const struct pci_dev *pdev); +void ice_adapter_put (const struct pci_dev *pdev); + +#endif /* _ICE_ADAPTER_H */
drivers/net/ethernet/intel/ice/ice_main.c +8 -0
@@ -5093,6 +5093,7 @@ static int ice_probe(struct pci_dev *pdev, const struct pci_device_id __always_unused *ent) { struct device *dev = &pdev->dev; +struct ice_adapter *adapter;struct ice_pf *pf;struct ice_hw *hw;int err;
@@ -5145,7 +5146,12 @@ ice_probe(struct pci_dev *pdev, const struct pci_device_id __always_unused *ent)pci_set_master (pdev); + adapter =ice_adapter_get (pdev); +if (IS_ERR (adapter)) +return PTR_ERR (adapter); + pf->pdev = pdev; + pf->adapter = adapter;pci_set_drvdata (pdev, pf);set_bit (ICE_DOWN , pf->state);/* Disable service task until DOWN bit is cleared */
@@ -5196,6 +5202,7 @@ ice_probe(struct pci_dev *pdev, const struct pci_device_id __always_unused *ent) err_load:ice_deinit (pf); err_init: +ice_adapter_put (pdev);pci_disable_device (pdev);return err; }
@@ -5302,6 +5309,7 @@ static void ice_remove(struct pci_dev *pdev)ice_setup_mc_magic_wake (pf);ice_set_wake (pf); +ice_adapter_put (pdev);pci_disable_device (pdev); }
Signature · 1 line
2.43.2
PATCH v3 2/3 ice: avoid the PTP hardware semaphore in gettimex64 path
In reply to Michal SchmidtThe PTP hardware semaphore (PFTSYN_SEM) is used to synchronize operations that program the PTP timers. The operations involve issuing commands to the sideband queue. The E810 does not have a hardware sideband queue, so the admin queue is used. The admin queue is slow. I have observed delays in hundreds of milliseconds waiting for ice_sq_done.
When phc2sys reads the time from the ice PTP clock and PFTSYN_SEM is held by a task performing one of the slow operations, ice_ptp_lock can easily time out. phc2sys gets -EBUSY and the kernel prints:
ice 0000:XX:YY.0: PTP failed to get time
These messages appear once every few seconds, causing log spam.
The E810 datasheet recommends an algorithm for reading the upper 64 bits of the GLTSYN_TIME register. It matches what's implemented in ice_ptp_read_src_clk_reg. It is robust against wrap-around, but not necessarily against the concurrent setting of the register (with GLTSYN_CMD_{INIT,ADJ}_TIME commands). Perhaps that's why ice_ptp_gettimex64 also takes PFTSYN_SEM.
The race with time setters can be prevented without relying on the PTP hardware semaphore. Using the "ice_adapter" from the previous patch, we can have a common spinlock for the PFs that share the clock hardware. It will protect the reading and writing to the GLTSYN_TIME register. The writing is performed indirectly, by the hardware, as a result of the driver writing GLTSYN_CMD_SYNC in ice_ptp_exec_tmr_cmd. I wasn't sure if the ice_flush there is enough to make sure GLTSYN_TIME has been updated, but it works well in my testing.
My test code can be seen here:
https://gitlab.com/mschmidt2/linux/-/commits/ice-ptp-host-side-lock-9
It consists of:
- kernel threads reading the time in a busy loop and looking at the deltas between consecutive values, reporting new maxima.
- a shell script that sets the time repeatedly;
- a bpftrace probe to produce a histogram of the measured deltas.
Without the spinlock ptp_gltsyn_time_lock, it is easy to see tearing. Deltas in the [2G, 4G) range appear in the histograms.
With the spinlock added, there is no tearing and the biggest delta I saw was in the range [1M, 2M), that is under 2 ms.
--- drivers/net/ethernet/intel/ice/ice_adapter.c | 2 ++ drivers/net/ethernet/intel/ice/ice_adapter.h | 6 ++++++ drivers/net/ethernet/intel/ice/ice_ptp.c | 8 +------- drivers/net/ethernet/intel/ice/ice_ptp_hw.c | 3 +++ 4 files changed, 12 insertions(+), 7 deletions(-)
drivers/net/ethernet/intel/ice/ice_adapter.c +2 -0
@@ -5,6 +5,7 @@#include <linux/mutex.h> #include <linux/pci.h> #include <linux/slab.h> +#include <linux/spinlock.h> #include <linux/xarray.h> #include "ice_adapter.h"
@@ -28,6 +29,7 @@ static struct ice_adapter *ice_adapter_new(void)if (!adapter)return NULL ; +spin_lock_init (&adapter->ptp_gltsyn_time_lock);refcount_set (&adapter->refcount,1 );return adapter;
drivers/net/ethernet/intel/ice/ice_adapter.h +6 -0
@@ -4,15 +4,21 @@#ifndef _ICE_ADAPTER_H_#define _ICE_ADAPTER_H_ +#include <linux/spinlock_types.h> #include <linux/refcount_types.h> struct pci_dev ;/** * struct ice_adapter - PCI adapter resources shared across PFs +* @ptp_gltsyn_time_lock: Spinlock protecting access to the GLTSYN_TIME +* register of the PTP clock. * @refcount: Reference count. struct ice_pf objects hold the references. */ struct ice_adapter { +/* For access to the GLTSYN_TIME register */ +spinlock_t ptp_gltsyn_time_lock; +refcount_t refcount; };
drivers/net/ethernet/intel/ice/ice_ptp.c +1 -7
@@ -374,6 +374,7 @@ ice_ptp_read_src_clk_reg(struct ice_pf *pf, struct ptp_system_timestamp *sts)u8 tmr_idx; tmr_idx =ice_get_ptp_src_clock_index (hw); +guard (spinlock)(&pf->adapter->ptp_gltsyn_time_lock);/* Read the system timestamp pre PHC read */ ptp_read_system_prets (sts);
@@ -1925,15 +1926,8 @@ ice_ptp_gettimex64(struct ptp_clock_info *info, struct timespec64 *ts, struct ptp_system_timestamp *sts) {struct ice_pf *pf =ptp_info_to_pf (info); -struct ice_hw *hw = &pf->hw; - -if (!ice_ptp_lock (hw)) { -dev_err (ice_pf_to_dev (pf),"PTP failed to get time\n" ); -return -EBUSY ; - }ice_ptp_read_time (pf, ts, sts); -ice_ptp_unlock (hw);return 0 ; }
drivers/net/ethernet/intel/ice/ice_ptp_hw.c +3 -0
@@ -274,6 +274,9 @@ void ice_ptp_src_cmd(struct ice_hw *hw, enum ice_ptp_tmr_cmd cmd)*/ static void ice_ptp_exec_tmr_cmd (struct ice_hw *hw) { + struct ice_pf *pf = container_of(hw, struct ice_pf, hw); + +guard (spinlock)(&pf->adapter->ptp_gltsyn_time_lock);wr32 (hw,GLTSYN_CMD_SYNC ,SYNC_EXEC_CMD );ice_flush (hw); }
Signature · 1 line
2.43.2
PATCH v3 3/3 ice: fold ice_ptp_read_time into ice_ptp_gettimex64
In reply to Michal SchmidtThis is a cleanup. It is unnecessary to have this function just to call another function.
--- drivers/net/ethernet/intel/ice/ice_ptp.c | 25 +++--------------------- 1 file changed, 3 insertions(+), 22 deletions(-)
drivers/net/ethernet/intel/ice/ice_ptp.c +3 -22
@@ -1166,26 +1166,6 @@ static void ice_ptp_reset_cached_phctime(struct ice_pf *pf)ice_ptp_mark_tx_tracker_stale (&pf->ptp.port.tx); } -/** -* ice_ptp_read_time - Read the time from the device -* @pf: Board private structure -* @ts: timespec structure to hold the current time value -* @sts: Optional parameter for holding a pair of system timestamps from -* the system clock. Will be ignored if NULL is given. -* -* This function reads the source clock registers and stores them in a timespec. -* However, since the registers are 64 bits of nanoseconds, we must convert the -* result to a timespec before we can return. -*/ -static void -ice_ptp_read_time (struct ice_pf *pf,struct timespec64 *ts, -struct ptp_system_timestamp *sts) -{ -u64 time_ns =ice_ptp_read_src_clk_reg (pf, sts); - - *ts =ns_to_timespec64 (time_ns); -} -/** * ice_ptp_write_init - Set PHC time to provided value * @pf: Board private structure
@@ -1926,9 +1906,10 @@ ice_ptp_gettimex64(struct ptp_clock_info *info, struct timespec64 *ts, struct ptp_system_timestamp *sts) {struct ice_pf *pf =ptp_info_to_pf (info); +u64 time_ns; -ice_ptp_read_time (pf, ts, sts); - + time_ns =ice_ptp_read_src_clk_reg (pf, sts); + *ts =ns_to_timespec64 (time_ns);return 0 ; }
Signature · 1 line
2.43.2
Re: [Intel-wired-lan] [PATCH net-next v3 1/3] ice: add ice_adapter for shared data across PFs on the same NIC
In reply to Michal SchmidtQuoted 70 lines · Michal Schmidt
There is a need for synchronization between ice PFs on the same physical adapter.
Add a "struct
ice_adapter" for holding data shared between PFs of the same multifunction PCI device. The struct is refcounted - eachice_pfholds a reference to it.Its first use will be for PTP. I expect it will be useful also to improve the ugliness that is
ice_prot_id_tbl.Signed-off-by Michal Schmidt <[email protected]>--- drivers/net/ethernet/intel/ice/Makefile | 3 +- drivers/net/ethernet/intel/ice/ice.h | 2 + drivers/net/ethernet/intel/ice/ice_adapter.c | 107 +++++++++++++++++++ drivers/net/ethernet/intel/ice/ice_adapter.h | 22 ++++ drivers/net/ethernet/intel/ice/ice_main.c | 8 ++ 5 files changed, 141 insertions(+), 1 deletion(-) create mode 100644 drivers/net/ethernet/intel/ice/ice_adapter.c create mode 100644 drivers/net/ethernet/intel/ice/ice_adapter.hdrivers/net/ethernet/intel/ice/Makefile +2
-1@@ -36,7 +36,8 @@ ice-y := ice_main.o \ ice_repr.o \ ice_tc_lib.o \ ice_fwlog.o \ - ice_debugfs.o + ice_debugfs.o \ + ice_adapter.o ice-$(CONFIG_PCI_IOV) += \ ice_sriov.o \ ice_virtchnl.o \drivers/net/ethernet/intel/ice/ice.h +2
-0@@ -77,6 +77,7 @@#include "ice_gnss.h" #include "ice_irq.h" #include "ice_dpll.h" +#include "ice_adapter.h" #define ICE_BAR0 0#define ICE_REQ_DESC_MULTIPLE 32@@ -544,6 +545,7 @@ struct ice_agg_node {struct ice_pf {struct pci_dev *pdev; +struct ice_adapter *adapter;struct devlink_region *nvm_region;struct devlink_region *sram_region;drivers/net/ethernet/intel/ice/ice_adapter.c new file +9
-0@@ -0,0 +1,107 @@ +// SPDX-License-Identifier: GPL-2.0-only +// SPDX-FileCopyrightText: Copyright Red Hat + +#include <linux/cleanup.h> +#include <linux/mutex.h> +#include <linux/pci.h> +#include <linux/slab.h> +#include <linux/xarray.h> +#include "ice_adapter.h"
drivers/net/ethernet/intel/ice/ice_adapter.c line 19
Quoted 10 lines
drivers/net/ethernet/intel/ice/ice_adapter.c new file +10
-0+ +static DEFINE_XARRAY (ice_adapters); + +static unsigned long ice_adapter_index (const struct pci_dev *pdev) +{ +unsigned int domain =pci_domain_nr (pdev->bus); + +WARN_ON ((unsigned long )domain >> (BITS_PER_LONG -13 )); +return ((unsigned long )domain <<13 ) | + ((unsigned long)pdev->bus->number << 5) |
Magic numbers?
Quoted 25 lines
drivers/net/ethernet/intel/ice/ice_adapter.c new file +25
-0+PCI_SLOT (pdev->devfn); +} + +static struct ice_adapter *ice_adapter_new (void ) +{ +struct ice_adapter *adapter; + + adapter =kzalloc (sizeof (*adapter),GFP_KERNEL ); +if (!adapter) +return NULL ; + +refcount_set (&adapter->refcount,1 ); + +return adapter; +} + +static void ice_adapter_free (struct ice_adapter *adapter) +{ +kfree (adapter); +} + +DEFINE_FREE(ice_adapter_free, struct ice_adapter*, if (_T) ice_adapter_free(_T)) + +/** +* ice_adapter_get - Get a shared ice_adapter structure.
drivers/net/ethernet/intel/ice/ice_adapter.c line 54
Quoted 10 lines
drivers/net/ethernet/intel/ice/ice_adapter.c new file +10
-0+* @pdev: Pointer to the pci_dev whose driver is getting the ice_adapter. +* +* Gets a pointer to a shared ice_adapter structure. Physical functions (PFs) +* of the same multi-function PCI device share one ice_adapter structure. +* The ice_adapter is reference-counted. The PF driver must use ice_adapter_put +* to release its reference. +* +* Context: Process, may sleep. +* Return: Pointer to ice_adapter on success. +* ERR_PTR() on error. -ENOMEM is the only possible error.
What about ERR_PTR(xa_err(ret))?
drivers/net/ethernet/intel/ice/ice_adapter.c line 63
Quoted 9 lines
drivers/net/ethernet/intel/ice/ice_adapter.c new file +9
-0+*/ +struct ice_adapter *ice_adapter_get (const struct pci_dev *pdev) +{ + struct ice_adapter *ret, __free(ice_adapter_free) *adapter = NULL; +unsigned long index =ice_adapter_index (pdev); + + adapter =ice_adapter_new (); +if (!adapter) +return ERR_PTR (-ENOMEM );
---8<---
Thanks,
Marcin
Przemek Kitszel
maintainerRe: [Intel-wired-lan] [PATCH net-next v3 1/3] ice: add ice_adapter for shared data across PFs on the same NIC
In reply to Michal SchmidtQuoted 11 lines · Michal Schmidt
There is a need for synchronization between ice PFs on the same physical adapter.
Add a "struct
ice_adapter" for holding data shared between PFs of the same multifunction PCI device. The struct is refcounted - eachice_pfholds a reference to it.Its first use will be for PTP. I expect it will be useful also to improve the ugliness that is
ice_prot_id_tbl.Signed-off-by Michal Schmidt <[email protected]>
Thank you very much for this series, we have spotted the need for something like that very recently, I have already pinged our PTP folks to take a look. (+CC Sergey)
Why not wipe ice_ptp_lock() entirely?
(could be left for Intel folks though)
please find the usual code related feedback inline (again, I really appreciate and I am grateful for this series)
Quoted 60 lines
--- drivers/net/ethernet/intel/ice/Makefile | 3 +- drivers/net/ethernet/intel/ice/ice.h | 2 + drivers/net/ethernet/intel/ice/ice_adapter.c | 107 +++++++++++++++++++ drivers/net/ethernet/intel/ice/ice_adapter.h | 22 ++++ drivers/net/ethernet/intel/ice/ice_main.c | 8 ++ 5 files changed, 141 insertions(+), 1 deletion(-)create mode 100644
drivers/net/ethernet/intel/ice/ice_adapter.ccreate mode 100644drivers/net/ethernet/intel/ice/ice_adapter.hdrivers/net/ethernet/intel/ice/Makefile +2
-1@@ -36,7 +36,8 @@ ice-y := ice_main.o \ ice_repr.o \ ice_tc_lib.o \ ice_fwlog.o \ - ice_debugfs.o + ice_debugfs.o \ + ice_adapter.o ice-$(CONFIG_PCI_IOV) += \ ice_sriov.o \ ice_virtchnl.o \drivers/net/ethernet/intel/ice/ice.h +2
-0@@ -77,6 +77,7 @@#include "ice_gnss.h" #include "ice_irq.h" #include "ice_dpll.h" +#include "ice_adapter.h" #define ICE_BAR0 0#define ICE_REQ_DESC_MULTIPLE 32@@ -544,6 +545,7 @@ struct ice_agg_node {struct ice_pf {struct pci_dev *pdev; +struct ice_adapter *adapter;struct devlink_region *nvm_region;struct devlink_region *sram_region;drivers/net/ethernet/intel/ice/ice_adapter.c new file +10
-0@@ -0,0 +1,107 @@ +// SPDX-License-Identifier: GPL-2.0-only +// SPDX-FileCopyrightText: Copyright Red Hat + +#include <linux/cleanup.h> +#include <linux/mutex.h> +#include <linux/pci.h> +#include <linux/slab.h> +#include <linux/xarray.h> +#include "ice_adapter.h" +
drivers/net/ethernet/intel/ice/ice_adapter.c line 20
Quoted 10 lines
drivers/net/ethernet/intel/ice/ice_adapter.c new file +10
-0+static DEFINE_XARRAY (ice_adapters); + +static unsigned long ice_adapter_index (const struct pci_dev *pdev) +{ +unsigned int domain =pci_domain_nr (pdev->bus); + +WARN_ON ((unsigned long )domain >> (BITS_PER_LONG -13 )); +return ((unsigned long )domain <<13 ) | + ((unsigned long )pdev->bus->number <<5 ) | +PCI_SLOT (pdev->devfn);
xarray is free to use non-0-based indices, so this whole function could be simplified as:
/* note the PCI_SLOT() call to clear function from devfn */ return PCI_DEVID(pci_domain_nr(pdev->bus), PCI_SLOT(pdev->devfn));
Quoted 9 lines
drivers/net/ethernet/intel/ice/ice_adapter.c new file +9
-0+} + +static struct ice_adapter *ice_adapter_new (void ) +{ +struct ice_adapter *adapter; + + adapter =kzalloc (sizeof (*adapter),GFP_KERNEL ); +if (!adapter) +return NULL ;
drivers/net/ethernet/intel/ice/ice_adapter.c line 39
Quoted 10 lines
drivers/net/ethernet/intel/ice/ice_adapter.c new file +10
-0+ +refcount_set (&adapter->refcount,1 ); + +return adapter; +} + +static void ice_adapter_free (struct ice_adapter *adapter) +{ +kfree (adapter); +}
I would say that this is too thin wrapper for "kernel interface" (memory ptr) to warrant it, IOW just place kfree in place of ice_adapter_free, that will also free us from the need to use DEFINE_FREE()
Quoted 5 lines
drivers/net/ethernet/intel/ice/ice_adapter.c new file +5
-0+ +DEFINE_FREE(ice_adapter_free, struct ice_adapter*, if (_T) ice_adapter_free(_T)) + +/** +* ice_adapter_get - Get a shared ice_adapter structure.
drivers/net/ethernet/intel/ice/ice_adapter.c line 54
Quoted 10 lines
drivers/net/ethernet/intel/ice/ice_adapter.c new file +10
-0+* @pdev: Pointer to the pci_dev whose driver is getting the ice_adapter. +* +* Gets a pointer to a shared ice_adapter structure. Physical functions (PFs) +* of the same multi-function PCI device share one ice_adapter structure. +* The ice_adapter is reference-counted. The PF driver must use ice_adapter_put +* to release its reference. +* +* Context: Process, may sleep. +* Return: Pointer to ice_adapter on success. +* ERR_PTR() on error. -ENOMEM is the only possible error.
that's inconvenient, to the point that it will be better to have a dummy static entry used for this purpose, but I see that this is something broader that this particular use case, so - feel free to ignore
Quoted 11 lines
drivers/net/ethernet/intel/ice/ice_adapter.c new file +11
-0+*/ +struct ice_adapter *ice_adapter_get (const struct pci_dev *pdev) +{ + struct ice_adapter *ret, __free(ice_adapter_free) *adapter = NULL; +unsigned long index =ice_adapter_index (pdev); + + adapter =ice_adapter_new (); +if (!adapter) +return ERR_PTR (-ENOMEM ); + +xa_lock (&ice_adapters);
drivers/net/ethernet/intel/ice/ice_adapter.c line 75
Quoted 10 lines
drivers/net/ethernet/intel/ice/ice_adapter.c new file +10
-0+ ret =__xa_cmpxchg (&ice_adapters, index,NULL , adapter,GFP_KERNEL ); +if (xa_is_err (ret)) { + ret =ERR_PTR (xa_err (ret)); + goto unlock; + } +if (ret) { +refcount_inc (&ret->refcount); + goto unlock; + } + ret =no_free_ptr (adapter);
nice solution, but this is an idiom that we want across the kernel instead of opting out of auto management in such cases as this one? (esp that you have open-coded locking anyway)
I would expect to have explicit two stores (first to ensure index is present, second to overwrite entry if null) easier than cmpxchg
+ unneeded allocation (that could cause whole function to fail!)
Quoted 44 lines
drivers/net/ethernet/intel/ice/ice_adapter.c new file +32
-0+unlock: +xa_unlock (&ice_adapters); +return ret; +} + +/** +* ice_adapter_put - Release a reference to the shared ice_adapter structure. +* @pdev: Pointer to the pci_dev whose driver is releasing the ice_adapter. +* +* Releases the reference to ice_adapter previously obtained with +* ice_adapter_get. +* +* Context: Any. +*/ +void ice_adapter_put (const struct pci_dev *pdev) +{ +unsigned long index =ice_adapter_index (pdev); +struct ice_adapter *adapter; + +xa_lock (&ice_adapters); + adapter =xa_load (&ice_adapters, index); +if (WARN_ON (!adapter)) + goto unlock; + +if (!refcount_dec_and_test (&adapter->refcount)) + goto unlock; + +WARN_ON (__xa_erase (&ice_adapters, index) != adapter); +ice_adapter_free (adapter); +unlock: +xa_unlock (&ice_adapters); +}drivers/net/ethernet/intel/ice/ice_adapter.h new file +6
-0@@ -0,0 +1,22 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* SPDX-FileCopyrightText: Copyright Red Hat */ + +#ifndef _ICE_ADAPTER_H_ +#define _ICE_ADAPTER_H_ +
drivers/net/ethernet/intel/ice/ice_adapter.h line 16
Quoted 10 lines
drivers/net/ethernet/intel/ice/ice_adapter.h new file +10
-0+#include <linux/refcount_types.h> + +struct pci_dev ; + +/** +* struct ice_adapter - PCI adapter resources shared across PFs +* @refcount: Reference count. struct ice_pf objects hold the references. +*/ +struct ice_adapter { +refcount_t refcount;
this is refcounted always under a lock, so could be plain "int", but not a big deal
Quoted 47 lines
drivers/net/ethernet/intel/ice/ice_adapter.h new file +6
-0+}; + +struct ice_adapter *ice_adapter_get (const struct pci_dev *pdev); +void ice_adapter_put (const struct pci_dev *pdev); + +#endif /* _ICE_ADAPTER_H */ drivers/net/ethernet/intel/ice/ice_main.c +8
-0@@ -5093,6 +5093,7 @@ static int ice_probe(struct pci_dev *pdev, const struct pci_device_id __always_unused *ent) { struct device *dev = &pdev->dev; +struct ice_adapter *adapter;struct ice_pf *pf;struct ice_hw *hw;int err;@@ -5145,7 +5146,12 @@ ice_probe(struct pci_dev *pdev, const struct pci_device_id __always_unused *ent)pci_set_master (pdev); + adapter =ice_adapter_get (pdev); +if (IS_ERR (adapter)) +return PTR_ERR (adapter); + pf->pdev = pdev; + pf->adapter = adapter;pci_set_drvdata (pdev, pf);set_bit (ICE_DOWN , pf->state);/* Disable service task until DOWN bit is cleared */ @@ -5196,6 +5202,7 @@ ice_probe(struct pci_dev *pdev, const struct pci_device_id __always_unused *ent) err_load:ice_deinit (pf); err_init: +ice_adapter_put (pdev);pci_disable_device (pdev);return err; }@@ -5302,6 +5309,7 @@ static void ice_remove(struct pci_dev *pdev)ice_setup_mc_magic_wake (pf);ice_set_wake (pf); +ice_adapter_put (pdev);pci_disable_device (pdev); }
Re: [Intel-wired-lan] [PATCH net-next v3 1/3] ice: add ice_adapter for shared data across PFs on the same NIC
In reply to Marcin SzycikQuoted 75 lines · Marcin Szycik
Quoted 74 lines · Michal Schmidt
There is a need for synchronization between ice PFs on the same physical adapter.
Add a "struct
ice_adapter" for holding data shared between PFs of the same multifunction PCI device. The struct is refcounted - eachice_pfholds a reference to it.Its first use will be for PTP. I expect it will be useful also to improve the ugliness that is
ice_prot_id_tbl.Signed-off-by Michal Schmidt <[email protected]>--- drivers/net/ethernet/intel/ice/Makefile | 3 +- drivers/net/ethernet/intel/ice/ice.h | 2 + drivers/net/ethernet/intel/ice/ice_adapter.c | 107 +++++++++++++++++++ drivers/net/ethernet/intel/ice/ice_adapter.h | 22 ++++ drivers/net/ethernet/intel/ice/ice_main.c | 8 ++ 5 files changed, 141 insertions(+), 1 deletion(-) create mode 100644 drivers/net/ethernet/intel/ice/ice_adapter.c create mode 100644 drivers/net/ethernet/intel/ice/ice_adapter.hdrivers/net/ethernet/intel/ice/Makefile +2
-1@@ -36,7 +36,8 @@ ice-y := ice_main.o \ ice_repr.o \ ice_tc_lib.o \ ice_fwlog.o \ - ice_debugfs.o + ice_debugfs.o \ + ice_adapter.o ice-$(CONFIG_PCI_IOV) += \ ice_sriov.o \ ice_virtchnl.o \drivers/net/ethernet/intel/ice/ice.h +2
-0@@ -77,6 +77,7 @@#include "ice_gnss.h" #include "ice_irq.h" #include "ice_dpll.h" +#include "ice_adapter.h" #define ICE_BAR0 0#define ICE_REQ_DESC_MULTIPLE 32@@ -544,6 +545,7 @@ struct ice_agg_node {struct ice_pf {struct pci_dev *pdev; +struct ice_adapter *adapter;struct devlink_region *nvm_region;struct devlink_region *sram_region;drivers/net/ethernet/intel/ice/ice_adapter.c new file +13
-0@@ -0,0 +1,107 @@ +// SPDX-License-Identifier: GPL-2.0-only +// SPDX-FileCopyrightText: Copyright Red Hat + +#include <linux/cleanup.h> +#include <linux/mutex.h> +#include <linux/pci.h> +#include <linux/slab.h> +#include <linux/xarray.h> +#include "ice_adapter.h" + +static DEFINE_XARRAY (ice_adapters); + +static unsigned long ice_adapter_index (const struct pci_dev *pdev)
Quoted 8 lines
drivers/net/ethernet/intel/ice/ice_adapter.c line 19
Quoted 6 lines
drivers/net/ethernet/intel/ice/ice_adapter.c new file +6
-0+{ +unsigned int domain =pci_domain_nr (pdev->bus); + +WARN_ON ((unsigned long )domain >> (BITS_PER_LONG -13 )); +return ((unsigned long )domain <<13 ) | + ((unsigned long)pdev->bus->number << 5) |Magic numbers?
5 bits for the slot number, 8 bits for the bus number. 5+18=13. I did not find any existing definitions for this purpose in pci.h, but I can add some local macros.
Quoted 29 lines
Quoted 29 lines
drivers/net/ethernet/intel/ice/ice_adapter.c new file +29
-0+PCI_SLOT (pdev->devfn); +} + +static struct ice_adapter *ice_adapter_new (void ) +{ +struct ice_adapter *adapter; + + adapter =kzalloc (sizeof (*adapter),GFP_KERNEL ); +if (!adapter) +return NULL ; + +refcount_set (&adapter->refcount,1 ); + +return adapter; +} + +static void ice_adapter_free (struct ice_adapter *adapter) +{ +kfree (adapter); +} + +DEFINE_FREE(ice_adapter_free, struct ice_adapter*, if (_T) ice_adapter_free(_T)) + +/** +* ice_adapter_get - Get a shared ice_adapter structure. +* @pdev: Pointer to the pci_dev whose driver is getting the ice_adapter. +* +* Gets a pointer to a shared ice_adapter structure. Physical functions (PFs) +* of the same multi-function PCI device share one ice_adapter structure.
Quoted 8 lines
drivers/net/ethernet/intel/ice/ice_adapter.c line 54
Quoted 6 lines
drivers/net/ethernet/intel/ice/ice_adapter.c new file +6
-0+* The ice_adapter is reference-counted. The PF driver must use ice_adapter_put +* to release its reference. +* +* Context: Process, may sleep. +* Return: Pointer to ice_adapter on success. +* ERR_PTR() on error. -ENOMEM is the only possible error. What about ERR_PTR(xa_err(ret))?
The Xarray call can fail with -EINVAL or -ENOMEM. The -EINVAL would be the result only if I'd attempt to insert an unaligned pointer, which I'm not doing, so that leaves -ENOMEM as the only possible error.
Quoted 15 lines
drivers/net/ethernet/intel/ice/ice_adapter.c line 63
Quoted 9 lines
drivers/net/ethernet/intel/ice/ice_adapter.c new file +9
-0+*/ +struct ice_adapter *ice_adapter_get (const struct pci_dev *pdev) +{ + struct ice_adapter *ret, __free(ice_adapter_free) *adapter = NULL; +unsigned long index =ice_adapter_index (pdev); + + adapter =ice_adapter_new (); +if (!adapter) +return ERR_PTR (-ENOMEM );---8<---
Thanks,
Marcin
Re: [Intel-wired-lan] [PATCH net-next v3 1/3] ice: add ice_adapter for shared data across PFs on the same NIC
In reply to Przemek KitszelQuoted 11 lines · Przemek Kitszel
Quoted 10 lines · Michal Schmidt
There is a need for synchronization between ice PFs on the same physical adapter.
Add a "struct
ice_adapter" for holding data shared between PFs of the same multifunction PCI device. The struct is refcounted - eachice_pfholds a reference to it.Its first use will be for PTP. I expect it will be useful also to improve the ugliness that is
ice_prot_id_tbl.
Quoted 8 lines
Quoted 1 line
Signed-off-by Michal Schmidt <[email protected]>Thank you very much for this series, we have spotted the need for something like that very recently, I have already pinged our PTP folks to take a look. (+CC Sergey)
Why not wipe
ice_ptp_lock()entirely?
(could be left for Intel folks though)
I am doing this too, just not yet in this series I posted, because I did not want to expand the scope of the series between reviews. See my gitlab branch I linked in the cover letter, specifically this patch:
https://gitlab.com/mschmidt2/linux/-/commit/89a1bd2904ac8b0614bcfc2fce464bf5f60b0f0c
Quoted 77 lines
please find the usual code related feedback inline (again, I really appreciate and I am grateful for this series)
Quoted 60 lines
--- drivers/net/ethernet/intel/ice/Makefile | 3 +- drivers/net/ethernet/intel/ice/ice.h | 2 + drivers/net/ethernet/intel/ice/ice_adapter.c | 107 +++++++++++++++++++ drivers/net/ethernet/intel/ice/ice_adapter.h | 22 ++++ drivers/net/ethernet/intel/ice/ice_main.c | 8 ++ 5 files changed, 141 insertions(+), 1 deletion(-)create mode 100644
drivers/net/ethernet/intel/ice/ice_adapter.ccreate mode 100644drivers/net/ethernet/intel/ice/ice_adapter.hdrivers/net/ethernet/intel/ice/Makefile +2
-1@@ -36,7 +36,8 @@ ice-y := ice_main.o \ ice_repr.o \ ice_tc_lib.o \ ice_fwlog.o \ - ice_debugfs.o + ice_debugfs.o \ + ice_adapter.o ice-$(CONFIG_PCI_IOV) += \ ice_sriov.o \ ice_virtchnl.o \drivers/net/ethernet/intel/ice/ice.h +2
-0@@ -77,6 +77,7 @@#include "ice_gnss.h" #include "ice_irq.h" #include "ice_dpll.h" +#include "ice_adapter.h" #define ICE_BAR0 0#define ICE_REQ_DESC_MULTIPLE 32@@ -544,6 +545,7 @@ struct ice_agg_node {struct ice_pf {struct pci_dev *pdev; +struct ice_adapter *adapter;struct devlink_region *nvm_region;struct devlink_region *sram_region;drivers/net/ethernet/intel/ice/ice_adapter.c new file +10
-0@@ -0,0 +1,107 @@ +// SPDX-License-Identifier: GPL-2.0-only +// SPDX-FileCopyrightText: Copyright Red Hat + +#include <linux/cleanup.h> +#include <linux/mutex.h> +#include <linux/pci.h> +#include <linux/slab.h> +#include <linux/xarray.h> +#include "ice_adapter.h" +drivers/net/ethernet/intel/ice/ice_adapter.c line 20
Quoted 10 lines
drivers/net/ethernet/intel/ice/ice_adapter.c new file +10
-0+static DEFINE_XARRAY (ice_adapters); + +static unsigned long ice_adapter_index (const struct pci_dev *pdev) +{ +unsigned int domain =pci_domain_nr (pdev->bus); + +WARN_ON ((unsigned long )domain >> (BITS_PER_LONG -13 )); +return ((unsigned long )domain <<13 ) | + ((unsigned long )pdev->bus->number <<5 ) | +PCI_SLOT (pdev->devfn);xarray is free to use non-0-based indices, so this whole function could be simplified as:
Quoted 2 lines
/* note the
PCI_SLOT()call to clear function from devfn */ returnPCI_DEVID(pci_domain_nr(pdev->bus), PCI_SLOT(pdev->devfn));
This is not equivalent. My function encodes three PCI numbers into the
index: domain, bus, slot.
Your version would have only: domain, slot. The bus number would be lost. And also, higher-than-16 bits of the domain would be lost unnecessarily.
Quoted 15 lines
Quoted 15 lines
drivers/net/ethernet/intel/ice/ice_adapter.c new file +15
-0+} + +static struct ice_adapter *ice_adapter_new (void ) +{ +struct ice_adapter *adapter; + + adapter =kzalloc (sizeof (*adapter),GFP_KERNEL ); +if (!adapter) +return NULL ; + +refcount_set (&adapter->refcount,1 ); + +return adapter; +} +
Quoted 8 lines
drivers/net/ethernet/intel/ice/ice_adapter.c line 39
Quoted 4 lines
drivers/net/ethernet/intel/ice/ice_adapter.c new file +4
-0+static void ice_adapter_free (struct ice_adapter *adapter) +{ +kfree (adapter); +}I would say that this is too thin wrapper for "kernel interface" (memory ptr) to warrant it, IOW just place kfree in place of
ice_adapter_free, that will also free us from the need to useDEFINE_FREE()
I am anticipating the need for struct members to destroy here. Eg. in order to replace ice_ptp_lock entirely, I will add a mutex, which will require mutex_destroy to be called in ice_adapter_free.
Quoted 12 lines
Quoted 12 lines
drivers/net/ethernet/intel/ice/ice_adapter.c new file +11
-0+ +DEFINE_FREE(ice_adapter_free, struct ice_adapter*, if (_T) ice_adapter_free(_T)) + +/** +* ice_adapter_get - Get a shared ice_adapter structure. +* @pdev: Pointer to the pci_dev whose driver is getting the ice_adapter. +* +* Gets a pointer to a shared ice_adapter structure. Physical functions (PFs) +* of the same multi-function PCI device share one ice_adapter structure. +* The ice_adapter is reference-counted. The PF driver must use ice_adapter_put +* to release its reference.
Quoted 8 lines
drivers/net/ethernet/intel/ice/ice_adapter.c line 54
Quoted 4 lines
drivers/net/ethernet/intel/ice/ice_adapter.c new file +4
-0+* +* Context: Process, may sleep. +* Return: Pointer to ice_adapter on success. +* ERR_PTR() on error. -ENOMEM is the only possible error. that's inconvenient, to the point that it will be better to have a dummy static entry used for this purpose, but I see that this is something broader that this particular use case, so - feel free to ignore
Perhaps I don't get what you mean by a static entry. Maybe a static singleton instance of struct ice_adapter? I don't want that, because I can have multiple E810 NICs in the system and I don't want them to share anything unnecessarily.
Besides, this allocates only a small amount of memory and the allocation is GFP_KERNEL. It won't fail under any realistic scenario (afaik, the "too small to fail" rule still holds in the kernel). This is called only from ice_probe, which surely allocates much more memory. I am not calling this every time I need to access the ice_adapter. I'm keeping a pointer in ice_pf.
Quoted 17 lines
Quoted 17 lines
drivers/net/ethernet/intel/ice/ice_adapter.c new file +17
-0+*/ +struct ice_adapter *ice_adapter_get (const struct pci_dev *pdev) +{ + struct ice_adapter *ret, __free(ice_adapter_free) *adapter = NULL; +unsigned long index =ice_adapter_index (pdev); + + adapter =ice_adapter_new (); +if (!adapter) +return ERR_PTR (-ENOMEM ); + +xa_lock (&ice_adapters); + ret =__xa_cmpxchg (&ice_adapters, index,NULL , adapter,GFP_KERNEL ); +if (xa_is_err (ret)) { + ret =ERR_PTR (xa_err (ret)); + goto unlock; + } +if (ret) {
Quoted 8 lines
drivers/net/ethernet/intel/ice/ice_adapter.c line 75
Quoted 4 lines
drivers/net/ethernet/intel/ice/ice_adapter.c new file +4
-0+refcount_inc (&ret->refcount); + goto unlock; + } + ret =no_free_ptr (adapter);nice solution, but this is an idiom that we want across the kernel instead of opting out of auto management in such cases as this one? (esp that you have open-coded locking anyway)
I will follow up by changing the xa_lock usage to a guard if my recently proposed patch ("xarray: add guard definitions for xa_lock") gets accepted:
https://lore.kernel.org/lkml/[email protected]/
Quoted 3 lines
I would expect to have explicit two stores (first to ensure index is present, second to overwrite entry if null) easier than cmpxchg
+ unneeded allocation (that could cause whole function to fail!)
For reasons mentioned above, I am not worried about the allocation failing. I am afraid moving away from the cmpxchg approach would force me to either:
- Re-add the additional mutex I had in v1 of this series and that
Jiri Pirko asked me to remove and rely on xa_lock; or
- Allocate ice_adapter under xa_lock, i.e. with GFP_ATOMIC. That
would only make running into ENOMEM more likely.
Quoted 49 lines
Quoted 49 lines
drivers/net/ethernet/intel/ice/ice_adapter.c new file +32
-0+unlock: +xa_unlock (&ice_adapters); +return ret; +} + +/** +* ice_adapter_put - Release a reference to the shared ice_adapter structure. +* @pdev: Pointer to the pci_dev whose driver is releasing the ice_adapter. +* +* Releases the reference to ice_adapter previously obtained with +* ice_adapter_get. +* +* Context: Any. +*/ +void ice_adapter_put (const struct pci_dev *pdev) +{ +unsigned long index =ice_adapter_index (pdev); +struct ice_adapter *adapter; + +xa_lock (&ice_adapters); + adapter =xa_load (&ice_adapters, index); +if (WARN_ON (!adapter)) + goto unlock; + +if (!refcount_dec_and_test (&adapter->refcount)) + goto unlock; + +WARN_ON (__xa_erase (&ice_adapters, index) != adapter); +ice_adapter_free (adapter); +unlock: +xa_unlock (&ice_adapters); +}drivers/net/ethernet/intel/ice/ice_adapter.h new file +11
-0@@ -0,0 +1,22 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* SPDX-FileCopyrightText: Copyright Red Hat */ + +#ifndef _ICE_ADAPTER_H_ +#define _ICE_ADAPTER_H_ + +#include <linux/refcount_types.h> + +struct pci_dev ; + +/**
Quoted 8 lines
drivers/net/ethernet/intel/ice/ice_adapter.h line 16
Quoted 5 lines
drivers/net/ethernet/intel/ice/ice_adapter.h new file +5
-0+* struct ice_adapter - PCI adapter resources shared across PFs +* @refcount: Reference count. struct ice_pf objects hold the references. +*/ +struct ice_adapter { +refcount_t refcount;this is refcounted always under a lock, so could be plain "int", but not a big deal
Yes, I know, but the over/under-flow checks provided by refcount_t keep me warm and fuzzy :)
Quoted 48 lines
Quoted 47 lines
drivers/net/ethernet/intel/ice/ice_adapter.h new file +6
-0+}; + +struct ice_adapter *ice_adapter_get (const struct pci_dev *pdev); +void ice_adapter_put (const struct pci_dev *pdev); + +#endif /* _ICE_ADAPTER_H */ drivers/net/ethernet/intel/ice/ice_main.c +8
-0@@ -5093,6 +5093,7 @@ static int ice_probe(struct pci_dev *pdev, const struct pci_device_id __always_unused *ent) { struct device *dev = &pdev->dev; +struct ice_adapter *adapter;struct ice_pf *pf;struct ice_hw *hw;int err;@@ -5145,7 +5146,12 @@ ice_probe(struct pci_dev *pdev, const struct pci_device_id __always_unused *ent)pci_set_master (pdev); + adapter =ice_adapter_get (pdev); +if (IS_ERR (adapter)) +return PTR_ERR (adapter); + pf->pdev = pdev; + pf->adapter = adapter;pci_set_drvdata (pdev, pf);set_bit (ICE_DOWN , pf->state);/* Disable service task until DOWN bit is cleared */ @@ -5196,6 +5202,7 @@ ice_probe(struct pci_dev *pdev, const struct pci_device_id __always_unused *ent) err_load:ice_deinit (pf); err_init: +ice_adapter_put (pdev);pci_disable_device (pdev);return err; }@@ -5302,6 +5309,7 @@ static void ice_remove(struct pci_dev *pdev)ice_setup_mc_magic_wake (pf);ice_set_wake (pf); +ice_adapter_put (pdev);pci_disable_device (pdev); }
Przemek Kitszel
maintainerRe: [Intel-wired-lan] [PATCH net-next v3 1/3] ice: add ice_adapter for shared data across PFs on the same NIC
Quoted 209 lines · Michal Schmidt
Quoted 11 lines · Przemek Kitszel
Quoted 10 lines · Michal Schmidt
There is a need for synchronization between ice PFs on the same physical adapter.
Add a "struct
ice_adapter" for holding data shared between PFs of the same multifunction PCI device. The struct is refcounted - eachice_pfholds a reference to it.Its first use will be for PTP. I expect it will be useful also to improve the ugliness that is
ice_prot_id_tbl.Quoted 8 lines
Quoted 1 line
Signed-off-by Michal Schmidt <[email protected]>Thank you very much for this series, we have spotted the need for something like that very recently, I have already pinged our PTP folks to take a look. (+CC Sergey)
Why not wipe
ice_ptp_lock()entirely?
(could be left for Intel folks though)I am doing this too, just not yet in this series I posted, because I did not want to expand the scope of the series between reviews. See my gitlab branch I linked in the cover letter, specifically this patch:
https://gitlab.com/mschmidt2/linux/-/commit/89a1bd2904ac8b0614bcfc2fce464bf5f60b0f0cQuoted 77 lines
please find the usual code related feedback inline (again, I really appreciate and I am grateful for this series)
Quoted 60 lines
--- drivers/net/ethernet/intel/ice/Makefile | 3 +- drivers/net/ethernet/intel/ice/ice.h | 2 + drivers/net/ethernet/intel/ice/ice_adapter.c | 107 +++++++++++++++++++ drivers/net/ethernet/intel/ice/ice_adapter.h | 22 ++++ drivers/net/ethernet/intel/ice/ice_main.c | 8 ++ 5 files changed, 141 insertions(+), 1 deletion(-)create mode 100644
drivers/net/ethernet/intel/ice/ice_adapter.ccreate mode 100644drivers/net/ethernet/intel/ice/ice_adapter.hdrivers/net/ethernet/intel/ice/Makefile +2
-1@@ -36,7 +36,8 @@ ice-y := ice_main.o \ ice_repr.o \ ice_tc_lib.o \ ice_fwlog.o \ - ice_debugfs.o + ice_debugfs.o \ + ice_adapter.o ice-$(CONFIG_PCI_IOV) += \ ice_sriov.o \ ice_virtchnl.o \drivers/net/ethernet/intel/ice/ice.h +2
-0@@ -77,6 +77,7 @@#include "ice_gnss.h" #include "ice_irq.h" #include "ice_dpll.h" +#include "ice_adapter.h" #define ICE_BAR0 0#define ICE_REQ_DESC_MULTIPLE 32@@ -544,6 +545,7 @@ struct ice_agg_node {struct ice_pf {struct pci_dev *pdev; +struct ice_adapter *adapter;struct devlink_region *nvm_region;struct devlink_region *sram_region;drivers/net/ethernet/intel/ice/ice_adapter.c new file +10
-0@@ -0,0 +1,107 @@ +// SPDX-License-Identifier: GPL-2.0-only +// SPDX-FileCopyrightText: Copyright Red Hat + +#include <linux/cleanup.h> +#include <linux/mutex.h> +#include <linux/pci.h> +#include <linux/slab.h> +#include <linux/xarray.h> +#include "ice_adapter.h" +drivers/net/ethernet/intel/ice/ice_adapter.c line 20
Quoted 10 lines
drivers/net/ethernet/intel/ice/ice_adapter.c new file +10
-0+static DEFINE_XARRAY (ice_adapters); + +static unsigned long ice_adapter_index (const struct pci_dev *pdev) +{ +unsigned int domain =pci_domain_nr (pdev->bus); + +WARN_ON ((unsigned long )domain >> (BITS_PER_LONG -13 )); +return ((unsigned long )domain <<13 ) | + ((unsigned long )pdev->bus->number <<5 ) | +PCI_SLOT (pdev->devfn);xarray is free to use non-0-based indices, so this whole function could be simplified as:
Quoted 2 lines
/* note the
PCI_SLOT()call to clear function from devfn */ returnPCI_DEVID(pci_domain_nr(pdev->bus), PCI_SLOT(pdev->devfn));This is not equivalent. My function encodes three PCI numbers into the
index: domain, bus, slot.
Your version would have only: domain, slot. The bus number would be lost. And also, higher-than-16 bits of the domain would be lost unnecessarily.Quoted 15 lines
Quoted 15 lines
drivers/net/ethernet/intel/ice/ice_adapter.c new file +15
-0+} + +static struct ice_adapter *ice_adapter_new (void ) +{ +struct ice_adapter *adapter; + + adapter =kzalloc (sizeof (*adapter),GFP_KERNEL ); +if (!adapter) +return NULL ; + +refcount_set (&adapter->refcount,1 ); + +return adapter; +} +Quoted 8 lines
drivers/net/ethernet/intel/ice/ice_adapter.c line 39
Quoted 4 lines
drivers/net/ethernet/intel/ice/ice_adapter.c new file +4
-0+static void ice_adapter_free (struct ice_adapter *adapter) +{ +kfree (adapter); +}I would say that this is too thin wrapper for "kernel interface" (memory ptr) to warrant it, IOW just place kfree in place of
ice_adapter_free, that will also free us from the need to useDEFINE_FREE()I am anticipating the need for struct members to destroy here. Eg. in order to replace
ice_ptp_lockentirely, I will add a mutex, which will requiremutex_destroyto be called inice_adapter_free.Quoted 12 lines
Quoted 12 lines
drivers/net/ethernet/intel/ice/ice_adapter.c new file +11
-0+ +DEFINE_FREE(ice_adapter_free, struct ice_adapter*, if (_T) ice_adapter_free(_T)) + +/** +* ice_adapter_get - Get a shared ice_adapter structure. +* @pdev: Pointer to the pci_dev whose driver is getting the ice_adapter. +* +* Gets a pointer to a shared ice_adapter structure. Physical functions (PFs) +* of the same multi-function PCI device share one ice_adapter structure. +* The ice_adapter is reference-counted. The PF driver must use ice_adapter_put +* to release its reference. Quoted 8 lines
drivers/net/ethernet/intel/ice/ice_adapter.c line 54
Quoted 4 lines
drivers/net/ethernet/intel/ice/ice_adapter.c new file +4
-0+* +* Context: Process, may sleep. +* Return: Pointer to ice_adapter on success. +* ERR_PTR() on error. -ENOMEM is the only possible error. that's inconvenient, to the point that it will be better to have a dummy static entry used for this purpose, but I see that this is something broader that this particular use case, so - feel free to ignore
Perhaps I don't get what you mean by a static entry. Maybe a static singleton instance of struct
ice_adapter? I don't want that, because I can have multiple E810 NICs in the system and I don't want them to share anything unnecessarily.Besides, this allocates only a small amount of memory and the allocation is
GFP_KERNEL. It won't fail under any realistic scenario (afaik, the "too small to fail" rule still holds in the kernel). This is called only fromice_probe, which surely allocates much more memory. I am not calling this every time I need to access theice_adapter. I'm keeping a pointer inice_pf.Quoted 17 lines
Quoted 17 lines
drivers/net/ethernet/intel/ice/ice_adapter.c new file +17
-0+*/ +struct ice_adapter *ice_adapter_get (const struct pci_dev *pdev) +{ + struct ice_adapter *ret, __free(ice_adapter_free) *adapter = NULL; +unsigned long index =ice_adapter_index (pdev); + + adapter =ice_adapter_new (); +if (!adapter) +return ERR_PTR (-ENOMEM ); + +xa_lock (&ice_adapters); + ret =__xa_cmpxchg (&ice_adapters, index,NULL , adapter,GFP_KERNEL ); +if (xa_is_err (ret)) { + ret =ERR_PTR (xa_err (ret)); + goto unlock; + } +if (ret) {Quoted 8 lines
drivers/net/ethernet/intel/ice/ice_adapter.c line 75
Quoted 4 lines
drivers/net/ethernet/intel/ice/ice_adapter.c new file +4
-0+refcount_inc (&ret->refcount); + goto unlock; + } + ret =no_free_ptr (adapter);nice solution, but this is an idiom that we want across the kernel instead of opting out of auto management in such cases as this one? (esp that you have open-coded locking anyway)
I will follow up by changing the
xa_lockusage to a guard if my recently proposed patch ("xarray: add guard definitions forxa_lock") gets accepted:
https://lore.kernel.org/lkml/[email protected]/Quoted 3 lines
I would expect to have explicit two stores (first to ensure index is present, second to overwrite entry if null) easier than cmpxchg
+ unneeded allocation (that could cause whole function to fail!)
Quoted 6 lines
For reasons mentioned above, I am not worried about the allocation failing. I am afraid moving away from the cmpxchg approach would force me to either:
- Re-add the additional mutex I had in v1 of this series and that
Jiri Pirko asked me to remove and rely onxa_lock; or
- Allocateice_adapterunderxa_lock, i.e. withGFP_ATOMIC. That
would only make running into ENOMEM more likely.
good point, thank for explanation :)
[...]
Quoted 35 lines · Michal Schmidt
This series removes the use of the heavy-weight PTP hardware semaphore in the gettimex64 path. Instead, serialization of access to the time register is done using a host-side spinlock. The timer hardware is shared between PFs on the PCI adapter, so the spinlock must be shared between
ice_pfinstances too.Replacing the PTP hardware semaphore entirely with a mutex is also possible and you can see it done in my git branch[1], but I am not posting those patches yet to keep the scope of this series limited.
[1] https://gitlab.com/mschmidt2/linux/-/commits/ice-ptp-host-side-lock-9
v3:
- Longer variable name ("a" -> "adapter").
- Propagate xarray error inice_adapter_getwithERR_PTR.
- Added kernel-doc comments for ice_adapter_{get,put}.v2:
- Patch 1: Rely on xarray's own lock. (Suggested by Jiri Pirko)
- Patch 2: Do not use*_irqsavewithptp_gltsyn_time_lock, as it's used only in process contexts.Michal Schmidt (3):
ice: addice_adapterfor shared data across PFs on the same NIC
ice: avoid the PTP hardware semaphore in gettimex64 path
ice: foldice_ptp_read_timeintoice_ptp_gettimex64drivers/net/ethernet/intel/ice/Makefile | 3 +- drivers/net/ethernet/intel/ice/ice.h | 2 + drivers/net/ethernet/intel/ice/ice_adapter.c | 109 +++++++++++++++++++ drivers/net/ethernet/intel/ice/ice_adapter.h | 28 +++++ drivers/net/ethernet/intel/ice/ice_main.c | 8 ++ drivers/net/ethernet/intel/ice/ice_ptp.c | 33 +----- drivers/net/ethernet/intel/ice/ice_ptp_hw.c | 3 + 7 files changed, 156 insertions(+), 30 deletions(-)
Quoted 3 lines
create mode 100644
drivers/net/ethernet/intel/ice/ice_adapter.ccreate mode 100644drivers/net/ethernet/intel/ice/ice_adapter.h