diff mbox series

inetpeer: use else if instead of if to reduce judgment

Message ID 20210226105746.29240-1-yejune.deng@gmail.com (mailing list archive)
State Changes Requested
Delegated to: Netdev Maintainers
Headers show
Series inetpeer: use else if instead of if to reduce judgment | expand

Checks

Context Check Description
netdev/cover_letter success Link
netdev/fixes_present success Link
netdev/patch_count success Link
netdev/tree_selection success Guessed tree name to be net-next
netdev/subject_prefix warning Target tree name not specified in the subject
netdev/cc_maintainers success CCed 5 of 5 maintainers
netdev/source_inline success Was 0 now: 0
netdev/verify_signedoff success Link
netdev/module_param success Was 0 now: 0
netdev/build_32bit success Errors and warnings before: 1 this patch: 1
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/verify_fixes success Link
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 17 lines checked
netdev/build_allmodconfig_warn success Errors and warnings before: 1 this patch: 1
netdev/header_inline success Link
netdev/stable success Stable not CCed

Commit Message

Yejune Deng Feb. 26, 2021, 10:57 a.m. UTC
In inet_initpeers(), if si.totalram <= (8192*1024)/PAGE_SIZE, it will
be judged three times. Use else if instead of if, it only needs to be
judged once.

Signed-off-by: Yejune Deng <yejune.deng@gmail.com>
---
 net/ipv4/inetpeer.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

Comments

Eric Dumazet Feb. 26, 2021, 2:38 p.m. UTC | #1
On 2/26/21 11:57 AM, Yejune Deng wrote:
> In inet_initpeers(), if si.totalram <= (8192*1024)/PAGE_SIZE, it will
> be judged three times. Use else if instead of if, it only needs to be
> judged once.
> 
> Signed-off-by: Yejune Deng <yejune.deng@gmail.com>
> ---
>  net/ipv4/inetpeer.c | 10 +++++-----
>  1 file changed, 5 insertions(+), 5 deletions(-)
> 
> diff --git a/net/ipv4/inetpeer.c b/net/ipv4/inetpeer.c
> index ff327a62c9ce..07cd1f8204b3 100644
> --- a/net/ipv4/inetpeer.c
> +++ b/net/ipv4/inetpeer.c
> @@ -81,12 +81,12 @@ void __init inet_initpeers(void)
>  	 * <kuznet@ms2.inr.ac.ru>.  I don't have any opinion about the values
>  	 * myself.  --SAW
>  	 */
> -	if (si.totalram <= (32768*1024)/PAGE_SIZE)
> +	if (si.totalram <= (8192 * 1024) / PAGE_SIZE)
> +		inet_peer_threshold >>= 4; /* about 128KB */
> +	else if (si.totalram <= (16384 * 1024) / PAGE_SIZE)
> +		inet_peer_threshold >>= 2; /* about 512KB */
> +	else if (si.totalram <= (32768 * 1024) / PAGE_SIZE)
>  		inet_peer_threshold >>= 1; /* max pool size about 1MB on IA32 */


If you really want to change this stuff, I would suggest updating comments,
because nowadays, struct inet_peer on IA32 uses 128 bytes.

So 32768 entries would consume 4 MB,
   16384 entries would consume 2 MB

and 4096 entries would consume 512KB

Another idea would be to get rid of the cascade and use something that
will not need to be adjusted in the future.

diff --git a/net/ipv4/inetpeer.c b/net/ipv4/inetpeer.c
index ff327a62c9ce9b1794104c3c924f5f2b9820ac8b..d5f486bd8c35234f99b22842e756a10531e070d6 100644
--- a/net/ipv4/inetpeer.c
+++ b/net/ipv4/inetpeer.c
@@ -65,7 +65,7 @@ EXPORT_SYMBOL_GPL(inet_peer_base_init);
 #define PEER_MAX_GC 32
 
 /* Exported for sysctl_net_ipv4.  */
-int inet_peer_threshold __read_mostly = 65536 + 128;   /* start to throw entries more
+int inet_peer_threshold __read_mostly; /* start to throw entries more
                                         * aggressively at this stage */
 int inet_peer_minttl __read_mostly = 120 * HZ; /* TTL under high load: 120 sec */
 int inet_peer_maxttl __read_mostly = 10 * 60 * HZ;     /* usual time to live: 10 min */
@@ -73,20 +73,13 @@ int inet_peer_maxttl __read_mostly = 10 * 60 * HZ;  /* usual time to live: 10 min
 /* Called from ip_output.c:ip_init  */
 void __init inet_initpeers(void)
 {
-       struct sysinfo si;
+       u64 nr_entries;
 
-       /* Use the straight interface to information about memory. */
-       si_meminfo(&si);
-       /* The values below were suggested by Alexey Kuznetsov
-        * <kuznet@ms2.inr.ac.ru>.  I don't have any opinion about the values
-        * myself.  --SAW
-        */
-       if (si.totalram <= (32768*1024)/PAGE_SIZE)
-               inet_peer_threshold >>= 1; /* max pool size about 1MB on IA32 */
-       if (si.totalram <= (16384*1024)/PAGE_SIZE)
-               inet_peer_threshold >>= 1; /* about 512KB */
-       if (si.totalram <= (8192*1024)/PAGE_SIZE)
-               inet_peer_threshold >>= 2; /* about 128KB */
+       /* 1% of physical memory */
+       nr_entries = div64_ul((u64)totalram_pages() << PAGE_SHIFT,
+                             100 * L1_CACHE_ALIGN(sizeof(struct inet_peer)));
+
+       inet_peer_threshold = clamp_val(nr_entries, 4096, 65536 + 128);
 
        peer_cachep = kmem_cache_create("inet_peer_cache",
                        sizeof(struct inet_peer),
Yejune Deng March 1, 2021, 1:43 a.m. UTC | #2
Thanks,I will adopt it and resubmit.

On Fri, Feb 26, 2021 at 10:50 PM Eric Dumazet <eric.dumazet@gmail.com> wrote:
>
>
>
> On 2/26/21 11:57 AM, Yejune Deng wrote:
> > In inet_initpeers(), if si.totalram <= (8192*1024)/PAGE_SIZE, it will
> > be judged three times. Use else if instead of if, it only needs to be
> > judged once.
> >
> > Signed-off-by: Yejune Deng <yejune.deng@gmail.com>
> > ---
> >  net/ipv4/inetpeer.c | 10 +++++-----
> >  1 file changed, 5 insertions(+), 5 deletions(-)
> >
> > diff --git a/net/ipv4/inetpeer.c b/net/ipv4/inetpeer.c
> > index ff327a62c9ce..07cd1f8204b3 100644
> > --- a/net/ipv4/inetpeer.c
> > +++ b/net/ipv4/inetpeer.c
> > @@ -81,12 +81,12 @@ void __init inet_initpeers(void)
> >        * <kuznet@ms2.inr.ac.ru>.  I don't have any opinion about the values
> >        * myself.  --SAW
> >        */
> > -     if (si.totalram <= (32768*1024)/PAGE_SIZE)
> > +     if (si.totalram <= (8192 * 1024) / PAGE_SIZE)
> > +             inet_peer_threshold >>= 4; /* about 128KB */
> > +     else if (si.totalram <= (16384 * 1024) / PAGE_SIZE)
> > +             inet_peer_threshold >>= 2; /* about 512KB */
> > +     else if (si.totalram <= (32768 * 1024) / PAGE_SIZE)
> >               inet_peer_threshold >>= 1; /* max pool size about 1MB on IA32 */
>
>
> If you really want to change this stuff, I would suggest updating comments,
> because nowadays, struct inet_peer on IA32 uses 128 bytes.
>
> So 32768 entries would consume 4 MB,
>    16384 entries would consume 2 MB
>
> and 4096 entries would consume 512KB
>
> Another idea would be to get rid of the cascade and use something that
> will not need to be adjusted in the future.
>
> diff --git a/net/ipv4/inetpeer.c b/net/ipv4/inetpeer.c
> index ff327a62c9ce9b1794104c3c924f5f2b9820ac8b..d5f486bd8c35234f99b22842e756a10531e070d6 100644
> --- a/net/ipv4/inetpeer.c
> +++ b/net/ipv4/inetpeer.c
> @@ -65,7 +65,7 @@ EXPORT_SYMBOL_GPL(inet_peer_base_init);
>  #define PEER_MAX_GC 32
>
>  /* Exported for sysctl_net_ipv4.  */
> -int inet_peer_threshold __read_mostly = 65536 + 128;   /* start to throw entries more
> +int inet_peer_threshold __read_mostly; /* start to throw entries more
>                                          * aggressively at this stage */
>  int inet_peer_minttl __read_mostly = 120 * HZ; /* TTL under high load: 120 sec */
>  int inet_peer_maxttl __read_mostly = 10 * 60 * HZ;     /* usual time to live: 10 min */
> @@ -73,20 +73,13 @@ int inet_peer_maxttl __read_mostly = 10 * 60 * HZ;  /* usual time to live: 10 min
>  /* Called from ip_output.c:ip_init  */
>  void __init inet_initpeers(void)
>  {
> -       struct sysinfo si;
> +       u64 nr_entries;
>
> -       /* Use the straight interface to information about memory. */
> -       si_meminfo(&si);
> -       /* The values below were suggested by Alexey Kuznetsov
> -        * <kuznet@ms2.inr.ac.ru>.  I don't have any opinion about the values
> -        * myself.  --SAW
> -        */
> -       if (si.totalram <= (32768*1024)/PAGE_SIZE)
> -               inet_peer_threshold >>= 1; /* max pool size about 1MB on IA32 */
> -       if (si.totalram <= (16384*1024)/PAGE_SIZE)
> -               inet_peer_threshold >>= 1; /* about 512KB */
> -       if (si.totalram <= (8192*1024)/PAGE_SIZE)
> -               inet_peer_threshold >>= 2; /* about 128KB */
> +       /* 1% of physical memory */
> +       nr_entries = div64_ul((u64)totalram_pages() << PAGE_SHIFT,
> +                             100 * L1_CACHE_ALIGN(sizeof(struct inet_peer)));
> +
> +       inet_peer_threshold = clamp_val(nr_entries, 4096, 65536 + 128);
>
>         peer_cachep = kmem_cache_create("inet_peer_cache",
>                         sizeof(struct inet_peer),
>
>
>
>
>
>
diff mbox series

Patch

diff --git a/net/ipv4/inetpeer.c b/net/ipv4/inetpeer.c
index ff327a62c9ce..07cd1f8204b3 100644
--- a/net/ipv4/inetpeer.c
+++ b/net/ipv4/inetpeer.c
@@ -81,12 +81,12 @@  void __init inet_initpeers(void)
 	 * <kuznet@ms2.inr.ac.ru>.  I don't have any opinion about the values
 	 * myself.  --SAW
 	 */
-	if (si.totalram <= (32768*1024)/PAGE_SIZE)
+	if (si.totalram <= (8192 * 1024) / PAGE_SIZE)
+		inet_peer_threshold >>= 4; /* about 128KB */
+	else if (si.totalram <= (16384 * 1024) / PAGE_SIZE)
+		inet_peer_threshold >>= 2; /* about 512KB */
+	else if (si.totalram <= (32768 * 1024) / PAGE_SIZE)
 		inet_peer_threshold >>= 1; /* max pool size about 1MB on IA32 */
-	if (si.totalram <= (16384*1024)/PAGE_SIZE)
-		inet_peer_threshold >>= 1; /* about 512KB */
-	if (si.totalram <= (8192*1024)/PAGE_SIZE)
-		inet_peer_threshold >>= 2; /* about 128KB */
 
 	peer_cachep = kmem_cache_create("inet_peer_cache",
 			sizeof(struct inet_peer),