Skip to content

Commit 0f0023c

Browse files
pkitszelanguy11
authored andcommitted
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>
1 parent fdd288e commit 0f0023c

File tree

1 file changed

+28
-32
lines changed

1 file changed

+28
-32
lines changed

drivers/net/ethernet/intel/ice/ice_adapter.c

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "ice_adapter.h"
1212

1313
static DEFINE_XARRAY(ice_adapters);
14+
static DEFINE_MUTEX(ice_adapters_mutex);
1415

1516
/* PCI bus number is 8 bits. Slot is 5 bits. Domain can have the rest. */
1617
#define INDEX_FIELD_DOMAIN GENMASK(BITS_PER_LONG - 1, 13)
@@ -47,8 +48,6 @@ static void ice_adapter_free(struct ice_adapter *adapter)
4748
kfree(adapter);
4849
}
4950

50-
DEFINE_FREE(ice_adapter_free, struct ice_adapter*, if (_T) ice_adapter_free(_T))
51-
5251
/**
5352
* ice_adapter_get - Get a shared ice_adapter structure.
5453
* @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))
6463
*/
6564
struct ice_adapter *ice_adapter_get(const struct pci_dev *pdev)
6665
{
67-
struct ice_adapter *ret, __free(ice_adapter_free) *adapter = NULL;
6866
unsigned long index = ice_adapter_index(pdev);
69-
70-
adapter = ice_adapter_new();
71-
if (!adapter)
72-
return ERR_PTR(-ENOMEM);
73-
74-
xa_lock(&ice_adapters);
75-
ret = __xa_cmpxchg(&ice_adapters, index, NULL, adapter, GFP_KERNEL);
76-
if (xa_is_err(ret)) {
77-
ret = ERR_PTR(xa_err(ret));
78-
goto unlock;
79-
}
80-
if (ret) {
81-
refcount_inc(&ret->refcount);
82-
goto unlock;
67+
struct ice_adapter *adapter;
68+
int err;
69+
70+
scoped_guard(mutex, &ice_adapters_mutex) {
71+
err = xa_insert(&ice_adapters, index, NULL, GFP_KERNEL);
72+
if (err == -EBUSY) {
73+
adapter = xa_load(&ice_adapters, index);
74+
refcount_inc(&adapter->refcount);
75+
return adapter;
76+
}
77+
if (err)
78+
return ERR_PTR(err);
79+
80+
adapter = ice_adapter_new();
81+
if (!adapter)
82+
return ERR_PTR(-ENOMEM);
83+
xa_store(&ice_adapters, index, adapter, GFP_KERNEL);
8384
}
84-
ret = no_free_ptr(adapter);
85-
unlock:
86-
xa_unlock(&ice_adapters);
87-
return ret;
85+
return adapter;
8886
}
8987

9088
/**
@@ -94,23 +92,21 @@ struct ice_adapter *ice_adapter_get(const struct pci_dev *pdev)
9492
* Releases the reference to ice_adapter previously obtained with
9593
* ice_adapter_get.
9694
*
97-
* Context: Any.
95+
* Context: Process, may sleep.
9896
*/
9997
void ice_adapter_put(const struct pci_dev *pdev)
10098
{
10199
unsigned long index = ice_adapter_index(pdev);
102100
struct ice_adapter *adapter;
103101

104-
xa_lock(&ice_adapters);
105-
adapter = xa_load(&ice_adapters, index);
106-
if (WARN_ON(!adapter))
107-
goto unlock;
102+
scoped_guard(mutex, &ice_adapters_mutex) {
103+
adapter = xa_load(&ice_adapters, index);
104+
if (WARN_ON(!adapter))
105+
return;
106+
if (!refcount_dec_and_test(&adapter->refcount))
107+
return;
108108

109-
if (!refcount_dec_and_test(&adapter->refcount))
110-
goto unlock;
111-
112-
WARN_ON(__xa_erase(&ice_adapters, index) != adapter);
109+
WARN_ON(xa_erase(&ice_adapters, index) != adapter);
110+
}
113111
ice_adapter_free(adapter);
114-
unlock:
115-
xa_unlock(&ice_adapters);
116112
}

0 commit comments

Comments
 (0)