限制printk打印频率函数printk_ratelimit
在Linux内核代码里当需要限制printk打印频率时会用到__ratelimit或printk_ratelimit(封装了__ratelimit)。
/*
* printk rate limiting, lifted from the networking subsystem.
*
* This enforces a rate limit: not more than 10 kernel messages
* every 5s to make a denial-of-service attack impossible.
*/
DEFINE_RATELIMIT_STATE(printk_ratelimit_state, 5 * HZ, 10);
int printk_ratelimit(void)
{
return __ratelimit(&printk_ratelimit_state);
}
EXPORT_SYMBOL(printk_ratelimit);
/var/log/messages里一个可能的输出如下:
kernel: end_request: I/O error, dev sdr, sector 2048
kernel: end_request: I/O error, dev sdr, sector 2048
kernel: end_request: I/O error, dev sdr, sector 2104
kernel: end_request: I/O error, dev sdr, sector 2048
kernel: end_request: I/O error, dev sdr, sector 2048
kernel: __ratelimit: 125 callbacks suppressed
kernel: sd 21:0:0:0: [sdr] Unhandled error code
kernel: sd 21:0:0:0: [sdr] Result: hostbyte=DID_BAD_TARGET driverbyte=DRIVER_OK
kernel: sd 21:0:0:0: [sdr] CDB: Read(10): 28 00 00 00 00 00 00 00 20 00
kernel: __ratelimit: 125 callbacks suppressed
kernel: __ratelimit: 128 callbacks suppressed
kernel: Buffer I/O error on device sdr, logical block 0
kernel: Buffer I/O error on device sdr, logical block 1
kernel: Buffer I/O error on device sdr, logical block 2
kernel: Buffer I/O error on device sdr, logical block 3
内核块设备代码中的一个例子如下:
bool blk_update_request(struct request *req, int error, unsigned int nr_bytes)
{
int total_bytes, bio_nbytes, next_idx = 0;
struct bio *bio;
if (!req->bio)
return false;
trace_block_rq_complete(req->q, req);
/*
* For fs requests, rq is just carrier of independent bio's
* and each partial completion should be handled separately.
* Reset per-request error on each partial completion.
*
* TODO: tj: This is too subtle. It would be better to let
* low level drivers do what they see fit.
*/
if (req->cmd_type == REQ_TYPE_FS)
req->errors = 0;
if (error && req->cmd_type == REQ_TYPE_FS &&
!(req->cmd_flags & REQ_QUIET)) {
char *error_type;
switch (error) {
case -ENOLINK:
error_type = "recoverable transport";
break;
case -EREMOTEIO:
error_type = "critical target";
break;
case -EBADE:
error_type = "critical nexus";
break;
case -EIO:
default:
error_type = "I/O";
break;
}
printk_ratelimited(KERN_ERR "end_request: %s error, dev %s, "
"sector %llu\n", error_type, req->rq_disk ?
req->rq_disk->disk_name : "?",
(unsigned long long)blk_rq_pos(req));
}
blk_account_io_completion(req, nr_bytes);
...
}
#define printk_ratelimited(fmt, ...) ({ \
static struct ratelimit_state _rs = { \
.interval = DEFAULT_RATELIMIT_INTERVAL, \
.burst = DEFAULT_RATELIMIT_BURST, \
}; \
\
if (!__ratelimit(&_rs)) \
printk(fmt, ##__VA_ARGS__); \
})
更多推荐
所有评论(0)