linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH bpf-next v2] bpf: Initialize same number of free nodes for each pcpu_freelist
@ 2022-11-08 14:22 Xu Kuohai
  2022-11-09 23:56 ` Andrii Nakryiko
  0 siblings, 1 reply; 3+ messages in thread
From: Xu Kuohai @ 2022-11-08 14:22 UTC (permalink / raw)
  To: bpf, linux-kernel
  Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	Martin KaFai Lau, Song Liu, Yonghong Song, John Fastabend,
	KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa

From: Xu Kuohai <xukuohai@huawei.com>

pcpu_freelist_populate() initializes nr_elems / num_possible_cpus() + 1
free nodes for some CPUs, and then possibly one CPU with fewer nodes,
followed by remaining cpus with 0 nodes. For example, when nr_elems == 256
and num_possible_cpus() == 32, if CPU 0 is the current cpu, CPU 0~27
each gets 9 free nodes, CPU 28 gets 4 free nodes, CPU 29~31 get 0 free
nodes, while in fact each CPU should get 8 nodes equally.

This patch initializes nr_elems / num_possible_cpus() free nodes for each
CPU firstly, then allocates the remaining free nodes by one for each CPU
until no free nodes left.

Signed-off-by: Xu Kuohai <xukuohai@huawei.com>
Acked-by: Yonghong Song <yhs@fb.com>
---
v2: Update commit message and add Yonghong's ack
---
 kernel/bpf/percpu_freelist.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/kernel/bpf/percpu_freelist.c b/kernel/bpf/percpu_freelist.c
index b6e7f5c5b9ab..89e84f7381cc 100644
--- a/kernel/bpf/percpu_freelist.c
+++ b/kernel/bpf/percpu_freelist.c
@@ -100,12 +100,15 @@ void pcpu_freelist_populate(struct pcpu_freelist *s, void *buf, u32 elem_size,
 			    u32 nr_elems)
 {
 	struct pcpu_freelist_head *head;
-	int i, cpu, pcpu_entries;
+	int i, cpu, pcpu_entries, remain_entries;
+
+	pcpu_entries = nr_elems / num_possible_cpus();
+	remain_entries = nr_elems % num_possible_cpus();
 
-	pcpu_entries = nr_elems / num_possible_cpus() + 1;
 	i = 0;
 
 	for_each_possible_cpu(cpu) {
+		int j = i + pcpu_entries + (remain_entries-- > 0 ? 1 : 0);
 again:
 		head = per_cpu_ptr(s->freelist, cpu);
 		/* No locking required as this is not visible yet. */
@@ -114,7 +117,7 @@ void pcpu_freelist_populate(struct pcpu_freelist *s, void *buf, u32 elem_size,
 		buf += elem_size;
 		if (i == nr_elems)
 			break;
-		if (i % pcpu_entries)
+		if (i < j)
 			goto again;
 	}
 }
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH bpf-next v2] bpf: Initialize same number of free nodes for each pcpu_freelist
  2022-11-08 14:22 [PATCH bpf-next v2] bpf: Initialize same number of free nodes for each pcpu_freelist Xu Kuohai
@ 2022-11-09 23:56 ` Andrii Nakryiko
  2022-11-10  2:39   ` Xu Kuohai
  0 siblings, 1 reply; 3+ messages in thread
From: Andrii Nakryiko @ 2022-11-09 23:56 UTC (permalink / raw)
  To: Xu Kuohai
  Cc: bpf, linux-kernel, Alexei Starovoitov, Daniel Borkmann,
	Andrii Nakryiko, Martin KaFai Lau, Song Liu, Yonghong Song,
	John Fastabend, KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa

On Tue, Nov 8, 2022 at 6:05 AM Xu Kuohai <xukuohai@huaweicloud.com> wrote:
>
> From: Xu Kuohai <xukuohai@huawei.com>
>
> pcpu_freelist_populate() initializes nr_elems / num_possible_cpus() + 1
> free nodes for some CPUs, and then possibly one CPU with fewer nodes,
> followed by remaining cpus with 0 nodes. For example, when nr_elems == 256
> and num_possible_cpus() == 32, if CPU 0 is the current cpu, CPU 0~27
> each gets 9 free nodes, CPU 28 gets 4 free nodes, CPU 29~31 get 0 free
> nodes, while in fact each CPU should get 8 nodes equally.
>
> This patch initializes nr_elems / num_possible_cpus() free nodes for each
> CPU firstly, then allocates the remaining free nodes by one for each CPU
> until no free nodes left.
>
> Signed-off-by: Xu Kuohai <xukuohai@huawei.com>
> Acked-by: Yonghong Song <yhs@fb.com>
> ---
> v2: Update commit message and add Yonghong's ack
> ---
>  kernel/bpf/percpu_freelist.c | 9 ++++++---
>  1 file changed, 6 insertions(+), 3 deletions(-)
>
> diff --git a/kernel/bpf/percpu_freelist.c b/kernel/bpf/percpu_freelist.c
> index b6e7f5c5b9ab..89e84f7381cc 100644
> --- a/kernel/bpf/percpu_freelist.c
> +++ b/kernel/bpf/percpu_freelist.c
> @@ -100,12 +100,15 @@ void pcpu_freelist_populate(struct pcpu_freelist *s, void *buf, u32 elem_size,
>                             u32 nr_elems)
>  {
>         struct pcpu_freelist_head *head;
> -       int i, cpu, pcpu_entries;
> +       int i, cpu, pcpu_entries, remain_entries;
> +
> +       pcpu_entries = nr_elems / num_possible_cpus();
> +       remain_entries = nr_elems % num_possible_cpus();
>
> -       pcpu_entries = nr_elems / num_possible_cpus() + 1;
>         i = 0;
>
>         for_each_possible_cpu(cpu) {
> +               int j = i + pcpu_entries + (remain_entries-- > 0 ? 1 : 0);
>  again:
>                 head = per_cpu_ptr(s->freelist, cpu);
>                 /* No locking required as this is not visible yet. */
> @@ -114,7 +117,7 @@ void pcpu_freelist_populate(struct pcpu_freelist *s, void *buf, u32 elem_size,
>                 buf += elem_size;
>                 if (i == nr_elems)
>                         break;
> -               if (i % pcpu_entries)
> +               if (i < j)
>                         goto again;
>         }

this loop's logic is quite hard to follow, if we are fixing it, can we
simplify it maybe? something like:

int cpu, cpu_idx, i, j, n, m;

n = nr_elems / num_possible_cpus();
m = nr_elems % num_possible_cpus();

for_each_possible_cpu(cpu) {
    i = n + (cpu_idx < m ? 1 : 0);
    for (j = 0; j < i; j++) {
        head = per_cpu_ptr(s->freelist, cpu);
        pcpu_freelist_push_node(head, buf);
        buf += elem_size;
    }
    cpu_idx++;
}


no gotos, no extra ifs: for each cpu we determine correct number of
elements to allocate, then just allocate them in a straightforward
loop

>  }
> --
> 2.30.2
>

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH bpf-next v2] bpf: Initialize same number of free nodes for each pcpu_freelist
  2022-11-09 23:56 ` Andrii Nakryiko
@ 2022-11-10  2:39   ` Xu Kuohai
  0 siblings, 0 replies; 3+ messages in thread
From: Xu Kuohai @ 2022-11-10  2:39 UTC (permalink / raw)
  To: Andrii Nakryiko
  Cc: bpf, linux-kernel, Alexei Starovoitov, Daniel Borkmann,
	Andrii Nakryiko, Martin KaFai Lau, Song Liu, Yonghong Song,
	John Fastabend, KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa

On 11/10/2022 7:56 AM, Andrii Nakryiko wrote:
> On Tue, Nov 8, 2022 at 6:05 AM Xu Kuohai <xukuohai@huaweicloud.com> wrote:
>>
>> From: Xu Kuohai <xukuohai@huawei.com>
>>
>> pcpu_freelist_populate() initializes nr_elems / num_possible_cpus() + 1
>> free nodes for some CPUs, and then possibly one CPU with fewer nodes,
>> followed by remaining cpus with 0 nodes. For example, when nr_elems == 256
>> and num_possible_cpus() == 32, if CPU 0 is the current cpu, CPU 0~27
>> each gets 9 free nodes, CPU 28 gets 4 free nodes, CPU 29~31 get 0 free
>> nodes, while in fact each CPU should get 8 nodes equally.
>>
>> This patch initializes nr_elems / num_possible_cpus() free nodes for each
>> CPU firstly, then allocates the remaining free nodes by one for each CPU
>> until no free nodes left.
>>
>> Signed-off-by: Xu Kuohai <xukuohai@huawei.com>
>> Acked-by: Yonghong Song <yhs@fb.com>
>> ---
>> v2: Update commit message and add Yonghong's ack
>> ---
>>   kernel/bpf/percpu_freelist.c | 9 ++++++---
>>   1 file changed, 6 insertions(+), 3 deletions(-)
>>
>> diff --git a/kernel/bpf/percpu_freelist.c b/kernel/bpf/percpu_freelist.c
>> index b6e7f5c5b9ab..89e84f7381cc 100644
>> --- a/kernel/bpf/percpu_freelist.c
>> +++ b/kernel/bpf/percpu_freelist.c
>> @@ -100,12 +100,15 @@ void pcpu_freelist_populate(struct pcpu_freelist *s, void *buf, u32 elem_size,
>>                              u32 nr_elems)
>>   {
>>          struct pcpu_freelist_head *head;
>> -       int i, cpu, pcpu_entries;
>> +       int i, cpu, pcpu_entries, remain_entries;
>> +
>> +       pcpu_entries = nr_elems / num_possible_cpus();
>> +       remain_entries = nr_elems % num_possible_cpus();
>>
>> -       pcpu_entries = nr_elems / num_possible_cpus() + 1;
>>          i = 0;
>>
>>          for_each_possible_cpu(cpu) {
>> +               int j = i + pcpu_entries + (remain_entries-- > 0 ? 1 : 0);
>>   again:
>>                  head = per_cpu_ptr(s->freelist, cpu);
>>                  /* No locking required as this is not visible yet. */
>> @@ -114,7 +117,7 @@ void pcpu_freelist_populate(struct pcpu_freelist *s, void *buf, u32 elem_size,
>>                  buf += elem_size;
>>                  if (i == nr_elems)
>>                          break;
>> -               if (i % pcpu_entries)
>> +               if (i < j)
>>                          goto again;
>>          }
> 
> this loop's logic is quite hard to follow, if we are fixing it, can we
> simplify it maybe? something like:
> 
> int cpu, cpu_idx, i, j, n, m;
> 
> n = nr_elems / num_possible_cpus();
> m = nr_elems % num_possible_cpus();
> 
> for_each_possible_cpu(cpu) {
>      i = n + (cpu_idx < m ? 1 : 0);
>      for (j = 0; j < i; j++) {
>          head = per_cpu_ptr(s->freelist, cpu);
>          pcpu_freelist_push_node(head, buf);
>          buf += elem_size;
>      }
>      cpu_idx++;
> }
> 
> 
> no gotos, no extra ifs: for each cpu we determine correct number of
> elements to allocate, then just allocate them in a straightforward
> loop
> 

that's great, will update to:

int cpu, cpu_idx, i, j, n, m;

n = nr_elems / num_possible_cpus();
m = nr_elems % num_possible_cpus();

for_each_possible_cpu(cpu) {
       j = min(n + (cpu_idx < m ? 1 : 0), nr_elems);
       for (i = 0; i < j; i++) {
           head = per_cpu_ptr(s->freelist, cpu);
           pcpu_freelist_push_node(head, buf);
           buf += elem_size;
       }
       nr_elems -= j;
       cpu_idx++;
}

>>   }
>> --
>> 2.30.2
>>


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-11-10  2:39 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-08 14:22 [PATCH bpf-next v2] bpf: Initialize same number of free nodes for each pcpu_freelist Xu Kuohai
2022-11-09 23:56 ` Andrii Nakryiko
2022-11-10  2:39   ` Xu Kuohai

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).