qobject: make refcount atomic
PATCH 1/1 qobject: make refcount atomic
The Rust bindings for QObject will only operate on complete objects, treating them as immutable as long as the Rust QObject is live.
With that constraint, it is trivial for Rust code to treat QObjects as thread-safe; all that's needed is to make reference count operations atomic. Do the same when the C code adds or removes references, since we don't really know what the Rust code is up to; of course C code will have to agree with not making changes to the QObjects after they've been passed to Rust code.
--- include/qobject/qobject.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
include/qobject/qobject.h +3 -2
@@ -32,6 +32,7 @@#ifndef QOBJECT_H #define QOBJECT_H +#include "qemu/atomic.h" #include "qapi/qapi-builtin-types.h" /* Not for use outside include/qobject/ */
@@ -73,7 +74,7 @@ QEMU_BUILD_BUG_MSG(QTYPE__MAX != 7,static inline void qobject_ref_impl (QObject *obj) {if (obj) { - obj->base.refcnt++; +qatomic_inc (&obj->base.refcnt); } }
@@ -95,7 +96,7 @@ void qobject_destroy(QObject *obj);static inline void qobject_unref_impl (QObject *obj) {assert (!obj || obj->base.refcnt); -if (obj && --obj->base.refcnt ==0 ) { +if (obj &&qatomic_fetch_dec (&obj->base.refcnt) ==1 ) {qobject_destroy (obj); } }
Signature · 1 line
2.51.0
include/qobject/qobject.h void qobject_destroy(QObject *obj); · line 99
Quoted 6 lines · Paolo Bonzini
include/qobject/qobject.h +1
-1@@ -95,7 +96,7 @@ void qobject_destroy(QObject *obj);static inline void qobject_unref_impl (QObject *obj) {assert (!obj || obj->base.refcnt); -if (obj && --obj->base.refcnt ==0 ) { +if (obj &&qatomic_fetch_dec (&obj->base.refcnt) ==1 ) {
qatomic_dec_fetch lets you compare against 0, which makes all isa's happier.
Otherwise,
r~
Quoted 9 lines · Richard Henderson
include/qobject/qobject.h void qobject_destroy(QObject *obj); · line 99
Quoted 6 lines · Paolo Bonzini
include/qobject/qobject.h +1
-1@@ -95,7 +96,7 @@ void qobject_destroy(QObject *obj); static inline void qobject_unref_impl(QObject *obj) { assert(!obj || obj->base.refcnt); - if (obj && --obj->base.refcnt == 0) { + if (obj && qatomic_fetch_dec(&obj->base.refcnt) == 1) {
qatomic_dec_fetchlets you compare against 0, which makes all isa's happier.
Happy to change all fetch_decs to dec_fetches, but wouldn't the compiler be able to change one to the other?
Quoted 2 lines
Otherwise,
Reviewed-by Richard Henderson <[email protected]>
Thanks!
Paolo
Quoted 11 lines · Paolo Bonzini
Quoted 9 lines · Richard Henderson
include/qobject/qobject.h void qobject_destroy(QObject *obj); · line 99
Quoted 6 lines · Paolo Bonzini
include/qobject/qobject.h +1
-1@@ -95,7 +96,7 @@ void qobject_destroy(QObject *obj); static inline void qobject_unref_impl(QObject *obj) { assert(!obj || obj->base.refcnt); - if (obj && --obj->base.refcnt == 0) { + if (obj && qatomic_fetch_dec(&obj->base.refcnt) == 1) {
qatomic_dec_fetchlets you compare against 0, which makes all isa's happier.
Quoted 1 line
Happy to change all
fetch_decstodec_fetches, but wouldn't the compiler be able to change one to the other?
Interesting. I had just written "optimizations tend to get blocked around atomics, so I wouldn't count on it", but then I stopped to actually try it:
int f(int *p) { return __atomic_add_fetch(p, -1, 0) == 0; } int g(int *p) { return __atomic_fetch_add(p, -1, 0) == 1; }
With current gcc, these two functions compile identically for x86_64, s390x, riscv, and aarch64. I didn't bother testing further.
So, I'm happy with either formulation.
r~
Quoted 5 lines · Richard Henderson
int
f(int *p){ return__atomic_add_fetch(p, -1, 0)== 0; } intg(int *p){ return__atomic_fetch_add(p, -1, 0)== 1; }With current gcc, these two functions compile identically for
x86_64, s390x, riscv, and aarch64. I didn't bother testing further.
The trick is that you have constants everywhere. You have by definition add_fetch(x, y) == fetch_add(x, y) + y, but also in this case the "+ y" can move to the other side and simplify, as if you were in 8th grade.
If either the addend or the compared value were variables things would be different. I had never thought about it, so thanks for pointing it out!
Paolo
Quoted 9 lines · Paolo Bonzini
The Rust bindings for QObject will only operate on complete objects, treating them as immutable as long as the Rust QObject is live.
With that constraint, it is trivial for Rust code to treat QObjects as thread-safe; all that's needed is to make reference count operations atomic. Do the same when the C code adds or removes references, since we don't really know what the Rust code is up to; of course C code will have to agree with not making changes to the QObjects after they've been passed to Rust code.
I wonder whether that latter constraint on the C code will hold true in practice ? Particularly thinking about scenarios where we add/remove link properties to QObject's, potentially at any time during life of QEMU via QMP/HMP commands ?
Quoted 28 lines
Signed-off-by Paolo Bonzini <[email protected]>--- include/qobject/qobject.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)include/qobject/qobject.h +2
-1@@ -32,6 +32,7 @@#ifndef QOBJECT_H #define QOBJECT_H +#include "qemu/atomic.h" #include "qapi/qapi-builtin-types.h" /* Not for use outside include/qobject/ */ @@ -73,7 +74,7 @@ QEMU_BUILD_BUG_MSG(QTYPE__MAX != 7,static inline void qobject_ref_impl (QObject *obj) {if (obj) { - obj->base.refcnt++; +qatomic_inc (&obj->base.refcnt); } }
include/qobject/qobject.h void qobject_destroy(QObject *obj); · line 102
Quoted 8 lines
include/qobject/qobject.h +1
-1@@ -95,7 +96,7 @@ void qobject_destroy(QObject *obj);static inline void qobject_unref_impl (QObject *obj) {assert (!obj || obj->base.refcnt); -if (obj && --obj->base.refcnt ==0 ) { +if (obj &&qatomic_fetch_dec (&obj->base.refcnt) ==1 ) {qobject_destroy (obj); } }
With this unref method being void, how does the Rust code know when a QObject is no longer alive after it has called unref ? Does it need to know this to provide any safety guarantees ?
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Quoted 8 lines · Daniel P. Berrangé
Quoted 3 lines
C code will
have to agree with not making changes to the QObjects after they've been passed to Rust code.I wonder whether that latter constraint on the C code will hold true in practice ? Particularly thinking about scenarios where we add/remove link properties to QObject's, potentially at any time during life of QEMU via QMP/HMP commands ?
Those are QOM objects, not QObjects (the QMP/JSON data model) which are what this patch affects.
C "things" that are not thread-safe are protected by "assert!(bql::is_locked!())", either written explicitly or hidden behind BqlCell/BqlRefCell.
In this case, the safety is covered by QObject::from_raw being unsafe. Whoever calls it needs to know that the C code is not going to mutate the QObject until the Rust side is done with it.
Quoted 4 lines
With this unref method being void, how does the Rust code know when a QObject is no longer alive after it has called unref ? Does it need to know this to provide any safety guarantees ?
No, the safety guarantees can come from knowledge of what the C code will do.
In particular, the safety is covered by QObject::from_raw being unsafe. Whoever calls it needs to know that the C code is not going to mutate the QObject until the Rust side is done with it. This for example is true of arguments to QMP commands, which are the main case where C could pass QObjects directly to Rust.
Something like
void qmp_marshal_query_stats(QDict *args, QObject **ret, Error **errp)
would become
fn qmp_marshal_query_stats(args: *mut QDict,
retp: *mut *mut QObject, errp: *mut *mut Error)
{
let qobj = unsafe { QObject::cloned_from_raw(args.cast()) };
let result = from_qobject::<StatsFilter>(qobj)
.map_err(Into::into)
.and_then(qmp_query_stats);
match result {
Ok(ret) => {
let ret_qobj = to_qobject::<Vec<StatsResult>>(ret);
unsafe { *retp = ret_qobj.into_raw(); }
},
Err(e) => unsafe { e.propagate(errp) },
}
}(and even discounting the lack of tracing, that's quite a bit smaller than the C code thanks to all the automatic consumption of args and ret when passed respectively to qmp_query_stats and to_qobject, just by virtue of how those functions are declared).
Paolo