From nobody Tue Dec 03 00:51:26 2024 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4Y2MXt5xdJz5fH3c; Tue, 03 Dec 2024 00:51:26 +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 "R10" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Y2MXt4NX8z4Tfh; Tue, 3 Dec 2024 00:51:26 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1733187086; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=1VwEYZXw70l8BzK5Y7QoUYCybZ1LTaYMd5yeLRJQWfE=; b=s7F5xxlw1n0gRq5G9+Ti73IdgAeITVXUtONZ21pRKWlT3BYfz7Jxva4HQOTz2D9qYnwaeI RJGLxn5BHHL0Ys+g9h7T0Jj18e5n6/Oi0bI1cf+dklqaMAS6LXtr5LflueUIU/pDqiQ4xC xoiddlDQWMKys1LXDdfk35BJd2abKsPSTjL9D1lfMwlxL858cmSrYf6QlRkWF6VWKvzUqo hxYjIkRzeVAhiwAR5vfTjVDfR2I8wtxG1wBcY6sCK/E+z5HyFB66z7cCrB2+7u2IdLTwrm kE4nXX2lD0H3TeUG+ZOzC7HXmocA30dWzcGFy4AEkENSKVlAPglleUUNeVg6FA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1733187086; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=1VwEYZXw70l8BzK5Y7QoUYCybZ1LTaYMd5yeLRJQWfE=; b=KDKXD2zThpXUNguchelxSM/Vt0FCuk/xovxKrB2MvjWd1GFQVdo+x/xo04Pdkm2ryjSa9D iqEXE9Ege2Er4U/FeVnKTTEJa+Ola6mul3F1Mhvr+AZ0/JgwnR33tiKMLoD7drL/0ygSHd qC9ejnd+0u8uVCWn7oth/+qXx4MKq4KN1JhH1xTlLtGp/0osS76Hr/2Tf5AT7dPQjge+d9 1EPm6ejrB5KmagISssXUl49hkQWumjZNnYewYswH7/sLJ/OygS3ItLcbM6Y234loL8btOD LegDARUwPCzB3+k+5exKHRJIwtD39KIMIDwkzSA40ErV0HQ0z28uFyNlz/x7Sg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1733187086; a=rsa-sha256; cv=none; b=PNw+I/4Riu1kKSmrVmT2G8z82PN3VptDQnuXmm7FqUvTAOXccUZNW7aJcnEbiZn5ECNwx0 LCop+dQCeumvUv5T6p1bQjHawLJBkD9S1aDFWUTQ/hh9KN0lZNuCuUJ4lvPJPGDo/MNP1i ONrqzMoHeHg6kyvDFYC99jEDlXLxi3Vqw5pSaRBJ2V58pe2/THY/2FV8Lk9RnH+fWRwL20 fmSZzWSCVG5kxZs9faa4PH5BBC9/iig3Y32FZbGzrT7n9H92SBXY1mAMBXA4vUMvfOAVea Z5sAMDdu3a1RZ822zBoYZQLYEULZMC609eFnTOc1hMSS/rkIkJxyekCbq/ImMw== 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 4Y2MXt40lPz117G; Tue, 3 Dec 2024 00:51:26 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 4B30pQES016319; Tue, 3 Dec 2024 00:51:26 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 4B30pQI9016316; Tue, 3 Dec 2024 00:51:26 GMT (envelope-from git) Date: Tue, 3 Dec 2024 00:51:26 GMT Message-Id: <202412030051.4B30pQI9016316@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Konstantin Belousov Subject: git: 9ff300614ba7 - stable/14 - mlx5e tls: Ensure all allocated tags have a hw context associated List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kib X-Git-Repository: src X-Git-Refname: refs/heads/stable/14 X-Git-Reftype: branch X-Git-Commit: 9ff300614ba71cfc2958b8ffe606e3df53023b29 Auto-Submitted: auto-generated The branch stable/14 has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=9ff300614ba71cfc2958b8ffe606e3df53023b29 commit 9ff300614ba71cfc2958b8ffe606e3df53023b29 Author: Andrew Gallatin AuthorDate: 2024-11-23 09:43:17 +0000 Commit: Konstantin Belousov CommitDate: 2024-12-03 00:39:22 +0000 mlx5e tls: Ensure all allocated tags have a hw context associated (cherry picked from commit 81b38bce07940b7a4001dfeb8cd63774229ca950) --- sys/dev/mlx5/mlx5_en/en_hw_tls.h | 2 +- sys/dev/mlx5/mlx5_en/mlx5_en_hw_tls.c | 132 ++++++++++++++++++++++++---------- 2 files changed, 95 insertions(+), 39 deletions(-) diff --git a/sys/dev/mlx5/mlx5_en/en_hw_tls.h b/sys/dev/mlx5/mlx5_en/en_hw_tls.h index 2018198e5e52..d637314e040e 100644 --- a/sys/dev/mlx5/mlx5_en/en_hw_tls.h +++ b/sys/dev/mlx5/mlx5_en/en_hw_tls.h @@ -84,7 +84,7 @@ struct mlx5e_tls { struct workqueue_struct *wq; uma_zone_t zone; uint32_t max_resources; /* max number of resources */ - volatile uint32_t num_resources; /* current number of resources */ + int zone_max; int init; /* set when ready */ char zname[32]; }; diff --git a/sys/dev/mlx5/mlx5_en/mlx5_en_hw_tls.c b/sys/dev/mlx5/mlx5_en/mlx5_en_hw_tls.c index f84493f49b5d..a4ddf2d7f69e 100644 --- a/sys/dev/mlx5/mlx5_en/mlx5_en_hw_tls.c +++ b/sys/dev/mlx5/mlx5_en/mlx5_en_hw_tls.c @@ -80,17 +80,57 @@ static const char *mlx5e_tls_stats_desc[] = { static void mlx5e_tls_work(struct work_struct *); +/* + * Expand the tls tag UMA zone in a sleepable context + */ + +static void +mlx5e_prealloc_tags(struct mlx5e_priv *priv, int nitems) +{ + struct mlx5e_tls_tag **tags; + int i; + + tags = malloc(sizeof(tags[0]) * nitems, + M_MLX5E_TLS, M_WAITOK); + for (i = 0; i < nitems; i++) + tags[i] = uma_zalloc(priv->tls.zone, M_WAITOK); + __compiler_membar(); + for (i = 0; i < nitems; i++) + uma_zfree(priv->tls.zone, tags[i]); + free(tags, M_MLX5E_TLS); +} + static int mlx5e_tls_tag_import(void *arg, void **store, int cnt, int domain, int flags) { struct mlx5e_tls_tag *ptag; - int i; + struct mlx5e_priv *priv = arg; + int err, i; + + /* + * mlx5_tls_open_tis() sleeps on a firmware command, so + * zone allocations must be done from a sleepable context. + * Note that the uma_zalloc() in mlx5e_tls_snd_tag_alloc() + * is done with M_NOWAIT so that hitting the zone limit does + * not cause the allocation to pause forever. + */ for (i = 0; i != cnt; i++) { ptag = malloc_domainset(sizeof(*ptag), M_MLX5E_TLS, mlx5_dev_domainset(arg), flags | M_ZERO); + if (ptag == NULL) + return (i); + ptag->tls = &priv->tls; mtx_init(&ptag->mtx, "mlx5-tls-tag-mtx", NULL, MTX_DEF); INIT_WORK(&ptag->work, mlx5e_tls_work); + err = mlx5_tls_open_tis(priv->mdev, 0, priv->tdn, + priv->pdn, &ptag->tisn); + if (err) { + MLX5E_TLS_STAT_INC(ptag, tx_error, 1); + free(ptag, M_MLX5E_TLS); + return (i); + } + store[i] = ptag; } return (i); @@ -113,7 +153,6 @@ mlx5e_tls_tag_release(void *arg, void **store, int cnt) if (ptag->tisn != 0) { mlx5_tls_close_tis(priv->mdev, ptag->tisn); - atomic_add_32(&ptls->num_resources, -1U); } mtx_destroy(&ptag->mtx); @@ -135,20 +174,38 @@ mlx5e_tls_tag_zfree(struct mlx5e_tls_tag *ptag) /* avoid leaking keys */ memset(ptag->crypto_params, 0, sizeof(ptag->crypto_params)); - /* update number of TIS contexts */ - if (ptag->tisn == 0) - atomic_add_32(&ptag->tls->num_resources, -1U); - /* return tag to UMA */ uma_zfree(ptag->tls->zone, ptag); } +static int +mlx5e_max_tag_proc(SYSCTL_HANDLER_ARGS) +{ + struct mlx5e_priv *priv = (struct mlx5e_priv *)arg1; + struct mlx5e_tls *ptls = &priv->tls; + int err; + unsigned int max_tags; + + max_tags = ptls->zone_max; + err = sysctl_handle_int(oidp, &max_tags, arg2, req); + if (err != 0 || req->newptr == NULL ) + return err; + if (max_tags == ptls->zone_max) + return 0; + if (max_tags > priv->tls.max_resources || max_tags == 0) + return (EINVAL); + ptls->zone_max = max_tags; + uma_zone_set_max(ptls->zone, ptls->zone_max); + return 0; +} + int mlx5e_tls_init(struct mlx5e_priv *priv) { struct mlx5e_tls *ptls = &priv->tls; struct sysctl_oid *node; - uint32_t x; + uint32_t max_dek, max_tis, x; + int zone_max = 0, prealloc_tags = 0; if (MLX5_CAP_GEN(priv->mdev, tls_tx) == 0 || MLX5_CAP_GEN(priv->mdev, log_max_dek) == 0) @@ -163,13 +220,31 @@ mlx5e_tls_init(struct mlx5e_priv *priv) snprintf(ptls->zname, sizeof(ptls->zname), "mlx5_%u_tls", device_get_unit(priv->mdev->pdev->dev.bsddev)); + + TUNABLE_INT_FETCH("hw.mlx5.tls_max_tags", &zone_max); + TUNABLE_INT_FETCH("hw.mlx5.tls_prealloc_tags", &prealloc_tags); + ptls->zone = uma_zcache_create(ptls->zname, sizeof(struct mlx5e_tls_tag), NULL, NULL, NULL, NULL, - mlx5e_tls_tag_import, mlx5e_tls_tag_release, priv->mdev, - UMA_ZONE_UNMANAGED); + mlx5e_tls_tag_import, mlx5e_tls_tag_release, priv, + UMA_ZONE_UNMANAGED | (prealloc_tags ? UMA_ZONE_NOFREE : 0)); /* shared between RX and TX TLS */ - ptls->max_resources = 1U << (MLX5_CAP_GEN(priv->mdev, log_max_dek) - 1); + max_dek = 1U << (MLX5_CAP_GEN(priv->mdev, log_max_dek) - 1); + max_tis = 1U << (MLX5_CAP_GEN(priv->mdev, log_max_tis) - 1); + ptls->max_resources = MIN(max_dek, max_tis); + + if (zone_max != 0) { + ptls->zone_max = zone_max; + if (ptls->zone_max > priv->tls.max_resources) + ptls->zone_max = priv->tls.max_resources; + } else { + ptls->zone_max = priv->tls.max_resources; + } + + uma_zone_set_max(ptls->zone, ptls->zone_max); + if (prealloc_tags != 0) + mlx5e_prealloc_tags(priv, ptls->zone_max); for (x = 0; x != MLX5E_TLS_STATS_NUM; x++) ptls->stats.arg[x] = counter_u64_alloc(M_WAITOK); @@ -182,6 +257,10 @@ mlx5e_tls_init(struct mlx5e_priv *priv) if (node == NULL) return (0); + SYSCTL_ADD_PROC(&priv->sysctl_ctx, SYSCTL_CHILDREN(node), OID_AUTO, "tls_max_tag", + CTLFLAG_RW | CTLTYPE_UINT | CTLFLAG_MPSAFE, priv, 0, mlx5e_max_tag_proc, + "IU", "Max number of TLS offload session tags"); + mlx5e_create_counter_stats(&ptls->ctx, SYSCTL_CHILDREN(node), "stats", mlx5e_tls_stats_desc, MLX5E_TLS_STATS_NUM, @@ -205,9 +284,6 @@ mlx5e_tls_cleanup(struct mlx5e_priv *priv) uma_zdestroy(ptls->zone); destroy_workqueue(ptls->wq); - /* check if all resources are freed */ - MPASS(priv->tls.num_resources == 0); - for (x = 0; x != MLX5E_TLS_STATS_NUM; x++) counter_u64_free(ptls->stats.arg[x]); } @@ -332,28 +408,16 @@ mlx5e_tls_snd_tag_alloc(if_t ifp, if (priv->gone != 0 || priv->tls.init == 0) return (EOPNOTSUPP); - /* allocate new tag from zone, if any */ ptag = uma_zalloc(priv->tls.zone, M_WAITOK); + if (ptag == NULL) + return (ENOMEM); /* sanity check default values */ MPASS(ptag->dek_index == 0); MPASS(ptag->dek_index_ok == 0); - /* setup TLS tag */ - ptag->tls = &priv->tls; - /* check if there is no TIS context */ - if (ptag->tisn == 0) { - uint32_t value; - - value = atomic_fetchadd_32(&priv->tls.num_resources, 1U); - - /* check resource limits */ - if (value >= priv->tls.max_resources) { - error = ENOMEM; - goto failure; - } - } + KASSERT(ptag->tisn != 0, ("ptag %p w/0 tisn", ptag)); en = ¶ms->tls.tls->params; @@ -446,17 +510,9 @@ mlx5e_tls_snd_tag_alloc(if_t ifp, /* reset state */ ptag->state = MLX5E_TLS_ST_INIT; - /* - * Try to immediately init the tag. We may fail if the NIC's - * resources are tied up with send tags that are in the work - * queue, waiting to be freed. So if we fail, put ourselves - * on the queue so as to try again after resouces have been freed. - */ error = mlx5e_tls_st_init(priv, ptag); - if (error != 0) { - queue_work(priv->tls.wq, &ptag->work); - flush_work(&ptag->work); - } + if (error != 0) + goto failure; return (0);