netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Phil Sutter <phil@nwl.cc>
To: Jakub Kicinski <jakub.kicinski@netronome.com>
Cc: netdev@vger.kernel.org, davem@davemloft.net,
	daniel@iogearbox.net, alexei.starovoitov@gmail.com,
	jiri@resnulli.us, oss-drivers@netronome.com,
	Sabrina Dubroca <sd@queasysnail.net>
Subject: Re: [PATCH net-next v2 7/8] netdevsim: add SR-IOV functionality
Date: Fri, 1 Dec 2017 22:36:52 +0100	[thread overview]
Message-ID: <20171201213652.GT32305@orbyte.nwl.cc> (raw)
In-Reply-To: <20171201121407.4f802f18@cakuba.netronome.com>

On Fri, Dec 01, 2017 at 12:14:07PM -0800, Jakub Kicinski wrote:
> On Fri, 1 Dec 2017 14:43:06 +0100, Phil Sutter wrote:
> > On Thu, Nov 30, 2017 at 05:35:39PM -0800, Jakub Kicinski wrote:
> > [...]
> > > +static int nsim_vfs_enable(struct netdevsim *ns, unsigned int num_vfs)
> > > +{
> > > +	ns->vfconfigs = kcalloc(num_vfs, sizeof(struct nsim_vf_config),
> > > +				GFP_KERNEL);
> > > +	if (!ns->vfconfigs)
> > > +		return -ENOMEM;
> > > +	ns->num_vfs = num_vfs;
> > > +
> > > +	return 0;
> > > +}
> > > +
> > > +static void nsim_vfs_disable(struct netdevsim *ns)
> > > +{
> > > +	kfree(ns->vfconfigs);
> > > +	ns->vfconfigs = NULL;
> > > +	ns->num_vfs = 0;
> > > +}  
> > 
> > Why not something like:
> > 
> > | static int nsim_vfs_set(struct netdevsim *ns, unsigned int num_vfs)
> > | {
> > | 	void *ptr = krealloc(ns->vfconfigs,
> > | 			     num_vfs * sizeof(struct nsim_vf_config),
> > | 			     GFP_KERNEL);
> > | 
> > | 	if (!ptr)
> > | 		return -ENOMEM;
> > | 
> > | 	ns->vfconfigs = ptr;
> > | 	ns->num_vfs = num_vfs;
> > | 	return 0;
> > | }
> 
> Um.  It either frees or allocates, never reallocates so I felt realloc
> is misleading.  ZERO_SIZE_PTR is less clearly a NULL than a NULL.  I
> will have to specify __GFP_ZERO.  It's not a calloc so there could be
> potentially some overflows?

I don't understand: How can overflows happen if I use malloc() instead
of calloc()?

> > > +static ssize_t
> > > +nsim_numvfs_store(struct device *dev, struct device_attribute *attr,
> > > +		  const char *buf, size_t count)
> > > +{
> > > +	struct netdevsim *ns = to_nsim(dev);
> > > +	unsigned int num_vfs;
> > > +	int ret;
> > > +
> > > +	ret = kstrtouint(buf, 0, &num_vfs);
> > > +	if (ret)
> > > +		return ret;
> > > +
> > > +	rtnl_lock();
> > > +	if (ns->num_vfs == num_vfs)
> > > +		goto exit_good;  
> > 
> > Then replace this:
> > 
> > > +	if (ns->num_vfs && num_vfs) {
> > > +		ret = -EBUSY;
> > > +		goto exit_unlock;
> > > +	}
> > > +
> > > +	if (num_vfs) {
> > > +		ret = nsim_vfs_enable(ns, num_vfs);
> > > +		if (ret)
> > > +			goto exit_unlock;
> > > +	} else {
> > > +		nsim_vfs_disable(ns);
> > > +	}  
> > 
> > with just:
> > 
> > |	nsim_vfs_set(ns, num_vfs);
> 
> I'm trying to mirror the PCI subsystem behaviour here, which only
> allows enable or disable, not increase.  I felt we should follow how
> real devices behave:
> 
> 	/* enable VFs */
> 	if (pdev->sriov->num_VFs) {
> 		dev_warn(&pdev->dev, "%d VFs already enabled. Disable before enabling %d VFs\n",
> 			 pdev->sriov->num_VFs, num_vfs);
> 		return -EBUSY;
> 	}
> 
> So IOW this is intentional.

Ah, I see. Yes, then it makes sense! Keeping this virtual VF
functionality as close to real ones as possible is certainly feasible.

> > > +	ret = count;
> > > +exit_unlock:
> > > +	rtnl_unlock();
> > > +
> > > +	return ret;
> > > +}  
> > 
> > [...]
> > 
> > > +static void nsim_free(struct net_device *dev)
> > > +{
> > > +	struct netdevsim *ns = netdev_priv(dev);
> > > +
> > > +	device_unregister(&ns->dev);
> > >  }  
> > 
> > Shouldn't this also kfree(ns->vfconfigs)?
> 
> It's in uninit, I will move it to release.

Oh, I missed that. If you're certain this won't lead to memleaks, no
objection from my side. :)

Cheers, Phil

  reply	other threads:[~2017-12-01 21:36 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-12-01  1:35 [PATCH net-next v2 0/8] xdp: make stack perform remove and tests Jakub Kicinski
2017-12-01  1:35 ` [PATCH net-next v2 1/8] net: xdp: avoid output parameters when querying XDP prog Jakub Kicinski
2017-12-01  1:35 ` [PATCH net-next v2 2/8] net: xdp: report flags program was installed with on query Jakub Kicinski
2017-12-01  1:35 ` [PATCH net-next v2 3/8] net: xdp: make the stack take care of the tear down Jakub Kicinski
2017-12-01  1:35 ` [PATCH net-next v2 4/8] netdevsim: add software driver for testing offloads Jakub Kicinski
2017-12-01  1:35 ` [PATCH net-next v2 5/8] netdevsim: add bpf offload support Jakub Kicinski
2017-12-01  1:35 ` [PATCH net-next v2 6/8] selftests/bpf: add offload test based on netdevsim Jakub Kicinski
2017-12-01  1:35 ` [PATCH net-next v2 7/8] netdevsim: add SR-IOV functionality Jakub Kicinski
2017-12-01 13:43   ` Phil Sutter
2017-12-01 20:14     ` Jakub Kicinski
2017-12-01 21:36       ` Phil Sutter [this message]
2017-12-01 21:45         ` Jakub Kicinski
2017-12-01 21:58           ` Phil Sutter
2017-12-01 22:14             ` Jakub Kicinski
2017-12-01  1:35 ` [PATCH net-next v2 8/8] net: dummy: remove fake " Jakub Kicinski
2017-12-01 13:46   ` Phil Sutter
2017-12-01 20:19     ` Jakub Kicinski
2017-12-01 21:41       ` Phil Sutter

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=20171201213652.GT32305@orbyte.nwl.cc \
    --to=phil@nwl.cc \
    --cc=alexei.starovoitov@gmail.com \
    --cc=daniel@iogearbox.net \
    --cc=davem@davemloft.net \
    --cc=jakub.kicinski@netronome.com \
    --cc=jiri@resnulli.us \
    --cc=netdev@vger.kernel.org \
    --cc=oss-drivers@netronome.com \
    --cc=sd@queasysnail.net \
    /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).