Skip to content

Commit 841c950

Browse files
jacob-kellerJeff Kirsher
authored andcommitted
i40e/i40evf: use cmpxchg64 when updating private flags in ethtool
When a user gives an invalid command to change a private flag which is not supported, either because it is read-only, or the device is not capable of the feature, we simply ignore the request. A naive solution would simply be to report error codes when one of the flags was not supported. However, this causes problems because it makes the operation not atomic. If a user requests multiple private flags together at once we could end up changing one before failing at the second flag. We can do a bit better if we instead update a temporary copy of the flags variable in the loop, and then copy it into place after. If we aren't careful this has the pitfall of potentially silently overwriting any changes caused by other threads. Avoid this by using cmpxchg64 which will compare and swap the flags variable only if it currently matched the old value. We'll report -EAGAIN in the (hopefully rare!) case where the cmpxchg64 fails. This ensures that we can properly report when flags are not supported in an atomic fashion without the risk of overwriting other threads changes. Signed-off-by: Jacob Keller <jacob.e.keller@intel.com> Tested-by: Andrew Bowers <andrewx.bowers@intel.com> Signed-off-by: Jeff Kirsher <jeffrey.t.kirsher@intel.com>
1 parent 10a955f commit 841c950

2 files changed

Lines changed: 70 additions & 28 deletions

File tree

‎drivers/net/ethernet/intel/i40e/i40e_ethtool.c‎

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4069,23 +4069,26 @@ static int i40e_set_priv_flags(struct net_device *dev, u32 flags)
40694069
struct i40e_netdev_priv *np = netdev_priv(dev);
40704070
struct i40e_vsi *vsi = np->vsi;
40714071
struct i40e_pf *pf = vsi->back;
4072-
u64 changed_flags;
4072+
u64 orig_flags, new_flags, changed_flags;
40734073
u32 i, j;
40744074

4075-
changed_flags = pf->flags;
4075+
orig_flags = READ_ONCE(pf->flags);
4076+
new_flags = orig_flags;
40764077

40774078
for (i = 0; i < I40E_PRIV_FLAGS_STR_LEN; i++) {
40784079
const struct i40e_priv_flags *priv_flags;
40794080

40804081
priv_flags = &i40e_gstrings_priv_flags[i];
40814082

4082-
if (priv_flags->read_only)
4083-
continue;
4084-
40854083
if (flags & BIT(i))
4086-
pf->flags |= priv_flags->flag;
4084+
new_flags |= priv_flags->flag;
40874085
else
4088-
pf->flags &= ~(priv_flags->flag);
4086+
new_flags &= ~(priv_flags->flag);
4087+
4088+
/* If this is a read-only flag, it can't be changed */
4089+
if (priv_flags->read_only &&
4090+
((orig_flags ^ new_flags) & ~BIT(i)))
4091+
return -EOPNOTSUPP;
40894092
}
40904093

40914094
if (pf->hw.pf_id != 0)
@@ -4096,18 +4099,40 @@ static int i40e_set_priv_flags(struct net_device *dev, u32 flags)
40964099

40974100
priv_flags = &i40e_gl_gstrings_priv_flags[j];
40984101

4099-
if (priv_flags->read_only)
4100-
continue;
4101-
41024102
if (flags & BIT(i + j))
4103-
pf->flags |= priv_flags->flag;
4103+
new_flags |= priv_flags->flag;
41044104
else
4105-
pf->flags &= ~(priv_flags->flag);
4105+
new_flags &= ~(priv_flags->flag);
4106+
4107+
/* If this is a read-only flag, it can't be changed */
4108+
if (priv_flags->read_only &&
4109+
((orig_flags ^ new_flags) & ~BIT(i)))
4110+
return -EOPNOTSUPP;
41064111
}
41074112

41084113
flags_complete:
4109-
/* check for flags that changed */
4110-
changed_flags ^= pf->flags;
4114+
/* Before we finalize any flag changes, we need to perform some
4115+
* checks to ensure that the changes are supported and safe.
4116+
*/
4117+
4118+
/* ATR eviction is not supported on all devices */
4119+
if ((new_flags & I40E_FLAG_HW_ATR_EVICT_ENABLED) &&
4120+
!(pf->hw_features & I40E_HW_ATR_EVICT_CAPABLE))
4121+
return -EOPNOTSUPP;
4122+
4123+
/* Compare and exchange the new flags into place. If we failed, that
4124+
* is if cmpxchg64 returns anything but the old value, this means that
4125+
* something else has modified the flags variable since we copied it
4126+
* originally. We'll just punt with an error and log something in the
4127+
* message buffer.
4128+
*/
4129+
if (cmpxchg64(&pf->flags, orig_flags, new_flags) != orig_flags) {
4130+
dev_warn(&pf->pdev->dev,
4131+
"Unable to update pf->flags as it was modified by another thread...\n");
4132+
return -EAGAIN;
4133+
}
4134+
4135+
changed_flags = orig_flags ^ new_flags;
41114136

41124137
/* Process any additional changes needed as a result of flag changes.
41134138
* The changed_flags value reflects the list of bits that were
@@ -4121,10 +4146,6 @@ static int i40e_set_priv_flags(struct net_device *dev, u32 flags)
41214146
set_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
41224147
}
41234148

4124-
/* Only allow ATR evict on hardware that is capable of handling it */
4125-
if (!(pf->hw_features & I40E_HW_ATR_EVICT_CAPABLE))
4126-
pf->flags &= ~I40E_FLAG_HW_ATR_EVICT_ENABLED;
4127-
41284149
if (changed_flags & I40E_FLAG_TRUE_PROMISC_SUPPORT) {
41294150
u16 sw_flags = 0, valid_flags = 0;
41304151
int ret;

‎drivers/net/ethernet/intel/i40evf/i40evf_ethtool.c‎

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -258,29 +258,50 @@ static u32 i40evf_get_priv_flags(struct net_device *netdev)
258258
static int i40evf_set_priv_flags(struct net_device *netdev, u32 flags)
259259
{
260260
struct i40evf_adapter *adapter = netdev_priv(netdev);
261-
u64 changed_flags;
261+
u32 orig_flags, new_flags, changed_flags;
262262
u32 i;
263263

264-
changed_flags = adapter->flags;
264+
orig_flags = READ_ONCE(adapter->flags);
265+
new_flags = orig_flags;
265266

266267
for (i = 0; i < I40EVF_PRIV_FLAGS_STR_LEN; i++) {
267268
const struct i40evf_priv_flags *priv_flags;
268269

269270
priv_flags = &i40evf_gstrings_priv_flags[i];
270271

271-
if (priv_flags->read_only)
272-
continue;
273-
274272
if (flags & BIT(i))
275-
adapter->flags |= priv_flags->flag;
273+
new_flags |= priv_flags->flag;
276274
else
277-
adapter->flags &= ~(priv_flags->flag);
275+
new_flags &= ~(priv_flags->flag);
276+
277+
if (priv_flags->read_only &&
278+
((orig_flags ^ new_flags) & ~BIT(i)))
279+
return -EOPNOTSUPP;
280+
}
281+
282+
/* Before we finalize any flag changes, any checks which we need to
283+
* perform to determine if the new flags will be supported should go
284+
* here...
285+
*/
286+
287+
/* Compare and exchange the new flags into place. If we failed, that
288+
* is if cmpxchg returns anything but the old value, this means
289+
* something else must have modified the flags variable since we
290+
* copied it. We'll just punt with an error and log something in the
291+
* message buffer.
292+
*/
293+
if (cmpxchg(&adapter->flags, orig_flags, new_flags) != orig_flags) {
294+
dev_warn(&adapter->pdev->dev,
295+
"Unable to update adapter->flags as it was modified by another thread...\n");
296+
return -EAGAIN;
278297
}
279298

280-
/* check for flags that changed */
281-
changed_flags ^= adapter->flags;
299+
changed_flags = orig_flags ^ new_flags;
282300

283-
/* Process any additional changes needed as a result of flag changes. */
301+
/* Process any additional changes needed as a result of flag changes.
302+
* The changed_flags value reflects the list of bits that were changed
303+
* in the code above.
304+
*/
284305

285306
/* issue a reset to force legacy-rx change to take effect */
286307
if (changed_flags & I40EVF_FLAG_LEGACY_RX) {

0 commit comments

Comments
 (0)