All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Théo Lebrun" <theo.lebrun@bootlin.com>
To: "Conor Dooley" <conor.dooley@microchip.com>,
	"Andrew Lunn" <andrew+netdev@lunn.ch>,
	"David S. Miller" <davem@davemloft.net>,
	"Eric Dumazet" <edumazet@kernel.org>,
	"Jakub Kicinski" <kuba@kernel.org>,
	"Paolo Abeni" <pabeni@redhat.com>,
	"Russell King" <linux@armlinux.org.uk>
Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org,
	"Nicolai Buchwitz" <nb@tipi-net.de>,
	"Paolo Valerio" <pvalerio@redhat.com>,
	"Vladimir Kondratiev" <vladimir.kondratiev@mobileye.com>,
	"Gregory CLEMENT" <gregory.clement@bootlin.com>,
	"Benoît Monin" <benoit.monin@bootlin.com>,
	"Tawfik Bayouk" <tawfik.bayouk@mobileye.com>,
	"Thomas Petazzoni" <thomas.petazzoni@bootlin.com>,
	"Théo Lebrun" <theo.lebrun@bootlin.com>
Subject: Re: [PATCH net-next v6 2/2] net: macb: distribute evenly Tx SRAM segments
Date: Fri, 13 Nov 2026 19:18:32 +0100	[thread overview]
Message-ID: <DMUAG5X9SJS5.3IJRLV67X9SXH@bootlin.com> (raw)
In-Reply-To: <20261113-macb-set-channels-v6-2-5de59d9e7f02@bootlin.com>

On Fri Nov 13, 2026 at 6:32 PM CET, Théo Lebrun wrote:
> +/*
> + * Distribute evenly available segments across queues. The computation is
> + * complex because (1) segments are counted in powers of two and (2) a queue
> + * can only use up to 8 segments. There are four types of cases:
> + *  - Sharing all segments equally is doable. Take num_queues=4 and
> + *    num_segments=16. Each queue will get 2^2=4 segments.
> + *  - Sharing all segments is doable. Take num_queues=5 and num_segments=16.
> + *    Three queues will get 2^2=4 segments and two will get 2^1=2 segments.
> + *  - Sharing all segments is not doable because not enough queues are
> + *    available. Take num_queues=1 and num_segments=16; queue 0 can only have 8
> + *    segments.
> + *  - Sharing all segments is not doable because not enough segments are
> + *    available. Take num_queues=4 and num_segments=2.
> + *
> + * We start by computing the power each queue will have. For num_queues=5 and
> + * num_segments=16, each queue will have at least 2^1 segments. That leaves us
> + * with remaining_segments=6. If we increase the power for a queue, we get a
> + * delta of 2 (2^2-2^1). The first three queues will therefore be advantaged
> + * and each have 2^2 segments. The remaining 2 queues will only have 2^1
> + * segments.
> + */
> +static u32 gem_sram_distribute_segments(unsigned int num_queues,
> +					unsigned int num_segments)
> +{
> +	unsigned int pow, remaining_segments, i;
> +	unsigned int num_advantaged_queues = 0;
> +	u32 val = 0;
> +
> +	/* When growing the max # of queues, make sure to return a u64 here and
> +	 * to write into GEM_TXQSEGALLOC_UPPER.
> +	 */
> +	static_assert(MACB_MAX_QUEUES == 8);
> +
> +	/* pow=0 for all queues. ilog2(0) is dangerous. */
> +	if (num_queues >= num_segments)
> +		return 0;
> +
> +	pow = min_t(unsigned int, ilog2(num_segments / num_queues), 3);
> +	remaining_segments = num_segments - num_queues * (1U << pow);
> +
> +	/* We can only distribute remaining segments if (1) there are remaining
> +	 * segments and (2) we did not reach the max segments per queue (2^3).
> +	 */
> +	if (remaining_segments != 0 && pow != 3) {
> +		unsigned int delta = (1U << (pow + 1)) - (1U << pow);
> +
> +		num_advantaged_queues = remaining_segments / delta;
> +	}
> +
> +	for (i = 0; i < num_advantaged_queues; i++)
> +		val |= field_prep(GEM_TXQSEGALLOC_MASK(i), pow + 1);
> +	for (i = num_advantaged_queues; i < num_queues; i++)
> +		val |= field_prep(GEM_TXQSEGALLOC_MASK(i), pow);
> +
> +	return val;
> +}

This implementation doesn't use all segments if
num_segments > 8*num_queues. Reported by Nicolai and discussed here [0].
I don't think any hardware lands in that case, but let's fix the bug.
I will let some time pass before iterating.

[0]: https://lore.kernel.org/all/1113dac19771ff7881b60bfdd14b0af6@tipi-net.de/

Thanks,

--
Théo Lebrun, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com


      reply	other threads:[~2026-11-13 18:18 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-13 17:32 [PATCH net-next v6 0/2] net: macb: implement ethtool set channels count operation Théo Lebrun
2026-11-13 17:32 ` [PATCH net-next v6 1/2] net: macb: implement ethtool_ops.get|set_channels() Théo Lebrun
2026-11-13 17:32 ` [PATCH net-next v6 2/2] net: macb: distribute evenly Tx SRAM segments Théo Lebrun
2026-11-13 18:18   ` Théo Lebrun [this message]

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=DMUAG5X9SJS5.3IJRLV67X9SXH@bootlin.com \
    --to=theo.lebrun@bootlin.com \
    --cc=andrew+netdev@lunn.ch \
    --cc=benoit.monin@bootlin.com \
    --cc=conor.dooley@microchip.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=gregory.clement@bootlin.com \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@armlinux.org.uk \
    --cc=nb@tipi-net.de \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=pvalerio@redhat.com \
    --cc=tawfik.bayouk@mobileye.com \
    --cc=thomas.petazzoni@bootlin.com \
    --cc=vladimir.kondratiev@mobileye.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.