diff options
Diffstat (limited to 'include/linux/kref.h')
-rw-r--r-- | include/linux/kref.h | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/include/linux/kref.h b/include/linux/kref.h index 4972e6e9ca93..7419c02085d7 100644 --- a/include/linux/kref.h +++ b/include/linux/kref.h | |||
@@ -19,6 +19,7 @@ | |||
19 | #include <linux/atomic.h> | 19 | #include <linux/atomic.h> |
20 | #include <linux/kernel.h> | 20 | #include <linux/kernel.h> |
21 | #include <linux/mutex.h> | 21 | #include <linux/mutex.h> |
22 | #include <linux/spinlock.h> | ||
22 | 23 | ||
23 | struct kref { | 24 | struct kref { |
24 | atomic_t refcount; | 25 | atomic_t refcount; |
@@ -95,6 +96,38 @@ static inline int kref_put(struct kref *kref, void (*release)(struct kref *kref) | |||
95 | return kref_sub(kref, 1, release); | 96 | return kref_sub(kref, 1, release); |
96 | } | 97 | } |
97 | 98 | ||
99 | /** | ||
100 | * kref_put_spinlock_irqsave - decrement refcount for object. | ||
101 | * @kref: object. | ||
102 | * @release: pointer to the function that will clean up the object when the | ||
103 | * last reference to the object is released. | ||
104 | * This pointer is required, and it is not acceptable to pass kfree | ||
105 | * in as this function. | ||
106 | * @lock: lock to take in release case | ||
107 | * | ||
108 | * Behaves identical to kref_put with one exception. If the reference count | ||
109 | * drops to zero, the lock will be taken atomically wrt dropping the reference | ||
110 | * count. The release function has to call spin_unlock() without _irqrestore. | ||
111 | */ | ||
112 | static inline int kref_put_spinlock_irqsave(struct kref *kref, | ||
113 | void (*release)(struct kref *kref), | ||
114 | spinlock_t *lock) | ||
115 | { | ||
116 | unsigned long flags; | ||
117 | |||
118 | WARN_ON(release == NULL); | ||
119 | if (atomic_add_unless(&kref->refcount, -1, 1)) | ||
120 | return 0; | ||
121 | spin_lock_irqsave(lock, flags); | ||
122 | if (atomic_dec_and_test(&kref->refcount)) { | ||
123 | release(kref); | ||
124 | local_irq_restore(flags); | ||
125 | return 1; | ||
126 | } | ||
127 | spin_unlock_irqrestore(lock, flags); | ||
128 | return 0; | ||
129 | } | ||
130 | |||
98 | static inline int kref_put_mutex(struct kref *kref, | 131 | static inline int kref_put_mutex(struct kref *kref, |
99 | void (*release)(struct kref *kref), | 132 | void (*release)(struct kref *kref), |
100 | struct mutex *lock) | 133 | struct mutex *lock) |