Skip to content

Commit f80bbd8

Browse files
committed
random: convert DEBUG_ENT to tracepoints
Instead of using the random driver's ad-hoc DEBUG_ENT() mechanism, use tracepoints instead. This allows for a much more fine-grained control of which debugging mechanism which a developer might need, and unifies the debugging messages with all of the existing tracepoints. Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
1 parent 6265e16 commit f80bbd8

2 files changed

Lines changed: 144 additions & 36 deletions

File tree

‎drivers/char/random.c‎

Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -404,17 +404,6 @@ static DECLARE_WAIT_QUEUE_HEAD(random_read_wait);
404404
static DECLARE_WAIT_QUEUE_HEAD(random_write_wait);
405405
static struct fasync_struct *fasync;
406406

407-
static bool debug;
408-
module_param(debug, bool, 0644);
409-
#define DEBUG_ENT(fmt, arg...) do { \
410-
if (debug) \
411-
printk(KERN_DEBUG "random %04d %04d %04d: " \
412-
fmt,\
413-
input_pool.entropy_count,\
414-
blocking_pool.entropy_count,\
415-
nonblocking_pool.entropy_count,\
416-
## arg); } while (0)
417-
418407
/**********************************************************************
419408
*
420409
* OS independent entropy store. Here are the functions which handle
@@ -612,7 +601,6 @@ static void credit_entropy_bits(struct entropy_store *r, int nbits)
612601
if (!nbits)
613602
return;
614603

615-
DEBUG_ENT("added %d entropy credits to %s\n", nbits, r->name);
616604
retry:
617605
entropy_count = orig = ACCESS_ONCE(r->entropy_count);
618606
if (nfrac < 0) {
@@ -655,7 +643,9 @@ static void credit_entropy_bits(struct entropy_store *r, int nbits)
655643
}
656644

657645
if (entropy_count < 0) {
658-
DEBUG_ENT("negative entropy/overflow\n");
646+
pr_warn("random: negative entropy/overflow: pool %s count %d\n",
647+
r->name, entropy_count);
648+
WARN_ON(1);
659649
entropy_count = 0;
660650
} else if (entropy_count > pool_size)
661651
entropy_count = pool_size;
@@ -832,10 +822,10 @@ void add_input_randomness(unsigned int type, unsigned int code,
832822
if (value == last_value)
833823
return;
834824

835-
DEBUG_ENT("input event\n");
836825
last_value = value;
837826
add_timer_randomness(&input_timer_state,
838827
(type << 4) ^ code ^ (code >> 4) ^ value);
828+
trace_add_input_randomness(ENTROPY_BITS(&input_pool));
839829
}
840830
EXPORT_SYMBOL_GPL(add_input_randomness);
841831

@@ -890,10 +880,8 @@ void add_disk_randomness(struct gendisk *disk)
890880
if (!disk || !disk->random)
891881
return;
892882
/* first major is 1, so we get >= 0x200 here */
893-
DEBUG_ENT("disk event %d:%d\n",
894-
MAJOR(disk_devt(disk)), MINOR(disk_devt(disk)));
895-
896883
add_timer_randomness(disk->random, 0x100 + disk_devt(disk));
884+
trace_add_disk_randomness(disk_devt(disk), ENTROPY_BITS(&input_pool));
897885
}
898886
#endif
899887

@@ -941,10 +929,8 @@ static void _xfer_secondary_pool(struct entropy_store *r, size_t nbytes)
941929
/* but never more than the buffer size */
942930
bytes = min_t(int, bytes, sizeof(tmp));
943931

944-
DEBUG_ENT("going to reseed %s with %d bits (%zu of %d requested)\n",
945-
r->name, bytes * 8, nbytes * 8,
946-
r->entropy_count >> ENTROPY_SHIFT);
947-
932+
trace_xfer_secondary_pool(r->name, bytes * 8, nbytes * 8,
933+
ENTROPY_BITS(r), ENTROPY_BITS(r->pull));
948934
bytes = extract_entropy(r->pull, tmp, bytes,
949935
random_read_wakeup_thresh / 8, rsvd);
950936
mix_pool_bytes(r, tmp, bytes, NULL);
@@ -992,8 +978,6 @@ static size_t account(struct entropy_store *r, size_t nbytes, int min,
992978
spin_lock_irqsave(&r->lock, flags);
993979

994980
BUG_ON(r->entropy_count > r->poolinfo->poolfracbits);
995-
DEBUG_ENT("trying to extract %zu bits from %s\n",
996-
nbytes * 8, r->name);
997981

998982
/* Can we pull enough? */
999983
retry:
@@ -1019,12 +1003,9 @@ static size_t account(struct entropy_store *r, size_t nbytes, int min,
10191003
< random_write_wakeup_thresh)
10201004
wakeup_write = 1;
10211005
}
1022-
1023-
DEBUG_ENT("debiting %zu entropy credits from %s%s\n",
1024-
ibytes * 8, r->name, r->limit ? "" : " (unlimited)");
1025-
10261006
spin_unlock_irqrestore(&r->lock, flags);
10271007

1008+
trace_debit_entropy(r->name, 8 * ibytes);
10281009
if (wakeup_write) {
10291010
wake_up_interruptible(&random_write_wait);
10301011
kill_fasync(&fasync, SIGIO, POLL_OUT);
@@ -1303,32 +1284,27 @@ random_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)
13031284
if (n > SEC_XFER_SIZE)
13041285
n = SEC_XFER_SIZE;
13051286

1306-
DEBUG_ENT("reading %zu bits\n", n*8);
1307-
13081287
n = extract_entropy_user(&blocking_pool, buf, n);
13091288

13101289
if (n < 0) {
13111290
retval = n;
13121291
break;
13131292
}
13141293

1315-
DEBUG_ENT("read got %zd bits (%zd still needed)\n",
1316-
n*8, (nbytes-n)*8);
1294+
trace_random_read(n*8, (nbytes-n)*8,
1295+
ENTROPY_BITS(&blocking_pool),
1296+
ENTROPY_BITS(&input_pool));
13171297

13181298
if (n == 0) {
13191299
if (file->f_flags & O_NONBLOCK) {
13201300
retval = -EAGAIN;
13211301
break;
13221302
}
13231303

1324-
DEBUG_ENT("sleeping?\n");
1325-
13261304
wait_event_interruptible(random_read_wait,
13271305
ENTROPY_BITS(&input_pool) >=
13281306
random_read_wakeup_thresh);
13291307

1330-
DEBUG_ENT("awake\n");
1331-
13321308
if (signal_pending(current)) {
13331309
retval = -ERESTARTSYS;
13341310
break;
@@ -1350,7 +1326,11 @@ random_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)
13501326
static ssize_t
13511327
urandom_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)
13521328
{
1353-
return extract_entropy_user(&nonblocking_pool, buf, nbytes);
1329+
int ret = extract_entropy_user(&nonblocking_pool, buf, nbytes);
1330+
1331+
trace_urandom_read(8 * nbytes, ENTROPY_BITS(&nonblocking_pool),
1332+
ENTROPY_BITS(&input_pool));
1333+
return ret;
13541334
}
13551335

13561336
static unsigned int

‎include/trace/events/random.h‎

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,89 @@ TRACE_EVENT(push_to_pool,
109109
__entry->input_bits)
110110
);
111111

112+
TRACE_EVENT(debit_entropy,
113+
TP_PROTO(const char *pool_name, int debit_bits),
114+
115+
TP_ARGS(pool_name, debit_bits),
116+
117+
TP_STRUCT__entry(
118+
__field( const char *, pool_name )
119+
__field( int, debit_bits )
120+
),
121+
122+
TP_fast_assign(
123+
__entry->pool_name = pool_name;
124+
__entry->debit_bits = debit_bits;
125+
),
126+
127+
TP_printk("%s: debit_bits %d", __entry->pool_name,
128+
__entry->debit_bits)
129+
);
130+
131+
TRACE_EVENT(add_input_randomness,
132+
TP_PROTO(int input_bits),
133+
134+
TP_ARGS(input_bits),
135+
136+
TP_STRUCT__entry(
137+
__field( int, input_bits )
138+
),
139+
140+
TP_fast_assign(
141+
__entry->input_bits = input_bits;
142+
),
143+
144+
TP_printk("input_pool_bits %d", __entry->input_bits)
145+
);
146+
147+
TRACE_EVENT(add_disk_randomness,
148+
TP_PROTO(dev_t dev, int input_bits),
149+
150+
TP_ARGS(dev, input_bits),
151+
152+
TP_STRUCT__entry(
153+
__field( dev_t, dev )
154+
__field( int, input_bits )
155+
),
156+
157+
TP_fast_assign(
158+
__entry->dev = dev;
159+
__entry->input_bits = input_bits;
160+
),
161+
162+
TP_printk("dev %d,%d input_pool_bits %d", MAJOR(__entry->dev),
163+
MINOR(__entry->dev), __entry->input_bits)
164+
);
165+
166+
TRACE_EVENT(xfer_secondary_pool,
167+
TP_PROTO(const char *pool_name, int xfer_bits, int request_bits,
168+
int pool_entropy, int input_entropy),
169+
170+
TP_ARGS(pool_name, xfer_bits, request_bits, pool_entropy,
171+
input_entropy),
172+
173+
TP_STRUCT__entry(
174+
__field( const char *, pool_name )
175+
__field( int, xfer_bits )
176+
__field( int, request_bits )
177+
__field( int, pool_entropy )
178+
__field( int, input_entropy )
179+
),
180+
181+
TP_fast_assign(
182+
__entry->pool_name = pool_name;
183+
__entry->xfer_bits = xfer_bits;
184+
__entry->request_bits = request_bits;
185+
__entry->pool_entropy = pool_entropy;
186+
__entry->input_entropy = input_entropy;
187+
),
188+
189+
TP_printk("pool %s xfer_bits %d request_bits %d pool_entropy %d "
190+
"input_entropy %d", __entry->pool_name, __entry->xfer_bits,
191+
__entry->request_bits, __entry->pool_entropy,
192+
__entry->input_entropy)
193+
);
194+
112195
DECLARE_EVENT_CLASS(random__get_random_bytes,
113196
TP_PROTO(int nbytes, unsigned long IP),
114197

@@ -179,7 +262,52 @@ DEFINE_EVENT(random__extract_entropy, extract_entropy_user,
179262
TP_ARGS(pool_name, nbytes, entropy_count, IP)
180263
);
181264

265+
TRACE_EVENT(random_read,
266+
TP_PROTO(int got_bits, int need_bits, int pool_left, int input_left),
182267

268+
TP_ARGS(got_bits, need_bits, pool_left, input_left),
269+
270+
TP_STRUCT__entry(
271+
__field( int, got_bits )
272+
__field( int, need_bits )
273+
__field( int, pool_left )
274+
__field( int, input_left )
275+
),
276+
277+
TP_fast_assign(
278+
__entry->got_bits = got_bits;
279+
__entry->need_bits = need_bits;
280+
__entry->pool_left = pool_left;
281+
__entry->input_left = input_left;
282+
),
283+
284+
TP_printk("got_bits %d still_needed_bits %d "
285+
"blocking_pool_entropy_left %d input_entropy_left %d",
286+
__entry->got_bits, __entry->got_bits, __entry->pool_left,
287+
__entry->input_left)
288+
);
289+
290+
TRACE_EVENT(urandom_read,
291+
TP_PROTO(int got_bits, int pool_left, int input_left),
292+
293+
TP_ARGS(got_bits, pool_left, input_left),
294+
295+
TP_STRUCT__entry(
296+
__field( int, got_bits )
297+
__field( int, pool_left )
298+
__field( int, input_left )
299+
),
300+
301+
TP_fast_assign(
302+
__entry->got_bits = got_bits;
303+
__entry->pool_left = pool_left;
304+
__entry->input_left = input_left;
305+
),
306+
307+
TP_printk("got_bits %d nonblocking_pool_entropy_left %d "
308+
"input_entropy_left %d", __entry->got_bits,
309+
__entry->pool_left, __entry->input_left)
310+
);
183311

184312
#endif /* _TRACE_RANDOM_H */
185313

0 commit comments

Comments
 (0)