Skip to content

Commit

Permalink
remove provider reference counter
Browse files Browse the repository at this point in the history
  • Loading branch information
calccrypto committed Jul 9, 2024
1 parent 8d8cd82 commit 20f2d43
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
1 change: 0 additions & 1 deletion include/dpusm/provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ typedef struct dpusm_provider_handle {
struct module *module;
dpusm_pc_t capabilities; /* constant set of capabilities */
const dpusm_pf_t *funcs; /* reference to a struct */
atomic_t refs; /* how many users are holding this provider */
struct list_head list;
struct dpusm_provider_handle *self;
} dpusm_ph_t;
Expand Down
19 changes: 9 additions & 10 deletions src/provider.c
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,6 @@ dpusmph_init(struct module *module, const dpusm_pf_t *funcs)
dpusmph->module = module;
dpusmph->funcs = funcs;
dpusmph->self = dpusmph;
atomic_set(&dpusmph->refs, 0);
}

return dpusmph;
Expand Down Expand Up @@ -310,8 +309,8 @@ dpusm_provider_unregister_handle(dpusm_t *dpusm, dpusm_ph_t **provider) {
}

int rc = 0;
const int refs = atomic_read(&(*provider)->refs);
if (refs) {
const int refs = module_refcount((*provider)->module);
if (refs > 0) {
printk("%s: Unregistering provider \"%s\" with %d references remaining.\n",
__func__, module_name((*provider)->module), refs);
rc = -EBUSY;
Expand Down Expand Up @@ -358,17 +357,18 @@ dpusm_provider_get(dpusm_t *dpusm, const char *name) {
read_lock(&dpusm->lock);
dpusm_ph_t **provider = find_provider(dpusm, name);
if (provider) {
struct module *module = (*provider)->module;

/* make sure provider can't be unloaded before user */
if (!try_module_get((*provider)->module)) {
if (!try_module_get(module)) {
printk("Error: Could not increment reference count of %s\n", name);
return NULL;
}

atomic_inc(&(*provider)->refs);
atomic_inc(&dpusm->active);

printk("%s: User has been given a handle to \"%s\" (%p) (now %d users).\n",
__func__, name, *provider, atomic_read(&(*provider)->refs));
__func__, name, *provider, module_refcount(module));

if ((*provider)->funcs->at_connect) {
(*provider)->funcs->at_connect();
Expand All @@ -393,14 +393,13 @@ dpusm_provider_put(dpusm_t *dpusm, void *handle) {

struct module *module = (*provider)->module;

if (!atomic_read(&(*provider)->refs)) {
if (!module_refcount(module)) {
printk("%s Error: Cannot decrement provider \"%s\" user count already at 0.\n",
__func__, module_name(module));
return DPUSM_ERROR;
}

module_put(module);
atomic_dec(&(*provider)->refs);
atomic_dec(&dpusm->active);

if ((*provider)->funcs) { /* provider might have been invalidated */
Expand All @@ -410,7 +409,7 @@ dpusm_provider_put(dpusm_t *dpusm, void *handle) {
}

printk("%s: User has returned a handle to \"%s\" (%p) (now %d users).\n",
__func__, module_name(module), *provider, atomic_read(&(*provider)->refs));
__func__, module_name(module), *provider, module_refcount(module));
return DPUSM_OK;
}

Expand All @@ -431,7 +430,7 @@ void dpusm_provider_invalidate(dpusm_t *dpusm, const char *name) {
(*provider)->funcs = NULL;
memset(&(*provider)->capabilities, 0, sizeof((*provider)->capabilities));
printk("%s: Provider \"%s\" has been invalidated with %d users active.\n",
__func__, name, atomic_read(&(*provider)->refs));
__func__, name, module_refcount((*provider)->module));
/* not decrementing module reference count here - provider is still registered */
}
else {
Expand Down

0 comments on commit 20f2d43

Please sign in to comment.