Skip to content
Permalink
Browse files
dyndbg: add print-once and print-ratelimited features. RFC.
Expand ddebug.flags to 11 bits, and define new flags to support
pr_debug_once() and pr_debug_ratelimited() semantics:

  echo module main +o > control		# init/main runs once anyway
  echo module foo +r > control		# turn on ratelimiting
  echo module foo +g > control		# turn on group flag

Test these conditions in new is_onced_or_ratelimited(),
and call it from __dynamic_pr_debug and others.

print-once: can be done with just 2 bits in flags;

.o _DPRINTK_FLAGS_ONCE     enables state test and set
.P _DPRINTK_FLAGS_PRINTED  state bit

Just adding the flags lets the existing code operate them.
We will need new code to enforce constraints on flag combos;
'+ro' is nonsense, but this can wait, or can take a new meaning.

is_onced_or_ratelimited() should be correct for +o,
and should be testable now. tbd.

rate-limiting:
.r _DPRINTK_FLAGS_RATELIMITED	- track & limit prdbgs callrate

Wewait until a prdebug is called, and if RATELIMITED is set, THEN
lookup a RateLimitState (RL) for it.  If found, bump its state and
return true/false, otherwise create & initialize one and return false.

group-flag: RFC
.g _DPRINTK_FLAGS_GROUPED

Currently, the hash-key is just the prdebug descriptor, so is unique
per prdebug.  With the 'g' flag, we could use a different key, for
example desc->site.function, to get a shared ratelimit for whole
functions.

This gets subtly different behavior at the ratelimit transition, but
it is predictable for a given function (except perhaps recursive, but
thats not done anyway).

Note also that any function can have a single group of prdebugs, plus
any number of prdbgs without 'g', either with or without 'r'.  So
grouping should be flexible enough to use advantageously.

Notes:
- runtime mechanisms can be hidden (by not handling '+org' > control)
- exposed only thru api flags (not done, probably just another param
  to DEFINE_DYANMIC_DEBUG_METADATA)

TLDR: older key thinking

That lookup is basically a hash, with 2 part key:
. &builtin-vector-base OR &module
  or the hash(s) could hang off the header struct
. ._back OR ._map
  chosen by _DPRINTK_FLAGS_GROUPED
  choice dictates per-site OR sharing across function
  • Loading branch information
jimc committed Jun 15, 2021
1 parent 3b8c3ae commit d29f2fbe3a2ecb1391e1ed5271f3f0728e963759
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 4 deletions.
@@ -64,18 +64,22 @@ struct _ddebug {
#define _DPRINTK_FLAGS_INCL_FUNCNAME (1<<2)
#define _DPRINTK_FLAGS_INCL_LINENO (1<<3)
#define _DPRINTK_FLAGS_INCL_TID (1<<4)
#define _DPRINTK_FLAGS_DELETE_SITE (1<<7) /* drop site info to save ram */

#define _DPRINTK_FLAGS_INCL_ANY \
(_DPRINTK_FLAGS_INCL_MODNAME | _DPRINTK_FLAGS_INCL_FUNCNAME |\
_DPRINTK_FLAGS_INCL_LINENO | _DPRINTK_FLAGS_INCL_TID)

#define _DPRINTK_FLAGS_ONCE (1<<5) /* print once flag */
#define _DPRINTK_FLAGS_PRINTED (1<<6) /* print once state */
#define _DPRINTK_FLAGS_RATELIMITED (1<<7)
#define _DPRINTK_FLAGS_GROUPED (1<<8) /* manipulate as a group */
#define _DPRINTK_FLAGS_DELETE_SITE (1<<9) /* drop site info to save ram */

#if defined DEBUG
#define _DPRINTK_FLAGS_DEFAULT _DPRINTK_FLAGS_PRINT
#else
#define _DPRINTK_FLAGS_DEFAULT 0
#endif
unsigned int flags:8;
unsigned int flags:11;

#ifdef CONFIG_JUMP_LABEL
union {
@@ -86,13 +86,17 @@ static inline const char *trim_prefix(const char *path)
return path + skip;
}

static struct { unsigned flag:8; char opt_char; } opt_array[] = {
static struct { unsigned flag:11; char opt_char; } opt_array[] = {
{ _DPRINTK_FLAGS_PRINT, 'p' },
{ _DPRINTK_FLAGS_INCL_MODNAME, 'm' },
{ _DPRINTK_FLAGS_INCL_FUNCNAME, 'f' },
{ _DPRINTK_FLAGS_INCL_LINENO, 'l' },
{ _DPRINTK_FLAGS_INCL_TID, 't' },
{ _DPRINTK_FLAGS_NONE, '_' },
{ _DPRINTK_FLAGS_ONCE, 'o' },
{ _DPRINTK_FLAGS_PRINTED, 'P' },
{ _DPRINTK_FLAGS_RATELIMITED, 'r' },
{ _DPRINTK_FLAGS_GROUPED, 'g' },
{ _DPRINTK_FLAGS_DELETE_SITE, 'D' },
};

@@ -731,6 +735,45 @@ static inline char *dynamic_emit_prefix(struct _ddebug *desc, char *buf)
return buf;
}

struct dd_ratelimit {
struct hlist_node hnode;
struct ratelimit_state rs;
u64 key;
};

/* test print-once or ratelimited conditions */
#define is_rated(desc) unlikely(desc->flags & _DPRINTK_FLAGS_RATELIMITED)
#define is_once(desc) unlikely(desc->flags & _DPRINTK_FLAGS_ONCE)
#define is_onced(desc) \
unlikely((desc->flags & _DPRINTK_FLAGS_ONCE) \
&& (desc->flags & _DPRINTK_FLAGS_PRINTED))

static struct dd_ratelimit *dd_rl_fetch(struct _ddebug *desc);

static inline bool is_onced_or_limited(struct _ddebug *descriptor)
{
if (unlikely(descriptor->flags & _DPRINTK_FLAGS_ONCE &&
descriptor->flags & _DPRINTK_FLAGS_RATELIMITED))
pr_info(" ONCE & RATELIMITED together is nonsense\n");

if (is_once(descriptor)) {
if (is_onced(descriptor)) {
v4pr_info("already printed once\n");
return true;
}
descriptor->flags |= _DPRINTK_FLAGS_PRINTED;
return false;

} else if (is_rated(descriptor)) {
/* test rate-limits */
v4pr_info("todo: fetch RLstate{%s}\n",
descriptor->flags & _DPRINTK_FLAGS_GROUPED
? "grouped" : "solo");
return __ratelimit(&dd_rl_fetch(descriptor)->rs);
}
return false;
}

void __dynamic_pr_debug(struct _ddebug *descriptor, const char *fmt, ...)
{
va_list args;
@@ -740,6 +783,9 @@ void __dynamic_pr_debug(struct _ddebug *descriptor, const char *fmt, ...)
BUG_ON(!descriptor);
BUG_ON(!fmt);

if (is_onced_or_limited(descriptor))
return;

va_start(args, fmt);

vaf.fmt = fmt;
@@ -760,6 +806,9 @@ void __dynamic_dev_dbg(struct _ddebug *descriptor,
BUG_ON(!descriptor);
BUG_ON(!fmt);

if (is_onced_or_limited(descriptor))
return;

va_start(args, fmt);

vaf.fmt = fmt;
@@ -791,6 +840,9 @@ void __dynamic_netdev_dbg(struct _ddebug *descriptor,
BUG_ON(!descriptor);
BUG_ON(!fmt);

if (is_onced_or_limited(descriptor))
return;

va_start(args, fmt);

vaf.fmt = fmt;
@@ -827,6 +879,9 @@ void __dynamic_ibdev_dbg(struct _ddebug *descriptor,
struct va_format vaf;
va_list args;

if (is_onced_or_limited(descriptor))
return;

va_start(args, fmt);

vaf.fmt = fmt;
@@ -1350,3 +1405,58 @@ early_initcall(dynamic_debug_init);

/* Debugfs setup must be done later */
fs_initcall(dynamic_debug_init_control);

/*
* Rate-Limited debug is expected to rarely be needed, so it is
* provisioned on-demand when an enabled and rate-limit-flagged
* pr_debug is called, by dd_rl_fetch(). For now, key is just
* descriptor, so is unique per site.
* Plan: for 'gr' flagged callsites, choose a key that is same across
* all prdebugs in a function, to apply a single rate-limit to the
* whole function. This should give nearly identical behavior at much
* lower memory cost.
*/
static DEFINE_HASHTABLE(dd_rl_map, 4);

static struct dd_ratelimit *dd_rl_find(u64 key)
{
struct dd_ratelimit *limiter;

hash_for_each_possible(dd_rl_map, limiter, hnode, key) {
if (limiter->key == key)
return limiter;
}
return NULL;
}

/* Must be called with dd_rl_lock locked. */
static struct dd_ratelimit *dd_rl_add(u64 key)
{
struct dd_ratelimit *limiter;

limiter = dd_rl_find(key);
if (limiter)
return limiter;
limiter = kmalloc(sizeof(*limiter), GFP_ATOMIC);
if (!limiter)
return ERR_PTR(-ENOMEM);

limiter->key = key;
ratelimit_default_init(&limiter->rs);
hash_add(dd_rl_map, &limiter->hnode, key);
return limiter;
}

/*
* called when enabled callsite has _DPRINTK_FLAGS_RATELIMITED flag
* set (echo +pr >control), it hashes on &table-header+index
*/
static struct dd_ratelimit * dd_rl_fetch(struct _ddebug *desc)
{
u64 key = (u64)desc;
struct dd_ratelimit *dd_rl = dd_rl_find(key);
if (dd_rl)
return dd_rl;
return dd_rl_add(key);
}

0 comments on commit d29f2fb

Please sign in to comment.