mars/kernel/lib_limiter.c
Thomas Schoebel-Theuer f24ea2752f limiter: fix divide by zero
Feb 10 04:41:59 t1 kernel: divide error: 0000 [#1] SMP KASAN NOPTI
Feb 10 04:41:59 t1 kernel: CPU: 5 PID: 13507 Comm: mars_copy7 Not tainted 5.4.86+ #237
Feb 10 04:41:59 t1 kernel: Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS rel-1.12.0-59-gc9ba527-rebuilt.opensuse.org 04/01/2014
Feb 10 04:41:59 t1 kernel: RIP: 0010:mars_limit+0x533/0x6bb [mars]
Feb 10 04:41:59 t1 kernel: Code: 15 48 63 95 e4 fe ff ff 48 ff c0 49 89 47 48 49 01 d5 4d 89 6f 50 49 69 47 50 00 ca 9a 3b 49 8d 7f 20 41 bd ff ff ff 7f 48 99 <49> f7 fc 48 3d ff ff ff 7f 4c 0f 4e e8 48 89 f8 48 c1 e8 03 8a 04
Feb 10 04:41:59 t1 kernel: RSP: 0018:ffff888033b87a58 EFLAGS: 00010212
Feb 10 04:41:59 t1 kernel: RAX: 00001a5e39dc8000 RBX: dffffc0000000000 RCX: 0000000000000017
Feb 10 04:41:59 t1 kernel: RDX: 0000000000000000 RSI: 0000000225c17d03 RDI: ffffffffa03bdce0
Feb 10 04:41:59 t1 kernel: RBP: ffff888033b87b98 R08: fffffbfff073f9ce R09: 0000000000000001
Feb 10 04:41:59 t1 kernel: R10: 0000000000000000 R11: ffffffff839fce6b R12: 0000000000000000
Feb 10 04:41:59 t1 kernel: R13: 000000007fffffff R14: 0000000025e89700 R15: ffffffffa03bdcc0
Feb 10 04:41:59 t1 kernel: FS:  0000000000000000(0000) GS:ffff88818b940000(0000) knlGS:0000000000000000
Feb 10 04:41:59 t1 kernel: CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
Feb 10 04:41:59 t1 kernel: CR2: 00005651d25b7b98 CR3: 0000000171668000 CR4: 0000000000340ee0
Feb 10 04:41:59 t1 kernel: Call Trace:
Feb 10 04:41:59 t1 kernel:  ? ranking_compute+0x1a8/0x1a8 [mars]
Feb 10 04:41:59 t1 kernel:  ? _brick_block_alloc+0x2ff/0x590 [mars]
Feb 10 04:41:59 t1 kernel:  ? __brick_block_alloc+0x8a/0x8a [mars]
Feb 10 04:41:59 t1 kernel:  ? kasan_unpoison_shadow+0xf/0x2e
Feb 10 04:41:59 t1 kernel:  ? _new_aspect+0x464/0x4ab [mars]
Feb 10 04:41:59 t1 kernel:  mars_limit_sleep+0xa/0x57 [mars]
Feb 10 04:41:59 t1 kernel:  bio_ref_get+0x762/0xc4c [mars]
Feb 10 04:41:59 t1 kernel:  _make_mref+0x48c/0x73e [mars]
Feb 10 04:41:59 t1 kernel:  _next_state+0x3f6/0xc4b [mars]
Feb 10 04:41:59 t1 kernel:  _run_copy+0x2cc/0x6f7 [mars]
Feb 10 04:41:59 t1 kernel:  _copy_thread+0x3dd/0x84c [mars]
Feb 10 04:41:59 t1 kernel:  ? _run_copy+0x6f7/0x6f7 [mars]
Feb 10 04:41:59 t1 kernel:  ? lock_downgrade+0x55b/0x55b
Feb 10 04:41:59 t1 kernel:  ? do_raw_spin_lock+0x144/0x1ef
Feb 10 04:41:59 t1 kernel:  ? atomic_add_return.constprop.2+0x1e/0x1e
Feb 10 04:41:59 t1 kernel:  ? add_wait_queue+0x85/0x85
Feb 10 04:41:59 t1 kernel:  ? lockdep_hardirqs_on+0x433/0x482
Feb 10 04:41:59 t1 kernel:  ? test_bit+0x1d/0x27
Feb 10 04:41:59 t1 kernel:  kthread+0x2be/0x2cd
Feb 10 04:41:59 t1 kernel:  ? _run_copy+0x6f7/0x6f7 [mars]
Feb 10 04:41:59 t1 kernel:  ? kthread_worker_fn+0x3ef/0x3ef
Feb 10 04:41:59 t1 kernel:  ret_from_fork+0x27/0x50
Feb 10 04:41:59 t1 kernel: Modules linked in: mars cls_u32 sch_netem sch_tbf sch_prio xt_mark lz4_compress lz4_decompress virtio_console crct10dif_pclmul virtio_balloon crc32_pclmul usbhid virtio_net net_failover failover virtio_blk xhci_pci xhci_hcd crc32c_intel usbcore virtio_pci virtio_ring usb_common virtio [last unloaded: mars]
Feb 10 04:41:59 t1 kernel: ---[ end trace e009385d7938b1c4 ]---
2021-02-10 13:31:55 +01:00

210 lines
5.9 KiB
C

/*
* MARS Long Distance Replication Software
*
* This file is part of MARS project: http://schoebel.github.io/mars/
*
* Copyright (C) 2010-2014 Thomas Schoebel-Theuer
* Copyright (C) 2011-2014 1&1 Internet AG
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "lib_limiter.h"
#include "lamport.h"
#include <linux/kernel.h>
#include <linux/module.h>
/* For precisions, _internal_ time is in multiples of the following basic time units */
#define LIMITER_TIME_RESOLUTION NSEC_PER_SEC
#define DEFAULT_MIN_WINDOW (LIMITER_TIME_RESOLUTION * 1)
#define DEFAULT_MAX_WINDOW (LIMITER_TIME_RESOLUTION * 4)
#define MIN_DIVIDER (DEFAULT_MIN_WINDOW / 10)
#define MAX_DIVIDER (DEFAULT_MAX_WINDOW * 10)
#define CLAMP_WINDOW(x) \
(x) < MIN_DIVIDER ? MIN_DIVIDER : \
(x) > MAX_DIVIDER ? MAX_DIVIDER : \
(x)
#define MS_TO_TR(x) ((__s64)(x) * (LIMITER_TIME_RESOLUTION / 1000))
#define TR_TO_MS(x) ((x) / (LIMITER_TIME_RESOLUTION / 1000))
int mars_limit(struct mars_limiter *lim, int amount)
{
int delay = 0;
struct lamport_time now;
if (unlikely(amount < 0))
amount = 0;
now = get_real_lamport();
/* Compute the maximum delay along the path
* down to the root of the hierarchy tree.
*/
while (lim != NULL) {
struct lamport_time diff = lamport_time_sub(now, lim->lim_stamp);
__s64 window = lamport_time_to_ns(&diff);
__s64 rate_raw;
int rate;
int max_rate;
/* Sometimes, raw CPU clocks may do weired things...
* Small windows in the denominator could fake unrealistic rates.
* Do not divide by too small numbers.
*/
window = CLAMP_WINDOW(window);
if (unlikely(lim->lim_min_window_ms <= TR_TO_MS(MAX_DIVIDER)))
lim->lim_min_window_ms = TR_TO_MS(DEFAULT_MIN_WINDOW);
if (unlikely(lim->lim_max_window_ms <= lim->lim_min_window_ms))
lim->lim_max_window_ms = lim->lim_min_window_ms + TR_TO_MS(DEFAULT_MAX_WINDOW);
/* Update total statistics.
* They will intentionally wrap around.
* Userspace must take care of that.
*/
if (likely(amount > 0)) {
lim->lim_total_amount += amount;
lim->lim_total_ops++;
}
/* Only use incremental accumulation at repeated calls, but
* never after longer pauses.
*/
if (!lim->lim_stamp.tv_sec ||
window > MS_TO_TR(lim->lim_max_window_ms)) {
/* reset, start over with new measurement cycle */
memset(&diff, 0, sizeof(diff));
lim->lim_stamp = now;
lim->lim_ops_accu = 0;
lim->lim_amount_accu = 0;
lim->lim_ops_rate = 0;
lim->lim_amount_rate = 0;
window = MIN_DIVIDER;
} else {
__s64 diff_window;
/* Try to keep the window between min_window and 2 * min_window.
* We wait until min_window has been exceeded _twice_,
* and then reduce the window by only 1 * min_window.
*/
diff_window = window - MS_TO_TR(lim->lim_min_window_ms);
if (diff_window > MS_TO_TR(lim->lim_min_window_ms)) {
__s64 used_up;
__s64 add_window = 0;
used_up = lim->lim_amount_accu * diff_window / window;
if (used_up > 0) {
add_window = diff_window;
lim->lim_amount_accu -= used_up;
if (unlikely(lim->lim_amount_accu < 0))
lim->lim_amount_accu = 0;
}
used_up = lim->lim_ops_accu * diff_window / window;
if (used_up > 0) {
if (diff_window > add_window)
add_window = diff_window;
lim->lim_ops_accu -= used_up;
if (unlikely(lim->lim_ops_accu < 0))
lim->lim_ops_accu = 0;
}
if (add_window > 0) {
lamport_time_add_ns(&lim->lim_stamp, add_window);
/* recompute the new window */
diff = lamport_time_sub(now, lim->lim_stamp);
window = lamport_time_to_ns(&diff);
window = CLAMP_WINDOW(window);
}
}
}
/* Races are possible, but taken into account.
* There is no real harm from rarely lost updates.
*/
if (likely(amount > 0)) {
lim->lim_amount_accu += amount;
lim->lim_ops_accu++;
}
/* compute amount values */
rate_raw = lim->lim_amount_accu * LIMITER_TIME_RESOLUTION / window;
rate = rate_raw;
if (unlikely(rate_raw > INT_MAX)) {
rate = INT_MAX;
}
lim->lim_amount_rate = rate;
/* amount limit exceeded? */
max_rate = lim->lim_max_amount_rate;
if (max_rate > 0 && rate > max_rate) {
int this_delay = (window * rate / max_rate - window);
// compute maximum
if (this_delay > delay && this_delay > 0)
delay = this_delay;
}
/* compute ops values */
rate_raw = lim->lim_ops_accu * LIMITER_TIME_RESOLUTION / window;
rate = rate_raw;
if (unlikely(rate_raw > INT_MAX)) {
rate = INT_MAX;
}
lim->lim_ops_rate = rate;
/* ops limit exceeded? */
max_rate = lim->lim_max_ops_rate;
if (max_rate > 0 && rate > max_rate) {
int this_delay = (window * rate / max_rate - window);
// compute maximum
if (this_delay > delay && this_delay > 0)
delay = this_delay;
}
lim = lim->lim_father;
}
return TR_TO_MS(delay);
}
void mars_limit_sleep(struct mars_limiter *lim, int amount)
{
int sleep = mars_limit(lim, amount);
if (sleep > 0) {
if (unlikely(lim->lim_max_delay_ms <= 0))
lim->lim_max_delay_ms = 1000;
if (sleep > lim->lim_max_delay_ms)
sleep = lim->lim_max_delay_ms;
brick_msleep(sleep);
}
}
void mars_limit_reset(struct mars_limiter *lim)
{
if (!lim)
return;
memset(&lim->lim_stamp, 0, sizeof(lim->lim_stamp));
mars_limit(lim, 0);
}