Lines Matching +full:current +full:- +full:rotate

1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
16 if (!!log->ubuf != !!log->len_total) in bpf_verifier_log_attr_valid()
19 if (log->ubuf && log->level == 0) in bpf_verifier_log_attr_valid()
21 if (log->level & ~BPF_LOG_MASK) in bpf_verifier_log_attr_valid()
23 if (log->len_total > UINT_MAX >> 2) in bpf_verifier_log_attr_valid()
31 log->level = log_level; in bpf_vlog_init()
32 log->ubuf = log_buf; in bpf_vlog_init()
33 log->len_total = log_size; in bpf_vlog_init()
37 return -EINVAL; in bpf_vlog_init()
45 u64 len = log->end_pos + add_len; in bpf_vlog_update_len_max()
47 /* log->len_max could be larger than our current len due to in bpf_vlog_update_len_max()
52 log->len_max = UINT_MAX; in bpf_vlog_update_len_max()
53 else if (len > log->len_max) in bpf_vlog_update_len_max()
54 log->len_max = len; in bpf_vlog_update_len_max()
63 n = vscnprintf(log->kbuf, BPF_VERIFIER_TMP_LOG_SIZE, fmt, args); in bpf_verifier_vlog()
65 if (log->level == BPF_LOG_KERNEL) { in bpf_verifier_vlog()
66 bool newline = n > 0 && log->kbuf[n - 1] == '\n'; in bpf_verifier_vlog()
68 pr_err("BPF: %s%s", log->kbuf, newline ? "" : "\n"); in bpf_verifier_vlog()
75 if (log->level & BPF_LOG_FIXED) { in bpf_verifier_vlog()
78 if (log->end_pos < log->len_total) { in bpf_verifier_vlog()
79 new_n = min_t(u32, log->len_total - log->end_pos, n); in bpf_verifier_vlog()
80 log->kbuf[new_n - 1] = '\0'; in bpf_verifier_vlog()
83 cur_pos = log->end_pos; in bpf_verifier_vlog()
84 log->end_pos += n - 1; /* don't count terminating '\0' */ in bpf_verifier_vlog()
86 if (log->ubuf && new_n && in bpf_verifier_vlog()
87 copy_to_user(log->ubuf + cur_pos, log->kbuf, new_n)) in bpf_verifier_vlog()
93 new_end = log->end_pos + n; in bpf_verifier_vlog()
94 if (new_end - log->start_pos >= log->len_total) in bpf_verifier_vlog()
95 new_start = new_end - log->len_total; in bpf_verifier_vlog()
97 new_start = log->start_pos; in bpf_verifier_vlog()
99 log->start_pos = new_start; in bpf_verifier_vlog()
100 log->end_pos = new_end - 1; /* don't count terminating '\0' */ in bpf_verifier_vlog()
102 if (!log->ubuf) in bpf_verifier_vlog()
105 new_n = min(n, log->len_total); in bpf_verifier_vlog()
106 cur_pos = new_end - new_n; in bpf_verifier_vlog()
107 div_u64_rem(cur_pos, log->len_total, &buf_start); in bpf_verifier_vlog()
108 div_u64_rem(new_end, log->len_total, &buf_end); in bpf_verifier_vlog()
114 buf_end = log->len_total; in bpf_verifier_vlog()
124 if (copy_to_user(log->ubuf + buf_start, in bpf_verifier_vlog()
125 log->kbuf + n - new_n, in bpf_verifier_vlog()
126 buf_end - buf_start)) in bpf_verifier_vlog()
130 if (copy_to_user(log->ubuf + buf_start, in bpf_verifier_vlog()
131 log->kbuf + n - new_n, in bpf_verifier_vlog()
132 log->len_total - buf_start)) in bpf_verifier_vlog()
134 if (copy_to_user(log->ubuf, in bpf_verifier_vlog()
135 log->kbuf + n - buf_end, in bpf_verifier_vlog()
143 log->ubuf = NULL; in bpf_verifier_vlog()
151 if (WARN_ON_ONCE(new_pos > log->end_pos)) in bpf_vlog_reset()
154 if (!bpf_verifier_log_needed(log) || log->level == BPF_LOG_KERNEL) in bpf_vlog_reset()
157 /* if position to which we reset is beyond current log window, in bpf_vlog_reset()
161 log->end_pos = new_pos; in bpf_vlog_reset()
162 if (log->end_pos < log->start_pos) in bpf_vlog_reset()
163 log->start_pos = log->end_pos; in bpf_vlog_reset()
165 if (!log->ubuf) in bpf_vlog_reset()
168 if (log->level & BPF_LOG_FIXED) in bpf_vlog_reset()
169 pos = log->end_pos + 1; in bpf_vlog_reset()
171 div_u64_rem(new_pos, log->len_total, &pos); in bpf_vlog_reset()
173 if (pos < log->len_total && put_user(zero, log->ubuf + pos)) in bpf_vlog_reset()
174 log->ubuf = NULL; in bpf_vlog_reset()
181 for (i = 0, j = len - 1; i < j; i++, j--) in bpf_vlog_reverse_kbuf()
187 /* we split log->kbuf into two equal parts for both ends of array */ in bpf_vlog_reverse_ubuf()
188 int n = sizeof(log->kbuf) / 2, nn; in bpf_vlog_reverse_ubuf()
189 char *lbuf = log->kbuf, *rbuf = log->kbuf + n; in bpf_vlog_reverse_ubuf()
197 while (end - start > 1) { in bpf_vlog_reverse_ubuf()
198 nn = min(n, (end - start ) / 2); in bpf_vlog_reverse_ubuf()
200 if (copy_from_user(lbuf, log->ubuf + start, nn)) in bpf_vlog_reverse_ubuf()
201 return -EFAULT; in bpf_vlog_reverse_ubuf()
202 if (copy_from_user(rbuf, log->ubuf + end - nn, nn)) in bpf_vlog_reverse_ubuf()
203 return -EFAULT; in bpf_vlog_reverse_ubuf()
211 if (copy_to_user(log->ubuf + start, rbuf, nn)) in bpf_vlog_reverse_ubuf()
212 return -EFAULT; in bpf_vlog_reverse_ubuf()
213 if (copy_to_user(log->ubuf + end - nn, lbuf, nn)) in bpf_vlog_reverse_ubuf()
214 return -EFAULT; in bpf_vlog_reverse_ubuf()
217 end -= nn; in bpf_vlog_reverse_ubuf()
229 if (!log || log->level == 0 || log->level == BPF_LOG_KERNEL) in bpf_vlog_finalize()
232 if (!log->ubuf) in bpf_vlog_finalize()
235 if (log->start_pos == 0) in bpf_vlog_finalize()
238 /* Otherwise we need to rotate log contents to make it start from the in bpf_vlog_finalize()
239 * buffer beginning and be a continuous zero-terminated string. Note in bpf_vlog_finalize()
240 * that if log->start_pos != 0 then we definitely filled up entire log in bpf_vlog_finalize()
242 * the left by (log->start_pos % log->len_total) bytes. in bpf_vlog_finalize()
247 * do in-place array rotation. This is a leetcode-style problem, which in bpf_vlog_finalize()
257 * Then we rotate first 4 bytes (DCBA) and separately last 7 bytes in bpf_vlog_finalize()
261 * We'll utilize log->kbuf to read user memory chunk by chunk, swap in bpf_vlog_finalize()
262 * bytes, and write them back. Doing it byte-by-byte would be in bpf_vlog_finalize()
271 div_u64_rem(log->start_pos, log->len_total, &sublen); in bpf_vlog_finalize()
272 sublen = log->len_total - sublen; in bpf_vlog_finalize()
274 err = bpf_vlog_reverse_ubuf(log, 0, log->len_total); in bpf_vlog_finalize()
276 err = err ?: bpf_vlog_reverse_ubuf(log, sublen, log->len_total); in bpf_vlog_finalize()
278 log->ubuf = NULL; in bpf_vlog_finalize()
281 *log_size_actual = log->len_max; in bpf_vlog_finalize()
287 if (!!log->ubuf != !!log->len_total) in bpf_vlog_finalize()
288 return -EFAULT; in bpf_vlog_finalize()
291 if (log->ubuf && log->len_max > log->len_total) in bpf_vlog_finalize()
292 return -ENOSPC; in bpf_vlog_finalize()
306 if (!bpf_verifier_log_needed(&env->log)) in bpf_verifier_log_write()
310 bpf_verifier_vlog(&env->log, fmt, args); in bpf_verifier_log_write()