printk 인쇄 주파수 제한 함수 printkratelimit

5038 단어 linux 커널
Linux 커널 코드에서 printk 인쇄 주파수를 제한해야 할 때ratelimit 또는 printkratelimit ( 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                         
    printk_ratelimit은 기본적으로 5s에서 최대 10개의 메시지를 출력할 수 있습니다./proc/sys/kernel/printkratelimit(몇 시간)과/proc/sys/kernel/printkratelimit_burst (printk ratelimit 시간대에 허용되는 최대 메시지 수)물론 내부 코드에 printk 가 호출되지 않았다면ratelimit은 이 두 값의 제한을 받지 않는다.
커널 블록 장치 코드의 예는 다음과 같습니다.
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
", error_type, req->rq_disk ? req->rq_disk->disk_name : "?", (unsigned long long)blk_rq_pos(req)); } blk_account_io_completion(req, nr_bytes); ... }

    printk_ratelimited는 매크로이며 를 직접 호출합니다ratelimit
#define printk_ratelimited(fmt, ...)  ({        \                                                      
    static struct ratelimit_state _rs = {       \                                                      
        .interval = DEFAULT_RATELIMIT_INTERVAL, \                                                      
        .burst = DEFAULT_RATELIMIT_BURST,       \
    };                                              \                                                  
                            \
    if (!__ratelimit(&_rs))                         \                                                  
        printk(fmt, ##__VA_ARGS__);     \                                                              
})

좋은 웹페이지 즐겨찾기