Re: [PATCH 3/4] of: Custom printk format specifier for device node

From: Joe Perches
Date: Thu Jun 15 2017 - 17:50:29 EST


On Thu, 2017-06-15 at 16:26 -0500, Rob Herring wrote:
> On Wed, Jun 14, 2017 at 01:56:48PM -0700, Joe Perches wrote:
> > On Wed, 2017-06-14 at 15:30 -0500, Rob Herring wrote:
> > > From: Pantelis Antoniou <pantelis.antoniou@xxxxxxxxxxxx>
[]
> > > diff --git a/lib/vsprintf.c b/lib/vsprintf.c
> > > []
> > > @@ -1470,6 +1471,123 @@ char *flags_string(char *buf, char *end, void *flags_ptr, const char *fmt)
> > > return format_flags(buf, end, flags, names);
> > > }
> > >
> > > +static noinline_for_stack
> > > +char *device_node_gen_full_name(const struct device_node *np, char *buf, char *end)
> > > +{
> > > + int len, ret;
> > > +
> > > + if (!np || !np->parent)
> > > + return buf;
> > > +
> > > + buf = device_node_gen_full_name(np->parent, buf, end);
> >
> > This is recursive. How many levels of parents could there be?
> > Perhaps there should be a recursion limit.
>
> Okay, unlike unflattening code, we can easily calculate the depth and
> then allocate an array on the stack. So this is what I've ended up
> with:
>
> static int device_node_calc_depth(const struct device_node *np)
> {
> int d;
>
> for (d = 0; np; d++)
> np = np->parent;
>
> return d;
> }
>
> static noinline_for_stack
> char *device_node_gen_full_name(const struct device_node *np, char *buf, char *end)
> {
> int i;
> int depth = device_node_calc_depth(np);
> const struct device_node *nodes[depth];
> const struct printf_spec strspec = {
> .field_width = -1,
> .precision = -1,
> };

static const struct printf_spec strspec = { etc...
and please move strspec above *nodes

>
> if (!depth)
> return buf;
> /* special case for root node */
> if (depth == 1)
> return string(buf, end, "/", strspec);
>
> depth--;
> for (i = depth - 1; i >= 0; i--) {
> nodes[i] = np;
> np = np->parent;
> }
> for (i = 0; i < depth; i++) {
> buf = string(buf, end, "/", strspec);
> buf = string(buf, end, kbasename(nodes[i]->full_name), strspec);
> }
> return buf;
> }