18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * ratelimit.c - Do something with rate limit. 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Isolated from kernel/printk.c by Dave Young <hidave.darkstar@gmail.com> 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * 2008-05-01 rewrite the function and use a ratelimit_state data struct as 88c2ecf20Sopenharmony_ci * parameter. Now every user can use their own standalone ratelimit_state. 98c2ecf20Sopenharmony_ci */ 108c2ecf20Sopenharmony_ci 118c2ecf20Sopenharmony_ci#include <linux/ratelimit.h> 128c2ecf20Sopenharmony_ci#include <linux/jiffies.h> 138c2ecf20Sopenharmony_ci#include <linux/export.h> 148c2ecf20Sopenharmony_ci 158c2ecf20Sopenharmony_ci/* 168c2ecf20Sopenharmony_ci * __ratelimit - rate limiting 178c2ecf20Sopenharmony_ci * @rs: ratelimit_state data 188c2ecf20Sopenharmony_ci * @func: name of calling function 198c2ecf20Sopenharmony_ci * 208c2ecf20Sopenharmony_ci * This enforces a rate limit: not more than @rs->burst callbacks 218c2ecf20Sopenharmony_ci * in every @rs->interval 228c2ecf20Sopenharmony_ci * 238c2ecf20Sopenharmony_ci * RETURNS: 248c2ecf20Sopenharmony_ci * 0 means callbacks will be suppressed. 258c2ecf20Sopenharmony_ci * 1 means go ahead and do it. 268c2ecf20Sopenharmony_ci */ 278c2ecf20Sopenharmony_ciint ___ratelimit(struct ratelimit_state *rs, const char *func) 288c2ecf20Sopenharmony_ci{ 298c2ecf20Sopenharmony_ci /* Paired with WRITE_ONCE() in .proc_handler(). 308c2ecf20Sopenharmony_ci * Changing two values seperately could be inconsistent 318c2ecf20Sopenharmony_ci * and some message could be lost. (See: net_ratelimit_state). 328c2ecf20Sopenharmony_ci */ 338c2ecf20Sopenharmony_ci int interval = READ_ONCE(rs->interval); 348c2ecf20Sopenharmony_ci int burst = READ_ONCE(rs->burst); 358c2ecf20Sopenharmony_ci unsigned long flags; 368c2ecf20Sopenharmony_ci int ret; 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_ci if (!interval) 398c2ecf20Sopenharmony_ci return 1; 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_ci /* 428c2ecf20Sopenharmony_ci * If we contend on this state's lock then almost 438c2ecf20Sopenharmony_ci * by definition we are too busy to print a message, 448c2ecf20Sopenharmony_ci * in addition to the one that will be printed by 458c2ecf20Sopenharmony_ci * the entity that is holding the lock already: 468c2ecf20Sopenharmony_ci */ 478c2ecf20Sopenharmony_ci if (!raw_spin_trylock_irqsave(&rs->lock, flags)) 488c2ecf20Sopenharmony_ci return 0; 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_ci if (!rs->begin) 518c2ecf20Sopenharmony_ci rs->begin = jiffies; 528c2ecf20Sopenharmony_ci 538c2ecf20Sopenharmony_ci if (time_is_before_jiffies(rs->begin + interval)) { 548c2ecf20Sopenharmony_ci if (rs->missed) { 558c2ecf20Sopenharmony_ci if (!(rs->flags & RATELIMIT_MSG_ON_RELEASE)) { 568c2ecf20Sopenharmony_ci printk_deferred(KERN_WARNING 578c2ecf20Sopenharmony_ci "%s: %d callbacks suppressed\n", 588c2ecf20Sopenharmony_ci func, rs->missed); 598c2ecf20Sopenharmony_ci rs->missed = 0; 608c2ecf20Sopenharmony_ci } 618c2ecf20Sopenharmony_ci } 628c2ecf20Sopenharmony_ci rs->begin = jiffies; 638c2ecf20Sopenharmony_ci rs->printed = 0; 648c2ecf20Sopenharmony_ci } 658c2ecf20Sopenharmony_ci if (burst && burst > rs->printed) { 668c2ecf20Sopenharmony_ci rs->printed++; 678c2ecf20Sopenharmony_ci ret = 1; 688c2ecf20Sopenharmony_ci } else { 698c2ecf20Sopenharmony_ci rs->missed++; 708c2ecf20Sopenharmony_ci ret = 0; 718c2ecf20Sopenharmony_ci } 728c2ecf20Sopenharmony_ci raw_spin_unlock_irqrestore(&rs->lock, flags); 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_ci return ret; 758c2ecf20Sopenharmony_ci} 768c2ecf20Sopenharmony_ciEXPORT_SYMBOL(___ratelimit); 77