mirror of
https://github.com/raspberrypi/linux.git
synced 2025-12-07 10:29:52 +00:00
ice: do not init struct ice_adapter more times than needed
Allocate and initialize struct ice_adapter object only once per physical card instead of once per port. This is not a big deal by now, but we want to extend this struct more and more in the near future. Our plans include PTP stuff and a devlink instance representing whole-device/physical card. Transactions requiring to be sleep-able (like those doing user (here ice) memory allocation) must be performed with an additional (on top of xarray) mutex. Adding it here removes need to xa_lock() manually. Since this commit is a reimplementation of ice_adapter_get(), a rather new scoped_guard() wrapper for locking is used to simplify the logic. It's worth to mention that xa_insert() use gives us both slot reservation and checks if it is already filled, what simplifies code a tiny bit. Reviewed-by: Wojciech Drewek <wojciech.drewek@intel.com> Signed-off-by: Przemek Kitszel <przemyslaw.kitszel@intel.com> Reviewed-by: Simon Horman <horms@kernel.org> Reviewed-by: Michal Schmidt <mschmidt@redhat.com> Tested-by: Pucha Himasekhar Reddy <himasekharx.reddy.pucha@intel.com> (A Contingent worker at Intel) Signed-off-by: Tony Nguyen <anthony.l.nguyen@intel.com>
This commit is contained in:
committed by
Tony Nguyen
parent
fdd288e9b7
commit
0f0023c649
@@ -11,6 +11,7 @@
|
|||||||
#include "ice_adapter.h"
|
#include "ice_adapter.h"
|
||||||
|
|
||||||
static DEFINE_XARRAY(ice_adapters);
|
static DEFINE_XARRAY(ice_adapters);
|
||||||
|
static DEFINE_MUTEX(ice_adapters_mutex);
|
||||||
|
|
||||||
/* PCI bus number is 8 bits. Slot is 5 bits. Domain can have the rest. */
|
/* PCI bus number is 8 bits. Slot is 5 bits. Domain can have the rest. */
|
||||||
#define INDEX_FIELD_DOMAIN GENMASK(BITS_PER_LONG - 1, 13)
|
#define INDEX_FIELD_DOMAIN GENMASK(BITS_PER_LONG - 1, 13)
|
||||||
@@ -47,8 +48,6 @@ static void ice_adapter_free(struct ice_adapter *adapter)
|
|||||||
kfree(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.
|
* ice_adapter_get - Get a shared ice_adapter structure.
|
||||||
* @pdev: Pointer to the pci_dev whose driver is getting the ice_adapter.
|
* @pdev: Pointer to the pci_dev whose driver is getting the ice_adapter.
|
||||||
@@ -64,27 +63,26 @@ DEFINE_FREE(ice_adapter_free, struct ice_adapter*, if (_T) ice_adapter_free(_T))
|
|||||||
*/
|
*/
|
||||||
struct ice_adapter *ice_adapter_get(const struct pci_dev *pdev)
|
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);
|
unsigned long index = ice_adapter_index(pdev);
|
||||||
|
struct ice_adapter *adapter;
|
||||||
|
int err;
|
||||||
|
|
||||||
adapter = ice_adapter_new();
|
scoped_guard(mutex, &ice_adapters_mutex) {
|
||||||
if (!adapter)
|
err = xa_insert(&ice_adapters, index, NULL, GFP_KERNEL);
|
||||||
return ERR_PTR(-ENOMEM);
|
if (err == -EBUSY) {
|
||||||
|
adapter = xa_load(&ice_adapters, index);
|
||||||
|
refcount_inc(&adapter->refcount);
|
||||||
|
return adapter;
|
||||||
|
}
|
||||||
|
if (err)
|
||||||
|
return ERR_PTR(err);
|
||||||
|
|
||||||
xa_lock(&ice_adapters);
|
adapter = ice_adapter_new();
|
||||||
ret = __xa_cmpxchg(&ice_adapters, index, NULL, adapter, GFP_KERNEL);
|
if (!adapter)
|
||||||
if (xa_is_err(ret)) {
|
return ERR_PTR(-ENOMEM);
|
||||||
ret = ERR_PTR(xa_err(ret));
|
xa_store(&ice_adapters, index, adapter, GFP_KERNEL);
|
||||||
goto unlock;
|
|
||||||
}
|
}
|
||||||
if (ret) {
|
return adapter;
|
||||||
refcount_inc(&ret->refcount);
|
|
||||||
goto unlock;
|
|
||||||
}
|
|
||||||
ret = no_free_ptr(adapter);
|
|
||||||
unlock:
|
|
||||||
xa_unlock(&ice_adapters);
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -94,23 +92,21 @@ unlock:
|
|||||||
* Releases the reference to ice_adapter previously obtained with
|
* Releases the reference to ice_adapter previously obtained with
|
||||||
* ice_adapter_get.
|
* ice_adapter_get.
|
||||||
*
|
*
|
||||||
* Context: Any.
|
* Context: Process, may sleep.
|
||||||
*/
|
*/
|
||||||
void ice_adapter_put(const struct pci_dev *pdev)
|
void ice_adapter_put(const struct pci_dev *pdev)
|
||||||
{
|
{
|
||||||
unsigned long index = ice_adapter_index(pdev);
|
unsigned long index = ice_adapter_index(pdev);
|
||||||
struct ice_adapter *adapter;
|
struct ice_adapter *adapter;
|
||||||
|
|
||||||
xa_lock(&ice_adapters);
|
scoped_guard(mutex, &ice_adapters_mutex) {
|
||||||
adapter = xa_load(&ice_adapters, index);
|
adapter = xa_load(&ice_adapters, index);
|
||||||
if (WARN_ON(!adapter))
|
if (WARN_ON(!adapter))
|
||||||
goto unlock;
|
return;
|
||||||
|
if (!refcount_dec_and_test(&adapter->refcount))
|
||||||
|
return;
|
||||||
|
|
||||||
if (!refcount_dec_and_test(&adapter->refcount))
|
WARN_ON(xa_erase(&ice_adapters, index) != adapter);
|
||||||
goto unlock;
|
}
|
||||||
|
|
||||||
WARN_ON(__xa_erase(&ice_adapters, index) != adapter);
|
|
||||||
ice_adapter_free(adapter);
|
ice_adapter_free(adapter);
|
||||||
unlock:
|
|
||||||
xa_unlock(&ice_adapters);
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user