Skip to content

Commit 7221762

Browse files
willdeaconIngo Molnar
authored andcommitted
locking/refcount: Remove unused refcount_*_checked() variants
The full-fat refcount implementation is exposed via a set of functions suffixed with "_checked()", the idea being that code can choose to use the more expensive, yet more secure implementation on a case-by-case basis. In reality, this hasn't happened, so with a grand total of zero users, let's remove the checked variants for now by simply dropping the suffix and predicating the out-of-line functions on CONFIG_REFCOUNT_FULL=y. Signed-off-by: Will Deacon <[email protected]> Reviewed-by: Ard Biesheuvel <[email protected]> Reviewed-by: Kees Cook <[email protected]> Tested-by: Hanjun Guo <[email protected]> Cc: Ard Biesheuvel <[email protected]> Cc: Elena Reshetova <[email protected]> Cc: Linus Torvalds <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Thomas Gleixner <[email protected]> Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Ingo Molnar <[email protected]>
1 parent 97a1420 commit 7221762

File tree

2 files changed

+36
-43
lines changed

2 files changed

+36
-43
lines changed

include/linux/refcount.h

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,32 +44,21 @@ static inline unsigned int refcount_read(const refcount_t *r)
4444
return atomic_read(&r->refs);
4545
}
4646

47-
extern __must_check bool refcount_add_not_zero_checked(int i, refcount_t *r);
48-
extern void refcount_add_checked(int i, refcount_t *r);
49-
50-
extern __must_check bool refcount_inc_not_zero_checked(refcount_t *r);
51-
extern void refcount_inc_checked(refcount_t *r);
52-
53-
extern __must_check bool refcount_sub_and_test_checked(int i, refcount_t *r);
54-
55-
extern __must_check bool refcount_dec_and_test_checked(refcount_t *r);
56-
extern void refcount_dec_checked(refcount_t *r);
57-
5847
#ifdef CONFIG_REFCOUNT_FULL
5948

6049
#define REFCOUNT_MAX (UINT_MAX - 1)
6150
#define REFCOUNT_SATURATED UINT_MAX
6251

63-
#define refcount_add_not_zero refcount_add_not_zero_checked
64-
#define refcount_add refcount_add_checked
52+
extern __must_check bool refcount_add_not_zero(int i, refcount_t *r);
53+
extern void refcount_add(int i, refcount_t *r);
6554

66-
#define refcount_inc_not_zero refcount_inc_not_zero_checked
67-
#define refcount_inc refcount_inc_checked
55+
extern __must_check bool refcount_inc_not_zero(refcount_t *r);
56+
extern void refcount_inc(refcount_t *r);
6857

69-
#define refcount_sub_and_test refcount_sub_and_test_checked
58+
extern __must_check bool refcount_sub_and_test(int i, refcount_t *r);
7059

71-
#define refcount_dec_and_test refcount_dec_and_test_checked
72-
#define refcount_dec refcount_dec_checked
60+
extern __must_check bool refcount_dec_and_test(refcount_t *r);
61+
extern void refcount_dec(refcount_t *r);
7362

7463
#else
7564

lib/refcount.c

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,10 @@
4343
#include <linux/spinlock.h>
4444
#include <linux/bug.h>
4545

46+
#ifdef CONFIG_REFCOUNT_FULL
47+
4648
/**
47-
* refcount_add_not_zero_checked - add a value to a refcount unless it is 0
49+
* refcount_add_not_zero - add a value to a refcount unless it is 0
4850
* @i: the value to add to the refcount
4951
* @r: the refcount
5052
*
@@ -61,7 +63,7 @@
6163
*
6264
* Return: false if the passed refcount is 0, true otherwise
6365
*/
64-
bool refcount_add_not_zero_checked(int i, refcount_t *r)
66+
bool refcount_add_not_zero(int i, refcount_t *r)
6567
{
6668
unsigned int new, val = atomic_read(&r->refs);
6769

@@ -83,10 +85,10 @@ bool refcount_add_not_zero_checked(int i, refcount_t *r)
8385

8486
return true;
8587
}
86-
EXPORT_SYMBOL(refcount_add_not_zero_checked);
88+
EXPORT_SYMBOL(refcount_add_not_zero);
8789

8890
/**
89-
* refcount_add_checked - add a value to a refcount
91+
* refcount_add - add a value to a refcount
9092
* @i: the value to add to the refcount
9193
* @r: the refcount
9294
*
@@ -101,14 +103,14 @@ EXPORT_SYMBOL(refcount_add_not_zero_checked);
101103
* cases, refcount_inc(), or one of its variants, should instead be used to
102104
* increment a reference count.
103105
*/
104-
void refcount_add_checked(int i, refcount_t *r)
106+
void refcount_add(int i, refcount_t *r)
105107
{
106-
WARN_ONCE(!refcount_add_not_zero_checked(i, r), "refcount_t: addition on 0; use-after-free.\n");
108+
WARN_ONCE(!refcount_add_not_zero(i, r), "refcount_t: addition on 0; use-after-free.\n");
107109
}
108-
EXPORT_SYMBOL(refcount_add_checked);
110+
EXPORT_SYMBOL(refcount_add);
109111

110112
/**
111-
* refcount_inc_not_zero_checked - increment a refcount unless it is 0
113+
* refcount_inc_not_zero - increment a refcount unless it is 0
112114
* @r: the refcount to increment
113115
*
114116
* Similar to atomic_inc_not_zero(), but will saturate at REFCOUNT_SATURATED
@@ -120,7 +122,7 @@ EXPORT_SYMBOL(refcount_add_checked);
120122
*
121123
* Return: true if the increment was successful, false otherwise
122124
*/
123-
bool refcount_inc_not_zero_checked(refcount_t *r)
125+
bool refcount_inc_not_zero(refcount_t *r)
124126
{
125127
unsigned int new, val = atomic_read(&r->refs);
126128

@@ -140,10 +142,10 @@ bool refcount_inc_not_zero_checked(refcount_t *r)
140142

141143
return true;
142144
}
143-
EXPORT_SYMBOL(refcount_inc_not_zero_checked);
145+
EXPORT_SYMBOL(refcount_inc_not_zero);
144146

145147
/**
146-
* refcount_inc_checked - increment a refcount
148+
* refcount_inc - increment a refcount
147149
* @r: the refcount to increment
148150
*
149151
* Similar to atomic_inc(), but will saturate at REFCOUNT_SATURATED and WARN.
@@ -154,14 +156,14 @@ EXPORT_SYMBOL(refcount_inc_not_zero_checked);
154156
* Will WARN if the refcount is 0, as this represents a possible use-after-free
155157
* condition.
156158
*/
157-
void refcount_inc_checked(refcount_t *r)
159+
void refcount_inc(refcount_t *r)
158160
{
159-
WARN_ONCE(!refcount_inc_not_zero_checked(r), "refcount_t: increment on 0; use-after-free.\n");
161+
WARN_ONCE(!refcount_inc_not_zero(r), "refcount_t: increment on 0; use-after-free.\n");
160162
}
161-
EXPORT_SYMBOL(refcount_inc_checked);
163+
EXPORT_SYMBOL(refcount_inc);
162164

163165
/**
164-
* refcount_sub_and_test_checked - subtract from a refcount and test if it is 0
166+
* refcount_sub_and_test - subtract from a refcount and test if it is 0
165167
* @i: amount to subtract from the refcount
166168
* @r: the refcount
167169
*
@@ -180,7 +182,7 @@ EXPORT_SYMBOL(refcount_inc_checked);
180182
*
181183
* Return: true if the resulting refcount is 0, false otherwise
182184
*/
183-
bool refcount_sub_and_test_checked(int i, refcount_t *r)
185+
bool refcount_sub_and_test(int i, refcount_t *r)
184186
{
185187
unsigned int new, val = atomic_read(&r->refs);
186188

@@ -203,10 +205,10 @@ bool refcount_sub_and_test_checked(int i, refcount_t *r)
203205
return false;
204206

205207
}
206-
EXPORT_SYMBOL(refcount_sub_and_test_checked);
208+
EXPORT_SYMBOL(refcount_sub_and_test);
207209

208210
/**
209-
* refcount_dec_and_test_checked - decrement a refcount and test if it is 0
211+
* refcount_dec_and_test - decrement a refcount and test if it is 0
210212
* @r: the refcount
211213
*
212214
* Similar to atomic_dec_and_test(), it will WARN on underflow and fail to
@@ -218,14 +220,14 @@ EXPORT_SYMBOL(refcount_sub_and_test_checked);
218220
*
219221
* Return: true if the resulting refcount is 0, false otherwise
220222
*/
221-
bool refcount_dec_and_test_checked(refcount_t *r)
223+
bool refcount_dec_and_test(refcount_t *r)
222224
{
223-
return refcount_sub_and_test_checked(1, r);
225+
return refcount_sub_and_test(1, r);
224226
}
225-
EXPORT_SYMBOL(refcount_dec_and_test_checked);
227+
EXPORT_SYMBOL(refcount_dec_and_test);
226228

227229
/**
228-
* refcount_dec_checked - decrement a refcount
230+
* refcount_dec - decrement a refcount
229231
* @r: the refcount
230232
*
231233
* Similar to atomic_dec(), it will WARN on underflow and fail to decrement
@@ -234,11 +236,13 @@ EXPORT_SYMBOL(refcount_dec_and_test_checked);
234236
* Provides release memory ordering, such that prior loads and stores are done
235237
* before.
236238
*/
237-
void refcount_dec_checked(refcount_t *r)
239+
void refcount_dec(refcount_t *r)
238240
{
239-
WARN_ONCE(refcount_dec_and_test_checked(r), "refcount_t: decrement hit 0; leaking memory.\n");
241+
WARN_ONCE(refcount_dec_and_test(r), "refcount_t: decrement hit 0; leaking memory.\n");
240242
}
241-
EXPORT_SYMBOL(refcount_dec_checked);
243+
EXPORT_SYMBOL(refcount_dec);
244+
245+
#endif /* CONFIG_REFCOUNT_FULL */
242246

243247
/**
244248
* refcount_dec_if_one - decrement a refcount if it is 1

0 commit comments

Comments
 (0)