netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jakub Kicinski <kuba@kernel.org>
To: Tony Nguyen <anthony.l.nguyen@intel.com>
Cc: davem@davemloft.net, pabeni@redhat.com, edumazet@google.com,
	netdev@vger.kernel.org,
	Paul M Stillwell Jr <paul.m.stillwell.jr@intel.com>,
	jacob.e.keller@intel.com, vaishnavi.tipireddy@intel.com,
	horms@kernel.org, leon@kernel.org,
	Pucha Himasekhar Reddy <himasekharx.reddy.pucha@intel.com>
Subject: Re: [PATCH net-next v4 2/5] ice: configure FW logging
Date: Fri, 6 Oct 2023 17:02:06 -0700	[thread overview]
Message-ID: <20231006170206.297687e2@kernel.org> (raw)
In-Reply-To: <20231005170110.3221306-3-anthony.l.nguyen@intel.com>

On Thu,  5 Oct 2023 10:01:07 -0700 Tony Nguyen wrote:
> +static ssize_t ice_debugfs_parse_cmd_line(const char __user *src, size_t len,
> +					  char ***argv, int *argc)
> +{
> +	char *cmd_buf, *cmd_buf_tmp;
> +
> +	cmd_buf = memdup_user(src, len + 1);

memdup() with len + 1 is quite suspicious, the buffer has length 
of len you shouldn't copy more than that

> +	if (IS_ERR(cmd_buf))
> +		return PTR_ERR(cmd_buf);
> +	cmd_buf[len] = '\0';
> +
> +	/* the cmd_buf has a newline at the end of the command so
> +	 * remove it
> +	 */
> +	cmd_buf_tmp = strchr(cmd_buf, '\n');
> +	if (cmd_buf_tmp) {
> +		*cmd_buf_tmp = '\0';
> +		len = (size_t)cmd_buf_tmp - (size_t)cmd_buf + 1;
> +	}
> +
> +	*argv = argv_split(GFP_KERNEL, cmd_buf, argc);
> +	if (!*argv)
> +		return -ENOMEM;
> +
> +	kfree(cmd_buf);
> +	return 0;

> +static ssize_t
> +ice_debugfs_module_write(struct file *filp, const char __user *buf,
> +			 size_t count, loff_t *ppos)
> +{
> +	struct ice_pf *pf = filp->private_data;
> +	struct device *dev = ice_pf_to_dev(pf);
> +	ssize_t ret;
> +	char **argv;
> +	int argc;
> +
> +	/* don't allow commands if the FW doesn't support it */
> +	if (!ice_fwlog_supported(&pf->hw))
> +		return -EOPNOTSUPP;
> +
> +	/* don't allow partial writes */
> +	if (*ppos != 0)
> +		return 0;
> +
> +	ret = ice_debugfs_parse_cmd_line(buf, count, &argv, &argc);
> +	if (ret)
> +		goto err_copy_from_user;
> +
> +	if (argc == 2) {
> +		int module, log_level;
> +
> +		module = sysfs_match_string(ice_fwlog_module_string, argv[0]);
> +		if (module < 0) {
> +			dev_info(dev, "unknown module '%s'\n", argv[0]);
> +			ret = -EINVAL;
> +			goto module_write_error;
> +		}
> +
> +		log_level = sysfs_match_string(ice_fwlog_level_string, argv[1]);
> +		if (log_level < 0) {
> +			dev_info(dev, "unknown log level '%s'\n", argv[1]);
> +			ret = -EINVAL;
> +			goto module_write_error;
> +		}

The parsing looks pretty over-engineered.

You can group the entries into structs like this:

struct something {
	const char *name;
	size_t sz;
	enum whatever value;
};
#define FILL_IN_STH(thing) \
	{ .name = thing, sz = sizeof(thing) - 1, value = ICE_..##thing,}

struct something[] = {
  FILL_IN_STH(ALL),
  FILL_IN_STH(MNG),
  ...
};

but with nicer names

Then just:

for entry in array(..) {
  if !strncmp(input, entry->name, entry->sz) {
    str += entry->sz + 1
    found = entry;
    break
  }
}

> +static ssize_t ice_debugfs_resolution_read(struct file *filp,
> +					   char __user *buffer, size_t count,
> +					   loff_t *ppos)
> +{
> +	struct ice_pf *pf = filp->private_data;
> +	struct ice_hw *hw = &pf->hw;
> +	char buff[32] = {};
> +	int status;
> +
> +	/* don't allow commands if the FW doesn't support it */
> +	if (!ice_fwlog_supported(&pf->hw))
> +		return -EOPNOTSUPP;
> +
> +	snprintf(buff, sizeof(buff), "%d\n",
> +		 hw->fwlog_cfg.log_resolution);
> +
> +	status = simple_read_from_buffer(buffer, count, ppos, buff,
> +					 strlen(buff));
> +
> +	return status;
> +}

> +static ssize_t
> +ice_debugfs_resolution_write(struct file *filp, const char __user *buf,
> +			     size_t count, loff_t *ppos)
> +{
> +	struct ice_pf *pf = filp->private_data;
> +	struct device *dev = ice_pf_to_dev(pf);
> +	struct ice_hw *hw = &pf->hw;
> +	ssize_t ret;
> +	char **argv;
> +	int argc;
> +
> +	/* don't allow commands if the FW doesn't support it */
> +	if (!ice_fwlog_supported(hw))
> +		return -EOPNOTSUPP;
> +
> +	/* don't allow partial writes */
> +	if (*ppos != 0)
> +		return 0;
> +
> +	ret = ice_debugfs_parse_cmd_line(buf, count, &argv, &argc);
> +	if (ret)
> +		goto err_copy_from_user;

And for the simple params can you try to reuse existing debugfs
helpers? They can already read and write scalars, all you need
is to inject yourself on the write path to update the config
in the device.

  reply	other threads:[~2023-10-07  0:02 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-05 17:01 [PATCH net-next v4 0/5][pull request] add v2 FW logging for ice driver Tony Nguyen
2023-10-05 17:01 ` [PATCH net-next v4 1/5] ice: remove FW logging code Tony Nguyen
2023-10-05 17:01 ` [PATCH net-next v4 2/5] ice: configure FW logging Tony Nguyen
2023-10-07  0:02   ` Jakub Kicinski [this message]
2023-10-10 23:26     ` Paul M Stillwell Jr
2023-10-11  2:01       ` Jakub Kicinski
2023-10-12  0:40         ` Paul M Stillwell Jr
2023-10-12 23:40           ` Jakub Kicinski
2023-10-17 20:38             ` Paul M Stillwell Jr
2023-10-05 17:01 ` [PATCH net-next v4 3/5] ice: enable " Tony Nguyen
2023-10-05 17:01 ` [PATCH net-next v4 4/5] ice: add ability to read FW log data and configure the number of log buffers Tony Nguyen
2023-10-05 17:01 ` [PATCH net-next v4 5/5] ice: add documentation for FW logging Tony Nguyen
2023-10-06 23:46   ` Jakub Kicinski
2023-10-10 23:00     ` Paul M Stillwell Jr
2023-10-11  1:18       ` Jakub Kicinski
2023-10-12  0:43         ` Paul M Stillwell Jr

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231006170206.297687e2@kernel.org \
    --to=kuba@kernel.org \
    --cc=anthony.l.nguyen@intel.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=himasekharx.reddy.pucha@intel.com \
    --cc=horms@kernel.org \
    --cc=jacob.e.keller@intel.com \
    --cc=leon@kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=paul.m.stillwell.jr@intel.com \
    --cc=vaishnavi.tipireddy@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).