From owner-dev-commits-src-all@freebsd.org Fri Apr 16 20:45:34 2021 Return-Path: Delivered-To: dev-commits-src-all@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id EB3895DF820; Fri, 16 Apr 2021 20:45:33 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4FMSs95XC5z4prW; Fri, 16 Apr 2021 20:45:33 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id A04693730; Fri, 16 Apr 2021 20:45:33 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 13GKjXNS065993; Fri, 16 Apr 2021 20:45:33 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 13GKjXaK065992; Fri, 16 Apr 2021 20:45:33 GMT (envelope-from git) Date: Fri, 16 Apr 2021 20:45:33 GMT Message-Id: <202104162045.13GKjXaK065992@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Richard Scheffenegger Subject: git: 632e3363087c - stable/13 - tcp: Make hostcache.cache_count MPSAFE by using a counter_u64_t MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: rscheff X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: 632e3363087cb6ef2a7b26a291a044b97afabea7 Auto-Submitted: auto-generated X-BeenThere: dev-commits-src-all@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Commit messages for all branches of the src repository List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 16 Apr 2021 20:45:34 -0000 The branch stable/13 has been updated by rscheff: URL: https://cgit.FreeBSD.org/src/commit/?id=632e3363087cb6ef2a7b26a291a044b97afabea7 commit 632e3363087cb6ef2a7b26a291a044b97afabea7 Author: Richard Scheffenegger AuthorDate: 2021-03-31 17:30:20 +0000 Commit: Richard Scheffenegger CommitDate: 2021-04-16 19:43:14 +0000 tcp: Make hostcache.cache_count MPSAFE by using a counter_u64_t Addressing the underlying root cause for cache_count to show unexpectedly high values, by protecting all arithmetic on that global variable by using counter(9). PR: 254333 Reviewed By: tuexen, #transport MFC after: 2 weeks Sponsored by: NetApp, Inc. Differential Revision: https://reviews.freebsd.org/D29510 (cherry picked from commit 95e56d31e348594973affd0ea81d8f8383bc3031) --- sys/netinet/tcp_hostcache.c | 20 ++++++++++++-------- sys/netinet/tcp_hostcache.h | 20 ++++++++++---------- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/sys/netinet/tcp_hostcache.c b/sys/netinet/tcp_hostcache.c index b6d0c0410df9..67da97405c3f 100644 --- a/sys/netinet/tcp_hostcache.c +++ b/sys/netinet/tcp_hostcache.c @@ -147,8 +147,8 @@ SYSCTL_UINT(_net_inet_tcp_hostcache, OID_AUTO, bucketlimit, CTLFLAG_VNET | CTLFLAG_RDTUN, &VNET_NAME(tcp_hostcache.bucket_limit), 0, "Per-bucket hash limit for hostcache"); -SYSCTL_UINT(_net_inet_tcp_hostcache, OID_AUTO, count, CTLFLAG_VNET | CTLFLAG_RD, - &VNET_NAME(tcp_hostcache.cache_count), 0, +SYSCTL_COUNTER_U64(_net_inet_tcp_hostcache, OID_AUTO, count, CTLFLAG_VNET | CTLFLAG_RD, + &VNET_NAME(tcp_hostcache.cache_count), "Current number of entries in hostcache"); SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, expire, CTLFLAG_VNET | CTLFLAG_RW, @@ -199,7 +199,8 @@ tcp_hc_init(void) /* * Initialize hostcache structures. */ - V_tcp_hostcache.cache_count = 0; + V_tcp_hostcache.cache_count = counter_u64_alloc(M_WAITOK); + counter_u64_zero(V_tcp_hostcache.cache_count); V_tcp_hostcache.hashsize = TCP_HOSTCACHE_HASHSIZE; V_tcp_hostcache.bucket_limit = TCP_HOSTCACHE_BUCKETLIMIT; V_tcp_hostcache.expire = TCP_HOSTCACHE_EXPIRE; @@ -267,6 +268,9 @@ tcp_hc_destroy(void) /* Purge all hc entries. */ tcp_hc_purge_internal(1); + /* Release the counter */ + counter_u64_free(V_tcp_hostcache.cache_count); + /* Free the uma zone and the allocated hash table. */ uma_zdestroy(V_tcp_hostcache.zone); @@ -374,7 +378,7 @@ tcp_hc_insert(struct in_conninfo *inc) * If the bucket limit is reached, reuse the least-used element. */ if (hc_head->hch_length >= V_tcp_hostcache.bucket_limit || - V_tcp_hostcache.cache_count >= V_tcp_hostcache.cache_limit) { + counter_u64_fetch(V_tcp_hostcache.cache_count) >= V_tcp_hostcache.cache_limit) { hc_entry = TAILQ_LAST(&hc_head->hch_bucket, hc_qhead); /* * At first we were dropping the last element, just to @@ -391,7 +395,7 @@ tcp_hc_insert(struct in_conninfo *inc) } TAILQ_REMOVE(&hc_head->hch_bucket, hc_entry, rmx_q); V_tcp_hostcache.hashbase[hash].hch_length--; - V_tcp_hostcache.cache_count--; + counter_u64_add(V_tcp_hostcache.cache_count, -1); TCPSTAT_INC(tcps_hc_bucketoverflow); #if 0 uma_zfree(V_tcp_hostcache.zone, hc_entry); @@ -424,7 +428,7 @@ tcp_hc_insert(struct in_conninfo *inc) */ TAILQ_INSERT_HEAD(&hc_head->hch_bucket, hc_entry, rmx_q); V_tcp_hostcache.hashbase[hash].hch_length++; - V_tcp_hostcache.cache_count++; + counter_u64_add(V_tcp_hostcache.cache_count, 1); TCPSTAT_INC(tcps_hc_added); return hc_entry; @@ -640,7 +644,7 @@ sysctl_tcp_hc_list(SYSCTL_HANDLER_ARGS) /* Optimize Buffer length query by sbin/sysctl */ if (req->oldptr == NULL) { - len = (V_tcp_hostcache.cache_count + 1) * linesize; + len = (counter_u64_fetch(V_tcp_hostcache.cache_count) + 1) * linesize; return (SYSCTL_OUT(req, NULL, len)); } @@ -712,7 +716,7 @@ tcp_hc_purge_internal(int all) hc_entry, rmx_q); uma_zfree(V_tcp_hostcache.zone, hc_entry); V_tcp_hostcache.hashbase[i].hch_length--; - V_tcp_hostcache.cache_count--; + counter_u64_add(V_tcp_hostcache.cache_count, -1); } else hc_entry->rmx_expire -= V_tcp_hostcache.prune; } diff --git a/sys/netinet/tcp_hostcache.h b/sys/netinet/tcp_hostcache.h index f4e1013aac60..b5237392acc2 100644 --- a/sys/netinet/tcp_hostcache.h +++ b/sys/netinet/tcp_hostcache.h @@ -69,16 +69,16 @@ struct hc_metrics { }; struct tcp_hostcache { - struct hc_head *hashbase; - uma_zone_t zone; - u_int hashsize; - u_int hashmask; - u_int bucket_limit; - u_int cache_count; - u_int cache_limit; - int expire; - int prune; - int purgeall; + struct hc_head *hashbase; + uma_zone_t zone; + u_int hashsize; + u_int hashmask; + u_int bucket_limit; + counter_u64_t cache_count; + u_int cache_limit; + int expire; + int prune; + int purgeall; }; #endif /* !_NETINET_TCP_HOSTCACHE_H_*/