Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix assertions in crypto reference helpers #13880

Merged
merged 1 commit into from
Sep 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 21 additions & 21 deletions module/icp/include/sys/crypto/impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,38 +126,37 @@ typedef struct kcf_provider_desc {
crypto_provider_id_t pd_prov_id;
} kcf_provider_desc_t;

/* atomic operations in linux implicitly form a memory barrier */
#define membar_exit()

/*
* If a component has a reference to a kcf_provider_desc_t,
* it REFHOLD()s. A new provider descriptor which is referenced only
* by the providers table has a reference counter of one.
*/
#define KCF_PROV_REFHOLD(desc) { \
atomic_add_32(&(desc)->pd_refcnt, 1); \
ASSERT((desc)->pd_refcnt != 0); \
#define KCF_PROV_REFHOLD(desc) { \
int newval = atomic_add_32_nv(&(desc)->pd_refcnt, 1); \
ASSERT(newval != 0); \
}

#define KCF_PROV_IREFHOLD(desc) { \
atomic_add_32(&(desc)->pd_irefcnt, 1); \
ASSERT((desc)->pd_irefcnt != 0); \
#define KCF_PROV_IREFHOLD(desc) { \
int newval = atomic_add_32_nv(&(desc)->pd_irefcnt, 1); \
ASSERT(newval != 0); \
}

#define KCF_PROV_IREFRELE(desc) { \
ASSERT((desc)->pd_irefcnt != 0); \
membar_exit(); \
if (atomic_add_32_nv(&(desc)->pd_irefcnt, -1) == 0) { \
membar_producer(); \
int newval = atomic_add_32_nv(&(desc)->pd_irefcnt, -1); \
ASSERT(newval != -1); \
if (newval == 0) { \
cv_broadcast(&(desc)->pd_remove_cv); \
} \
}

#define KCF_PROV_REFHELD(desc) ((desc)->pd_refcnt >= 1)

#define KCF_PROV_REFRELE(desc) { \
ASSERT((desc)->pd_refcnt != 0); \
membar_exit(); \
if (atomic_add_32_nv(&(desc)->pd_refcnt, -1) == 0) { \
membar_producer(); \
int newval = atomic_add_32_nv(&(desc)->pd_refcnt, -1); \
ASSERT(newval != -1); \
if (newval == 0) { \
kcf_provider_zero_refcnt((desc)); \
} \
}
Expand Down Expand Up @@ -193,19 +192,20 @@ typedef struct kcf_mech_entry {
* it REFHOLD()s. A new policy descriptor which is referenced only
* by the policy table has a reference count of one.
*/
#define KCF_POLICY_REFHOLD(desc) { \
atomic_add_32(&(desc)->pd_refcnt, 1); \
ASSERT((desc)->pd_refcnt != 0); \
#define KCF_POLICY_REFHOLD(desc) { \
int newval = atomic_add_32_nv(&(desc)->pd_refcnt, 1); \
ASSERT(newval != 0); \
}

/*
* Releases a reference to a policy descriptor. When the last
* reference is released, the descriptor is freed.
*/
#define KCF_POLICY_REFRELE(desc) { \
ASSERT((desc)->pd_refcnt != 0); \
membar_exit(); \
if (atomic_add_32_nv(&(desc)->pd_refcnt, -1) == 0) \
membar_producer(); \
int newval = atomic_add_32_nv(&(desc)->pd_refcnt, -1); \
ASSERT(newval != -1); \
if (newval == 0) \
kcf_policy_free_desc(desc); \
}

Expand Down
7 changes: 4 additions & 3 deletions module/icp/include/sys/crypto/sched_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,10 @@ typedef struct kcf_context {
* context structure is freed along with the global context.
*/
#define KCF_CONTEXT_REFRELE(ictx) { \
ASSERT((ictx)->kc_refcnt != 0); \
membar_exit(); \
if (atomic_add_32_nv(&(ictx)->kc_refcnt, -1) == 0) \
membar_producer(); \
int newval = atomic_add_32_nv(&(ictx)->kc_refcnt, -1); \
ASSERT(newval != -1); \
if (newval == 0) \
kcf_free_context(ictx); \
}

Expand Down