Re: [PATCH v3 2/5] iommu/amd: Introduce helper functions to access and update 256-bit DTE

From: Uros Bizjak
Date: Fri Sep 06 2024 - 15:31:22 EST


On Fri, Sep 6, 2024 at 5:53 PM Jacob Pan <jacob.pan@xxxxxxxxxxxxxxxxxxx> wrote:

> > +static void update_dte256(struct amd_iommu *iommu, struct
> > iommu_dev_data *dev_data,
> > + struct dev_table_entry *new)
> > +{
> > + struct dev_table_entry *dev_table = get_dev_table(iommu);
> > + struct dev_table_entry *ptr = &dev_table[dev_data->devid];
> > + struct dev_table_entry old;
> > + u128 tmp;
> > +
> > + lockdep_assert_held(&dev_data->dte_lock);
> > +
> > + old.data128[0] = ptr->data128[0];
> > + old.data128[1] = ptr->data128[1];
> > +
> > + tmp = cmpxchg128(&ptr->data128[1], old.data128[1], new->data128[1]);
> > + if (tmp == old.data128[1]) {
> If you are able to deal with the failure, why not use try_cmpxchg128
> for the hi 128 bit also? It is more efficient.

Indeed, just write:

if (try_cmpxchg128(&ptr->data128[1], &old.data128[1], new->data128[1])) {

to substitute the last two lines above.

Please also note that try_cmpxchg128() updates its second argument on failure.

Uros.