From patchwork Fri May 24 15:27:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philip Cox X-Patchwork-Id: 1939011 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.ubuntu.com (client-ip=185.125.189.65; helo=lists.ubuntu.com; envelope-from=kernel-team-bounces@lists.ubuntu.com; receiver=patchwork.ozlabs.org) Received: from lists.ubuntu.com (lists.ubuntu.com [185.125.189.65]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Vm89v6vrLz1ynR for ; Sat, 25 May 2024 01:30:11 +1000 (AEST) Received: from localhost ([127.0.0.1] helo=lists.ubuntu.com) by lists.ubuntu.com with esmtp (Exim 4.86_2) (envelope-from ) id 1sAWrm-0002r4-2G; Fri, 24 May 2024 15:29:58 +0000 Received: from smtp-relay-internal-1.internal ([10.131.114.114] helo=smtp-relay-internal-1.canonical.com) by lists.ubuntu.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1sAWrj-0002pf-Lg for kernel-team@lists.ubuntu.com; Fri, 24 May 2024 15:29:55 +0000 Received: from mail-oo1-f72.google.com (mail-oo1-f72.google.com [209.85.161.72]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-internal-1.canonical.com (Postfix) with ESMTPS id 611D73F6A7 for ; Fri, 24 May 2024 15:29:55 +0000 (UTC) Received: by mail-oo1-f72.google.com with SMTP id 006d021491bc7-5b2dada6765so3202369eaf.1 for ; Fri, 24 May 2024 08:29:55 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716564592; x=1717169392; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=7aDaFd0r9z432tqcSEBpeE56PX7wFVo17HWWRHfl1dA=; b=okZJJ08KEc6XtjeBDDqeTSmOuzd7cJQm4OW6kD2TOrRZSEr63LqOZ0sF1X3toInzfu PXuny/I9/z78TyEBeKIsIME4KGzdHXCjxOYz/wMdV9e0D1ZLxuMkAicE+qE2S1XgdVGd GSdoQJouQiBJulzONBoSW22mpQdEPrEAyTnrOKbgd/GtHkftRjARNKeZvQWncGRYG5ZU 1iI4Afx156U/nvBwJDZxpgYRc/HgMDZ6Th4HyZBojOhl4X2GrZ3OaeZtwcl3JWUb+POO NW0pARHWH8+zmjsuMfXg1CfURj2fnDNdWWEcLVVEZw3VLRhtlejmSZOTe/zc6QpfgsJo i6hQ== X-Gm-Message-State: AOJu0YyGqAzqYox1YSrMJmiPZsL2/TbAAxuH1mFTjCw1wS94vK/9Ll2+ 58s9ESkwCWT6+PX5CQ/Dnim8+dT+hIVuPZOClV28yv2urk6JG1M/TYDr6wvFG/n6RzlmgjXFTyC m0V7+qJ1aCYm2RllxAXquGL/2INccvcAtACvW1WYAQE0fFijfI7IBNs/DjtKY/C8WxAeEKFnWUa IS7tpFfb8htg== X-Received: by 2002:a05:6358:228f:b0:183:6427:10b6 with SMTP id e5c5f4694b2df-197e521926amr324089655d.19.1716564592169; Fri, 24 May 2024 08:29:52 -0700 (PDT) X-Google-Smtp-Source: AGHT+IERaXfprHX1q6a5JYkK63PhxzbMCV/gphsp5E8XIgWe8u3RMdXMn4R9MPiAuOzOWX+e7efGvA== X-Received: by 2002:a05:6358:228f:b0:183:6427:10b6 with SMTP id e5c5f4694b2df-197e521926amr324086855d.19.1716564591644; Fri, 24 May 2024 08:29:51 -0700 (PDT) Received: from cox.home.arpa ([76.69.53.230]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6ac070c8792sm8239066d6.11.2024.05.24.08.29.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 May 2024 08:29:49 -0700 (PDT) From: Philip Cox To: kernel-team@lists.ubuntu.com Subject: [SRU][m/n][PATCH 1/2] net: make SK_MEMORY_PCPU_RESERV tunable Date: Fri, 24 May 2024 11:27:39 -0400 Message-Id: <20240524152740.176176-2-philip.cox@canonical.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240524152740.176176-1-philip.cox@canonical.com> References: <20240524152740.176176-1-philip.cox@canonical.com> MIME-Version: 1.0 X-BeenThere: kernel-team@lists.ubuntu.com X-Mailman-Version: 2.1.20 Precedence: list List-Id: Kernel team discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: kernel-team-bounces@lists.ubuntu.com Sender: "kernel-team" From: Adam Li BugLink: BugLink: https://bugs.launchpad.net/bugs/2045560 [ Upstream commit 12a686c2e761f1f1f6e6e2117a9ab9c6de2ac8a7 ] This patch adds /proc/sys/net/core/mem_pcpu_rsv sysctl file, to make SK_MEMORY_PCPU_RESERV tunable. Commit 3cd3399dd7a8 ("net: implement per-cpu reserves for memory_allocated") introduced per-cpu forward alloc cache: "Implement a per-cpu cache of +1/-1 MB, to reduce number of changes to sk->sk_prot->memory_allocated, which would otherwise be cause of false sharing." sk_prot->memory_allocated points to global atomic variable: atomic_long_t tcp_memory_allocated ____cacheline_aligned_in_smp; If increasing the per-cpu cache size from 1MB to e.g. 16MB, changes to sk->sk_prot->memory_allocated can be further reduced. Performance may be improved on system with many cores. Signed-off-by: Adam Li Reviewed-by: Christoph Lameter (Ampere) Signed-off-by: David S. Miller Stable-dep-of: 3584718cf2ec ("net: fix sk_memory_allocated_{add|sub} vs softirqs") Signed-off-by: Sasha Levin (cherry picked from commit e830c804e26733fb09219cb9b92f167715c3b8a0 from 6.8.y stable branch) Signed-off-by: Philip Cox --- Documentation/admin-guide/sysctl/net.rst | 5 +++++ include/net/sock.h | 5 +++-- net/core/sock.c | 1 + net/core/sysctl_net_core.c | 9 +++++++++ 4 files changed, 18 insertions(+), 2 deletions(-) diff --git a/Documentation/admin-guide/sysctl/net.rst b/Documentation/admin-guide/sysctl/net.rst index 396091651955..7250c0542828 100644 --- a/Documentation/admin-guide/sysctl/net.rst +++ b/Documentation/admin-guide/sysctl/net.rst @@ -206,6 +206,11 @@ Will increase power usage. Default: 0 (off) +mem_pcpu_rsv +------------ + +Per-cpu reserved forward alloc cache size in page units. Default 1MB per CPU. + rmem_default ------------ diff --git a/include/net/sock.h b/include/net/sock.h index 54ca8dcbfb43..d156ae285639 100644 --- a/include/net/sock.h +++ b/include/net/sock.h @@ -1429,6 +1429,7 @@ sk_memory_allocated(const struct sock *sk) /* 1 MB per cpu, in page units */ #define SK_MEMORY_PCPU_RESERVE (1 << (20 - PAGE_SHIFT)) +extern int sysctl_mem_pcpu_rsv; static inline void sk_memory_allocated_add(struct sock *sk, int amt) @@ -1437,7 +1438,7 @@ sk_memory_allocated_add(struct sock *sk, int amt) preempt_disable(); local_reserve = __this_cpu_add_return(*sk->sk_prot->per_cpu_fw_alloc, amt); - if (local_reserve >= SK_MEMORY_PCPU_RESERVE) { + if (local_reserve >= READ_ONCE(sysctl_mem_pcpu_rsv)) { __this_cpu_sub(*sk->sk_prot->per_cpu_fw_alloc, local_reserve); atomic_long_add(local_reserve, sk->sk_prot->memory_allocated); } @@ -1451,7 +1452,7 @@ sk_memory_allocated_sub(struct sock *sk, int amt) preempt_disable(); local_reserve = __this_cpu_sub_return(*sk->sk_prot->per_cpu_fw_alloc, amt); - if (local_reserve <= -SK_MEMORY_PCPU_RESERVE) { + if (local_reserve <= -READ_ONCE(sysctl_mem_pcpu_rsv)) { __this_cpu_sub(*sk->sk_prot->per_cpu_fw_alloc, local_reserve); atomic_long_add(local_reserve, sk->sk_prot->memory_allocated); } diff --git a/net/core/sock.c b/net/core/sock.c index 5e78798456fd..9cf404e8038a 100644 --- a/net/core/sock.c +++ b/net/core/sock.c @@ -283,6 +283,7 @@ __u32 sysctl_rmem_max __read_mostly = SK_RMEM_MAX; EXPORT_SYMBOL(sysctl_rmem_max); __u32 sysctl_wmem_default __read_mostly = SK_WMEM_MAX; __u32 sysctl_rmem_default __read_mostly = SK_RMEM_MAX; +int sysctl_mem_pcpu_rsv __read_mostly = SK_MEMORY_PCPU_RESERVE; int sysctl_tstamp_allow_data __read_mostly = 1; diff --git a/net/core/sysctl_net_core.c b/net/core/sysctl_net_core.c index 0f0cb1465e08..986f15e5d6c4 100644 --- a/net/core/sysctl_net_core.c +++ b/net/core/sysctl_net_core.c @@ -30,6 +30,7 @@ static int int_3600 = 3600; static int min_sndbuf = SOCK_MIN_SNDBUF; static int min_rcvbuf = SOCK_MIN_RCVBUF; static int max_skb_frags = MAX_SKB_FRAGS; +static int min_mem_pcpu_rsv = SK_MEMORY_PCPU_RESERVE; static int net_msg_warn; /* Unused, but still a sysctl */ @@ -407,6 +408,14 @@ static struct ctl_table net_core_table[] = { .proc_handler = proc_dointvec_minmax, .extra1 = &min_rcvbuf, }, + { + .procname = "mem_pcpu_rsv", + .data = &sysctl_mem_pcpu_rsv, + .maxlen = sizeof(int), + .mode = 0644, + .proc_handler = proc_dointvec_minmax, + .extra1 = &min_mem_pcpu_rsv, + }, { .procname = "dev_weight", .data = &weight_p, From patchwork Fri May 24 15:27:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philip Cox X-Patchwork-Id: 1939013 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.ubuntu.com (client-ip=185.125.189.65; helo=lists.ubuntu.com; envelope-from=kernel-team-bounces@lists.ubuntu.com; receiver=patchwork.ozlabs.org) Received: from lists.ubuntu.com (lists.ubuntu.com [185.125.189.65]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Vm89w0fyKz20dw for ; Sat, 25 May 2024 01:30:11 +1000 (AEST) Received: from localhost ([127.0.0.1] helo=lists.ubuntu.com) by lists.ubuntu.com with esmtp (Exim 4.86_2) (envelope-from ) id 1sAWrn-0002rW-9t; Fri, 24 May 2024 15:29:59 +0000 Received: from smtp-relay-internal-0.internal ([10.131.114.225] helo=smtp-relay-internal-0.canonical.com) by lists.ubuntu.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1sAWrl-0002qr-AJ for kernel-team@lists.ubuntu.com; Fri, 24 May 2024 15:29:57 +0000 Received: from mail-qv1-f70.google.com (mail-qv1-f70.google.com [209.85.219.70]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-internal-0.canonical.com (Postfix) with ESMTPS id 25DC73F847 for ; Fri, 24 May 2024 15:29:57 +0000 (UTC) Received: by mail-qv1-f70.google.com with SMTP id 6a1803df08f44-6ab9ce5a776so12768766d6.0 for ; Fri, 24 May 2024 08:29:57 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716564593; x=1717169393; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=tH3VlSrssVWohWHSfDuMNyQVn/Kro5fLgy565tE4YuM=; b=DNkrvxgkDbjg/n7d3DfgsBV0b62iXbskh6AkPJo71AeGiBY/d7twWnI6jabmUSA5u9 sxFhyp6zRP227z3UZtaQ6H69M72nkgshNP3g2y0zDZBjruD3p9LEQEpthlPes0c5tglC SZIcN8LIup/JmhSzpasrkPoCI9+wI5wIqxct5PMNEf03c44iMLnYI2YUAOyDZeO72OIj BPf+vWXFnma5hY76wB5FRl0CTjnB4YrIklB6EgDFXfv8Z8a/v7yTh6zqcNrRBSd+U7VQ IskX2+DUR4FFTiMukJjWnRwrPsP4UrOsPIIYZMIZ5EHshenTOgQvtLKRvir53GPpIM1I wBcw== X-Gm-Message-State: AOJu0YxJX+27Ynh0GtQ+H/C+rK616f+1Wr5XSJzoVEURkFqrL035Trz3 r3w//zOwYwHFqfudVYsIPCFNcTKTNXeMlEPpw0mCV6Wvrzx4SVxks/qjU4S86iuMUh1MlThamYv c2hOjQEYgYz6WYOOQL6XbfooEGdde990LhFJ39LPRxdEu6vleTpBiVSa5FKZjlD6Z/Qxi9O3oIh SSB0ZrqWGf2w== X-Received: by 2002:a05:6214:5890:b0:6ab:7b2c:a7df with SMTP id 6a1803df08f44-6abbbcb7fa6mr27656876d6.38.1716564593582; Fri, 24 May 2024 08:29:53 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF5YZLpnS8ZZO+JUoYt4xjZBJFyJ+jpfR5BnKmFQBKHB1WqUhyL0x5Sy2lJrG4FVOz5ZXF4Dw== X-Received: by 2002:a05:6214:5890:b0:6ab:7b2c:a7df with SMTP id 6a1803df08f44-6abbbcb7fa6mr27656706d6.38.1716564593267; Fri, 24 May 2024 08:29:53 -0700 (PDT) Received: from cox.home.arpa ([76.69.53.230]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6ac070c8792sm8239066d6.11.2024.05.24.08.29.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 May 2024 08:29:52 -0700 (PDT) From: Philip Cox To: kernel-team@lists.ubuntu.com Subject: [SRU][m/n][PATCH 2/2] net: fix sk_memory_allocated_{add|sub} vs softirqs Date: Fri, 24 May 2024 11:27:40 -0400 Message-Id: <20240524152740.176176-3-philip.cox@canonical.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240524152740.176176-1-philip.cox@canonical.com> References: <20240524152740.176176-1-philip.cox@canonical.com> MIME-Version: 1.0 X-BeenThere: kernel-team@lists.ubuntu.com X-Mailman-Version: 2.1.20 Precedence: list List-Id: Kernel team discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: kernel-team-bounces@lists.ubuntu.com Sender: "kernel-team" From: Eric Dumazet BugLink: BugLink: https://bugs.launchpad.net/bugs/2045560 [ Upstream commit 3584718cf2ec7e79b6814f2596dcf398c5fb2eca ] Jonathan Heathcote reported a regression caused by blamed commit on aarch64 architecture. x86 happens to have irq-safe __this_cpu_add_return() and __this_cpu_sub(), but this is not generic. I think my confusion came from "struct sock" argument, because these helpers are called with a locked socket. But the memory accounting is per-proto (and per-cpu after the blamed commit). We might cleanup these helpers later to directly accept a "struct proto *proto" argument. Switch to this_cpu_add_return() and this_cpu_xchg() operations, and get rid of preempt_disable()/preempt_enable() pairs. Fast path becomes a bit faster as a result :) Many thanks to Jonathan Heathcote for his awesome report and investigations. Fixes: 3cd3399dd7a8 ("net: implement per-cpu reserves for memory_allocated") Reported-by: Jonathan Heathcote Closes: https://lore.kernel.org/netdev/VI1PR01MB42407D7947B2EA448F1E04EFD10D2@VI1PR01MB4240.eurprd01.prod.exchangelabs.com/ Signed-off-by: Eric Dumazet Acked-by: Soheil Hassas Yeganeh Reviewed-by: Shakeel Butt Link: https://lore.kernel.org/r/20240421175248.1692552-1-edumazet@google.com Signed-off-by: Jakub Kicinski Signed-off-by: Sasha Levin (cherry picked from commit d2fa3493811ecd49f1581940111ccf475fa5cd38 from 6.8.y stable branch)) Signed-off-by: Philip Cox --- include/net/sock.h | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/include/net/sock.h b/include/net/sock.h index d156ae285639..81afa8af2ac0 100644 --- a/include/net/sock.h +++ b/include/net/sock.h @@ -1431,32 +1431,34 @@ sk_memory_allocated(const struct sock *sk) #define SK_MEMORY_PCPU_RESERVE (1 << (20 - PAGE_SHIFT)) extern int sysctl_mem_pcpu_rsv; +static inline void proto_memory_pcpu_drain(struct proto *proto) +{ + int val = this_cpu_xchg(*proto->per_cpu_fw_alloc, 0); + + if (val) + atomic_long_add(val, proto->memory_allocated); +} + static inline void -sk_memory_allocated_add(struct sock *sk, int amt) +sk_memory_allocated_add(const struct sock *sk, int val) { - int local_reserve; + struct proto *proto = sk->sk_prot; - preempt_disable(); - local_reserve = __this_cpu_add_return(*sk->sk_prot->per_cpu_fw_alloc, amt); - if (local_reserve >= READ_ONCE(sysctl_mem_pcpu_rsv)) { - __this_cpu_sub(*sk->sk_prot->per_cpu_fw_alloc, local_reserve); - atomic_long_add(local_reserve, sk->sk_prot->memory_allocated); - } - preempt_enable(); + val = this_cpu_add_return(*proto->per_cpu_fw_alloc, val); + + if (unlikely(val >= READ_ONCE(sysctl_mem_pcpu_rsv))) + proto_memory_pcpu_drain(proto); } static inline void -sk_memory_allocated_sub(struct sock *sk, int amt) +sk_memory_allocated_sub(const struct sock *sk, int val) { - int local_reserve; + struct proto *proto = sk->sk_prot; - preempt_disable(); - local_reserve = __this_cpu_sub_return(*sk->sk_prot->per_cpu_fw_alloc, amt); - if (local_reserve <= -READ_ONCE(sysctl_mem_pcpu_rsv)) { - __this_cpu_sub(*sk->sk_prot->per_cpu_fw_alloc, local_reserve); - atomic_long_add(local_reserve, sk->sk_prot->memory_allocated); - } - preempt_enable(); + val = this_cpu_sub_return(*proto->per_cpu_fw_alloc, val); + + if (unlikely(val <= -READ_ONCE(sysctl_mem_pcpu_rsv))) + proto_memory_pcpu_drain(proto); } #define SK_ALLOC_PERCPU_COUNTER_BATCH 16