~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/bcachefs/util.c

Version: ~ [ linux-6.11-rc3 ] ~ [ linux-6.10.4 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.45 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.104 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.164 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.223 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.281 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.319 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0
  2 /*
  3  * random utility code, for bcache but in theory not specific to bcache
  4  *
  5  * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com>
  6  * Copyright 2012 Google, Inc.
  7  */
  8 
  9 #include <linux/bio.h>
 10 #include <linux/blkdev.h>
 11 #include <linux/console.h>
 12 #include <linux/ctype.h>
 13 #include <linux/debugfs.h>
 14 #include <linux/freezer.h>
 15 #include <linux/kthread.h>
 16 #include <linux/log2.h>
 17 #include <linux/math64.h>
 18 #include <linux/percpu.h>
 19 #include <linux/preempt.h>
 20 #include <linux/random.h>
 21 #include <linux/seq_file.h>
 22 #include <linux/string.h>
 23 #include <linux/types.h>
 24 #include <linux/sched/clock.h>
 25 
 26 #include "eytzinger.h"
 27 #include "mean_and_variance.h"
 28 #include "util.h"
 29 
 30 static const char si_units[] = "?kMGTPEZY";
 31 
 32 /* string_get_size units: */
 33 static const char *const units_2[] = {
 34         "B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"
 35 };
 36 static const char *const units_10[] = {
 37         "B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"
 38 };
 39 
 40 static int parse_u64(const char *cp, u64 *res)
 41 {
 42         const char *start = cp;
 43         u64 v = 0;
 44 
 45         if (!isdigit(*cp))
 46                 return -EINVAL;
 47 
 48         do {
 49                 if (v > U64_MAX / 10)
 50                         return -ERANGE;
 51                 v *= 10;
 52                 if (v > U64_MAX - (*cp - ''))
 53                         return -ERANGE;
 54                 v += *cp - '';
 55                 cp++;
 56         } while (isdigit(*cp));
 57 
 58         *res = v;
 59         return cp - start;
 60 }
 61 
 62 static int bch2_pow(u64 n, u64 p, u64 *res)
 63 {
 64         *res = 1;
 65 
 66         while (p--) {
 67                 if (*res > div_u64(U64_MAX, n))
 68                         return -ERANGE;
 69                 *res *= n;
 70         }
 71         return 0;
 72 }
 73 
 74 static int parse_unit_suffix(const char *cp, u64 *res)
 75 {
 76         const char *start = cp;
 77         u64 base = 1024;
 78         unsigned u;
 79         int ret;
 80 
 81         if (*cp == ' ')
 82                 cp++;
 83 
 84         for (u = 1; u < strlen(si_units); u++)
 85                 if (*cp == si_units[u]) {
 86                         cp++;
 87                         goto got_unit;
 88                 }
 89 
 90         for (u = 0; u < ARRAY_SIZE(units_2); u++)
 91                 if (!strncmp(cp, units_2[u], strlen(units_2[u]))) {
 92                         cp += strlen(units_2[u]);
 93                         goto got_unit;
 94                 }
 95 
 96         for (u = 0; u < ARRAY_SIZE(units_10); u++)
 97                 if (!strncmp(cp, units_10[u], strlen(units_10[u]))) {
 98                         cp += strlen(units_10[u]);
 99                         base = 1000;
100                         goto got_unit;
101                 }
102 
103         *res = 1;
104         return 0;
105 got_unit:
106         ret = bch2_pow(base, u, res);
107         if (ret)
108                 return ret;
109 
110         return cp - start;
111 }
112 
113 #define parse_or_ret(cp, _f)                    \
114 do {                                            \
115         int _ret = _f;                          \
116         if (_ret < 0)                           \
117                 return _ret;                    \
118         cp += _ret;                             \
119 } while (0)
120 
121 static int __bch2_strtou64_h(const char *cp, u64 *res)
122 {
123         const char *start = cp;
124         u64 v = 0, b, f_n = 0, f_d = 1;
125         int ret;
126 
127         parse_or_ret(cp, parse_u64(cp, &v));
128 
129         if (*cp == '.') {
130                 cp++;
131                 ret = parse_u64(cp, &f_n);
132                 if (ret < 0)
133                         return ret;
134                 cp += ret;
135 
136                 ret = bch2_pow(10, ret, &f_d);
137                 if (ret)
138                         return ret;
139         }
140 
141         parse_or_ret(cp, parse_unit_suffix(cp, &b));
142 
143         if (v > div_u64(U64_MAX, b))
144                 return -ERANGE;
145         v *= b;
146 
147         if (f_n > div_u64(U64_MAX, b))
148                 return -ERANGE;
149 
150         f_n = div_u64(f_n * b, f_d);
151         if (v + f_n < v)
152                 return -ERANGE;
153         v += f_n;
154 
155         *res = v;
156         return cp - start;
157 }
158 
159 static int __bch2_strtoh(const char *cp, u64 *res,
160                          u64 t_max, bool t_signed)
161 {
162         bool positive = *cp != '-';
163         u64 v = 0;
164 
165         if (*cp == '+' || *cp == '-')
166                 cp++;
167 
168         parse_or_ret(cp, __bch2_strtou64_h(cp, &v));
169 
170         if (*cp == '\n')
171                 cp++;
172         if (*cp)
173                 return -EINVAL;
174 
175         if (positive) {
176                 if (v > t_max)
177                         return -ERANGE;
178         } else {
179                 if (v && !t_signed)
180                         return -ERANGE;
181 
182                 if (v > t_max + 1)
183                         return -ERANGE;
184                 v = -v;
185         }
186 
187         *res = v;
188         return 0;
189 }
190 
191 #define STRTO_H(name, type)                                     \
192 int bch2_ ## name ## _h(const char *cp, type *res)              \
193 {                                                               \
194         u64 v = 0;                                              \
195         int ret = __bch2_strtoh(cp, &v, ANYSINT_MAX(type),      \
196                         ANYSINT_MAX(type) != ((type) ~0ULL));   \
197         *res = v;                                               \
198         return ret;                                             \
199 }
200 
201 STRTO_H(strtoint, int)
202 STRTO_H(strtouint, unsigned int)
203 STRTO_H(strtoll, long long)
204 STRTO_H(strtoull, unsigned long long)
205 STRTO_H(strtou64, u64)
206 
207 u64 bch2_read_flag_list(char *opt, const char * const list[])
208 {
209         u64 ret = 0;
210         char *p, *s, *d = kstrdup(opt, GFP_KERNEL);
211 
212         if (!d)
213                 return -ENOMEM;
214 
215         s = strim(d);
216 
217         while ((p = strsep(&s, ","))) {
218                 int flag = match_string(list, -1, p);
219 
220                 if (flag < 0) {
221                         ret = -1;
222                         break;
223                 }
224 
225                 ret |= 1 << flag;
226         }
227 
228         kfree(d);
229 
230         return ret;
231 }
232 
233 bool bch2_is_zero(const void *_p, size_t n)
234 {
235         const char *p = _p;
236         size_t i;
237 
238         for (i = 0; i < n; i++)
239                 if (p[i])
240                         return false;
241         return true;
242 }
243 
244 void bch2_prt_u64_base2_nbits(struct printbuf *out, u64 v, unsigned nr_bits)
245 {
246         while (nr_bits)
247                 prt_char(out, '' + ((v >> --nr_bits) & 1));
248 }
249 
250 void bch2_prt_u64_base2(struct printbuf *out, u64 v)
251 {
252         bch2_prt_u64_base2_nbits(out, v, fls64(v) ?: 1);
253 }
254 
255 static void __bch2_print_string_as_lines(const char *prefix, const char *lines,
256                                          bool nonblocking)
257 {
258         bool locked = false;
259         const char *p;
260 
261         if (!lines) {
262                 printk("%s (null)\n", prefix);
263                 return;
264         }
265 
266         if (!nonblocking) {
267                 console_lock();
268                 locked = true;
269         } else {
270                 locked = console_trylock();
271         }
272 
273         while (1) {
274                 p = strchrnul(lines, '\n');
275                 printk("%s%.*s\n", prefix, (int) (p - lines), lines);
276                 if (!*p)
277                         break;
278                 lines = p + 1;
279         }
280         if (locked)
281                 console_unlock();
282 }
283 
284 void bch2_print_string_as_lines(const char *prefix, const char *lines)
285 {
286         return __bch2_print_string_as_lines(prefix, lines, false);
287 }
288 
289 void bch2_print_string_as_lines_nonblocking(const char *prefix, const char *lines)
290 {
291         return __bch2_print_string_as_lines(prefix, lines, true);
292 }
293 
294 int bch2_save_backtrace(bch_stacktrace *stack, struct task_struct *task, unsigned skipnr,
295                         gfp_t gfp)
296 {
297 #ifdef CONFIG_STACKTRACE
298         unsigned nr_entries = 0;
299 
300         stack->nr = 0;
301         int ret = darray_make_room_gfp(stack, 32, gfp);
302         if (ret)
303                 return ret;
304 
305         if (!down_read_trylock(&task->signal->exec_update_lock))
306                 return -1;
307 
308         do {
309                 nr_entries = stack_trace_save_tsk(task, stack->data, stack->size, skipnr + 1);
310         } while (nr_entries == stack->size &&
311                  !(ret = darray_make_room_gfp(stack, stack->size * 2, gfp)));
312 
313         stack->nr = nr_entries;
314         up_read(&task->signal->exec_update_lock);
315 
316         return ret;
317 #else
318         return 0;
319 #endif
320 }
321 
322 void bch2_prt_backtrace(struct printbuf *out, bch_stacktrace *stack)
323 {
324         darray_for_each(*stack, i) {
325                 prt_printf(out, "[<0>] %pB", (void *) *i);
326                 prt_newline(out);
327         }
328 }
329 
330 int bch2_prt_task_backtrace(struct printbuf *out, struct task_struct *task, unsigned skipnr, gfp_t gfp)
331 {
332         bch_stacktrace stack = { 0 };
333         int ret = bch2_save_backtrace(&stack, task, skipnr + 1, gfp);
334 
335         bch2_prt_backtrace(out, &stack);
336         darray_exit(&stack);
337         return ret;
338 }
339 
340 #ifndef __KERNEL__
341 #include <time.h>
342 void bch2_prt_datetime(struct printbuf *out, time64_t sec)
343 {
344         time_t t = sec;
345         char buf[64];
346         ctime_r(&t, buf);
347         strim(buf);
348         prt_str(out, buf);
349 }
350 #else
351 void bch2_prt_datetime(struct printbuf *out, time64_t sec)
352 {
353         char buf[64];
354         snprintf(buf, sizeof(buf), "%ptT", &sec);
355         prt_u64(out, sec);
356 }
357 #endif
358 
359 void bch2_pr_time_units(struct printbuf *out, u64 ns)
360 {
361         const struct time_unit *u = bch2_pick_time_units(ns);
362 
363         prt_printf(out, "%llu %s", div_u64(ns, u->nsecs), u->name);
364 }
365 
366 static void bch2_pr_time_units_aligned(struct printbuf *out, u64 ns)
367 {
368         const struct time_unit *u = bch2_pick_time_units(ns);
369 
370         prt_printf(out, "%llu \r%s", div64_u64(ns, u->nsecs), u->name);
371 }
372 
373 static inline void pr_name_and_units(struct printbuf *out, const char *name, u64 ns)
374 {
375         prt_printf(out, "%s\t", name);
376         bch2_pr_time_units_aligned(out, ns);
377         prt_newline(out);
378 }
379 
380 #define TABSTOP_SIZE 12
381 
382 void bch2_time_stats_to_text(struct printbuf *out, struct bch2_time_stats *stats)
383 {
384         struct quantiles *quantiles = time_stats_to_quantiles(stats);
385         s64 f_mean = 0, d_mean = 0;
386         u64 f_stddev = 0, d_stddev = 0;
387 
388         if (stats->buffer) {
389                 int cpu;
390 
391                 spin_lock_irq(&stats->lock);
392                 for_each_possible_cpu(cpu)
393                         __bch2_time_stats_clear_buffer(stats, per_cpu_ptr(stats->buffer, cpu));
394                 spin_unlock_irq(&stats->lock);
395         }
396 
397         /*
398          * avoid divide by zero
399          */
400         if (stats->freq_stats.n) {
401                 f_mean = mean_and_variance_get_mean(stats->freq_stats);
402                 f_stddev = mean_and_variance_get_stddev(stats->freq_stats);
403                 d_mean = mean_and_variance_get_mean(stats->duration_stats);
404                 d_stddev = mean_and_variance_get_stddev(stats->duration_stats);
405         }
406 
407         printbuf_tabstop_push(out, out->indent + TABSTOP_SIZE);
408         prt_printf(out, "count:\t%llu\n", stats->duration_stats.n);
409         printbuf_tabstop_pop(out);
410 
411         printbuf_tabstops_reset(out);
412 
413         printbuf_tabstop_push(out, out->indent + 20);
414         printbuf_tabstop_push(out, TABSTOP_SIZE + 2);
415         printbuf_tabstop_push(out, 0);
416         printbuf_tabstop_push(out, TABSTOP_SIZE + 2);
417 
418         prt_printf(out, "\tsince mount\r\trecent\r\n");
419         prt_printf(out, "recent");
420 
421         printbuf_tabstops_reset(out);
422         printbuf_tabstop_push(out, out->indent + 20);
423         printbuf_tabstop_push(out, TABSTOP_SIZE);
424         printbuf_tabstop_push(out, 2);
425         printbuf_tabstop_push(out, TABSTOP_SIZE);
426 
427         prt_printf(out, "duration of events\n");
428         printbuf_indent_add(out, 2);
429 
430         pr_name_and_units(out, "min:", stats->min_duration);
431         pr_name_and_units(out, "max:", stats->max_duration);
432         pr_name_and_units(out, "total:", stats->total_duration);
433 
434         prt_printf(out, "mean:\t");
435         bch2_pr_time_units_aligned(out, d_mean);
436         prt_tab(out);
437         bch2_pr_time_units_aligned(out, mean_and_variance_weighted_get_mean(stats->duration_stats_weighted, TIME_STATS_MV_WEIGHT));
438         prt_newline(out);
439 
440         prt_printf(out, "stddev:\t");
441         bch2_pr_time_units_aligned(out, d_stddev);
442         prt_tab(out);
443         bch2_pr_time_units_aligned(out, mean_and_variance_weighted_get_stddev(stats->duration_stats_weighted, TIME_STATS_MV_WEIGHT));
444 
445         printbuf_indent_sub(out, 2);
446         prt_newline(out);
447 
448         prt_printf(out, "time between events\n");
449         printbuf_indent_add(out, 2);
450 
451         pr_name_and_units(out, "min:", stats->min_freq);
452         pr_name_and_units(out, "max:", stats->max_freq);
453 
454         prt_printf(out, "mean:\t");
455         bch2_pr_time_units_aligned(out, f_mean);
456         prt_tab(out);
457         bch2_pr_time_units_aligned(out, mean_and_variance_weighted_get_mean(stats->freq_stats_weighted, TIME_STATS_MV_WEIGHT));
458         prt_newline(out);
459 
460         prt_printf(out, "stddev:\t");
461         bch2_pr_time_units_aligned(out, f_stddev);
462         prt_tab(out);
463         bch2_pr_time_units_aligned(out, mean_and_variance_weighted_get_stddev(stats->freq_stats_weighted, TIME_STATS_MV_WEIGHT));
464 
465         printbuf_indent_sub(out, 2);
466         prt_newline(out);
467 
468         printbuf_tabstops_reset(out);
469 
470         if (quantiles) {
471                 int i = eytzinger0_first(NR_QUANTILES);
472                 const struct time_unit *u =
473                         bch2_pick_time_units(quantiles->entries[i].m);
474                 u64 last_q = 0;
475 
476                 prt_printf(out, "quantiles (%s):\t", u->name);
477                 eytzinger0_for_each(i, NR_QUANTILES) {
478                         bool is_last = eytzinger0_next(i, NR_QUANTILES) == -1;
479 
480                         u64 q = max(quantiles->entries[i].m, last_q);
481                         prt_printf(out, "%llu ", div_u64(q, u->nsecs));
482                         if (is_last)
483                                 prt_newline(out);
484                         last_q = q;
485                 }
486         }
487 }
488 
489 /* ratelimit: */
490 
491 /**
492  * bch2_ratelimit_delay() - return how long to delay until the next time to do
493  *              some work
494  * @d:          the struct bch_ratelimit to update
495  * Returns:     the amount of time to delay by, in jiffies
496  */
497 u64 bch2_ratelimit_delay(struct bch_ratelimit *d)
498 {
499         u64 now = local_clock();
500 
501         return time_after64(d->next, now)
502                 ? nsecs_to_jiffies(d->next - now)
503                 : 0;
504 }
505 
506 /**
507  * bch2_ratelimit_increment() - increment @d by the amount of work done
508  * @d:          the struct bch_ratelimit to update
509  * @done:       the amount of work done, in arbitrary units
510  */
511 void bch2_ratelimit_increment(struct bch_ratelimit *d, u64 done)
512 {
513         u64 now = local_clock();
514 
515         d->next += div_u64(done * NSEC_PER_SEC, d->rate);
516 
517         if (time_before64(now + NSEC_PER_SEC, d->next))
518                 d->next = now + NSEC_PER_SEC;
519 
520         if (time_after64(now - NSEC_PER_SEC * 2, d->next))
521                 d->next = now - NSEC_PER_SEC * 2;
522 }
523 
524 /* pd controller: */
525 
526 /*
527  * Updates pd_controller. Attempts to scale inputed values to units per second.
528  * @target: desired value
529  * @actual: current value
530  *
531  * @sign: 1 or -1; 1 if increasing the rate makes actual go up, -1 if increasing
532  * it makes actual go down.
533  */
534 void bch2_pd_controller_update(struct bch_pd_controller *pd,
535                               s64 target, s64 actual, int sign)
536 {
537         s64 proportional, derivative, change;
538 
539         unsigned long seconds_since_update = (jiffies - pd->last_update) / HZ;
540 
541         if (seconds_since_update == 0)
542                 return;
543 
544         pd->last_update = jiffies;
545 
546         proportional = actual - target;
547         proportional *= seconds_since_update;
548         proportional = div_s64(proportional, pd->p_term_inverse);
549 
550         derivative = actual - pd->last_actual;
551         derivative = div_s64(derivative, seconds_since_update);
552         derivative = ewma_add(pd->smoothed_derivative, derivative,
553                               (pd->d_term / seconds_since_update) ?: 1);
554         derivative = derivative * pd->d_term;
555         derivative = div_s64(derivative, pd->p_term_inverse);
556 
557         change = proportional + derivative;
558 
559         /* Don't increase rate if not keeping up */
560         if (change > 0 &&
561             pd->backpressure &&
562             time_after64(local_clock(),
563                          pd->rate.next + NSEC_PER_MSEC))
564                 change = 0;
565 
566         change *= (sign * -1);
567 
568         pd->rate.rate = clamp_t(s64, (s64) pd->rate.rate + change,
569                                 1, UINT_MAX);
570 
571         pd->last_actual         = actual;
572         pd->last_derivative     = derivative;
573         pd->last_proportional   = proportional;
574         pd->last_change         = change;
575         pd->last_target         = target;
576 }
577 
578 void bch2_pd_controller_init(struct bch_pd_controller *pd)
579 {
580         pd->rate.rate           = 1024;
581         pd->last_update         = jiffies;
582         pd->p_term_inverse      = 6000;
583         pd->d_term              = 30;
584         pd->d_smooth            = pd->d_term;
585         pd->backpressure        = 1;
586 }
587 
588 void bch2_pd_controller_debug_to_text(struct printbuf *out, struct bch_pd_controller *pd)
589 {
590         if (!out->nr_tabstops)
591                 printbuf_tabstop_push(out, 20);
592 
593         prt_printf(out, "rate:\t");
594         prt_human_readable_s64(out, pd->rate.rate);
595         prt_newline(out);
596 
597         prt_printf(out, "target:\t");
598         prt_human_readable_u64(out, pd->last_target);
599         prt_newline(out);
600 
601         prt_printf(out, "actual:\t");
602         prt_human_readable_u64(out, pd->last_actual);
603         prt_newline(out);
604 
605         prt_printf(out, "proportional:\t");
606         prt_human_readable_s64(out, pd->last_proportional);
607         prt_newline(out);
608 
609         prt_printf(out, "derivative:\t");
610         prt_human_readable_s64(out, pd->last_derivative);
611         prt_newline(out);
612 
613         prt_printf(out, "change:\t");
614         prt_human_readable_s64(out, pd->last_change);
615         prt_newline(out);
616 
617         prt_printf(out, "next io:\t%llims\n", div64_s64(pd->rate.next - local_clock(), NSEC_PER_MSEC));
618 }
619 
620 /* misc: */
621 
622 void bch2_bio_map(struct bio *bio, void *base, size_t size)
623 {
624         while (size) {
625                 struct page *page = is_vmalloc_addr(base)
626                                 ? vmalloc_to_page(base)
627                                 : virt_to_page(base);
628                 unsigned offset = offset_in_page(base);
629                 unsigned len = min_t(size_t, PAGE_SIZE - offset, size);
630 
631                 BUG_ON(!bio_add_page(bio, page, len, offset));
632                 size -= len;
633                 base += len;
634         }
635 }
636 
637 int bch2_bio_alloc_pages(struct bio *bio, size_t size, gfp_t gfp_mask)
638 {
639         while (size) {
640                 struct page *page = alloc_pages(gfp_mask, 0);
641                 unsigned len = min_t(size_t, PAGE_SIZE, size);
642 
643                 if (!page)
644                         return -ENOMEM;
645 
646                 if (unlikely(!bio_add_page(bio, page, len, 0))) {
647                         __free_page(page);
648                         break;
649                 }
650 
651                 size -= len;
652         }
653 
654         return 0;
655 }
656 
657 size_t bch2_rand_range(size_t max)
658 {
659         size_t rand;
660 
661         if (!max)
662                 return 0;
663 
664         do {
665                 rand = get_random_long();
666                 rand &= roundup_pow_of_two(max) - 1;
667         } while (rand >= max);
668 
669         return rand;
670 }
671 
672 void memcpy_to_bio(struct bio *dst, struct bvec_iter dst_iter, const void *src)
673 {
674         struct bio_vec bv;
675         struct bvec_iter iter;
676 
677         __bio_for_each_segment(bv, dst, iter, dst_iter) {
678                 void *dstp = kmap_local_page(bv.bv_page);
679 
680                 memcpy(dstp + bv.bv_offset, src, bv.bv_len);
681                 kunmap_local(dstp);
682 
683                 src += bv.bv_len;
684         }
685 }
686 
687 void memcpy_from_bio(void *dst, struct bio *src, struct bvec_iter src_iter)
688 {
689         struct bio_vec bv;
690         struct bvec_iter iter;
691 
692         __bio_for_each_segment(bv, src, iter, src_iter) {
693                 void *srcp = kmap_local_page(bv.bv_page);
694 
695                 memcpy(dst, srcp + bv.bv_offset, bv.bv_len);
696                 kunmap_local(srcp);
697 
698                 dst += bv.bv_len;
699         }
700 }
701 
702 #if 0
703 void eytzinger1_test(void)
704 {
705         unsigned inorder, eytz, size;
706 
707         pr_info("1 based eytzinger test:");
708 
709         for (size = 2;
710              size < 65536;
711              size++) {
712                 unsigned extra = eytzinger1_extra(size);
713 
714                 if (!(size % 4096))
715                         pr_info("tree size %u", size);
716 
717                 BUG_ON(eytzinger1_prev(0, size) != eytzinger1_last(size));
718                 BUG_ON(eytzinger1_next(0, size) != eytzinger1_first(size));
719 
720                 BUG_ON(eytzinger1_prev(eytzinger1_first(size), size)    != 0);
721                 BUG_ON(eytzinger1_next(eytzinger1_last(size), size)     != 0);
722 
723                 inorder = 1;
724                 eytzinger1_for_each(eytz, size) {
725                         BUG_ON(__inorder_to_eytzinger1(inorder, size, extra) != eytz);
726                         BUG_ON(__eytzinger1_to_inorder(eytz, size, extra) != inorder);
727                         BUG_ON(eytz != eytzinger1_last(size) &&
728                                eytzinger1_prev(eytzinger1_next(eytz, size), size) != eytz);
729 
730                         inorder++;
731                 }
732         }
733 }
734 
735 void eytzinger0_test(void)
736 {
737 
738         unsigned inorder, eytz, size;
739 
740         pr_info("0 based eytzinger test:");
741 
742         for (size = 1;
743              size < 65536;
744              size++) {
745                 unsigned extra = eytzinger0_extra(size);
746 
747                 if (!(size % 4096))
748                         pr_info("tree size %u", size);
749 
750                 BUG_ON(eytzinger0_prev(-1, size) != eytzinger0_last(size));
751                 BUG_ON(eytzinger0_next(-1, size) != eytzinger0_first(size));
752 
753                 BUG_ON(eytzinger0_prev(eytzinger0_first(size), size)    != -1);
754                 BUG_ON(eytzinger0_next(eytzinger0_last(size), size)     != -1);
755 
756                 inorder = 0;
757                 eytzinger0_for_each(eytz, size) {
758                         BUG_ON(__inorder_to_eytzinger0(inorder, size, extra) != eytz);
759                         BUG_ON(__eytzinger0_to_inorder(eytz, size, extra) != inorder);
760                         BUG_ON(eytz != eytzinger0_last(size) &&
761                                eytzinger0_prev(eytzinger0_next(eytz, size), size) != eytz);
762 
763                         inorder++;
764                 }
765         }
766 }
767 
768 static inline int cmp_u16(const void *_l, const void *_r, size_t size)
769 {
770         const u16 *l = _l, *r = _r;
771 
772         return (*l > *r) - (*r - *l);
773 }
774 
775 static void eytzinger0_find_test_val(u16 *test_array, unsigned nr, u16 search)
776 {
777         int i, c1 = -1, c2 = -1;
778         ssize_t r;
779 
780         r = eytzinger0_find_le(test_array, nr,
781                                sizeof(test_array[0]),
782                                cmp_u16, &search);
783         if (r >= 0)
784                 c1 = test_array[r];
785 
786         for (i = 0; i < nr; i++)
787                 if (test_array[i] <= search && test_array[i] > c2)
788                         c2 = test_array[i];
789 
790         if (c1 != c2) {
791                 eytzinger0_for_each(i, nr)
792                         pr_info("[%3u] = %12u", i, test_array[i]);
793                 pr_info("find_le(%2u) -> [%2zi] = %2i should be %2i",
794                         i, r, c1, c2);
795         }
796 }
797 
798 void eytzinger0_find_test(void)
799 {
800         unsigned i, nr, allocated = 1 << 12;
801         u16 *test_array = kmalloc_array(allocated, sizeof(test_array[0]), GFP_KERNEL);
802 
803         for (nr = 1; nr < allocated; nr++) {
804                 pr_info("testing %u elems", nr);
805 
806                 get_random_bytes(test_array, nr * sizeof(test_array[0]));
807                 eytzinger0_sort(test_array, nr, sizeof(test_array[0]), cmp_u16, NULL);
808 
809                 /* verify array is sorted correctly: */
810                 eytzinger0_for_each(i, nr)
811                         BUG_ON(i != eytzinger0_last(nr) &&
812                                test_array[i] > test_array[eytzinger0_next(i, nr)]);
813 
814                 for (i = 0; i < U16_MAX; i += 1 << 12)
815                         eytzinger0_find_test_val(test_array, nr, i);
816 
817                 for (i = 0; i < nr; i++) {
818                         eytzinger0_find_test_val(test_array, nr, test_array[i] - 1);
819                         eytzinger0_find_test_val(test_array, nr, test_array[i]);
820                         eytzinger0_find_test_val(test_array, nr, test_array[i] + 1);
821                 }
822         }
823 
824         kfree(test_array);
825 }
826 #endif
827 
828 /*
829  * Accumulate percpu counters onto one cpu's copy - only valid when access
830  * against any percpu counter is guarded against
831  */
832 u64 *bch2_acc_percpu_u64s(u64 __percpu *p, unsigned nr)
833 {
834         u64 *ret;
835         int cpu;
836 
837         /* access to pcpu vars has to be blocked by other locking */
838         preempt_disable();
839         ret = this_cpu_ptr(p);
840         preempt_enable();
841 
842         for_each_possible_cpu(cpu) {
843                 u64 *i = per_cpu_ptr(p, cpu);
844 
845                 if (i != ret) {
846                         acc_u64s(ret, i, nr);
847                         memset(i, 0, nr * sizeof(u64));
848                 }
849         }
850 
851         return ret;
852 }
853 
854 void bch2_darray_str_exit(darray_str *d)
855 {
856         darray_for_each(*d, i)
857                 kfree(*i);
858         darray_exit(d);
859 }
860 
861 int bch2_split_devs(const char *_dev_name, darray_str *ret)
862 {
863         darray_init(ret);
864 
865         char *dev_name, *s, *orig;
866 
867         dev_name = orig = kstrdup(_dev_name, GFP_KERNEL);
868         if (!dev_name)
869                 return -ENOMEM;
870 
871         while ((s = strsep(&dev_name, ":"))) {
872                 char *p = kstrdup(s, GFP_KERNEL);
873                 if (!p)
874                         goto err;
875 
876                 if (darray_push(ret, p)) {
877                         kfree(p);
878                         goto err;
879                 }
880         }
881 
882         kfree(orig);
883         return 0;
884 err:
885         bch2_darray_str_exit(ret);
886         kfree(orig);
887         return -ENOMEM;
888 }
889 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php