IOASID extensions for guest SVA
Subsystems (3)
Re: [PATCH v2 4/9] iommu/ioasid: Add reference couting functions
In reply to Jacob PanQuoted 38 lines · Jacob Pan
There can be multiple users of an IOASID, each user could have hardware contexts associated with the IOASID. In order to align lifecycles, reference counting is introduced in this patch. It is expected that when an IOASID is being freed, each user will drop a reference only after its context is cleared.
Signed-off-by Jacob Pan <[email protected]>--- drivers/iommu/ioasid.c | 113 +++++++++++++++++++++++++++++++++++++++++++++++++ include/linux/ioasid.h | 4 ++ 2 files changed, 117 insertions(+)drivers/iommu/ioasid.c +18
-0@@ -717,6 +717,119 @@ int ioasid_set_for_each_ioasid(struct ioasid_set *set,EXPORT_SYMBOL_GPL (ioasid_set_for_each_ioasid);/** +* IOASID refcounting rules +* - ioasid_alloc() set initial refcount to 1 +* +* - ioasid_free() decrement and test refcount. +* If refcount is 0, ioasid will be freed. Deleted from the system-wide +* xarray as well as per set xarray. The IOASID will be returned to the +* pool and available for new allocations. +* +* If recount is non-zero, mark IOASID as IOASID_STATE_FREE_PENDING. +* No new reference can be added. The IOASID is not returned to the pool +* for reuse. +* After free, ioasid_get() will return error but ioasid_find() and other +* non refcount adding APIs will continue to work until the last reference +* is dropped +* +* - ioasid_get() get a reference on an active IOASID +* +* - ioasid_put() decrement and test refcount of the IOASID.
drivers/iommu/ioasid.c int ioasid_set_for_each_ioasid(struct ioasid_set *set, · line 747
Quoted 10 lines
drivers/iommu/ioasid.c +10
-0+* If refcount is 0, ioasid will be freed. Deleted from the system-wide +* xarray as well as per set xarray. The IOASID will be returned to the +* pool and available for new allocations. +* Do nothing if refcount is non-zero. +* +* - ioasid_find() does not take reference, caller must hold reference +* +* ioasid_free() can be called multiple times without error until all refs are +* dropped. +*/
Since you already document this in ioasid.rst, I'm not sure the comment is necessary. Maybe the doc for _free/_put would be better in the function's documentation.
Quoted 15 lines
drivers/iommu/ioasid.c +15
-0+ +int ioasid_get_locked (struct ioasid_set *set,ioasid_t ioasid) +{ +struct ioasid_data *data; + + data =xa_load (&active_allocator->xa, ioasid); +if (!data) { +pr_err ("Trying to get unknown IOASID %u\n" , ioasid); +return -EINVAL ; + } +if (data->state ==IOASID_STATE_FREE_PENDING ) { +pr_err ("Trying to get IOASID being freed%u\n" , ioasid); +return -EBUSY ; + } +
drivers/iommu/ioasid.c int ioasid_set_for_each_ioasid(struct ioasid_set *set, · line 772
Quoted 10 lines
drivers/iommu/ioasid.c +10
-0+if (set && data->set != set) { +pr_err ("Trying to get IOASID not in set%u\n" , ioasid); +/* data found but does not belong to the set */ +return -EACCES ; + } +refcount_inc (&data->users); + +return 0 ; +} +EXPORT_SYMBOL_GPL (ioasid_get_locked);
Is it necessary to export the *_locked variant? Who'd call them and how would they acquire the lock?
drivers/iommu/ioasid.c int ioasid_set_for_each_ioasid(struct ioasid_set *set, · line 777
Quoted 5 lines
drivers/iommu/ioasid.c +5
-0+ +/** +* ioasid_get - Obtain a reference of an ioasid +* @set +* @ioasid
Can be dropped. The doc checker will throw a warning, though.
drivers/iommu/ioasid.c int ioasid_set_for_each_ioasid(struct ioasid_set *set, · line 783
Quoted 6 lines
drivers/iommu/ioasid.c +6
-0+* +* Check set ownership if @set is non-null. +*/ +int ioasid_get (struct ioasid_set *set,ioasid_t ioasid) +{ +int ret =0 ;
No need to initialize ret
Quoted 57 lines
drivers/iommu/ioasid.c +49
-0+ +spin_lock (&ioasid_allocator_lock); + ret =ioasid_get_locked (set, ioasid); +spin_unlock (&ioasid_allocator_lock); + +return ret; +} +EXPORT_SYMBOL_GPL (ioasid_get); + +void ioasid_put_locked (struct ioasid_set *set,ioasid_t ioasid) +{ +struct ioasid_data *data; + + data =xa_load (&active_allocator->xa, ioasid); +if (!data) { +pr_err ("Trying to put unknown IOASID %u\n" , ioasid); +return ; + } + +if (set && data->set != set) { +pr_err ("Trying to drop IOASID not in the set %u\n" , ioasid); +return ; + } + +if (!refcount_dec_and_test (&data->users)) { +pr_debug ("%s: IOASID %d has %d remainning users\n" , + __func__, ioasid,refcount_read (&data->users)); +return ; + } +ioasid_do_free (data); +} +EXPORT_SYMBOL_GPL (ioasid_put_locked); + +/** +* ioasid_put - Drop a reference of an ioasid +* @set +* @ioasid +* +* Check set ownership if @set is non-null. +*/ +void ioasid_put (struct ioasid_set *set,ioasid_t ioasid) +{ +spin_lock (&ioasid_allocator_lock); +ioasid_put_locked (set, ioasid); +spin_unlock (&ioasid_allocator_lock); +} +EXPORT_SYMBOL_GPL (ioasid_put); + +/** * ioasid_find - Find IOASID data * @set: the IOASID set * @ioasid: the IOASID to find include/linux/ioasid.h +0
-0
include/linux/ioasid.h int ioasid_attach_data(ioasid_t ioasid, void *data); · line 82
Quoted 7 lines
include/linux/ioasid.h +4
-0@@ -76,6 +76,10 @@ int ioasid_attach_data(ioasid_t ioasid, void *data);int ioasid_register_allocator (struct ioasid_allocator_ops *allocator);void ioasid_unregister_allocator (struct ioasid_allocator_ops *allocator);void ioasid_is_in_set (struct ioasid_set *set,ioasid_t ioasid); +int ioasid_get (struct ioasid_set *set,ioasid_t ioasid); +int ioasid_get_locked (struct ioasid_set *set,ioasid_t ioasid); +void ioasid_put (struct ioasid_set *set,ioasid_t ioasid); +void ioasid_put_locked (struct ioasid_set *set,ioasid_t ioasid);
Please also add the stubs for !CONFIG_IOASID.
Thanks,
Jean
Quoted 6 lines
include/linux/ioasid.h +0
-0int ioasid_set_for_each_ioasid (struct ioasid_set *sdata,void (*fn)(ioasid_t id,void *data),void *data);Signature · 1 line
2.7.4
Re: [PATCH v2 4/9] iommu/ioasid: Add reference couting functions
Quoted 55 lines · On Tue, 25 Aug 2020 12:19:37 +0200 Jean-Philippe Brucker…
Quoted 54 lines · Jacob Pan
There can be multiple users of an IOASID, each user could have hardware contexts associated with the IOASID. In order to align lifecycles, reference counting is introduced in this patch. It is expected that when an IOASID is being freed, each user will drop a reference only after its context is cleared.
Signed-off-by Jacob Pan <[email protected]>--- drivers/iommu/ioasid.c | 113+++++++++++++++++++++++++++++++++++++++++++++++++
include/linux/ioasid.h| 4 ++ 2 files changed, 117 insertions(+)drivers/iommu/ioasid.c +5
-0@@ -717,6 +717,119 @@ int ioasid_set_for_each_ioasid(structioasid_set *set,EXPORT_SYMBOL_GPL (ioasid_set_for_each_ioasid );/** +* IOASID refcounting rules +* - ioasid_alloc() set initial refcount to 1 +* +* - ioasid_free() decrement and test refcount. +* If refcount is 0, ioasid will be freed. Deleted from the system-wide
+ * xarray as well as per set xarray. The IOASID will be
returned to the
+ * pool and available for new allocations.
+ *
+ * If recount is non-zero, mark IOASID asIOASID_STATE_FREE_PENDING.
+ * No new reference can be added. The IOASID is not returned
to the pool
+ * for reuse.
+ * After free,ioasid_get()will return error butioasid_find()and other
+ * non refcount adding APIs will continue to work until the
last reference
+ * is dropped
+ *
+ * -ioasid_get()get a reference on an active IOASID
+ *
+ * -ioasid_put()decrement and test refcount of the IOASID.
+ * If refcount is 0, ioasid will be freed. Deleted from the
system-wide
+ * xarray as well as per set xarray. The IOASID will be
returned to the
+ * pool and available for new allocations.
+ * Do nothing if refcount is non-zero.
+ *
+ * -ioasid_find()does not take reference, caller must hold reference
+ *
Quoted 9 lines
Quoted 4 lines
+ *
ioasid_free()can be called multiple times without error until all refs are
+ * dropped.
+ */Since you already document this in
ioasid.rst, I'm not sure the comment is necessary. Maybe the doc for _free/_put would be better in the function's documentation.
good point. will move the free/put to doc.
Quoted 23 lines
Quoted 23 lines
drivers/iommu/ioasid.c +8
-0+ +int ioasid_get_locked (struct ioasid_set *set,ioasid_t ioasid) +{ +struct ioasid_data *data; + + data =xa_load (&active_allocator->xa, ioasid); +if (!data) { + pr_err("Trying to get unknown IOASID %u\n",ioasid);
drivers/iommu/ioasid.c +4
-0+return -EINVAL ; + } +if (data->state ==IOASID_STATE_FREE_PENDING ) { + pr_err("Trying to get IOASID being freed%u\n",ioasid);
drivers/iommu/ioasid.c +5
-0+return -EBUSY ; + } + +if (set && data->set != set) { + pr_err("Trying to get IOASID not in set%u\n",ioasid);
drivers/iommu/ioasid.c +3
-0+/* data found but does not belong to the set */ +return -EACCES ; + }
Quoted 9 lines
drivers/iommu/ioasid.c int ioasid_set_for_each_ioasid(struct ioasid_set *set, · line 772
Quoted 5 lines
drivers/iommu/ioasid.c +5
-0+refcount_inc (&data->users); + +return 0 ; +} +EXPORT_SYMBOL_GPL (ioasid_get_locked);Is it necessary to export the
*_lockedvariant? Who'd call them and how would they acquire the lock?
It is used by KVM inside the atomic notifier handler. Notifier chain is invoked under the lock.
Quoted 8 lines
drivers/iommu/ioasid.c int ioasid_set_for_each_ioasid(struct ioasid_set *set, · line 777
Quoted 5 lines
drivers/iommu/ioasid.c +5
-0+ +/** +* ioasid_get - Obtain a reference of an ioasid +* @set +* @ioasid Can be dropped. The doc checker will throw a warning, though.
yes, will do
Quoted 9 lines
drivers/iommu/ioasid.c int ioasid_set_for_each_ioasid(struct ioasid_set *set, · line 783
Quoted 6 lines
+ *
+ * Check set ownership if @set is non-null.
+ */drivers/iommu/ioasid.c +3
-0+int ioasid_get (struct ioasid_set *set,ioasid_t ioasid) +{ +int ret =0 ;No need to initialize ret
right,
Quoted 62 lines
Quoted 62 lines
drivers/iommu/ioasid.c +16
-0+ +spin_lock (&ioasid_allocator_lock); + ret =ioasid_get_locked (set, ioasid); +spin_unlock (&ioasid_allocator_lock); + +return ret; +} +EXPORT_SYMBOL_GPL (ioasid_get); + +void ioasid_put_locked (struct ioasid_set *set,ioasid_t ioasid) +{ +struct ioasid_data *data; + + data =xa_load (&active_allocator->xa, ioasid); +if (!data) { + pr_err("Trying to put unknown IOASID %u\n",ioasid);
drivers/iommu/ioasid.c +5
-0+return ; + } + +if (set && data->set != set) { + pr_err("Trying to drop IOASID not in the set%u\n", ioasid);
drivers/iommu/ioasid.c +6
-0+return ; + } + +if (!refcount_dec_and_test (&data->users)) { + pr_debug("%s: IOASID %d has %d remainning users\n", + __func__, ioasid,
refcount_read(&data->users));drivers/iommu/ioasid.c +22
-0+return ; + } +ioasid_do_free (data); +} +EXPORT_SYMBOL_GPL (ioasid_put_locked); + +/** +* ioasid_put - Drop a reference of an ioasid +* @set +* @ioasid +* +* Check set ownership if @set is non-null. +*/ +void ioasid_put (struct ioasid_set *set,ioasid_t ioasid) +{ +spin_lock (&ioasid_allocator_lock); +ioasid_put_locked (set, ioasid); +spin_unlock (&ioasid_allocator_lock); +} +EXPORT_SYMBOL_GPL (ioasid_put); + +/** * ioasid_find - Find IOASID data * @set: the IOASID set * @ioasid: the IOASID to find include/linux/ioasid.h +0
-0@@ -76,6 +76,10 @@ int ioasid_attach_data(ioasid_t ioasid, void *data); int ioasid_register_allocator(struct ioasid_allocator_ops *allocator); void ioasid_unregister_allocator(struct
Quoted 9 lines
Quoted 6 lines
ioasid_allocator_ops*allocator); voidioasid_is_in_set(struct ioasid_set *set, ioasid_t ioasid); +intioasid_get(struct ioasid_set *set, ioasid_t ioasid); +intioasid_get_locked(struct ioasid_set *set, ioasid_t ioasid); +voidioasid_put(struct ioasid_set *set, ioasid_t ioasid); +voidioasid_put_locked(struct ioasid_set *set, ioasid_t ioasid);Please also add the stubs for !CONFIG_IOASID.
yes, will do.
Thanks a lot!
Quoted 9 lines
Thanks,
JeanQuoted 6 lines
int ioasid_set_for_each_ioasid(struct
ioasid_set*sdata,include/linux/ioasid.h +0
-0void (*fn)(ioasid_t id,void *data), void *data);Signature · 1 line
2.7.4
Quoted 4 lines
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
[Jacob Pan]
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
Re: [PATCH v2 4/9] iommu/ioasid: Add reference couting functions
In reply to Jacob PanHi Jacob,
Quoted 19 lines · Jacob Pan
There can be multiple users of an IOASID, each user could have hardware contexts associated with the IOASID. In order to align lifecycles, reference counting is introduced in this patch. It is expected that when an IOASID is being freed, each user will drop a reference only after its context is cleared.
Signed-off-by Jacob Pan <[email protected]>--- drivers/iommu/ioasid.c | 113 +++++++++++++++++++++++++++++++++++++++++++++++++ include/linux/ioasid.h | 4 ++ 2 files changed, 117 insertions(+)drivers/iommu/ioasid.c +0
-0@@ -717,6 +717,119 @@ int ioasid_set_for_each_ioasid(struct ioasid_set *set,EXPORT_SYMBOL_GPL (ioasid_set_for_each_ioasid);
drivers/iommu/ioasid.c int ioasid_set_for_each_ioasid(struct ioasid_set *set, · line 728
Quoted 10 lines
drivers/iommu/ioasid.c +9
-0/** +* IOASID refcounting rules +* - ioasid_alloc() set initial refcount to 1 +* +* - ioasid_free() decrement and test refcount. +* If refcount is 0, ioasid will be freed. Deleted from the system-wide +* xarray as well as per set xarray. The IOASID will be returned to the +* pool and available for new allocations. +* +* If recount is non-zero, mark IOASID as IOASID_STATE_FREE_PENDING.
s/recount/refcount
drivers/iommu/ioasid.c int ioasid_set_for_each_ioasid(struct ioasid_set *set, · line 729
Quoted 1 line
drivers/iommu/ioasid.c +1
-0+* No new reference can be added. The IOASID is not returned to the pool
can be taken
drivers/iommu/ioasid.c int ioasid_set_for_each_ioasid(struct ioasid_set *set, · line 741
Quoted 12 lines
drivers/iommu/ioasid.c +12
-0+* for reuse. +* After free, ioasid_get() will return error but ioasid_find() and other +* non refcount adding APIs will continue to work until the last reference +* is dropped +* +* - ioasid_get() get a reference on an active IOASID +* +* - ioasid_put() decrement and test refcount of the IOASID. +* If refcount is 0, ioasid will be freed. Deleted from the system-wide +* xarray as well as per set xarray. The IOASID will be returned to the +* pool and available for new allocations. +* Do nothing if refcount is non-zero.
I would drop this last sentence
drivers/iommu/ioasid.c int ioasid_set_for_each_ioasid(struct ioasid_set *set, · line 743
Quoted 2 lines
drivers/iommu/ioasid.c +2
-0+* +* - ioasid_find() does not take reference, caller must hold reference
So can you use ioasid_find() once the state is IOASID_STATE_FREE_PENDING? According to Jean's reply, the "IOASID may be freed once ioasid_find() returns but not the returned data." So holding a ref is not mandated right?
Quoted 11 lines
drivers/iommu/ioasid.c +11
-0+* +* ioasid_free() can be called multiple times without error until all refs are +* dropped. +*/ + +int ioasid_get_locked (struct ioasid_set *set,ioasid_t ioasid) +{ +struct ioasid_data *data; + + data =xa_load (&active_allocator->xa, ioasid); +if (!data) {
drivers/iommu/ioasid.c int ioasid_set_for_each_ioasid(struct ioasid_set *set, · line 764
Quoted 10 lines
drivers/iommu/ioasid.c +10
-0+pr_err ("Trying to get unknown IOASID %u\n" , ioasid); +return -EINVAL ; + } +if (data->state ==IOASID_STATE_FREE_PENDING ) { +pr_err ("Trying to get IOASID being freed%u\n" , ioasid); +return -EBUSY ; + } + +if (set && data->set != set) { +pr_err ("Trying to get IOASID not in set%u\n" , ioasid);
maybe try to normalize your traces using always the same formatting for ioasids and ioasid sets. Also we can understand %u is the id of the set.
drivers/iommu/ioasid.c int ioasid_set_for_each_ioasid(struct ioasid_set *set, · line 765
Quoted 1 line
drivers/iommu/ioasid.c +1
-0+/* data found but does not belong to the set */
you can get rid of this comment
Quoted 29 lines
drivers/iommu/ioasid.c +29
-0+return -EACCES ; + } +refcount_inc (&data->users); + +return 0 ; +} +EXPORT_SYMBOL_GPL (ioasid_get_locked); + +/** +* ioasid_get - Obtain a reference of an ioasid +* @set +* @ioasid +* +* Check set ownership if @set is non-null. +*/ +int ioasid_get (struct ioasid_set *set,ioasid_t ioasid) +{ +int ret =0 ; + +spin_lock (&ioasid_allocator_lock); + ret =ioasid_get_locked (set, ioasid); +spin_unlock (&ioasid_allocator_lock); + +return ret; +} +EXPORT_SYMBOL_GPL (ioasid_get); + +void ioasid_put_locked (struct ioasid_set *set,ioasid_t ioasid) +{
drivers/iommu/ioasid.c int ioasid_set_for_each_ioasid(struct ioasid_set *set, · line 804
Quoted 10 lines
drivers/iommu/ioasid.c +10
-0+struct ioasid_data *data; + + data =xa_load (&active_allocator->xa, ioasid); +if (!data) { +pr_err ("Trying to put unknown IOASID %u\n" , ioasid); +return ; + } + +if (set && data->set != set) { +pr_err ("Trying to drop IOASID not in the set %u\n" , ioasid);
was set%u above
Quoted 37 lines
drivers/iommu/ioasid.c +28
-0+return ; + } + +if (!refcount_dec_and_test (&data->users)) { +pr_debug ("%s: IOASID %d has %d remainning users\n" , + __func__, ioasid,refcount_read (&data->users)); +return ; + } +ioasid_do_free (data); +} +EXPORT_SYMBOL_GPL (ioasid_put_locked); + +/** +* ioasid_put - Drop a reference of an ioasid +* @set +* @ioasid +* +* Check set ownership if @set is non-null. +*/ +void ioasid_put (struct ioasid_set *set,ioasid_t ioasid) +{ +spin_lock (&ioasid_allocator_lock); +ioasid_put_locked (set, ioasid); +spin_unlock (&ioasid_allocator_lock); +} +EXPORT_SYMBOL_GPL (ioasid_put); + +/** * ioasid_find - Find IOASID data * @set: the IOASID set * @ioasid: the IOASID to find include/linux/ioasid.h +0
-0
include/linux/ioasid.h int ioasid_attach_data(ioasid_t ioasid, void *data); · line 85
Quoted 10 lines
include/linux/ioasid.h +4
-0@@ -76,6 +76,10 @@ int ioasid_attach_data(ioasid_t ioasid, void *data);int ioasid_register_allocator (struct ioasid_allocator_ops *allocator);void ioasid_unregister_allocator (struct ioasid_allocator_ops *allocator);void ioasid_is_in_set (struct ioasid_set *set,ioasid_t ioasid); +int ioasid_get (struct ioasid_set *set,ioasid_t ioasid); +int ioasid_get_locked (struct ioasid_set *set,ioasid_t ioasid); +void ioasid_put (struct ioasid_set *set,ioasid_t ioasid); +void ioasid_put_locked (struct ioasid_set *set,ioasid_t ioasid);int ioasid_set_for_each_ioasid (struct ioasid_set *sdata,void (*fn)(ioasid_t id,void *data),void *data);
Thanks
Eric
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
Re: [PATCH v2 4/9] iommu/ioasid: Add reference couting functions
Quoted 36 lines · On Tue, 1 Sep 2020 14:13:00 +0200 Auger Eric <[email protected]> wrote:
Hi Jacob,
Quoted 25 lines · Jacob Pan
There can be multiple users of an IOASID, each user could have hardware contexts associated with the IOASID. In order to align lifecycles, reference counting is introduced in this patch. It is expected that when an IOASID is being freed, each user will drop a reference only after its context is cleared.
Signed-off-by Jacob Pan <[email protected]>--- drivers/iommu/ioasid.c | 113+++++++++++++++++++++++++++++++++++++++++++++++++
include/linux/ioasid.h| 4 ++ 2 files changed, 117 insertions(+)drivers/iommu/ioasid.c +5
-0@@ -717,6 +717,119 @@ int ioasid_set_for_each_ioasid(structioasid_set *set,EXPORT_SYMBOL_GPL (ioasid_set_for_each_ioasid );/** +* IOASID refcounting rules +* - ioasid_alloc() set initial refcount to 1 +* +* - ioasid_free() decrement and test refcount. +* If refcount is 0, ioasid will be freed. Deleted from the Quoted 7 lines
system-wide
+ * xarray as well as per set xarray. The IOASID will be
returned to the
+ * pool and available for new allocations.
+ *
+ * If recount is non-zero, mark IOASID asIOASID_STATE_FREE_PENDING.s/recount/refcount
Quoted 3 lines
Quoted 2 lines
+ * No new reference can be added. The IOASID is not returned
to the poolcan be taken
will do. and move to doc as Jean suggested.
Quoted 16 lines
Quoted 8 lines
+ * for reuse.
+ * After free,ioasid_get()will return error butioasid_find()and other
+ * non refcount adding APIs will continue to work until the
last reference
+ * is dropped
+ *
+ * -ioasid_get()get a reference on an active IOASIDQuoted 8 lines
+ *
+ * -ioasid_put()decrement and test refcount of the IOASID.
+ * If refcount is 0, ioasid will be freed. Deleted from the
system-wide
+ * xarray as well as per set xarray. The IOASID will be
returned to the
+ * pool and available for new allocations.
+ * Do nothing if refcount is non-zero.
Quoted 1 line
I would drop this last sentence
will do.
Quoted 7 lines
Quoted 3 lines
+ *
+ * -ioasid_find()does not take reference, caller must hold referenceSo can you use
ioasid_find()once the state isIOASID_STATE_FREE_PENDING? According to Jean's reply, the "IOASID may be freed onceioasid_find()returns but not the returned data." So holding a ref is not mandated right?
right, you can still find the private data in FREE_PENDING state. I will drop the comment.
Quoted 20 lines
Quoted 20 lines
+ *
+ *ioasid_free()can be called multiple times without error until all refs are
+ * dropped.
+ */drivers/iommu/ioasid.c +8
-0+ +int ioasid_get_locked (struct ioasid_set *set,ioasid_t ioasid) +{ +struct ioasid_data *data; + + data =xa_load (&active_allocator->xa, ioasid); +if (!data) { + pr_err("Trying to get unknown IOASID %u\n",ioasid);
drivers/iommu/ioasid.c +4
-0+return -EINVAL ; + } +if (data->state ==IOASID_STATE_FREE_PENDING ) { + pr_err("Trying to get IOASID being freed%u\n",ioasid);
drivers/iommu/ioasid.c +1
-0+return -EBUSY ;
Quoted 8 lines
Quoted 5 lines
drivers/iommu/ioasid.c +4
-0+ } + +if (set && data->set != set) { + pr_err("Trying to get IOASID not in set%u\n",ioasid);
maybe try to normalize your traces using always the same formatting for ioasids and ioasid sets. Also we can understand %u is the id of the set.
it is confusing, this is not the set ID. I will change to:pr_err("Trying to get IOASID %u outside the set\n", ioasid);
Quoted 36 lines
drivers/iommu/ioasid.c int ioasid_set_for_each_ioasid(struct ioasid_set *set, · line 765
Quoted 1 line
drivers/iommu/ioasid.c +1
-0+/* data found but does not belong to the set */ you can get rid of this comment
Quoted 34 lines
drivers/iommu/ioasid.c +34
-0+return -EACCES ; + } +refcount_inc (&data->users); + +return 0 ; +} +EXPORT_SYMBOL_GPL (ioasid_get_locked); + +/** +* ioasid_get - Obtain a reference of an ioasid +* @set +* @ioasid +* +* Check set ownership if @set is non-null. +*/ +int ioasid_get (struct ioasid_set *set,ioasid_t ioasid) +{ +int ret =0 ; + +spin_lock (&ioasid_allocator_lock); + ret =ioasid_get_locked (set, ioasid); +spin_unlock (&ioasid_allocator_lock); + +return ret; +} +EXPORT_SYMBOL_GPL (ioasid_get); + +void ioasid_put_locked (struct ioasid_set *set,ioasid_t ioasid) +{ +struct ioasid_data *data; + + data =xa_load (&active_allocator->xa, ioasid); +if (!data) { + pr_err("Trying to put unknown IOASID %u\n",
Quoted 8 lines
Quoted 7 lines
ioasid);
drivers/iommu/ioasid.c +5
-0+return ; + } + +if (set && data->set != set) { + pr_err("Trying to drop IOASID not in the set%u\n", ioasid);
was set%u above
yes, same change below?pr_err("Trying to drop IOASID %u outside the set\n", ioasid);
Quoted 50 lines
Quoted 39 lines
drivers/iommu/ioasid.c +6
-0+return ; + } + +if (!refcount_dec_and_test (&data->users)) { + pr_debug("%s: IOASID %d has %d remainning users\n", + __func__, ioasid,
refcount_read(&data->users));drivers/iommu/ioasid.c +22
-0+return ; + } +ioasid_do_free (data); +} +EXPORT_SYMBOL_GPL (ioasid_put_locked); + +/** +* ioasid_put - Drop a reference of an ioasid +* @set +* @ioasid +* +* Check set ownership if @set is non-null. +*/ +void ioasid_put (struct ioasid_set *set,ioasid_t ioasid) +{ +spin_lock (&ioasid_allocator_lock); +ioasid_put_locked (set, ioasid); +spin_unlock (&ioasid_allocator_lock); +} +EXPORT_SYMBOL_GPL (ioasid_put); + +/** * ioasid_find - Find IOASID data * @set: the IOASID set * @ioasid: the IOASID to find include/linux/ioasid.h +0
-0@@ -76,6 +76,10 @@ int ioasid_attach_data(ioasid_t ioasid, void *data); int ioasid_register_allocator(struct ioasid_allocator_ops *allocator); void ioasid_unregister_allocator(structQuoted 9 lines
ioasid_allocator_ops*allocator); voidioasid_is_in_set(struct ioasid_set *set, ioasid_t ioasid); +intioasid_get(struct ioasid_set *set, ioasid_t ioasid); +intioasid_get_locked(struct ioasid_set *set, ioasid_t ioasid); +voidioasid_put(struct ioasid_set *set, ioasid_t ioasid); +voidioasid_put_locked(struct ioasid_set *set, ioasid_t ioasid); intioasid_set_for_each_ioasid(struct ioasid_set *sdata, void (*fn)(ioasid_t id, void *data), void *data);Thanks
Quoted 6 lines
Eric
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
[Jacob Pan]
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
RE: [PATCH v2 4/9] iommu/ioasid: Add reference couting functions
In reply to Jacob PanHi Jacob,
Quoted 54 lines
-----Original Message-----
From: iommu [mailto:[email protected]] On Behalf Of Jacob Pan
Sent: 22 August 2020 05:35
To: [email protected]; LKML <[email protected]>; Jean-Philippe Brucker <[email protected]>; Lu Baolu <[email protected]>; Joerg Roedel <[email protected]>; David Woodhouse <[email protected]>
Cc: Tian, Kevin <[email protected]>; Raj Ashok <[email protected]>; Wu Hao <[email protected]>
Subject: [PATCH v2 4/9] iommu/ioasid: Add reference couting functionsThere can be multiple users of an IOASID, each user could have hardware contexts associated with the IOASID. In order to align lifecycles, reference counting is introduced in this patch. It is expected that when an IOASID is being freed, each user will drop a reference only after its context is cleared.
Signed-off-by Jacob Pan <[email protected]>--- drivers/iommu/ioasid.c | 113 +++++++++++++++++++++++++++++++++++++++++++++++++ include/linux/ioasid.h | 4 ++ 2 files changed, 117 insertions(+)drivers/iommu/ioasid.c +9
-0@@ -717,6 +717,119 @@ int ioasid_set_for_each_ioasid(struct ioasid_set *set,EXPORT_SYMBOL_GPL (ioasid_set_for_each_ioasid);/** +* IOASID refcounting rules +* - ioasid_alloc() set initial refcount to 1 +* +* - ioasid_free() decrement and test refcount. +* If refcount is 0, ioasid will be freed. Deleted from the system-wide +* xarray as well as per set xarray. The IOASID will be returned to the +* pool and available for new allocations. +* +* If recount is non-zero, mark IOASID as
IOASID_STATE_FREE_PENDING.
+ * No new reference can be added. The IOASID is not returned to the
pool+ * for reuse. + * After free, ioasid_get() will return error but ioasid_find() and other + * non refcount adding APIs will continue to work until the last reference + * is dropped+ *
+ * -ioasid_get()get a reference on an active IOASID
+ *
+ * -ioasid_put()decrement and test refcount of the IOASID.
Quoted 5 lines
+ * If refcount is 0, ioasid will be freed. Deleted from the system-wide + * xarray as well as per set xarray. The IOASID will be returned to the + * pool and available for new allocations. + * Do nothing if refcount is non-zero.+ *
Is it better to have a return for this based on whether ioasid is freed or not?
I was going through Jean's SMMUv3 SVA patches[1] and that one returns true if ioasid was freed. And that info is subsequently used to reset the pasid associated with a mm. Though, not sure that is still relevant or not.
Thanks,
Shameer
1. https://lore.kernel.org/linux-iommu/[email protected]/
Quoted 115 lines
+ * -
ioasid_find()does not take reference, caller must hold reference
+ *
+ *ioasid_free()can be called multiple times without error until all refs are
+ * dropped.
+ */drivers/iommu/ioasid.c +85
-0+ +int ioasid_get_locked (struct ioasid_set *set,ioasid_t ioasid) +{ +struct ioasid_data *data; + + data =xa_load (&active_allocator->xa, ioasid); +if (!data) { +pr_err ("Trying to get unknown IOASID %u\n" , ioasid); +return -EINVAL ; + } +if (data->state ==IOASID_STATE_FREE_PENDING ) { +pr_err ("Trying to get IOASID being freed%u\n" , ioasid); +return -EBUSY ; + } + +if (set && data->set != set) { +pr_err ("Trying to get IOASID not in set%u\n" , ioasid); +/* data found but does not belong to the set */ +return -EACCES ; + } +refcount_inc (&data->users); + +return 0 ; +} +EXPORT_SYMBOL_GPL (ioasid_get_locked); + +/** +* ioasid_get - Obtain a reference of an ioasid +* @set +* @ioasid +* +* Check set ownership if @set is non-null. +*/ +int ioasid_get (struct ioasid_set *set,ioasid_t ioasid) +{ +int ret =0 ; + +spin_lock (&ioasid_allocator_lock); + ret =ioasid_get_locked (set, ioasid); +spin_unlock (&ioasid_allocator_lock); + +return ret; +} +EXPORT_SYMBOL_GPL (ioasid_get); + +void ioasid_put_locked (struct ioasid_set *set,ioasid_t ioasid) +{ +struct ioasid_data *data; + + data =xa_load (&active_allocator->xa, ioasid); +if (!data) { +pr_err ("Trying to put unknown IOASID %u\n" , ioasid); +return ; + } + +if (set && data->set != set) { +pr_err ("Trying to drop IOASID not in the set %u\n" , ioasid); +return ; + } + +if (!refcount_dec_and_test (&data->users)) { +pr_debug ("%s: IOASID %d has %d remainning users\n" , + __func__, ioasid,refcount_read (&data->users)); +return ; + } +ioasid_do_free (data); +} +EXPORT_SYMBOL_GPL (ioasid_put_locked); + +/** +* ioasid_put - Drop a reference of an ioasid +* @set +* @ioasid +* +* Check set ownership if @set is non-null. +*/ +void ioasid_put (struct ioasid_set *set,ioasid_t ioasid) +{ +spin_lock (&ioasid_allocator_lock); +ioasid_put_locked (set, ioasid); +spin_unlock (&ioasid_allocator_lock); +} +EXPORT_SYMBOL_GPL (ioasid_put); + +/** * ioasid_find - Find IOASID data * @set: the IOASID set * @ioasid: the IOASID to find include/linux/ioasid.h +4
-1@@ -76,6 +76,10 @@ int ioasid_attach_data(ioasid_t ioasid, void *data);int ioasid_register_allocator (struct ioasid_allocator_ops *allocator);void ioasid_unregister_allocator (struct ioasid_allocator_ops *allocator);void ioasid_is_in_set (struct ioasid_set *set,ioasid_t ioasid); +int ioasid_get (struct ioasid_set *set,ioasid_t ioasid); +int ioasid_get_locked (struct ioasid_set *set,ioasid_t ioasid); +void ioasid_put (struct ioasid_set *set,ioasid_t ioasid); +void ioasid_put_locked (struct ioasid_set *set,ioasid_t ioasid);int ioasid_set_for_each_ioasid (struct ioasid_set *sdata,void (*fn)(ioasid_t id,void *data),void *data); --2.7.4
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
PATCH v2 5/9 iommu/ioasid: Introduce ioasid_set private ID
In reply to Jacob PanWhen an IOASID set is used for guest SVA, each VM will acquire its ioasid_set for IOASID allocations. IOASIDs within the VM must have a host/physical IOASID backing, mapping between guest and host IOASIDs can be non-identical. IOASID set private ID (SPID) is introduced in this patch to be used as guest IOASID. However, the concept of ioasid_set specific namespace is generic, thus named SPID.
As SPID namespace is within the IOASID set, the IOASID core can provide lookup services at both directions. SPIDs may not be allocated when its IOASID is allocated, the mapping between SPID and IOASID is usually established when a guest page table is bound to a host PASID.
--- drivers/iommu/ioasid.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++ include/linux/ioasid.h | 12 +++++++++++ 2 files changed, 66 insertions(+)
drivers/iommu/ioasid.c +54 -0
@@ -21,6 +21,7 @@ enum ioasid_state {* struct ioasid_data - Meta data about ioasid * * @id: Unique ID +* @spid: Private ID unique within a set * @users Number of active users * @state Track state of the IOASID * @set Meta data of the set this IOASID belongs to
@@ -29,6 +30,7 @@ enum ioasid_state {*/ struct ioasid_data {ioasid_t id; +ioasid_t spid;struct ioasid_set *set;refcount_t users;enum ioasid_state state;
@@ -326,6 +328,58 @@ int ioasid_attach_data(ioasid_t ioasid, void *data)EXPORT_SYMBOL_GPL (ioasid_attach_data);/** +* ioasid_attach_spid - Attach ioasid_set private ID to an IOASID +* +* @ioasid: the ID to attach +* @spid: the ioasid_set private ID of @ioasid +* +* For IOASID that is already allocated, private ID within the set can be +* attached via this API. Future lookup can be done via ioasid_find. +*/ +int ioasid_attach_spid (ioasid_t ioasid,ioasid_t spid) +{ +struct ioasid_data *ioasid_data; +int ret =0 ; + +spin_lock (&ioasid_allocator_lock); + ioasid_data =xa_load (&active_allocator->xa, ioasid); + +if (!ioasid_data) { +pr_err ("No IOASID entry %d to attach SPID %d\n" , + ioasid, spid); + ret = -ENOENT ; + goto done_unlock; + } + ioasid_data->spid = spid; + +done_unlock: +spin_unlock (&ioasid_allocator_lock); +return ret; +} +EXPORT_SYMBOL_GPL (ioasid_attach_spid); + +ioasid_t ioasid_find_by_spid (struct ioasid_set *set,ioasid_t spid) +{ +struct ioasid_data *entry; +unsigned long index; + +if (!xa_load (&ioasid_sets, set->sid)) { +pr_warn ("Invalid set\n" ); +return INVALID_IOASID ; + } + +xa_for_each (&set->xa, index, entry) { +if (spid == entry->spid) { +pr_debug ("Found ioasid %lu by spid %u\n" , index, spid); +refcount_inc (&entry->users); +return index; + } + } +return INVALID_IOASID ; +} +EXPORT_SYMBOL_GPL (ioasid_find_by_spid); + +/** * ioasid_alloc - Allocate an IOASID * @set: the IOASID set * @min: the minimum ID (inclusive)
include/linux/ioasid.h +12 -0
@@ -73,6 +73,8 @@ bool ioasid_is_active(ioasid_t ioasid);void *ioasid_find (struct ioasid_set *set,ioasid_t ioasid,bool (*getter)(void *));int ioasid_attach_data (ioasid_t ioasid,void *data); +int ioasid_attach_spid (ioasid_t ioasid,ioasid_t spid); +ioasid_t ioasid_find_by_spid (struct ioasid_set *set,ioasid_t spid);int ioasid_register_allocator (struct ioasid_allocator_ops *allocator);void ioasid_unregister_allocator (struct ioasid_allocator_ops *allocator);void ioasid_is_in_set (struct ioasid_set *set,ioasid_t ioasid);
@@ -136,5 +138,15 @@ static inline int ioasid_attach_data(ioasid_t ioasid, void *data)return -ENOTSUPP ; } +staic inline int ioasid_attach_spid(ioasid_t ioasid, ioasid_t spid) +{ +return -ENOTSUPP ; +} + +static inline ioasid_t ioasid_find_by_spid (struct ioasid_set *set,ioasid_t spid) +{ +return -ENOTSUPP ; +} +#endif /* CONFIG_IOASID */ #endif /* __LINUX_IOASID_H */
Signature · 1 line
2.7.4
kernel test robot
build botRe: [PATCH v2 5/9] iommu/ioasid: Introduce ioasid_set private ID
I love your patch! Yet something to improve:
Hi Jacob,
I love your patch! Yet something to improve:
[auto build test ERROR on iommu/next]
[also build test ERROR on linux/master linus/master v5.9-rc1 next-20200821] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]
url: https://github.com/0day-ci/linux/commits/Jacob-Pan/IOASID-extensions-for-guest-SVA/20200822-123111 base: https://git.kernel.org/pub/scm/linux/kernel/git/joro/iommu.git next config: ia64-randconfig-r003-20200822 (attached as .config) compiler: ia64-linux-gcc (GCC) 9.3.0
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross ARCH=ia64
If you fix the issue, kindly add following tag as appropriate
All errors (new ones prefixed by >>):
In file included from include/linux/iommu.h:16,
from include/linux/of_iommu.h:6, from drivers/of/device.c:7:
>> include/linux/ioasid.h:141:6: error: expected ';' before 'inline'
141 | staic inline int ioasid_attach_spid(ioasid_t ioasid, ioasid_t spid)
| ^
| ;
--
In file included from include/linux/iommu.h:16,
from drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c:34:
>> include/linux/ioasid.h:141:6: error: expected ';' before 'inline'
141 | staic inline int ioasid_attach_spid(ioasid_t ioasid, ioasid_t spid)
| ^
| ;
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c:1094:5: warning: no previous prototype for 'amdgpu_ttm_gart_bind' [-Wmissing-prototypes]
1094 | int amdgpu_ttm_gart_bind(struct amdgpu_device *adev,
| ^~~~~~~~~~~~~~~~~~~~
In file included from drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c:55:
drivers/gpu/drm/amd/amdgpu/amdgpu.h:190:18: warning: 'sched_policy' defined but not used [-Wunused-const-variable=]
190 | static const int sched_policy = KFD_SCHED_POLICY_HWS;
| ^~~~~~~~~~~~
In file included from drivers/gpu/drm/amd/amdgpu/../display/dc/dc_types.h:33,
from drivers/gpu/drm/amd/amdgpu/../display/dc/dm_services_types.h:30,
from drivers/gpu/drm/amd/amdgpu/../include/dm_pp_interface.h:26,
from drivers/gpu/drm/amd/amdgpu/amdgpu.h:65,
from drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c:55:
drivers/gpu/drm/amd/amdgpu/../display/include/fixed31_32.h:76:32: warning: 'dc_fixpt_ln2_div_2' defined but not used [-Wunused-const-variable=]
76 | static const struct fixed31_32 dc_fixpt_ln2_div_2 = { 1488522236LL };
| ^~~~~~~~~~~~~~~~~~
drivers/gpu/drm/amd/amdgpu/../display/include/fixed31_32.h:75:32: warning: 'dc_fixpt_ln2' defined but not used [-Wunused-const-variable=]
75 | static const struct fixed31_32 dc_fixpt_ln2 = { 2977044471LL };
| ^~~~~~~~~~~~
drivers/gpu/drm/amd/amdgpu/../display/include/fixed31_32.h:74:32: warning: 'dc_fixpt_e' defined but not used [-Wunused-const-variable=]
74 | static const struct fixed31_32 dc_fixpt_e = { 11674931555LL };
| ^~~~~~~~~~
drivers/gpu/drm/amd/amdgpu/../display/include/fixed31_32.h:73:32: warning: 'dc_fixpt_two_pi' defined but not used [-Wunused-const-variable=]
73 | static const struct fixed31_32 dc_fixpt_two_pi = { 26986075409LL };
| ^~~~~~~~~~~~~~~
drivers/gpu/drm/amd/amdgpu/../display/include/fixed31_32.h:72:32: warning: 'dc_fixpt_pi' defined but not used [-Wunused-const-variable=]
72 | static const struct fixed31_32 dc_fixpt_pi = { 13493037705LL };
| ^~~~~~~~~~~
drivers/gpu/drm/amd/amdgpu/../display/include/fixed31_32.h:67:32: warning: 'dc_fixpt_zero' defined but not used [-Wunused-const-variable=]
67 | static const struct fixed31_32 dc_fixpt_zero = { 0 };
| ^~~~~~~~~~~~~--
In file included from include/linux/iommu.h:16,
from drivers/gpu/drm/nouveau/include/nvif/os.h:30, from drivers/gpu/drm/nouveau/include/nvkm/core/os.h:4, from drivers/gpu/drm/nouveau/include/nvkm/core/oclass.h:3, from drivers/gpu/drm/nouveau/include/nvkm/core/device.h:4, from drivers/gpu/drm/nouveau/include/nvkm/core/subdev.h:4, from drivers/gpu/drm/nouveau/nvkm/nvfw/acr.c:22:
>> include/linux/ioasid.h:141:6: error: expected ';' before 'inline'
141 | staic inline int ioasid_attach_spid(ioasid_t ioasid, ioasid_t spid)
| ^
| ; drivers/gpu/drm/nouveau/nvkm/nvfw/acr.c:49:1: warning: no previous prototype for 'lsb_header_tail_dump' [-Wmissing-prototypes]
49 | lsb_header_tail_dump(struct nvkm_subdev *subdev,
| ^~~~~~~~~~~~~~~~~~~~
--
In file included from include/linux/iommu.h:16,
from drivers/gpu/drm/nouveau/include/nvif/os.h:30,
from drivers/gpu/drm/nouveau/include/nvkm/core/os.h:4,
from drivers/gpu/drm/nouveau/include/nvkm/core/oclass.h:3,
from drivers/gpu/drm/nouveau/include/nvkm/core/device.h:4,
from drivers/gpu/drm/nouveau/include/nvkm/core/subdev.h:4,
from drivers/gpu/drm/nouveau/include/nvkm/subdev/mc.h:4,
from drivers/gpu/drm/nouveau/nvkm/subdev/mc/priv.h:5,
from drivers/gpu/drm/nouveau/nvkm/subdev/mc/gp10b.c:24:
>> include/linux/ioasid.h:141:6: error: expected ';' before 'inline'
141 | staic inline int ioasid_attach_spid(ioasid_t ioasid, ioasid_t spid)
| ^
| ;
drivers/gpu/drm/nouveau/nvkm/subdev/mc/gp10b.c:27:1: warning: no previous prototype for 'gp10b_mc_init' [-Wmissing-prototypes]
27 | gp10b_mc_init(struct nvkm_mc *mc)
| ^~~~~~~~~~~~~--
In file included from include/linux/iommu.h:16,
from drivers/gpu/drm/nouveau/include/nvif/os.h:30, from drivers/gpu/drm/nouveau/include/nvkm/core/os.h:4, from drivers/gpu/drm/nouveau/include/nvkm/core/oclass.h:3, from drivers/gpu/drm/nouveau/include/nvkm/core/object.h:4, from drivers/gpu/drm/nouveau/nvkm/subdev/mmu/ummu.h:4, from drivers/gpu/drm/nouveau/nvkm/subdev/mmu/base.c:24:
>> include/linux/ioasid.h:141:6: error: expected ';' before 'inline'
141 | staic inline int ioasid_attach_spid(ioasid_t ioasid, ioasid_t spid)
| ^
| ; drivers/gpu/drm/nouveau/nvkm/subdev/mmu/base.c:65:1: warning: no previous prototype for 'nvkm_mmu_ptp_get' [-Wmissing-prototypes]
65 | nvkm_mmu_ptp_get(struct nvkm_mmu *mmu, u32 size, bool zero)
| ^~~~~~~~~~~~~~~~
--
In file included from include/linux/iommu.h:16,
from drivers/gpu/drm/nouveau/include/nvif/os.h:30,
from drivers/gpu/drm/nouveau/include/nvkm/core/os.h:4,
from drivers/gpu/drm/nouveau/include/nvkm/core/oclass.h:3,
from drivers/gpu/drm/nouveau/include/nvkm/core/device.h:4,
from drivers/gpu/drm/nouveau/include/nvkm/core/subdev.h:4,
from drivers/gpu/drm/nouveau/include/nvkm/subdev/mmu.h:4,
from drivers/gpu/drm/nouveau/nvkm/subdev/mmu/priv.h:5,
from drivers/gpu/drm/nouveau/nvkm/subdev/mmu/mem.h:3,
from drivers/gpu/drm/nouveau/nvkm/subdev/mmu/tu102.c:23:
>> include/linux/ioasid.h:141:6: error: expected ';' before 'inline'
141 | staic inline int ioasid_attach_spid(ioasid_t ioasid, ioasid_t spid)
| ^
| ;
drivers/gpu/drm/nouveau/nvkm/subdev/mmu/tu102.c:31:1: warning: no previous prototype for 'tu102_mmu_kind' [-Wmissing-prototypes]
31 | tu102_mmu_kind(struct nvkm_mmu *mmu, int *count, u8 *invalid)
| ^~~~~~~~~~~~~~
--
In file included from include/linux/iommu.h:16,
from drivers/gpu/drm/nouveau/include/nvif/os.h:30,
from drivers/gpu/drm/nouveau/include/nvkm/core/os.h:4,
from drivers/gpu/drm/nouveau/include/nvkm/core/oclass.h:3,
from drivers/gpu/drm/nouveau/include/nvkm/core/device.h:4,
from drivers/gpu/drm/nouveau/include/nvkm/core/subdev.h:4,
from drivers/gpu/drm/nouveau/include/nvkm/subdev/therm.h:4,
from drivers/gpu/drm/nouveau/nvkm/subdev/therm/priv.h:27,
from drivers/gpu/drm/nouveau/nvkm/subdev/therm/gt215.c:24:
>> include/linux/ioasid.h:141:6: error: expected ';' before 'inline'
141 | staic inline int ioasid_attach_spid(ioasid_t ioasid, ioasid_t spid)
| ^
| ;
drivers/gpu/drm/nouveau/nvkm/subdev/therm/gt215.c:40:1: warning: no previous prototype for 'gt215_therm_init' [-Wmissing-prototypes]
40 | gt215_therm_init(struct nvkm_therm *therm)
| ^~~~~~~~~~~~~~~~
--
In file included from include/linux/iommu.h:16,
from drivers/gpu/drm/nouveau/include/nvif/os.h:30,
from drivers/gpu/drm/nouveau/include/nvkm/core/os.h:4,
from drivers/gpu/drm/nouveau/include/nvkm/core/oclass.h:3,
from drivers/gpu/drm/nouveau/include/nvkm/core/device.h:4,
from drivers/gpu/drm/nouveau/include/nvkm/core/subdev.h:4,
from drivers/gpu/drm/nouveau/include/nvkm/core/engine.h:5,
from drivers/gpu/drm/nouveau/include/nvkm/engine/gr.h:4,
from drivers/gpu/drm/nouveau/nvkm/engine/gr/priv.h:5,
from drivers/gpu/drm/nouveau/nvkm/engine/gr/gf100.h:27,
from drivers/gpu/drm/nouveau/nvkm/engine/gr/gf100.c:24:
>> include/linux/ioasid.h:141:6: error: expected ';' before 'inline'
141 | staic inline int ioasid_attach_spid(ioasid_t ioasid, ioasid_t spid)
| ^
| ;
drivers/gpu/drm/nouveau/nvkm/engine/gr/gf100.c:745:1: warning: no previous prototype for 'gf100_gr_fecs_start_ctxsw' [-Wmissing-prototypes]
745 | gf100_gr_fecs_start_ctxsw(struct nvkm_gr *base)
| ^~~~~~~~~~~~~~~~~~~~~~~~~
drivers/gpu/drm/nouveau/nvkm/engine/gr/gf100.c:760:1: warning: no previous prototype for 'gf100_gr_fecs_stop_ctxsw' [-Wmissing-prototypes]
760 | gf100_gr_fecs_stop_ctxsw(struct nvkm_gr *base)
| ^~~~~~~~~~~~~~~~~~~~~~~~
drivers/gpu/drm/nouveau/nvkm/engine/gr/gf100.c:2036:1: warning: no previous prototype for 'gf100_gr_dtor' [-Wmissing-prototypes]
2036 | gf100_gr_dtor(struct nvkm_gr *base)
| ^~~~~~~~~~~~~
--
In file included from include/linux/iommu.h:16,
from drivers/gpu/drm/nouveau/include/nvif/os.h:30,
from drivers/gpu/drm/nouveau/include/nvkm/core/os.h:4,
from drivers/gpu/drm/nouveau/include/nvkm/core/oclass.h:3,
from drivers/gpu/drm/nouveau/include/nvkm/core/device.h:4,
from drivers/gpu/drm/nouveau/include/nvkm/core/subdev.h:4,
from drivers/gpu/drm/nouveau/include/nvkm/core/engine.h:5,
from drivers/gpu/drm/nouveau/include/nvkm/engine/gr.h:4,
from drivers/gpu/drm/nouveau/nvkm/engine/gr/priv.h:5,
from drivers/gpu/drm/nouveau/nvkm/engine/gr/gf100.h:27,
from drivers/gpu/drm/nouveau/nvkm/engine/gr/gk20a.c:22:
>> include/linux/ioasid.h:141:6: error: expected ';' before 'inline'
141 | staic inline int ioasid_attach_spid(ioasid_t ioasid, ioasid_t spid)
| ^
| ; drivers/gpu/drm/nouveau/nvkm/engine/gr/gk20a.c:37:1: warning: no previous prototype for 'gk20a_gr_av_to_init' [-Wmissing-prototypes]
37 | gk20a_gr_av_to_init(struct gf100_gr *gr, const char *path, const char *name,
| ^~~~~~~~~~~~~~~~~~~
drivers/gpu/drm/nouveau/nvkm/engine/gr/gk20a.c:87:1: warning: no previous prototype for 'gk20a_gr_aiv_to_init' [-Wmissing-prototypes]
87 | gk20a_gr_aiv_to_init(struct gf100_gr *gr, const char *path, const char *name,
| ^~~~~~~~~~~~~~~~~~~~
drivers/gpu/drm/nouveau/nvkm/engine/gr/gk20a.c:130:1: warning: no previous prototype for 'gk20a_gr_av_to_method' [-Wmissing-prototypes]
130 | gk20a_gr_av_to_method(struct gf100_gr *gr, const char *path, const char *name,
| ^~~~~~~~~~~~~~~~~~~~~
--
In file included from include/linux/iommu.h:16,
from drivers/gpu/drm/nouveau/include/nvif/os.h:30,
from drivers/gpu/drm/nouveau/include/nvif/object.h:5,
from drivers/gpu/drm/nouveau/include/nvif/client.h:5,
from drivers/gpu/drm/nouveau/nouveau_drv.h:43,
from drivers/gpu/drm/nouveau/nouveau_display.h:5,
from drivers/gpu/drm/nouveau/nouveau_fbcon.h:32,
from drivers/gpu/drm/nouveau/nouveau_display.c:38:
>> include/linux/ioasid.h:141:6: error: expected ';' before 'inline'
141 | staic inline int ioasid_attach_spid(ioasid_t ioasid, ioasid_t spid)
| ^
| ;
drivers/gpu/drm/nouveau/nouveau_display.c: In function 'nouveau_framebuffer_new':
drivers/gpu/drm/nouveau/nouveau_display.c:338:15: warning: variable 'width' set but not used [-Wunused-but-set-variable]
338 | unsigned int width, height, i;
| ^~~~~--
In file included from include/linux/iommu.h:16,
from drivers/gpu/drm/nouveau/include/nvif/os.h:30, from drivers/gpu/drm/nouveau/include/nvif/object.h:5, from drivers/gpu/drm/nouveau/include/nvif/mmu.h:3, from drivers/gpu/drm/nouveau/include/nvif/mem.h:3, from drivers/gpu/drm/nouveau/dispnv50/disp.h:3, from drivers/gpu/drm/nouveau/dispnv50/disp.c:24:
>> include/linux/ioasid.h:141:6: error: expected ';' before 'inline'
141 | staic inline int ioasid_attach_spid(ioasid_t ioasid, ioasid_t spid)
| ^
| ;
drivers/gpu/drm/nouveau/dispnv50/disp.c: In function 'nv50_mstm_cleanup':
drivers/gpu/drm/nouveau/dispnv50/disp.c:1237:6: warning: variable 'ret' set but not used [-Wunused-but-set-variable]
1237 | int ret;
| ^~~
drivers/gpu/drm/nouveau/dispnv50/disp.c: In function 'nv50_mstm_prepare':
drivers/gpu/drm/nouveau/dispnv50/disp.c:1261:6: warning: variable 'ret' set but not used [-Wunused-but-set-variable]
1261 | int ret;
| ^~~
drivers/gpu/drm/nouveau/dispnv50/disp.c: At top level:
drivers/gpu/drm/nouveau/dispnv50/disp.c:2450:1: warning: no previous prototype for 'nv50_display_create' [-Wmissing-prototypes]
2450 | nv50_display_create(struct drm_device *dev)
| ^~~~~~~~~~~~~~~~~~~..
# https://github.com/0day-ci/linux/commit/09f31e901946399a274ce954bdefa4108e895b33
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Jacob-Pan/IOASID-extensions-for-guest-SVA/20200822-123111 git checkout 09f31e901946399a274ce954bdefa4108e895b33
vim +141 include/linux/ioasid.h
140
> 141 staic inline int ioasid_attach_spid(ioasid_t ioasid, ioasid_t spid)
142 {
143 return -ENOTSUPP;
144 }
145
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
kernel test robot
build botRe: [PATCH v2 5/9] iommu/ioasid: Introduce ioasid_set private ID
In reply to Jacob PanI love your patch! Yet something to improve:
Hi Jacob,
I love your patch! Yet something to improve:
[auto build test ERROR on iommu/next]
[also build test ERROR on linux/master linus/master v5.9-rc1 next-20200821] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]
url: https://github.com/0day-ci/linux/commits/Jacob-Pan/IOASID-extensions-for-guest-SVA/20200822-123111 base: https://git.kernel.org/pub/scm/linux/kernel/git/joro/iommu.git next config: mips-randconfig-r015-20200822 (attached as .config) compiler: clang version 12.0.0 (https://github.com/llvm/llvm-project b587ca93be114d07ec3bf654add97d7872325281)
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# install mips cross compiling tool for clang build
# apt-get install binutils-mips-linux-gnu
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross ARCH=mips
If you fix the issue, kindly add following tag as appropriate
All errors (new ones prefixed by >>):
In file included from drivers/of/device.c:7:
In file included from include/linux/of_iommu.h:6:
In file included from include/linux/iommu.h:16:
>> include/linux/ioasid.h:141:1: error: unknown type name 'staic'; did you mean 'static'?
staic inline int ioasid_attach_spid(ioasid_t ioasid, ioasid_t spid)
^~~~~
static
In file included from drivers/of/device.c:8:
include/linux/dma-mapping.h:824:9: warning: implicit conversion from 'unsigned long long' to 'unsigned long' changes value from 18446744073709551615 to 4294967295 [-Wconstant-conversion]
return DMA_BIT_MASK(32);
~~~~~~ ^~~~~~~~~~~~~~~~
include/linux/dma-mapping.h:139:40: note: expanded from macro 'DMA_BIT_MASK'
#define DMA_BIT_MASK(n) (((n) == 64) ? ~0ULL : ((1ULL<<(n))-1))
^~~~~ 1 warning and 1 error generated.
--
In file included from drivers/of/platform.c:20:
In file included from include/linux/of_iommu.h:6:
In file included from include/linux/iommu.h:16:
>> include/linux/ioasid.h:141:1: error: unknown type name 'staic'; did you mean 'static'?
staic inline int ioasid_attach_spid(ioasid_t ioasid, ioasid_t spid) ^~~~~
static
1 error generated.
# https://github.com/0day-ci/linux/commit/09f31e901946399a274ce954bdefa4108e895b33
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Jacob-Pan/IOASID-extensions-for-guest-SVA/20200822-123111 git checkout 09f31e901946399a274ce954bdefa4108e895b33
vim +141 include/linux/ioasid.h
140
> 141 staic inline int ioasid_attach_spid(ioasid_t ioasid, ioasid_t spid)
142 {
143 return -ENOTSUPP;
144 }
145
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
Re: [PATCH v2 5/9] iommu/ioasid: Introduce ioasid_set private ID
In reply to Jacob PanQuoted 39 lines · Jacob Pan
When an IOASID set is used for guest SVA, each VM will acquire its
ioasid_setfor IOASID allocations. IOASIDs within the VM must have a host/physical IOASID backing, mapping between guest and host IOASIDs can be non-identical. IOASID set private ID (SPID) is introduced in this patch to be used as guest IOASID. However, the concept ofioasid_setspecific namespace is generic, thus named SPID.As SPID namespace is within the IOASID set, the IOASID core can provide lookup services at both directions. SPIDs may not be allocated when its IOASID is allocated, the mapping between SPID and IOASID is usually established when a guest page table is bound to a host PASID.
Signed-off-by Jacob Pan <[email protected]>--- drivers/iommu/ioasid.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++ include/linux/ioasid.h | 12 +++++++++++ 2 files changed, 66 insertions(+)drivers/iommu/ioasid.c +2
-0@@ -21,6 +21,7 @@ enum ioasid_state {* struct ioasid_data - Meta data about ioasid * * @id: Unique ID +* @spid: Private ID unique within a set * @users Number of active users * @state Track state of the IOASID * @set Meta data of the set this IOASID belongs to @@ -29,6 +30,7 @@ enum ioasid_state {*/ struct ioasid_data {ioasid_t id; +ioasid_t spid;struct ioasid_set *set;refcount_t users;enum ioasid_state state;
drivers/iommu/ioasid.c int ioasid_attach_data(ioasid_t ioasid, void *data) · line 337
Quoted 10 lines
drivers/iommu/ioasid.c +7
-0@@ -326,6 +328,58 @@ int ioasid_attach_data(ioasid_t ioasid, void *data)EXPORT_SYMBOL_GPL (ioasid_attach_data);/** +* ioasid_attach_spid - Attach ioasid_set private ID to an IOASID +* +* @ioasid: the ID to attach +* @spid: the ioasid_set private ID of @ioasid +* +* For IOASID that is already allocated, private ID within the set can be +* attached via this API. Future lookup can be done via ioasid_find.
via ioasid_find_by_spid()?
Quoted 14 lines
drivers/iommu/ioasid.c +14
-0+*/ +int ioasid_attach_spid (ioasid_t ioasid,ioasid_t spid) +{ +struct ioasid_data *ioasid_data; +int ret =0 ; + +spin_lock (&ioasid_allocator_lock); + ioasid_data =xa_load (&active_allocator->xa, ioasid); + +if (!ioasid_data) { +pr_err ("No IOASID entry %d to attach SPID %d\n" , + ioasid, spid); + ret = -ENOENT ; + goto done_unlock;
drivers/iommu/ioasid.c int ioasid_attach_data(ioasid_t ioasid, void *data) · line 361
Quoted 10 lines
drivers/iommu/ioasid.c +10
-0+ } + ioasid_data->spid = spid; + +done_unlock: +spin_unlock (&ioasid_allocator_lock); +return ret; +} +EXPORT_SYMBOL_GPL (ioasid_attach_spid); + +ioasid_t ioasid_find_by_spid (struct ioasid_set *set,ioasid_t spid)
Maybe add a bit of documentation as this is public-facing.
drivers/iommu/ioasid.c int ioasid_attach_data(ioasid_t ioasid, void *data) · line 374
Quoted 13 lines
drivers/iommu/ioasid.c +13
-0+{ +struct ioasid_data *entry; +unsigned long index; + +if (!xa_load (&ioasid_sets, set->sid)) { +pr_warn ("Invalid set\n" ); +return INVALID_IOASID ; + } + +xa_for_each (&set->xa, index, entry) { +if (spid == entry->spid) { +pr_debug ("Found ioasid %lu by spid %u\n" , index, spid); +refcount_inc (&entry->users);
Nothing prevents ioasid_free() from concurrently dropping the refcount to zero and calling ioasid_do_free(). The caller will later call ioasid_put() on a stale/reallocated index.
Quoted 26 lines
drivers/iommu/ioasid.c +8
-0+return index; + } + } +return INVALID_IOASID ; +} +EXPORT_SYMBOL_GPL (ioasid_find_by_spid); + +/** * ioasid_alloc - Allocate an IOASID * @set: the IOASID set * @min: the minimum ID (inclusive) include/linux/ioasid.h +2
-0@@ -73,6 +73,8 @@ bool ioasid_is_active(ioasid_t ioasid);void *ioasid_find (struct ioasid_set *set,ioasid_t ioasid,bool (*getter)(void *));int ioasid_attach_data (ioasid_t ioasid,void *data); +int ioasid_attach_spid (ioasid_t ioasid,ioasid_t spid); +ioasid_t ioasid_find_by_spid (struct ioasid_set *set,ioasid_t spid);int ioasid_register_allocator (struct ioasid_allocator_ops *allocator);void ioasid_unregister_allocator (struct ioasid_allocator_ops *allocator);void ioasid_is_in_set (struct ioasid_set *set,ioasid_t ioasid);@@ -136,5 +138,15 @@ static inline int ioasid_attach_data(ioasid_t ioasid, void *data)return -ENOTSUPP ;
include/linux/ioasid.h static inline int ioasid_attach_data(ioasid_t ioasid, void *data) · line 148
Quoted 10 lines
include/linux/ioasid.h +8
-0} +staic inline int ioasid_attach_spid(ioasid_t ioasid, ioasid_t spid) +{ +return -ENOTSUPP ; +} + +static inline ioasid_t ioasid_find_by_spid (struct ioasid_set *set,ioasid_t spid) +{ +return -ENOTSUPP ;
INVALID_IOASID
Thanks,
Jean
Quoted 7 lines
include/linux/ioasid.h +2
-0+} +#endif /* CONFIG_IOASID */ #endif /* __LINUX_IOASID_H */ Signature · 1 line
2.7.4
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
Re: [PATCH v2 5/9] iommu/ioasid: Introduce ioasid_set private ID
Quoted 42 lines · On Tue, 25 Aug 2020 12:22:09 +0200 Jean-Philippe Brucker…
Quoted 41 lines · Jacob Pan
When an IOASID set is used for guest SVA, each VM will acquire its
ioasid_setfor IOASID allocations. IOASIDs within the VM must have a host/physical IOASID backing, mapping between guest and host IOASIDs can be non-identical. IOASID set private ID (SPID) is introduced in this patch to be used as guest IOASID. However, the concept ofioasid_setspecific namespace is generic, thus named SPID.As SPID namespace is within the IOASID set, the IOASID core can provide lookup services at both directions. SPIDs may not be allocated when its IOASID is allocated, the mapping between SPID and IOASID is usually established when a guest page table is bound to a host PASID.
Signed-off-by Jacob Pan <[email protected]>--- drivers/iommu/ioasid.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++ include/linux/ioasid.h | 12 +++++++++++ 2 files changed, 66 insertions(+)drivers/iommu/ioasid.c +2
-0@@ -21,6 +21,7 @@ enum ioasid_state {* struct ioasid_data - Meta data about ioasid * * @id: Unique ID +* @spid: Private ID unique within a set * @users Number of active users * @state Track state of the IOASID * @set Meta data of the set this IOASID belongs to @@ -29,6 +30,7 @@ enum ioasid_state {*/ struct ioasid_data {ioasid_t id; +ioasid_t spid;struct ioasid_set *set;refcount_t users;enum ioasid_state state;@@ -326,6 +328,58 @@ int ioasid_attach_data(ioasid_t ioasid, void *data)EXPORT_SYMBOL_GPL (ioasid_attach_data);
Quoted 11 lines
drivers/iommu/ioasid.c int ioasid_attach_data(ioasid_t ioasid, void *data) · line 337
Quoted 8 lines
drivers/iommu/ioasid.c +7
-0/** +* ioasid_attach_spid - Attach ioasid_set private ID to an IOASID +* +* @ioasid: the ID to attach +* @spid: the ioasid_set private ID of @ioasid +* +* For IOASID that is already allocated, private ID within the set can be +* attached via this API. Future lookup can be done via ioasid_find. via
ioasid_find_by_spid()?
yes, will update.
Quoted 18 lines
Quoted 18 lines
drivers/iommu/ioasid.c +18
-0+*/ +int ioasid_attach_spid (ioasid_t ioasid,ioasid_t spid) +{ +struct ioasid_data *ioasid_data; +int ret =0 ; + +spin_lock (&ioasid_allocator_lock); + ioasid_data =xa_load (&active_allocator->xa, ioasid); + +if (!ioasid_data) { +pr_err ("No IOASID entry %d to attach SPID %d\n" , + ioasid, spid); + ret = -ENOENT ; + goto done_unlock; + } + ioasid_data->spid = spid; + +done_unlock:
Quoted 9 lines
drivers/iommu/ioasid.c int ioasid_attach_data(ioasid_t ioasid, void *data) · line 361
Quoted 6 lines
drivers/iommu/ioasid.c +6
-0+spin_unlock (&ioasid_allocator_lock); +return ret; +} +EXPORT_SYMBOL_GPL (ioasid_attach_spid); + +ioasid_t ioasid_find_by_spid (struct ioasid_set *set,ioasid_t spid)Maybe add a bit of documentation as this is public-facing.
Good point, I will add
/**
* ioasid_find_by_spid - Find the system-wide IOASID by a set private ID and
* its set.
*
* @set: the ioasid_set to search within
* @spid: the set private ID
*
* Given a set private ID and its IOASID set, find the system-wide IOASID. Take
* a reference upon finding the matching IOASID. Return INVALID_IOASID if the
* IOASID is not found in the set or the set is not valid.
*/
Quoted 9 lines
Quoted 9 lines
drivers/iommu/ioasid.c +9
-0+{ +struct ioasid_data *entry; +unsigned long index; + +if (!xa_load (&ioasid_sets, set->sid)) { +pr_warn ("Invalid set\n" ); +return INVALID_IOASID ; + } +
Quoted 9 lines
drivers/iommu/ioasid.c int ioasid_attach_data(ioasid_t ioasid, void *data) · line 374
Quoted 4 lines
drivers/iommu/ioasid.c +4
-0+xa_for_each (&set->xa, index, entry) { +if (spid == entry->spid) { +pr_debug ("Found ioasid %lu by spid %u\n" , index, spid); +refcount_inc (&entry->users);Nothing prevents
ioasid_free()from concurrently dropping the refcount to zero and callingioasid_do_free(). The caller will later callioasid_put()on a stale/reallocated index.
right, need to add spin_lock(&ioasid_allocator_lock);
Quoted 28 lines
Quoted 28 lines
drivers/iommu/ioasid.c +8
-0+return index; + } + } +return INVALID_IOASID ; +} +EXPORT_SYMBOL_GPL (ioasid_find_by_spid); + +/** * ioasid_alloc - Allocate an IOASID * @set: the IOASID set * @min: the minimum ID (inclusive) include/linux/ioasid.h +2
-0@@ -73,6 +73,8 @@ bool ioasid_is_active(ioasid_t ioasid);void *ioasid_find (struct ioasid_set *set,ioasid_t ioasid,bool (*getter)(void *));int ioasid_attach_data (ioasid_t ioasid,void *data); +int ioasid_attach_spid (ioasid_t ioasid,ioasid_t spid); +ioasid_t ioasid_find_by_spid (struct ioasid_set *set,ioasid_t spid);int ioasid_register_allocator (struct ioasid_allocator_ops *allocator);void ioasid_unregister_allocator (struct ioasid_allocator_ops *allocator);void ioasid_is_in_set (struct ioasid_set *set,ioasid_t ioasid);@@ -136,5 +138,15 @@ static inline int ioasid_attach_data(ioasid_t ioasid, void *data)return -ENOTSUPP ; }
Quoted 11 lines
include/linux/ioasid.h static inline int ioasid_attach_data(ioasid_t ioasid, void *data) · line 148
Quoted 8 lines
include/linux/ioasid.h +8
-0+staic inline int ioasid_attach_spid(ioasid_t ioasid, ioasid_t spid) +{ +return -ENOTSUPP ; +} + +static inline ioasid_t ioasid_find_by_spid (struct ioasid_set *set,ioasid_t spid) +{ +return -ENOTSUPP ;
INVALID_IOASID
right, will fix.
Thanks!
Quoted 14 lines
Thanks,
JeanQuoted 7 lines
include/linux/ioasid.h +2
-0+} +#endif /* CONFIG_IOASID */ #endif /* __LINUX_IOASID_H */ Signature · 1 line
2.7.4_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
[Jacob Pan]
Re: [PATCH v2 5/9] iommu/ioasid: Introduce ioasid_set private ID
In reply to Jacob PanHi Jacob,
Quoted 39 lines · Jacob Pan
When an IOASID set is used for guest SVA, each VM will acquire its
ioasid_setfor IOASID allocations. IOASIDs within the VM must have a host/physical IOASID backing, mapping between guest and host IOASIDs can be non-identical. IOASID set private ID (SPID) is introduced in this patch to be used as guest IOASID. However, the concept ofioasid_setspecific namespace is generic, thus named SPID.As SPID namespace is within the IOASID set, the IOASID core can provide lookup services at both directions. SPIDs may not be allocated when its IOASID is allocated, the mapping between SPID and IOASID is usually established when a guest page table is bound to a host PASID.
Signed-off-by Jacob Pan <[email protected]>--- drivers/iommu/ioasid.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++ include/linux/ioasid.h | 12 +++++++++++ 2 files changed, 66 insertions(+)drivers/iommu/ioasid.c +2
-0@@ -21,6 +21,7 @@ enum ioasid_state {* struct ioasid_data - Meta data about ioasid * * @id: Unique ID +* @spid: Private ID unique within a set * @users Number of active users * @state Track state of the IOASID * @set Meta data of the set this IOASID belongs to @@ -29,6 +30,7 @@ enum ioasid_state {*/ struct ioasid_data {ioasid_t id; +ioasid_t spid;struct ioasid_set *set;refcount_t users;enum ioasid_state state;
drivers/iommu/ioasid.c int ioasid_attach_data(ioasid_t ioasid, void *data) · line 337
Quoted 10 lines
drivers/iommu/ioasid.c +7
-0@@ -326,6 +328,58 @@ int ioasid_attach_data(ioasid_t ioasid, void *data)EXPORT_SYMBOL_GPL (ioasid_attach_data);/** +* ioasid_attach_spid - Attach ioasid_set private ID to an IOASID +* +* @ioasid: the ID to attach +* @spid: the ioasid_set private ID of @ioasid +* +* For IOASID that is already allocated, private ID within the set can be +* attached via this API. Future lookup can be done via ioasid_find.
I would remove "For IOASID that is already allocated, private ID within the set can be attached via this API"
drivers/iommu/ioasid.c int ioasid_attach_data(ioasid_t ioasid, void *data) · line 344
Quoted 7 lines
drivers/iommu/ioasid.c +7
-0+*/ +int ioasid_attach_spid (ioasid_t ioasid,ioasid_t spid) +{ +struct ioasid_data *ioasid_data; +int ret =0 ; + +spin_lock (&ioasid_allocator_lock);
We keep on saying the SPID is local to an IOASID set but we don't check any IOASID set contains this ioasid. It looks a bit weird to me.
drivers/iommu/ioasid.c int ioasid_attach_data(ioasid_t ioasid, void *data) · line 353
Quoted 9 lines
drivers/iommu/ioasid.c +9
-0+ ioasid_data =xa_load (&active_allocator->xa, ioasid); + +if (!ioasid_data) { +pr_err ("No IOASID entry %d to attach SPID %d\n" , + ioasid, spid); + ret = -ENOENT ; + goto done_unlock; + } + ioasid_data->spid = spid;
is there any way/need to remove an spid binding?
Quoted 52 lines
drivers/iommu/ioasid.c +29
-0+ +done_unlock: +spin_unlock (&ioasid_allocator_lock); +return ret; +} +EXPORT_SYMBOL_GPL (ioasid_attach_spid); + +ioasid_t ioasid_find_by_spid (struct ioasid_set *set,ioasid_t spid) +{ +struct ioasid_data *entry; +unsigned long index; + +if (!xa_load (&ioasid_sets, set->sid)) { +pr_warn ("Invalid set\n" ); +return INVALID_IOASID ; + } + +xa_for_each (&set->xa, index, entry) { +if (spid == entry->spid) { +pr_debug ("Found ioasid %lu by spid %u\n" , index, spid); +refcount_inc (&entry->users); +return index; + } + } +return INVALID_IOASID ; +} +EXPORT_SYMBOL_GPL (ioasid_find_by_spid); + +/** * ioasid_alloc - Allocate an IOASID * @set: the IOASID set * @min: the minimum ID (inclusive) include/linux/ioasid.h +4
-0@@ -73,6 +73,8 @@ bool ioasid_is_active(ioasid_t ioasid);void *ioasid_find (struct ioasid_set *set,ioasid_t ioasid,bool (*getter)(void *));int ioasid_attach_data (ioasid_t ioasid,void *data); +int ioasid_attach_spid (ioasid_t ioasid,ioasid_t spid); +ioasid_t ioasid_find_by_spid (struct ioasid_set *set,ioasid_t spid);int ioasid_register_allocator (struct ioasid_allocator_ops *allocator);void ioasid_unregister_allocator (struct ioasid_allocator_ops *allocator);void ioasid_is_in_set (struct ioasid_set *set,ioasid_t ioasid);@@ -136,5 +138,15 @@ static inline int ioasid_attach_data(ioasid_t ioasid, void *data)return -ENOTSUPP ; } +staic inline int ioasid_attach_spid(ioasid_t ioasid, ioasid_t spid) +{
include/linux/ioasid.h static inline int ioasid_attach_data(ioasid_t ioasid, void *data) · line 152
Quoted 10 lines
include/linux/ioasid.h +8
-0+return -ENOTSUPP ; +} + +static inline ioasid_t ioasid_find_by_spid (struct ioasid_set *set,ioasid_t spid) +{ +return -ENOTSUPP ; +} +#endif /* CONFIG_IOASID */ #endif /* __LINUX_IOASID_H */
Thanks
Eric
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
Re: [PATCH v2 5/9] iommu/ioasid: Introduce ioasid_set private ID
Quoted 46 lines · On Tue, 1 Sep 2020 17:38:44 +0200 Auger Eric <[email protected]> wrote:
Hi Jacob,
Quoted 44 lines · Jacob Pan
When an IOASID set is used for guest SVA, each VM will acquire its
ioasid_setfor IOASID allocations. IOASIDs within the VM must have a host/physical IOASID backing, mapping between guest and host IOASIDs can be non-identical. IOASID set private ID (SPID) is introduced in this patch to be used as guest IOASID. However, the concept ofioasid_setspecific namespace is generic, thus named SPID.As SPID namespace is within the IOASID set, the IOASID core can provide lookup services at both directions. SPIDs may not be allocated when its IOASID is allocated, the mapping between SPID and IOASID is usually established when a guest page table is bound to a host PASID.
Signed-off-by Jacob Pan <[email protected]>--- drivers/iommu/ioasid.c | 54++++++++++++++++++++++++++++++++++++++++++++++++++
include/linux/ioasid.h| 12 +++++++++++ 2 files changed, 66 insertions(+)drivers/iommu/ioasid.c +2
-0@@ -21,6 +21,7 @@ enum ioasid_state {* struct ioasid_data - Meta data about ioasid * * @id: Unique ID +* @spid: Private ID unique within a set * @users Number of active users * @state Track state of the IOASID * @set Meta data of the set this IOASID belongs to @@ -29,6 +30,7 @@ enum ioasid_state {*/ struct ioasid_data {ioasid_t id; +ioasid_t spid;struct ioasid_set *set;refcount_t users;enum ioasid_state state;@@ -326,6 +328,58 @@ int ioasid_attach_data(ioasid_t ioasid, void *data) EXPORT_SYMBOL_GPL(ioasid_attach_data);
Quoted 12 lines
Quoted 10 lines
drivers/iommu/ioasid.c +6
-0/** +* ioasid_attach_spid - Attach ioasid_set private ID to an IOASID +* +* @ioasid: the ID to attach +* @spid: the ioasid_set private ID of @ioasid +* +* For IOASID that is already allocated, private ID within the set can be
+ * attached via this API. Future lookup can be done viaioasid_find.I would remove "For IOASID that is already allocated, private ID within the set can be attached via this API"
I guess it is implied. Will remove.
Quoted 9 lines
drivers/iommu/ioasid.c int ioasid_attach_data(ioasid_t ioasid, void *data) · line 344
Quoted 7 lines
+ */
drivers/iommu/ioasid.c +6
-0+int ioasid_attach_spid (ioasid_t ioasid,ioasid_t spid) +{ +struct ioasid_data *ioasid_data; +int ret =0 ; + +spin_lock (&ioasid_allocator_lock);We keep on saying the SPID is local to an IOASID set but we don't check any IOASID set contains this ioasid. It looks a bit weird to me.
We store ioasid_set inside ioasid_data when an IOASID is allocated, so we don't need to search all the ioasid_sets. Perhaps I missed your point?
Quoted 10 lines
drivers/iommu/ioasid.c int ioasid_attach_data(ioasid_t ioasid, void *data) · line 353
Quoted 9 lines
drivers/iommu/ioasid.c +9
-0+ ioasid_data =xa_load (&active_allocator->xa, ioasid); + +if (!ioasid_data) { +pr_err ("No IOASID entry %d to attach SPID %d\n" , + ioasid, spid); + ret = -ENOENT ; + goto done_unlock; + } + ioasid_data->spid = spid;is there any way/need to remove an spid binding?
For guest SVA, we attach SPID as a guest PASID during bind guest page table. Unbind does the opposite, ioasid_attach_spid() with spid=INVALID_IOASID clears the binding.
Perhaps add more symmetric functions? i.e. ioasid_detach_spid(ioasid_t ioasid) ioasid_attach_spid(struct ioasid_set *set, ioasid_t ioasid)
Quoted 65 lines
Quoted 58 lines
drivers/iommu/ioasid.c +20
-0+ +done_unlock: +spin_unlock (&ioasid_allocator_lock); +return ret; +} +EXPORT_SYMBOL_GPL (ioasid_attach_spid); + +ioasid_t ioasid_find_by_spid (struct ioasid_set *set,ioasid_t spid) +{ +struct ioasid_data *entry; +unsigned long index; + +if (!xa_load (&ioasid_sets, set->sid)) { +pr_warn ("Invalid set\n" ); +return INVALID_IOASID ; + } + +xa_for_each (&set->xa, index, entry) { +if (spid == entry->spid) { + pr_debug("Found ioasid %lu by spid %u\n",index, spid);
drivers/iommu/ioasid.c +9
-0+refcount_inc (&entry->users); +return index; + } + } +return INVALID_IOASID ; +} +EXPORT_SYMBOL_GPL (ioasid_find_by_spid); + +/** * ioasid_alloc - Allocate an IOASID * @set: the IOASID set * @min: the minimum ID (inclusive) include/linux/ioasid.h +0
-0@@ -73,6 +73,8 @@ bool ioasid_is_active(ioasid_t ioasid); void *ioasid_find(struct ioasid_set *set, ioasid_t ioasid, bool(*getter)(void *)); int
ioasid_attach_data(ioasid_t ioasid, void *data); +intioasid_attach_spid(ioasid_t ioasid, ioasid_t spid); +ioasid_tioasid_find_by_spid(struct ioasid_set *set, ioasid_t spid); intioasid_register_allocator(struct ioasid_allocator_ops *allocator); voidioasid_unregister_allocator(struct ioasid_allocator_ops *allocator); voidioasid_is_in_set(struct ioasid_set *set, ioasid_t ioasid); @@ -136,5 +138,15 @@ static inline intioasid_attach_data(ioasid_t ioasid, void *data)returninclude/linux/ioasid.h +6
-1-ENOTSUPP ; } +staic inline int ioasid_attach_spid(ioasid_t ioasid, ioasid_t spid) +{ +return -ENOTSUPP ; +} + +static inline ioasid_t ioasid_find_by_spid(struct ioasid_set *set,
ioasid_tspid) +{include/linux/ioasid.h static inline int ioasid_attach_data(ioasid_t ioasid, void *data) · line 152
Quoted 5 lines
include/linux/ioasid.h +3
-0+return -ENOTSUPP ; +} +#endif /* CONFIG_IOASID */ #endif /* __LINUX_IOASID_H */ Thanks
Quoted 6 lines
Eric
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
[Jacob Pan]
Re: [PATCH v2 5/9] iommu/ioasid: Introduce ioasid_set private ID
Hi Jacob,
Quoted 72 lines · Jacob Pan
Quoted 46 lines · On Tue, 1 Sep 2020 17:38:44 +0200 Auger Eric <[email protected]> wrote:
Hi Jacob,
Quoted 44 lines · Jacob Pan
When an IOASID set is used for guest SVA, each VM will acquire its
ioasid_setfor IOASID allocations. IOASIDs within the VM must have a host/physical IOASID backing, mapping between guest and host IOASIDs can be non-identical. IOASID set private ID (SPID) is introduced in this patch to be used as guest IOASID. However, the concept ofioasid_setspecific namespace is generic, thus named SPID.As SPID namespace is within the IOASID set, the IOASID core can provide lookup services at both directions. SPIDs may not be allocated when its IOASID is allocated, the mapping between SPID and IOASID is usually established when a guest page table is bound to a host PASID.
Signed-off-by Jacob Pan <[email protected]>--- drivers/iommu/ioasid.c | 54++++++++++++++++++++++++++++++++++++++++++++++++++
include/linux/ioasid.h| 12 +++++++++++ 2 files changed, 66 insertions(+)drivers/iommu/ioasid.c +2
-0@@ -21,6 +21,7 @@ enum ioasid_state {* struct ioasid_data - Meta data about ioasid * * @id: Unique ID +* @spid: Private ID unique within a set * @users Number of active users * @state Track state of the IOASID * @set Meta data of the set this IOASID belongs to @@ -29,6 +30,7 @@ enum ioasid_state {*/ struct ioasid_data {ioasid_t id; +ioasid_t spid;struct ioasid_set *set;refcount_t users;enum ioasid_state state;@@ -326,6 +328,58 @@ int ioasid_attach_data(ioasid_t ioasid, void *data) EXPORT_SYMBOL_GPL(ioasid_attach_data);Quoted 12 lines
Quoted 10 lines
drivers/iommu/ioasid.c +6
-0/** +* ioasid_attach_spid - Attach ioasid_set private ID to an IOASID +* +* @ioasid: the ID to attach +* @spid: the ioasid_set private ID of @ioasid +* +* For IOASID that is already allocated, private ID within the set can be
+ * attached via this API. Future lookup can be done viaioasid_find.I would remove "For IOASID that is already allocated, private ID within the set can be attached via this API"
I guess it is implied. Will remove.
Quoted 9 lines
drivers/iommu/ioasid.c int ioasid_attach_data(ioasid_t ioasid, void *data) · line 344
Quoted 7 lines
+ */
drivers/iommu/ioasid.c +6
-0+int ioasid_attach_spid (ioasid_t ioasid,ioasid_t spid) +{ +struct ioasid_data *ioasid_data; +int ret =0 ; + +spin_lock (&ioasid_allocator_lock);We keep on saying the SPID is local to an IOASID set but we don't check any IOASID set contains this ioasid. It looks a bit weird to me.
Quoted 3 lines
We store
ioasid_setinsideioasid_datawhen an IOASID is allocated, so we don't need to search all theioasid_sets. Perhaps I missed your point?
No I think it became clearer ;-)
Quoted 11 lines
Quoted 11 lines
drivers/iommu/ioasid.c int ioasid_attach_data(ioasid_t ioasid, void *data) · line 353
Quoted 10 lines
drivers/iommu/ioasid.c +9
-0+ ioasid_data =xa_load (&active_allocator->xa, ioasid); + +if (!ioasid_data) { +pr_err ("No IOASID entry %d to attach SPID %d\n" , + ioasid, spid); + ret = -ENOENT ; + goto done_unlock; + } + ioasid_data->spid = spid;is there any way/need to remove an spid binding?
Quoted 7 lines
For guest SVA, we attach SPID as a guest PASID during bind guest page table. Unbind does the opposite,
ioasid_attach_spid()with spid=INVALID_IOASID clears the binding.Perhaps add more symmetric functions? i.e.
ioasid_detach_spid(ioasid_t ioasid)ioasid_attach_spid(struct ioasid_set *set, ioasid_t ioasid)
yep make sense
Thanks
Eric
Quoted 66 lines
Quoted 66 lines
Quoted 59 lines
drivers/iommu/ioasid.c +20
-0+ +done_unlock: +spin_unlock (&ioasid_allocator_lock); +return ret; +} +EXPORT_SYMBOL_GPL (ioasid_attach_spid); + +ioasid_t ioasid_find_by_spid (struct ioasid_set *set,ioasid_t spid) +{ +struct ioasid_data *entry; +unsigned long index; + +if (!xa_load (&ioasid_sets, set->sid)) { +pr_warn ("Invalid set\n" ); +return INVALID_IOASID ; + } + +xa_for_each (&set->xa, index, entry) { +if (spid == entry->spid) { + pr_debug("Found ioasid %lu by spid %u\n",index, spid);
drivers/iommu/ioasid.c +9
-0+refcount_inc (&entry->users); +return index; + } + } +return INVALID_IOASID ; +} +EXPORT_SYMBOL_GPL (ioasid_find_by_spid); + +/** * ioasid_alloc - Allocate an IOASID * @set: the IOASID set * @min: the minimum ID (inclusive) include/linux/ioasid.h +0
-0@@ -73,6 +73,8 @@ bool ioasid_is_active(ioasid_t ioasid); void *ioasid_find(struct ioasid_set *set, ioasid_t ioasid, bool(*getter)(void *)); int
ioasid_attach_data(ioasid_t ioasid, void *data); +intioasid_attach_spid(ioasid_t ioasid, ioasid_t spid); +ioasid_tioasid_find_by_spid(struct ioasid_set *set, ioasid_t spid); intioasid_register_allocator(struct ioasid_allocator_ops *allocator); voidioasid_unregister_allocator(struct ioasid_allocator_ops *allocator); voidioasid_is_in_set(struct ioasid_set *set, ioasid_t ioasid); @@ -136,5 +138,15 @@ static inline intioasid_attach_data(ioasid_t ioasid, void *data)returninclude/linux/ioasid.h +6
-1-ENOTSUPP ; } +staic inline int ioasid_attach_spid(ioasid_t ioasid, ioasid_t spid) +{ +return -ENOTSUPP ; +} + +static inline ioasid_t ioasid_find_by_spid(struct ioasid_set *set,
ioasid_tspid) +{include/linux/ioasid.h static inline int ioasid_attach_data(ioasid_t ioasid, void *data) · line 152
Quoted 5 lines
include/linux/ioasid.h +3
-0+return -ENOTSUPP ; +} +#endif /* CONFIG_IOASID */ #endif /* __LINUX_IOASID_H */ Thanks
Quoted 9 lines
Quoted 6 lines
Eric
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu[Jacob Pan]
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
PATCH v2 6/9 iommu/ioasid: Introduce notification APIs
In reply to Jacob PanRelations among IOASID users largely follow a publisher-subscriber pattern. E.g. to support guest SVA on Intel Scalable I/O Virtualization (SIOV) enabled platforms, VFIO, IOMMU, device drivers, KVM are all users of IOASIDs. When a state change occurs, VFIO publishes the change event that needs to be processed by other users/subscribers.
This patch introduced two types of notifications: global and per ioasid_set. The latter is intended for users who only needs to handle events related to the IOASID of a given set.
For more information, refer to the kernel documentation at Documentation/ioasid.rst.
--- drivers/iommu/ioasid.c | 280 ++++++++++++++++++++++++++++++++++++++++++++++++- include/linux/ioasid.h | 70 +++++++++++++ 2 files changed, 348 insertions(+), 2 deletions(-)
drivers/iommu/ioasid.c +278 -2
@@ -9,8 +9,35 @@#include <linux/spinlock.h> #include <linux/xarray.h> #include <linux/ioasid.h> +#include <linux/sched/mm.h> static DEFINE_XARRAY_ALLOC (ioasid_sets); +/* +* An IOASID could have multiple consumers where each consumeer may have +* hardware contexts associated with IOASIDs. +* When a status change occurs, such as IOASID is being freed, notifier chains +* are used to keep the consumers in sync. +* This is a publisher-subscriber pattern where publisher can change the +* state of each IOASID, e.g. alloc/free, bind IOASID to a device and mm. +* On the other hand, subscribers gets notified for the state change and +* keep local states in sync. +* +* Currently, the notifier is global. A further optimization could be per +* IOASID set notifier chain. +*/ +static ATOMIC_NOTIFIER_HEAD (ioasid_chain); + +/* List to hold pending notification block registrations */ +static LIST_HEAD (ioasid_nb_pending_list); +static DEFINE_SPINLOCK (ioasid_nb_lock); +struct ioasid_set_nb { +struct list_head list; +struct notifier_block *nb; +void *token; +struct ioasid_set *set; +bool active; +}; +enum ioasid_state {IOASID_STATE_INACTIVE ,IOASID_STATE_ACTIVE ,
@@ -394,6 +421,7 @@ EXPORT_SYMBOL_GPL(ioasid_find_by_spid);ioasid_t ioasid_alloc (struct ioasid_set *set,ioasid_t min,ioasid_t max,void *private) { +struct ioasid_nb_args args;struct ioasid_data *data;void *adata;ioasid_t id =INVALID_IOASID ;
@@ -445,8 +473,14 @@ ioasid_t ioasid_alloc(struct ioasid_set *set, ioasid_t min, ioasid_t max, goto exit_free; } set->nr_ioasids++; - goto done_unlock; + args.id = id; +/* Set private ID is not attached during allocation */ + args.spid =INVALID_IOASID ; + args.set = set; +atomic_notifier_call_chain (&set->nh,IOASID_ALLOC , &args); +spin_unlock (&ioasid_allocator_lock); +return id; exit_free:kfree (data); done_unlock:
@@ -479,6 +513,7 @@ static void ioasid_do_free(struct ioasid_data *data)static void ioasid_free_locked (struct ioasid_set *set,ioasid_t ioasid) { +struct ioasid_nb_args args;struct ioasid_data *data; data =xa_load (&active_allocator->xa, ioasid);
@@ -491,7 +526,16 @@ static void ioasid_free_locked(struct ioasid_set *set, ioasid_t ioasid)pr_warn ("Cannot free IOASID %u due to set ownership\n" , ioasid);return ; } + data->state =IOASID_STATE_FREE_PENDING ; +/* Notify all users that this IOASID is being freed */ + args.id = ioasid; + args.spid = data->spid; + args.pdata = data->private; + args.set = data->set; +atomic_notifier_call_chain (&ioasid_chain,IOASID_FREE , &args); +/* Notify the ioasid_set for per set users */ +atomic_notifier_call_chain (&set->nh,IOASID_FREE , &args);if (!refcount_dec_and_test (&data->users))return ;
@@ -514,6 +558,28 @@ void ioasid_free(struct ioasid_set *set, ioasid_t ioasid) }EXPORT_SYMBOL_GPL (ioasid_free); +static void ioasid_add_pending_nb (struct ioasid_set *set) +{ +struct ioasid_set_nb *curr; + +if (set->type !=IOASID_SET_TYPE_MM ) +return ; + +/* +* Check if there are any pending nb requests for the given token, if so +* add them to the notifier chain. +*/ +spin_lock (&ioasid_nb_lock); +list_for_each_entry (curr, &ioasid_nb_pending_list, list) { +if (curr->token == set->token && !curr->active) { +atomic_notifier_chain_register (&set->nh, curr->nb); + curr->set = set; + curr->active = true; + } + } +spin_unlock (&ioasid_nb_lock); +} +/** * ioasid_alloc_set - Allocate a new IOASID set for a given token *
@@ -601,6 +667,13 @@ struct ioasid_set *ioasid_alloc_set(void *token, ioasid_t quota, int type) sdata->quota = quota; sdata->sid = id;refcount_set (&sdata->ref,1 ); +ATOMIC_INIT_NOTIFIER_HEAD (&sdata->nh); + +/* +* Check if there are any pending nb requests for the given token, if so +* add them to the notifier chain. +*/ +ioasid_add_pending_nb (sdata);/* * Per set XA is used to store private IDs within the set, get ready
@@ -617,6 +690,30 @@ struct ioasid_set *ioasid_alloc_set(void *token, ioasid_t quota, int type) }EXPORT_SYMBOL_GPL (ioasid_alloc_set); + +/* +* ioasid_find_mm_set - Retrieve IOASID set with mm token +* Take a reference of the set if found. +*/ +static struct ioasid_set *ioasid_find_mm_set (struct mm_struct *token) +{ +struct ioasid_set *sdata, *set =NULL ; +unsigned long index; + +spin_lock (&ioasid_allocator_lock); + +xa_for_each (&ioasid_sets, index, sdata) { +if (sdata->type ==IOASID_SET_TYPE_MM && sdata->token == token) { +refcount_inc (&sdata->ref); + set = sdata; + goto exit_unlock; + } + } +exit_unlock: +spin_unlock (&ioasid_allocator_lock); +return set; +} +void ioasid_set_get_locked (struct ioasid_set *set) {if (WARN_ON (xa_load (&ioasid_sets, set->sid) != set)) {
@@ -638,6 +735,8 @@ EXPORT_SYMBOL_GPL(ioasid_set_get);void ioasid_set_put_locked (struct ioasid_set *set) { +struct ioasid_nb_args args = {0 }; +struct ioasid_set_nb *curr;struct ioasid_data *entry;unsigned long index;
@@ -673,8 +772,24 @@ void ioasid_set_put_locked(struct ioasid_set *set) done_destroy:/* Return the quota back to system pool */ ioasid_capacity_avail += set->quota; -kfree_rcu (set, rcu); +/* Restore pending status of the set NBs */ +spin_lock (&ioasid_nb_lock); +list_for_each_entry (curr, &ioasid_nb_pending_list, list) { +if (curr->token == set->token) { +if (curr->active) + curr->active = false; +else +pr_warn ("Set token exists but not active!\n" ); + } + } +spin_unlock (&ioasid_nb_lock); + + args.set = set; +atomic_notifier_call_chain (&ioasid_chain,IOASID_SET_FREE , &args); + +kfree_rcu (set, rcu); +pr_debug ("Set freed %d\n" , set->sid);/* * Token got released right away after the ioasid_set is freed. * If a new set is created immediately with the newly released token,
@@ -927,6 +1042,167 @@ void *ioasid_find(struct ioasid_set *set, ioasid_t ioasid, }EXPORT_SYMBOL_GPL (ioasid_find); +int ioasid_register_notifier (struct ioasid_set *set,struct notifier_block *nb) +{ +if (set) +return atomic_notifier_chain_register (&set->nh, nb); +else +return atomic_notifier_chain_register (&ioasid_chain, nb); +} +EXPORT_SYMBOL_GPL (ioasid_register_notifier); + +void ioasid_unregister_notifier (struct ioasid_set *set, +struct notifier_block *nb) +{ +struct ioasid_set_nb *curr; + +spin_lock (&ioasid_nb_lock); +/* +* Pending list is registered with a token without an ioasid_set, +* therefore should not be unregistered directly. +*/ +list_for_each_entry (curr, &ioasid_nb_pending_list, list) { +if (curr->nb == nb) { +pr_warn ("Cannot unregister NB from pending list\n" ); +spin_unlock (&ioasid_nb_lock); +return ; + } + } +spin_unlock (&ioasid_nb_lock); + +if (set) +atomic_notifier_chain_unregister (&set->nh, nb); +else +atomic_notifier_chain_unregister (&ioasid_chain, nb); +} +EXPORT_SYMBOL_GPL (ioasid_unregister_notifier); + +int ioasid_register_notifier_mm (struct mm_struct *mm,struct notifier_block *nb) +{ +struct ioasid_set_nb *curr; +struct ioasid_set *set; +int ret =0 ; + +if (!mm) +return -EINVAL ; + +spin_lock (&ioasid_nb_lock); + +/* Check for duplicates, nb is unique per set */ +list_for_each_entry (curr, &ioasid_nb_pending_list, list) { +if (curr->token == mm && curr->nb == nb) { + ret = -EBUSY ; + goto exit_unlock; + } + } + +/* Check if the token has an existing set */ + set =ioasid_find_mm_set (mm); +if (IS_ERR_OR_NULL (set)) { +/* Add to the rsvd list as inactive */ + curr->active = false; + }else { +/* REVISIT: Only register empty set for now. Can add an option +* in the future to playback existing PASIDs. +*/ +if (set->nr_ioasids) { +pr_warn ("IOASID set %d not empty\n" , set->sid); + ret = -EBUSY ; + goto exit_unlock; + } + curr =kzalloc (sizeof (*curr),GFP_ATOMIC ); +if (!curr) { + ret = -ENOMEM ; + goto exit_unlock; + } + curr->token = mm; + curr->nb = nb; + curr->active = true; + curr->set = set; + +/* Set already created, add to the notifier chain */ +atomic_notifier_chain_register (&set->nh, nb); +/* +* Do not hold a reference, if the set gets destroyed, the nb +* entry will be marked inactive. +*/ +ioasid_set_put (set); + } + +list_add (&curr->list, &ioasid_nb_pending_list); + +exit_unlock: +spin_unlock (&ioasid_nb_lock); +return ret; +} +EXPORT_SYMBOL_GPL (ioasid_register_notifier_mm); + +void ioasid_unregister_notifier_mm (struct mm_struct *mm,struct notifier_block *nb) +{ +struct ioasid_set_nb *curr; + +spin_lock (&ioasid_nb_lock); +list_for_each_entry (curr, &ioasid_nb_pending_list, list) { +if (curr->token == mm && curr->nb == nb) { +list_del (&curr->list); + goto exit_free; + } + } +pr_warn ("No ioasid set found for mm token %llx\n" , (u64 )mm); + goto done_unlock; + +exit_free: +if (curr->active) { +pr_debug ("mm set active, unregister %llx\n" , + (u64 )mm); +atomic_notifier_chain_unregister (&curr->set->nh, nb); + } +kfree (curr); +done_unlock: +spin_unlock (&ioasid_nb_lock); +return ; +} +EXPORT_SYMBOL_GPL (ioasid_unregister_notifier_mm); + +/** +* ioasid_notify - Send notification on a given IOASID for status change. +* Used by publishers when the status change may affect +* subscriber's internal state. +* +* @ioasid: The IOASID to which the notification will send +* @cmd: The notification event +* @flags: Special instructions, e.g. notify with a set or global +*/ +int ioasid_notify (ioasid_t ioasid,enum ioasid_notify_val cmd,unsigned int flags) +{ +struct ioasid_data *ioasid_data; +struct ioasid_nb_args args = {0 }; +int ret =0 ; + +spin_lock (&ioasid_allocator_lock); + ioasid_data =xa_load (&active_allocator->xa, ioasid); +if (!ioasid_data) { +pr_err ("Trying to notify unknown IOASID %u\n" , ioasid); +spin_unlock (&ioasid_allocator_lock); +return -EINVAL ; + } + + args.id = ioasid; + args.set = ioasid_data->set; + args.pdata = ioasid_data->private; + args.spid = ioasid_data->spid; +if (flags &IOASID_NOTIFY_ALL ) { + ret =atomic_notifier_call_chain (&ioasid_chain, cmd, &args); + }else if (flags &IOASID_NOTIFY_SET ) { + ret =atomic_notifier_call_chain (&ioasid_data->set->nh, + cmd, &args); + } +spin_unlock (&ioasid_allocator_lock); + +return ret; +} +EXPORT_SYMBOL_GPL (ioasid_notify); +MODULE_AUTHOR ("Jean-Philippe Brucker <[email protected]>" );MODULE_AUTHOR ("Jacob Pan <[email protected]>" );MODULE_DESCRIPTION ("IO Address Space ID (IOASID) allocator" );
include/linux/ioasid.h +70 -0
108 lines not on this page · raw message
Signature · 1 line
2.7.4
Re: [PATCH v2 6/9] iommu/ioasid: Introduce notification APIs
Quoted 25 lines · Jacob Pan
Relations among IOASID users largely follow a publisher-subscriber pattern. E.g. to support guest SVA on Intel Scalable I/O Virtualization (SIOV) enabled platforms, VFIO, IOMMU, device drivers, KVM are all users of IOASIDs. When a state change occurs, VFIO publishes the change event that needs to be processed by other users/subscribers.
This patch introduced two types of notifications: global and per
ioasid_set. The latter is intended for users who only needs to handle events related to the IOASID of a given set.
For more information, refer to the kernel documentation atDocumentation/ioasid.rst.Signed-off-by Liu Yi L <[email protected]>Signed-off-by Wu Hao <[email protected]>Signed-off-by Jacob Pan <[email protected]>--- drivers/iommu/ioasid.c | 280 ++++++++++++++++++++++++++++++++++++++++++++++++- include/linux/ioasid.h | 70 +++++++++++++ 2 files changed, 348 insertions(+), 2 deletions(-)drivers/iommu/ioasid.c +0
-0
drivers/iommu/ioasid.c line 16
Quoted 8 lines
drivers/iommu/ioasid.c +3
-0@@ -9,8 +9,35 @@#include <linux/spinlock.h> #include <linux/xarray.h> #include <linux/ioasid.h> +#include <linux/sched/mm.h> static DEFINE_XARRAY_ALLOC (ioasid_sets); +/* +* An IOASID could have multiple consumers where each consumeer may have
consumer
drivers/iommu/ioasid.c line 26
Quoted 10 lines
drivers/iommu/ioasid.c +10
-0+* hardware contexts associated with IOASIDs. +* When a status change occurs, such as IOASID is being freed, notifier chains +* are used to keep the consumers in sync. +* This is a publisher-subscriber pattern where publisher can change the +* state of each IOASID, e.g. alloc/free, bind IOASID to a device and mm. +* On the other hand, subscribers gets notified for the state change and +* keep local states in sync. +* +* Currently, the notifier is global. A further optimization could be per +* IOASID set notifier chain.
The patch adds both
drivers/iommu/ioasid.c line 28
Quoted 2 lines
drivers/iommu/ioasid.c +2
-0+*/ +static ATOMIC_NOTIFIER_HEAD (ioasid_chain);
"ioasid_notifier" may be clearer
Quoted 24 lines
drivers/iommu/ioasid.c +13
-0+ +/* List to hold pending notification block registrations */ +static LIST_HEAD (ioasid_nb_pending_list); +static DEFINE_SPINLOCK (ioasid_nb_lock); +struct ioasid_set_nb { +struct list_head list; +struct notifier_block *nb; +void *token; +struct ioasid_set *set; +bool active; +}; +enum ioasid_state {IOASID_STATE_INACTIVE ,IOASID_STATE_ACTIVE ,@@ -394,6 +421,7 @@ EXPORT_SYMBOL_GPL(ioasid_find_by_spid);ioasid_t ioasid_alloc (struct ioasid_set *set,ioasid_t min,ioasid_t max,void *private) { +struct ioasid_nb_args args;struct ioasid_data *data;void *adata;ioasid_t id =INVALID_IOASID ;
drivers/iommu/ioasid.c ioasid_t ioasid_alloc(struct ioasid_set *set, ioasid_t min, ioasid_t max, · line 479
Quoted 8 lines
drivers/iommu/ioasid.c +4
-1@@ -445,8 +473,14 @@ ioasid_t ioasid_alloc(struct ioasid_set *set, ioasid_t min, ioasid_t max, goto exit_free; } set->nr_ioasids++; - goto done_unlock; + args.id = id; +/* Set private ID is not attached during allocation */ + args.spid =INVALID_IOASID ; + args.set = set;
args.pdata is uninitialized
drivers/iommu/ioasid.c ioasid_t ioasid_alloc(struct ioasid_set *set, ioasid_t min, ioasid_t max, · line 480
Quoted 1 line
drivers/iommu/ioasid.c +1
-0+atomic_notifier_call_chain (&set->nh,IOASID_ALLOC , &args);
No global notification?
Quoted 85 lines
drivers/iommu/ioasid.c +48
-0+spin_unlock (&ioasid_allocator_lock); +return id; exit_free:kfree (data); done_unlock:@@ -479,6 +513,7 @@ static void ioasid_do_free(struct ioasid_data *data)static void ioasid_free_locked (struct ioasid_set *set,ioasid_t ioasid) { +struct ioasid_nb_args args;struct ioasid_data *data; data =xa_load (&active_allocator->xa, ioasid);@@ -491,7 +526,16 @@ static void ioasid_free_locked(struct ioasid_set *set, ioasid_t ioasid)pr_warn ("Cannot free IOASID %u due to set ownership\n" , ioasid);return ; } + data->state =IOASID_STATE_FREE_PENDING ; +/* Notify all users that this IOASID is being freed */ + args.id = ioasid; + args.spid = data->spid; + args.pdata = data->private; + args.set = data->set; +atomic_notifier_call_chain (&ioasid_chain,IOASID_FREE , &args); +/* Notify the ioasid_set for per set users */ +atomic_notifier_call_chain (&set->nh,IOASID_FREE , &args);if (!refcount_dec_and_test (&data->users))return ;@@ -514,6 +558,28 @@ void ioasid_free(struct ioasid_set *set, ioasid_t ioasid) }EXPORT_SYMBOL_GPL (ioasid_free); +static void ioasid_add_pending_nb (struct ioasid_set *set) +{ +struct ioasid_set_nb *curr; + +if (set->type !=IOASID_SET_TYPE_MM ) +return ; + +/* +* Check if there are any pending nb requests for the given token, if so +* add them to the notifier chain. +*/ +spin_lock (&ioasid_nb_lock); +list_for_each_entry (curr, &ioasid_nb_pending_list, list) { +if (curr->token == set->token && !curr->active) { +atomic_notifier_chain_register (&set->nh, curr->nb); + curr->set = set; + curr->active = true; + } + } +spin_unlock (&ioasid_nb_lock); +} +/** * ioasid_alloc_set - Allocate a new IOASID set for a given token * @@ -601,6 +667,13 @@ struct ioasid_set *ioasid_alloc_set(void *token, ioasid_t quota, int type) sdata->quota = quota; sdata->sid = id;refcount_set (&sdata->ref,1 ); +ATOMIC_INIT_NOTIFIER_HEAD (&sdata->nh); + +/* +* Check if there are any pending nb requests for the given token, if so +* add them to the notifier chain. +*/ +ioasid_add_pending_nb (sdata);/* * Per set XA is used to store private IDs within the set, get ready @@ -617,6 +690,30 @@ struct ioasid_set *ioasid_alloc_set(void *token, ioasid_t quota, int type) }EXPORT_SYMBOL_GPL (ioasid_alloc_set); + +/* +* ioasid_find_mm_set - Retrieve IOASID set with mm token +* Take a reference of the set if found. +*/ +static struct ioasid_set *ioasid_find_mm_set (struct mm_struct *token) +{
drivers/iommu/ioasid.c struct ioasid_set *ioasid_alloc_set(void *token, ioasid_t quota, int type) · line 709
Quoted 10 lines
drivers/iommu/ioasid.c +10
-0+struct ioasid_set *sdata, *set =NULL ; +unsigned long index; + +spin_lock (&ioasid_allocator_lock); + +xa_for_each (&ioasid_sets, index, sdata) { +if (sdata->type ==IOASID_SET_TYPE_MM && sdata->token == token) { +refcount_inc (&sdata->ref); + set = sdata; + goto exit_unlock;
Or just break
Quoted 32 lines
drivers/iommu/ioasid.c +16
-1+ } + } +exit_unlock: +spin_unlock (&ioasid_allocator_lock); +return set; +} +void ioasid_set_get_locked (struct ioasid_set *set) {if (WARN_ON (xa_load (&ioasid_sets, set->sid) != set)) {@@ -638,6 +735,8 @@ EXPORT_SYMBOL_GPL(ioasid_set_get);void ioasid_set_put_locked (struct ioasid_set *set) { +struct ioasid_nb_args args = {0 }; +struct ioasid_set_nb *curr;struct ioasid_data *entry;unsigned long index;@@ -673,8 +772,24 @@ void ioasid_set_put_locked(struct ioasid_set *set) done_destroy:/* Return the quota back to system pool */ ioasid_capacity_avail += set->quota; -kfree_rcu (set, rcu); +/* Restore pending status of the set NBs */ +spin_lock (&ioasid_nb_lock); +list_for_each_entry (curr, &ioasid_nb_pending_list, list) { +if (curr->token == set->token) { +if (curr->active) + curr->active = false; +else
drivers/iommu/ioasid.c void ioasid_set_put_locked(struct ioasid_set *set) · line 792
Quoted 10 lines
drivers/iommu/ioasid.c +10
-0+pr_warn ("Set token exists but not active!\n" ); + } + } +spin_unlock (&ioasid_nb_lock); + + args.set = set; +atomic_notifier_call_chain (&ioasid_chain,IOASID_SET_FREE , &args); + +kfree_rcu (set, rcu); +pr_debug ("Set freed %d\n" , set->sid);
set might have been freed
drivers/iommu/ioasid.c void *ioasid_find(struct ioasid_set *set, ioasid_t ioasid, · line 1045
Quoted 8 lines
drivers/iommu/ioasid.c +1
-0/* * Token got released right away after the ioasid_set is freed. * If a new set is created immediately with the newly released token, @@ -927,6 +1042,167 @@ void *ioasid_find(struct ioasid_set *set, ioasid_t ioasid, }EXPORT_SYMBOL_GPL (ioasid_find); +int ioasid_register_notifier (struct ioasid_set *set,struct notifier_block *nb)
Maybe add a bit of documentation on the difference with the _mm variant, as well as the @set parameter.
Will this be used by anyone at first? We could introduce only the _mm functions for now.
Quoted 45 lines
drivers/iommu/ioasid.c +45
-0+{ +if (set) +return atomic_notifier_chain_register (&set->nh, nb); +else +return atomic_notifier_chain_register (&ioasid_chain, nb); +} +EXPORT_SYMBOL_GPL (ioasid_register_notifier); + +void ioasid_unregister_notifier (struct ioasid_set *set, +struct notifier_block *nb) +{ +struct ioasid_set_nb *curr; + +spin_lock (&ioasid_nb_lock); +/* +* Pending list is registered with a token without an ioasid_set, +* therefore should not be unregistered directly. +*/ +list_for_each_entry (curr, &ioasid_nb_pending_list, list) { +if (curr->nb == nb) { +pr_warn ("Cannot unregister NB from pending list\n" ); +spin_unlock (&ioasid_nb_lock); +return ; + } + } +spin_unlock (&ioasid_nb_lock); + +if (set) +atomic_notifier_chain_unregister (&set->nh, nb); +else +atomic_notifier_chain_unregister (&ioasid_chain, nb); +} +EXPORT_SYMBOL_GPL (ioasid_unregister_notifier); + +int ioasid_register_notifier_mm (struct mm_struct *mm,struct notifier_block *nb) +{ +struct ioasid_set_nb *curr; +struct ioasid_set *set; +int ret =0 ; + +if (!mm) +return -EINVAL ; + +spin_lock (&ioasid_nb_lock); +
drivers/iommu/ioasid.c void *ioasid_find(struct ioasid_set *set, ioasid_t ioasid, · line 1100
Quoted 10 lines
drivers/iommu/ioasid.c +10
-0+/* Check for duplicates, nb is unique per set */ +list_for_each_entry (curr, &ioasid_nb_pending_list, list) { +if (curr->token == mm && curr->nb == nb) { + ret = -EBUSY ; + goto exit_unlock; + } + } + +/* Check if the token has an existing set */ + set =ioasid_find_mm_set (mm);
Seems to be a deadlock here, as ioasid_find_mm_set() grabs ioasid_allocator_lock while holding ioasid_nb_lock, and ioasid_set_put/get_locked() grabs ioasid_nb_lock while holding ioasid_allocator_lock.
drivers/iommu/ioasid.c void *ioasid_find(struct ioasid_set *set, ioasid_t ioasid, · line 1101
Quoted 1 line
drivers/iommu/ioasid.c +1
-0+if (IS_ERR_OR_NULL (set)) {
Looks a bit off, maybe we can check !set since ioasid_find_mm_set() doesn't return errors.
drivers/iommu/ioasid.c void *ioasid_find(struct ioasid_set *set, ioasid_t ioasid, · line 1103
Quoted 2 lines
drivers/iommu/ioasid.c +2
-0+/* Add to the rsvd list as inactive */ + curr->active = false;
curr isn't valid here
drivers/iommu/ioasid.c void *ioasid_find(struct ioasid_set *set, ioasid_t ioasid, · line 1113
Quoted 10 lines
drivers/iommu/ioasid.c +10
-0+ }else { +/* REVISIT: Only register empty set for now. Can add an option +* in the future to playback existing PASIDs. +*/ +if (set->nr_ioasids) { +pr_warn ("IOASID set %d not empty\n" , set->sid); + ret = -EBUSY ; + goto exit_unlock; + } + curr =kzalloc (sizeof (*curr),GFP_ATOMIC );
As a side-note, I think there's too much atomic allocation in this file, I'd like to try and rework the locking once it stabilizes and I find some time. Do you remember why ioasid_allocator_lock needed to be a spinlock?
Quoted 34 lines
drivers/iommu/ioasid.c +34
-0+if (!curr) { + ret = -ENOMEM ; + goto exit_unlock; + } + curr->token = mm; + curr->nb = nb; + curr->active = true; + curr->set = set; + +/* Set already created, add to the notifier chain */ +atomic_notifier_chain_register (&set->nh, nb); +/* +* Do not hold a reference, if the set gets destroyed, the nb +* entry will be marked inactive. +*/ +ioasid_set_put (set); + } + +list_add (&curr->list, &ioasid_nb_pending_list); + +exit_unlock: +spin_unlock (&ioasid_nb_lock); +return ret; +} +EXPORT_SYMBOL_GPL (ioasid_register_notifier_mm); + +void ioasid_unregister_notifier_mm (struct mm_struct *mm,struct notifier_block *nb) +{ +struct ioasid_set_nb *curr; + +spin_lock (&ioasid_nb_lock); +list_for_each_entry (curr, &ioasid_nb_pending_list, list) { +if (curr->token == mm && curr->nb == nb) { +list_del (&curr->list);
drivers/iommu/ioasid.c void *ioasid_find(struct ioasid_set *set, ioasid_t ioasid, · line 1157
Quoted 10 lines
drivers/iommu/ioasid.c +10
-0+ goto exit_free; + } + } +pr_warn ("No ioasid set found for mm token %llx\n" , (u64 )mm); + goto done_unlock; + +exit_free: +if (curr->active) { +pr_debug ("mm set active, unregister %llx\n" , + (u64 )mm);
%px shows raw pointers, but I would drop this altogether or use %p.
Quoted 7 lines
drivers/iommu/ioasid.c +7
-0+atomic_notifier_chain_unregister (&curr->set->nh, nb); + } +kfree (curr); +done_unlock: +spin_unlock (&ioasid_nb_lock); +return ; +}
drivers/iommu/ioasid.c void *ioasid_find(struct ioasid_set *set, ioasid_t ioasid, · line 1174
Quoted 10 lines
drivers/iommu/ioasid.c +10
-0+EXPORT_SYMBOL_GPL (ioasid_unregister_notifier_mm); + +/** +* ioasid_notify - Send notification on a given IOASID for status change. +* Used by publishers when the status change may affect +* subscriber's internal state. +* +* @ioasid: The IOASID to which the notification will send +* @cmd: The notification event +* @flags: Special instructions, e.g. notify with a set or global
Describe valid values for @cmd and @flags? I guess this function shouldn't accept IOASID_ALLOC, IOASID_FREE etc
Quoted 15 lines
drivers/iommu/ioasid.c +15
-0+*/ +int ioasid_notify (ioasid_t ioasid,enum ioasid_notify_val cmd,unsigned int flags) +{ +struct ioasid_data *ioasid_data; +struct ioasid_nb_args args = {0 }; +int ret =0 ; + +spin_lock (&ioasid_allocator_lock); + ioasid_data =xa_load (&active_allocator->xa, ioasid); +if (!ioasid_data) { +pr_err ("Trying to notify unknown IOASID %u\n" , ioasid); +spin_unlock (&ioasid_allocator_lock); +return -EINVAL ; + } +
drivers/iommu/ioasid.c void *ioasid_find(struct ioasid_set *set, ioasid_t ioasid, · line 1199
Quoted 10 lines
drivers/iommu/ioasid.c +10
-0+ args.id = ioasid; + args.set = ioasid_data->set; + args.pdata = ioasid_data->private; + args.spid = ioasid_data->spid; +if (flags &IOASID_NOTIFY_ALL ) { + ret =atomic_notifier_call_chain (&ioasid_chain, cmd, &args); + }else if (flags &IOASID_NOTIFY_SET ) { + ret =atomic_notifier_call_chain (&ioasid_data->set->nh, + cmd, &args); + }
else ret = -EINVAL?
What about allowing both flags?
Quoted 32 lines
drivers/iommu/ioasid.c +6
-0+spin_unlock (&ioasid_allocator_lock); + +return ret; +} +EXPORT_SYMBOL_GPL (ioasid_notify); +MODULE_AUTHOR ("Jean-Philippe Brucker <[email protected]>" );MODULE_AUTHOR ("Jacob Pan <[email protected]>" );MODULE_DESCRIPTION ("IO Address Space ID (IOASID) allocator" );include/linux/ioasid.h +2
-0@@ -23,6 +23,7 @@ enum ioasid_set_type {* struct ioasid_set - Meta data about ioasid_set * @type: Token types and other features * @token: Unique to identify an IOASID set +* @nh: Notifier for IOASID events within the set * @xa: XArray to store ioasid_set private IDs, can be used for * guest-host IOASID mapping, or just a private IOASID namespace. * @quota: Max number of IOASIDs can be allocated within the set @@ -32,6 +33,7 @@ enum ioasid_set_type {*/ struct ioasid_set {void *token; +struct atomic_notifier_head nh;struct xarray xa;int type;int quota;@@ -56,6 +58,49 @@ struct ioasid_allocator_ops {void *pdata; };
include/linux/ioasid.h struct ioasid_allocator_ops { · line 69
Quoted 10 lines
include/linux/ioasid.h +9
-0+/* Notification data when IOASID status changed */ +enum ioasid_notify_val { +IOASID_ALLOC =1 , +IOASID_FREE , +IOASID_BIND , +IOASID_UNBIND , +IOASID_SET_ALLOC , +IOASID_SET_FREE , +};
May be nicer to prefix these with IOASID_NOTIFY_
Quoted 8 lines
include/linux/ioasid.h +8
-0+ +#define IOASID_NOTIFY_ALL BIT(0) +#define IOASID_NOTIFY_SET BIT(1) +/** +* enum ioasid_notifier_prios - IOASID event notification order +* +* When status of an IOASID changes, users might need to take actions to +* reflect the new state. For example, when an IOASID is freed due to
include/linux/ioasid.h struct ioasid_allocator_ops { · line 87
Quoted 10 lines
include/linux/ioasid.h +10
-0+* exception, the hardware context in virtual CPU, DMA device, and IOMMU +* shall be cleared and drained. Order is required to prevent life cycle +* problems. +*/ +enum ioasid_notifier_prios { +IOASID_PRIO_LAST , +IOASID_PRIO_DEVICE , +IOASID_PRIO_IOMMU , +IOASID_PRIO_CPU , +};
Not used by this patch, can be added later
Quoted 37 lines
include/linux/ioasid.h +24
-0+ +/** +* struct ioasid_nb_args - Argument provided by IOASID core when notifier +* is called. +* @id: The IOASID being notified +* @spid: The set private ID associated with the IOASID +* @set: The IOASID set of @id +* @pdata: The private data attached to the IOASID +*/ +struct ioasid_nb_args { +ioasid_t id; +ioasid_t spid; +struct ioasid_set *set; +void *pdata; +}; +#if IS_ENABLED (CONFIG_IOASID )void ioasid_install_capacity (ioasid_t total);ioasid_t ioasid_get_capacity (void );@@ -75,8 +120,16 @@ void *ioasid_find(struct ioasid_set *set, ioasid_t ioasid, bool (*getter)(void *int ioasid_attach_data (ioasid_t ioasid,void *data);int ioasid_attach_spid (ioasid_t ioasid,ioasid_t spid);ioasid_t ioasid_find_by_spid (struct ioasid_set *set,ioasid_t spid); + +int ioasid_register_notifier (struct ioasid_set *set, +struct notifier_block *nb); +void ioasid_unregister_notifier (struct ioasid_set *set, +struct notifier_block *nb); +int ioasid_register_allocator (struct ioasid_allocator_ops *allocator);void ioasid_unregister_allocator (struct ioasid_allocator_ops *allocator); + +int ioasid_notify (ioasid_t ioasid,enum ioasid_notify_val cmd,unsigned int flags);void ioasid_is_in_set (struct ioasid_set *set,ioasid_t ioasid);int ioasid_get (struct ioasid_set *set,ioasid_t ioasid);int ioasid_get_locked (struct ioasid_set *set,ioasid_t ioasid);
include/linux/ioasid.h void ioasid_put_locked(struct ioasid_set *set, ioasid_t ioasid); · line 142
Quoted 5 lines
include/linux/ioasid.h +2
-0@@ -85,6 +138,9 @@ void ioasid_put_locked(struct ioasid_set *set, ioasid_t ioasid);int ioasid_set_for_each_ioasid (struct ioasid_set *sdata,void (*fn)(ioasid_t id,void *data),void *data); +int ioasid_register_notifier_mm (struct mm_struct *mm,struct notifier_block *nb); +void ioasid_unregister_notifier_mm (struct mm_struct *mm,struct notifier_block *nb);
These need stubs for !CONFIG_IOASID
include/linux/ioasid.h static inline void *ioasid_find(struct ioasid_set *set, ioasid_t ioasid, bool (* · line 183
Quoted 9 lines
include/linux/ioasid.h +2
-0+#else /* !CONFIG_IOASID */ static inline void ioasid_install_capacity (ioasid_t total) {@@ -124,6 +180,20 @@ static inline void *ioasid_find(struct ioasid_set *set, ioasid_t ioasid, bool (*return NULL ; } +static inline int ioasid_register_notifier (struct notifier_block *nb)
Missing set argument
Thanks,
Jean
Quoted 19 lines
include/linux/ioasid.h +13
-0+{ +return -ENOTSUPP ; +} + +static inline void ioasid_unregister_notifier (struct notifier_block *nb) +{ +} + +static inline int ioasid_notify (ioasid_t ioasid,enum ioasid_notify_val cmd,unsigned int flags) +{ +return -ENOTSUPP ; +} +static inline int ioasid_register_allocator (struct ioasid_allocator_ops *allocator) {return -ENOTSUPP ;Signature · 1 line
2.7.4
Re: [PATCH v2 6/9] iommu/ioasid: Introduce notification APIs
Quoted 33 lines · On Tue, 25 Aug 2020 12:26:17 +0200 Jean-Philippe Brucker…
Quoted 32 lines · Jacob Pan
Relations among IOASID users largely follow a publisher-subscriber pattern. E.g. to support guest SVA on Intel Scalable I/O Virtualization (SIOV) enabled platforms, VFIO, IOMMU, device drivers, KVM are all users of IOASIDs. When a state change occurs, VFIO publishes the change event that needs to be processed by other users/subscribers.
This patch introduced two types of notifications: global and per
ioasid_set. The latter is intended for users who only needs to handle events related to the IOASID of a given set. For more information, refer to the kernel documentation atDocumentation/ioasid.rst.Signed-off-by Liu Yi L <[email protected]>Signed-off-by Wu Hao <[email protected]>Signed-off-by Jacob Pan <[email protected]>--- drivers/iommu/ioasid.c | 280++++++++++++++++++++++++++++++++++++++++++++++++-
include/linux/ioasid.h| 70 +++++++++++++ 2 files changed, 348 insertions(+), 2 deletions(-)drivers/iommu/ioasid.c +1
-0@@ -9,8 +9,35 @@#include <linux/spinlock.h> #include <linux/xarray.h> #include <linux/ioasid.h> +#include <linux/sched/mm.h>
Quoted 7 lines
Quoted 4 lines
drivers/iommu/ioasid.c +2
-0static DEFINE_XARRAY_ALLOC (ioasid_sets); +/* +* An IOASID could have multiple consumers where each consumeer may have
consumer
got it
Quoted 16 lines
Quoted 7 lines
+ * hardware contexts associated with IOASIDs.
+ * When a status change occurs, such as IOASID is being freed, notifier chains
+ * are used to keep the consumers in sync.
+ * This is a publisher-subscriber pattern where publisher can change the
+ * state of each IOASID, e.g. alloc/free, bind IOASID to a deviceQuoted 8 lines
and mm.
+ * On the other hand, subscribers gets notified for the state change and
+ * keep local states in sync.
+ *
+ * Currently, the notifier is global. A further optimization could be per
+ * IOASID set notifier chain.
Quoted 2 lines
The patch adds both
right, the comment is old. I will remove the paragraph since it is in the doc.
Quoted 5 lines
Quoted 2 lines
+ */
+staticATOMIC_NOTIFIER_HEAD(ioasid_chain);"
ioasid_notifier" may be clearer
will do.
Quoted 26 lines
Quoted 26 lines
drivers/iommu/ioasid.c +13
-0+ +/* List to hold pending notification block registrations */ +static LIST_HEAD (ioasid_nb_pending_list); +static DEFINE_SPINLOCK (ioasid_nb_lock); +struct ioasid_set_nb { +struct list_head list; +struct notifier_block *nb; +void *token; +struct ioasid_set *set; +bool active; +}; +enum ioasid_state {IOASID_STATE_INACTIVE ,IOASID_STATE_ACTIVE ,@@ -394,6 +421,7 @@ EXPORT_SYMBOL_GPL(ioasid_find_by_spid);ioasid_t ioasid_alloc (struct ioasid_set *set,ioasid_t min,ioasid_t max,void *private) { +struct ioasid_nb_args args;struct ioasid_data *data;void *adata;ioasid_t id =INVALID_IOASID ;@@ -445,8 +473,14 @@ ioasid_t ioasid_alloc(struct ioasid_set *set, ioasid_t min, ioasid_t max, goto exit_free; }
Quoted 9 lines
drivers/iommu/ioasid.c ioasid_t ioasid_alloc(struct ioasid_set *set, ioasid_t min, ioasid_t max, · line 479
Quoted 6 lines
drivers/iommu/ioasid.c +4
-1set->nr_ioasids++; - goto done_unlock; + args.id = id; +/* Set private ID is not attached during allocation */ + args.spid =INVALID_IOASID ; + args.set = set;args.pdata is uninitialized
right, it should be
args.pdata = data->private;
Quoted 5 lines
drivers/iommu/ioasid.c ioasid_t ioasid_alloc(struct ioasid_set *set, · line 481
Quoted 2 lines
drivers/iommu/ioasid.c +1
-0+atomic_notifier_call_chain (&set->nh,IOASID_ALLOC , &args);No global notification?
There hasn't been a need since the only global notifier listener is vt-d driver which cares about FREE event only.
Quoted 94 lines
Quoted 94 lines
drivers/iommu/ioasid.c +3
-0+spin_unlock (&ioasid_allocator_lock); +return id; exit_free:kfree (data); done_unlock:@@ -479,6 +513,7 @@ static void ioasid_do_free(struct ioasid_data *data)static void ioasid_free_locked (struct ioasid_set *set,ioasid_t ioasid) { +struct ioasid_nb_args args;struct ioasid_data *data; data =xa_load (&active_allocator->xa, ioasid);@@ -491,7 +526,16 @@ static void ioasid_free_locked(struct
ioasid_set*set,ioasid_tioasid)pr_warn("Cannot free IOASID %u due to set ownership\n", ioasid); return;drivers/iommu/ioasid.c +7
-0} + data->state =IOASID_STATE_FREE_PENDING ; +/* Notify all users that this IOASID is being freed */ + args.id = ioasid; + args.spid = data->spid; + args.pdata = data->private; + args.set = data->set; + atomic_notifier_call_chain(&ioasid_chain, IOASID_FREE,&args);
drivers/iommu/ioasid.c +11
-0+/* Notify the ioasid_set for per set users */ +atomic_notifier_call_chain (&set->nh,IOASID_FREE , &args);if (!refcount_dec_and_test (&data->users))return ;@@ -514,6 +558,28 @@ void ioasid_free(struct ioasid_set *set, ioasid_t ioasid) }EXPORT_SYMBOL_GPL (ioasid_free); +static void ioasid_add_pending_nb (struct ioasid_set *set) +{ +struct ioasid_set_nb *curr; + +if (set->type !=IOASID_SET_TYPE_MM ) +return ; + +/* +* Check if there are any pending nb requests for the given token, if so
drivers/iommu/ioasid.c +6
-0+* add them to the notifier chain. +*/ +spin_lock (&ioasid_nb_lock); +list_for_each_entry (curr, &ioasid_nb_pending_list, list) { +if (curr->token == set->token && !curr->active) { + atomic_notifier_chain_register(&set->nh,
curr->nb);drivers/iommu/ioasid.c +11
-0+ curr->set = set; + curr->active = true; + } + } +spin_unlock (&ioasid_nb_lock); +} +/** * ioasid_alloc_set - Allocate a new IOASID set for a given token * @@ -601,6 +667,13 @@ struct ioasid_set *ioasid_alloc_set(void *token, ioasid_t quota, int type) sdata->quota = quota; sdata->sid = id;refcount_set (&sdata->ref,1 ); +ATOMIC_INIT_NOTIFIER_HEAD (&sdata->nh); + +/* +* Check if there are any pending nb requests for the given token, if so
drivers/iommu/ioasid.c +3
-0+* add them to the notifier chain. +*/ +ioasid_add_pending_nb (sdata);/* * Per set XA is used to store private IDs within the set, get ready @@ -617,6 +690,30 @@ struct
ioasid_set*ioasid_alloc_set(void *token,ioasid_tquota, int type) }
EXPORT_SYMBOL_GPL(ioasid_alloc_set);drivers/iommu/ioasid.c +6
-0+ +/* +* ioasid_find_mm_set - Retrieve IOASID set with mm token +* Take a reference of the set if found. +*/ +static struct ioasid_set *ioasid_find_mm_set(struct mm_struct*token) +{
drivers/iommu/ioasid.c +5
-0+struct ioasid_set *sdata, *set =NULL ; +unsigned long index; + +spin_lock (&ioasid_allocator_lock); +
Quoted 9 lines
drivers/iommu/ioasid.c struct ioasid_set *ioasid_alloc_set(void *token, ioasid_t quota, int type) · line 709
Quoted 6 lines
drivers/iommu/ioasid.c +2
-0+xa_for_each (&ioasid_sets, index, sdata) { + if (sdata->type == IOASID_SET_TYPE_MM &&
sdata->token== token) {drivers/iommu/ioasid.c +3
-0+refcount_inc (&sdata->ref); + set = sdata; + goto exit_unlock;Or just break
right, but I missed set = NULL outside xa_for_each(). so I have to keep this goto, i.e.
spin_lock(&ioasid_allocator_lock);
xa_for_each(&ioasid_sets, index, set) {
if (set->type == IOASID_SET_TYPE_MM && set->token ==
token) {
refcount_inc(&set->ref);
goto exit_unlock;
}
}
set = NULL;
exit_unlock:
spin_unlock(&ioasid_allocator_lock);
return set;Quoted 37 lines
Quoted 37 lines
drivers/iommu/ioasid.c +20
-1+ } + } +exit_unlock: +spin_unlock (&ioasid_allocator_lock); +return set; +} +void ioasid_set_get_locked (struct ioasid_set *set) {if (WARN_ON (xa_load (&ioasid_sets, set->sid) != set)) {@@ -638,6 +735,8 @@ EXPORT_SYMBOL_GPL(ioasid_set_get);void ioasid_set_put_locked (struct ioasid_set *set) { +struct ioasid_nb_args args = {0 }; +struct ioasid_set_nb *curr;struct ioasid_data *entry;unsigned long index;@@ -673,8 +772,24 @@ void ioasid_set_put_locked(struct ioasid_set *set) done_destroy:/* Return the quota back to system pool */ ioasid_capacity_avail += set->quota; -kfree_rcu (set, rcu); +/* Restore pending status of the set NBs */ +spin_lock (&ioasid_nb_lock); +list_for_each_entry (curr, &ioasid_nb_pending_list, list) { +if (curr->token == set->token) { +if (curr->active) + curr->active = false; +else +pr_warn ("Set token exists but not active!\n"); + } + } +spin_unlock (&ioasid_nb_lock);
Quoted 9 lines
drivers/iommu/ioasid.c void ioasid_set_put_locked(struct ioasid_set *set) · line 792
Quoted 6 lines
drivers/iommu/ioasid.c +3
-0+ + args.set = set; + atomic_notifier_call_chain(&ioasid_chain, IOASID_SET_FREE,&args); +
drivers/iommu/ioasid.c +2
-0+kfree_rcu (set, rcu); +pr_debug ("Set freed %d\n" , set->sid);set might have been freed
right, will delete this.
Quoted 8 lines
Quoted 8 lines
drivers/iommu/ioasid.c +0
-0/* * Token got released right away after the ioasid_set is freed.
* If a new set is created immediately with the newly
released token, @@ -927,6 +1042,167 @@ void *ioasid_find(structioasid_set*set,ioasid_tioasid, }
EXPORT_SYMBOL_GPL(ioasid_find);
Quoted 9 lines
Quoted 2 lines
+int
ioasid_register_notifier(struct ioasid_set *set, struct notifier_block *nb)Maybe add a bit of documentation on the difference with the
_mmvariant, as well as the @set parameter.Will this be used by anyone at first? We could introduce only the
_mmfunctions for now.
We do need both variants, VT-d driver registers global notifier with set=NULL, KVM registers notifier on the mm_struct pointer. How about the following comments:
/**
* ioasid_register_notifier_mm - Register a notifier block on the
IOASID set
* created by the mm_struct pointer as
the token *
* @mm: the mm_struct token of the ioasid_set
* @nb: notfier block to be registered on the ioasid_set
*
* This a variant of ioasid_register_notifier() where the caller
intends to
* listen to IOASID events belong the ioasid_set created under the same
* process. Caller is not aware of the ioasid_set, no need to hold
reference
* of the ioasid_set.
*/
Quoted 55 lines
Quoted 55 lines
drivers/iommu/ioasid.c +3
-0+{ +if (set) + return atomic_notifier_chain_register(&set->nh,nb);
drivers/iommu/ioasid.c +2
-0+else +return
atomic_notifier_chain_register(&ioasid_chain, nb); +}drivers/iommu/ioasid.c +10
-0+EXPORT_SYMBOL_GPL (ioasid_register_notifier); + +void ioasid_unregister_notifier (struct ioasid_set *set, +struct notifier_block *nb) +{ +struct ioasid_set_nb *curr; + +spin_lock (&ioasid_nb_lock); +/* +* Pending list is registered with a token without an
ioasid_set,drivers/iommu/ioasid.c +5
-0+* therefore should not be unregistered directly. +*/ +list_for_each_entry (curr, &ioasid_nb_pending_list, list) { +if (curr->nb == nb) { + pr_warn("Cannot unregister NB from pendinglist\n");
drivers/iommu/ioasid.c +10
-0+spin_unlock (&ioasid_nb_lock); +return ; + } + } +spin_unlock (&ioasid_nb_lock); + +if (set) +atomic_notifier_chain_unregister (&set->nh, nb); +else + atomic_notifier_chain_unregister(&ioasid_chain,nb); +}
drivers/iommu/ioasid.c +3
-0+EXPORT_SYMBOL_GPL (ioasid_unregister_notifier); + +int ioasid_register_notifier_mm(struct mm_struct *mm, struct
notifier_block*nb) +{drivers/iommu/ioasid.c +16
-0+struct ioasid_set_nb *curr; +struct ioasid_set *set; +int ret =0 ; + +if (!mm) +return -EINVAL ; + +spin_lock (&ioasid_nb_lock); + +/* Check for duplicates, nb is unique per set */ +list_for_each_entry (curr, &ioasid_nb_pending_list, list) { +if (curr->token == mm && curr->nb == nb) { + ret = -EBUSY ; + goto exit_unlock; + } + }
Quoted 9 lines
drivers/iommu/ioasid.c void *ioasid_find(struct ioasid_set *set, ioasid_t ioasid, · line 1100
Quoted 3 lines
drivers/iommu/ioasid.c +3
-0+ +/* Check if the token has an existing set */ + set =ioasid_find_mm_set (mm);Seems to be a deadlock here, as
ioasid_find_mm_set()grabsioasid_allocator_lockwhile holdingioasid_nb_lock, and ioasid_set_put/get_locked() grabsioasid_nb_lockwhile holdingioasid_allocator_lock.
Good catch, I will move the nb_lock before allocator lock in ioasid_set_put.
Quoted 5 lines
drivers/iommu/ioasid.c void *ioasid_find(struct ioasid_set *set, ioasid_t ioasid, · line 1101
Quoted 1 line
drivers/iommu/ioasid.c +1
-0+if (IS_ERR_OR_NULL (set)) {Looks a bit off, maybe we can check !set since
ioasid_find_mm_set()doesn't return errors.
will do.
Quoted 5 lines
drivers/iommu/ioasid.c void *ioasid_find(struct ioasid_set *set, ioasid_t ioasid, · line 1103
Quoted 2 lines
drivers/iommu/ioasid.c +2
-0+/* Add to the rsvd list as inactive */ + curr->active = false;curr isn't valid here
This is the case where the IOASID set has not been created yet, so we just put the NB on the pending list. Am I missing your point? The use case is that when a guest with assigned devices is launched, KVM and VFIO can be initialized in any order. If KVM starts before VFIO, which creates the IOASID set, the KVM notifier block will be registered on the pending list as inactive.
Quoted 9 lines
Quoted 9 lines
drivers/iommu/ioasid.c +2
-0+ }else { +/* REVISIT: Only register empty set for now. Can add an option
drivers/iommu/ioasid.c +4
-0+* in the future to playback existing PASIDs. +*/ +if (set->nr_ioasids) { + pr_warn("IOASID set %d not empty\n",
set->sid);drivers/iommu/ioasid.c +1
-0+ ret = -EBUSY ;
Quoted 9 lines
drivers/iommu/ioasid.c void *ioasid_find(struct ioasid_set *set, ioasid_t ioasid, · line 1113
Quoted 3 lines
drivers/iommu/ioasid.c +3
-0+ goto exit_unlock; + } + curr =kzalloc (sizeof (*curr),GFP_ATOMIC );As a side-note, I think there's too much atomic allocation in this file, I'd like to try and rework the locking once it stabilizes and I find some time. Do you remember why
ioasid_allocator_lockneeded to be a spinlock?
The spinlock was needed for calling ioasid_free from mmu notifier and mm_drop().
Quoted 41 lines
Quoted 41 lines
drivers/iommu/ioasid.c +10
-0+if (!curr) { + ret = -ENOMEM ; + goto exit_unlock; + } + curr->token = mm; + curr->nb = nb; + curr->active = true; + curr->set = set; + +/* Set already created, add to the notifier chain */
drivers/iommu/ioasid.c +3
-0+atomic_notifier_chain_register (&set->nh, nb); +/* +* Do not hold a reference, if the set gets destroyed, the nb
drivers/iommu/ioasid.c +14
-0+* entry will be marked inactive. +*/ +ioasid_set_put (set); + } + +list_add (&curr->list, &ioasid_nb_pending_list); + +exit_unlock: +spin_unlock (&ioasid_nb_lock); +return ret; +} +EXPORT_SYMBOL_GPL (ioasid_register_notifier_mm); + +void ioasid_unregister_notifier_mm(struct mm_struct *mm, struct
notifier_block*nb) +{drivers/iommu/ioasid.c +10
-0+struct ioasid_set_nb *curr; + +spin_lock (&ioasid_nb_lock); +list_for_each_entry (curr, &ioasid_nb_pending_list, list) { +if (curr->token == mm && curr->nb == nb) { +list_del (&curr->list); + goto exit_free; + } + } + pr_warn("No ioasid set found for mm token %llx\n",(u64)mm);
Quoted 9 lines
drivers/iommu/ioasid.c void *ioasid_find(struct ioasid_set *set, ioasid_t ioasid, · line 1157
Quoted 6 lines
drivers/iommu/ioasid.c +6
-0+ goto done_unlock; + +exit_free: +if (curr->active) { +pr_debug ("mm set active, unregister %llx\n" , + (u64 )mm);%px shows raw pointers, but I would drop this altogether or use %p.
will drop.
Quoted 16 lines
Quoted 16 lines
drivers/iommu/ioasid.c +1
-0+ atomic_notifier_chain_unregister(&curr->set->nh,nb);
drivers/iommu/ioasid.c +10
-0+ } +kfree (curr); +done_unlock: +spin_unlock (&ioasid_nb_lock); +return ; +} +EXPORT_SYMBOL_GPL (ioasid_unregister_notifier_mm); + +/** +* ioasid_notify - Send notification on a given IOASID for status change.
+ * Used by publishers when the status change may
affect
+ * subscriber's internal state.
Quoted 9 lines
Quoted 5 lines
+ *
+ * @ioasid: The IOASID to which the notification will send
+ * @cmd: The notification event
+ * @flags: Special instructions, e.g. notify with a set or
globalDescribe valid values for @cmd and @flags? I guess this function shouldn't accept
IOASID_ALLOC,IOASID_FREEetc
Good point. only IOASID_BIND and IOASID_UNBIND are allowed. Will add a check
/* IOASID_FREE/ALLOC are internal events emitted by IOASID core only */ if (cmd <= IOASID_FREE) return -EINVAL;
And comment:
* @cmd: Notification event sent by IOASID external users, can be
* IOASID_BIND or IOASID_UNBIND.
*
* @flags: Special instructions, e.g. notify within a set or global by
* IOASID_NOTIFY_SET or IOASID_NOTIFY_ALL flags
Quoted 23 lines
Quoted 23 lines
+ */
+intioasid_notify(ioasid_t ioasid, enum ioasid_notify_val cmd, unsigned int flags)+{drivers/iommu/ioasid.c +8
-0+struct ioasid_data *ioasid_data; +struct ioasid_nb_args args = {0 }; +int ret =0 ; + +spin_lock (&ioasid_allocator_lock); + ioasid_data =xa_load (&active_allocator->xa, ioasid); +if (!ioasid_data) { + pr_err("Trying to notify unknown IOASID %u\n",ioasid);
drivers/iommu/ioasid.c +10
-0+spin_unlock (&ioasid_allocator_lock); +return -EINVAL ; + } + + args.id = ioasid; + args.set = ioasid_data->set; + args.pdata = ioasid_data->private; + args.spid = ioasid_data->spid; +if (flags &IOASID_NOTIFY_ALL ) { + ret = atomic_notifier_call_chain(&ioasid_chain,cmd, &args);
Quoted 9 lines
drivers/iommu/ioasid.c
Quoted 5 lines
drivers/iommu/ioasid.c +2
-0+ }else if (flags &IOASID_NOTIFY_SET ) { + ret =atomic_notifier_call_chain(&ioasid_data->set->nh,
drivers/iommu/ioasid.c +2
-0+ cmd, &args); + }else ret =
-EINVAL?
What about allowing both flags?
both flags should be allowed. Let me add a check for valid flags upfront, then remove the else.
Quoted 35 lines
Quoted 35 lines
drivers/iommu/ioasid.c +6
-0+spin_unlock (&ioasid_allocator_lock); + +return ret; +} +EXPORT_SYMBOL_GPL (ioasid_notify); + MODULE_AUTHOR("Jean-Philippe Brucker<[email protected]>");
MODULE_AUTHOR("Jacob Pan <[email protected]>");MODULE_DESCRIPTION("IO Address Space ID (IOASID) allocator"); diff --gita/include/linux/ioasid.hb/include/linux/ioasid.hindexd4b3e83672f6..572111cd3b4b100644include/linux/ioasid.h +1
-0@@ -23,6 +23,7 @@ enum ioasid_set_type {* struct ioasid_set - Meta data about ioasid_set * @type: Token types and other features * @token: Unique to identify an IOASID set +* @nh: Notifier for IOASID events within the set * @xa: XArray to store ioasid_set private IDs, can be used for
* guest-host IOASID mapping, or just a private
IOASID namespace.
* @quota: Max number of IOASIDs can be allocated within
the set @@ -32,6 +33,7 @@ enumioasid_set_type{
*/
structioasid_set{include/linux/ioasid.h +1
-0void *token; +struct atomic_notifier_head nh;struct xarray xa;int type;int quota;@@ -56,6 +58,49 @@ struct ioasid_allocator_ops {void *pdata; };
Quoted 12 lines
include/linux/ioasid.h struct ioasid_allocator_ops { · line 69
Quoted 9 lines
include/linux/ioasid.h +9
-0+/* Notification data when IOASID status changed */ +enum ioasid_notify_val { +IOASID_ALLOC =1 , +IOASID_FREE , +IOASID_BIND , +IOASID_UNBIND , +IOASID_SET_ALLOC , +IOASID_SET_FREE , +};May be nicer to prefix these with
IOASID_NOTIFY_
yes, good idea. also add FLAG_ to the flags below
Quoted 16 lines
Quoted 16 lines
include/linux/ioasid.h +7
-0+ +#define IOASID_NOTIFY_ALL BIT(0) +#define IOASID_NOTIFY_SET BIT(1) +/** +* enum ioasid_notifier_prios - IOASID event notification order +* +* When status of an IOASID changes, users might need to take actions to
+ * reflect the new state. For example, when an IOASID is freed due to
+ * exception, the hardware context in virtual CPU, DMA device, and IOMMU
+ * shall be cleared and drained. Order is required to prevent life cycle
+ * problems.
+ */
Quoted 9 lines
include/linux/ioasid.h struct ioasid_allocator_ops { · line 87
Quoted 6 lines
include/linux/ioasid.h +6
-0+enum ioasid_notifier_prios { +IOASID_PRIO_LAST , +IOASID_PRIO_DEVICE , +IOASID_PRIO_IOMMU , +IOASID_PRIO_CPU , +};Not used by this patch, can be added later
will do
Quoted 38 lines
Quoted 38 lines
include/linux/ioasid.h +3
-0+ +/** +* struct ioasid_nb_args - Argument provided by IOASID core when notifier
+ * is called.
+ * @id: The IOASID being notified
+ * @spid: The set private ID associated with the IOASID
+ * @set: The IOASID set of @id
+ * @pdata: The private data attached to the IOASID
+ */include/linux/ioasid.h +7
-0+struct ioasid_nb_args { +ioasid_t id; +ioasid_t spid; +struct ioasid_set *set; +void *pdata; +}; +#if IS_ENABLED (CONFIG_IOASID )void ioasid_install_capacity (ioasid_t total);ioasid_t ioasid_get_capacity (void );@@ -75,8 +120,16 @@ void *ioasid_find(struct ioasid_set *set,
ioasid_tioasid, bool (*getter)(void * intioasid_attach_data(ioasid_t ioasid, void *data); intioasid_attach_spid(ioasid_t ioasid, ioasid_t spid);ioasid_tioasid_find_by_spid(struct ioasid_set *set, ioasid_t spid); +include/linux/ioasid.h +5
-0+int ioasid_register_notifier (struct ioasid_set *set, +struct notifier_block *nb); +void ioasid_unregister_notifier (struct ioasid_set *set, +struct notifier_block *nb); + int ioasid_register_allocator(struct ioasid_allocator_ops*allocator); void
ioasid_unregister_allocator(struct ioasid_allocator_ops *allocator); +
+intioasid_notify(ioasid_t ioasid, enum ioasid_notify_val cmd, unsigned int flags); voidioasid_is_in_set(struct ioasid_set *set, ioasid_t ioasid); intioasid_get(struct ioasid_set *set, ioasid_t ioasid); intioasid_get_locked(struct ioasid_set *set, ioasid_t ioasid); @@ -85,6 +138,9 @@ void ioasid_put_locked(struct
Quoted 9 lines
Quoted 6 lines
ioasid_set*set,ioasid_tioasid); intioasid_set_for_each_ioasid(struct ioasid_set *sdata, void (*fn)(ioasid_t id, void *data), void *data);
+intioasid_register_notifier_mm(struct mm_struct *mm, struct notifier_block *nb); +voidioasid_unregister_notifier_mm(struct mm_struct *mm, struct notifier_block *nb);These need stubs for !CONFIG_IOASID
got it
Quoted 8 lines
Quoted 8 lines
include/linux/ioasid.h +1
-0+#else /* !CONFIG_IOASID */ static inline void ioasid_install_capacity (ioasid_t total) {@@ -124,6 +180,20 @@ static inline void *ioasid_find(struct ioasid_set *set, ioasid_t ioasid, bool (* return NULL; }
Quoted 5 lines
include/linux/ioasid.h static inline void *ioasid_find(struct · line 184
Quoted 2 lines
include/linux/ioasid.h +1
-0+static inline int ioasid_register_notifier (struct notifier_block *nb)Missing set argument
got it
Thanks a lot!
Quoted 18 lines
Thanks,
JeanQuoted 15 lines
include/linux/ioasid.h +11
-0+{ +return -ENOTSUPP ; +} + +static inline void ioasid_unregister_notifier (struct notifier_block *nb) +{ +} + +static inline int ioasid_notify (ioasid_t ioasid,enum ioasid_notify_val cmd, unsigned int flags) +{ +return -ENOTSUPP ; +} + static inline int ioasid_register_allocator(struct
ioasid_allocator_ops*allocator) {
Quoted 8 lines
Quoted 4 lines
return
-ENOTSUPP;Signature · 1 line
2.7.4_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
[Jacob Pan]
Re: [PATCH v2 6/9] iommu/ioasid: Introduce notification APIs
In reply to Jacob PanHi Jacob,
Quoted 25 lines · Jacob Pan
Relations among IOASID users largely follow a publisher-subscriber pattern. E.g. to support guest SVA on Intel Scalable I/O Virtualization (SIOV) enabled platforms, VFIO, IOMMU, device drivers, KVM are all users of IOASIDs. When a state change occurs, VFIO publishes the change event that needs to be processed by other users/subscribers.
This patch introduced two types of notifications: global and per
ioasid_set. The latter is intended for users who only needs to handle events related to the IOASID of a given set.
For more information, refer to the kernel documentation atDocumentation/ioasid.rst.Signed-off-by Liu Yi L <[email protected]>Signed-off-by Wu Hao <[email protected]>Signed-off-by Jacob Pan <[email protected]>--- drivers/iommu/ioasid.c | 280 ++++++++++++++++++++++++++++++++++++++++++++++++- include/linux/ioasid.h | 70 +++++++++++++ 2 files changed, 348 insertions(+), 2 deletions(-)drivers/iommu/ioasid.c +0
-0
drivers/iommu/ioasid.c line 16
Quoted 8 lines
drivers/iommu/ioasid.c +3
-0@@ -9,8 +9,35 @@#include <linux/spinlock.h> #include <linux/xarray.h> #include <linux/ioasid.h> +#include <linux/sched/mm.h> static DEFINE_XARRAY_ALLOC (ioasid_sets); +/* +* An IOASID could have multiple consumers where each consumeer may have
can have multiple consumers
drivers/iommu/ioasid.c line 18
Quoted 2 lines
drivers/iommu/ioasid.c +2
-0+* hardware contexts associated with IOASIDs. +* When a status change occurs, such as IOASID is being freed, notifier chains
s/such as IOASID is being freed/, like on IOASID deallocation,
Quoted 36 lines
drivers/iommu/ioasid.c +23
-0+* are used to keep the consumers in sync. +* This is a publisher-subscriber pattern where publisher can change the +* state of each IOASID, e.g. alloc/free, bind IOASID to a device and mm. +* On the other hand, subscribers gets notified for the state change and +* keep local states in sync. +* +* Currently, the notifier is global. A further optimization could be per +* IOASID set notifier chain. +*/ +static ATOMIC_NOTIFIER_HEAD (ioasid_chain); + +/* List to hold pending notification block registrations */ +static LIST_HEAD (ioasid_nb_pending_list); +static DEFINE_SPINLOCK (ioasid_nb_lock); +struct ioasid_set_nb { +struct list_head list; +struct notifier_block *nb; +void *token; +struct ioasid_set *set; +bool active; +}; +enum ioasid_state {IOASID_STATE_INACTIVE ,IOASID_STATE_ACTIVE ,@@ -394,6 +421,7 @@ EXPORT_SYMBOL_GPL(ioasid_find_by_spid);ioasid_t ioasid_alloc (struct ioasid_set *set,ioasid_t min,ioasid_t max,void *private) { +struct ioasid_nb_args args;struct ioasid_data *data;void *adata;ioasid_t id =INVALID_IOASID ;@@ -445,8 +473,14 @@ ioasid_t ioasid_alloc(struct ioasid_set *set, ioasid_t min, ioasid_t max, goto exit_free; }
drivers/iommu/ioasid.c ioasid_t ioasid_alloc(struct ioasid_set *set, ioasid_t min, ioasid_t max, · line 483
Quoted 10 lines
drivers/iommu/ioasid.c +7
-1set->nr_ioasids++; - goto done_unlock; + args.id = id; +/* Set private ID is not attached during allocation */ + args.spid =INVALID_IOASID ; + args.set = set; +atomic_notifier_call_chain (&set->nh,IOASID_ALLOC , &args); +spin_unlock (&ioasid_allocator_lock); +return id;
spurious change
Quoted 12 lines
drivers/iommu/ioasid.c +1
-0exit_free:kfree (data); done_unlock:@@ -479,6 +513,7 @@ static void ioasid_do_free(struct ioasid_data *data)static void ioasid_free_locked (struct ioasid_set *set,ioasid_t ioasid) { +struct ioasid_nb_args args;struct ioasid_data *data; data =xa_load (&active_allocator->xa, ioasid);
drivers/iommu/ioasid.c static void ioasid_free_locked(struct ioasid_set *set, ioasid_t ioasid) · line 529
Quoted 4 lines
drivers/iommu/ioasid.c +1
-0@@ -491,7 +526,16 @@ static void ioasid_free_locked(struct ioasid_set *set, ioasid_t ioasid)pr_warn ("Cannot free IOASID %u due to set ownership\n" , ioasid);return ; } +
spurious new line
drivers/iommu/ioasid.c static void ioasid_free_locked(struct ioasid_set *set, ioasid_t ioasid) · line 541
Quoted 12 lines
drivers/iommu/ioasid.c +8
-0data->state =IOASID_STATE_FREE_PENDING ; +/* Notify all users that this IOASID is being freed */ + args.id = ioasid; + args.spid = data->spid; + args.pdata = data->private; + args.set = data->set; +atomic_notifier_call_chain (&ioasid_chain,IOASID_FREE , &args); +/* Notify the ioasid_set for per set users */ +atomic_notifier_call_chain (&set->nh,IOASID_FREE , &args);if (!refcount_dec_and_test (&data->users))return ;
Shouldn't we call the notifier only when ref count == 0?
Quoted 130 lines
drivers/iommu/ioasid.c +89
-1@@ -514,6 +558,28 @@ void ioasid_free(struct ioasid_set *set, ioasid_t ioasid) }EXPORT_SYMBOL_GPL (ioasid_free); +static void ioasid_add_pending_nb (struct ioasid_set *set) +{ +struct ioasid_set_nb *curr; + +if (set->type !=IOASID_SET_TYPE_MM ) +return ; + +/* +* Check if there are any pending nb requests for the given token, if so +* add them to the notifier chain. +*/ +spin_lock (&ioasid_nb_lock); +list_for_each_entry (curr, &ioasid_nb_pending_list, list) { +if (curr->token == set->token && !curr->active) { +atomic_notifier_chain_register (&set->nh, curr->nb); + curr->set = set; + curr->active = true; + } + } +spin_unlock (&ioasid_nb_lock); +} +/** * ioasid_alloc_set - Allocate a new IOASID set for a given token * @@ -601,6 +667,13 @@ struct ioasid_set *ioasid_alloc_set(void *token, ioasid_t quota, int type) sdata->quota = quota; sdata->sid = id;refcount_set (&sdata->ref,1 ); +ATOMIC_INIT_NOTIFIER_HEAD (&sdata->nh); + +/* +* Check if there are any pending nb requests for the given token, if so +* add them to the notifier chain. +*/ +ioasid_add_pending_nb (sdata);/* * Per set XA is used to store private IDs within the set, get ready @@ -617,6 +690,30 @@ struct ioasid_set *ioasid_alloc_set(void *token, ioasid_t quota, int type) }EXPORT_SYMBOL_GPL (ioasid_alloc_set); + +/* +* ioasid_find_mm_set - Retrieve IOASID set with mm token +* Take a reference of the set if found. +*/ +static struct ioasid_set *ioasid_find_mm_set (struct mm_struct *token) +{ +struct ioasid_set *sdata, *set =NULL ; +unsigned long index; + +spin_lock (&ioasid_allocator_lock); + +xa_for_each (&ioasid_sets, index, sdata) { +if (sdata->type ==IOASID_SET_TYPE_MM && sdata->token == token) { +refcount_inc (&sdata->ref); + set = sdata; + goto exit_unlock; + } + } +exit_unlock: +spin_unlock (&ioasid_allocator_lock); +return set; +} +void ioasid_set_get_locked (struct ioasid_set *set) {if (WARN_ON (xa_load (&ioasid_sets, set->sid) != set)) {@@ -638,6 +735,8 @@ EXPORT_SYMBOL_GPL(ioasid_set_get);void ioasid_set_put_locked (struct ioasid_set *set) { +struct ioasid_nb_args args = {0 }; +struct ioasid_set_nb *curr;struct ioasid_data *entry;unsigned long index;@@ -673,8 +772,24 @@ void ioasid_set_put_locked(struct ioasid_set *set) done_destroy:/* Return the quota back to system pool */ ioasid_capacity_avail += set->quota; -kfree_rcu (set, rcu); +/* Restore pending status of the set NBs */ +spin_lock (&ioasid_nb_lock); +list_for_each_entry (curr, &ioasid_nb_pending_list, list) { +if (curr->token == set->token) { +if (curr->active) + curr->active = false; +else +pr_warn ("Set token exists but not active!\n" ); + } + } +spin_unlock (&ioasid_nb_lock); + + args.set = set; +atomic_notifier_call_chain (&ioasid_chain,IOASID_SET_FREE , &args); + +kfree_rcu (set, rcu); +pr_debug ("Set freed %d\n" , set->sid);/* * Token got released right away after the ioasid_set is freed. * If a new set is created immediately with the newly released token, @@ -927,6 +1042,167 @@ void *ioasid_find(struct ioasid_set *set, ioasid_t ioasid, }EXPORT_SYMBOL_GPL (ioasid_find); +int ioasid_register_notifier (struct ioasid_set *set,struct notifier_block *nb) +{ +if (set) +return atomic_notifier_chain_register (&set->nh, nb); +else +return atomic_notifier_chain_register (&ioasid_chain, nb); +} +EXPORT_SYMBOL_GPL (ioasid_register_notifier); + +void ioasid_unregister_notifier (struct ioasid_set *set, +struct notifier_block *nb) +{ +struct ioasid_set_nb *curr; + +spin_lock (&ioasid_nb_lock); +/* +* Pending list is registered with a token without an ioasid_set,
drivers/iommu/ioasid.c void *ioasid_find(struct ioasid_set *set, ioasid_t ioasid, · line 1071
Quoted 10 lines
drivers/iommu/ioasid.c +10
-0+* therefore should not be unregistered directly. +*/ +list_for_each_entry (curr, &ioasid_nb_pending_list, list) { +if (curr->nb == nb) { +pr_warn ("Cannot unregister NB from pending list\n" ); +spin_unlock (&ioasid_nb_lock); +return ; + } + } +spin_unlock (&ioasid_nb_lock);
is it safe to release the lock here? What does prevent another NB to be added to ioasid_nb_pending_list after that?
Quoted 142 lines
drivers/iommu/ioasid.c +134
-0+ +if (set) +atomic_notifier_chain_unregister (&set->nh, nb); +else +atomic_notifier_chain_unregister (&ioasid_chain, nb); +} +EXPORT_SYMBOL_GPL (ioasid_unregister_notifier); + +int ioasid_register_notifier_mm (struct mm_struct *mm,struct notifier_block *nb) +{ +struct ioasid_set_nb *curr; +struct ioasid_set *set; +int ret =0 ; + +if (!mm) +return -EINVAL ; + +spin_lock (&ioasid_nb_lock); + +/* Check for duplicates, nb is unique per set */ +list_for_each_entry (curr, &ioasid_nb_pending_list, list) { +if (curr->token == mm && curr->nb == nb) { + ret = -EBUSY ; + goto exit_unlock; + } + } + +/* Check if the token has an existing set */ + set =ioasid_find_mm_set (mm); +if (IS_ERR_OR_NULL (set)) { +/* Add to the rsvd list as inactive */ + curr->active = false; + }else { +/* REVISIT: Only register empty set for now. Can add an option +* in the future to playback existing PASIDs. +*/ +if (set->nr_ioasids) { +pr_warn ("IOASID set %d not empty\n" , set->sid); + ret = -EBUSY ; + goto exit_unlock; + } + curr =kzalloc (sizeof (*curr),GFP_ATOMIC ); +if (!curr) { + ret = -ENOMEM ; + goto exit_unlock; + } + curr->token = mm; + curr->nb = nb; + curr->active = true; + curr->set = set; + +/* Set already created, add to the notifier chain */ +atomic_notifier_chain_register (&set->nh, nb); +/* +* Do not hold a reference, if the set gets destroyed, the nb +* entry will be marked inactive. +*/ +ioasid_set_put (set); + } + +list_add (&curr->list, &ioasid_nb_pending_list); + +exit_unlock: +spin_unlock (&ioasid_nb_lock); +return ret; +} +EXPORT_SYMBOL_GPL (ioasid_register_notifier_mm); + +void ioasid_unregister_notifier_mm (struct mm_struct *mm,struct notifier_block *nb) +{ +struct ioasid_set_nb *curr; + +spin_lock (&ioasid_nb_lock); +list_for_each_entry (curr, &ioasid_nb_pending_list, list) { +if (curr->token == mm && curr->nb == nb) { +list_del (&curr->list); + goto exit_free; + } + } +pr_warn ("No ioasid set found for mm token %llx\n" , (u64 )mm); + goto done_unlock; + +exit_free: +if (curr->active) { +pr_debug ("mm set active, unregister %llx\n" , + (u64 )mm); +atomic_notifier_chain_unregister (&curr->set->nh, nb); + } +kfree (curr); +done_unlock: +spin_unlock (&ioasid_nb_lock); +return ; +} +EXPORT_SYMBOL_GPL (ioasid_unregister_notifier_mm); + +/** +* ioasid_notify - Send notification on a given IOASID for status change. +* Used by publishers when the status change may affect +* subscriber's internal state. +* +* @ioasid: The IOASID to which the notification will send +* @cmd: The notification event +* @flags: Special instructions, e.g. notify with a set or global +*/ +int ioasid_notify (ioasid_t ioasid,enum ioasid_notify_val cmd,unsigned int flags) +{ +struct ioasid_data *ioasid_data; +struct ioasid_nb_args args = {0 }; +int ret =0 ; + +spin_lock (&ioasid_allocator_lock); + ioasid_data =xa_load (&active_allocator->xa, ioasid); +if (!ioasid_data) { +pr_err ("Trying to notify unknown IOASID %u\n" , ioasid); +spin_unlock (&ioasid_allocator_lock); +return -EINVAL ; + } + + args.id = ioasid; + args.set = ioasid_data->set; + args.pdata = ioasid_data->private; + args.spid = ioasid_data->spid; +if (flags &IOASID_NOTIFY_ALL ) { + ret =atomic_notifier_call_chain (&ioasid_chain, cmd, &args); + }else if (flags &IOASID_NOTIFY_SET ) { + ret =atomic_notifier_call_chain (&ioasid_data->set->nh, + cmd, &args); + } +spin_unlock (&ioasid_allocator_lock); + +return ret; +} +EXPORT_SYMBOL_GPL (ioasid_notify); +MODULE_AUTHOR ("Jean-Philippe Brucker <[email protected]>" );MODULE_AUTHOR ("Jacob Pan <[email protected]>" );MODULE_DESCRIPTION ("IO Address Space ID (IOASID) allocator" );include/linux/ioasid.h +0
-0
include/linux/ioasid.h enum ioasid_set_type { · line 26
Quoted 4 lines
include/linux/ioasid.h +1
-0@@ -23,6 +23,7 @@ enum ioasid_set_type {* struct ioasid_set - Meta data about ioasid_set * @type: Token types and other features * @token: Unique to identify an IOASID set +* @nh: Notifier for IOASID events within the set
list of notifiers private to that set?
Quoted 100 lines
include/linux/ioasid.h +62
-0* @xa: XArray to store ioasid_set private IDs, can be used for * guest-host IOASID mapping, or just a private IOASID namespace. * @quota: Max number of IOASIDs can be allocated within the set @@ -32,6 +33,7 @@ enum ioasid_set_type {*/ struct ioasid_set {void *token; +struct atomic_notifier_head nh;struct xarray xa;int type;int quota;@@ -56,6 +58,49 @@ struct ioasid_allocator_ops {void *pdata; }; +/* Notification data when IOASID status changed */ +enum ioasid_notify_val { +IOASID_ALLOC =1 , +IOASID_FREE , +IOASID_BIND , +IOASID_UNBIND , +IOASID_SET_ALLOC , +IOASID_SET_FREE , +}; + +#define IOASID_NOTIFY_ALL BIT(0) +#define IOASID_NOTIFY_SET BIT(1) +/** +* enum ioasid_notifier_prios - IOASID event notification order +* +* When status of an IOASID changes, users might need to take actions to +* reflect the new state. For example, when an IOASID is freed due to +* exception, the hardware context in virtual CPU, DMA device, and IOMMU +* shall be cleared and drained. Order is required to prevent life cycle +* problems. +*/ +enum ioasid_notifier_prios { +IOASID_PRIO_LAST , +IOASID_PRIO_DEVICE , +IOASID_PRIO_IOMMU , +IOASID_PRIO_CPU , +}; + +/** +* struct ioasid_nb_args - Argument provided by IOASID core when notifier +* is called. +* @id: The IOASID being notified +* @spid: The set private ID associated with the IOASID +* @set: The IOASID set of @id +* @pdata: The private data attached to the IOASID +*/ +struct ioasid_nb_args { +ioasid_t id; +ioasid_t spid; +struct ioasid_set *set; +void *pdata; +}; +#if IS_ENABLED (CONFIG_IOASID )void ioasid_install_capacity (ioasid_t total);ioasid_t ioasid_get_capacity (void );@@ -75,8 +120,16 @@ void *ioasid_find(struct ioasid_set *set, ioasid_t ioasid, bool (*getter)(void *int ioasid_attach_data (ioasid_t ioasid,void *data);int ioasid_attach_spid (ioasid_t ioasid,ioasid_t spid);ioasid_t ioasid_find_by_spid (struct ioasid_set *set,ioasid_t spid); + +int ioasid_register_notifier (struct ioasid_set *set, +struct notifier_block *nb); +void ioasid_unregister_notifier (struct ioasid_set *set, +struct notifier_block *nb); +int ioasid_register_allocator (struct ioasid_allocator_ops *allocator);void ioasid_unregister_allocator (struct ioasid_allocator_ops *allocator); + +int ioasid_notify (ioasid_t ioasid,enum ioasid_notify_val cmd,unsigned int flags);void ioasid_is_in_set (struct ioasid_set *set,ioasid_t ioasid);int ioasid_get (struct ioasid_set *set,ioasid_t ioasid);int ioasid_get_locked (struct ioasid_set *set,ioasid_t ioasid);@@ -85,6 +138,9 @@ void ioasid_put_locked(struct ioasid_set *set, ioasid_t ioasid);int ioasid_set_for_each_ioasid (struct ioasid_set *sdata,void (*fn)(ioasid_t id,void *data),void *data); +int ioasid_register_notifier_mm (struct mm_struct *mm,struct notifier_block *nb); +void ioasid_unregister_notifier_mm (struct mm_struct *mm,struct notifier_block *nb); +#else /* !CONFIG_IOASID */ static inline void ioasid_install_capacity (ioasid_t total) {@@ -124,6 +180,20 @@ static inline void *ioasid_find(struct ioasid_set *set, ioasid_t ioasid, bool (*return NULL ; } +static inline int ioasid_register_notifier (struct notifier_block *nb) +{ +return -ENOTSUPP ; +} + +static inline void ioasid_unregister_notifier (struct notifier_block *nb) +{
include/linux/ioasid.h static inline void *ioasid_find(struct ioasid_set *set, ioasid_t ioasid, bool (* · line 199
Quoted 10 lines
include/linux/ioasid.h +7
-0+} + +static inline int ioasid_notify (ioasid_t ioasid,enum ioasid_notify_val cmd,unsigned int flags) +{ +return -ENOTSUPP ; +} +static inline int ioasid_register_allocator (struct ioasid_allocator_ops *allocator) {return -ENOTSUPP ;
Thanks
Eric
Re: [PATCH v2 6/9] iommu/ioasid: Introduce notification APIs
Quoted 35 lines · On Tue, 1 Sep 2020 18:49:38 +0200 Auger Eric <[email protected]> wrote:
Hi Jacob,
Quoted 32 lines · Jacob Pan
Relations among IOASID users largely follow a publisher-subscriber pattern. E.g. to support guest SVA on Intel Scalable I/O Virtualization (SIOV) enabled platforms, VFIO, IOMMU, device drivers, KVM are all users of IOASIDs. When a state change occurs, VFIO publishes the change event that needs to be processed by other users/subscribers.
This patch introduced two types of notifications: global and per
ioasid_set. The latter is intended for users who only needs to handle events related to the IOASID of a given set. For more information, refer to the kernel documentation atDocumentation/ioasid.rst.Signed-off-by Liu Yi L <[email protected]>Signed-off-by Wu Hao <[email protected]>Signed-off-by Jacob Pan <[email protected]>--- drivers/iommu/ioasid.c | 280++++++++++++++++++++++++++++++++++++++++++++++++-
include/linux/ioasid.h| 70 +++++++++++++ 2 files changed, 348 insertions(+), 2 deletions(-)drivers/iommu/ioasid.c +1
-0@@ -9,8 +9,35 @@#include <linux/spinlock.h> #include <linux/xarray.h> #include <linux/ioasid.h> +#include <linux/sched/mm.h>
Quoted 5 lines
Quoted 4 lines
drivers/iommu/ioasid.c +2
-0static DEFINE_XARRAY_ALLOC (ioasid_sets); +/* +* An IOASID could have multiple consumers where each consumeer may have
can have multiple consumers
Sounds good, I used past tense to describe a possibility :)
Quoted 4 lines
Quoted 3 lines
+ * hardware contexts associated with IOASIDs.
+ * When a status change occurs, such as IOASID is being freed, notifier chainss/such as IOASID is being freed/, like on IOASID deallocation,
Better, will do.
Quoted 48 lines
Quoted 48 lines
+ * are used to keep the consumers in sync.
+ * This is a publisher-subscriber pattern where publisher can change the
+ * state of each IOASID, e.g. alloc/free, bind IOASID to a device and mm.
+ * On the other hand, subscribers gets notified for the state change and
+ * keep local states in sync.
+ *
+ * Currently, the notifier is global. A further optimization could be per
+ * IOASID set notifier chain.
+ */drivers/iommu/ioasid.c +19
-1+static ATOMIC_NOTIFIER_HEAD (ioasid_chain); + +/* List to hold pending notification block registrations */ +static LIST_HEAD (ioasid_nb_pending_list); +static DEFINE_SPINLOCK (ioasid_nb_lock); +struct ioasid_set_nb { +struct list_head list; +struct notifier_block *nb; +void *token; +struct ioasid_set *set; +bool active; +}; +enum ioasid_state {IOASID_STATE_INACTIVE ,IOASID_STATE_ACTIVE ,@@ -394,6 +421,7 @@ EXPORT_SYMBOL_GPL(ioasid_find_by_spid);ioasid_t ioasid_alloc (struct ioasid_set *set,ioasid_t min,ioasid_t max,void *private) { +struct ioasid_nb_args args;struct ioasid_data *data;void *adata;ioasid_t id =INVALID_IOASID ;@@ -445,8 +473,14 @@ ioasid_t ioasid_alloc(struct ioasid_set *set, ioasid_t min, ioasid_t max, goto exit_free; } set->nr_ioasids++; - goto done_unlock; + args.id = id; +/* Set private ID is not attached during allocation */ + args.spid =INVALID_IOASID ; + args.set = set; +atomic_notifier_call_chain (&set->nh,IOASID_ALLOC , &args);
Quoted 3 lines
drivers/iommu/ioasid.c ioasid_t ioasid_alloc(struct ioasid_set *set, ioasid_t min, ioasid_t max, · line 483
Quoted 2 lines
drivers/iommu/ioasid.c +2
-0+spin_unlock (&ioasid_allocator_lock); +return id;spurious change
Good catch. should just goto done_unlock.
Quoted 10 lines
Quoted 10 lines
drivers/iommu/ioasid.c +1
-0exit_free:kfree (data); done_unlock:@@ -479,6 +513,7 @@ static void ioasid_do_free(struct ioasid_data *data)static void ioasid_free_locked (struct ioasid_set *set,ioasid_t ioasid) { +struct ioasid_nb_args args;struct ioasid_data *data;
Quoted 7 lines
drivers/iommu/ioasid.c
Quoted 6 lines
drivers/iommu/ioasid.c +0
-0data =xa_load (&active_allocator->xa, ioasid);@@ -491,7 +526,16 @@ static void ioasid_free_locked(struct
ioasid_set*set,ioasid_tioasid)pr_warn("Cannot free IOASID %u due to set ownership\n", ioasid); return;drivers/iommu/ioasid.c +1
-0} +spurious new line
got it
Quoted 11 lines
Quoted 11 lines
drivers/iommu/ioasid.c +6
-0data->state =IOASID_STATE_FREE_PENDING ; +/* Notify all users that this IOASID is being freed */ + args.id = ioasid; + args.spid = data->spid; + args.pdata = data->private; + args.set = data->set; + atomic_notifier_call_chain(&ioasid_chain, IOASID_FREE,&args);
drivers/iommu/ioasid.c +2
-0+/* Notify the ioasid_set for per set users */ +atomic_notifier_call_chain (&set->nh,IOASID_FREE , &args);
Quoted 3 lines
drivers/iommu/ioasid.c static void ioasid_free_locked(struct ioasid_set *set, ioasid_t ioasid) · line 541
Quoted 2 lines
drivers/iommu/ioasid.c +0
-0if (!refcount_dec_and_test (&data->users))return ;Shouldn't we call the notifier only when ref count == 0?
Not in the current scheme. The idea is to notify all users the PASID is being freed, then each user can drop its reference. When refcount == 0, the PASID will be returned to the pool.
Quoted 143 lines
Quoted 143 lines
drivers/iommu/ioasid.c +9
-0@@ -514,6 +558,28 @@ void ioasid_free(struct ioasid_set *set, ioasid_t ioasid) }EXPORT_SYMBOL_GPL (ioasid_free); +static void ioasid_add_pending_nb (struct ioasid_set *set) +{ +struct ioasid_set_nb *curr; + +if (set->type !=IOASID_SET_TYPE_MM ) +return ; + +/* +* Check if there are any pending nb requests for the given token, if so
drivers/iommu/ioasid.c +6
-0+* add them to the notifier chain. +*/ +spin_lock (&ioasid_nb_lock); +list_for_each_entry (curr, &ioasid_nb_pending_list, list) { +if (curr->token == set->token && !curr->active) { + atomic_notifier_chain_register(&set->nh,
curr->nb);drivers/iommu/ioasid.c +11
-0+ curr->set = set; + curr->active = true; + } + } +spin_unlock (&ioasid_nb_lock); +} +/** * ioasid_alloc_set - Allocate a new IOASID set for a given token * @@ -601,6 +667,13 @@ struct ioasid_set *ioasid_alloc_set(void *token, ioasid_t quota, int type) sdata->quota = quota; sdata->sid = id;refcount_set (&sdata->ref,1 ); +ATOMIC_INIT_NOTIFIER_HEAD (&sdata->nh); + +/* +* Check if there are any pending nb requests for the given token, if so
drivers/iommu/ioasid.c +3
-0+* add them to the notifier chain. +*/ +ioasid_add_pending_nb (sdata);/* * Per set XA is used to store private IDs within the set, get ready @@ -617,6 +690,30 @@ struct
ioasid_set*ioasid_alloc_set(void *token,ioasid_tquota, int type) }
EXPORT_SYMBOL_GPL(ioasid_alloc_set);drivers/iommu/ioasid.c +6
-0+ +/* +* ioasid_find_mm_set - Retrieve IOASID set with mm token +* Take a reference of the set if found. +*/ +static struct ioasid_set *ioasid_find_mm_set(struct mm_struct*token) +{
drivers/iommu/ioasid.c +7
-0+struct ioasid_set *sdata, *set =NULL ; +unsigned long index; + +spin_lock (&ioasid_allocator_lock); + +xa_for_each (&ioasid_sets, index, sdata) { + if (sdata->type == IOASID_SET_TYPE_MM &&
sdata->token== token) {drivers/iommu/ioasid.c +26
-1+refcount_inc (&sdata->ref); + set = sdata; + goto exit_unlock; + } + } +exit_unlock: +spin_unlock (&ioasid_allocator_lock); +return set; +} +void ioasid_set_get_locked (struct ioasid_set *set) {if (WARN_ON (xa_load (&ioasid_sets, set->sid) != set)) {@@ -638,6 +735,8 @@ EXPORT_SYMBOL_GPL(ioasid_set_get);void ioasid_set_put_locked (struct ioasid_set *set) { +struct ioasid_nb_args args = {0 }; +struct ioasid_set_nb *curr;struct ioasid_data *entry;unsigned long index;@@ -673,8 +772,24 @@ void ioasid_set_put_locked(struct ioasid_set *set) done_destroy:/* Return the quota back to system pool */ ioasid_capacity_avail += set->quota; -kfree_rcu (set, rcu); +/* Restore pending status of the set NBs */ +spin_lock (&ioasid_nb_lock); +list_for_each_entry (curr, &ioasid_nb_pending_list, list) { +if (curr->token == set->token) { +if (curr->active) + curr->active = false; +else +pr_warn ("Set token exists but not active!\n"); + } + } +spin_unlock (&ioasid_nb_lock); + + args.set = set; + atomic_notifier_call_chain(&ioasid_chain, IOASID_SET_FREE,&args); +
drivers/iommu/ioasid.c +2
-0+kfree_rcu (set, rcu); +pr_debug ("Set freed %d\n" , set->sid);/* * Token got released right away after the ioasid_set is freed.
* If a new set is created immediately with the newly
released token, @@ -927,6 +1042,167 @@ void *ioasid_find(structioasid_set*set,ioasid_tioasid, }
EXPORT_SYMBOL_GPL(ioasid_find);+int
ioasid_register_notifier(struct ioasid_set *set, struct notifier_block *nb)+{drivers/iommu/ioasid.c +2
-0+if (set) + return atomic_notifier_chain_register(&set->nh,nb);
drivers/iommu/ioasid.c +2
-0+else +return
atomic_notifier_chain_register(&ioasid_chain, nb); +}drivers/iommu/ioasid.c +10
-0+EXPORT_SYMBOL_GPL (ioasid_register_notifier); + +void ioasid_unregister_notifier (struct ioasid_set *set, +struct notifier_block *nb) +{ +struct ioasid_set_nb *curr; + +spin_lock (&ioasid_nb_lock); +/* +* Pending list is registered with a token without an
ioasid_set,drivers/iommu/ioasid.c +5
-0+* therefore should not be unregistered directly. +*/ +list_for_each_entry (curr, &ioasid_nb_pending_list, list) { +if (curr->nb == nb) { + pr_warn("Cannot unregister NB from pending
Quoted 8 lines
drivers/iommu/ioasid.c void *ioasid_find(struct ioasid_set *set, ioasid_t ioasid, · line 1071
Quoted 6 lines
list\n");
drivers/iommu/ioasid.c +5
-0+spin_unlock (&ioasid_nb_lock); +return ; + } + } +spin_unlock (&ioasid_nb_lock);is it safe to release the lock here? What does prevent another NB to be added to
ioasid_nb_pending_listafter that?
Another NB will not be the same one as this NB, which is being removed. I don't see any issues.
The only reason we check the pending list is to make sure the NB on the pending list must be removed by ioasid_unregister_notifier_mm() API.
Quoted 151 lines
Quoted 151 lines
drivers/iommu/ioasid.c +5
-0+ +if (set) +atomic_notifier_chain_unregister (&set->nh, nb); +else + atomic_notifier_chain_unregister(&ioasid_chain,nb); +}
drivers/iommu/ioasid.c +3
-0+EXPORT_SYMBOL_GPL (ioasid_unregister_notifier); + +int ioasid_register_notifier_mm(struct mm_struct *mm, struct
notifier_block*nb) +{drivers/iommu/ioasid.c +24
-0+struct ioasid_set_nb *curr; +struct ioasid_set *set; +int ret =0 ; + +if (!mm) +return -EINVAL ; + +spin_lock (&ioasid_nb_lock); + +/* Check for duplicates, nb is unique per set */ +list_for_each_entry (curr, &ioasid_nb_pending_list, list) { +if (curr->token == mm && curr->nb == nb) { + ret = -EBUSY ; + goto exit_unlock; + } + } + +/* Check if the token has an existing set */ + set =ioasid_find_mm_set (mm); +if (IS_ERR_OR_NULL (set)) { +/* Add to the rsvd list as inactive */ + curr->active = false; + }else { +/* REVISIT: Only register empty set for now. Can add an option
drivers/iommu/ioasid.c +4
-0+* in the future to playback existing PASIDs. +*/ +if (set->nr_ioasids) { + pr_warn("IOASID set %d not empty\n",
set->sid);drivers/iommu/ioasid.c +14
-0+ ret = -EBUSY ; + goto exit_unlock; + } + curr =kzalloc (sizeof (*curr),GFP_ATOMIC ); +if (!curr) { + ret = -ENOMEM ; + goto exit_unlock; + } + curr->token = mm; + curr->nb = nb; + curr->active = true; + curr->set = set; + +/* Set already created, add to the notifier chain */
drivers/iommu/ioasid.c +3
-0+atomic_notifier_chain_register (&set->nh, nb); +/* +* Do not hold a reference, if the set gets destroyed, the nb
drivers/iommu/ioasid.c +14
-0+* entry will be marked inactive. +*/ +ioasid_set_put (set); + } + +list_add (&curr->list, &ioasid_nb_pending_list); + +exit_unlock: +spin_unlock (&ioasid_nb_lock); +return ret; +} +EXPORT_SYMBOL_GPL (ioasid_register_notifier_mm); + +void ioasid_unregister_notifier_mm(struct mm_struct *mm, struct
notifier_block*nb) +{drivers/iommu/ioasid.c +10
-0+struct ioasid_set_nb *curr; + +spin_lock (&ioasid_nb_lock); +list_for_each_entry (curr, &ioasid_nb_pending_list, list) { +if (curr->token == mm && curr->nb == nb) { +list_del (&curr->list); + goto exit_free; + } + } + pr_warn("No ioasid set found for mm token %llx\n",(u64)mm);
drivers/iommu/ioasid.c +7
-0+ goto done_unlock; + +exit_free: +if (curr->active) { +pr_debug ("mm set active, unregister %llx\n" , + (u64 )mm); + atomic_notifier_chain_unregister(&curr->set->nh,nb);
drivers/iommu/ioasid.c +10
-0+ } +kfree (curr); +done_unlock: +spin_unlock (&ioasid_nb_lock); +return ; +} +EXPORT_SYMBOL_GPL (ioasid_unregister_notifier_mm); + +/** +* ioasid_notify - Send notification on a given IOASID for status change.
+ * Used by publishers when the status change may
affect
+ * subscriber's internal state.
+ *
+ * @ioasid: The IOASID to which the notification will send
+ * @cmd: The notification event
+ * @flags: Special instructions, e.g. notify with a set or
global
+ */
+intioasid_notify(ioasid_t ioasid, enum ioasid_notify_val cmd, unsigned int flags)+{drivers/iommu/ioasid.c +8
-0+struct ioasid_data *ioasid_data; +struct ioasid_nb_args args = {0 }; +int ret =0 ; + +spin_lock (&ioasid_allocator_lock); + ioasid_data =xa_load (&active_allocator->xa, ioasid); +if (!ioasid_data) { + pr_err("Trying to notify unknown IOASID %u\n",ioasid);
drivers/iommu/ioasid.c +10
-0+spin_unlock (&ioasid_allocator_lock); +return -EINVAL ; + } + + args.id = ioasid; + args.set = ioasid_data->set; + args.pdata = ioasid_data->private; + args.spid = ioasid_data->spid; +if (flags &IOASID_NOTIFY_ALL ) { + ret = atomic_notifier_call_chain(&ioasid_chain,cmd, &args);
drivers/iommu/ioasid.c +2
-0+ }else if (flags &IOASID_NOTIFY_SET ) { + ret =atomic_notifier_call_chain(&ioasid_data->set->nh,
drivers/iommu/ioasid.c +8
-0+ cmd, &args); + } +spin_unlock (&ioasid_allocator_lock); + +return ret; +} +EXPORT_SYMBOL_GPL (ioasid_notify); + MODULE_AUTHOR("Jean-Philippe Brucker<[email protected]>");
MODULE_AUTHOR("Jacob Pan <[email protected]>");MODULE_DESCRIPTION("IO Address Space ID (IOASID) allocator"); diff --gita/include/linux/ioasid.hb/include/linux/ioasid.hindexd4b3e83672f6..572111cd3b4b100644
Quoted 8 lines
include/linux/ioasid.h enum ioasid_set_type { · line 26
Quoted 7 lines
include/linux/ioasid.h +1
-0@@ -23,6 +23,7 @@ enum ioasid_set_type {* struct ioasid_set - Meta data about ioasid_set * @type: Token types and other features * @token: Unique to identify an IOASID set +* @nh: Notifier for IOASID events within the set list of notifiers private to that set?
Sounds more accurate.
Quoted 119 lines
Quoted 114 lines
include/linux/ioasid.h +0
-0* @xa: XArray to store ioasid_set private IDs, can be used for
* guest-host IOASID mapping, or just a private
IOASID namespace.
* @quota: Max number of IOASIDs can be allocated within
the set @@ -32,6 +33,7 @@ enumioasid_set_type{
*/
structioasid_set{include/linux/ioasid.h +17
-0void *token; +struct atomic_notifier_head nh;struct xarray xa;int type;int quota;@@ -56,6 +58,49 @@ struct ioasid_allocator_ops {void *pdata; }; +/* Notification data when IOASID status changed */ +enum ioasid_notify_val { +IOASID_ALLOC =1 , +IOASID_FREE , +IOASID_BIND , +IOASID_UNBIND , +IOASID_SET_ALLOC , +IOASID_SET_FREE , +}; + +#define IOASID_NOTIFY_ALL BIT(0) +#define IOASID_NOTIFY_SET BIT(1) +/** +* enum ioasid_notifier_prios - IOASID event notification order +* +* When status of an IOASID changes, users might need to take actions to
+ * reflect the new state. For example, when an IOASID is freed due to
+ * exception, the hardware context in virtual CPU, DMA device, and IOMMU
+ * shall be cleared and drained. Order is required to prevent life cycle
+ * problems.
+ */include/linux/ioasid.h +9
-0+enum ioasid_notifier_prios { +IOASID_PRIO_LAST , +IOASID_PRIO_DEVICE , +IOASID_PRIO_IOMMU , +IOASID_PRIO_CPU , +}; + +/** +* struct ioasid_nb_args - Argument provided by IOASID core when notifier
+ * is called.
+ * @id: The IOASID being notified
+ * @spid: The set private ID associated with the IOASID
+ * @set: The IOASID set of @id
+ * @pdata: The private data attached to the IOASID
+ */include/linux/ioasid.h +7
-0+struct ioasid_nb_args { +ioasid_t id; +ioasid_t spid; +struct ioasid_set *set; +void *pdata; +}; +#if IS_ENABLED (CONFIG_IOASID )void ioasid_install_capacity (ioasid_t total);ioasid_t ioasid_get_capacity (void );@@ -75,8 +120,16 @@ void *ioasid_find(struct ioasid_set *set,
ioasid_tioasid, bool (*getter)(void * intioasid_attach_data(ioasid_t ioasid, void *data); intioasid_attach_spid(ioasid_t ioasid, ioasid_t spid);ioasid_tioasid_find_by_spid(struct ioasid_set *set, ioasid_t spid); +include/linux/ioasid.h +5
-0+int ioasid_register_notifier (struct ioasid_set *set, +struct notifier_block *nb); +void ioasid_unregister_notifier (struct ioasid_set *set, +struct notifier_block *nb); + int ioasid_register_allocator(struct ioasid_allocator_ops*allocator); void
ioasid_unregister_allocator(struct ioasid_allocator_ops *allocator); +
+intioasid_notify(ioasid_t ioasid, enum ioasid_notify_val cmd, unsigned int flags); voidioasid_is_in_set(struct ioasid_set *set, ioasid_t ioasid); intioasid_get(struct ioasid_set *set, ioasid_t ioasid); intioasid_get_locked(struct ioasid_set *set, ioasid_t ioasid); @@ -85,6 +138,9 @@ voidioasid_put_locked(struct ioasid_set *set, ioasid_t ioasid); intioasid_set_for_each_ioasid(struct ioasid_set *sdata, void (*fn)(ioasid_t id, void *data), void *data);
+intioasid_register_notifier_mm(struct mm_struct *mm, struct notifier_block *nb); +voidioasid_unregister_notifier_mm(struct mm_struct *mm, struct notifier_block *nb); +
#else /* !CONFIG_IOASID */
static inline voidioasid_install_capacity(ioasid_t total){include/linux/ioasid.h +11
-0@@ -124,6 +180,20 @@ static inline void *ioasid_find(struct ioasid_set *set, ioasid_t ioasid, bool (* return NULL; } +static inline int ioasid_register_notifier (struct notifier_block *nb) +{ +return -ENOTSUPP ; +} + +static inline void ioasid_unregister_notifier (struct notifier_block *nb) +{ +} + +static inline int ioasid_notify (ioasid_t ioasid,enum ioasid_notify_val cmd, unsigned int flags) +{ +return -ENOTSUPP ; +} + static inline int ioasid_register_allocator(structQuoted 3 lines
ioasid_allocator_ops*allocator) {
return-ENOTSUPP;Thanks
Quoted 6 lines
Eric
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
[Jacob Pan]
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
Re: [PATCH v2 6/9] iommu/ioasid: Introduce notification APIs
Hi Jacob,
Quoted 134 lines · Jacob Pan
Quoted 35 lines · On Tue, 1 Sep 2020 18:49:38 +0200 Auger Eric <[email protected]> wrote:
Hi Jacob,
Quoted 32 lines · Jacob Pan
Relations among IOASID users largely follow a publisher-subscriber pattern. E.g. to support guest SVA on Intel Scalable I/O Virtualization (SIOV) enabled platforms, VFIO, IOMMU, device drivers, KVM are all users of IOASIDs. When a state change occurs, VFIO publishes the change event that needs to be processed by other users/subscribers.
This patch introduced two types of notifications: global and per
ioasid_set. The latter is intended for users who only needs to handle events related to the IOASID of a given set. For more information, refer to the kernel documentation atDocumentation/ioasid.rst.Signed-off-by Liu Yi L <[email protected]>Signed-off-by Wu Hao <[email protected]>Signed-off-by Jacob Pan <[email protected]>--- drivers/iommu/ioasid.c | 280++++++++++++++++++++++++++++++++++++++++++++++++-
include/linux/ioasid.h| 70 +++++++++++++ 2 files changed, 348 insertions(+), 2 deletions(-)drivers/iommu/ioasid.c +1
-0@@ -9,8 +9,35 @@#include <linux/spinlock.h> #include <linux/xarray.h> #include <linux/ioasid.h> +#include <linux/sched/mm.h> Quoted 5 lines
Quoted 4 lines
drivers/iommu/ioasid.c +2
-0static DEFINE_XARRAY_ALLOC (ioasid_sets); +/* +* An IOASID could have multiple consumers where each consumeer may have
can have multiple consumers
Sounds good, I used past tense to describe a possibility :)
Quoted 4 lines
Quoted 3 lines
+ * hardware contexts associated with IOASIDs.
+ * When a status change occurs, such as IOASID is being freed, notifier chainss/such as IOASID is being freed/, like on IOASID deallocation,
Better, will do.
Quoted 48 lines
Quoted 48 lines
+ * are used to keep the consumers in sync.
+ * This is a publisher-subscriber pattern where publisher can change the
+ * state of each IOASID, e.g. alloc/free, bind IOASID to a device and mm.
+ * On the other hand, subscribers gets notified for the state change and
+ * keep local states in sync.
+ *
+ * Currently, the notifier is global. A further optimization could be per
+ * IOASID set notifier chain.
+ */drivers/iommu/ioasid.c +19
-1+static ATOMIC_NOTIFIER_HEAD (ioasid_chain); + +/* List to hold pending notification block registrations */ +static LIST_HEAD (ioasid_nb_pending_list); +static DEFINE_SPINLOCK (ioasid_nb_lock); +struct ioasid_set_nb { +struct list_head list; +struct notifier_block *nb; +void *token; +struct ioasid_set *set; +bool active; +}; +enum ioasid_state {IOASID_STATE_INACTIVE ,IOASID_STATE_ACTIVE ,@@ -394,6 +421,7 @@ EXPORT_SYMBOL_GPL(ioasid_find_by_spid);ioasid_t ioasid_alloc (struct ioasid_set *set,ioasid_t min,ioasid_t max,void *private) { +struct ioasid_nb_args args;struct ioasid_data *data;void *adata;ioasid_t id =INVALID_IOASID ;@@ -445,8 +473,14 @@ ioasid_t ioasid_alloc(struct ioasid_set *set, ioasid_t min, ioasid_t max, goto exit_free; } set->nr_ioasids++; - goto done_unlock; + args.id = id; +/* Set private ID is not attached during allocation */ + args.spid =INVALID_IOASID ; + args.set = set; +atomic_notifier_call_chain (&set->nh,IOASID_ALLOC , &args);Quoted 3 lines
drivers/iommu/ioasid.c ioasid_t ioasid_alloc(struct ioasid_set *set, ioasid_t min, ioasid_t max, · line 483
Quoted 2 lines
drivers/iommu/ioasid.c +2
-0+spin_unlock (&ioasid_allocator_lock); +return id;spurious change
Good catch. should just goto
done_unlock.Quoted 10 lines
Quoted 10 lines
drivers/iommu/ioasid.c +1
-0exit_free:kfree (data); done_unlock:@@ -479,6 +513,7 @@ static void ioasid_do_free(struct ioasid_data *data)static void ioasid_free_locked (struct ioasid_set *set,ioasid_t ioasid) { +struct ioasid_nb_args args;struct ioasid_data *data;Quoted 7 lines
drivers/iommu/ioasid.c
Quoted 6 lines
drivers/iommu/ioasid.c +0
-0data =xa_load (&active_allocator->xa, ioasid);@@ -491,7 +526,16 @@ static void ioasid_free_locked(struct
ioasid_set*set,ioasid_tioasid)pr_warn("Cannot free IOASID %u due to set ownership\n", ioasid); return;drivers/iommu/ioasid.c +1
-0} +spurious new line
got it
Quoted 11 lines
Quoted 11 lines
drivers/iommu/ioasid.c +6
-0data->state =IOASID_STATE_FREE_PENDING ; +/* Notify all users that this IOASID is being freed */ + args.id = ioasid; + args.spid = data->spid; + args.pdata = data->private; + args.set = data->set; + atomic_notifier_call_chain(&ioasid_chain, IOASID_FREE,&args);
drivers/iommu/ioasid.c +2
-0+/* Notify the ioasid_set for per set users */ +atomic_notifier_call_chain (&set->nh,IOASID_FREE , &args);
Quoted 6 lines
Quoted 3 lines
drivers/iommu/ioasid.c static void ioasid_free_locked(struct ioasid_set *set, ioasid_t ioasid) · line 541
Quoted 2 lines
drivers/iommu/ioasid.c +0
-0if (!refcount_dec_and_test (&data->users))return ;Shouldn't we call the notifier only when ref count == 0?
Not in the current scheme. The idea is to notify all users the PASID is being freed, then each user can drop its reference. When refcount == 0, the PASID will be returned to the pool.
OK
Quoted 152 lines
Quoted 144 lines
Quoted 144 lines
drivers/iommu/ioasid.c +9
-0@@ -514,6 +558,28 @@ void ioasid_free(struct ioasid_set *set, ioasid_t ioasid) }EXPORT_SYMBOL_GPL (ioasid_free); +static void ioasid_add_pending_nb (struct ioasid_set *set) +{ +struct ioasid_set_nb *curr; + +if (set->type !=IOASID_SET_TYPE_MM ) +return ; + +/* +* Check if there are any pending nb requests for the given token, if so
drivers/iommu/ioasid.c +6
-0+* add them to the notifier chain. +*/ +spin_lock (&ioasid_nb_lock); +list_for_each_entry (curr, &ioasid_nb_pending_list, list) { +if (curr->token == set->token && !curr->active) { + atomic_notifier_chain_register(&set->nh,
curr->nb);drivers/iommu/ioasid.c +11
-0+ curr->set = set; + curr->active = true; + } + } +spin_unlock (&ioasid_nb_lock); +} +/** * ioasid_alloc_set - Allocate a new IOASID set for a given token * @@ -601,6 +667,13 @@ struct ioasid_set *ioasid_alloc_set(void *token, ioasid_t quota, int type) sdata->quota = quota; sdata->sid = id;refcount_set (&sdata->ref,1 ); +ATOMIC_INIT_NOTIFIER_HEAD (&sdata->nh); + +/* +* Check if there are any pending nb requests for the given token, if so
drivers/iommu/ioasid.c +3
-0+* add them to the notifier chain. +*/ +ioasid_add_pending_nb (sdata);/* * Per set XA is used to store private IDs within the set, get ready @@ -617,6 +690,30 @@ struct
ioasid_set*ioasid_alloc_set(void *token,ioasid_tquota, int type) }
EXPORT_SYMBOL_GPL(ioasid_alloc_set);drivers/iommu/ioasid.c +6
-0+ +/* +* ioasid_find_mm_set - Retrieve IOASID set with mm token +* Take a reference of the set if found. +*/ +static struct ioasid_set *ioasid_find_mm_set(struct mm_struct*token) +{
drivers/iommu/ioasid.c +7
-0+struct ioasid_set *sdata, *set =NULL ; +unsigned long index; + +spin_lock (&ioasid_allocator_lock); + +xa_for_each (&ioasid_sets, index, sdata) { + if (sdata->type == IOASID_SET_TYPE_MM &&
sdata->token== token) {drivers/iommu/ioasid.c +26
-1+refcount_inc (&sdata->ref); + set = sdata; + goto exit_unlock; + } + } +exit_unlock: +spin_unlock (&ioasid_allocator_lock); +return set; +} +void ioasid_set_get_locked (struct ioasid_set *set) {if (WARN_ON (xa_load (&ioasid_sets, set->sid) != set)) {@@ -638,6 +735,8 @@ EXPORT_SYMBOL_GPL(ioasid_set_get);void ioasid_set_put_locked (struct ioasid_set *set) { +struct ioasid_nb_args args = {0 }; +struct ioasid_set_nb *curr;struct ioasid_data *entry;unsigned long index;@@ -673,8 +772,24 @@ void ioasid_set_put_locked(struct ioasid_set *set) done_destroy:/* Return the quota back to system pool */ ioasid_capacity_avail += set->quota; -kfree_rcu (set, rcu); +/* Restore pending status of the set NBs */ +spin_lock (&ioasid_nb_lock); +list_for_each_entry (curr, &ioasid_nb_pending_list, list) { +if (curr->token == set->token) { +if (curr->active) + curr->active = false; +else +pr_warn ("Set token exists but not active!\n"); + } + } +spin_unlock (&ioasid_nb_lock); + + args.set = set; + atomic_notifier_call_chain(&ioasid_chain, IOASID_SET_FREE,&args); +
drivers/iommu/ioasid.c +2
-0+kfree_rcu (set, rcu); +pr_debug ("Set freed %d\n" , set->sid);/* * Token got released right away after the ioasid_set is freed.
* If a new set is created immediately with the newly
released token, @@ -927,6 +1042,167 @@ void *ioasid_find(structioasid_set*set,ioasid_tioasid, }
EXPORT_SYMBOL_GPL(ioasid_find);+int
ioasid_register_notifier(struct ioasid_set *set, struct notifier_block *nb)+{drivers/iommu/ioasid.c +2
-0+if (set) + return atomic_notifier_chain_register(&set->nh,nb);
drivers/iommu/ioasid.c +2
-0+else +return
atomic_notifier_chain_register(&ioasid_chain, nb); +}drivers/iommu/ioasid.c +10
-0+EXPORT_SYMBOL_GPL (ioasid_register_notifier); + +void ioasid_unregister_notifier (struct ioasid_set *set, +struct notifier_block *nb) +{ +struct ioasid_set_nb *curr; + +spin_lock (&ioasid_nb_lock); +/* +* Pending list is registered with a token without an
ioasid_set,drivers/iommu/ioasid.c +5
-0+* therefore should not be unregistered directly. +*/ +list_for_each_entry (curr, &ioasid_nb_pending_list, list) { +if (curr->nb == nb) { + pr_warn("Cannot unregister NB from pendingQuoted 8 lines
drivers/iommu/ioasid.c void *ioasid_find(struct ioasid_set *set, ioasid_t ioasid, · line 1071
Quoted 6 lines
list\n");
drivers/iommu/ioasid.c +5
-0+spin_unlock (&ioasid_nb_lock); +return ; + } + } +spin_unlock (&ioasid_nb_lock);is it safe to release the lock here? What does prevent another NB to be added to
ioasid_nb_pending_listafter that?
Quoted 4 lines
Another NB will not be the same one as this NB, which is being removed. I don't see any issues.
The only reason we check the pending list is to make sure the NB on the pending list must be removed byioasid_unregister_notifier_mm()API.
Hum you're right, sorry for the noise.
Thanks
Eric
Quoted 290 lines
Quoted 152 lines
Quoted 152 lines
drivers/iommu/ioasid.c +5
-0+ +if (set) +atomic_notifier_chain_unregister (&set->nh, nb); +else + atomic_notifier_chain_unregister(&ioasid_chain,nb); +}
drivers/iommu/ioasid.c +3
-0+EXPORT_SYMBOL_GPL (ioasid_unregister_notifier); + +int ioasid_register_notifier_mm(struct mm_struct *mm, struct
notifier_block*nb) +{drivers/iommu/ioasid.c +24
-0+struct ioasid_set_nb *curr; +struct ioasid_set *set; +int ret =0 ; + +if (!mm) +return -EINVAL ; + +spin_lock (&ioasid_nb_lock); + +/* Check for duplicates, nb is unique per set */ +list_for_each_entry (curr, &ioasid_nb_pending_list, list) { +if (curr->token == mm && curr->nb == nb) { + ret = -EBUSY ; + goto exit_unlock; + } + } + +/* Check if the token has an existing set */ + set =ioasid_find_mm_set (mm); +if (IS_ERR_OR_NULL (set)) { +/* Add to the rsvd list as inactive */ + curr->active = false; + }else { +/* REVISIT: Only register empty set for now. Can add an option
drivers/iommu/ioasid.c +4
-0+* in the future to playback existing PASIDs. +*/ +if (set->nr_ioasids) { + pr_warn("IOASID set %d not empty\n",
set->sid);drivers/iommu/ioasid.c +14
-0+ ret = -EBUSY ; + goto exit_unlock; + } + curr =kzalloc (sizeof (*curr),GFP_ATOMIC ); +if (!curr) { + ret = -ENOMEM ; + goto exit_unlock; + } + curr->token = mm; + curr->nb = nb; + curr->active = true; + curr->set = set; + +/* Set already created, add to the notifier chain */
drivers/iommu/ioasid.c +3
-0+atomic_notifier_chain_register (&set->nh, nb); +/* +* Do not hold a reference, if the set gets destroyed, the nb
drivers/iommu/ioasid.c +14
-0+* entry will be marked inactive. +*/ +ioasid_set_put (set); + } + +list_add (&curr->list, &ioasid_nb_pending_list); + +exit_unlock: +spin_unlock (&ioasid_nb_lock); +return ret; +} +EXPORT_SYMBOL_GPL (ioasid_register_notifier_mm); + +void ioasid_unregister_notifier_mm(struct mm_struct *mm, struct
notifier_block*nb) +{drivers/iommu/ioasid.c +10
-0+struct ioasid_set_nb *curr; + +spin_lock (&ioasid_nb_lock); +list_for_each_entry (curr, &ioasid_nb_pending_list, list) { +if (curr->token == mm && curr->nb == nb) { +list_del (&curr->list); + goto exit_free; + } + } + pr_warn("No ioasid set found for mm token %llx\n",(u64)mm);
drivers/iommu/ioasid.c +7
-0+ goto done_unlock; + +exit_free: +if (curr->active) { +pr_debug ("mm set active, unregister %llx\n" , + (u64 )mm); + atomic_notifier_chain_unregister(&curr->set->nh,nb);
drivers/iommu/ioasid.c +10
-0+ } +kfree (curr); +done_unlock: +spin_unlock (&ioasid_nb_lock); +return ; +} +EXPORT_SYMBOL_GPL (ioasid_unregister_notifier_mm); + +/** +* ioasid_notify - Send notification on a given IOASID for status change.
+ * Used by publishers when the status change may
affect
+ * subscriber's internal state.
+ *
+ * @ioasid: The IOASID to which the notification will send
+ * @cmd: The notification event
+ * @flags: Special instructions, e.g. notify with a set or
global
+ */
+intioasid_notify(ioasid_t ioasid, enum ioasid_notify_val cmd, unsigned int flags)+{drivers/iommu/ioasid.c +8
-0+struct ioasid_data *ioasid_data; +struct ioasid_nb_args args = {0 }; +int ret =0 ; + +spin_lock (&ioasid_allocator_lock); + ioasid_data =xa_load (&active_allocator->xa, ioasid); +if (!ioasid_data) { + pr_err("Trying to notify unknown IOASID %u\n",ioasid);
drivers/iommu/ioasid.c +10
-0+spin_unlock (&ioasid_allocator_lock); +return -EINVAL ; + } + + args.id = ioasid; + args.set = ioasid_data->set; + args.pdata = ioasid_data->private; + args.spid = ioasid_data->spid; +if (flags &IOASID_NOTIFY_ALL ) { + ret = atomic_notifier_call_chain(&ioasid_chain,cmd, &args);
drivers/iommu/ioasid.c +2
-0+ }else if (flags &IOASID_NOTIFY_SET ) { + ret =atomic_notifier_call_chain(&ioasid_data->set->nh,
drivers/iommu/ioasid.c +8
-0+ cmd, &args); + } +spin_unlock (&ioasid_allocator_lock); + +return ret; +} +EXPORT_SYMBOL_GPL (ioasid_notify); + MODULE_AUTHOR("Jean-Philippe Brucker<[email protected]>");
MODULE_AUTHOR("Jacob Pan <[email protected]>");MODULE_DESCRIPTION("IO Address Space ID (IOASID) allocator"); diff --gita/include/linux/ioasid.hb/include/linux/ioasid.hindexd4b3e83672f6..572111cd3b4b100644Quoted 8 lines
include/linux/ioasid.h enum ioasid_set_type { · line 26
Quoted 7 lines
include/linux/ioasid.h +1
-0@@ -23,6 +23,7 @@ enum ioasid_set_type {* struct ioasid_set - Meta data about ioasid_set * @type: Token types and other features * @token: Unique to identify an IOASID set +* @nh: Notifier for IOASID events within the set list of notifiers private to that set?
Sounds more accurate.
Quoted 119 lines
Quoted 114 lines
include/linux/ioasid.h +0
-0* @xa: XArray to store ioasid_set private IDs, can be used for
* guest-host IOASID mapping, or just a private
IOASID namespace.
* @quota: Max number of IOASIDs can be allocated within
the set @@ -32,6 +33,7 @@ enumioasid_set_type{
*/
structioasid_set{include/linux/ioasid.h +17
-0void *token; +struct atomic_notifier_head nh;struct xarray xa;int type;int quota;@@ -56,6 +58,49 @@ struct ioasid_allocator_ops {void *pdata; }; +/* Notification data when IOASID status changed */ +enum ioasid_notify_val { +IOASID_ALLOC =1 , +IOASID_FREE , +IOASID_BIND , +IOASID_UNBIND , +IOASID_SET_ALLOC , +IOASID_SET_FREE , +}; + +#define IOASID_NOTIFY_ALL BIT(0) +#define IOASID_NOTIFY_SET BIT(1) +/** +* enum ioasid_notifier_prios - IOASID event notification order +* +* When status of an IOASID changes, users might need to take actions to
+ * reflect the new state. For example, when an IOASID is freed due to
+ * exception, the hardware context in virtual CPU, DMA device, and IOMMU
+ * shall be cleared and drained. Order is required to prevent life cycle
+ * problems.
+ */include/linux/ioasid.h +9
-0+enum ioasid_notifier_prios { +IOASID_PRIO_LAST , +IOASID_PRIO_DEVICE , +IOASID_PRIO_IOMMU , +IOASID_PRIO_CPU , +}; + +/** +* struct ioasid_nb_args - Argument provided by IOASID core when notifier
+ * is called.
+ * @id: The IOASID being notified
+ * @spid: The set private ID associated with the IOASID
+ * @set: The IOASID set of @id
+ * @pdata: The private data attached to the IOASID
+ */include/linux/ioasid.h +7
-0+struct ioasid_nb_args { +ioasid_t id; +ioasid_t spid; +struct ioasid_set *set; +void *pdata; +}; +#if IS_ENABLED (CONFIG_IOASID )void ioasid_install_capacity (ioasid_t total);ioasid_t ioasid_get_capacity (void );@@ -75,8 +120,16 @@ void *ioasid_find(struct ioasid_set *set,
ioasid_tioasid, bool (*getter)(void * intioasid_attach_data(ioasid_t ioasid, void *data); intioasid_attach_spid(ioasid_t ioasid, ioasid_t spid);ioasid_tioasid_find_by_spid(struct ioasid_set *set, ioasid_t spid); +include/linux/ioasid.h +5
-0+int ioasid_register_notifier (struct ioasid_set *set, +struct notifier_block *nb); +void ioasid_unregister_notifier (struct ioasid_set *set, +struct notifier_block *nb); + int ioasid_register_allocator(struct ioasid_allocator_ops*allocator); void
ioasid_unregister_allocator(struct ioasid_allocator_ops *allocator); +
+intioasid_notify(ioasid_t ioasid, enum ioasid_notify_val cmd, unsigned int flags); voidioasid_is_in_set(struct ioasid_set *set, ioasid_t ioasid); intioasid_get(struct ioasid_set *set, ioasid_t ioasid); intioasid_get_locked(struct ioasid_set *set, ioasid_t ioasid); @@ -85,6 +138,9 @@ voidioasid_put_locked(struct ioasid_set *set, ioasid_t ioasid); intioasid_set_for_each_ioasid(struct ioasid_set *sdata, void (*fn)(ioasid_t id, void *data), void *data);
+intioasid_register_notifier_mm(struct mm_struct *mm, struct notifier_block *nb); +voidioasid_unregister_notifier_mm(struct mm_struct *mm, struct notifier_block *nb); +
#else /* !CONFIG_IOASID */
static inline voidioasid_install_capacity(ioasid_t total){include/linux/ioasid.h +11
-0@@ -124,6 +180,20 @@ static inline void *ioasid_find(struct ioasid_set *set, ioasid_t ioasid, bool (* return NULL; } +static inline int ioasid_register_notifier (struct notifier_block *nb) +{ +return -ENOTSUPP ; +} + +static inline void ioasid_unregister_notifier (struct notifier_block *nb) +{ +} + +static inline int ioasid_notify (ioasid_t ioasid,enum ioasid_notify_val cmd, unsigned int flags) +{ +return -ENOTSUPP ; +} + static inline int ioasid_register_allocator(structQuoted 3 lines
ioasid_allocator_ops*allocator) {
return-ENOTSUPP;Thanks
Quoted 6 lines
Eric
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu[Jacob Pan]
PATCH v2 7/9 iommu/vt-d: Listen to IOASID notifications
In reply to Jacob PanOn Intel Scalable I/O Virtualization (SIOV) enabled platforms, IOMMU driver is one of the users of IOASIDs. In normal flow, callers will perform IOASID allocation, bind, unbind, and free in order. However, for guest SVA, IOASID free could come before unbind as guest is untrusted. This patch registers IOASID notification handler such that IOMMU driver can perform PASID teardown upon receiving an unexpected IOASID free event.
--- drivers/iommu/intel/svm.c | 74 ++++++++++++++++++++++++++++++++++++++++++++- include/linux/intel-iommu.h | 2 ++ 2 files changed, 75 insertions(+), 1 deletion(-)
drivers/iommu/intel/svm.c +73 -1
@@ -95,6 +95,72 @@ static inline bool intel_svm_capable(struct intel_iommu *iommu)return iommu->flags &VTD_FLAG_SVM_CAPABLE ; } +#define pasid_lock_held () lock_is_held(&pasid_mutex.dep_map) +static DEFINE_MUTEX (pasid_mutex); + +static void intel_svm_free_async_fn (struct work_struct *work) +{ + struct intel_svm *svm = container_of(work, struct intel_svm, work); +struct intel_svm_dev *sdev; + +/* +* Unbind all devices associated with this PASID which is +* being freed by other users such as VFIO. +*/ +mutex_lock (&pasid_mutex); +list_for_each_entry_rcu (sdev, &svm->devs, list,pasid_lock_held ()) { +/* Does not poison forward pointer */ +list_del_rcu (&sdev->list); +spin_lock (&svm->iommu->lock); +intel_pasid_tear_down_entry (svm->iommu, sdev->dev, + svm->pasid, true); +spin_unlock (&svm->iommu->lock); +kfree_rcu (sdev, rcu); +/* +* Free before unbind only happens with guest usaged +* host PASIDs. IOASID free will detach private data +* and free the IOASID entry. +*/ +ioasid_put (NULL , svm->pasid); +if (list_empty (&svm->devs)) +kfree (svm); + } +mutex_unlock (&pasid_mutex); +} + + +static int pasid_status_change (struct notifier_block *nb, +unsigned long code,void *data) +{ +struct ioasid_nb_args *args = (struct ioasid_nb_args *)data; +struct intel_svm *svm = (struct intel_svm *)args->pdata; +int ret =NOTIFY_DONE ; + +if (code ==IOASID_FREE ) { +if (!svm) + goto done; +if (args->id != svm->pasid) { +pr_warn ("Notify PASID does not match data %d : %d\n" , + args->id, svm->pasid); + goto done; + } +schedule_work (&svm->work); +return NOTIFY_OK ; + } +done: +return ret; +} + +static struct notifier_block pasid_nb = { + .notifier_call = pasid_status_change, +}; + +void intel_svm_add_pasid_notifier (void ) +{ +/* Listen to all PASIDs, not specific to a set */ +ioasid_register_notifier (NULL , &pasid_nb); +} +void intel_svm_check (struct intel_iommu *iommu) {if (!pasid_supported (iommu))
@@ -221,7 +287,6 @@ static const struct mmu_notifier_ops intel_mmuops = { .invalidate_range = intel_invalidate_range, }; -static DEFINE_MUTEX (pasid_mutex);static LIST_HEAD (global_svm_list);#define for_each_svm_dev (sdev, svm, d) \
@@ -342,7 +407,14 @@ int intel_svm_bind_gpasid(struct iommu_domain *domain, struct device *dev, svm->gpasid = data->gpasid; svm->flags |=SVM_FLAG_GUEST_PASID ; } + svm->iommu = iommu; +/* +* Set up cleanup async work in case IOASID core notify us PASID +* is freed before unbind. +*/ +INIT_WORK (&svm->work, intel_svm_free_async_fn);ioasid_attach_data (data->hpasid, svm); +ioasid_get (NULL , svm->pasid);INIT_LIST_HEAD_RCU (&svm->devs);mmput (svm->mm); }
include/linux/intel-iommu.h +2 -0
@@ -744,6 +744,7 @@ void intel_svm_unbind(struct iommu_sva *handle);int intel_svm_get_pasid (struct iommu_sva *handle);int intel_svm_page_response (struct device *dev,struct iommu_fault_event *evt,struct iommu_page_response *msg); +void intel_svm_add_pasid_notifier (void );struct svm_dev_ops ;
@@ -770,6 +771,7 @@ struct intel_svm {int gpasid;/* In case that guest PASID is different from host PASID */ struct list_head devs;struct list_head list; + struct work_struct work; /* For deferred clean up */ };#else static inline void intel_svm_check (struct intel_iommu *iommu) {}
Signature · 1 line
2.7.4
Re: [PATCH v2 7/9] iommu/vt-d: Listen to IOASID notifications
Hi Jacob,
Quoted 19 lines · Jacob Pan
On Intel Scalable I/O Virtualization (SIOV) enabled platforms, IOMMU driver is one of the users of IOASIDs. In normal flow, callers will perform IOASID allocation, bind, unbind, and free in order. However, for guest SVA, IOASID free could come before unbind as guest is untrusted. This patch registers IOASID notification handler such that IOMMU driver can perform PASID teardown upon receiving an unexpected IOASID free event.
Signed-off-by Jacob Pan <[email protected]>--- drivers/iommu/intel/svm.c | 74 ++++++++++++++++++++++++++++++++++++++++++++- include/linux/intel-iommu.h | 2 ++ 2 files changed, 75 insertions(+), 1 deletion(-)drivers/iommu/intel/svm.c +0
-0
drivers/iommu/intel/svm.c static inline bool intel_svm_capable(struct intel_iommu *iommu) · line 98
Quoted 4 lines
drivers/iommu/intel/svm.c +1
-0@@ -95,6 +95,72 @@ static inline bool intel_svm_capable(struct intel_iommu *iommu)return iommu->flags &VTD_FLAG_SVM_CAPABLE ; } +#define pasid_lock_held () lock_is_held(&pasid_mutex.dep_map)
put after the pasid_mutex definition?
Quoted 12 lines
drivers/iommu/intel/svm.c +12
-0+static DEFINE_MUTEX (pasid_mutex); + +static void intel_svm_free_async_fn (struct work_struct *work) +{ + struct intel_svm *svm = container_of(work, struct intel_svm, work); +struct intel_svm_dev *sdev; + +/* +* Unbind all devices associated with this PASID which is +* being freed by other users such as VFIO. +*/ +mutex_lock (&pasid_mutex);
drivers/iommu/intel/svm.c static inline bool intel_svm_capable(struct intel_iommu *iommu) · line 120
Quoted 10 lines
drivers/iommu/intel/svm.c +10
-0+list_for_each_entry_rcu (sdev, &svm->devs, list,pasid_lock_held ()) { +/* Does not poison forward pointer */ +list_del_rcu (&sdev->list); +spin_lock (&svm->iommu->lock); +intel_pasid_tear_down_entry (svm->iommu, sdev->dev, + svm->pasid, true); +spin_unlock (&svm->iommu->lock); +kfree_rcu (sdev, rcu); +/* +* Free before unbind only happens with guest usaged
usaged?
Quoted 82 lines
drivers/iommu/intel/svm.c +49
-1+* host PASIDs. IOASID free will detach private data +* and free the IOASID entry. +*/ +ioasid_put (NULL , svm->pasid); +if (list_empty (&svm->devs)) +kfree (svm); + } +mutex_unlock (&pasid_mutex); +} + + +static int pasid_status_change (struct notifier_block *nb, +unsigned long code,void *data) +{ +struct ioasid_nb_args *args = (struct ioasid_nb_args *)data; +struct intel_svm *svm = (struct intel_svm *)args->pdata; +int ret =NOTIFY_DONE ; + +if (code ==IOASID_FREE ) { +if (!svm) + goto done; +if (args->id != svm->pasid) { +pr_warn ("Notify PASID does not match data %d : %d\n" , + args->id, svm->pasid); + goto done; + } +schedule_work (&svm->work); +return NOTIFY_OK ; + } +done: + return ret;> +} + +static struct notifier_block pasid_nb = { + .notifier_call = pasid_status_change, +}; + +void intel_svm_add_pasid_notifier (void ) +{ +/* Listen to all PASIDs, not specific to a set */ +ioasid_register_notifier (NULL , &pasid_nb); +} +void intel_svm_check (struct intel_iommu *iommu) {if (!pasid_supported (iommu))@@ -221,7 +287,6 @@ static const struct mmu_notifier_ops intel_mmuops = { .invalidate_range = intel_invalidate_range, }; -static DEFINE_MUTEX (pasid_mutex);static LIST_HEAD (global_svm_list);#define for_each_svm_dev (sdev, svm, d) \@@ -342,7 +407,14 @@ int intel_svm_bind_gpasid(struct iommu_domain *domain, struct device *dev, svm->gpasid = data->gpasid; svm->flags |=SVM_FLAG_GUEST_PASID ; } + svm->iommu = iommu; +/* +* Set up cleanup async work in case IOASID core notify us PASID +* is freed before unbind. +*/ +INIT_WORK (&svm->work, intel_svm_free_async_fn);ioasid_attach_data (data->hpasid, svm); +ioasid_get (NULL , svm->pasid);INIT_LIST_HEAD_RCU (&svm->devs);mmput (svm->mm); }include/linux/intel-iommu.h +1
-0@@ -744,6 +744,7 @@ void intel_svm_unbind(struct iommu_sva *handle);int intel_svm_get_pasid (struct iommu_sva *handle);int intel_svm_page_response (struct device *dev,struct iommu_fault_event *evt,struct iommu_page_response *msg); +void intel_svm_add_pasid_notifier (void );struct svm_dev_ops ;
include/linux/intel-iommu.h struct intel_svm { · line 777
Quoted 7 lines
include/linux/intel-iommu.h +1
-0@@ -770,6 +771,7 @@ struct intel_svm {int gpasid;/* In case that guest PASID is different from host PASID */ struct list_head devs;struct list_head list; + struct work_struct work; /* For deferred clean up */ };#else static inline void intel_svm_check (struct intel_iommu *iommu) {}
Thanks
Eric
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
Re: [PATCH v2 7/9] iommu/vt-d: Listen to IOASID notifications
Quoted 25 lines · On Tue, 1 Sep 2020 19:03:23 +0200 Auger Eric <[email protected]> wrote:
Hi Jacob,
Quoted 22 lines · Jacob Pan
On Intel Scalable I/O Virtualization (SIOV) enabled platforms, IOMMU driver is one of the users of IOASIDs. In normal flow, callers will perform IOASID allocation, bind, unbind, and free in order. However, for guest SVA, IOASID free could come before unbind as guest is untrusted. This patch registers IOASID notification handler such that IOMMU driver can perform PASID teardown upon receiving an unexpected IOASID free event.
Signed-off-by Jacob Pan <[email protected]>--- drivers/iommu/intel/svm.c | 74 ++++++++++++++++++++++++++++++++++++++++++++- include/linux/intel-iommu.h | 2 ++ 2 files changed, 75 insertions(+), 1 deletion(-)drivers/iommu/intel/svm.c +0
-0@@ -95,6 +95,72 @@ static inline bool intel_svm_capable(struct intel_iommu *iommu)return iommu->flags &VTD_FLAG_SVM_CAPABLE ; }
Quoted 2 lines
drivers/iommu/intel/svm.c static inline bool intel_svm_capable(struct intel_iommu *iommu) · line 98
Quoted 1 line
drivers/iommu/intel/svm.c +1
-0+#define pasid_lock_held () lock_is_held(&pasid_mutex.dep_map)put after the
pasid_mutexdefinition?
yes,
Quoted 15 lines
Quoted 15 lines
drivers/iommu/intel/svm.c +15
-0+static DEFINE_MUTEX (pasid_mutex); + +static void intel_svm_free_async_fn (struct work_struct *work) +{ + struct intel_svm *svm = container_of(work, struct intel_svm, work); +struct intel_svm_dev *sdev; + +/* +* Unbind all devices associated with this PASID which is +* being freed by other users such as VFIO. +*/ +mutex_lock (&pasid_mutex); +list_for_each_entry_rcu (sdev, &svm->devs, list,pasid_lock_held ()) { +/* Does not poison forward pointer */ +list_del_rcu (&sdev->list);
Quoted 8 lines
drivers/iommu/intel/svm.c static inline bool intel_svm_capable(struct intel_iommu *iommu) · line 120
Quoted 7 lines
drivers/iommu/intel/svm.c +7
-0+spin_lock (&svm->iommu->lock); +intel_pasid_tear_down_entry (svm->iommu, sdev->dev, + svm->pasid, true); +spin_unlock (&svm->iommu->lock); +kfree_rcu (sdev, rcu); +/* +* Free before unbind only happens with guest usaged usaged?
used. I meant host PASID used to back guest PASID. I will reword the comments:
/*
* Free before unbind can only happen with host PASIDs used for
* guest SVM. We get here because ioasid_free is called with
* outstanding references. So we need to drop the reference
* such that the PASID can be reclaimed. Once refcount reaches
* zero, IOASID core will detach the private data and erase the
* IOASID entry.
*/
Quoted 92 lines
Quoted 82 lines
drivers/iommu/intel/svm.c +49
-1+* host PASIDs. IOASID free will detach private data +* and free the IOASID entry. +*/ +ioasid_put (NULL , svm->pasid); +if (list_empty (&svm->devs)) +kfree (svm); + } +mutex_unlock (&pasid_mutex); +} + + +static int pasid_status_change (struct notifier_block *nb, +unsigned long code,void *data) +{ +struct ioasid_nb_args *args = (struct ioasid_nb_args *)data; +struct intel_svm *svm = (struct intel_svm *)args->pdata; +int ret =NOTIFY_DONE ; + +if (code ==IOASID_FREE ) { +if (!svm) + goto done; +if (args->id != svm->pasid) { +pr_warn ("Notify PASID does not match data %d : %d\n" , + args->id, svm->pasid); + goto done; + } +schedule_work (&svm->work); +return NOTIFY_OK ; + } +done: + return ret;> +} + +static struct notifier_block pasid_nb = { + .notifier_call = pasid_status_change, +}; + +void intel_svm_add_pasid_notifier (void ) +{ +/* Listen to all PASIDs, not specific to a set */ +ioasid_register_notifier (NULL , &pasid_nb); +} +void intel_svm_check (struct intel_iommu *iommu) {if (!pasid_supported (iommu))@@ -221,7 +287,6 @@ static const struct mmu_notifier_ops intel_mmuops = { .invalidate_range = intel_invalidate_range, }; -static DEFINE_MUTEX (pasid_mutex);static LIST_HEAD (global_svm_list);#define for_each_svm_dev (sdev, svm, d) \@@ -342,7 +407,14 @@ int intel_svm_bind_gpasid(struct iommu_domain *domain, struct device *dev, svm->gpasid = data->gpasid; svm->flags |=SVM_FLAG_GUEST_PASID ; } + svm->iommu = iommu; +/* +* Set up cleanup async work in case IOASID core notify us PASID +* is freed before unbind. +*/ +INIT_WORK (&svm->work, intel_svm_free_async_fn);ioasid_attach_data (data->hpasid, svm); +ioasid_get (NULL , svm->pasid);INIT_LIST_HEAD_RCU (&svm->devs);mmput (svm->mm); }include/linux/intel-iommu.h +1
-0@@ -744,6 +744,7 @@ void intel_svm_unbind(struct iommu_sva *handle);int intel_svm_get_pasid (struct iommu_sva *handle);int intel_svm_page_response (struct device *dev,struct iommu_fault_event *evt,struct iommu_page_response *msg); +void intel_svm_add_pasid_notifier (void );struct svm_dev_ops ;include/linux/intel-iommu.h struct intel_svm { · line 777
Quoted 7 lines
include/linux/intel-iommu.h +1
-0@@ -770,6 +771,7 @@ struct intel_svm {int gpasid;/* In case that guest PASID is different from host PASID */ struct list_head devs;struct list_head list; + struct work_struct work; /* For deferred clean up */ };#else static inline void intel_svm_check (struct intel_iommu *iommu) {}Thanks
Quoted 6 lines
Eric
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
[Jacob Pan]
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
PATCH v2 8/9 iommu/vt-d: Send IOASID bind/unbind notifications
In reply to Jacob PanOn Intel Scalable I/O Virtualization (SIOV) enabled platforms with ENQCMD in use by the guest, KVM must establish a guest-host PASID translation table prior to the issuing of ENQCMD. PASID translation table also depends on the IOMMU PASID entry, which is configured during the guest page table bind time. This patch adds a notification event to the IOMMU driver such that KVM can be notified when a new guest-host PASID mapping is established or demolished.
--- drivers/iommu/intel/svm.c | 7 +++++++ 1 file changed, 7 insertions(+)
drivers/iommu/intel/svm.c +7 -0
@@ -471,6 +471,11 @@ int intel_svm_bind_gpasid(struct iommu_domain *domain, struct device *dev, }mutex_unlock (&pasid_mutex); +/* +* Notify KVM new host-guest PASID bind is ready. KVM will set up +* PASID translation table to support guest ENQCMD. +*/ +ioasid_notify (data->hpasid,IOASID_BIND ,IOASID_NOTIFY_SET );return ret; }
@@ -510,6 +515,8 @@ int intel_svm_unbind_gpasid(struct device *dev, int pasid)* and perform cleanup. */ ioasid_attach_data (pasid,NULL ); +ioasid_notify (pasid,IOASID_UNBIND , +IOASID_NOTIFY_SET );kfree (svm); } }
Signature · 1 line
2.7.4
PATCH v2 9/9 iommu/vt-d: Store guest PASID during bind
In reply to Jacob PanIOASID core maintains the guest-host mapping in the form of SPID and IOASID. This patch assigns the guest PASID (if valid) as SPID while binding guest page table with a host PASID. This mapping will be used for lookup and notifications.
--- drivers/iommu/intel/svm.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/iommu/intel/svm.c +2 -0
@@ -406,6 +406,7 @@ int intel_svm_bind_gpasid(struct iommu_domain *domain, struct device *dev,if (data->flags &IOMMU_SVA_GPASID_VAL ) { svm->gpasid = data->gpasid; svm->flags |=SVM_FLAG_GUEST_PASID ; +ioasid_attach_spid (data->hpasid, data->gpasid); } svm->iommu = iommu;/*
@@ -517,6 +518,7 @@ int intel_svm_unbind_gpasid(struct device *dev, int pasid)ioasid_attach_data (pasid,NULL );ioasid_notify (pasid,IOASID_UNBIND ,IOASID_NOTIFY_SET ); +ioasid_attach_spid (pasid,INVALID_IOASID );kfree (svm); } }
Signature · 1 line
2.7.4
Re: [PATCH v2 9/9] iommu/vt-d: Store guest PASID during bind
Hi Jacob,
Quoted 15 lines · Jacob Pan
IOASID core maintains the guest-host mapping in the form of SPID and IOASID. This patch assigns the guest PASID (if valid) as SPID while binding guest page table with a host PASID. This mapping will be used for lookup and notifications.
Signed-off-by Jacob Pan <[email protected]>--- drivers/iommu/intel/svm.c | 2 ++ 1 file changed, 2 insertions(+)drivers/iommu/intel/svm.c +0
-0
drivers/iommu/intel/svm.c int intel_svm_bind_gpasid(struct iommu_domain *domain, struct device *dev, · line 409
Quoted 4 lines
drivers/iommu/intel/svm.c +1
-0@@ -406,6 +406,7 @@ int intel_svm_bind_gpasid(struct iommu_domain *domain, struct device *dev,if (data->flags &IOMMU_SVA_GPASID_VAL ) { svm->gpasid = data->gpasid; svm->flags |=SVM_FLAG_GUEST_PASID ; +ioasid_attach_spid (data->hpasid, data->gpasid);
don't you want to handle the returned value?
drivers/iommu/intel/svm.c int intel_svm_unbind_gpasid(struct device *dev, int pasid) · line 521
Quoted 8 lines
drivers/iommu/intel/svm.c +1
-0} svm->iommu = iommu;/* @@ -517,6 +518,7 @@ int intel_svm_unbind_gpasid(struct device *dev, int pasid)ioasid_attach_data (pasid,NULL );ioasid_notify (pasid,IOASID_UNBIND ,IOASID_NOTIFY_SET ); +ioasid_attach_spid (pasid,INVALID_IOASID );
So this answers my previous question ;-) but won't it enter the if (!ioasid_data) path and fail to reset the spid?
Eric
drivers/iommu/intel/svm.c int intel_svm_unbind_gpasid(struct device *dev, int pasid) · line 524
Quoted 4 lines
drivers/iommu/intel/svm.c +0
-0kfree (svm); } }
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
Re: [PATCH v2 9/9] iommu/vt-d: Store guest PASID during bind
Quoted 12 lines · On Tue, 1 Sep 2020 19:08:44 +0200 Auger Eric <[email protected]> wrote:
Hi Jacob,
Quoted 10 lines · Jacob Pan
IOASID core maintains the guest-host mapping in the form of SPID and IOASID. This patch assigns the guest PASID (if valid) as SPID while binding guest page table with a host PASID. This mapping will be used for lookup and notifications.
Signed-off-by Jacob Pan <[email protected]>--- drivers/iommu/intel/svm.c | 2 ++ 1 file changed, 2 insertions(+)
Quoted 11 lines
drivers/iommu/intel/svm.c int intel_svm_bind_gpasid(struct iommu_domain · line 410
Quoted 10 lines
drivers/iommu/intel/svm.c +1
-0@@ -406,6 +406,7 @@ int intel_svm_bind_gpasid(struct iommu_domain *domain, struct device *dev, if (data->flags & IOMMU_SVA_GPASID_VAL) { svm->gpasid = data->gpasid; svm->flags |=SVM_FLAG_GUEST_PASID ; +ioasid_attach_spid (data->hpasid, data->gpasid);don't you want to handle the returned value?
Yes, I also need to add a check for duplicated SPID within a set.
Quoted 12 lines
drivers/iommu/intel/svm.c int intel_svm_unbind_gpasid(struct device *dev, · line 522
Quoted 9 lines
drivers/iommu/intel/svm.c +0
-0} svm->iommu = iommu;drivers/iommu/intel/svm.c +1
-0/* @@ -517,6 +518,7 @@ int intel_svm_unbind_gpasid(struct device *dev, int pasid) ioasid_attach_data(pasid, NULL);ioasid_notify (pasid,IOASID_UNBIND ,IOASID_NOTIFY_SET ); +ioasid_attach_spid (pasid,INVALID_IOASID );So this answers my previous question ;-) but won't it enter the if (!ioasid_data) path and fail to reset the spid?
Sorry, i am not following. If there is no ioasid_data then there is no SPID to be stored.
BTW, I plan to separate the APIs into two.ioasid_attach_spidioasid_detach_spid
Only ioasid_detach_spid will be calling synchronize RCU, then ioasid_attach_spid can be called under spinlock.
Thanks,
Quoted 10 lines
Eric
Quoted 4 lines
drivers/iommu/intel/svm.c +0
-0kfree (svm); }}
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
[Jacob Pan]
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu