Re: [PATCH v6 02/25] iommu/ioasid: Add ioasid references

Jacob Pan
2020-05-04T15:21:00Z

On Mon, 4 May 2020 16:25:48 +0200
Jean-Philippe Brucker <jean-philippe@linaro.org> wrote:

> On Thu, Apr 30, 2020 at 11:39:31AM -0700, Jacob Pan wrote:
> > > +/**
> > > + * ioasid_get - obtain a reference to the IOASID
> > > + */
> > > +void ioasid_get(ioasid_t ioasid)
> > why void? what if the ioasid is not valid.
>
> My intended use was for the caller to get an additional reference when
> they're already holding one. So this should always succeed and I'd
> prefer a WARN_ON if the ioasid isn't valid rather than returning an
> error. But if you intend to add a state to ioasids between dropping
> refcount and free, then a return value makes sense.
>
Yes, a WARN_ON will do. No need for return value for now.

> Thanks,
> Jean
>
> >
> > > +{
> > > + struct ioasid_data *ioasid_data;
> > > +
> > > + spin_lock(&ioasid_allocator_lock);
> > > + ioasid_data = xa_load(&active_allocator->xa, ioasid);
> > > + if (ioasid_data)
> > > + refcount_inc(&ioasid_data->refs);
> > > + spin_unlock(&ioasid_allocator_lock);
> > > +}
> > > +EXPORT_SYMBOL_GPL(ioasid_get);
> > > +
> > > /**
> > > * ioasid_free - Free an IOASID
> > > * @ioasid: the ID to remove
> > > + *
> > > + * Put a reference to the IOASID, free it when the number of
> > > references drops to
> > > + * zero.
> > > + *
> > > + * Return: %true if the IOASID was freed, %false otherwise.
> > > */
> > > -void ioasid_free(ioasid_t ioasid)
> > > +bool ioasid_free(ioasid_t ioasid)
> > > {
> > > + bool free = false;
> > > struct ioasid_data *ioasid_data;
> > >
> > > spin_lock(&ioasid_allocator_lock);
> > > @@ -360,6 +383,10 @@ void ioasid_free(ioasid_t ioasid)
> > > goto exit_unlock;
> > > }
> > >
> > > + free = refcount_dec_and_test(&ioasid_data->refs);
> > > + if (!free)
> > > + goto exit_unlock;
> > > +
> > Just FYI, we may need to add states for the IOASID, i.g. mark the
> > IOASID inactive after free. And prohibit ioasid_get() after freed.
> > For VT-d, this is useful when KVM queries the IOASID.
> >
> > > active_allocator->ops->free(ioasid,
> > > active_allocator->ops->pdata); /* Custom allocator needs
> > > additional steps to free the xa element */ if
> > > (active_allocator->flags & IOASID_ALLOCATOR_CUSTOM) { @@ -369,6
> > > +396,7 @@ void ioasid_free(ioasid_t ioasid)
> > > exit_unlock:
> > > spin_unlock(&ioasid_allocator_lock);
> > > + return free;
> > > }
> > > EXPORT_SYMBOL_GPL(ioasid_free);
> > >
> >
> > [Jacob Pan]

[Jacob Pan]