|
| 1 | +/* |
| 2 | + * Copyright (c) 2016, Mellanox Technologies. All rights reserved. |
| 3 | + * |
| 4 | + * This software is available to you under a choice of one of two |
| 5 | + * licenses. You may choose to be licensed under the terms of the GNU |
| 6 | + * General Public License (GPL) Version 2, available from the file |
| 7 | + * COPYING in the main directory of this source tree, or the |
| 8 | + * OpenIB.org BSD license below: |
| 9 | + * |
| 10 | + * Redistribution and use in source and binary forms, with or |
| 11 | + * without modification, are permitted provided that the following |
| 12 | + * conditions are met: |
| 13 | + * |
| 14 | + * - Redistributions of source code must retain the above |
| 15 | + * copyright notice, this list of conditions and the following |
| 16 | + * disclaimer. |
| 17 | + * |
| 18 | + * - Redistributions in binary form must reproduce the above |
| 19 | + * copyright notice, this list of conditions and the following |
| 20 | + * disclaimer in the documentation and/or other materials |
| 21 | + * provided with the distribution. |
| 22 | + * |
| 23 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 24 | + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 25 | + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 26 | + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 27 | + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 28 | + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 29 | + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 30 | + * SOFTWARE. |
| 31 | + */ |
| 32 | + |
| 33 | +#include <linux/mlx5/driver.h> |
| 34 | +#include <linux/mlx5/fs.h> |
| 35 | +#include "mlx5_core.h" |
| 36 | +#include "fs_core.h" |
| 37 | +#include "fs_cmd.h" |
| 38 | + |
| 39 | +#define MLX5_FC_STATS_PERIOD msecs_to_jiffies(1000) |
| 40 | + |
| 41 | +/* locking scheme: |
| 42 | + * |
| 43 | + * It is the responsibility of the user to prevent concurrent calls or bad |
| 44 | + * ordering to mlx5_fc_create(), mlx5_fc_destroy() and accessing a reference |
| 45 | + * to struct mlx5_fc. |
| 46 | + * e.g en_tc.c is protected by RTNL lock of its caller, and will never call a |
| 47 | + * dump (access to struct mlx5_fc) after a counter is destroyed. |
| 48 | + * |
| 49 | + * access to counter list: |
| 50 | + * - create (user context) |
| 51 | + * - mlx5_fc_create() only adds to an addlist to be used by |
| 52 | + * mlx5_fc_stats_query_work(). addlist is protected by a spinlock. |
| 53 | + * - spawn thread to do the actual destroy |
| 54 | + * |
| 55 | + * - destroy (user context) |
| 56 | + * - mark a counter as deleted |
| 57 | + * - spawn thread to do the actual del |
| 58 | + * |
| 59 | + * - dump (user context) |
| 60 | + * user should not call dump after destroy |
| 61 | + * |
| 62 | + * - query (single thread workqueue context) |
| 63 | + * destroy/dump - no conflict (see destroy) |
| 64 | + * query/dump - packets and bytes might be inconsistent (since update is not |
| 65 | + * atomic) |
| 66 | + * query/create - no conflict (see create) |
| 67 | + * since every create/destroy spawn the work, only after necessary time has |
| 68 | + * elapsed, the thread will actually query the hardware. |
| 69 | + */ |
| 70 | + |
| 71 | +static void mlx5_fc_stats_work(struct work_struct *work) |
| 72 | +{ |
| 73 | + struct mlx5_core_dev *dev = container_of(work, struct mlx5_core_dev, |
| 74 | + priv.fc_stats.work.work); |
| 75 | + struct mlx5_fc_stats *fc_stats = &dev->priv.fc_stats; |
| 76 | + unsigned long now = jiffies; |
| 77 | + struct mlx5_fc *counter; |
| 78 | + struct mlx5_fc *tmp; |
| 79 | + int err = 0; |
| 80 | + |
| 81 | + spin_lock(&fc_stats->addlist_lock); |
| 82 | + |
| 83 | + list_splice_tail_init(&fc_stats->addlist, &fc_stats->list); |
| 84 | + |
| 85 | + if (!list_empty(&fc_stats->list)) |
| 86 | + queue_delayed_work(fc_stats->wq, &fc_stats->work, MLX5_FC_STATS_PERIOD); |
| 87 | + |
| 88 | + spin_unlock(&fc_stats->addlist_lock); |
| 89 | + |
| 90 | + list_for_each_entry_safe(counter, tmp, &fc_stats->list, list) { |
| 91 | + struct mlx5_fc_cache *c = &counter->cache; |
| 92 | + u64 packets; |
| 93 | + u64 bytes; |
| 94 | + |
| 95 | + if (counter->deleted) { |
| 96 | + list_del(&counter->list); |
| 97 | + |
| 98 | + mlx5_cmd_fc_free(dev, counter->id); |
| 99 | + |
| 100 | + kfree(counter); |
| 101 | + continue; |
| 102 | + } |
| 103 | + |
| 104 | + if (time_before(now, fc_stats->next_query)) |
| 105 | + continue; |
| 106 | + |
| 107 | + err = mlx5_cmd_fc_query(dev, counter->id, &packets, &bytes); |
| 108 | + if (err) { |
| 109 | + pr_err("Error querying stats for counter id %d\n", |
| 110 | + counter->id); |
| 111 | + continue; |
| 112 | + } |
| 113 | + |
| 114 | + if (packets == c->packets) |
| 115 | + continue; |
| 116 | + |
| 117 | + c->lastuse = jiffies; |
| 118 | + c->packets = packets; |
| 119 | + c->bytes = bytes; |
| 120 | + } |
| 121 | + |
| 122 | + if (time_after_eq(now, fc_stats->next_query)) |
| 123 | + fc_stats->next_query = now + MLX5_FC_STATS_PERIOD; |
| 124 | +} |
| 125 | + |
| 126 | +struct mlx5_fc *mlx5_fc_create(struct mlx5_core_dev *dev, bool aging) |
| 127 | +{ |
| 128 | + struct mlx5_fc_stats *fc_stats = &dev->priv.fc_stats; |
| 129 | + struct mlx5_fc *counter; |
| 130 | + int err; |
| 131 | + |
| 132 | + counter = kzalloc(sizeof(*counter), GFP_KERNEL); |
| 133 | + if (!counter) |
| 134 | + return ERR_PTR(-ENOMEM); |
| 135 | + |
| 136 | + err = mlx5_cmd_fc_alloc(dev, &counter->id); |
| 137 | + if (err) |
| 138 | + goto err_out; |
| 139 | + |
| 140 | + if (aging) { |
| 141 | + counter->aging = true; |
| 142 | + |
| 143 | + spin_lock(&fc_stats->addlist_lock); |
| 144 | + list_add(&counter->list, &fc_stats->addlist); |
| 145 | + spin_unlock(&fc_stats->addlist_lock); |
| 146 | + |
| 147 | + mod_delayed_work(fc_stats->wq, &fc_stats->work, 0); |
| 148 | + } |
| 149 | + |
| 150 | + return counter; |
| 151 | + |
| 152 | +err_out: |
| 153 | + kfree(counter); |
| 154 | + |
| 155 | + return ERR_PTR(err); |
| 156 | +} |
| 157 | + |
| 158 | +void mlx5_fc_destroy(struct mlx5_core_dev *dev, struct mlx5_fc *counter) |
| 159 | +{ |
| 160 | + struct mlx5_fc_stats *fc_stats = &dev->priv.fc_stats; |
| 161 | + |
| 162 | + if (!counter) |
| 163 | + return; |
| 164 | + |
| 165 | + if (counter->aging) { |
| 166 | + counter->deleted = true; |
| 167 | + mod_delayed_work(fc_stats->wq, &fc_stats->work, 0); |
| 168 | + return; |
| 169 | + } |
| 170 | + |
| 171 | + mlx5_cmd_fc_free(dev, counter->id); |
| 172 | + kfree(counter); |
| 173 | +} |
| 174 | + |
| 175 | +int mlx5_init_fc_stats(struct mlx5_core_dev *dev) |
| 176 | +{ |
| 177 | + struct mlx5_fc_stats *fc_stats = &dev->priv.fc_stats; |
| 178 | + |
| 179 | + INIT_LIST_HEAD(&fc_stats->list); |
| 180 | + INIT_LIST_HEAD(&fc_stats->addlist); |
| 181 | + spin_lock_init(&fc_stats->addlist_lock); |
| 182 | + |
| 183 | + fc_stats->wq = create_singlethread_workqueue("mlx5_fc"); |
| 184 | + if (!fc_stats->wq) |
| 185 | + return -ENOMEM; |
| 186 | + |
| 187 | + INIT_DELAYED_WORK(&fc_stats->work, mlx5_fc_stats_work); |
| 188 | + |
| 189 | + return 0; |
| 190 | +} |
| 191 | + |
| 192 | +void mlx5_cleanup_fc_stats(struct mlx5_core_dev *dev) |
| 193 | +{ |
| 194 | + struct mlx5_fc_stats *fc_stats = &dev->priv.fc_stats; |
| 195 | + struct mlx5_fc *counter; |
| 196 | + struct mlx5_fc *tmp; |
| 197 | + |
| 198 | + cancel_delayed_work_sync(&dev->priv.fc_stats.work); |
| 199 | + destroy_workqueue(dev->priv.fc_stats.wq); |
| 200 | + dev->priv.fc_stats.wq = NULL; |
| 201 | + |
| 202 | + list_splice_tail_init(&fc_stats->addlist, &fc_stats->list); |
| 203 | + |
| 204 | + list_for_each_entry_safe(counter, tmp, &fc_stats->list, list) { |
| 205 | + list_del(&counter->list); |
| 206 | + |
| 207 | + mlx5_cmd_fc_free(dev, counter->id); |
| 208 | + |
| 209 | + kfree(counter); |
| 210 | + } |
| 211 | +} |
| 212 | + |
| 213 | +void mlx5_fc_query_cached(struct mlx5_fc *counter, |
| 214 | + u64 *bytes, u64 *packets, u64 *lastuse) |
| 215 | +{ |
| 216 | + struct mlx5_fc_cache c; |
| 217 | + |
| 218 | + c = counter->cache; |
| 219 | + |
| 220 | + *bytes = c.bytes - counter->lastbytes; |
| 221 | + *packets = c.packets - counter->lastpackets; |
| 222 | + *lastuse = c.lastuse; |
| 223 | + |
| 224 | + counter->lastbytes = c.bytes; |
| 225 | + counter->lastpackets = c.packets; |
| 226 | +} |
0 commit comments