Skip to content

Commit df03f83

Browse files
thepacketgeekdavem330
authored andcommitted
net: netconsole: cache userdata formatted string in netconsole_target
Store a formatted string for userdata that will be appended to netconsole messages. The string has a capacity of 4KB, as calculated by the userdatum entry length of 256 bytes and a max of 16 userdata entries. Update the stored netconsole_target->userdata_complete string with the new formatted userdata values when a userdatum is created, edited, or removed. Each userdata entry contains a trailing newline, which will be formatted as such in netconsole messages:: 6.7.0-rc8-virtme,12,500,1646292204,-;test release=foo something=bar 6.7.0-rc8-virtme,12,500,1646292204,-;another test release=foo something=bar Enforcement of MAX_USERDATA_ITEMS is done in userdatum_make_item; update_userdata will not check for this case but will skip any userdata children over the limit of MAX_USERDATA_ITEMs. If a userdata entry/dir is created but no value is provided, that entry will be skipped. This is in part because update_userdata() can't be called in userdatum_make_item() since the item will not have been added to the userdata config_group children yet. To preserve the experience of adding an empty userdata that doesn't show up in the netconsole messages, purposefully skip empty userdata items even when update_userdata() can be called. Co-developed-by: Breno Leitao <[email protected]> Signed-off-by: Breno Leitao <[email protected]> Signed-off-by: Matthew Wood <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 8a6d5fe commit df03f83

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

drivers/net/netconsole.c

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ static struct console netconsole_ext;
8585
* @list: Links this target into the target_list.
8686
* @group: Links us into the configfs subsystem hierarchy.
8787
* @userdata_group: Links to the userdata configfs hierarchy
88+
* @userdata_complete: Cached, formatted string of append
89+
* @userdata_length: String length of userdata_complete
8890
* @enabled: On / off knob to enable / disable target.
8991
* Visible from userspace (read-write).
9092
* We maintain a strict 1:1 correspondence between this and
@@ -109,6 +111,8 @@ struct netconsole_target {
109111
#ifdef CONFIG_NETCONSOLE_DYNAMIC
110112
struct config_group group;
111113
struct config_group userdata_group;
114+
char userdata_complete[MAX_USERDATA_ENTRY_LENGTH * MAX_USERDATA_ITEMS];
115+
size_t userdata_length;
112116
#endif
113117
bool enabled;
114118
bool extended;
@@ -638,10 +642,48 @@ static ssize_t userdatum_value_show(struct config_item *item, char *buf)
638642
return sysfs_emit(buf, "%s\n", &(to_userdatum(item)->value[0]));
639643
}
640644

645+
static void update_userdata(struct netconsole_target *nt)
646+
{
647+
int complete_idx = 0, child_count = 0;
648+
struct list_head *entry;
649+
650+
/* Clear the current string in case the last userdatum was deleted */
651+
nt->userdata_length = 0;
652+
nt->userdata_complete[0] = 0;
653+
654+
list_for_each(entry, &nt->userdata_group.cg_children) {
655+
struct userdatum *udm_item;
656+
struct config_item *item;
657+
658+
if (child_count >= MAX_USERDATA_ITEMS)
659+
break;
660+
child_count++;
661+
662+
item = container_of(entry, struct config_item, ci_entry);
663+
udm_item = to_userdatum(item);
664+
665+
/* Skip userdata with no value set */
666+
if (strnlen(udm_item->value, MAX_USERDATA_VALUE_LENGTH) == 0)
667+
continue;
668+
669+
/* This doesn't overflow userdata_complete since it will write
670+
* one entry length (1/MAX_USERDATA_ITEMS long), entry count is
671+
* checked to not exceed MAX items with child_count above
672+
*/
673+
complete_idx += scnprintf(&nt->userdata_complete[complete_idx],
674+
MAX_USERDATA_ENTRY_LENGTH, "%s=%s\n",
675+
item->ci_name, udm_item->value);
676+
}
677+
nt->userdata_length = strnlen(nt->userdata_complete,
678+
sizeof(nt->userdata_complete));
679+
}
680+
641681
static ssize_t userdatum_value_store(struct config_item *item, const char *buf,
642682
size_t count)
643683
{
644684
struct userdatum *udm = to_userdatum(item);
685+
struct netconsole_target *nt;
686+
struct userdata *ud;
645687
int ret;
646688

647689
if (count > MAX_USERDATA_VALUE_LENGTH)
@@ -654,6 +696,10 @@ static ssize_t userdatum_value_store(struct config_item *item, const char *buf,
654696
goto out_unlock;
655697
trim_newline(udm->value, sizeof(udm->value));
656698

699+
ud = to_userdata(item->ci_parent);
700+
nt = userdata_to_target(ud);
701+
update_userdata(nt);
702+
657703
mutex_unlock(&dynamic_netconsole_mutex);
658704
return count;
659705
out_unlock:
@@ -708,12 +754,27 @@ static struct config_item *userdatum_make_item(struct config_group *group,
708754
return &udm->item;
709755
}
710756

757+
static void userdatum_drop(struct config_group *group, struct config_item *item)
758+
{
759+
struct netconsole_target *nt;
760+
struct userdata *ud;
761+
762+
ud = to_userdata(&group->cg_item);
763+
nt = userdata_to_target(ud);
764+
765+
mutex_lock(&dynamic_netconsole_mutex);
766+
update_userdata(nt);
767+
config_item_put(item);
768+
mutex_unlock(&dynamic_netconsole_mutex);
769+
}
770+
711771
static struct configfs_attribute *userdata_attrs[] = {
712772
NULL,
713773
};
714774

715775
static struct configfs_group_operations userdata_ops = {
716776
.make_item = userdatum_make_item,
777+
.drop_item = userdatum_drop,
717778
};
718779

719780
static struct config_item_type userdata_type = {

0 commit comments

Comments
 (0)