lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <ZW3om2dfA4U0lhVY@yury-ThinkPad>
Date:   Mon, 4 Dec 2023 06:56:27 -0800
From:   Yury Norov <yury.norov@...il.com>
To:     Souradeep Chakrabarti <schakrabarti@...ux.microsoft.com>
Cc:     kys@...rosoft.com, haiyangz@...rosoft.com, wei.liu@...nel.org,
        decui@...rosoft.com, davem@...emloft.net, edumazet@...gle.com,
        kuba@...nel.org, pabeni@...hat.com, longli@...rosoft.com,
        leon@...nel.org, cai.huoqing@...ux.dev,
        ssengar@...ux.microsoft.com, vkuznets@...hat.com,
        tglx@...utronix.de, linux-hyperv@...r.kernel.org,
        netdev@...r.kernel.org, linux-kernel@...r.kernel.org,
        linux-rdma@...r.kernel.org, sch^Crabarti@...rosoft.com,
        paulros@...rosoft.com
Subject: Re: [PATCH V4 net-next] net: mana: Assigning IRQ affinity on HT cores

On Mon, Dec 04, 2023 at 12:50:41AM -0800, Souradeep Chakrabarti wrote:
> Existing MANA design assigns IRQ to every CPU, including sibling
> hyper-threads. This may cause multiple IRQs to be active simultaneously
> in the same core and may reduce the network performance with RSS.
> 
> Improve the performance by assigning IRQ to non sibling CPUs in local
> NUMA node.
> 
> Signed-off-by: Souradeep Chakrabarti <schakrabarti@...ux.microsoft.com>
> ---
> V3 -> V4:
> * Used for_each_numa_hop_mask() macro and simplified the code.
> Thanks to Yury Norov for the suggestion.

We've got a special tag for this:

Suggested-by: Yury Norov <yury.norov@...li.com>

> * Added code to assign hwc irq separately in mana_gd_setup_irqs.
> 
> V2 -> V3:
> * Created a helper function to get the next NUMA with CPU.
> * Added some error checks for unsuccessful memory allocation.
> * Fixed some comments on the code.
> 
> V1 -> V2:
> * Simplified the code by removing filter_mask_list and using avail_cpus.
> * Addressed infinite loop issue when there are numa nodes with no CPUs.
> * Addressed uses of local numa node instead of 0 to start.
> * Removed uses of BUG_ON.
> * Placed cpus_read_lock in parent function to avoid num_online_cpus
>   to get changed before function finishes the affinity assignment.
> ---
>  .../net/ethernet/microsoft/mana/gdma_main.c   | 70 +++++++++++++++++--
>  1 file changed, 63 insertions(+), 7 deletions(-)
> 
> diff --git a/drivers/net/ethernet/microsoft/mana/gdma_main.c b/drivers/net/ethernet/microsoft/mana/gdma_main.c
> index 6367de0c2c2e..2194a53cce10 100644
> --- a/drivers/net/ethernet/microsoft/mana/gdma_main.c
> +++ b/drivers/net/ethernet/microsoft/mana/gdma_main.c
> @@ -1243,15 +1243,57 @@ void mana_gd_free_res_map(struct gdma_resource *r)
>  	r->size = 0;
>  }
>  
> +static int irq_setup(int *irqs, int nvec, int start_numa_node)
> +{
> +	int i = 0, cpu, err = 0;
> +	const struct cpumask *node_cpumask;
> +	unsigned int  next_node = start_numa_node;
> +	cpumask_var_t visited_cpus, node_cpumask_temp;
> +
> +	if (!zalloc_cpumask_var(&visited_cpus, GFP_KERNEL)) {
> +		err = ENOMEM;
> +		return err;
> +	}
> +	if (!zalloc_cpumask_var(&node_cpumask_temp, GFP_KERNEL)) {
> +		err = -ENOMEM;
> +		return err;
> +	}

Can you add a bit more of vertical spacing?

> +	rcu_read_lock();
> +	for_each_numa_hop_mask(node_cpumask, next_node) {
> +		cpumask_copy(node_cpumask_temp, node_cpumask);
> +		for_each_cpu(cpu, node_cpumask_temp) {
> +			cpumask_andnot(node_cpumask_temp, node_cpumask_temp,
> +				       topology_sibling_cpumask(cpu));
> +			irq_set_affinity_and_hint(irqs[i], cpumask_of(cpu));
> +			if (++i == nvec)
> +				goto free_mask;
> +			cpumask_set_cpu(cpu, visited_cpus);
> +			if (cpumask_empty(node_cpumask_temp)) {
> +				cpumask_copy(node_cpumask_temp, node_cpumask);
> +				cpumask_andnot(node_cpumask_temp, node_cpumask_temp,
> +					       visited_cpus);
> +				cpu = 0;
> +			}

It feels like you can calculate number of sibling groups in a hop in
advance, so that you'll know how many IRQs you want to assign per each
hop, and avoid resetting the node_cpumask_temp and spinning in inner
loop for more than once...

Can you print your topology, and describe how you want to spread IRQs
on it, and how your existing code does spread them?

Please add performance results in the commit message.

I feel like this may be a useful code for other kernel folks, and if
so, we'd invest in it for more and make it a generic API, similar to
cpumaks_local_spread()...

> +		}
> +	}
> +free_mask:
> +	rcu_read_unlock();
> +	free_cpumask_var(visited_cpus);
> +	free_cpumask_var(node_cpumask_temp);
> +	return err;
> +}
> +
>  static int mana_gd_setup_irqs(struct pci_dev *pdev)
>  {
> -	unsigned int max_queues_per_port = num_online_cpus();
>  	struct gdma_context *gc = pci_get_drvdata(pdev);
> +	unsigned int max_queues_per_port;
>  	struct gdma_irq_context *gic;
>  	unsigned int max_irqs, cpu;
> -	int nvec, irq;
> +	int nvec, *irqs, irq;
>  	int err, i = 0, j;
>  
> +	cpus_read_lock();
> +	max_queues_per_port = num_online_cpus();
>  	if (max_queues_per_port > MANA_MAX_NUM_QUEUES)
>  		max_queues_per_port = MANA_MAX_NUM_QUEUES;
>  
> @@ -1261,6 +1303,11 @@ static int mana_gd_setup_irqs(struct pci_dev *pdev)
>  	nvec = pci_alloc_irq_vectors(pdev, 2, max_irqs, PCI_IRQ_MSIX);
>  	if (nvec < 0)
>  		return nvec;
> +	irqs = kmalloc_array(max_queues_per_port, sizeof(int), GFP_KERNEL);
> +	if (!irqs) {
> +		err = -ENOMEM;
> +		goto free_irq_vector;
> +	}
>  
>  	gc->irq_contexts = kcalloc(nvec, sizeof(struct gdma_irq_context),
>  				   GFP_KERNEL);
> @@ -1287,21 +1334,28 @@ static int mana_gd_setup_irqs(struct pci_dev *pdev)
>  			goto free_irq;
>  		}
>  
> -		err = request_irq(irq, mana_gd_intr, 0, gic->name, gic);
> +		if (!i) {
> +			err = request_irq(irq, mana_gd_intr, 0, gic->name, gic);
> +			cpu = cpumask_local_spread(i, gc->numa_node);

If i == 0, you can simplify it because you just need the 1st CPU from
a given node.

> +			irq_set_affinity_and_hint(irq, cpumask_of(cpu));
> +		} else {
> +			irqs[i - 1] = irq;
> +			err = request_irq(irqs[i - 1], mana_gd_intr, 0, gic->name, gic);
> +		}
>  		if (err)
>  			goto free_irq;
> -
> -		cpu = cpumask_local_spread(i, gc->numa_node);
> -		irq_set_affinity_and_hint(irq, cpumask_of(cpu));
>  	}
>  
> +	err = irq_setup(irqs, max_queues_per_port, gc->numa_node);
> +	if (err)
> +		goto free_irq;
>  	err = mana_gd_alloc_res_map(nvec, &gc->msix_resource);
>  	if (err)
>  		goto free_irq;
>  
>  	gc->max_num_msix = nvec;
>  	gc->num_msix_usable = nvec;
> -
> +	cpus_read_unlock();
>  	return 0;
>  
>  free_irq:
> @@ -1314,8 +1368,10 @@ static int mana_gd_setup_irqs(struct pci_dev *pdev)
>  	}
>  
>  	kfree(gc->irq_contexts);
> +	kfree(irqs);
>  	gc->irq_contexts = NULL;
>  free_irq_vector:
> +	cpus_read_unlock();
>  	pci_free_irq_vectors(pdev);
>  	return err;
>  }
> -- 
> 2.34.1

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ