net/mlx5: Take fs_counters dellist before addlist
[sfrench/cifs-2.6.git] / drivers / net / ethernet / mellanox / mlx5 / core / fs_counters.c
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 <linux/rbtree.h>
36 #include "mlx5_core.h"
37 #include "fs_core.h"
38 #include "fs_cmd.h"
39
40 #define MLX5_FC_STATS_PERIOD msecs_to_jiffies(1000)
41 /* Max number of counters to query in bulk read is 32K */
42 #define MLX5_SW_MAX_COUNTERS_BULK BIT(15)
43
44 /* locking scheme:
45  *
46  * It is the responsibility of the user to prevent concurrent calls or bad
47  * ordering to mlx5_fc_create(), mlx5_fc_destroy() and accessing a reference
48  * to struct mlx5_fc.
49  * e.g en_tc.c is protected by RTNL lock of its caller, and will never call a
50  * dump (access to struct mlx5_fc) after a counter is destroyed.
51  *
52  * access to counter list:
53  * - create (user context)
54  *   - mlx5_fc_create() only adds to an addlist to be used by
55  *     mlx5_fc_stats_query_work(). addlist is a lockless single linked list
56  *     that doesn't require any additional synchronization when adding single
57  *     node.
58  *   - spawn thread to do the actual destroy
59  *
60  * - destroy (user context)
61  *   - add a counter to lockless dellist
62  *   - spawn thread to do the actual del
63  *
64  * - dump (user context)
65  *   user should not call dump after destroy
66  *
67  * - query (single thread workqueue context)
68  *   destroy/dump - no conflict (see destroy)
69  *   query/dump - packets and bytes might be inconsistent (since update is not
70  *                atomic)
71  *   query/create - no conflict (see create)
72  *   since every create/destroy spawn the work, only after necessary time has
73  *   elapsed, the thread will actually query the hardware.
74  */
75
76 static struct list_head *mlx5_fc_counters_lookup_next(struct mlx5_core_dev *dev,
77                                                       u32 id)
78 {
79         struct mlx5_fc_stats *fc_stats = &dev->priv.fc_stats;
80         unsigned long next_id = (unsigned long)id + 1;
81         struct mlx5_fc *counter;
82
83         rcu_read_lock();
84         /* skip counters that are in idr, but not yet in counters list */
85         while ((counter = idr_get_next_ul(&fc_stats->counters_idr,
86                                           &next_id)) != NULL &&
87                list_empty(&counter->list))
88                 next_id++;
89         rcu_read_unlock();
90
91         return counter ? &counter->list : &fc_stats->counters;
92 }
93
94 static void mlx5_fc_stats_insert(struct mlx5_core_dev *dev,
95                                  struct mlx5_fc *counter)
96 {
97         struct list_head *next = mlx5_fc_counters_lookup_next(dev, counter->id);
98
99         list_add_tail(&counter->list, next);
100 }
101
102 /* The function returns the last counter that was queried so the caller
103  * function can continue calling it till all counters are queried.
104  */
105 static struct mlx5_fc *mlx5_fc_stats_query(struct mlx5_core_dev *dev,
106                                            struct mlx5_fc *first,
107                                            u32 last_id)
108 {
109         struct mlx5_fc_stats *fc_stats = &dev->priv.fc_stats;
110         struct mlx5_fc *counter = NULL;
111         struct mlx5_cmd_fc_bulk *b;
112         bool more = false;
113         u32 afirst_id;
114         int num;
115         int err;
116
117         int max_bulk = min_t(int, MLX5_SW_MAX_COUNTERS_BULK,
118                              (1 << MLX5_CAP_GEN(dev, log_max_flow_counter_bulk)));
119
120         /* first id must be aligned to 4 when using bulk query */
121         afirst_id = first->id & ~0x3;
122
123         /* number of counters to query inc. the last counter */
124         num = ALIGN(last_id - afirst_id + 1, 4);
125         if (num > max_bulk) {
126                 num = max_bulk;
127                 last_id = afirst_id + num - 1;
128         }
129
130         b = mlx5_cmd_fc_bulk_alloc(dev, afirst_id, num);
131         if (!b) {
132                 mlx5_core_err(dev, "Error allocating resources for bulk query\n");
133                 return NULL;
134         }
135
136         err = mlx5_cmd_fc_bulk_query(dev, b);
137         if (err) {
138                 mlx5_core_err(dev, "Error doing bulk query: %d\n", err);
139                 goto out;
140         }
141
142         counter = first;
143         list_for_each_entry_from(counter, &fc_stats->counters, list) {
144                 struct mlx5_fc_cache *c = &counter->cache;
145                 u64 packets;
146                 u64 bytes;
147
148                 if (counter->id > last_id) {
149                         more = true;
150                         break;
151                 }
152
153                 mlx5_cmd_fc_bulk_get(dev, b,
154                                      counter->id, &packets, &bytes);
155
156                 if (c->packets == packets)
157                         continue;
158
159                 c->packets = packets;
160                 c->bytes = bytes;
161                 c->lastuse = jiffies;
162         }
163
164 out:
165         mlx5_cmd_fc_bulk_free(b);
166
167         return more ? counter : NULL;
168 }
169
170 static void mlx5_free_fc(struct mlx5_core_dev *dev,
171                          struct mlx5_fc *counter)
172 {
173         mlx5_cmd_fc_free(dev, counter->id);
174         kfree(counter);
175 }
176
177 static void mlx5_fc_stats_work(struct work_struct *work)
178 {
179         struct mlx5_core_dev *dev = container_of(work, struct mlx5_core_dev,
180                                                  priv.fc_stats.work.work);
181         struct mlx5_fc_stats *fc_stats = &dev->priv.fc_stats;
182         /* Take dellist first to ensure that counters cannot be deleted before
183          * they are inserted.
184          */
185         struct llist_node *dellist = llist_del_all(&fc_stats->dellist);
186         struct llist_node *addlist = llist_del_all(&fc_stats->addlist);
187         struct mlx5_fc *counter = NULL, *last = NULL, *tmp;
188         unsigned long now = jiffies;
189
190         if (addlist || !list_empty(&fc_stats->counters))
191                 queue_delayed_work(fc_stats->wq, &fc_stats->work,
192                                    fc_stats->sampling_interval);
193
194         llist_for_each_entry(counter, addlist, addlist)
195                 mlx5_fc_stats_insert(dev, counter);
196
197         llist_for_each_entry_safe(counter, tmp, dellist, dellist) {
198                 list_del(&counter->list);
199
200                 mlx5_free_fc(dev, counter);
201         }
202
203         if (time_before(now, fc_stats->next_query) ||
204             list_empty(&fc_stats->counters))
205                 return;
206         last = list_last_entry(&fc_stats->counters, struct mlx5_fc, list);
207
208         counter = list_first_entry(&fc_stats->counters, struct mlx5_fc,
209                                    list);
210         while (counter)
211                 counter = mlx5_fc_stats_query(dev, counter, last->id);
212
213         fc_stats->next_query = now + fc_stats->sampling_interval;
214 }
215
216 struct mlx5_fc *mlx5_fc_create(struct mlx5_core_dev *dev, bool aging)
217 {
218         struct mlx5_fc_stats *fc_stats = &dev->priv.fc_stats;
219         struct mlx5_fc *counter;
220         int err;
221
222         counter = kzalloc(sizeof(*counter), GFP_KERNEL);
223         if (!counter)
224                 return ERR_PTR(-ENOMEM);
225         INIT_LIST_HEAD(&counter->list);
226
227         err = mlx5_cmd_fc_alloc(dev, &counter->id);
228         if (err)
229                 goto err_out;
230
231         if (aging) {
232                 u32 id = counter->id;
233
234                 counter->cache.lastuse = jiffies;
235                 counter->aging = true;
236
237                 idr_preload(GFP_KERNEL);
238                 spin_lock(&fc_stats->counters_idr_lock);
239
240                 err = idr_alloc_u32(&fc_stats->counters_idr, counter, &id, id,
241                                     GFP_NOWAIT);
242
243                 spin_unlock(&fc_stats->counters_idr_lock);
244                 idr_preload_end();
245                 if (err)
246                         goto err_out_alloc;
247
248                 llist_add(&counter->addlist, &fc_stats->addlist);
249
250                 mod_delayed_work(fc_stats->wq, &fc_stats->work, 0);
251         }
252
253         return counter;
254
255 err_out_alloc:
256         mlx5_cmd_fc_free(dev, counter->id);
257 err_out:
258         kfree(counter);
259
260         return ERR_PTR(err);
261 }
262 EXPORT_SYMBOL(mlx5_fc_create);
263
264 u32 mlx5_fc_id(struct mlx5_fc *counter)
265 {
266         return counter->id;
267 }
268 EXPORT_SYMBOL(mlx5_fc_id);
269
270 void mlx5_fc_destroy(struct mlx5_core_dev *dev, struct mlx5_fc *counter)
271 {
272         struct mlx5_fc_stats *fc_stats = &dev->priv.fc_stats;
273
274         if (!counter)
275                 return;
276
277         if (counter->aging) {
278                 spin_lock(&fc_stats->counters_idr_lock);
279                 WARN_ON(!idr_remove(&fc_stats->counters_idr, counter->id));
280                 spin_unlock(&fc_stats->counters_idr_lock);
281
282                 llist_add(&counter->dellist, &fc_stats->dellist);
283                 mod_delayed_work(fc_stats->wq, &fc_stats->work, 0);
284                 return;
285         }
286
287         mlx5_free_fc(dev, counter);
288 }
289 EXPORT_SYMBOL(mlx5_fc_destroy);
290
291 int mlx5_init_fc_stats(struct mlx5_core_dev *dev)
292 {
293         struct mlx5_fc_stats *fc_stats = &dev->priv.fc_stats;
294
295         spin_lock_init(&fc_stats->counters_idr_lock);
296         idr_init(&fc_stats->counters_idr);
297         INIT_LIST_HEAD(&fc_stats->counters);
298         init_llist_head(&fc_stats->addlist);
299         init_llist_head(&fc_stats->dellist);
300
301         fc_stats->wq = create_singlethread_workqueue("mlx5_fc");
302         if (!fc_stats->wq)
303                 return -ENOMEM;
304
305         fc_stats->sampling_interval = MLX5_FC_STATS_PERIOD;
306         INIT_DELAYED_WORK(&fc_stats->work, mlx5_fc_stats_work);
307
308         return 0;
309 }
310
311 void mlx5_cleanup_fc_stats(struct mlx5_core_dev *dev)
312 {
313         struct mlx5_fc_stats *fc_stats = &dev->priv.fc_stats;
314         struct llist_node *tmplist;
315         struct mlx5_fc *counter;
316         struct mlx5_fc *tmp;
317
318         cancel_delayed_work_sync(&dev->priv.fc_stats.work);
319         destroy_workqueue(dev->priv.fc_stats.wq);
320         dev->priv.fc_stats.wq = NULL;
321
322         idr_destroy(&fc_stats->counters_idr);
323
324         tmplist = llist_del_all(&fc_stats->addlist);
325         llist_for_each_entry_safe(counter, tmp, tmplist, addlist)
326                 mlx5_free_fc(dev, counter);
327
328         list_for_each_entry_safe(counter, tmp, &fc_stats->counters, list)
329                 mlx5_free_fc(dev, counter);
330 }
331
332 int mlx5_fc_query(struct mlx5_core_dev *dev, struct mlx5_fc *counter,
333                   u64 *packets, u64 *bytes)
334 {
335         return mlx5_cmd_fc_query(dev, counter->id, packets, bytes);
336 }
337 EXPORT_SYMBOL(mlx5_fc_query);
338
339 void mlx5_fc_query_cached(struct mlx5_fc *counter,
340                           u64 *bytes, u64 *packets, u64 *lastuse)
341 {
342         struct mlx5_fc_cache c;
343
344         c = counter->cache;
345
346         *bytes = c.bytes - counter->lastbytes;
347         *packets = c.packets - counter->lastpackets;
348         *lastuse = c.lastuse;
349
350         counter->lastbytes = c.bytes;
351         counter->lastpackets = c.packets;
352 }
353
354 void mlx5_fc_queue_stats_work(struct mlx5_core_dev *dev,
355                               struct delayed_work *dwork,
356                               unsigned long delay)
357 {
358         struct mlx5_fc_stats *fc_stats = &dev->priv.fc_stats;
359
360         queue_delayed_work(fc_stats->wq, dwork, delay);
361 }
362
363 void mlx5_fc_update_sampling_interval(struct mlx5_core_dev *dev,
364                                       unsigned long interval)
365 {
366         struct mlx5_fc_stats *fc_stats = &dev->priv.fc_stats;
367
368         fc_stats->sampling_interval = min_t(unsigned long, interval,
369                                             fc_stats->sampling_interval);
370 }