aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/Kconfig3
-rw-r--r--lib/Kconfig.debug3
-rw-r--r--lib/Makefile1
-rw-r--r--lib/bitmap.c5
-rw-r--r--lib/bucket_locks.c5
-rw-r--r--lib/idr.c10
-rw-r--r--lib/kobject_uevent.c178
-rw-r--r--lib/mpi/mpi-internal.h75
-rw-r--r--lib/percpu_ida.c63
-rw-r--r--lib/reed_solomon/decode_rs.c34
-rw-r--r--lib/reed_solomon/encode_rs.c15
-rw-r--r--lib/reed_solomon/reed_solomon.c240
-rw-r--r--lib/rhashtable.c51
-rw-r--r--lib/test_bpf.c595
-rw-r--r--lib/test_overflow.c417
-rw-r--r--lib/test_printf.c2
-rw-r--r--lib/ucs2_string.c2
-rw-r--r--lib/vsprintf.c133
18 files changed, 1246 insertions, 586 deletions
diff --git a/lib/Kconfig b/lib/Kconfig
index 7a913937888b..abc111eb5054 100644
--- a/lib/Kconfig
+++ b/lib/Kconfig
@@ -621,6 +621,9 @@ config ARCH_HAS_PMEM_API
621config ARCH_HAS_UACCESS_FLUSHCACHE 621config ARCH_HAS_UACCESS_FLUSHCACHE
622 bool 622 bool
623 623
624config ARCH_HAS_UACCESS_MCSAFE
625 bool
626
624config STACKDEPOT 627config STACKDEPOT
625 bool 628 bool
626 select STACKTRACE 629 select STACKTRACE
diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
index 76555479ae36..eb885942eb0f 100644
--- a/lib/Kconfig.debug
+++ b/lib/Kconfig.debug
@@ -1802,6 +1802,9 @@ config TEST_BITMAP
1802config TEST_UUID 1802config TEST_UUID
1803 tristate "Test functions located in the uuid module at runtime" 1803 tristate "Test functions located in the uuid module at runtime"
1804 1804
1805config TEST_OVERFLOW
1806 tristate "Test check_*_overflow() functions at runtime"
1807
1805config TEST_RHASHTABLE 1808config TEST_RHASHTABLE
1806 tristate "Perform selftest on resizable hash table" 1809 tristate "Perform selftest on resizable hash table"
1807 default n 1810 default n
diff --git a/lib/Makefile b/lib/Makefile
index 9f18c8152281..84c6dcb31fbb 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -60,6 +60,7 @@ UBSAN_SANITIZE_test_ubsan.o := y
60obj-$(CONFIG_TEST_KSTRTOX) += test-kstrtox.o 60obj-$(CONFIG_TEST_KSTRTOX) += test-kstrtox.o
61obj-$(CONFIG_TEST_LIST_SORT) += test_list_sort.o 61obj-$(CONFIG_TEST_LIST_SORT) += test_list_sort.o
62obj-$(CONFIG_TEST_LKM) += test_module.o 62obj-$(CONFIG_TEST_LKM) += test_module.o
63obj-$(CONFIG_TEST_OVERFLOW) += test_overflow.o
63obj-$(CONFIG_TEST_RHASHTABLE) += test_rhashtable.o 64obj-$(CONFIG_TEST_RHASHTABLE) += test_rhashtable.o
64obj-$(CONFIG_TEST_SORT) += test_sort.o 65obj-$(CONFIG_TEST_SORT) += test_sort.o
65obj-$(CONFIG_TEST_USER_COPY) += test_user_copy.o 66obj-$(CONFIG_TEST_USER_COPY) += test_user_copy.o
diff --git a/lib/bitmap.c b/lib/bitmap.c
index a42eff7e8c48..58f9750e49c6 100644
--- a/lib/bitmap.c
+++ b/lib/bitmap.c
@@ -64,12 +64,9 @@ EXPORT_SYMBOL(__bitmap_equal);
64 64
65void __bitmap_complement(unsigned long *dst, const unsigned long *src, unsigned int bits) 65void __bitmap_complement(unsigned long *dst, const unsigned long *src, unsigned int bits)
66{ 66{
67 unsigned int k, lim = bits/BITS_PER_LONG; 67 unsigned int k, lim = BITS_TO_LONGS(bits);
68 for (k = 0; k < lim; ++k) 68 for (k = 0; k < lim; ++k)
69 dst[k] = ~src[k]; 69 dst[k] = ~src[k];
70
71 if (bits % BITS_PER_LONG)
72 dst[k] = ~src[k];
73} 70}
74EXPORT_SYMBOL(__bitmap_complement); 71EXPORT_SYMBOL(__bitmap_complement);
75 72
diff --git a/lib/bucket_locks.c b/lib/bucket_locks.c
index 266a97c5708b..ade3ce6c4af6 100644
--- a/lib/bucket_locks.c
+++ b/lib/bucket_locks.c
@@ -30,10 +30,7 @@ int alloc_bucket_spinlocks(spinlock_t **locks, unsigned int *locks_mask,
30 } 30 }
31 31
32 if (sizeof(spinlock_t) != 0) { 32 if (sizeof(spinlock_t) != 0) {
33 if (gfpflags_allow_blocking(gfp)) 33 tlocks = kvmalloc_array(size, sizeof(spinlock_t), gfp);
34 tlocks = kvmalloc(size * sizeof(spinlock_t), gfp);
35 else
36 tlocks = kmalloc_array(size, sizeof(spinlock_t), gfp);
37 if (!tlocks) 34 if (!tlocks)
38 return -ENOMEM; 35 return -ENOMEM;
39 for (i = 0; i < size; i++) 36 for (i = 0; i < size; i++)
diff --git a/lib/idr.c b/lib/idr.c
index 823b813f08f8..ed9c169c12bd 100644
--- a/lib/idr.c
+++ b/lib/idr.c
@@ -4,9 +4,9 @@
4#include <linux/idr.h> 4#include <linux/idr.h>
5#include <linux/slab.h> 5#include <linux/slab.h>
6#include <linux/spinlock.h> 6#include <linux/spinlock.h>
7#include <linux/xarray.h>
7 8
8DEFINE_PER_CPU(struct ida_bitmap *, ida_bitmap); 9DEFINE_PER_CPU(struct ida_bitmap *, ida_bitmap);
9static DEFINE_SPINLOCK(simple_ida_lock);
10 10
11/** 11/**
12 * idr_alloc_u32() - Allocate an ID. 12 * idr_alloc_u32() - Allocate an ID.
@@ -581,7 +581,7 @@ again:
581 if (!ida_pre_get(ida, gfp_mask)) 581 if (!ida_pre_get(ida, gfp_mask))
582 return -ENOMEM; 582 return -ENOMEM;
583 583
584 spin_lock_irqsave(&simple_ida_lock, flags); 584 xa_lock_irqsave(&ida->ida_rt, flags);
585 ret = ida_get_new_above(ida, start, &id); 585 ret = ida_get_new_above(ida, start, &id);
586 if (!ret) { 586 if (!ret) {
587 if (id > max) { 587 if (id > max) {
@@ -591,7 +591,7 @@ again:
591 ret = id; 591 ret = id;
592 } 592 }
593 } 593 }
594 spin_unlock_irqrestore(&simple_ida_lock, flags); 594 xa_unlock_irqrestore(&ida->ida_rt, flags);
595 595
596 if (unlikely(ret == -EAGAIN)) 596 if (unlikely(ret == -EAGAIN))
597 goto again; 597 goto again;
@@ -615,8 +615,8 @@ void ida_simple_remove(struct ida *ida, unsigned int id)
615 unsigned long flags; 615 unsigned long flags;
616 616
617 BUG_ON((int)id < 0); 617 BUG_ON((int)id < 0);
618 spin_lock_irqsave(&simple_ida_lock, flags); 618 xa_lock_irqsave(&ida->ida_rt, flags);
619 ida_remove(ida, id); 619 ida_remove(ida, id);
620 spin_unlock_irqrestore(&simple_ida_lock, flags); 620 xa_unlock_irqrestore(&ida->ida_rt, flags);
621} 621}
622EXPORT_SYMBOL(ida_simple_remove); 622EXPORT_SYMBOL(ida_simple_remove);
diff --git a/lib/kobject_uevent.c b/lib/kobject_uevent.c
index 15ea216a67ce..63d0816ab23b 100644
--- a/lib/kobject_uevent.c
+++ b/lib/kobject_uevent.c
@@ -22,6 +22,7 @@
22#include <linux/socket.h> 22#include <linux/socket.h>
23#include <linux/skbuff.h> 23#include <linux/skbuff.h>
24#include <linux/netlink.h> 24#include <linux/netlink.h>
25#include <linux/uidgid.h>
25#include <linux/uuid.h> 26#include <linux/uuid.h>
26#include <linux/ctype.h> 27#include <linux/ctype.h>
27#include <net/sock.h> 28#include <net/sock.h>
@@ -231,30 +232,6 @@ out:
231 return r; 232 return r;
232} 233}
233 234
234#ifdef CONFIG_NET
235static int kobj_bcast_filter(struct sock *dsk, struct sk_buff *skb, void *data)
236{
237 struct kobject *kobj = data, *ksobj;
238 const struct kobj_ns_type_operations *ops;
239
240 ops = kobj_ns_ops(kobj);
241 if (!ops && kobj->kset) {
242 ksobj = &kobj->kset->kobj;
243 if (ksobj->parent != NULL)
244 ops = kobj_ns_ops(ksobj->parent);
245 }
246
247 if (ops && ops->netlink_ns && kobj->ktype->namespace) {
248 const void *sock_ns, *ns;
249 ns = kobj->ktype->namespace(kobj);
250 sock_ns = ops->netlink_ns(dsk);
251 return sock_ns != ns;
252 }
253
254 return 0;
255}
256#endif
257
258#ifdef CONFIG_UEVENT_HELPER 235#ifdef CONFIG_UEVENT_HELPER
259static int kobj_usermode_filter(struct kobject *kobj) 236static int kobj_usermode_filter(struct kobject *kobj)
260{ 237{
@@ -296,15 +273,44 @@ static void cleanup_uevent_env(struct subprocess_info *info)
296} 273}
297#endif 274#endif
298 275
299static int kobject_uevent_net_broadcast(struct kobject *kobj, 276#ifdef CONFIG_NET
300 struct kobj_uevent_env *env, 277static struct sk_buff *alloc_uevent_skb(struct kobj_uevent_env *env,
301 const char *action_string, 278 const char *action_string,
302 const char *devpath) 279 const char *devpath)
303{ 280{
304 int retval = 0; 281 struct netlink_skb_parms *parms;
305#if defined(CONFIG_NET) 282 struct sk_buff *skb = NULL;
283 char *scratch;
284 size_t len;
285
286 /* allocate message with maximum possible size */
287 len = strlen(action_string) + strlen(devpath) + 2;
288 skb = alloc_skb(len + env->buflen, GFP_KERNEL);
289 if (!skb)
290 return NULL;
291
292 /* add header */
293 scratch = skb_put(skb, len);
294 sprintf(scratch, "%s@%s", action_string, devpath);
295
296 skb_put_data(skb, env->buf, env->buflen);
297
298 parms = &NETLINK_CB(skb);
299 parms->creds.uid = GLOBAL_ROOT_UID;
300 parms->creds.gid = GLOBAL_ROOT_GID;
301 parms->dst_group = 1;
302 parms->portid = 0;
303
304 return skb;
305}
306
307static int uevent_net_broadcast_untagged(struct kobj_uevent_env *env,
308 const char *action_string,
309 const char *devpath)
310{
306 struct sk_buff *skb = NULL; 311 struct sk_buff *skb = NULL;
307 struct uevent_sock *ue_sk; 312 struct uevent_sock *ue_sk;
313 int retval = 0;
308 314
309 /* send netlink message */ 315 /* send netlink message */
310 list_for_each_entry(ue_sk, &uevent_sock_list, list) { 316 list_for_each_entry(ue_sk, &uevent_sock_list, list) {
@@ -314,37 +320,99 @@ static int kobject_uevent_net_broadcast(struct kobject *kobj,
314 continue; 320 continue;
315 321
316 if (!skb) { 322 if (!skb) {
317 /* allocate message with the maximum possible size */
318 size_t len = strlen(action_string) + strlen(devpath) + 2;
319 char *scratch;
320
321 retval = -ENOMEM; 323 retval = -ENOMEM;
322 skb = alloc_skb(len + env->buflen, GFP_KERNEL); 324 skb = alloc_uevent_skb(env, action_string, devpath);
323 if (!skb) 325 if (!skb)
324 continue; 326 continue;
325
326 /* add header */
327 scratch = skb_put(skb, len);
328 sprintf(scratch, "%s@%s", action_string, devpath);
329
330 skb_put_data(skb, env->buf, env->buflen);
331
332 NETLINK_CB(skb).dst_group = 1;
333 } 327 }
334 328
335 retval = netlink_broadcast_filtered(uevent_sock, skb_get(skb), 329 retval = netlink_broadcast(uevent_sock, skb_get(skb), 0, 1,
336 0, 1, GFP_KERNEL, 330 GFP_KERNEL);
337 kobj_bcast_filter,
338 kobj);
339 /* ENOBUFS should be handled in userspace */ 331 /* ENOBUFS should be handled in userspace */
340 if (retval == -ENOBUFS || retval == -ESRCH) 332 if (retval == -ENOBUFS || retval == -ESRCH)
341 retval = 0; 333 retval = 0;
342 } 334 }
343 consume_skb(skb); 335 consume_skb(skb);
344#endif 336
345 return retval; 337 return retval;
346} 338}
347 339
340static int uevent_net_broadcast_tagged(struct sock *usk,
341 struct kobj_uevent_env *env,
342 const char *action_string,
343 const char *devpath)
344{
345 struct user_namespace *owning_user_ns = sock_net(usk)->user_ns;
346 struct sk_buff *skb = NULL;
347 int ret = 0;
348
349 skb = alloc_uevent_skb(env, action_string, devpath);
350 if (!skb)
351 return -ENOMEM;
352
353 /* fix credentials */
354 if (owning_user_ns != &init_user_ns) {
355 struct netlink_skb_parms *parms = &NETLINK_CB(skb);
356 kuid_t root_uid;
357 kgid_t root_gid;
358
359 /* fix uid */
360 root_uid = make_kuid(owning_user_ns, 0);
361 if (uid_valid(root_uid))
362 parms->creds.uid = root_uid;
363
364 /* fix gid */
365 root_gid = make_kgid(owning_user_ns, 0);
366 if (gid_valid(root_gid))
367 parms->creds.gid = root_gid;
368 }
369
370 ret = netlink_broadcast(usk, skb, 0, 1, GFP_KERNEL);
371 /* ENOBUFS should be handled in userspace */
372 if (ret == -ENOBUFS || ret == -ESRCH)
373 ret = 0;
374
375 return ret;
376}
377#endif
378
379static int kobject_uevent_net_broadcast(struct kobject *kobj,
380 struct kobj_uevent_env *env,
381 const char *action_string,
382 const char *devpath)
383{
384 int ret = 0;
385
386#ifdef CONFIG_NET
387 const struct kobj_ns_type_operations *ops;
388 const struct net *net = NULL;
389
390 ops = kobj_ns_ops(kobj);
391 if (!ops && kobj->kset) {
392 struct kobject *ksobj = &kobj->kset->kobj;
393 if (ksobj->parent != NULL)
394 ops = kobj_ns_ops(ksobj->parent);
395 }
396
397 /* kobjects currently only carry network namespace tags and they
398 * are the only tag relevant here since we want to decide which
399 * network namespaces to broadcast the uevent into.
400 */
401 if (ops && ops->netlink_ns && kobj->ktype->namespace)
402 if (ops->type == KOBJ_NS_TYPE_NET)
403 net = kobj->ktype->namespace(kobj);
404
405 if (!net)
406 ret = uevent_net_broadcast_untagged(env, action_string,
407 devpath);
408 else
409 ret = uevent_net_broadcast_tagged(net->uevent_sock->sk, env,
410 action_string, devpath);
411#endif
412
413 return ret;
414}
415
348static void zap_modalias_env(struct kobj_uevent_env *env) 416static void zap_modalias_env(struct kobj_uevent_env *env)
349{ 417{
350 static const char modalias_prefix[] = "MODALIAS="; 418 static const char modalias_prefix[] = "MODALIAS=";
@@ -703,9 +771,13 @@ static int uevent_net_init(struct net *net)
703 771
704 net->uevent_sock = ue_sk; 772 net->uevent_sock = ue_sk;
705 773
706 mutex_lock(&uevent_sock_mutex); 774 /* Restrict uevents to initial user namespace. */
707 list_add_tail(&ue_sk->list, &uevent_sock_list); 775 if (sock_net(ue_sk->sk)->user_ns == &init_user_ns) {
708 mutex_unlock(&uevent_sock_mutex); 776 mutex_lock(&uevent_sock_mutex);
777 list_add_tail(&ue_sk->list, &uevent_sock_list);
778 mutex_unlock(&uevent_sock_mutex);
779 }
780
709 return 0; 781 return 0;
710} 782}
711 783
@@ -713,9 +785,11 @@ static void uevent_net_exit(struct net *net)
713{ 785{
714 struct uevent_sock *ue_sk = net->uevent_sock; 786 struct uevent_sock *ue_sk = net->uevent_sock;
715 787
716 mutex_lock(&uevent_sock_mutex); 788 if (sock_net(ue_sk->sk)->user_ns == &init_user_ns) {
717 list_del(&ue_sk->list); 789 mutex_lock(&uevent_sock_mutex);
718 mutex_unlock(&uevent_sock_mutex); 790 list_del(&ue_sk->list);
791 mutex_unlock(&uevent_sock_mutex);
792 }
719 793
720 netlink_kernel_release(ue_sk->sk); 794 netlink_kernel_release(ue_sk->sk);
721 kfree(ue_sk); 795 kfree(ue_sk);
diff --git a/lib/mpi/mpi-internal.h b/lib/mpi/mpi-internal.h
index 7eceeddb3fb8..c2d6f4efcfbc 100644
--- a/lib/mpi/mpi-internal.h
+++ b/lib/mpi/mpi-internal.h
@@ -65,13 +65,6 @@
65typedef mpi_limb_t *mpi_ptr_t; /* pointer to a limb */ 65typedef mpi_limb_t *mpi_ptr_t; /* pointer to a limb */
66typedef int mpi_size_t; /* (must be a signed type) */ 66typedef int mpi_size_t; /* (must be a signed type) */
67 67
68static inline int RESIZE_IF_NEEDED(MPI a, unsigned b)
69{
70 if (a->alloced < b)
71 return mpi_resize(a, b);
72 return 0;
73}
74
75/* Copy N limbs from S to D. */ 68/* Copy N limbs from S to D. */
76#define MPN_COPY(d, s, n) \ 69#define MPN_COPY(d, s, n) \
77 do { \ 70 do { \
@@ -80,13 +73,6 @@ static inline int RESIZE_IF_NEEDED(MPI a, unsigned b)
80 (d)[_i] = (s)[_i]; \ 73 (d)[_i] = (s)[_i]; \
81 } while (0) 74 } while (0)
82 75
83#define MPN_COPY_INCR(d, s, n) \
84 do { \
85 mpi_size_t _i; \
86 for (_i = 0; _i < (n); _i++) \
87 (d)[_i] = (s)[_i]; \
88 } while (0)
89
90#define MPN_COPY_DECR(d, s, n) \ 76#define MPN_COPY_DECR(d, s, n) \
91 do { \ 77 do { \
92 mpi_size_t _i; \ 78 mpi_size_t _i; \
@@ -111,15 +97,6 @@ static inline int RESIZE_IF_NEEDED(MPI a, unsigned b)
111 } \ 97 } \
112 } while (0) 98 } while (0)
113 99
114#define MPN_NORMALIZE_NOT_ZERO(d, n) \
115 do { \
116 for (;;) { \
117 if ((d)[(n)-1]) \
118 break; \
119 (n)--; \
120 } \
121 } while (0)
122
123#define MPN_MUL_N_RECURSE(prodp, up, vp, size, tspace) \ 100#define MPN_MUL_N_RECURSE(prodp, up, vp, size, tspace) \
124 do { \ 101 do { \
125 if ((size) < KARATSUBA_THRESHOLD) \ 102 if ((size) < KARATSUBA_THRESHOLD) \
@@ -128,46 +105,11 @@ static inline int RESIZE_IF_NEEDED(MPI a, unsigned b)
128 mul_n(prodp, up, vp, size, tspace); \ 105 mul_n(prodp, up, vp, size, tspace); \
129 } while (0); 106 } while (0);
130 107
131/* Divide the two-limb number in (NH,,NL) by D, with DI being the largest
132 * limb not larger than (2**(2*BITS_PER_MP_LIMB))/D - (2**BITS_PER_MP_LIMB).
133 * If this would yield overflow, DI should be the largest possible number
134 * (i.e., only ones). For correct operation, the most significant bit of D
135 * has to be set. Put the quotient in Q and the remainder in R.
136 */
137#define UDIV_QRNND_PREINV(q, r, nh, nl, d, di) \
138 do { \
139 mpi_limb_t _q, _ql, _r; \
140 mpi_limb_t _xh, _xl; \
141 umul_ppmm(_q, _ql, (nh), (di)); \
142 _q += (nh); /* DI is 2**BITS_PER_MPI_LIMB too small */ \
143 umul_ppmm(_xh, _xl, _q, (d)); \
144 sub_ddmmss(_xh, _r, (nh), (nl), _xh, _xl); \
145 if (_xh) { \
146 sub_ddmmss(_xh, _r, _xh, _r, 0, (d)); \
147 _q++; \
148 if (_xh) { \
149 sub_ddmmss(_xh, _r, _xh, _r, 0, (d)); \
150 _q++; \
151 } \
152 } \
153 if (_r >= (d)) { \
154 _r -= (d); \
155 _q++; \
156 } \
157 (r) = _r; \
158 (q) = _q; \
159 } while (0)
160
161/*-- mpiutil.c --*/ 108/*-- mpiutil.c --*/
162mpi_ptr_t mpi_alloc_limb_space(unsigned nlimbs); 109mpi_ptr_t mpi_alloc_limb_space(unsigned nlimbs);
163void mpi_free_limb_space(mpi_ptr_t a); 110void mpi_free_limb_space(mpi_ptr_t a);
164void mpi_assign_limb_space(MPI a, mpi_ptr_t ap, unsigned nlimbs); 111void mpi_assign_limb_space(MPI a, mpi_ptr_t ap, unsigned nlimbs);
165 112
166/*-- mpi-bit.c --*/
167void mpi_rshift_limbs(MPI a, unsigned int count);
168int mpi_lshift_limbs(MPI a, unsigned int count);
169
170/*-- mpihelp-add.c --*/
171static inline mpi_limb_t mpihelp_add_1(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, 113static inline mpi_limb_t mpihelp_add_1(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
172 mpi_size_t s1_size, mpi_limb_t s2_limb); 114 mpi_size_t s1_size, mpi_limb_t s2_limb);
173mpi_limb_t mpihelp_add_n(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, 115mpi_limb_t mpihelp_add_n(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
@@ -175,7 +117,6 @@ mpi_limb_t mpihelp_add_n(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
175static inline mpi_limb_t mpihelp_add(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, mpi_size_t s1_size, 117static inline mpi_limb_t mpihelp_add(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, mpi_size_t s1_size,
176 mpi_ptr_t s2_ptr, mpi_size_t s2_size); 118 mpi_ptr_t s2_ptr, mpi_size_t s2_size);
177 119
178/*-- mpihelp-sub.c --*/
179static inline mpi_limb_t mpihelp_sub_1(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, 120static inline mpi_limb_t mpihelp_sub_1(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
180 mpi_size_t s1_size, mpi_limb_t s2_limb); 121 mpi_size_t s1_size, mpi_limb_t s2_limb);
181mpi_limb_t mpihelp_sub_n(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, 122mpi_limb_t mpihelp_sub_n(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
@@ -183,10 +124,10 @@ mpi_limb_t mpihelp_sub_n(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
183static inline mpi_limb_t mpihelp_sub(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, mpi_size_t s1_size, 124static inline mpi_limb_t mpihelp_sub(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, mpi_size_t s1_size,
184 mpi_ptr_t s2_ptr, mpi_size_t s2_size); 125 mpi_ptr_t s2_ptr, mpi_size_t s2_size);
185 126
186/*-- mpihelp-cmp.c --*/ 127/*-- mpih-cmp.c --*/
187int mpihelp_cmp(mpi_ptr_t op1_ptr, mpi_ptr_t op2_ptr, mpi_size_t size); 128int mpihelp_cmp(mpi_ptr_t op1_ptr, mpi_ptr_t op2_ptr, mpi_size_t size);
188 129
189/*-- mpihelp-mul.c --*/ 130/*-- mpih-mul.c --*/
190 131
191struct karatsuba_ctx { 132struct karatsuba_ctx {
192 struct karatsuba_ctx *next; 133 struct karatsuba_ctx *next;
@@ -202,7 +143,6 @@ mpi_limb_t mpihelp_addmul_1(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
202 mpi_size_t s1_size, mpi_limb_t s2_limb); 143 mpi_size_t s1_size, mpi_limb_t s2_limb);
203mpi_limb_t mpihelp_submul_1(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, 144mpi_limb_t mpihelp_submul_1(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
204 mpi_size_t s1_size, mpi_limb_t s2_limb); 145 mpi_size_t s1_size, mpi_limb_t s2_limb);
205int mpihelp_mul_n(mpi_ptr_t prodp, mpi_ptr_t up, mpi_ptr_t vp, mpi_size_t size);
206int mpihelp_mul(mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t usize, 146int mpihelp_mul(mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t usize,
207 mpi_ptr_t vp, mpi_size_t vsize, mpi_limb_t *_result); 147 mpi_ptr_t vp, mpi_size_t vsize, mpi_limb_t *_result);
208void mpih_sqr_n_basecase(mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t size); 148void mpih_sqr_n_basecase(mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t size);
@@ -214,21 +154,16 @@ int mpihelp_mul_karatsuba_case(mpi_ptr_t prodp,
214 mpi_ptr_t vp, mpi_size_t vsize, 154 mpi_ptr_t vp, mpi_size_t vsize,
215 struct karatsuba_ctx *ctx); 155 struct karatsuba_ctx *ctx);
216 156
217/*-- mpihelp-mul_1.c (or xxx/cpu/ *.S) --*/ 157/*-- generic_mpih-mul1.c --*/
218mpi_limb_t mpihelp_mul_1(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, 158mpi_limb_t mpihelp_mul_1(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
219 mpi_size_t s1_size, mpi_limb_t s2_limb); 159 mpi_size_t s1_size, mpi_limb_t s2_limb);
220 160
221/*-- mpihelp-div.c --*/ 161/*-- mpih-div.c --*/
222mpi_limb_t mpihelp_mod_1(mpi_ptr_t dividend_ptr, mpi_size_t dividend_size,
223 mpi_limb_t divisor_limb);
224mpi_limb_t mpihelp_divrem(mpi_ptr_t qp, mpi_size_t qextra_limbs, 162mpi_limb_t mpihelp_divrem(mpi_ptr_t qp, mpi_size_t qextra_limbs,
225 mpi_ptr_t np, mpi_size_t nsize, 163 mpi_ptr_t np, mpi_size_t nsize,
226 mpi_ptr_t dp, mpi_size_t dsize); 164 mpi_ptr_t dp, mpi_size_t dsize);
227mpi_limb_t mpihelp_divmod_1(mpi_ptr_t quot_ptr,
228 mpi_ptr_t dividend_ptr, mpi_size_t dividend_size,
229 mpi_limb_t divisor_limb);
230 165
231/*-- mpihelp-shift.c --*/ 166/*-- generic_mpih-[lr]shift.c --*/
232mpi_limb_t mpihelp_lshift(mpi_ptr_t wp, mpi_ptr_t up, mpi_size_t usize, 167mpi_limb_t mpihelp_lshift(mpi_ptr_t wp, mpi_ptr_t up, mpi_size_t usize,
233 unsigned cnt); 168 unsigned cnt);
234mpi_limb_t mpihelp_rshift(mpi_ptr_t wp, mpi_ptr_t up, mpi_size_t usize, 169mpi_limb_t mpihelp_rshift(mpi_ptr_t wp, mpi_ptr_t up, mpi_size_t usize,
diff --git a/lib/percpu_ida.c b/lib/percpu_ida.c
index 6016f1deb1f5..9bbd9c5d375a 100644
--- a/lib/percpu_ida.c
+++ b/lib/percpu_ida.c
@@ -112,18 +112,6 @@ static inline void alloc_global_tags(struct percpu_ida *pool,
112 min(pool->nr_free, pool->percpu_batch_size)); 112 min(pool->nr_free, pool->percpu_batch_size));
113} 113}
114 114
115static inline unsigned alloc_local_tag(struct percpu_ida_cpu *tags)
116{
117 int tag = -ENOSPC;
118
119 spin_lock(&tags->lock);
120 if (tags->nr_free)
121 tag = tags->freelist[--tags->nr_free];
122 spin_unlock(&tags->lock);
123
124 return tag;
125}
126
127/** 115/**
128 * percpu_ida_alloc - allocate a tag 116 * percpu_ida_alloc - allocate a tag
129 * @pool: pool to allocate from 117 * @pool: pool to allocate from
@@ -147,20 +135,22 @@ int percpu_ida_alloc(struct percpu_ida *pool, int state)
147 DEFINE_WAIT(wait); 135 DEFINE_WAIT(wait);
148 struct percpu_ida_cpu *tags; 136 struct percpu_ida_cpu *tags;
149 unsigned long flags; 137 unsigned long flags;
150 int tag; 138 int tag = -ENOSPC;
151 139
152 local_irq_save(flags); 140 tags = raw_cpu_ptr(pool->tag_cpu);
153 tags = this_cpu_ptr(pool->tag_cpu); 141 spin_lock_irqsave(&tags->lock, flags);
154 142
155 /* Fastpath */ 143 /* Fastpath */
156 tag = alloc_local_tag(tags); 144 if (likely(tags->nr_free >= 0)) {
157 if (likely(tag >= 0)) { 145 tag = tags->freelist[--tags->nr_free];
158 local_irq_restore(flags); 146 spin_unlock_irqrestore(&tags->lock, flags);
159 return tag; 147 return tag;
160 } 148 }
149 spin_unlock_irqrestore(&tags->lock, flags);
161 150
162 while (1) { 151 while (1) {
163 spin_lock(&pool->lock); 152 spin_lock_irqsave(&pool->lock, flags);
153 tags = this_cpu_ptr(pool->tag_cpu);
164 154
165 /* 155 /*
166 * prepare_to_wait() must come before steal_tags(), in case 156 * prepare_to_wait() must come before steal_tags(), in case
@@ -184,8 +174,7 @@ int percpu_ida_alloc(struct percpu_ida *pool, int state)
184 &pool->cpus_have_tags); 174 &pool->cpus_have_tags);
185 } 175 }
186 176
187 spin_unlock(&pool->lock); 177 spin_unlock_irqrestore(&pool->lock, flags);
188 local_irq_restore(flags);
189 178
190 if (tag >= 0 || state == TASK_RUNNING) 179 if (tag >= 0 || state == TASK_RUNNING)
191 break; 180 break;
@@ -196,9 +185,6 @@ int percpu_ida_alloc(struct percpu_ida *pool, int state)
196 } 185 }
197 186
198 schedule(); 187 schedule();
199
200 local_irq_save(flags);
201 tags = this_cpu_ptr(pool->tag_cpu);
202 } 188 }
203 if (state != TASK_RUNNING) 189 if (state != TASK_RUNNING)
204 finish_wait(&pool->wait, &wait); 190 finish_wait(&pool->wait, &wait);
@@ -222,28 +208,24 @@ void percpu_ida_free(struct percpu_ida *pool, unsigned tag)
222 208
223 BUG_ON(tag >= pool->nr_tags); 209 BUG_ON(tag >= pool->nr_tags);
224 210
225 local_irq_save(flags); 211 tags = raw_cpu_ptr(pool->tag_cpu);
226 tags = this_cpu_ptr(pool->tag_cpu);
227 212
228 spin_lock(&tags->lock); 213 spin_lock_irqsave(&tags->lock, flags);
229 tags->freelist[tags->nr_free++] = tag; 214 tags->freelist[tags->nr_free++] = tag;
230 215
231 nr_free = tags->nr_free; 216 nr_free = tags->nr_free;
232 spin_unlock(&tags->lock);
233 217
234 if (nr_free == 1) { 218 if (nr_free == 1) {
235 cpumask_set_cpu(smp_processor_id(), 219 cpumask_set_cpu(smp_processor_id(),
236 &pool->cpus_have_tags); 220 &pool->cpus_have_tags);
237 wake_up(&pool->wait); 221 wake_up(&pool->wait);
238 } 222 }
223 spin_unlock_irqrestore(&tags->lock, flags);
239 224
240 if (nr_free == pool->percpu_max_size) { 225 if (nr_free == pool->percpu_max_size) {
241 spin_lock(&pool->lock); 226 spin_lock_irqsave(&pool->lock, flags);
227 spin_lock(&tags->lock);
242 228
243 /*
244 * Global lock held and irqs disabled, don't need percpu
245 * lock
246 */
247 if (tags->nr_free == pool->percpu_max_size) { 229 if (tags->nr_free == pool->percpu_max_size) {
248 move_tags(pool->freelist, &pool->nr_free, 230 move_tags(pool->freelist, &pool->nr_free,
249 tags->freelist, &tags->nr_free, 231 tags->freelist, &tags->nr_free,
@@ -251,10 +233,9 @@ void percpu_ida_free(struct percpu_ida *pool, unsigned tag)
251 233
252 wake_up(&pool->wait); 234 wake_up(&pool->wait);
253 } 235 }
254 spin_unlock(&pool->lock); 236 spin_unlock(&tags->lock);
237 spin_unlock_irqrestore(&pool->lock, flags);
255 } 238 }
256
257 local_irq_restore(flags);
258} 239}
259EXPORT_SYMBOL_GPL(percpu_ida_free); 240EXPORT_SYMBOL_GPL(percpu_ida_free);
260 241
@@ -346,29 +327,27 @@ int percpu_ida_for_each_free(struct percpu_ida *pool, percpu_ida_cb fn,
346 struct percpu_ida_cpu *remote; 327 struct percpu_ida_cpu *remote;
347 unsigned cpu, i, err = 0; 328 unsigned cpu, i, err = 0;
348 329
349 local_irq_save(flags);
350 for_each_possible_cpu(cpu) { 330 for_each_possible_cpu(cpu) {
351 remote = per_cpu_ptr(pool->tag_cpu, cpu); 331 remote = per_cpu_ptr(pool->tag_cpu, cpu);
352 spin_lock(&remote->lock); 332 spin_lock_irqsave(&remote->lock, flags);
353 for (i = 0; i < remote->nr_free; i++) { 333 for (i = 0; i < remote->nr_free; i++) {
354 err = fn(remote->freelist[i], data); 334 err = fn(remote->freelist[i], data);
355 if (err) 335 if (err)
356 break; 336 break;
357 } 337 }
358 spin_unlock(&remote->lock); 338 spin_unlock_irqrestore(&remote->lock, flags);
359 if (err) 339 if (err)
360 goto out; 340 goto out;
361 } 341 }
362 342
363 spin_lock(&pool->lock); 343 spin_lock_irqsave(&pool->lock, flags);
364 for (i = 0; i < pool->nr_free; i++) { 344 for (i = 0; i < pool->nr_free; i++) {
365 err = fn(pool->freelist[i], data); 345 err = fn(pool->freelist[i], data);
366 if (err) 346 if (err)
367 break; 347 break;
368 } 348 }
369 spin_unlock(&pool->lock); 349 spin_unlock_irqrestore(&pool->lock, flags);
370out: 350out:
371 local_irq_restore(flags);
372 return err; 351 return err;
373} 352}
374EXPORT_SYMBOL_GPL(percpu_ida_for_each_free); 353EXPORT_SYMBOL_GPL(percpu_ida_for_each_free);
diff --git a/lib/reed_solomon/decode_rs.c b/lib/reed_solomon/decode_rs.c
index 0ec3f257ffdf..1db74eb098d0 100644
--- a/lib/reed_solomon/decode_rs.c
+++ b/lib/reed_solomon/decode_rs.c
@@ -1,22 +1,16 @@
1// SPDX-License-Identifier: GPL-2.0
1/* 2/*
2 * lib/reed_solomon/decode_rs.c 3 * Generic Reed Solomon encoder / decoder library
3 *
4 * Overview:
5 * Generic Reed Solomon encoder / decoder library
6 * 4 *
7 * Copyright 2002, Phil Karn, KA9Q 5 * Copyright 2002, Phil Karn, KA9Q
8 * May be used under the terms of the GNU General Public License (GPL) 6 * May be used under the terms of the GNU General Public License (GPL)
9 * 7 *
10 * Adaption to the kernel by Thomas Gleixner (tglx@linutronix.de) 8 * Adaption to the kernel by Thomas Gleixner (tglx@linutronix.de)
11 * 9 *
12 * $Id: decode_rs.c,v 1.7 2005/11/07 11:14:59 gleixner Exp $ 10 * Generic data width independent code which is included by the wrappers.
13 *
14 */
15
16/* Generic data width independent code which is included by the
17 * wrappers.
18 */ 11 */
19{ 12{
13 struct rs_codec *rs = rsc->codec;
20 int deg_lambda, el, deg_omega; 14 int deg_lambda, el, deg_omega;
21 int i, j, r, k, pad; 15 int i, j, r, k, pad;
22 int nn = rs->nn; 16 int nn = rs->nn;
@@ -27,16 +21,22 @@
27 uint16_t *alpha_to = rs->alpha_to; 21 uint16_t *alpha_to = rs->alpha_to;
28 uint16_t *index_of = rs->index_of; 22 uint16_t *index_of = rs->index_of;
29 uint16_t u, q, tmp, num1, num2, den, discr_r, syn_error; 23 uint16_t u, q, tmp, num1, num2, den, discr_r, syn_error;
30 /* Err+Eras Locator poly and syndrome poly The maximum value
31 * of nroots is 8. So the necessary stack size will be about
32 * 220 bytes max.
33 */
34 uint16_t lambda[nroots + 1], syn[nroots];
35 uint16_t b[nroots + 1], t[nroots + 1], omega[nroots + 1];
36 uint16_t root[nroots], reg[nroots + 1], loc[nroots];
37 int count = 0; 24 int count = 0;
38 uint16_t msk = (uint16_t) rs->nn; 25 uint16_t msk = (uint16_t) rs->nn;
39 26
27 /*
28 * The decoder buffers are in the rs control struct. They are
29 * arrays sized [nroots + 1]
30 */
31 uint16_t *lambda = rsc->buffers + RS_DECODE_LAMBDA * (nroots + 1);
32 uint16_t *syn = rsc->buffers + RS_DECODE_SYN * (nroots + 1);
33 uint16_t *b = rsc->buffers + RS_DECODE_B * (nroots + 1);
34 uint16_t *t = rsc->buffers + RS_DECODE_T * (nroots + 1);
35 uint16_t *omega = rsc->buffers + RS_DECODE_OMEGA * (nroots + 1);
36 uint16_t *root = rsc->buffers + RS_DECODE_ROOT * (nroots + 1);
37 uint16_t *reg = rsc->buffers + RS_DECODE_REG * (nroots + 1);
38 uint16_t *loc = rsc->buffers + RS_DECODE_LOC * (nroots + 1);
39
40 /* Check length parameter for validity */ 40 /* Check length parameter for validity */
41 pad = nn - nroots - len; 41 pad = nn - nroots - len;
42 BUG_ON(pad < 0 || pad >= nn); 42 BUG_ON(pad < 0 || pad >= nn);
diff --git a/lib/reed_solomon/encode_rs.c b/lib/reed_solomon/encode_rs.c
index 0b5b1a6728ec..9112d46e869e 100644
--- a/lib/reed_solomon/encode_rs.c
+++ b/lib/reed_solomon/encode_rs.c
@@ -1,23 +1,16 @@
1// SPDX-License-Identifier: GPL-2.0
1/* 2/*
2 * lib/reed_solomon/encode_rs.c 3 * Generic Reed Solomon encoder / decoder library
3 *
4 * Overview:
5 * Generic Reed Solomon encoder / decoder library
6 * 4 *
7 * Copyright 2002, Phil Karn, KA9Q 5 * Copyright 2002, Phil Karn, KA9Q
8 * May be used under the terms of the GNU General Public License (GPL) 6 * May be used under the terms of the GNU General Public License (GPL)
9 * 7 *
10 * Adaption to the kernel by Thomas Gleixner (tglx@linutronix.de) 8 * Adaption to the kernel by Thomas Gleixner (tglx@linutronix.de)
11 * 9 *
12 * $Id: encode_rs.c,v 1.5 2005/11/07 11:14:59 gleixner Exp $ 10 * Generic data width independent code which is included by the wrappers.
13 *
14 */
15
16/* Generic data width independent code which is included by the
17 * wrappers.
18 * int encode_rsX (struct rs_control *rs, uintX_t *data, int len, uintY_t *par)
19 */ 11 */
20{ 12{
13 struct rs_codec *rs = rsc->codec;
21 int i, j, pad; 14 int i, j, pad;
22 int nn = rs->nn; 15 int nn = rs->nn;
23 int nroots = rs->nroots; 16 int nroots = rs->nroots;
diff --git a/lib/reed_solomon/reed_solomon.c b/lib/reed_solomon/reed_solomon.c
index 06d04cfa9339..dfcf54242fb9 100644
--- a/lib/reed_solomon/reed_solomon.c
+++ b/lib/reed_solomon/reed_solomon.c
@@ -1,43 +1,34 @@
1// SPDX-License-Identifier: GPL-2.0
1/* 2/*
2 * lib/reed_solomon/reed_solomon.c 3 * Generic Reed Solomon encoder / decoder library
3 *
4 * Overview:
5 * Generic Reed Solomon encoder / decoder library
6 * 4 *
7 * Copyright (C) 2004 Thomas Gleixner (tglx@linutronix.de) 5 * Copyright (C) 2004 Thomas Gleixner (tglx@linutronix.de)
8 * 6 *
9 * Reed Solomon code lifted from reed solomon library written by Phil Karn 7 * Reed Solomon code lifted from reed solomon library written by Phil Karn
10 * Copyright 2002 Phil Karn, KA9Q 8 * Copyright 2002 Phil Karn, KA9Q
11 * 9 *
12 * $Id: rslib.c,v 1.7 2005/11/07 11:14:59 gleixner Exp $
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License version 2 as
16 * published by the Free Software Foundation.
17 *
18 * Description: 10 * Description:
19 * 11 *
20 * The generic Reed Solomon library provides runtime configurable 12 * The generic Reed Solomon library provides runtime configurable
21 * encoding / decoding of RS codes. 13 * encoding / decoding of RS codes.
22 * Each user must call init_rs to get a pointer to a rs_control
23 * structure for the given rs parameters. This structure is either
24 * generated or a already available matching control structure is used.
25 * If a structure is generated then the polynomial arrays for
26 * fast encoding / decoding are built. This can take some time so
27 * make sure not to call this function from a time critical path.
28 * Usually a module / driver should initialize the necessary
29 * rs_control structure on module / driver init and release it
30 * on exit.
31 * The encoding puts the calculated syndrome into a given syndrome
32 * buffer.
33 * The decoding is a two step process. The first step calculates
34 * the syndrome over the received (data + syndrome) and calls the
35 * second stage, which does the decoding / error correction itself.
36 * Many hw encoders provide a syndrome calculation over the received
37 * data + syndrome and can call the second stage directly.
38 * 14 *
15 * Each user must call init_rs to get a pointer to a rs_control structure
16 * for the given rs parameters. The control struct is unique per instance.
17 * It points to a codec which can be shared by multiple control structures.
18 * If a codec is newly allocated then the polynomial arrays for fast
19 * encoding / decoding are built. This can take some time so make sure not
20 * to call this function from a time critical path. Usually a module /
21 * driver should initialize the necessary rs_control structure on module /
22 * driver init and release it on exit.
23 *
24 * The encoding puts the calculated syndrome into a given syndrome buffer.
25 *
26 * The decoding is a two step process. The first step calculates the
27 * syndrome over the received (data + syndrome) and calls the second stage,
28 * which does the decoding / error correction itself. Many hw encoders
29 * provide a syndrome calculation over the received data + syndrome and can
30 * call the second stage directly.
39 */ 31 */
40
41#include <linux/errno.h> 32#include <linux/errno.h>
42#include <linux/kernel.h> 33#include <linux/kernel.h>
43#include <linux/init.h> 34#include <linux/init.h>
@@ -46,32 +37,44 @@
46#include <linux/slab.h> 37#include <linux/slab.h>
47#include <linux/mutex.h> 38#include <linux/mutex.h>
48 39
49/* This list holds all currently allocated rs control structures */ 40enum {
50static LIST_HEAD (rslist); 41 RS_DECODE_LAMBDA,
42 RS_DECODE_SYN,
43 RS_DECODE_B,
44 RS_DECODE_T,
45 RS_DECODE_OMEGA,
46 RS_DECODE_ROOT,
47 RS_DECODE_REG,
48 RS_DECODE_LOC,
49 RS_DECODE_NUM_BUFFERS
50};
51
52/* This list holds all currently allocated rs codec structures */
53static LIST_HEAD(codec_list);
51/* Protection for the list */ 54/* Protection for the list */
52static DEFINE_MUTEX(rslistlock); 55static DEFINE_MUTEX(rslistlock);
53 56
54/** 57/**
55 * rs_init - Initialize a Reed-Solomon codec 58 * codec_init - Initialize a Reed-Solomon codec
56 * @symsize: symbol size, bits (1-8) 59 * @symsize: symbol size, bits (1-8)
57 * @gfpoly: Field generator polynomial coefficients 60 * @gfpoly: Field generator polynomial coefficients
58 * @gffunc: Field generator function 61 * @gffunc: Field generator function
59 * @fcr: first root of RS code generator polynomial, index form 62 * @fcr: first root of RS code generator polynomial, index form
60 * @prim: primitive element to generate polynomial roots 63 * @prim: primitive element to generate polynomial roots
61 * @nroots: RS code generator polynomial degree (number of roots) 64 * @nroots: RS code generator polynomial degree (number of roots)
65 * @gfp: GFP_ flags for allocations
62 * 66 *
63 * Allocate a control structure and the polynom arrays for faster 67 * Allocate a codec structure and the polynom arrays for faster
64 * en/decoding. Fill the arrays according to the given parameters. 68 * en/decoding. Fill the arrays according to the given parameters.
65 */ 69 */
66static struct rs_control *rs_init(int symsize, int gfpoly, int (*gffunc)(int), 70static struct rs_codec *codec_init(int symsize, int gfpoly, int (*gffunc)(int),
67 int fcr, int prim, int nroots) 71 int fcr, int prim, int nroots, gfp_t gfp)
68{ 72{
69 struct rs_control *rs;
70 int i, j, sr, root, iprim; 73 int i, j, sr, root, iprim;
74 struct rs_codec *rs;
71 75
72 /* Allocate the control structure */ 76 rs = kzalloc(sizeof(*rs), gfp);
73 rs = kmalloc(sizeof (struct rs_control), GFP_KERNEL); 77 if (!rs)
74 if (rs == NULL)
75 return NULL; 78 return NULL;
76 79
77 INIT_LIST_HEAD(&rs->list); 80 INIT_LIST_HEAD(&rs->list);
@@ -85,17 +88,17 @@ static struct rs_control *rs_init(int symsize, int gfpoly, int (*gffunc)(int),
85 rs->gffunc = gffunc; 88 rs->gffunc = gffunc;
86 89
87 /* Allocate the arrays */ 90 /* Allocate the arrays */
88 rs->alpha_to = kmalloc(sizeof(uint16_t) * (rs->nn + 1), GFP_KERNEL); 91 rs->alpha_to = kmalloc(sizeof(uint16_t) * (rs->nn + 1), gfp);
89 if (rs->alpha_to == NULL) 92 if (rs->alpha_to == NULL)
90 goto errrs; 93 goto err;
91 94
92 rs->index_of = kmalloc(sizeof(uint16_t) * (rs->nn + 1), GFP_KERNEL); 95 rs->index_of = kmalloc(sizeof(uint16_t) * (rs->nn + 1), gfp);
93 if (rs->index_of == NULL) 96 if (rs->index_of == NULL)
94 goto erralp; 97 goto err;
95 98
96 rs->genpoly = kmalloc(sizeof(uint16_t) * (rs->nroots + 1), GFP_KERNEL); 99 rs->genpoly = kmalloc(sizeof(uint16_t) * (rs->nroots + 1), gfp);
97 if(rs->genpoly == NULL) 100 if(rs->genpoly == NULL)
98 goto erridx; 101 goto err;
99 102
100 /* Generate Galois field lookup tables */ 103 /* Generate Galois field lookup tables */
101 rs->index_of[0] = rs->nn; /* log(zero) = -inf */ 104 rs->index_of[0] = rs->nn; /* log(zero) = -inf */
@@ -120,7 +123,7 @@ static struct rs_control *rs_init(int symsize, int gfpoly, int (*gffunc)(int),
120 } 123 }
121 /* If it's not primitive, exit */ 124 /* If it's not primitive, exit */
122 if(sr != rs->alpha_to[0]) 125 if(sr != rs->alpha_to[0])
123 goto errpol; 126 goto err;
124 127
125 /* Find prim-th root of 1, used in decoding */ 128 /* Find prim-th root of 1, used in decoding */
126 for(iprim = 1; (iprim % prim) != 0; iprim += rs->nn); 129 for(iprim = 1; (iprim % prim) != 0; iprim += rs->nn);
@@ -148,42 +151,52 @@ static struct rs_control *rs_init(int symsize, int gfpoly, int (*gffunc)(int),
148 /* convert rs->genpoly[] to index form for quicker encoding */ 151 /* convert rs->genpoly[] to index form for quicker encoding */
149 for (i = 0; i <= nroots; i++) 152 for (i = 0; i <= nroots; i++)
150 rs->genpoly[i] = rs->index_of[rs->genpoly[i]]; 153 rs->genpoly[i] = rs->index_of[rs->genpoly[i]];
154
155 rs->users = 1;
156 list_add(&rs->list, &codec_list);
151 return rs; 157 return rs;
152 158
153 /* Error exit */ 159err:
154errpol:
155 kfree(rs->genpoly); 160 kfree(rs->genpoly);
156erridx:
157 kfree(rs->index_of); 161 kfree(rs->index_of);
158erralp:
159 kfree(rs->alpha_to); 162 kfree(rs->alpha_to);
160errrs:
161 kfree(rs); 163 kfree(rs);
162 return NULL; 164 return NULL;
163} 165}
164 166
165 167
166/** 168/**
167 * free_rs - Free the rs control structure, if it is no longer used 169 * free_rs - Free the rs control structure
168 * @rs: the control structure which is not longer used by the 170 * @rs: The control structure which is not longer used by the
169 * caller 171 * caller
172 *
173 * Free the control structure. If @rs is the last user of the associated
174 * codec, free the codec as well.
170 */ 175 */
171void free_rs(struct rs_control *rs) 176void free_rs(struct rs_control *rs)
172{ 177{
178 struct rs_codec *cd;
179
180 if (!rs)
181 return;
182
183 cd = rs->codec;
173 mutex_lock(&rslistlock); 184 mutex_lock(&rslistlock);
174 rs->users--; 185 cd->users--;
175 if(!rs->users) { 186 if(!cd->users) {
176 list_del(&rs->list); 187 list_del(&cd->list);
177 kfree(rs->alpha_to); 188 kfree(cd->alpha_to);
178 kfree(rs->index_of); 189 kfree(cd->index_of);
179 kfree(rs->genpoly); 190 kfree(cd->genpoly);
180 kfree(rs); 191 kfree(cd);
181 } 192 }
182 mutex_unlock(&rslistlock); 193 mutex_unlock(&rslistlock);
194 kfree(rs);
183} 195}
196EXPORT_SYMBOL_GPL(free_rs);
184 197
185/** 198/**
186 * init_rs_internal - Find a matching or allocate a new rs control structure 199 * init_rs_internal - Allocate rs control, find a matching codec or allocate a new one
187 * @symsize: the symbol size (number of bits) 200 * @symsize: the symbol size (number of bits)
188 * @gfpoly: the extended Galois field generator polynomial coefficients, 201 * @gfpoly: the extended Galois field generator polynomial coefficients,
189 * with the 0th coefficient in the low order bit. The polynomial 202 * with the 0th coefficient in the low order bit. The polynomial
@@ -191,55 +204,69 @@ void free_rs(struct rs_control *rs)
191 * @gffunc: pointer to function to generate the next field element, 204 * @gffunc: pointer to function to generate the next field element,
192 * or the multiplicative identity element if given 0. Used 205 * or the multiplicative identity element if given 0. Used
193 * instead of gfpoly if gfpoly is 0 206 * instead of gfpoly if gfpoly is 0
194 * @fcr: the first consecutive root of the rs code generator polynomial 207 * @fcr: the first consecutive root of the rs code generator polynomial
195 * in index form 208 * in index form
196 * @prim: primitive element to generate polynomial roots 209 * @prim: primitive element to generate polynomial roots
197 * @nroots: RS code generator polynomial degree (number of roots) 210 * @nroots: RS code generator polynomial degree (number of roots)
211 * @gfp: GFP_ flags for allocations
198 */ 212 */
199static struct rs_control *init_rs_internal(int symsize, int gfpoly, 213static struct rs_control *init_rs_internal(int symsize, int gfpoly,
200 int (*gffunc)(int), int fcr, 214 int (*gffunc)(int), int fcr,
201 int prim, int nroots) 215 int prim, int nroots, gfp_t gfp)
202{ 216{
203 struct list_head *tmp; 217 struct list_head *tmp;
204 struct rs_control *rs; 218 struct rs_control *rs;
219 unsigned int bsize;
205 220
206 /* Sanity checks */ 221 /* Sanity checks */
207 if (symsize < 1) 222 if (symsize < 1)
208 return NULL; 223 return NULL;
209 if (fcr < 0 || fcr >= (1<<symsize)) 224 if (fcr < 0 || fcr >= (1<<symsize))
210 return NULL; 225 return NULL;
211 if (prim <= 0 || prim >= (1<<symsize)) 226 if (prim <= 0 || prim >= (1<<symsize))
212 return NULL; 227 return NULL;
213 if (nroots < 0 || nroots >= (1<<symsize)) 228 if (nroots < 0 || nroots >= (1<<symsize))
214 return NULL; 229 return NULL;
215 230
231 /*
232 * The decoder needs buffers in each control struct instance to
233 * avoid variable size or large fixed size allocations on
234 * stack. Size the buffers to arrays of [nroots + 1].
235 */
236 bsize = sizeof(uint16_t) * RS_DECODE_NUM_BUFFERS * (nroots + 1);
237 rs = kzalloc(sizeof(*rs) + bsize, gfp);
238 if (!rs)
239 return NULL;
240
216 mutex_lock(&rslistlock); 241 mutex_lock(&rslistlock);
217 242
218 /* Walk through the list and look for a matching entry */ 243 /* Walk through the list and look for a matching entry */
219 list_for_each(tmp, &rslist) { 244 list_for_each(tmp, &codec_list) {
220 rs = list_entry(tmp, struct rs_control, list); 245 struct rs_codec *cd = list_entry(tmp, struct rs_codec, list);
221 if (symsize != rs->mm) 246
247 if (symsize != cd->mm)
222 continue; 248 continue;
223 if (gfpoly != rs->gfpoly) 249 if (gfpoly != cd->gfpoly)
224 continue; 250 continue;
225 if (gffunc != rs->gffunc) 251 if (gffunc != cd->gffunc)
226 continue; 252 continue;
227 if (fcr != rs->fcr) 253 if (fcr != cd->fcr)
228 continue; 254 continue;
229 if (prim != rs->prim) 255 if (prim != cd->prim)
230 continue; 256 continue;
231 if (nroots != rs->nroots) 257 if (nroots != cd->nroots)
232 continue; 258 continue;
233 /* We have a matching one already */ 259 /* We have a matching one already */
234 rs->users++; 260 cd->users++;
261 rs->codec = cd;
235 goto out; 262 goto out;
236 } 263 }
237 264
238 /* Create a new one */ 265 /* Create a new one */
239 rs = rs_init(symsize, gfpoly, gffunc, fcr, prim, nroots); 266 rs->codec = codec_init(symsize, gfpoly, gffunc, fcr, prim, nroots, gfp);
240 if (rs) { 267 if (!rs->codec) {
241 rs->users = 1; 268 kfree(rs);
242 list_add(&rs->list, &rslist); 269 rs = NULL;
243 } 270 }
244out: 271out:
245 mutex_unlock(&rslistlock); 272 mutex_unlock(&rslistlock);
@@ -247,45 +274,48 @@ out:
247} 274}
248 275
249/** 276/**
250 * init_rs - Find a matching or allocate a new rs control structure 277 * init_rs_gfp - Create a RS control struct and initialize it
251 * @symsize: the symbol size (number of bits) 278 * @symsize: the symbol size (number of bits)
252 * @gfpoly: the extended Galois field generator polynomial coefficients, 279 * @gfpoly: the extended Galois field generator polynomial coefficients,
253 * with the 0th coefficient in the low order bit. The polynomial 280 * with the 0th coefficient in the low order bit. The polynomial
254 * must be primitive; 281 * must be primitive;
255 * @fcr: the first consecutive root of the rs code generator polynomial 282 * @fcr: the first consecutive root of the rs code generator polynomial
256 * in index form 283 * in index form
257 * @prim: primitive element to generate polynomial roots 284 * @prim: primitive element to generate polynomial roots
258 * @nroots: RS code generator polynomial degree (number of roots) 285 * @nroots: RS code generator polynomial degree (number of roots)
286 * @gfp: GFP_ flags for allocations
259 */ 287 */
260struct rs_control *init_rs(int symsize, int gfpoly, int fcr, int prim, 288struct rs_control *init_rs_gfp(int symsize, int gfpoly, int fcr, int prim,
261 int nroots) 289 int nroots, gfp_t gfp)
262{ 290{
263 return init_rs_internal(symsize, gfpoly, NULL, fcr, prim, nroots); 291 return init_rs_internal(symsize, gfpoly, NULL, fcr, prim, nroots, gfp);
264} 292}
293EXPORT_SYMBOL_GPL(init_rs_gfp);
265 294
266/** 295/**
267 * init_rs_non_canonical - Find a matching or allocate a new rs control 296 * init_rs_non_canonical - Allocate rs control struct for fields with
268 * structure, for fields with non-canonical 297 * non-canonical representation
269 * representation
270 * @symsize: the symbol size (number of bits) 298 * @symsize: the symbol size (number of bits)
271 * @gffunc: pointer to function to generate the next field element, 299 * @gffunc: pointer to function to generate the next field element,
272 * or the multiplicative identity element if given 0. Used 300 * or the multiplicative identity element if given 0. Used
273 * instead of gfpoly if gfpoly is 0 301 * instead of gfpoly if gfpoly is 0
274 * @fcr: the first consecutive root of the rs code generator polynomial 302 * @fcr: the first consecutive root of the rs code generator polynomial
275 * in index form 303 * in index form
276 * @prim: primitive element to generate polynomial roots 304 * @prim: primitive element to generate polynomial roots
277 * @nroots: RS code generator polynomial degree (number of roots) 305 * @nroots: RS code generator polynomial degree (number of roots)
278 */ 306 */
279struct rs_control *init_rs_non_canonical(int symsize, int (*gffunc)(int), 307struct rs_control *init_rs_non_canonical(int symsize, int (*gffunc)(int),
280 int fcr, int prim, int nroots) 308 int fcr, int prim, int nroots)
281{ 309{
282 return init_rs_internal(symsize, 0, gffunc, fcr, prim, nroots); 310 return init_rs_internal(symsize, 0, gffunc, fcr, prim, nroots,
311 GFP_KERNEL);
283} 312}
313EXPORT_SYMBOL_GPL(init_rs_non_canonical);
284 314
285#ifdef CONFIG_REED_SOLOMON_ENC8 315#ifdef CONFIG_REED_SOLOMON_ENC8
286/** 316/**
287 * encode_rs8 - Calculate the parity for data values (8bit data width) 317 * encode_rs8 - Calculate the parity for data values (8bit data width)
288 * @rs: the rs control structure 318 * @rsc: the rs control structure
289 * @data: data field of a given type 319 * @data: data field of a given type
290 * @len: data length 320 * @len: data length
291 * @par: parity data, must be initialized by caller (usually all 0) 321 * @par: parity data, must be initialized by caller (usually all 0)
@@ -295,7 +325,7 @@ struct rs_control *init_rs_non_canonical(int symsize, int (*gffunc)(int),
295 * symbol size > 8. The calling code must take care of encoding of the 325 * symbol size > 8. The calling code must take care of encoding of the
296 * syndrome result for storage itself. 326 * syndrome result for storage itself.
297 */ 327 */
298int encode_rs8(struct rs_control *rs, uint8_t *data, int len, uint16_t *par, 328int encode_rs8(struct rs_control *rsc, uint8_t *data, int len, uint16_t *par,
299 uint16_t invmsk) 329 uint16_t invmsk)
300{ 330{
301#include "encode_rs.c" 331#include "encode_rs.c"
@@ -306,7 +336,7 @@ EXPORT_SYMBOL_GPL(encode_rs8);
306#ifdef CONFIG_REED_SOLOMON_DEC8 336#ifdef CONFIG_REED_SOLOMON_DEC8
307/** 337/**
308 * decode_rs8 - Decode codeword (8bit data width) 338 * decode_rs8 - Decode codeword (8bit data width)
309 * @rs: the rs control structure 339 * @rsc: the rs control structure
310 * @data: data field of a given type 340 * @data: data field of a given type
311 * @par: received parity data field 341 * @par: received parity data field
312 * @len: data length 342 * @len: data length
@@ -319,9 +349,14 @@ EXPORT_SYMBOL_GPL(encode_rs8);
319 * The syndrome and parity uses a uint16_t data type to enable 349 * The syndrome and parity uses a uint16_t data type to enable
320 * symbol size > 8. The calling code must take care of decoding of the 350 * symbol size > 8. The calling code must take care of decoding of the
321 * syndrome result and the received parity before calling this code. 351 * syndrome result and the received parity before calling this code.
352 *
353 * Note: The rs_control struct @rsc contains buffers which are used for
354 * decoding, so the caller has to ensure that decoder invocations are
355 * serialized.
356 *
322 * Returns the number of corrected bits or -EBADMSG for uncorrectable errors. 357 * Returns the number of corrected bits or -EBADMSG for uncorrectable errors.
323 */ 358 */
324int decode_rs8(struct rs_control *rs, uint8_t *data, uint16_t *par, int len, 359int decode_rs8(struct rs_control *rsc, uint8_t *data, uint16_t *par, int len,
325 uint16_t *s, int no_eras, int *eras_pos, uint16_t invmsk, 360 uint16_t *s, int no_eras, int *eras_pos, uint16_t invmsk,
326 uint16_t *corr) 361 uint16_t *corr)
327{ 362{
@@ -333,7 +368,7 @@ EXPORT_SYMBOL_GPL(decode_rs8);
333#ifdef CONFIG_REED_SOLOMON_ENC16 368#ifdef CONFIG_REED_SOLOMON_ENC16
334/** 369/**
335 * encode_rs16 - Calculate the parity for data values (16bit data width) 370 * encode_rs16 - Calculate the parity for data values (16bit data width)
336 * @rs: the rs control structure 371 * @rsc: the rs control structure
337 * @data: data field of a given type 372 * @data: data field of a given type
338 * @len: data length 373 * @len: data length
339 * @par: parity data, must be initialized by caller (usually all 0) 374 * @par: parity data, must be initialized by caller (usually all 0)
@@ -341,7 +376,7 @@ EXPORT_SYMBOL_GPL(decode_rs8);
341 * 376 *
342 * Each field in the data array contains up to symbol size bits of valid data. 377 * Each field in the data array contains up to symbol size bits of valid data.
343 */ 378 */
344int encode_rs16(struct rs_control *rs, uint16_t *data, int len, uint16_t *par, 379int encode_rs16(struct rs_control *rsc, uint16_t *data, int len, uint16_t *par,
345 uint16_t invmsk) 380 uint16_t invmsk)
346{ 381{
347#include "encode_rs.c" 382#include "encode_rs.c"
@@ -352,7 +387,7 @@ EXPORT_SYMBOL_GPL(encode_rs16);
352#ifdef CONFIG_REED_SOLOMON_DEC16 387#ifdef CONFIG_REED_SOLOMON_DEC16
353/** 388/**
354 * decode_rs16 - Decode codeword (16bit data width) 389 * decode_rs16 - Decode codeword (16bit data width)
355 * @rs: the rs control structure 390 * @rsc: the rs control structure
356 * @data: data field of a given type 391 * @data: data field of a given type
357 * @par: received parity data field 392 * @par: received parity data field
358 * @len: data length 393 * @len: data length
@@ -363,9 +398,14 @@ EXPORT_SYMBOL_GPL(encode_rs16);
363 * @corr: buffer to store correction bitmask on eras_pos 398 * @corr: buffer to store correction bitmask on eras_pos
364 * 399 *
365 * Each field in the data array contains up to symbol size bits of valid data. 400 * Each field in the data array contains up to symbol size bits of valid data.
401 *
402 * Note: The rc_control struct @rsc contains buffers which are used for
403 * decoding, so the caller has to ensure that decoder invocations are
404 * serialized.
405 *
366 * Returns the number of corrected bits or -EBADMSG for uncorrectable errors. 406 * Returns the number of corrected bits or -EBADMSG for uncorrectable errors.
367 */ 407 */
368int decode_rs16(struct rs_control *rs, uint16_t *data, uint16_t *par, int len, 408int decode_rs16(struct rs_control *rsc, uint16_t *data, uint16_t *par, int len,
369 uint16_t *s, int no_eras, int *eras_pos, uint16_t invmsk, 409 uint16_t *s, int no_eras, int *eras_pos, uint16_t invmsk,
370 uint16_t *corr) 410 uint16_t *corr)
371{ 411{
@@ -374,10 +414,6 @@ int decode_rs16(struct rs_control *rs, uint16_t *data, uint16_t *par, int len,
374EXPORT_SYMBOL_GPL(decode_rs16); 414EXPORT_SYMBOL_GPL(decode_rs16);
375#endif 415#endif
376 416
377EXPORT_SYMBOL_GPL(init_rs);
378EXPORT_SYMBOL_GPL(init_rs_non_canonical);
379EXPORT_SYMBOL_GPL(free_rs);
380
381MODULE_LICENSE("GPL"); 417MODULE_LICENSE("GPL");
382MODULE_DESCRIPTION("Reed Solomon encoder/decoder"); 418MODULE_DESCRIPTION("Reed Solomon encoder/decoder");
383MODULE_AUTHOR("Phil Karn, Thomas Gleixner"); 419MODULE_AUTHOR("Phil Karn, Thomas Gleixner");
diff --git a/lib/rhashtable.c b/lib/rhashtable.c
index 2b2b79974b61..9427b5766134 100644
--- a/lib/rhashtable.c
+++ b/lib/rhashtable.c
@@ -668,8 +668,9 @@ EXPORT_SYMBOL_GPL(rhashtable_insert_slow);
668 * For a completely stable walk you should construct your own data 668 * For a completely stable walk you should construct your own data
669 * structure outside the hash table. 669 * structure outside the hash table.
670 * 670 *
671 * This function may sleep so you must not call it from interrupt 671 * This function may be called from any process context, including
672 * context or with spin locks held. 672 * non-preemptable context, but cannot be called from softirq or
673 * hardirq context.
673 * 674 *
674 * You must call rhashtable_walk_exit after this function returns. 675 * You must call rhashtable_walk_exit after this function returns.
675 */ 676 */
@@ -726,6 +727,7 @@ int rhashtable_walk_start_check(struct rhashtable_iter *iter)
726 __acquires(RCU) 727 __acquires(RCU)
727{ 728{
728 struct rhashtable *ht = iter->ht; 729 struct rhashtable *ht = iter->ht;
730 bool rhlist = ht->rhlist;
729 731
730 rcu_read_lock(); 732 rcu_read_lock();
731 733
@@ -734,11 +736,52 @@ int rhashtable_walk_start_check(struct rhashtable_iter *iter)
734 list_del(&iter->walker.list); 736 list_del(&iter->walker.list);
735 spin_unlock(&ht->lock); 737 spin_unlock(&ht->lock);
736 738
737 if (!iter->walker.tbl && !iter->end_of_table) { 739 if (iter->end_of_table)
740 return 0;
741 if (!iter->walker.tbl) {
738 iter->walker.tbl = rht_dereference_rcu(ht->tbl, ht); 742 iter->walker.tbl = rht_dereference_rcu(ht->tbl, ht);
743 iter->slot = 0;
744 iter->skip = 0;
739 return -EAGAIN; 745 return -EAGAIN;
740 } 746 }
741 747
748 if (iter->p && !rhlist) {
749 /*
750 * We need to validate that 'p' is still in the table, and
751 * if so, update 'skip'
752 */
753 struct rhash_head *p;
754 int skip = 0;
755 rht_for_each_rcu(p, iter->walker.tbl, iter->slot) {
756 skip++;
757 if (p == iter->p) {
758 iter->skip = skip;
759 goto found;
760 }
761 }
762 iter->p = NULL;
763 } else if (iter->p && rhlist) {
764 /* Need to validate that 'list' is still in the table, and
765 * if so, update 'skip' and 'p'.
766 */
767 struct rhash_head *p;
768 struct rhlist_head *list;
769 int skip = 0;
770 rht_for_each_rcu(p, iter->walker.tbl, iter->slot) {
771 for (list = container_of(p, struct rhlist_head, rhead);
772 list;
773 list = rcu_dereference(list->next)) {
774 skip++;
775 if (list == iter->list) {
776 iter->p = p;
777 skip = skip;
778 goto found;
779 }
780 }
781 }
782 iter->p = NULL;
783 }
784found:
742 return 0; 785 return 0;
743} 786}
744EXPORT_SYMBOL_GPL(rhashtable_walk_start_check); 787EXPORT_SYMBOL_GPL(rhashtable_walk_start_check);
@@ -914,8 +957,6 @@ void rhashtable_walk_stop(struct rhashtable_iter *iter)
914 iter->walker.tbl = NULL; 957 iter->walker.tbl = NULL;
915 spin_unlock(&ht->lock); 958 spin_unlock(&ht->lock);
916 959
917 iter->p = NULL;
918
919out: 960out:
920 rcu_read_unlock(); 961 rcu_read_unlock();
921} 962}
diff --git a/lib/test_bpf.c b/lib/test_bpf.c
index 8e157806df7a..60aedc879361 100644
--- a/lib/test_bpf.c
+++ b/lib/test_bpf.c
@@ -356,29 +356,22 @@ static int bpf_fill_maxinsns11(struct bpf_test *self)
356 return __bpf_fill_ja(self, BPF_MAXINSNS, 68); 356 return __bpf_fill_ja(self, BPF_MAXINSNS, 68);
357} 357}
358 358
359static int bpf_fill_ja(struct bpf_test *self) 359static int bpf_fill_maxinsns12(struct bpf_test *self)
360{
361 /* Hits exactly 11 passes on x86_64 JIT. */
362 return __bpf_fill_ja(self, 12, 9);
363}
364
365static int bpf_fill_ld_abs_get_processor_id(struct bpf_test *self)
366{ 360{
367 unsigned int len = BPF_MAXINSNS; 361 unsigned int len = BPF_MAXINSNS;
368 struct sock_filter *insn; 362 struct sock_filter *insn;
369 int i; 363 int i = 0;
370 364
371 insn = kmalloc_array(len, sizeof(*insn), GFP_KERNEL); 365 insn = kmalloc_array(len, sizeof(*insn), GFP_KERNEL);
372 if (!insn) 366 if (!insn)
373 return -ENOMEM; 367 return -ENOMEM;
374 368
375 for (i = 0; i < len - 1; i += 2) { 369 insn[0] = __BPF_JUMP(BPF_JMP | BPF_JA, len - 2, 0, 0);
376 insn[i] = __BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 0);
377 insn[i + 1] = __BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
378 SKF_AD_OFF + SKF_AD_CPU);
379 }
380 370
381 insn[len - 1] = __BPF_STMT(BPF_RET | BPF_K, 0xbee); 371 for (i = 1; i < len - 1; i++)
372 insn[i] = __BPF_STMT(BPF_LDX | BPF_B | BPF_MSH, 0);
373
374 insn[len - 1] = __BPF_STMT(BPF_RET | BPF_K, 0xabababab);
382 375
383 self->u.ptr.insns = insn; 376 self->u.ptr.insns = insn;
384 self->u.ptr.len = len; 377 self->u.ptr.len = len;
@@ -386,50 +379,22 @@ static int bpf_fill_ld_abs_get_processor_id(struct bpf_test *self)
386 return 0; 379 return 0;
387} 380}
388 381
389#define PUSH_CNT 68 382static int bpf_fill_maxinsns13(struct bpf_test *self)
390/* test: {skb->data[0], vlan_push} x 68 + {skb->data[0], vlan_pop} x 68 */
391static int bpf_fill_ld_abs_vlan_push_pop(struct bpf_test *self)
392{ 383{
393 unsigned int len = BPF_MAXINSNS; 384 unsigned int len = BPF_MAXINSNS;
394 struct bpf_insn *insn; 385 struct sock_filter *insn;
395 int i = 0, j, k = 0; 386 int i = 0;
396 387
397 insn = kmalloc_array(len, sizeof(*insn), GFP_KERNEL); 388 insn = kmalloc_array(len, sizeof(*insn), GFP_KERNEL);
398 if (!insn) 389 if (!insn)
399 return -ENOMEM; 390 return -ENOMEM;
400 391
401 insn[i++] = BPF_MOV64_REG(R6, R1); 392 for (i = 0; i < len - 3; i++)
402loop: 393 insn[i] = __BPF_STMT(BPF_LDX | BPF_B | BPF_MSH, 0);
403 for (j = 0; j < PUSH_CNT; j++) {
404 insn[i++] = BPF_LD_ABS(BPF_B, 0);
405 insn[i] = BPF_JMP_IMM(BPF_JNE, R0, 0x34, len - i - 2);
406 i++;
407 insn[i++] = BPF_MOV64_REG(R1, R6);
408 insn[i++] = BPF_MOV64_IMM(R2, 1);
409 insn[i++] = BPF_MOV64_IMM(R3, 2);
410 insn[i++] = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
411 bpf_skb_vlan_push_proto.func - __bpf_call_base);
412 insn[i] = BPF_JMP_IMM(BPF_JNE, R0, 0, len - i - 2);
413 i++;
414 }
415
416 for (j = 0; j < PUSH_CNT; j++) {
417 insn[i++] = BPF_LD_ABS(BPF_B, 0);
418 insn[i] = BPF_JMP_IMM(BPF_JNE, R0, 0x34, len - i - 2);
419 i++;
420 insn[i++] = BPF_MOV64_REG(R1, R6);
421 insn[i++] = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
422 bpf_skb_vlan_pop_proto.func - __bpf_call_base);
423 insn[i] = BPF_JMP_IMM(BPF_JNE, R0, 0, len - i - 2);
424 i++;
425 }
426 if (++k < 5)
427 goto loop;
428 394
429 for (; i < len - 1; i++) 395 insn[len - 3] = __BPF_STMT(BPF_LD | BPF_IMM, 0xabababab);
430 insn[i] = BPF_ALU32_IMM(BPF_MOV, R0, 0xbef); 396 insn[len - 2] = __BPF_STMT(BPF_ALU | BPF_XOR | BPF_X, 0);
431 397 insn[len - 1] = __BPF_STMT(BPF_RET | BPF_A, 0);
432 insn[len - 1] = BPF_EXIT_INSN();
433 398
434 self->u.ptr.insns = insn; 399 self->u.ptr.insns = insn;
435 self->u.ptr.len = len; 400 self->u.ptr.len = len;
@@ -437,58 +402,29 @@ loop:
437 return 0; 402 return 0;
438} 403}
439 404
440static int bpf_fill_ld_abs_vlan_push_pop2(struct bpf_test *self) 405static int bpf_fill_ja(struct bpf_test *self)
441{ 406{
442 struct bpf_insn *insn; 407 /* Hits exactly 11 passes on x86_64 JIT. */
443 408 return __bpf_fill_ja(self, 12, 9);
444 insn = kmalloc_array(16, sizeof(*insn), GFP_KERNEL);
445 if (!insn)
446 return -ENOMEM;
447
448 /* Due to func address being non-const, we need to
449 * assemble this here.
450 */
451 insn[0] = BPF_MOV64_REG(R6, R1);
452 insn[1] = BPF_LD_ABS(BPF_B, 0);
453 insn[2] = BPF_LD_ABS(BPF_H, 0);
454 insn[3] = BPF_LD_ABS(BPF_W, 0);
455 insn[4] = BPF_MOV64_REG(R7, R6);
456 insn[5] = BPF_MOV64_IMM(R6, 0);
457 insn[6] = BPF_MOV64_REG(R1, R7);
458 insn[7] = BPF_MOV64_IMM(R2, 1);
459 insn[8] = BPF_MOV64_IMM(R3, 2);
460 insn[9] = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
461 bpf_skb_vlan_push_proto.func - __bpf_call_base);
462 insn[10] = BPF_MOV64_REG(R6, R7);
463 insn[11] = BPF_LD_ABS(BPF_B, 0);
464 insn[12] = BPF_LD_ABS(BPF_H, 0);
465 insn[13] = BPF_LD_ABS(BPF_W, 0);
466 insn[14] = BPF_MOV64_IMM(R0, 42);
467 insn[15] = BPF_EXIT_INSN();
468
469 self->u.ptr.insns = insn;
470 self->u.ptr.len = 16;
471
472 return 0;
473} 409}
474 410
475static int bpf_fill_jump_around_ld_abs(struct bpf_test *self) 411static int bpf_fill_ld_abs_get_processor_id(struct bpf_test *self)
476{ 412{
477 unsigned int len = BPF_MAXINSNS; 413 unsigned int len = BPF_MAXINSNS;
478 struct bpf_insn *insn; 414 struct sock_filter *insn;
479 int i = 0; 415 int i;
480 416
481 insn = kmalloc_array(len, sizeof(*insn), GFP_KERNEL); 417 insn = kmalloc_array(len, sizeof(*insn), GFP_KERNEL);
482 if (!insn) 418 if (!insn)
483 return -ENOMEM; 419 return -ENOMEM;
484 420
485 insn[i++] = BPF_MOV64_REG(R6, R1); 421 for (i = 0; i < len - 1; i += 2) {
486 insn[i++] = BPF_LD_ABS(BPF_B, 0); 422 insn[i] = __BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 0);
487 insn[i] = BPF_JMP_IMM(BPF_JEQ, R0, 10, len - i - 2); 423 insn[i + 1] = __BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
488 i++; 424 SKF_AD_OFF + SKF_AD_CPU);
489 while (i < len - 1) 425 }
490 insn[i++] = BPF_LD_ABS(BPF_B, 1); 426
491 insn[i] = BPF_EXIT_INSN(); 427 insn[len - 1] = __BPF_STMT(BPF_RET | BPF_K, 0xbee);
492 428
493 self->u.ptr.insns = insn; 429 self->u.ptr.insns = insn;
494 self->u.ptr.len = len; 430 self->u.ptr.len = len;
@@ -1988,40 +1924,6 @@ static struct bpf_test tests[] = {
1988 { { 0, -1 } } 1924 { { 0, -1 } }
1989 }, 1925 },
1990 { 1926 {
1991 "INT: DIV + ABS",
1992 .u.insns_int = {
1993 BPF_ALU64_REG(BPF_MOV, R6, R1),
1994 BPF_LD_ABS(BPF_B, 3),
1995 BPF_ALU64_IMM(BPF_MOV, R2, 2),
1996 BPF_ALU32_REG(BPF_DIV, R0, R2),
1997 BPF_ALU64_REG(BPF_MOV, R8, R0),
1998 BPF_LD_ABS(BPF_B, 4),
1999 BPF_ALU64_REG(BPF_ADD, R8, R0),
2000 BPF_LD_IND(BPF_B, R8, -70),
2001 BPF_EXIT_INSN(),
2002 },
2003 INTERNAL,
2004 { 10, 20, 30, 40, 50 },
2005 { { 4, 0 }, { 5, 10 } }
2006 },
2007 {
2008 /* This one doesn't go through verifier, but is just raw insn
2009 * as opposed to cBPF tests from here. Thus div by 0 tests are
2010 * done in test_verifier in BPF kselftests.
2011 */
2012 "INT: DIV by -1",
2013 .u.insns_int = {
2014 BPF_ALU64_REG(BPF_MOV, R6, R1),
2015 BPF_ALU64_IMM(BPF_MOV, R7, -1),
2016 BPF_LD_ABS(BPF_B, 3),
2017 BPF_ALU32_REG(BPF_DIV, R0, R7),
2018 BPF_EXIT_INSN(),
2019 },
2020 INTERNAL,
2021 { 10, 20, 30, 40, 50 },
2022 { { 3, 0 }, { 4, 0 } }
2023 },
2024 {
2025 "check: missing ret", 1927 "check: missing ret",
2026 .u.insns = { 1928 .u.insns = {
2027 BPF_STMT(BPF_LD | BPF_IMM, 1), 1929 BPF_STMT(BPF_LD | BPF_IMM, 1),
@@ -2383,50 +2285,6 @@ static struct bpf_test tests[] = {
2383 { }, 2285 { },
2384 { { 0, 1 } } 2286 { { 0, 1 } }
2385 }, 2287 },
2386 {
2387 "nmap reduced",
2388 .u.insns_int = {
2389 BPF_MOV64_REG(R6, R1),
2390 BPF_LD_ABS(BPF_H, 12),
2391 BPF_JMP_IMM(BPF_JNE, R0, 0x806, 28),
2392 BPF_LD_ABS(BPF_H, 12),
2393 BPF_JMP_IMM(BPF_JNE, R0, 0x806, 26),
2394 BPF_MOV32_IMM(R0, 18),
2395 BPF_STX_MEM(BPF_W, R10, R0, -64),
2396 BPF_LDX_MEM(BPF_W, R7, R10, -64),
2397 BPF_LD_IND(BPF_W, R7, 14),
2398 BPF_STX_MEM(BPF_W, R10, R0, -60),
2399 BPF_MOV32_IMM(R0, 280971478),
2400 BPF_STX_MEM(BPF_W, R10, R0, -56),
2401 BPF_LDX_MEM(BPF_W, R7, R10, -56),
2402 BPF_LDX_MEM(BPF_W, R0, R10, -60),
2403 BPF_ALU32_REG(BPF_SUB, R0, R7),
2404 BPF_JMP_IMM(BPF_JNE, R0, 0, 15),
2405 BPF_LD_ABS(BPF_H, 12),
2406 BPF_JMP_IMM(BPF_JNE, R0, 0x806, 13),
2407 BPF_MOV32_IMM(R0, 22),
2408 BPF_STX_MEM(BPF_W, R10, R0, -56),
2409 BPF_LDX_MEM(BPF_W, R7, R10, -56),
2410 BPF_LD_IND(BPF_H, R7, 14),
2411 BPF_STX_MEM(BPF_W, R10, R0, -52),
2412 BPF_MOV32_IMM(R0, 17366),
2413 BPF_STX_MEM(BPF_W, R10, R0, -48),
2414 BPF_LDX_MEM(BPF_W, R7, R10, -48),
2415 BPF_LDX_MEM(BPF_W, R0, R10, -52),
2416 BPF_ALU32_REG(BPF_SUB, R0, R7),
2417 BPF_JMP_IMM(BPF_JNE, R0, 0, 2),
2418 BPF_MOV32_IMM(R0, 256),
2419 BPF_EXIT_INSN(),
2420 BPF_MOV32_IMM(R0, 0),
2421 BPF_EXIT_INSN(),
2422 },
2423 INTERNAL,
2424 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x08, 0x06, 0, 0,
2425 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2426 0x10, 0xbf, 0x48, 0xd6, 0x43, 0xd6},
2427 { { 38, 256 } },
2428 .stack_depth = 64,
2429 },
2430 /* BPF_ALU | BPF_MOV | BPF_X */ 2288 /* BPF_ALU | BPF_MOV | BPF_X */
2431 { 2289 {
2432 "ALU_MOV_X: dst = 2", 2290 "ALU_MOV_X: dst = 2",
@@ -5478,28 +5336,29 @@ static struct bpf_test tests[] = {
5478 .expected_errcode = -ENOTSUPP, 5336 .expected_errcode = -ENOTSUPP,
5479 }, 5337 },
5480 { 5338 {
5481 "BPF_MAXINSNS: ld_abs+get_processor_id", 5339 "BPF_MAXINSNS: jump over MSH",
5482 { },
5483 CLASSIC,
5484 { }, 5340 { },
5485 { { 1, 0xbee } }, 5341 CLASSIC | FLAG_EXPECTED_FAIL,
5486 .fill_helper = bpf_fill_ld_abs_get_processor_id, 5342 { 0xfa, 0xfb, 0xfc, 0xfd, },
5343 { { 4, 0xabababab } },
5344 .fill_helper = bpf_fill_maxinsns12,
5345 .expected_errcode = -EINVAL,
5487 }, 5346 },
5488 { 5347 {
5489 "BPF_MAXINSNS: ld_abs+vlan_push/pop", 5348 "BPF_MAXINSNS: exec all MSH",
5490 { }, 5349 { },
5491 INTERNAL, 5350 CLASSIC,
5492 { 0x34 }, 5351 { 0xfa, 0xfb, 0xfc, 0xfd, },
5493 { { ETH_HLEN, 0xbef } }, 5352 { { 4, 0xababab83 } },
5494 .fill_helper = bpf_fill_ld_abs_vlan_push_pop, 5353 .fill_helper = bpf_fill_maxinsns13,
5495 }, 5354 },
5496 { 5355 {
5497 "BPF_MAXINSNS: jump around ld_abs", 5356 "BPF_MAXINSNS: ld_abs+get_processor_id",
5498 { }, 5357 { },
5499 INTERNAL, 5358 CLASSIC,
5500 { 10, 11 }, 5359 { },
5501 { { 2, 10 } }, 5360 { { 1, 0xbee } },
5502 .fill_helper = bpf_fill_jump_around_ld_abs, 5361 .fill_helper = bpf_fill_ld_abs_get_processor_id,
5503 }, 5362 },
5504 /* 5363 /*
5505 * LD_IND / LD_ABS on fragmented SKBs 5364 * LD_IND / LD_ABS on fragmented SKBs
@@ -5683,6 +5542,53 @@ static struct bpf_test tests[] = {
5683 { {0x40, 0x05 } }, 5542 { {0x40, 0x05 } },
5684 }, 5543 },
5685 { 5544 {
5545 "LD_IND byte positive offset, all ff",
5546 .u.insns = {
5547 BPF_STMT(BPF_LDX | BPF_IMM, 0x3e),
5548 BPF_STMT(BPF_LD | BPF_IND | BPF_B, 0x1),
5549 BPF_STMT(BPF_RET | BPF_A, 0x0),
5550 },
5551 CLASSIC,
5552 { [0x3c] = 0xff, [0x3d] = 0xff, [0x3e] = 0xff, [0x3f] = 0xff },
5553 { {0x40, 0xff } },
5554 },
5555 {
5556 "LD_IND byte positive offset, out of bounds",
5557 .u.insns = {
5558 BPF_STMT(BPF_LDX | BPF_IMM, 0x3e),
5559 BPF_STMT(BPF_LD | BPF_IND | BPF_B, 0x1),
5560 BPF_STMT(BPF_RET | BPF_A, 0x0),
5561 },
5562 CLASSIC,
5563 { [0x3c] = 0x25, [0x3d] = 0x05, [0x3e] = 0x19, [0x3f] = 0x82 },
5564 { {0x3f, 0 }, },
5565 },
5566 {
5567 "LD_IND byte negative offset, out of bounds",
5568 .u.insns = {
5569 BPF_STMT(BPF_LDX | BPF_IMM, 0x3e),
5570 BPF_STMT(BPF_LD | BPF_IND | BPF_B, -0x3f),
5571 BPF_STMT(BPF_RET | BPF_A, 0x0),
5572 },
5573 CLASSIC,
5574 { [0x3c] = 0x25, [0x3d] = 0x05, [0x3e] = 0x19, [0x3f] = 0x82 },
5575 { {0x3f, 0 } },
5576 },
5577 {
5578 "LD_IND byte negative offset, multiple calls",
5579 .u.insns = {
5580 BPF_STMT(BPF_LDX | BPF_IMM, 0x3b),
5581 BPF_STMT(BPF_LD | BPF_IND | BPF_B, SKF_LL_OFF + 1),
5582 BPF_STMT(BPF_LD | BPF_IND | BPF_B, SKF_LL_OFF + 2),
5583 BPF_STMT(BPF_LD | BPF_IND | BPF_B, SKF_LL_OFF + 3),
5584 BPF_STMT(BPF_LD | BPF_IND | BPF_B, SKF_LL_OFF + 4),
5585 BPF_STMT(BPF_RET | BPF_A, 0x0),
5586 },
5587 CLASSIC,
5588 { [0x3c] = 0x25, [0x3d] = 0x05, [0x3e] = 0x19, [0x3f] = 0x82 },
5589 { {0x40, 0x82 }, },
5590 },
5591 {
5686 "LD_IND halfword positive offset", 5592 "LD_IND halfword positive offset",
5687 .u.insns = { 5593 .u.insns = {
5688 BPF_STMT(BPF_LDX | BPF_IMM, 0x20), 5594 BPF_STMT(BPF_LDX | BPF_IMM, 0x20),
@@ -5731,6 +5637,39 @@ static struct bpf_test tests[] = {
5731 { {0x40, 0x66cc } }, 5637 { {0x40, 0x66cc } },
5732 }, 5638 },
5733 { 5639 {
5640 "LD_IND halfword positive offset, all ff",
5641 .u.insns = {
5642 BPF_STMT(BPF_LDX | BPF_IMM, 0x3d),
5643 BPF_STMT(BPF_LD | BPF_IND | BPF_H, 0x1),
5644 BPF_STMT(BPF_RET | BPF_A, 0x0),
5645 },
5646 CLASSIC,
5647 { [0x3c] = 0xff, [0x3d] = 0xff, [0x3e] = 0xff, [0x3f] = 0xff },
5648 { {0x40, 0xffff } },
5649 },
5650 {
5651 "LD_IND halfword positive offset, out of bounds",
5652 .u.insns = {
5653 BPF_STMT(BPF_LDX | BPF_IMM, 0x3e),
5654 BPF_STMT(BPF_LD | BPF_IND | BPF_H, 0x1),
5655 BPF_STMT(BPF_RET | BPF_A, 0x0),
5656 },
5657 CLASSIC,
5658 { [0x3c] = 0x25, [0x3d] = 0x05, [0x3e] = 0x19, [0x3f] = 0x82 },
5659 { {0x3f, 0 }, },
5660 },
5661 {
5662 "LD_IND halfword negative offset, out of bounds",
5663 .u.insns = {
5664 BPF_STMT(BPF_LDX | BPF_IMM, 0x3e),
5665 BPF_STMT(BPF_LD | BPF_IND | BPF_H, -0x3f),
5666 BPF_STMT(BPF_RET | BPF_A, 0x0),
5667 },
5668 CLASSIC,
5669 { [0x3c] = 0x25, [0x3d] = 0x05, [0x3e] = 0x19, [0x3f] = 0x82 },
5670 { {0x3f, 0 } },
5671 },
5672 {
5734 "LD_IND word positive offset", 5673 "LD_IND word positive offset",
5735 .u.insns = { 5674 .u.insns = {
5736 BPF_STMT(BPF_LDX | BPF_IMM, 0x20), 5675 BPF_STMT(BPF_LDX | BPF_IMM, 0x20),
@@ -5821,6 +5760,39 @@ static struct bpf_test tests[] = {
5821 { {0x40, 0x66cc77dd } }, 5760 { {0x40, 0x66cc77dd } },
5822 }, 5761 },
5823 { 5762 {
5763 "LD_IND word positive offset, all ff",
5764 .u.insns = {
5765 BPF_STMT(BPF_LDX | BPF_IMM, 0x3b),
5766 BPF_STMT(BPF_LD | BPF_IND | BPF_W, 0x1),
5767 BPF_STMT(BPF_RET | BPF_A, 0x0),
5768 },
5769 CLASSIC,
5770 { [0x3c] = 0xff, [0x3d] = 0xff, [0x3e] = 0xff, [0x3f] = 0xff },
5771 { {0x40, 0xffffffff } },
5772 },
5773 {
5774 "LD_IND word positive offset, out of bounds",
5775 .u.insns = {
5776 BPF_STMT(BPF_LDX | BPF_IMM, 0x3e),
5777 BPF_STMT(BPF_LD | BPF_IND | BPF_W, 0x1),
5778 BPF_STMT(BPF_RET | BPF_A, 0x0),
5779 },
5780 CLASSIC,
5781 { [0x3c] = 0x25, [0x3d] = 0x05, [0x3e] = 0x19, [0x3f] = 0x82 },
5782 { {0x3f, 0 }, },
5783 },
5784 {
5785 "LD_IND word negative offset, out of bounds",
5786 .u.insns = {
5787 BPF_STMT(BPF_LDX | BPF_IMM, 0x3e),
5788 BPF_STMT(BPF_LD | BPF_IND | BPF_W, -0x3f),
5789 BPF_STMT(BPF_RET | BPF_A, 0x0),
5790 },
5791 CLASSIC,
5792 { [0x3c] = 0x25, [0x3d] = 0x05, [0x3e] = 0x19, [0x3f] = 0x82 },
5793 { {0x3f, 0 } },
5794 },
5795 {
5824 "LD_ABS byte", 5796 "LD_ABS byte",
5825 .u.insns = { 5797 .u.insns = {
5826 BPF_STMT(BPF_LD | BPF_ABS | BPF_B, 0x20), 5798 BPF_STMT(BPF_LD | BPF_ABS | BPF_B, 0x20),
@@ -5838,6 +5810,68 @@ static struct bpf_test tests[] = {
5838 { {0x40, 0xcc } }, 5810 { {0x40, 0xcc } },
5839 }, 5811 },
5840 { 5812 {
5813 "LD_ABS byte positive offset, all ff",
5814 .u.insns = {
5815 BPF_STMT(BPF_LD | BPF_ABS | BPF_B, 0x3f),
5816 BPF_STMT(BPF_RET | BPF_A, 0x0),
5817 },
5818 CLASSIC,
5819 { [0x3c] = 0xff, [0x3d] = 0xff, [0x3e] = 0xff, [0x3f] = 0xff },
5820 { {0x40, 0xff } },
5821 },
5822 {
5823 "LD_ABS byte positive offset, out of bounds",
5824 .u.insns = {
5825 BPF_STMT(BPF_LD | BPF_ABS | BPF_B, 0x3f),
5826 BPF_STMT(BPF_RET | BPF_A, 0x0),
5827 },
5828 CLASSIC,
5829 { [0x3c] = 0x25, [0x3d] = 0x05, [0x3e] = 0x19, [0x3f] = 0x82 },
5830 { {0x3f, 0 }, },
5831 },
5832 {
5833 "LD_ABS byte negative offset, out of bounds load",
5834 .u.insns = {
5835 BPF_STMT(BPF_LD | BPF_ABS | BPF_B, -1),
5836 BPF_STMT(BPF_RET | BPF_A, 0x0),
5837 },
5838 CLASSIC | FLAG_EXPECTED_FAIL,
5839 .expected_errcode = -EINVAL,
5840 },
5841 {
5842 "LD_ABS byte negative offset, in bounds",
5843 .u.insns = {
5844 BPF_STMT(BPF_LD | BPF_ABS | BPF_B, SKF_LL_OFF + 0x3f),
5845 BPF_STMT(BPF_RET | BPF_A, 0x0),
5846 },
5847 CLASSIC,
5848 { [0x3c] = 0x25, [0x3d] = 0x05, [0x3e] = 0x19, [0x3f] = 0x82 },
5849 { {0x40, 0x82 }, },
5850 },
5851 {
5852 "LD_ABS byte negative offset, out of bounds",
5853 .u.insns = {
5854 BPF_STMT(BPF_LD | BPF_ABS | BPF_B, SKF_LL_OFF + 0x3f),
5855 BPF_STMT(BPF_RET | BPF_A, 0x0),
5856 },
5857 CLASSIC,
5858 { [0x3c] = 0x25, [0x3d] = 0x05, [0x3e] = 0x19, [0x3f] = 0x82 },
5859 { {0x3f, 0 }, },
5860 },
5861 {
5862 "LD_ABS byte negative offset, multiple calls",
5863 .u.insns = {
5864 BPF_STMT(BPF_LD | BPF_ABS | BPF_B, SKF_LL_OFF + 0x3c),
5865 BPF_STMT(BPF_LD | BPF_ABS | BPF_B, SKF_LL_OFF + 0x3d),
5866 BPF_STMT(BPF_LD | BPF_ABS | BPF_B, SKF_LL_OFF + 0x3e),
5867 BPF_STMT(BPF_LD | BPF_ABS | BPF_B, SKF_LL_OFF + 0x3f),
5868 BPF_STMT(BPF_RET | BPF_A, 0x0),
5869 },
5870 CLASSIC,
5871 { [0x3c] = 0x25, [0x3d] = 0x05, [0x3e] = 0x19, [0x3f] = 0x82 },
5872 { {0x40, 0x82 }, },
5873 },
5874 {
5841 "LD_ABS halfword", 5875 "LD_ABS halfword",
5842 .u.insns = { 5876 .u.insns = {
5843 BPF_STMT(BPF_LD | BPF_ABS | BPF_H, 0x22), 5877 BPF_STMT(BPF_LD | BPF_ABS | BPF_H, 0x22),
@@ -5872,6 +5906,55 @@ static struct bpf_test tests[] = {
5872 { {0x40, 0x99ff } }, 5906 { {0x40, 0x99ff } },
5873 }, 5907 },
5874 { 5908 {
5909 "LD_ABS halfword positive offset, all ff",
5910 .u.insns = {
5911 BPF_STMT(BPF_LD | BPF_ABS | BPF_H, 0x3e),
5912 BPF_STMT(BPF_RET | BPF_A, 0x0),
5913 },
5914 CLASSIC,
5915 { [0x3c] = 0xff, [0x3d] = 0xff, [0x3e] = 0xff, [0x3f] = 0xff },
5916 { {0x40, 0xffff } },
5917 },
5918 {
5919 "LD_ABS halfword positive offset, out of bounds",
5920 .u.insns = {
5921 BPF_STMT(BPF_LD | BPF_ABS | BPF_H, 0x3f),
5922 BPF_STMT(BPF_RET | BPF_A, 0x0),
5923 },
5924 CLASSIC,
5925 { [0x3c] = 0x25, [0x3d] = 0x05, [0x3e] = 0x19, [0x3f] = 0x82 },
5926 { {0x3f, 0 }, },
5927 },
5928 {
5929 "LD_ABS halfword negative offset, out of bounds load",
5930 .u.insns = {
5931 BPF_STMT(BPF_LD | BPF_ABS | BPF_H, -1),
5932 BPF_STMT(BPF_RET | BPF_A, 0x0),
5933 },
5934 CLASSIC | FLAG_EXPECTED_FAIL,
5935 .expected_errcode = -EINVAL,
5936 },
5937 {
5938 "LD_ABS halfword negative offset, in bounds",
5939 .u.insns = {
5940 BPF_STMT(BPF_LD | BPF_ABS | BPF_H, SKF_LL_OFF + 0x3e),
5941 BPF_STMT(BPF_RET | BPF_A, 0x0),
5942 },
5943 CLASSIC,
5944 { [0x3c] = 0x25, [0x3d] = 0x05, [0x3e] = 0x19, [0x3f] = 0x82 },
5945 { {0x40, 0x1982 }, },
5946 },
5947 {
5948 "LD_ABS halfword negative offset, out of bounds",
5949 .u.insns = {
5950 BPF_STMT(BPF_LD | BPF_ABS | BPF_H, SKF_LL_OFF + 0x3e),
5951 BPF_STMT(BPF_RET | BPF_A, 0x0),
5952 },
5953 CLASSIC,
5954 { [0x3c] = 0x25, [0x3d] = 0x05, [0x3e] = 0x19, [0x3f] = 0x82 },
5955 { {0x3f, 0 }, },
5956 },
5957 {
5875 "LD_ABS word", 5958 "LD_ABS word",
5876 .u.insns = { 5959 .u.insns = {
5877 BPF_STMT(BPF_LD | BPF_ABS | BPF_W, 0x1c), 5960 BPF_STMT(BPF_LD | BPF_ABS | BPF_W, 0x1c),
@@ -5939,6 +6022,140 @@ static struct bpf_test tests[] = {
5939 }, 6022 },
5940 { {0x40, 0x88ee99ff } }, 6023 { {0x40, 0x88ee99ff } },
5941 }, 6024 },
6025 {
6026 "LD_ABS word positive offset, all ff",
6027 .u.insns = {
6028 BPF_STMT(BPF_LD | BPF_ABS | BPF_W, 0x3c),
6029 BPF_STMT(BPF_RET | BPF_A, 0x0),
6030 },
6031 CLASSIC,
6032 { [0x3c] = 0xff, [0x3d] = 0xff, [0x3e] = 0xff, [0x3f] = 0xff },
6033 { {0x40, 0xffffffff } },
6034 },
6035 {
6036 "LD_ABS word positive offset, out of bounds",
6037 .u.insns = {
6038 BPF_STMT(BPF_LD | BPF_ABS | BPF_W, 0x3f),
6039 BPF_STMT(BPF_RET | BPF_A, 0x0),
6040 },
6041 CLASSIC,
6042 { [0x3c] = 0x25, [0x3d] = 0x05, [0x3e] = 0x19, [0x3f] = 0x82 },
6043 { {0x3f, 0 }, },
6044 },
6045 {
6046 "LD_ABS word negative offset, out of bounds load",
6047 .u.insns = {
6048 BPF_STMT(BPF_LD | BPF_ABS | BPF_W, -1),
6049 BPF_STMT(BPF_RET | BPF_A, 0x0),
6050 },
6051 CLASSIC | FLAG_EXPECTED_FAIL,
6052 .expected_errcode = -EINVAL,
6053 },
6054 {
6055 "LD_ABS word negative offset, in bounds",
6056 .u.insns = {
6057 BPF_STMT(BPF_LD | BPF_ABS | BPF_W, SKF_LL_OFF + 0x3c),
6058 BPF_STMT(BPF_RET | BPF_A, 0x0),
6059 },
6060 CLASSIC,
6061 { [0x3c] = 0x25, [0x3d] = 0x05, [0x3e] = 0x19, [0x3f] = 0x82 },
6062 { {0x40, 0x25051982 }, },
6063 },
6064 {
6065 "LD_ABS word negative offset, out of bounds",
6066 .u.insns = {
6067 BPF_STMT(BPF_LD | BPF_ABS | BPF_W, SKF_LL_OFF + 0x3c),
6068 BPF_STMT(BPF_RET | BPF_A, 0x0),
6069 },
6070 CLASSIC,
6071 { [0x3c] = 0x25, [0x3d] = 0x05, [0x3e] = 0x19, [0x3f] = 0x82 },
6072 { {0x3f, 0 }, },
6073 },
6074 {
6075 "LDX_MSH standalone, preserved A",
6076 .u.insns = {
6077 BPF_STMT(BPF_LD | BPF_IMM, 0xffeebbaa),
6078 BPF_STMT(BPF_LDX | BPF_B | BPF_MSH, 0x3c),
6079 BPF_STMT(BPF_RET | BPF_A, 0x0),
6080 },
6081 CLASSIC,
6082 { [0x3c] = 0x25, [0x3d] = 0x05, [0x3e] = 0x19, [0x3f] = 0x82 },
6083 { {0x40, 0xffeebbaa }, },
6084 },
6085 {
6086 "LDX_MSH standalone, preserved A 2",
6087 .u.insns = {
6088 BPF_STMT(BPF_LD | BPF_IMM, 0x175e9d63),
6089 BPF_STMT(BPF_LDX | BPF_B | BPF_MSH, 0x3c),
6090 BPF_STMT(BPF_LDX | BPF_B | BPF_MSH, 0x3d),
6091 BPF_STMT(BPF_LDX | BPF_B | BPF_MSH, 0x3e),
6092 BPF_STMT(BPF_LDX | BPF_B | BPF_MSH, 0x3f),
6093 BPF_STMT(BPF_RET | BPF_A, 0x0),
6094 },
6095 CLASSIC,
6096 { [0x3c] = 0x25, [0x3d] = 0x05, [0x3e] = 0x19, [0x3f] = 0x82 },
6097 { {0x40, 0x175e9d63 }, },
6098 },
6099 {
6100 "LDX_MSH standalone, test result 1",
6101 .u.insns = {
6102 BPF_STMT(BPF_LD | BPF_IMM, 0xffeebbaa),
6103 BPF_STMT(BPF_LDX | BPF_B | BPF_MSH, 0x3c),
6104 BPF_STMT(BPF_MISC | BPF_TXA, 0),
6105 BPF_STMT(BPF_RET | BPF_A, 0x0),
6106 },
6107 CLASSIC,
6108 { [0x3c] = 0x25, [0x3d] = 0x05, [0x3e] = 0x19, [0x3f] = 0x82 },
6109 { {0x40, 0x14 }, },
6110 },
6111 {
6112 "LDX_MSH standalone, test result 2",
6113 .u.insns = {
6114 BPF_STMT(BPF_LD | BPF_IMM, 0xffeebbaa),
6115 BPF_STMT(BPF_LDX | BPF_B | BPF_MSH, 0x3e),
6116 BPF_STMT(BPF_MISC | BPF_TXA, 0),
6117 BPF_STMT(BPF_RET | BPF_A, 0x0),
6118 },
6119 CLASSIC,
6120 { [0x3c] = 0x25, [0x3d] = 0x05, [0x3e] = 0x19, [0x3f] = 0x82 },
6121 { {0x40, 0x24 }, },
6122 },
6123 {
6124 "LDX_MSH standalone, negative offset",
6125 .u.insns = {
6126 BPF_STMT(BPF_LD | BPF_IMM, 0xffeebbaa),
6127 BPF_STMT(BPF_LDX | BPF_B | BPF_MSH, -1),
6128 BPF_STMT(BPF_MISC | BPF_TXA, 0),
6129 BPF_STMT(BPF_RET | BPF_A, 0x0),
6130 },
6131 CLASSIC,
6132 { [0x3c] = 0x25, [0x3d] = 0x05, [0x3e] = 0x19, [0x3f] = 0x82 },
6133 { {0x40, 0 }, },
6134 },
6135 {
6136 "LDX_MSH standalone, negative offset 2",
6137 .u.insns = {
6138 BPF_STMT(BPF_LD | BPF_IMM, 0xffeebbaa),
6139 BPF_STMT(BPF_LDX | BPF_B | BPF_MSH, SKF_LL_OFF + 0x3e),
6140 BPF_STMT(BPF_MISC | BPF_TXA, 0),
6141 BPF_STMT(BPF_RET | BPF_A, 0x0),
6142 },
6143 CLASSIC,
6144 { [0x3c] = 0x25, [0x3d] = 0x05, [0x3e] = 0x19, [0x3f] = 0x82 },
6145 { {0x40, 0x24 }, },
6146 },
6147 {
6148 "LDX_MSH standalone, out of bounds",
6149 .u.insns = {
6150 BPF_STMT(BPF_LD | BPF_IMM, 0xffeebbaa),
6151 BPF_STMT(BPF_LDX | BPF_B | BPF_MSH, 0x40),
6152 BPF_STMT(BPF_MISC | BPF_TXA, 0),
6153 BPF_STMT(BPF_RET | BPF_A, 0x0),
6154 },
6155 CLASSIC,
6156 { [0x3c] = 0x25, [0x3d] = 0x05, [0x3e] = 0x19, [0x3f] = 0x82 },
6157 { {0x40, 0 }, },
6158 },
5942 /* 6159 /*
5943 * verify that the interpreter or JIT correctly sets A and X 6160 * verify that the interpreter or JIT correctly sets A and X
5944 * to 0. 6161 * to 0.
@@ -6127,14 +6344,6 @@ static struct bpf_test tests[] = {
6127 {}, 6344 {},
6128 { {0x1, 0x42 } }, 6345 { {0x1, 0x42 } },
6129 }, 6346 },
6130 {
6131 "LD_ABS with helper changing skb data",
6132 { },
6133 INTERNAL,
6134 { 0x34 },
6135 { { ETH_HLEN, 42 } },
6136 .fill_helper = bpf_fill_ld_abs_vlan_push_pop2,
6137 },
6138 /* Checking interpreter vs JIT wrt signed extended imms. */ 6347 /* Checking interpreter vs JIT wrt signed extended imms. */
6139 { 6348 {
6140 "JNE signed compare, test 1", 6349 "JNE signed compare, test 1",
diff --git a/lib/test_overflow.c b/lib/test_overflow.c
new file mode 100644
index 000000000000..aecbbb217305
--- /dev/null
+++ b/lib/test_overflow.c
@@ -0,0 +1,417 @@
1// SPDX-License-Identifier: GPL-2.0 OR MIT
2/*
3 * Test cases for arithmetic overflow checks.
4 */
5#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
6
7#include <linux/device.h>
8#include <linux/init.h>
9#include <linux/kernel.h>
10#include <linux/mm.h>
11#include <linux/module.h>
12#include <linux/overflow.h>
13#include <linux/slab.h>
14#include <linux/types.h>
15#include <linux/vmalloc.h>
16
17#define DEFINE_TEST_ARRAY(t) \
18 static const struct test_ ## t { \
19 t a, b; \
20 t sum, diff, prod; \
21 bool s_of, d_of, p_of; \
22 } t ## _tests[] __initconst
23
24DEFINE_TEST_ARRAY(u8) = {
25 {0, 0, 0, 0, 0, false, false, false},
26 {1, 1, 2, 0, 1, false, false, false},
27 {0, 1, 1, U8_MAX, 0, false, true, false},
28 {1, 0, 1, 1, 0, false, false, false},
29 {0, U8_MAX, U8_MAX, 1, 0, false, true, false},
30 {U8_MAX, 0, U8_MAX, U8_MAX, 0, false, false, false},
31 {1, U8_MAX, 0, 2, U8_MAX, true, true, false},
32 {U8_MAX, 1, 0, U8_MAX-1, U8_MAX, true, false, false},
33 {U8_MAX, U8_MAX, U8_MAX-1, 0, 1, true, false, true},
34
35 {U8_MAX, U8_MAX-1, U8_MAX-2, 1, 2, true, false, true},
36 {U8_MAX-1, U8_MAX, U8_MAX-2, U8_MAX, 2, true, true, true},
37
38 {1U << 3, 1U << 3, 1U << 4, 0, 1U << 6, false, false, false},
39 {1U << 4, 1U << 4, 1U << 5, 0, 0, false, false, true},
40 {1U << 4, 1U << 3, 3*(1U << 3), 1U << 3, 1U << 7, false, false, false},
41 {1U << 7, 1U << 7, 0, 0, 0, true, false, true},
42
43 {48, 32, 80, 16, 0, false, false, true},
44 {128, 128, 0, 0, 0, true, false, true},
45 {123, 234, 101, 145, 110, true, true, true},
46};
47DEFINE_TEST_ARRAY(u16) = {
48 {0, 0, 0, 0, 0, false, false, false},
49 {1, 1, 2, 0, 1, false, false, false},
50 {0, 1, 1, U16_MAX, 0, false, true, false},
51 {1, 0, 1, 1, 0, false, false, false},
52 {0, U16_MAX, U16_MAX, 1, 0, false, true, false},
53 {U16_MAX, 0, U16_MAX, U16_MAX, 0, false, false, false},
54 {1, U16_MAX, 0, 2, U16_MAX, true, true, false},
55 {U16_MAX, 1, 0, U16_MAX-1, U16_MAX, true, false, false},
56 {U16_MAX, U16_MAX, U16_MAX-1, 0, 1, true, false, true},
57
58 {U16_MAX, U16_MAX-1, U16_MAX-2, 1, 2, true, false, true},
59 {U16_MAX-1, U16_MAX, U16_MAX-2, U16_MAX, 2, true, true, true},
60
61 {1U << 7, 1U << 7, 1U << 8, 0, 1U << 14, false, false, false},
62 {1U << 8, 1U << 8, 1U << 9, 0, 0, false, false, true},
63 {1U << 8, 1U << 7, 3*(1U << 7), 1U << 7, 1U << 15, false, false, false},
64 {1U << 15, 1U << 15, 0, 0, 0, true, false, true},
65
66 {123, 234, 357, 65425, 28782, false, true, false},
67 {1234, 2345, 3579, 64425, 10146, false, true, true},
68};
69DEFINE_TEST_ARRAY(u32) = {
70 {0, 0, 0, 0, 0, false, false, false},
71 {1, 1, 2, 0, 1, false, false, false},
72 {0, 1, 1, U32_MAX, 0, false, true, false},
73 {1, 0, 1, 1, 0, false, false, false},
74 {0, U32_MAX, U32_MAX, 1, 0, false, true, false},
75 {U32_MAX, 0, U32_MAX, U32_MAX, 0, false, false, false},
76 {1, U32_MAX, 0, 2, U32_MAX, true, true, false},
77 {U32_MAX, 1, 0, U32_MAX-1, U32_MAX, true, false, false},
78 {U32_MAX, U32_MAX, U32_MAX-1, 0, 1, true, false, true},
79
80 {U32_MAX, U32_MAX-1, U32_MAX-2, 1, 2, true, false, true},
81 {U32_MAX-1, U32_MAX, U32_MAX-2, U32_MAX, 2, true, true, true},
82
83 {1U << 15, 1U << 15, 1U << 16, 0, 1U << 30, false, false, false},
84 {1U << 16, 1U << 16, 1U << 17, 0, 0, false, false, true},
85 {1U << 16, 1U << 15, 3*(1U << 15), 1U << 15, 1U << 31, false, false, false},
86 {1U << 31, 1U << 31, 0, 0, 0, true, false, true},
87
88 {-2U, 1U, -1U, -3U, -2U, false, false, false},
89 {-4U, 5U, 1U, -9U, -20U, true, false, true},
90};
91
92DEFINE_TEST_ARRAY(u64) = {
93 {0, 0, 0, 0, 0, false, false, false},
94 {1, 1, 2, 0, 1, false, false, false},
95 {0, 1, 1, U64_MAX, 0, false, true, false},
96 {1, 0, 1, 1, 0, false, false, false},
97 {0, U64_MAX, U64_MAX, 1, 0, false, true, false},
98 {U64_MAX, 0, U64_MAX, U64_MAX, 0, false, false, false},
99 {1, U64_MAX, 0, 2, U64_MAX, true, true, false},
100 {U64_MAX, 1, 0, U64_MAX-1, U64_MAX, true, false, false},
101 {U64_MAX, U64_MAX, U64_MAX-1, 0, 1, true, false, true},
102
103 {U64_MAX, U64_MAX-1, U64_MAX-2, 1, 2, true, false, true},
104 {U64_MAX-1, U64_MAX, U64_MAX-2, U64_MAX, 2, true, true, true},
105
106 {1ULL << 31, 1ULL << 31, 1ULL << 32, 0, 1ULL << 62, false, false, false},
107 {1ULL << 32, 1ULL << 32, 1ULL << 33, 0, 0, false, false, true},
108 {1ULL << 32, 1ULL << 31, 3*(1ULL << 31), 1ULL << 31, 1ULL << 63, false, false, false},
109 {1ULL << 63, 1ULL << 63, 0, 0, 0, true, false, true},
110 {1000000000ULL /* 10^9 */, 10000000000ULL /* 10^10 */,
111 11000000000ULL, 18446744064709551616ULL, 10000000000000000000ULL,
112 false, true, false},
113 {-15ULL, 10ULL, -5ULL, -25ULL, -150ULL, false, false, true},
114};
115
116DEFINE_TEST_ARRAY(s8) = {
117 {0, 0, 0, 0, 0, false, false, false},
118
119 {0, S8_MAX, S8_MAX, -S8_MAX, 0, false, false, false},
120 {S8_MAX, 0, S8_MAX, S8_MAX, 0, false, false, false},
121 {0, S8_MIN, S8_MIN, S8_MIN, 0, false, true, false},
122 {S8_MIN, 0, S8_MIN, S8_MIN, 0, false, false, false},
123
124 {-1, S8_MIN, S8_MAX, S8_MAX, S8_MIN, true, false, true},
125 {S8_MIN, -1, S8_MAX, -S8_MAX, S8_MIN, true, false, true},
126 {-1, S8_MAX, S8_MAX-1, S8_MIN, -S8_MAX, false, false, false},
127 {S8_MAX, -1, S8_MAX-1, S8_MIN, -S8_MAX, false, true, false},
128 {-1, -S8_MAX, S8_MIN, S8_MAX-1, S8_MAX, false, false, false},
129 {-S8_MAX, -1, S8_MIN, S8_MIN+2, S8_MAX, false, false, false},
130
131 {1, S8_MIN, -S8_MAX, -S8_MAX, S8_MIN, false, true, false},
132 {S8_MIN, 1, -S8_MAX, S8_MAX, S8_MIN, false, true, false},
133 {1, S8_MAX, S8_MIN, S8_MIN+2, S8_MAX, true, false, false},
134 {S8_MAX, 1, S8_MIN, S8_MAX-1, S8_MAX, true, false, false},
135
136 {S8_MIN, S8_MIN, 0, 0, 0, true, false, true},
137 {S8_MAX, S8_MAX, -2, 0, 1, true, false, true},
138
139 {-4, -32, -36, 28, -128, false, false, true},
140 {-4, 32, 28, -36, -128, false, false, false},
141};
142
143DEFINE_TEST_ARRAY(s16) = {
144 {0, 0, 0, 0, 0, false, false, false},
145
146 {0, S16_MAX, S16_MAX, -S16_MAX, 0, false, false, false},
147 {S16_MAX, 0, S16_MAX, S16_MAX, 0, false, false, false},
148 {0, S16_MIN, S16_MIN, S16_MIN, 0, false, true, false},
149 {S16_MIN, 0, S16_MIN, S16_MIN, 0, false, false, false},
150
151 {-1, S16_MIN, S16_MAX, S16_MAX, S16_MIN, true, false, true},
152 {S16_MIN, -1, S16_MAX, -S16_MAX, S16_MIN, true, false, true},
153 {-1, S16_MAX, S16_MAX-1, S16_MIN, -S16_MAX, false, false, false},
154 {S16_MAX, -1, S16_MAX-1, S16_MIN, -S16_MAX, false, true, false},
155 {-1, -S16_MAX, S16_MIN, S16_MAX-1, S16_MAX, false, false, false},
156 {-S16_MAX, -1, S16_MIN, S16_MIN+2, S16_MAX, false, false, false},
157
158 {1, S16_MIN, -S16_MAX, -S16_MAX, S16_MIN, false, true, false},
159 {S16_MIN, 1, -S16_MAX, S16_MAX, S16_MIN, false, true, false},
160 {1, S16_MAX, S16_MIN, S16_MIN+2, S16_MAX, true, false, false},
161 {S16_MAX, 1, S16_MIN, S16_MAX-1, S16_MAX, true, false, false},
162
163 {S16_MIN, S16_MIN, 0, 0, 0, true, false, true},
164 {S16_MAX, S16_MAX, -2, 0, 1, true, false, true},
165};
166DEFINE_TEST_ARRAY(s32) = {
167 {0, 0, 0, 0, 0, false, false, false},
168
169 {0, S32_MAX, S32_MAX, -S32_MAX, 0, false, false, false},
170 {S32_MAX, 0, S32_MAX, S32_MAX, 0, false, false, false},
171 {0, S32_MIN, S32_MIN, S32_MIN, 0, false, true, false},
172 {S32_MIN, 0, S32_MIN, S32_MIN, 0, false, false, false},
173
174 {-1, S32_MIN, S32_MAX, S32_MAX, S32_MIN, true, false, true},
175 {S32_MIN, -1, S32_MAX, -S32_MAX, S32_MIN, true, false, true},
176 {-1, S32_MAX, S32_MAX-1, S32_MIN, -S32_MAX, false, false, false},
177 {S32_MAX, -1, S32_MAX-1, S32_MIN, -S32_MAX, false, true, false},
178 {-1, -S32_MAX, S32_MIN, S32_MAX-1, S32_MAX, false, false, false},
179 {-S32_MAX, -1, S32_MIN, S32_MIN+2, S32_MAX, false, false, false},
180
181 {1, S32_MIN, -S32_MAX, -S32_MAX, S32_MIN, false, true, false},
182 {S32_MIN, 1, -S32_MAX, S32_MAX, S32_MIN, false, true, false},
183 {1, S32_MAX, S32_MIN, S32_MIN+2, S32_MAX, true, false, false},
184 {S32_MAX, 1, S32_MIN, S32_MAX-1, S32_MAX, true, false, false},
185
186 {S32_MIN, S32_MIN, 0, 0, 0, true, false, true},
187 {S32_MAX, S32_MAX, -2, 0, 1, true, false, true},
188};
189DEFINE_TEST_ARRAY(s64) = {
190 {0, 0, 0, 0, 0, false, false, false},
191
192 {0, S64_MAX, S64_MAX, -S64_MAX, 0, false, false, false},
193 {S64_MAX, 0, S64_MAX, S64_MAX, 0, false, false, false},
194 {0, S64_MIN, S64_MIN, S64_MIN, 0, false, true, false},
195 {S64_MIN, 0, S64_MIN, S64_MIN, 0, false, false, false},
196
197 {-1, S64_MIN, S64_MAX, S64_MAX, S64_MIN, true, false, true},
198 {S64_MIN, -1, S64_MAX, -S64_MAX, S64_MIN, true, false, true},
199 {-1, S64_MAX, S64_MAX-1, S64_MIN, -S64_MAX, false, false, false},
200 {S64_MAX, -1, S64_MAX-1, S64_MIN, -S64_MAX, false, true, false},
201 {-1, -S64_MAX, S64_MIN, S64_MAX-1, S64_MAX, false, false, false},
202 {-S64_MAX, -1, S64_MIN, S64_MIN+2, S64_MAX, false, false, false},
203
204 {1, S64_MIN, -S64_MAX, -S64_MAX, S64_MIN, false, true, false},
205 {S64_MIN, 1, -S64_MAX, S64_MAX, S64_MIN, false, true, false},
206 {1, S64_MAX, S64_MIN, S64_MIN+2, S64_MAX, true, false, false},
207 {S64_MAX, 1, S64_MIN, S64_MAX-1, S64_MAX, true, false, false},
208
209 {S64_MIN, S64_MIN, 0, 0, 0, true, false, true},
210 {S64_MAX, S64_MAX, -2, 0, 1, true, false, true},
211
212 {-1, -1, -2, 0, 1, false, false, false},
213 {-1, -128, -129, 127, 128, false, false, false},
214 {-128, -1, -129, -127, 128, false, false, false},
215 {0, -S64_MAX, -S64_MAX, S64_MAX, 0, false, false, false},
216};
217
218#define check_one_op(t, fmt, op, sym, a, b, r, of) do { \
219 t _r; \
220 bool _of; \
221 \
222 _of = check_ ## op ## _overflow(a, b, &_r); \
223 if (_of != of) { \
224 pr_warn("expected "fmt" "sym" "fmt \
225 " to%s overflow (type %s)\n", \
226 a, b, of ? "" : " not", #t); \
227 err = 1; \
228 } \
229 if (_r != r) { \
230 pr_warn("expected "fmt" "sym" "fmt" == " \
231 fmt", got "fmt" (type %s)\n", \
232 a, b, r, _r, #t); \
233 err = 1; \
234 } \
235} while (0)
236
237#define DEFINE_TEST_FUNC(t, fmt) \
238static int __init do_test_ ## t(const struct test_ ## t *p) \
239{ \
240 int err = 0; \
241 \
242 check_one_op(t, fmt, add, "+", p->a, p->b, p->sum, p->s_of); \
243 check_one_op(t, fmt, add, "+", p->b, p->a, p->sum, p->s_of); \
244 check_one_op(t, fmt, sub, "-", p->a, p->b, p->diff, p->d_of); \
245 check_one_op(t, fmt, mul, "*", p->a, p->b, p->prod, p->p_of); \
246 check_one_op(t, fmt, mul, "*", p->b, p->a, p->prod, p->p_of); \
247 \
248 return err; \
249} \
250 \
251static int __init test_ ## t ## _overflow(void) { \
252 int err = 0; \
253 unsigned i; \
254 \
255 pr_info("%-3s: %zu tests\n", #t, ARRAY_SIZE(t ## _tests)); \
256 for (i = 0; i < ARRAY_SIZE(t ## _tests); ++i) \
257 err |= do_test_ ## t(&t ## _tests[i]); \
258 return err; \
259}
260
261DEFINE_TEST_FUNC(u8, "%d");
262DEFINE_TEST_FUNC(s8, "%d");
263DEFINE_TEST_FUNC(u16, "%d");
264DEFINE_TEST_FUNC(s16, "%d");
265DEFINE_TEST_FUNC(u32, "%u");
266DEFINE_TEST_FUNC(s32, "%d");
267#if BITS_PER_LONG == 64
268DEFINE_TEST_FUNC(u64, "%llu");
269DEFINE_TEST_FUNC(s64, "%lld");
270#endif
271
272static int __init test_overflow_calculation(void)
273{
274 int err = 0;
275
276 err |= test_u8_overflow();
277 err |= test_s8_overflow();
278 err |= test_u16_overflow();
279 err |= test_s16_overflow();
280 err |= test_u32_overflow();
281 err |= test_s32_overflow();
282#if BITS_PER_LONG == 64
283 err |= test_u64_overflow();
284 err |= test_s64_overflow();
285#endif
286
287 return err;
288}
289
290/*
291 * Deal with the various forms of allocator arguments. See comments above
292 * the DEFINE_TEST_ALLOC() instances for mapping of the "bits".
293 */
294#define alloc010(alloc, arg, sz) alloc(sz, GFP_KERNEL)
295#define alloc011(alloc, arg, sz) alloc(sz, GFP_KERNEL, NUMA_NO_NODE)
296#define alloc000(alloc, arg, sz) alloc(sz)
297#define alloc001(alloc, arg, sz) alloc(sz, NUMA_NO_NODE)
298#define alloc110(alloc, arg, sz) alloc(arg, sz, GFP_KERNEL)
299#define free0(free, arg, ptr) free(ptr)
300#define free1(free, arg, ptr) free(arg, ptr)
301
302/* Wrap around to 8K */
303#define TEST_SIZE (9 << PAGE_SHIFT)
304
305#define DEFINE_TEST_ALLOC(func, free_func, want_arg, want_gfp, want_node)\
306static int __init test_ ## func (void *arg) \
307{ \
308 volatile size_t a = TEST_SIZE; \
309 volatile size_t b = (SIZE_MAX / TEST_SIZE) + 1; \
310 void *ptr; \
311 \
312 /* Tiny allocation test. */ \
313 ptr = alloc ## want_arg ## want_gfp ## want_node (func, arg, 1);\
314 if (!ptr) { \
315 pr_warn(#func " failed regular allocation?!\n"); \
316 return 1; \
317 } \
318 free ## want_arg (free_func, arg, ptr); \
319 \
320 /* Wrapped allocation test. */ \
321 ptr = alloc ## want_arg ## want_gfp ## want_node (func, arg, \
322 a * b); \
323 if (!ptr) { \
324 pr_warn(#func " unexpectedly failed bad wrapping?!\n"); \
325 return 1; \
326 } \
327 free ## want_arg (free_func, arg, ptr); \
328 \
329 /* Saturated allocation test. */ \
330 ptr = alloc ## want_arg ## want_gfp ## want_node (func, arg, \
331 array_size(a, b)); \
332 if (ptr) { \
333 pr_warn(#func " missed saturation!\n"); \
334 free ## want_arg (free_func, arg, ptr); \
335 return 1; \
336 } \
337 pr_info(#func " detected saturation\n"); \
338 return 0; \
339}
340
341/*
342 * Allocator uses a trailing node argument --------+ (e.g. kmalloc_node())
343 * Allocator uses the gfp_t argument -----------+ | (e.g. kmalloc())
344 * Allocator uses a special leading argument + | | (e.g. devm_kmalloc())
345 * | | |
346 */
347DEFINE_TEST_ALLOC(kmalloc, kfree, 0, 1, 0);
348DEFINE_TEST_ALLOC(kmalloc_node, kfree, 0, 1, 1);
349DEFINE_TEST_ALLOC(kzalloc, kfree, 0, 1, 0);
350DEFINE_TEST_ALLOC(kzalloc_node, kfree, 0, 1, 1);
351DEFINE_TEST_ALLOC(vmalloc, vfree, 0, 0, 0);
352DEFINE_TEST_ALLOC(vmalloc_node, vfree, 0, 0, 1);
353DEFINE_TEST_ALLOC(vzalloc, vfree, 0, 0, 0);
354DEFINE_TEST_ALLOC(vzalloc_node, vfree, 0, 0, 1);
355DEFINE_TEST_ALLOC(kvmalloc, kvfree, 0, 1, 0);
356DEFINE_TEST_ALLOC(kvmalloc_node, kvfree, 0, 1, 1);
357DEFINE_TEST_ALLOC(kvzalloc, kvfree, 0, 1, 0);
358DEFINE_TEST_ALLOC(kvzalloc_node, kvfree, 0, 1, 1);
359DEFINE_TEST_ALLOC(devm_kmalloc, devm_kfree, 1, 1, 0);
360DEFINE_TEST_ALLOC(devm_kzalloc, devm_kfree, 1, 1, 0);
361
362static int __init test_overflow_allocation(void)
363{
364 const char device_name[] = "overflow-test";
365 struct device *dev;
366 int err = 0;
367
368 /* Create dummy device for devm_kmalloc()-family tests. */
369 dev = root_device_register(device_name);
370 if (!dev) {
371 pr_warn("Cannot register test device\n");
372 return 1;
373 }
374
375 err |= test_kmalloc(NULL);
376 err |= test_kmalloc_node(NULL);
377 err |= test_kzalloc(NULL);
378 err |= test_kzalloc_node(NULL);
379 err |= test_kvmalloc(NULL);
380 err |= test_kvmalloc_node(NULL);
381 err |= test_kvzalloc(NULL);
382 err |= test_kvzalloc_node(NULL);
383 err |= test_vmalloc(NULL);
384 err |= test_vmalloc_node(NULL);
385 err |= test_vzalloc(NULL);
386 err |= test_vzalloc_node(NULL);
387 err |= test_devm_kmalloc(dev);
388 err |= test_devm_kzalloc(dev);
389
390 device_unregister(dev);
391
392 return err;
393}
394
395static int __init test_module_init(void)
396{
397 int err = 0;
398
399 err |= test_overflow_calculation();
400 err |= test_overflow_allocation();
401
402 if (err) {
403 pr_warn("FAIL!\n");
404 err = -EINVAL;
405 } else {
406 pr_info("all tests passed\n");
407 }
408
409 return err;
410}
411
412static void __exit test_module_exit(void)
413{ }
414
415module_init(test_module_init);
416module_exit(test_module_exit);
417MODULE_LICENSE("Dual MIT/GPL");
diff --git a/lib/test_printf.c b/lib/test_printf.c
index 71ebfa43ad05..cea592f402ed 100644
--- a/lib/test_printf.c
+++ b/lib/test_printf.c
@@ -204,7 +204,7 @@ test_string(void)
204#if BITS_PER_LONG == 64 204#if BITS_PER_LONG == 64
205 205
206#define PTR_WIDTH 16 206#define PTR_WIDTH 16
207#define PTR ((void *)0xffff0123456789ab) 207#define PTR ((void *)0xffff0123456789abUL)
208#define PTR_STR "ffff0123456789ab" 208#define PTR_STR "ffff0123456789ab"
209#define ZEROS "00000000" /* hex 32 zero bits */ 209#define ZEROS "00000000" /* hex 32 zero bits */
210 210
diff --git a/lib/ucs2_string.c b/lib/ucs2_string.c
index d7e06b28de38..0a559a42359b 100644
--- a/lib/ucs2_string.c
+++ b/lib/ucs2_string.c
@@ -112,3 +112,5 @@ ucs2_as_utf8(u8 *dest, const ucs2_char_t *src, unsigned long maxlength)
112 return j; 112 return j;
113} 113}
114EXPORT_SYMBOL(ucs2_as_utf8); 114EXPORT_SYMBOL(ucs2_as_utf8);
115
116MODULE_LICENSE("GPL v2");
diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index 23920c5ff728..a48aaa79d352 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -703,6 +703,22 @@ char *symbol_string(char *buf, char *end, void *ptr,
703#endif 703#endif
704} 704}
705 705
706static const struct printf_spec default_str_spec = {
707 .field_width = -1,
708 .precision = -1,
709};
710
711static const struct printf_spec default_flag_spec = {
712 .base = 16,
713 .precision = -1,
714 .flags = SPECIAL | SMALL,
715};
716
717static const struct printf_spec default_dec_spec = {
718 .base = 10,
719 .precision = -1,
720};
721
706static noinline_for_stack 722static noinline_for_stack
707char *resource_string(char *buf, char *end, struct resource *res, 723char *resource_string(char *buf, char *end, struct resource *res,
708 struct printf_spec spec, const char *fmt) 724 struct printf_spec spec, const char *fmt)
@@ -732,21 +748,11 @@ char *resource_string(char *buf, char *end, struct resource *res,
732 .precision = -1, 748 .precision = -1,
733 .flags = SMALL | ZEROPAD, 749 .flags = SMALL | ZEROPAD,
734 }; 750 };
735 static const struct printf_spec dec_spec = {
736 .base = 10,
737 .precision = -1,
738 .flags = 0,
739 };
740 static const struct printf_spec str_spec = { 751 static const struct printf_spec str_spec = {
741 .field_width = -1, 752 .field_width = -1,
742 .precision = 10, 753 .precision = 10,
743 .flags = LEFT, 754 .flags = LEFT,
744 }; 755 };
745 static const struct printf_spec flag_spec = {
746 .base = 16,
747 .precision = -1,
748 .flags = SPECIAL | SMALL,
749 };
750 756
751 /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8) 757 /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
752 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */ 758 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
@@ -770,10 +776,10 @@ char *resource_string(char *buf, char *end, struct resource *res,
770 specp = &mem_spec; 776 specp = &mem_spec;
771 } else if (res->flags & IORESOURCE_IRQ) { 777 } else if (res->flags & IORESOURCE_IRQ) {
772 p = string(p, pend, "irq ", str_spec); 778 p = string(p, pend, "irq ", str_spec);
773 specp = &dec_spec; 779 specp = &default_dec_spec;
774 } else if (res->flags & IORESOURCE_DMA) { 780 } else if (res->flags & IORESOURCE_DMA) {
775 p = string(p, pend, "dma ", str_spec); 781 p = string(p, pend, "dma ", str_spec);
776 specp = &dec_spec; 782 specp = &default_dec_spec;
777 } else if (res->flags & IORESOURCE_BUS) { 783 } else if (res->flags & IORESOURCE_BUS) {
778 p = string(p, pend, "bus ", str_spec); 784 p = string(p, pend, "bus ", str_spec);
779 specp = &bus_spec; 785 specp = &bus_spec;
@@ -803,7 +809,7 @@ char *resource_string(char *buf, char *end, struct resource *res,
803 p = string(p, pend, " disabled", str_spec); 809 p = string(p, pend, " disabled", str_spec);
804 } else { 810 } else {
805 p = string(p, pend, " flags ", str_spec); 811 p = string(p, pend, " flags ", str_spec);
806 p = number(p, pend, res->flags, flag_spec); 812 p = number(p, pend, res->flags, default_flag_spec);
807 } 813 }
808 *p++ = ']'; 814 *p++ = ']';
809 *p = '\0'; 815 *p = '\0';
@@ -913,9 +919,6 @@ char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap,
913 int cur, rbot, rtop; 919 int cur, rbot, rtop;
914 bool first = true; 920 bool first = true;
915 921
916 /* reused to print numbers */
917 spec = (struct printf_spec){ .base = 10 };
918
919 rbot = cur = find_first_bit(bitmap, nr_bits); 922 rbot = cur = find_first_bit(bitmap, nr_bits);
920 while (cur < nr_bits) { 923 while (cur < nr_bits) {
921 rtop = cur; 924 rtop = cur;
@@ -930,13 +933,13 @@ char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap,
930 } 933 }
931 first = false; 934 first = false;
932 935
933 buf = number(buf, end, rbot, spec); 936 buf = number(buf, end, rbot, default_dec_spec);
934 if (rbot < rtop) { 937 if (rbot < rtop) {
935 if (buf < end) 938 if (buf < end)
936 *buf = '-'; 939 *buf = '-';
937 buf++; 940 buf++;
938 941
939 buf = number(buf, end, rtop, spec); 942 buf = number(buf, end, rtop, default_dec_spec);
940 } 943 }
941 944
942 rbot = cur; 945 rbot = cur;
@@ -1354,11 +1357,9 @@ char *uuid_string(char *buf, char *end, const u8 *addr,
1354 return string(buf, end, uuid, spec); 1357 return string(buf, end, uuid, spec);
1355} 1358}
1356 1359
1357int kptr_restrict __read_mostly;
1358
1359static noinline_for_stack 1360static noinline_for_stack
1360char *restricted_pointer(char *buf, char *end, const void *ptr, 1361char *pointer_string(char *buf, char *end, const void *ptr,
1361 struct printf_spec spec) 1362 struct printf_spec spec)
1362{ 1363{
1363 spec.base = 16; 1364 spec.base = 16;
1364 spec.flags |= SMALL; 1365 spec.flags |= SMALL;
@@ -1367,6 +1368,15 @@ char *restricted_pointer(char *buf, char *end, const void *ptr,
1367 spec.flags |= ZEROPAD; 1368 spec.flags |= ZEROPAD;
1368 } 1369 }
1369 1370
1371 return number(buf, end, (unsigned long int)ptr, spec);
1372}
1373
1374int kptr_restrict __read_mostly;
1375
1376static noinline_for_stack
1377char *restricted_pointer(char *buf, char *end, const void *ptr,
1378 struct printf_spec spec)
1379{
1370 switch (kptr_restrict) { 1380 switch (kptr_restrict) {
1371 case 0: 1381 case 0:
1372 /* Always print %pK values */ 1382 /* Always print %pK values */
@@ -1378,8 +1388,11 @@ char *restricted_pointer(char *buf, char *end, const void *ptr,
1378 * kptr_restrict==1 cannot be used in IRQ context 1388 * kptr_restrict==1 cannot be used in IRQ context
1379 * because its test for CAP_SYSLOG would be meaningless. 1389 * because its test for CAP_SYSLOG would be meaningless.
1380 */ 1390 */
1381 if (in_irq() || in_serving_softirq() || in_nmi()) 1391 if (in_irq() || in_serving_softirq() || in_nmi()) {
1392 if (spec.field_width == -1)
1393 spec.field_width = 2 * sizeof(ptr);
1382 return string(buf, end, "pK-error", spec); 1394 return string(buf, end, "pK-error", spec);
1395 }
1383 1396
1384 /* 1397 /*
1385 * Only print the real pointer value if the current 1398 * Only print the real pointer value if the current
@@ -1404,7 +1417,7 @@ char *restricted_pointer(char *buf, char *end, const void *ptr,
1404 break; 1417 break;
1405 } 1418 }
1406 1419
1407 return number(buf, end, (unsigned long)ptr, spec); 1420 return pointer_string(buf, end, ptr, spec);
1408} 1421}
1409 1422
1410static noinline_for_stack 1423static noinline_for_stack
@@ -1456,9 +1469,6 @@ char *clock(char *buf, char *end, struct clk *clk, struct printf_spec spec,
1456 return string(buf, end, NULL, spec); 1469 return string(buf, end, NULL, spec);
1457 1470
1458 switch (fmt[1]) { 1471 switch (fmt[1]) {
1459 case 'r':
1460 return number(buf, end, clk_get_rate(clk), spec);
1461
1462 case 'n': 1472 case 'n':
1463 default: 1473 default:
1464#ifdef CONFIG_COMMON_CLK 1474#ifdef CONFIG_COMMON_CLK
@@ -1474,23 +1484,13 @@ char *format_flags(char *buf, char *end, unsigned long flags,
1474 const struct trace_print_flags *names) 1484 const struct trace_print_flags *names)
1475{ 1485{
1476 unsigned long mask; 1486 unsigned long mask;
1477 const struct printf_spec strspec = {
1478 .field_width = -1,
1479 .precision = -1,
1480 };
1481 const struct printf_spec numspec = {
1482 .flags = SPECIAL|SMALL,
1483 .field_width = -1,
1484 .precision = -1,
1485 .base = 16,
1486 };
1487 1487
1488 for ( ; flags && names->name; names++) { 1488 for ( ; flags && names->name; names++) {
1489 mask = names->mask; 1489 mask = names->mask;
1490 if ((flags & mask) != mask) 1490 if ((flags & mask) != mask)
1491 continue; 1491 continue;
1492 1492
1493 buf = string(buf, end, names->name, strspec); 1493 buf = string(buf, end, names->name, default_str_spec);
1494 1494
1495 flags &= ~mask; 1495 flags &= ~mask;
1496 if (flags) { 1496 if (flags) {
@@ -1501,7 +1501,7 @@ char *format_flags(char *buf, char *end, unsigned long flags,
1501 } 1501 }
1502 1502
1503 if (flags) 1503 if (flags)
1504 buf = number(buf, end, flags, numspec); 1504 buf = number(buf, end, flags, default_flag_spec);
1505 1505
1506 return buf; 1506 return buf;
1507} 1507}
@@ -1548,22 +1548,18 @@ char *device_node_gen_full_name(const struct device_node *np, char *buf, char *e
1548{ 1548{
1549 int depth; 1549 int depth;
1550 const struct device_node *parent = np->parent; 1550 const struct device_node *parent = np->parent;
1551 static const struct printf_spec strspec = {
1552 .field_width = -1,
1553 .precision = -1,
1554 };
1555 1551
1556 /* special case for root node */ 1552 /* special case for root node */
1557 if (!parent) 1553 if (!parent)
1558 return string(buf, end, "/", strspec); 1554 return string(buf, end, "/", default_str_spec);
1559 1555
1560 for (depth = 0; parent->parent; depth++) 1556 for (depth = 0; parent->parent; depth++)
1561 parent = parent->parent; 1557 parent = parent->parent;
1562 1558
1563 for ( ; depth >= 0; depth--) { 1559 for ( ; depth >= 0; depth--) {
1564 buf = string(buf, end, "/", strspec); 1560 buf = string(buf, end, "/", default_str_spec);
1565 buf = string(buf, end, device_node_name_for_depth(np, depth), 1561 buf = string(buf, end, device_node_name_for_depth(np, depth),
1566 strspec); 1562 default_str_spec);
1567 } 1563 }
1568 return buf; 1564 return buf;
1569} 1565}
@@ -1655,20 +1651,6 @@ char *device_node_string(char *buf, char *end, struct device_node *dn,
1655 return widen_string(buf, buf - buf_start, end, spec); 1651 return widen_string(buf, buf - buf_start, end, spec);
1656} 1652}
1657 1653
1658static noinline_for_stack
1659char *pointer_string(char *buf, char *end, const void *ptr,
1660 struct printf_spec spec)
1661{
1662 spec.base = 16;
1663 spec.flags |= SMALL;
1664 if (spec.field_width == -1) {
1665 spec.field_width = 2 * sizeof(ptr);
1666 spec.flags |= ZEROPAD;
1667 }
1668
1669 return number(buf, end, (unsigned long int)ptr, spec);
1670}
1671
1672static DEFINE_STATIC_KEY_TRUE(not_filled_random_ptr_key); 1654static DEFINE_STATIC_KEY_TRUE(not_filled_random_ptr_key);
1673static siphash_key_t ptr_key __read_mostly; 1655static siphash_key_t ptr_key __read_mostly;
1674 1656
@@ -1710,13 +1692,13 @@ early_initcall(initialize_ptr_random);
1710/* Maps a pointer to a 32 bit unique identifier. */ 1692/* Maps a pointer to a 32 bit unique identifier. */
1711static char *ptr_to_id(char *buf, char *end, void *ptr, struct printf_spec spec) 1693static char *ptr_to_id(char *buf, char *end, void *ptr, struct printf_spec spec)
1712{ 1694{
1695 const char *str = sizeof(ptr) == 8 ? "(____ptrval____)" : "(ptrval)";
1713 unsigned long hashval; 1696 unsigned long hashval;
1714 const int default_width = 2 * sizeof(ptr);
1715 1697
1716 if (static_branch_unlikely(&not_filled_random_ptr_key)) { 1698 if (static_branch_unlikely(&not_filled_random_ptr_key)) {
1717 spec.field_width = default_width; 1699 spec.field_width = 2 * sizeof(ptr);
1718 /* string length must be less than default_width */ 1700 /* string length must be less than default_width */
1719 return string(buf, end, "(ptrval)", spec); 1701 return string(buf, end, str, spec);
1720 } 1702 }
1721 1703
1722#ifdef CONFIG_64BIT 1704#ifdef CONFIG_64BIT
@@ -1729,15 +1711,7 @@ static char *ptr_to_id(char *buf, char *end, void *ptr, struct printf_spec spec)
1729#else 1711#else
1730 hashval = (unsigned long)siphash_1u32((u32)ptr, &ptr_key); 1712 hashval = (unsigned long)siphash_1u32((u32)ptr, &ptr_key);
1731#endif 1713#endif
1732 1714 return pointer_string(buf, end, (const void *)hashval, spec);
1733 spec.flags |= SMALL;
1734 if (spec.field_width == -1) {
1735 spec.field_width = default_width;
1736 spec.flags |= ZEROPAD;
1737 }
1738 spec.base = 16;
1739
1740 return number(buf, end, hashval, spec);
1741} 1715}
1742 1716
1743/* 1717/*
@@ -1750,10 +1724,10 @@ static char *ptr_to_id(char *buf, char *end, void *ptr, struct printf_spec spec)
1750 * 1724 *
1751 * Right now we handle: 1725 * Right now we handle:
1752 * 1726 *
1753 * - 'F' For symbolic function descriptor pointers with offset 1727 * - 'S' For symbolic direct pointers (or function descriptors) with offset
1754 * - 'f' For simple symbolic function names without offset 1728 * - 's' For symbolic direct pointers (or function descriptors) without offset
1755 * - 'S' For symbolic direct pointers with offset 1729 * - 'F' Same as 'S'
1756 * - 's' For symbolic direct pointers without offset 1730 * - 'f' Same as 's'
1757 * - '[FfSs]R' as above with __builtin_extract_return_addr() translation 1731 * - '[FfSs]R' as above with __builtin_extract_return_addr() translation
1758 * - 'B' For backtraced symbolic direct pointers with offset 1732 * - 'B' For backtraced symbolic direct pointers with offset
1759 * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref] 1733 * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
@@ -1850,10 +1824,6 @@ static char *ptr_to_id(char *buf, char *end, void *ptr, struct printf_spec spec)
1850 * ** When making changes please also update: 1824 * ** When making changes please also update:
1851 * Documentation/core-api/printk-formats.rst 1825 * Documentation/core-api/printk-formats.rst
1852 * 1826 *
1853 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
1854 * function pointers are really function descriptors, which contain a
1855 * pointer to the real address.
1856 *
1857 * Note: The default behaviour (unadorned %p) is to hash the address, 1827 * Note: The default behaviour (unadorned %p) is to hash the address,
1858 * rendering it useful as a unique identifier. 1828 * rendering it useful as a unique identifier.
1859 */ 1829 */
@@ -2129,6 +2099,7 @@ qualifier:
2129 2099
2130 case 'x': 2100 case 'x':
2131 spec->flags |= SMALL; 2101 spec->flags |= SMALL;
2102 /* fall through */
2132 2103
2133 case 'X': 2104 case 'X':
2134 spec->base = 16; 2105 spec->base = 16;
@@ -3087,8 +3058,10 @@ int vsscanf(const char *buf, const char *fmt, va_list args)
3087 break; 3058 break;
3088 case 'i': 3059 case 'i':
3089 base = 0; 3060 base = 0;
3061 /* fall through */
3090 case 'd': 3062 case 'd':
3091 is_sign = true; 3063 is_sign = true;
3064 /* fall through */
3092 case 'u': 3065 case 'u':
3093 break; 3066 break;
3094 case '%': 3067 case '%':