Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3)  * ratelimit.c - Do something with rate limit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5)  * Isolated from kernel/printk.c by Dave Young <hidave.darkstar@gmail.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7)  * 2008-05-01 rewrite the function and use a ratelimit_state data struct as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8)  * parameter. Now every user can use their own standalone ratelimit_state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/ratelimit.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/jiffies.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)  * __ratelimit - rate limiting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)  * @rs: ratelimit_state data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)  * @func: name of calling function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)  * This enforces a rate limit: not more than @rs->burst callbacks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)  * in every @rs->interval
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)  * RETURNS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)  * 0 means callbacks will be suppressed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)  * 1 means go ahead and do it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) int ___ratelimit(struct ratelimit_state *rs, const char *func)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) 	if (!rs->interval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) 	 * If we contend on this state's lock then almost
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) 	 * by definition we are too busy to print a message,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) 	 * in addition to the one that will be printed by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) 	 * the entity that is holding the lock already:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) 	if (!raw_spin_trylock_irqsave(&rs->lock, flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) 	if (!rs->begin)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) 		rs->begin = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) 	if (time_is_before_jiffies(rs->begin + rs->interval)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) 		if (rs->missed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) 			if (!(rs->flags & RATELIMIT_MSG_ON_RELEASE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) 				printk_deferred(KERN_WARNING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) 						"%s: %d callbacks suppressed\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) 						func, rs->missed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) 				rs->missed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) 		rs->begin   = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) 		rs->printed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) 	if (rs->burst && rs->burst > rs->printed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) 		rs->printed++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) 		ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) 		rs->missed++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) 		ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) 	raw_spin_unlock_irqrestore(&rs->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) EXPORT_SYMBOL(___ratelimit);