aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/Kconfig3
-rw-r--r--lib/Kconfig.debug46
-rw-r--r--lib/Makefile2
-rw-r--r--lib/cpumask.c4
-rw-r--r--lib/debug_locks.c2
-rw-r--r--lib/debugobjects.c127
-rw-r--r--lib/decompress_bunzip2.c1
-rw-r--r--lib/decompress_inflate.c1
-rw-r--r--lib/decompress_unlzma.c1
-rw-r--r--lib/idr.c46
-rw-r--r--lib/kobject.c3
-rw-r--r--lib/kobject_uevent.c4
-rw-r--r--lib/locking-selftest.c4
-rw-r--r--lib/scatterlist.c9
-rw-r--r--lib/swiotlb.c2
-rw-r--r--lib/vsprintf.c993
16 files changed, 988 insertions, 260 deletions
diff --git a/lib/Kconfig b/lib/Kconfig
index 2a9c69f34482..8ade0a7a91e0 100644
--- a/lib/Kconfig
+++ b/lib/Kconfig
@@ -2,6 +2,9 @@
2# Library configuration 2# Library configuration
3# 3#
4 4
5config BINARY_PRINTF
6 def_bool n
7
5menu "Library routines" 8menu "Library routines"
6 9
7config BITREVERSE 10config BITREVERSE
diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
index 9638d99644af..812c28207baf 100644
--- a/lib/Kconfig.debug
+++ b/lib/Kconfig.debug
@@ -186,6 +186,44 @@ config BOOTPARAM_SOFTLOCKUP_PANIC_VALUE
186 default 0 if !BOOTPARAM_SOFTLOCKUP_PANIC 186 default 0 if !BOOTPARAM_SOFTLOCKUP_PANIC
187 default 1 if BOOTPARAM_SOFTLOCKUP_PANIC 187 default 1 if BOOTPARAM_SOFTLOCKUP_PANIC
188 188
189config DETECT_HUNG_TASK
190 bool "Detect Hung Tasks"
191 depends on DEBUG_KERNEL
192 default DETECT_SOFTLOCKUP
193 help
194 Say Y here to enable the kernel to detect "hung tasks",
195 which are bugs that cause the task to be stuck in
196 uninterruptible "D" state indefinitiley.
197
198 When a hung task is detected, the kernel will print the
199 current stack trace (which you should report), but the
200 task will stay in uninterruptible state. If lockdep is
201 enabled then all held locks will also be reported. This
202 feature has negligible overhead.
203
204config BOOTPARAM_HUNG_TASK_PANIC
205 bool "Panic (Reboot) On Hung Tasks"
206 depends on DETECT_HUNG_TASK
207 help
208 Say Y here to enable the kernel to panic on "hung tasks",
209 which are bugs that cause the kernel to leave a task stuck
210 in uninterruptible "D" state.
211
212 The panic can be used in combination with panic_timeout,
213 to cause the system to reboot automatically after a
214 hung task has been detected. This feature is useful for
215 high-availability systems that have uptime guarantees and
216 where a hung tasks must be resolved ASAP.
217
218 Say N if unsure.
219
220config BOOTPARAM_HUNG_TASK_PANIC_VALUE
221 int
222 depends on DETECT_HUNG_TASK
223 range 0 1
224 default 0 if !BOOTPARAM_HUNG_TASK_PANIC
225 default 1 if BOOTPARAM_HUNG_TASK_PANIC
226
189config SCHED_DEBUG 227config SCHED_DEBUG
190 bool "Collect scheduler debugging info" 228 bool "Collect scheduler debugging info"
191 depends on DEBUG_KERNEL && PROC_FS 229 depends on DEBUG_KERNEL && PROC_FS
@@ -402,7 +440,7 @@ config LOCKDEP
402 bool 440 bool
403 depends on DEBUG_KERNEL && TRACE_IRQFLAGS_SUPPORT && STACKTRACE_SUPPORT && LOCKDEP_SUPPORT 441 depends on DEBUG_KERNEL && TRACE_IRQFLAGS_SUPPORT && STACKTRACE_SUPPORT && LOCKDEP_SUPPORT
404 select STACKTRACE 442 select STACKTRACE
405 select FRAME_POINTER if !X86 && !MIPS && !PPC && !ARM_UNWIND 443 select FRAME_POINTER if !X86 && !MIPS && !PPC && !ARM_UNWIND && !S390
406 select KALLSYMS 444 select KALLSYMS
407 select KALLSYMS_ALL 445 select KALLSYMS_ALL
408 446
@@ -582,7 +620,7 @@ config ARCH_WANT_FRAME_POINTERS
582config FRAME_POINTER 620config FRAME_POINTER
583 bool "Compile the kernel with frame pointers" 621 bool "Compile the kernel with frame pointers"
584 depends on DEBUG_KERNEL && \ 622 depends on DEBUG_KERNEL && \
585 (CRIS || M68K || M68KNOMMU || FRV || UML || S390 || \ 623 (CRIS || M68K || M68KNOMMU || FRV || UML || \
586 AVR32 || SUPERH || BLACKFIN || MN10300) || \ 624 AVR32 || SUPERH || BLACKFIN || MN10300) || \
587 ARCH_WANT_FRAME_POINTERS 625 ARCH_WANT_FRAME_POINTERS
588 default y if (DEBUG_INFO && UML) || ARCH_WANT_FRAME_POINTERS 626 default y if (DEBUG_INFO && UML) || ARCH_WANT_FRAME_POINTERS
@@ -771,13 +809,13 @@ config FAULT_INJECTION_STACKTRACE_FILTER
771 depends on FAULT_INJECTION_DEBUG_FS && STACKTRACE_SUPPORT 809 depends on FAULT_INJECTION_DEBUG_FS && STACKTRACE_SUPPORT
772 depends on !X86_64 810 depends on !X86_64
773 select STACKTRACE 811 select STACKTRACE
774 select FRAME_POINTER if !PPC 812 select FRAME_POINTER if !PPC && !S390
775 help 813 help
776 Provide stacktrace filter for fault-injection capabilities 814 Provide stacktrace filter for fault-injection capabilities
777 815
778config LATENCYTOP 816config LATENCYTOP
779 bool "Latency measuring infrastructure" 817 bool "Latency measuring infrastructure"
780 select FRAME_POINTER if !MIPS && !PPC 818 select FRAME_POINTER if !MIPS && !PPC && !S390
781 select KALLSYMS 819 select KALLSYMS
782 select KALLSYMS_ALL 820 select KALLSYMS_ALL
783 select STACKTRACE 821 select STACKTRACE
diff --git a/lib/Makefile b/lib/Makefile
index d6edd6753f40..33a40e40e3ee 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -38,7 +38,7 @@ lib-$(CONFIG_RWSEM_GENERIC_SPINLOCK) += rwsem-spinlock.o
38lib-$(CONFIG_RWSEM_XCHGADD_ALGORITHM) += rwsem.o 38lib-$(CONFIG_RWSEM_XCHGADD_ALGORITHM) += rwsem.o
39lib-$(CONFIG_GENERIC_FIND_FIRST_BIT) += find_next_bit.o 39lib-$(CONFIG_GENERIC_FIND_FIRST_BIT) += find_next_bit.o
40lib-$(CONFIG_GENERIC_FIND_NEXT_BIT) += find_next_bit.o 40lib-$(CONFIG_GENERIC_FIND_NEXT_BIT) += find_next_bit.o
41lib-$(CONFIG_GENERIC_FIND_LAST_BIT) += find_last_bit.o 41obj-$(CONFIG_GENERIC_FIND_LAST_BIT) += find_last_bit.o
42obj-$(CONFIG_GENERIC_HWEIGHT) += hweight.o 42obj-$(CONFIG_GENERIC_HWEIGHT) += hweight.o
43obj-$(CONFIG_LOCK_KERNEL) += kernel_lock.o 43obj-$(CONFIG_LOCK_KERNEL) += kernel_lock.o
44obj-$(CONFIG_DEBUG_PREEMPT) += smp_processor_id.o 44obj-$(CONFIG_DEBUG_PREEMPT) += smp_processor_id.o
diff --git a/lib/cpumask.c b/lib/cpumask.c
index 3389e2440da0..1f71b97de0f9 100644
--- a/lib/cpumask.c
+++ b/lib/cpumask.c
@@ -109,10 +109,10 @@ bool alloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, int node)
109#endif 109#endif
110 /* FIXME: Bandaid to save us from old primitives which go to NR_CPUS. */ 110 /* FIXME: Bandaid to save us from old primitives which go to NR_CPUS. */
111 if (*mask) { 111 if (*mask) {
112 unsigned char *ptr = (unsigned char *)cpumask_bits(*mask);
112 unsigned int tail; 113 unsigned int tail;
113 tail = BITS_TO_LONGS(NR_CPUS - nr_cpumask_bits) * sizeof(long); 114 tail = BITS_TO_LONGS(NR_CPUS - nr_cpumask_bits) * sizeof(long);
114 memset(cpumask_bits(*mask) + cpumask_size() - tail, 115 memset(ptr + cpumask_size() - tail, 0, tail);
115 0, tail);
116 } 116 }
117 117
118 return *mask != NULL; 118 return *mask != NULL;
diff --git a/lib/debug_locks.c b/lib/debug_locks.c
index 0218b4693dd8..bc3b11731b9c 100644
--- a/lib/debug_locks.c
+++ b/lib/debug_locks.c
@@ -36,7 +36,7 @@ int debug_locks_silent;
36 */ 36 */
37int debug_locks_off(void) 37int debug_locks_off(void)
38{ 38{
39 if (xchg(&debug_locks, 0)) { 39 if (__debug_locks_off()) {
40 if (!debug_locks_silent) { 40 if (!debug_locks_silent) {
41 oops_in_progress = 1; 41 oops_in_progress = 1;
42 console_verbose(); 42 console_verbose();
diff --git a/lib/debugobjects.c b/lib/debugobjects.c
index 5d99be1fd988..2755a3bd16a1 100644
--- a/lib/debugobjects.c
+++ b/lib/debugobjects.c
@@ -30,7 +30,7 @@ struct debug_bucket {
30 30
31static struct debug_bucket obj_hash[ODEBUG_HASH_SIZE]; 31static struct debug_bucket obj_hash[ODEBUG_HASH_SIZE];
32 32
33static struct debug_obj obj_static_pool[ODEBUG_POOL_SIZE]; 33static struct debug_obj obj_static_pool[ODEBUG_POOL_SIZE] __initdata;
34 34
35static DEFINE_SPINLOCK(pool_lock); 35static DEFINE_SPINLOCK(pool_lock);
36 36
@@ -50,12 +50,23 @@ static int debug_objects_enabled __read_mostly
50 50
51static struct debug_obj_descr *descr_test __read_mostly; 51static struct debug_obj_descr *descr_test __read_mostly;
52 52
53static void free_obj_work(struct work_struct *work);
54static DECLARE_WORK(debug_obj_work, free_obj_work);
55
53static int __init enable_object_debug(char *str) 56static int __init enable_object_debug(char *str)
54{ 57{
55 debug_objects_enabled = 1; 58 debug_objects_enabled = 1;
56 return 0; 59 return 0;
57} 60}
61
62static int __init disable_object_debug(char *str)
63{
64 debug_objects_enabled = 0;
65 return 0;
66}
67
58early_param("debug_objects", enable_object_debug); 68early_param("debug_objects", enable_object_debug);
69early_param("no_debug_objects", disable_object_debug);
59 70
60static const char *obj_states[ODEBUG_STATE_MAX] = { 71static const char *obj_states[ODEBUG_STATE_MAX] = {
61 [ODEBUG_STATE_NONE] = "none", 72 [ODEBUG_STATE_NONE] = "none",
@@ -146,25 +157,51 @@ alloc_object(void *addr, struct debug_bucket *b, struct debug_obj_descr *descr)
146} 157}
147 158
148/* 159/*
149 * Put the object back into the pool or give it back to kmem_cache: 160 * workqueue function to free objects.
150 */ 161 */
151static void free_object(struct debug_obj *obj) 162static void free_obj_work(struct work_struct *work)
152{ 163{
153 unsigned long idx = (unsigned long)(obj - obj_static_pool); 164 struct debug_obj *obj;
154 unsigned long flags; 165 unsigned long flags;
155 166
156 if (obj_pool_free < ODEBUG_POOL_SIZE || idx < ODEBUG_POOL_SIZE) { 167 spin_lock_irqsave(&pool_lock, flags);
157 spin_lock_irqsave(&pool_lock, flags); 168 while (obj_pool_free > ODEBUG_POOL_SIZE) {
158 hlist_add_head(&obj->node, &obj_pool); 169 obj = hlist_entry(obj_pool.first, typeof(*obj), node);
159 obj_pool_free++; 170 hlist_del(&obj->node);
160 obj_pool_used--; 171 obj_pool_free--;
161 spin_unlock_irqrestore(&pool_lock, flags); 172 /*
162 } else { 173 * We release pool_lock across kmem_cache_free() to
163 spin_lock_irqsave(&pool_lock, flags); 174 * avoid contention on pool_lock.
164 obj_pool_used--; 175 */
165 spin_unlock_irqrestore(&pool_lock, flags); 176 spin_unlock_irqrestore(&pool_lock, flags);
166 kmem_cache_free(obj_cache, obj); 177 kmem_cache_free(obj_cache, obj);
178 spin_lock_irqsave(&pool_lock, flags);
167 } 179 }
180 spin_unlock_irqrestore(&pool_lock, flags);
181}
182
183/*
184 * Put the object back into the pool and schedule work to free objects
185 * if necessary.
186 */
187static void free_object(struct debug_obj *obj)
188{
189 unsigned long flags;
190 int sched = 0;
191
192 spin_lock_irqsave(&pool_lock, flags);
193 /*
194 * schedule work when the pool is filled and the cache is
195 * initialized:
196 */
197 if (obj_pool_free > ODEBUG_POOL_SIZE && obj_cache)
198 sched = !work_pending(&debug_obj_work);
199 hlist_add_head(&obj->node, &obj_pool);
200 obj_pool_free++;
201 obj_pool_used--;
202 spin_unlock_irqrestore(&pool_lock, flags);
203 if (sched)
204 schedule_work(&debug_obj_work);
168} 205}
169 206
170/* 207/*
@@ -876,6 +913,63 @@ void __init debug_objects_early_init(void)
876} 913}
877 914
878/* 915/*
916 * Convert the statically allocated objects to dynamic ones:
917 */
918static int debug_objects_replace_static_objects(void)
919{
920 struct debug_bucket *db = obj_hash;
921 struct hlist_node *node, *tmp;
922 struct debug_obj *obj, *new;
923 HLIST_HEAD(objects);
924 int i, cnt = 0;
925
926 for (i = 0; i < ODEBUG_POOL_SIZE; i++) {
927 obj = kmem_cache_zalloc(obj_cache, GFP_KERNEL);
928 if (!obj)
929 goto free;
930 hlist_add_head(&obj->node, &objects);
931 }
932
933 /*
934 * When debug_objects_mem_init() is called we know that only
935 * one CPU is up, so disabling interrupts is enough
936 * protection. This avoids the lockdep hell of lock ordering.
937 */
938 local_irq_disable();
939
940 /* Remove the statically allocated objects from the pool */
941 hlist_for_each_entry_safe(obj, node, tmp, &obj_pool, node)
942 hlist_del(&obj->node);
943 /* Move the allocated objects to the pool */
944 hlist_move_list(&objects, &obj_pool);
945
946 /* Replace the active object references */
947 for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
948 hlist_move_list(&db->list, &objects);
949
950 hlist_for_each_entry(obj, node, &objects, node) {
951 new = hlist_entry(obj_pool.first, typeof(*obj), node);
952 hlist_del(&new->node);
953 /* copy object data */
954 *new = *obj;
955 hlist_add_head(&new->node, &db->list);
956 cnt++;
957 }
958 }
959
960 printk(KERN_DEBUG "ODEBUG: %d of %d active objects replaced\n", cnt,
961 obj_pool_used);
962 local_irq_enable();
963 return 0;
964free:
965 hlist_for_each_entry_safe(obj, node, tmp, &objects, node) {
966 hlist_del(&obj->node);
967 kmem_cache_free(obj_cache, obj);
968 }
969 return -ENOMEM;
970}
971
972/*
879 * Called after the kmem_caches are functional to setup a dedicated 973 * Called after the kmem_caches are functional to setup a dedicated
880 * cache pool, which has the SLAB_DEBUG_OBJECTS flag set. This flag 974 * cache pool, which has the SLAB_DEBUG_OBJECTS flag set. This flag
881 * prevents that the debug code is called on kmem_cache_free() for the 975 * prevents that the debug code is called on kmem_cache_free() for the
@@ -890,8 +984,11 @@ void __init debug_objects_mem_init(void)
890 sizeof (struct debug_obj), 0, 984 sizeof (struct debug_obj), 0,
891 SLAB_DEBUG_OBJECTS, NULL); 985 SLAB_DEBUG_OBJECTS, NULL);
892 986
893 if (!obj_cache) 987 if (!obj_cache || debug_objects_replace_static_objects()) {
894 debug_objects_enabled = 0; 988 debug_objects_enabled = 0;
895 else 989 if (obj_cache)
990 kmem_cache_destroy(obj_cache);
991 printk(KERN_WARNING "ODEBUG: out of memory.\n");
992 } else
896 debug_objects_selftest(); 993 debug_objects_selftest();
897} 994}
diff --git a/lib/decompress_bunzip2.c b/lib/decompress_bunzip2.c
index 5d3ddb5fcfd9..708e2a86d87b 100644
--- a/lib/decompress_bunzip2.c
+++ b/lib/decompress_bunzip2.c
@@ -50,6 +50,7 @@
50#endif /* !STATIC */ 50#endif /* !STATIC */
51 51
52#include <linux/decompress/mm.h> 52#include <linux/decompress/mm.h>
53#include <linux/slab.h>
53 54
54#ifndef INT_MAX 55#ifndef INT_MAX
55#define INT_MAX 0x7fffffff 56#define INT_MAX 0x7fffffff
diff --git a/lib/decompress_inflate.c b/lib/decompress_inflate.c
index 839a329b4fc4..e36b296fc9f8 100644
--- a/lib/decompress_inflate.c
+++ b/lib/decompress_inflate.c
@@ -23,6 +23,7 @@
23#endif /* STATIC */ 23#endif /* STATIC */
24 24
25#include <linux/decompress/mm.h> 25#include <linux/decompress/mm.h>
26#include <linux/slab.h>
26 27
27#define INBUF_LEN (16*1024) 28#define INBUF_LEN (16*1024)
28 29
diff --git a/lib/decompress_unlzma.c b/lib/decompress_unlzma.c
index 546f2f4c157e..32123a1340e6 100644
--- a/lib/decompress_unlzma.c
+++ b/lib/decompress_unlzma.c
@@ -34,6 +34,7 @@
34#endif /* STATIC */ 34#endif /* STATIC */
35 35
36#include <linux/decompress/mm.h> 36#include <linux/decompress/mm.h>
37#include <linux/slab.h>
37 38
38#define MIN(a, b) (((a) < (b)) ? (a) : (b)) 39#define MIN(a, b) (((a) < (b)) ? (a) : (b))
39 40
diff --git a/lib/idr.c b/lib/idr.c
index dab4bca86f5d..80ca9aca038b 100644
--- a/lib/idr.c
+++ b/lib/idr.c
@@ -579,6 +579,52 @@ int idr_for_each(struct idr *idp,
579EXPORT_SYMBOL(idr_for_each); 579EXPORT_SYMBOL(idr_for_each);
580 580
581/** 581/**
582 * idr_get_next - lookup next object of id to given id.
583 * @idp: idr handle
584 * @id: pointer to lookup key
585 *
586 * Returns pointer to registered object with id, which is next number to
587 * given id.
588 */
589
590void *idr_get_next(struct idr *idp, int *nextidp)
591{
592 struct idr_layer *p, *pa[MAX_LEVEL];
593 struct idr_layer **paa = &pa[0];
594 int id = *nextidp;
595 int n, max;
596
597 /* find first ent */
598 n = idp->layers * IDR_BITS;
599 max = 1 << n;
600 p = rcu_dereference(idp->top);
601 if (!p)
602 return NULL;
603
604 while (id < max) {
605 while (n > 0 && p) {
606 n -= IDR_BITS;
607 *paa++ = p;
608 p = rcu_dereference(p->ary[(id >> n) & IDR_MASK]);
609 }
610
611 if (p) {
612 *nextidp = id;
613 return p;
614 }
615
616 id += 1 << n;
617 while (n < fls(id)) {
618 n += IDR_BITS;
619 p = *--paa;
620 }
621 }
622 return NULL;
623}
624
625
626
627/**
582 * idr_replace - replace pointer for given id 628 * idr_replace - replace pointer for given id
583 * @idp: idr handle 629 * @idp: idr handle
584 * @ptr: pointer you want associated with the id 630 * @ptr: pointer you want associated with the id
diff --git a/lib/kobject.c b/lib/kobject.c
index a6dec32f2ddd..bacf6fe4f7a0 100644
--- a/lib/kobject.c
+++ b/lib/kobject.c
@@ -218,6 +218,9 @@ int kobject_set_name_vargs(struct kobject *kobj, const char *fmt,
218 const char *old_name = kobj->name; 218 const char *old_name = kobj->name;
219 char *s; 219 char *s;
220 220
221 if (kobj->name && !fmt)
222 return 0;
223
221 kobj->name = kvasprintf(GFP_KERNEL, fmt, vargs); 224 kobj->name = kvasprintf(GFP_KERNEL, fmt, vargs);
222 if (!kobj->name) 225 if (!kobj->name)
223 return -ENOMEM; 226 return -ENOMEM;
diff --git a/lib/kobject_uevent.c b/lib/kobject_uevent.c
index 97a777ad4f59..920a3ca6e259 100644
--- a/lib/kobject_uevent.c
+++ b/lib/kobject_uevent.c
@@ -258,7 +258,7 @@ int kobject_uevent_env(struct kobject *kobj, enum kobject_action action,
258 goto exit; 258 goto exit;
259 259
260 retval = call_usermodehelper(argv[0], argv, 260 retval = call_usermodehelper(argv[0], argv,
261 env->envp, UMH_NO_WAIT); 261 env->envp, UMH_WAIT_EXEC);
262 } 262 }
263 263
264exit: 264exit:
@@ -328,7 +328,7 @@ static int __init kobject_uevent_init(void)
328 "kobject_uevent: unable to create netlink socket!\n"); 328 "kobject_uevent: unable to create netlink socket!\n");
329 return -ENODEV; 329 return -ENODEV;
330 } 330 }
331 331 netlink_set_nonroot(NETLINK_KOBJECT_UEVENT, NL_NONROOT_RECV);
332 return 0; 332 return 0;
333} 333}
334 334
diff --git a/lib/locking-selftest.c b/lib/locking-selftest.c
index 280332c1827c..619313ed6c46 100644
--- a/lib/locking-selftest.c
+++ b/lib/locking-selftest.c
@@ -157,11 +157,11 @@ static void init_shared_classes(void)
157#define SOFTIRQ_ENTER() \ 157#define SOFTIRQ_ENTER() \
158 local_bh_disable(); \ 158 local_bh_disable(); \
159 local_irq_disable(); \ 159 local_irq_disable(); \
160 trace_softirq_enter(); \ 160 lockdep_softirq_enter(); \
161 WARN_ON(!in_softirq()); 161 WARN_ON(!in_softirq());
162 162
163#define SOFTIRQ_EXIT() \ 163#define SOFTIRQ_EXIT() \
164 trace_softirq_exit(); \ 164 lockdep_softirq_exit(); \
165 local_irq_enable(); \ 165 local_irq_enable(); \
166 local_bh_enable(); 166 local_bh_enable();
167 167
diff --git a/lib/scatterlist.c b/lib/scatterlist.c
index b7b449dafbe5..a295e404e908 100644
--- a/lib/scatterlist.c
+++ b/lib/scatterlist.c
@@ -347,9 +347,12 @@ bool sg_miter_next(struct sg_mapping_iter *miter)
347 sg_miter_stop(miter); 347 sg_miter_stop(miter);
348 348
349 /* get to the next sg if necessary. __offset is adjusted by stop */ 349 /* get to the next sg if necessary. __offset is adjusted by stop */
350 if (miter->__offset == miter->__sg->length && --miter->__nents) { 350 while (miter->__offset == miter->__sg->length) {
351 miter->__sg = sg_next(miter->__sg); 351 if (--miter->__nents) {
352 miter->__offset = 0; 352 miter->__sg = sg_next(miter->__sg);
353 miter->__offset = 0;
354 } else
355 return false;
353 } 356 }
354 357
355 /* map the next page */ 358 /* map the next page */
diff --git a/lib/swiotlb.c b/lib/swiotlb.c
index 32e2bd3b1142..2b0b5a7d2ced 100644
--- a/lib/swiotlb.c
+++ b/lib/swiotlb.c
@@ -549,7 +549,7 @@ swiotlb_alloc_coherent(struct device *hwdev, size_t size,
549 dma_addr_t dev_addr; 549 dma_addr_t dev_addr;
550 void *ret; 550 void *ret;
551 int order = get_order(size); 551 int order = get_order(size);
552 u64 dma_mask = DMA_32BIT_MASK; 552 u64 dma_mask = DMA_BIT_MASK(32);
553 553
554 if (hwdev && hwdev->coherent_dma_mask) 554 if (hwdev && hwdev->coherent_dma_mask)
555 dma_mask = hwdev->coherent_dma_mask; 555 dma_mask = hwdev->coherent_dma_mask;
diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index 0fbd0121d91d..7536acea135b 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -396,7 +396,38 @@ static noinline char* put_dec(char *buf, unsigned long long num)
396#define SMALL 32 /* Must be 32 == 0x20 */ 396#define SMALL 32 /* Must be 32 == 0x20 */
397#define SPECIAL 64 /* 0x */ 397#define SPECIAL 64 /* 0x */
398 398
399static char *number(char *buf, char *end, unsigned long long num, int base, int size, int precision, int type) 399enum format_type {
400 FORMAT_TYPE_NONE, /* Just a string part */
401 FORMAT_TYPE_WIDTH,
402 FORMAT_TYPE_PRECISION,
403 FORMAT_TYPE_CHAR,
404 FORMAT_TYPE_STR,
405 FORMAT_TYPE_PTR,
406 FORMAT_TYPE_PERCENT_CHAR,
407 FORMAT_TYPE_INVALID,
408 FORMAT_TYPE_LONG_LONG,
409 FORMAT_TYPE_ULONG,
410 FORMAT_TYPE_LONG,
411 FORMAT_TYPE_USHORT,
412 FORMAT_TYPE_SHORT,
413 FORMAT_TYPE_UINT,
414 FORMAT_TYPE_INT,
415 FORMAT_TYPE_NRCHARS,
416 FORMAT_TYPE_SIZE_T,
417 FORMAT_TYPE_PTRDIFF
418};
419
420struct printf_spec {
421 enum format_type type;
422 int flags; /* flags to number() */
423 int field_width; /* width of output field */
424 int base;
425 int precision; /* # of digits/chars */
426 int qualifier;
427};
428
429static char *number(char *buf, char *end, unsigned long long num,
430 struct printf_spec spec)
400{ 431{
401 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */ 432 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
402 static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */ 433 static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
@@ -404,32 +435,32 @@ static char *number(char *buf, char *end, unsigned long long num, int base, int
404 char tmp[66]; 435 char tmp[66];
405 char sign; 436 char sign;
406 char locase; 437 char locase;
407 int need_pfx = ((type & SPECIAL) && base != 10); 438 int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
408 int i; 439 int i;
409 440
410 /* locase = 0 or 0x20. ORing digits or letters with 'locase' 441 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
411 * produces same digits or (maybe lowercased) letters */ 442 * produces same digits or (maybe lowercased) letters */
412 locase = (type & SMALL); 443 locase = (spec.flags & SMALL);
413 if (type & LEFT) 444 if (spec.flags & LEFT)
414 type &= ~ZEROPAD; 445 spec.flags &= ~ZEROPAD;
415 sign = 0; 446 sign = 0;
416 if (type & SIGN) { 447 if (spec.flags & SIGN) {
417 if ((signed long long) num < 0) { 448 if ((signed long long) num < 0) {
418 sign = '-'; 449 sign = '-';
419 num = - (signed long long) num; 450 num = - (signed long long) num;
420 size--; 451 spec.field_width--;
421 } else if (type & PLUS) { 452 } else if (spec.flags & PLUS) {
422 sign = '+'; 453 sign = '+';
423 size--; 454 spec.field_width--;
424 } else if (type & SPACE) { 455 } else if (spec.flags & SPACE) {
425 sign = ' '; 456 sign = ' ';
426 size--; 457 spec.field_width--;
427 } 458 }
428 } 459 }
429 if (need_pfx) { 460 if (need_pfx) {
430 size--; 461 spec.field_width--;
431 if (base == 16) 462 if (spec.base == 16)
432 size--; 463 spec.field_width--;
433 } 464 }
434 465
435 /* generate full string in tmp[], in reverse order */ 466 /* generate full string in tmp[], in reverse order */
@@ -441,10 +472,10 @@ static char *number(char *buf, char *end, unsigned long long num, int base, int
441 tmp[i++] = (digits[do_div(num,base)] | locase); 472 tmp[i++] = (digits[do_div(num,base)] | locase);
442 } while (num != 0); 473 } while (num != 0);
443 */ 474 */
444 else if (base != 10) { /* 8 or 16 */ 475 else if (spec.base != 10) { /* 8 or 16 */
445 int mask = base - 1; 476 int mask = spec.base - 1;
446 int shift = 3; 477 int shift = 3;
447 if (base == 16) shift = 4; 478 if (spec.base == 16) shift = 4;
448 do { 479 do {
449 tmp[i++] = (digits[((unsigned char)num) & mask] | locase); 480 tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
450 num >>= shift; 481 num >>= shift;
@@ -454,12 +485,12 @@ static char *number(char *buf, char *end, unsigned long long num, int base, int
454 } 485 }
455 486
456 /* printing 100 using %2d gives "100", not "00" */ 487 /* printing 100 using %2d gives "100", not "00" */
457 if (i > precision) 488 if (i > spec.precision)
458 precision = i; 489 spec.precision = i;
459 /* leading space padding */ 490 /* leading space padding */
460 size -= precision; 491 spec.field_width -= spec.precision;
461 if (!(type & (ZEROPAD+LEFT))) { 492 if (!(spec.flags & (ZEROPAD+LEFT))) {
462 while(--size >= 0) { 493 while(--spec.field_width >= 0) {
463 if (buf < end) 494 if (buf < end)
464 *buf = ' '; 495 *buf = ' ';
465 ++buf; 496 ++buf;
@@ -476,23 +507,23 @@ static char *number(char *buf, char *end, unsigned long long num, int base, int
476 if (buf < end) 507 if (buf < end)
477 *buf = '0'; 508 *buf = '0';
478 ++buf; 509 ++buf;
479 if (base == 16) { 510 if (spec.base == 16) {
480 if (buf < end) 511 if (buf < end)
481 *buf = ('X' | locase); 512 *buf = ('X' | locase);
482 ++buf; 513 ++buf;
483 } 514 }
484 } 515 }
485 /* zero or space padding */ 516 /* zero or space padding */
486 if (!(type & LEFT)) { 517 if (!(spec.flags & LEFT)) {
487 char c = (type & ZEROPAD) ? '0' : ' '; 518 char c = (spec.flags & ZEROPAD) ? '0' : ' ';
488 while (--size >= 0) { 519 while (--spec.field_width >= 0) {
489 if (buf < end) 520 if (buf < end)
490 *buf = c; 521 *buf = c;
491 ++buf; 522 ++buf;
492 } 523 }
493 } 524 }
494 /* hmm even more zero padding? */ 525 /* hmm even more zero padding? */
495 while (i <= --precision) { 526 while (i <= --spec.precision) {
496 if (buf < end) 527 if (buf < end)
497 *buf = '0'; 528 *buf = '0';
498 ++buf; 529 ++buf;
@@ -504,7 +535,7 @@ static char *number(char *buf, char *end, unsigned long long num, int base, int
504 ++buf; 535 ++buf;
505 } 536 }
506 /* trailing space padding */ 537 /* trailing space padding */
507 while (--size >= 0) { 538 while (--spec.field_width >= 0) {
508 if (buf < end) 539 if (buf < end)
509 *buf = ' '; 540 *buf = ' ';
510 ++buf; 541 ++buf;
@@ -512,17 +543,17 @@ static char *number(char *buf, char *end, unsigned long long num, int base, int
512 return buf; 543 return buf;
513} 544}
514 545
515static char *string(char *buf, char *end, char *s, int field_width, int precision, int flags) 546static char *string(char *buf, char *end, char *s, struct printf_spec spec)
516{ 547{
517 int len, i; 548 int len, i;
518 549
519 if ((unsigned long)s < PAGE_SIZE) 550 if ((unsigned long)s < PAGE_SIZE)
520 s = "<NULL>"; 551 s = "<NULL>";
521 552
522 len = strnlen(s, precision); 553 len = strnlen(s, spec.precision);
523 554
524 if (!(flags & LEFT)) { 555 if (!(spec.flags & LEFT)) {
525 while (len < field_width--) { 556 while (len < spec.field_width--) {
526 if (buf < end) 557 if (buf < end)
527 *buf = ' '; 558 *buf = ' ';
528 ++buf; 559 ++buf;
@@ -533,7 +564,7 @@ static char *string(char *buf, char *end, char *s, int field_width, int precisio
533 *buf = *s; 564 *buf = *s;
534 ++buf; ++s; 565 ++buf; ++s;
535 } 566 }
536 while (len < field_width--) { 567 while (len < spec.field_width--) {
537 if (buf < end) 568 if (buf < end)
538 *buf = ' '; 569 *buf = ' ';
539 ++buf; 570 ++buf;
@@ -541,21 +572,24 @@ static char *string(char *buf, char *end, char *s, int field_width, int precisio
541 return buf; 572 return buf;
542} 573}
543 574
544static char *symbol_string(char *buf, char *end, void *ptr, int field_width, int precision, int flags) 575static char *symbol_string(char *buf, char *end, void *ptr,
576 struct printf_spec spec)
545{ 577{
546 unsigned long value = (unsigned long) ptr; 578 unsigned long value = (unsigned long) ptr;
547#ifdef CONFIG_KALLSYMS 579#ifdef CONFIG_KALLSYMS
548 char sym[KSYM_SYMBOL_LEN]; 580 char sym[KSYM_SYMBOL_LEN];
549 sprint_symbol(sym, value); 581 sprint_symbol(sym, value);
550 return string(buf, end, sym, field_width, precision, flags); 582 return string(buf, end, sym, spec);
551#else 583#else
552 field_width = 2*sizeof(void *); 584 spec.field_width = 2*sizeof(void *);
553 flags |= SPECIAL | SMALL | ZEROPAD; 585 spec.flags |= SPECIAL | SMALL | ZEROPAD;
554 return number(buf, end, value, 16, field_width, precision, flags); 586 spec.base = 16;
587 return number(buf, end, value, spec);
555#endif 588#endif
556} 589}
557 590
558static char *resource_string(char *buf, char *end, struct resource *res, int field_width, int precision, int flags) 591static char *resource_string(char *buf, char *end, struct resource *res,
592 struct printf_spec spec)
559{ 593{
560#ifndef IO_RSRC_PRINTK_SIZE 594#ifndef IO_RSRC_PRINTK_SIZE
561#define IO_RSRC_PRINTK_SIZE 4 595#define IO_RSRC_PRINTK_SIZE 4
@@ -564,7 +598,11 @@ static char *resource_string(char *buf, char *end, struct resource *res, int fie
564#ifndef MEM_RSRC_PRINTK_SIZE 598#ifndef MEM_RSRC_PRINTK_SIZE
565#define MEM_RSRC_PRINTK_SIZE 8 599#define MEM_RSRC_PRINTK_SIZE 8
566#endif 600#endif
567 601 struct printf_spec num_spec = {
602 .base = 16,
603 .precision = -1,
604 .flags = SPECIAL | SMALL | ZEROPAD,
605 };
568 /* room for the actual numbers, the two "0x", -, [, ] and the final zero */ 606 /* room for the actual numbers, the two "0x", -, [, ] and the final zero */
569 char sym[4*sizeof(resource_size_t) + 8]; 607 char sym[4*sizeof(resource_size_t) + 8];
570 char *p = sym, *pend = sym + sizeof(sym); 608 char *p = sym, *pend = sym + sizeof(sym);
@@ -576,17 +614,18 @@ static char *resource_string(char *buf, char *end, struct resource *res, int fie
576 size = MEM_RSRC_PRINTK_SIZE; 614 size = MEM_RSRC_PRINTK_SIZE;
577 615
578 *p++ = '['; 616 *p++ = '[';
579 p = number(p, pend, res->start, 16, size, -1, SPECIAL | SMALL | ZEROPAD); 617 num_spec.field_width = size;
618 p = number(p, pend, res->start, num_spec);
580 *p++ = '-'; 619 *p++ = '-';
581 p = number(p, pend, res->end, 16, size, -1, SPECIAL | SMALL | ZEROPAD); 620 p = number(p, pend, res->end, num_spec);
582 *p++ = ']'; 621 *p++ = ']';
583 *p = 0; 622 *p = 0;
584 623
585 return string(buf, end, sym, field_width, precision, flags); 624 return string(buf, end, sym, spec);
586} 625}
587 626
588static char *mac_address_string(char *buf, char *end, u8 *addr, int field_width, 627static char *mac_address_string(char *buf, char *end, u8 *addr,
589 int precision, int flags) 628 struct printf_spec spec)
590{ 629{
591 char mac_addr[6 * 3]; /* (6 * 2 hex digits), 5 colons and trailing zero */ 630 char mac_addr[6 * 3]; /* (6 * 2 hex digits), 5 colons and trailing zero */
592 char *p = mac_addr; 631 char *p = mac_addr;
@@ -594,16 +633,17 @@ static char *mac_address_string(char *buf, char *end, u8 *addr, int field_width,
594 633
595 for (i = 0; i < 6; i++) { 634 for (i = 0; i < 6; i++) {
596 p = pack_hex_byte(p, addr[i]); 635 p = pack_hex_byte(p, addr[i]);
597 if (!(flags & SPECIAL) && i != 5) 636 if (!(spec.flags & SPECIAL) && i != 5)
598 *p++ = ':'; 637 *p++ = ':';
599 } 638 }
600 *p = '\0'; 639 *p = '\0';
640 spec.flags &= ~SPECIAL;
601 641
602 return string(buf, end, mac_addr, field_width, precision, flags & ~SPECIAL); 642 return string(buf, end, mac_addr, spec);
603} 643}
604 644
605static char *ip6_addr_string(char *buf, char *end, u8 *addr, int field_width, 645static char *ip6_addr_string(char *buf, char *end, u8 *addr,
606 int precision, int flags) 646 struct printf_spec spec)
607{ 647{
608 char ip6_addr[8 * 5]; /* (8 * 4 hex digits), 7 colons and trailing zero */ 648 char ip6_addr[8 * 5]; /* (8 * 4 hex digits), 7 colons and trailing zero */
609 char *p = ip6_addr; 649 char *p = ip6_addr;
@@ -612,16 +652,17 @@ static char *ip6_addr_string(char *buf, char *end, u8 *addr, int field_width,
612 for (i = 0; i < 8; i++) { 652 for (i = 0; i < 8; i++) {
613 p = pack_hex_byte(p, addr[2 * i]); 653 p = pack_hex_byte(p, addr[2 * i]);
614 p = pack_hex_byte(p, addr[2 * i + 1]); 654 p = pack_hex_byte(p, addr[2 * i + 1]);
615 if (!(flags & SPECIAL) && i != 7) 655 if (!(spec.flags & SPECIAL) && i != 7)
616 *p++ = ':'; 656 *p++ = ':';
617 } 657 }
618 *p = '\0'; 658 *p = '\0';
659 spec.flags &= ~SPECIAL;
619 660
620 return string(buf, end, ip6_addr, field_width, precision, flags & ~SPECIAL); 661 return string(buf, end, ip6_addr, spec);
621} 662}
622 663
623static char *ip4_addr_string(char *buf, char *end, u8 *addr, int field_width, 664static char *ip4_addr_string(char *buf, char *end, u8 *addr,
624 int precision, int flags) 665 struct printf_spec spec)
625{ 666{
626 char ip4_addr[4 * 4]; /* (4 * 3 decimal digits), 3 dots and trailing zero */ 667 char ip4_addr[4 * 4]; /* (4 * 3 decimal digits), 3 dots and trailing zero */
627 char temp[3]; /* hold each IP quad in reverse order */ 668 char temp[3]; /* hold each IP quad in reverse order */
@@ -637,8 +678,9 @@ static char *ip4_addr_string(char *buf, char *end, u8 *addr, int field_width,
637 *p++ = '.'; 678 *p++ = '.';
638 } 679 }
639 *p = '\0'; 680 *p = '\0';
681 spec.flags &= ~SPECIAL;
640 682
641 return string(buf, end, ip4_addr, field_width, precision, flags & ~SPECIAL); 683 return string(buf, end, ip4_addr, spec);
642} 684}
643 685
644/* 686/*
@@ -663,41 +705,233 @@ static char *ip4_addr_string(char *buf, char *end, u8 *addr, int field_width,
663 * function pointers are really function descriptors, which contain a 705 * function pointers are really function descriptors, which contain a
664 * pointer to the real address. 706 * pointer to the real address.
665 */ 707 */
666static char *pointer(const char *fmt, char *buf, char *end, void *ptr, int field_width, int precision, int flags) 708static char *pointer(const char *fmt, char *buf, char *end, void *ptr,
709 struct printf_spec spec)
667{ 710{
668 if (!ptr) 711 if (!ptr)
669 return string(buf, end, "(null)", field_width, precision, flags); 712 return string(buf, end, "(null)", spec);
670 713
671 switch (*fmt) { 714 switch (*fmt) {
672 case 'F': 715 case 'F':
673 ptr = dereference_function_descriptor(ptr); 716 ptr = dereference_function_descriptor(ptr);
674 /* Fallthrough */ 717 /* Fallthrough */
675 case 'S': 718 case 'S':
676 return symbol_string(buf, end, ptr, field_width, precision, flags); 719 return symbol_string(buf, end, ptr, spec);
677 case 'R': 720 case 'R':
678 return resource_string(buf, end, ptr, field_width, precision, flags); 721 return resource_string(buf, end, ptr, spec);
679 case 'm': 722 case 'm':
680 flags |= SPECIAL; 723 spec.flags |= SPECIAL;
681 /* Fallthrough */ 724 /* Fallthrough */
682 case 'M': 725 case 'M':
683 return mac_address_string(buf, end, ptr, field_width, precision, flags); 726 return mac_address_string(buf, end, ptr, spec);
684 case 'i': 727 case 'i':
685 flags |= SPECIAL; 728 spec.flags |= SPECIAL;
686 /* Fallthrough */ 729 /* Fallthrough */
687 case 'I': 730 case 'I':
688 if (fmt[1] == '6') 731 if (fmt[1] == '6')
689 return ip6_addr_string(buf, end, ptr, field_width, precision, flags); 732 return ip6_addr_string(buf, end, ptr, spec);
690 if (fmt[1] == '4') 733 if (fmt[1] == '4')
691 return ip4_addr_string(buf, end, ptr, field_width, precision, flags); 734 return ip4_addr_string(buf, end, ptr, spec);
692 flags &= ~SPECIAL; 735 spec.flags &= ~SPECIAL;
693 break; 736 break;
694 } 737 }
695 flags |= SMALL; 738 spec.flags |= SMALL;
696 if (field_width == -1) { 739 if (spec.field_width == -1) {
697 field_width = 2*sizeof(void *); 740 spec.field_width = 2*sizeof(void *);
698 flags |= ZEROPAD; 741 spec.flags |= ZEROPAD;
699 } 742 }
700 return number(buf, end, (unsigned long) ptr, 16, field_width, precision, flags); 743 spec.base = 16;
744
745 return number(buf, end, (unsigned long) ptr, spec);
746}
747
748/*
749 * Helper function to decode printf style format.
750 * Each call decode a token from the format and return the
751 * number of characters read (or likely the delta where it wants
752 * to go on the next call).
753 * The decoded token is returned through the parameters
754 *
755 * 'h', 'l', or 'L' for integer fields
756 * 'z' support added 23/7/1999 S.H.
757 * 'z' changed to 'Z' --davidm 1/25/99
758 * 't' added for ptrdiff_t
759 *
760 * @fmt: the format string
761 * @type of the token returned
762 * @flags: various flags such as +, -, # tokens..
763 * @field_width: overwritten width
764 * @base: base of the number (octal, hex, ...)
765 * @precision: precision of a number
766 * @qualifier: qualifier of a number (long, size_t, ...)
767 */
768static int format_decode(const char *fmt, struct printf_spec *spec)
769{
770 const char *start = fmt;
771
772 /* we finished early by reading the field width */
773 if (spec->type == FORMAT_TYPE_WIDTH) {
774 if (spec->field_width < 0) {
775 spec->field_width = -spec->field_width;
776 spec->flags |= LEFT;
777 }
778 spec->type = FORMAT_TYPE_NONE;
779 goto precision;
780 }
781
782 /* we finished early by reading the precision */
783 if (spec->type == FORMAT_TYPE_PRECISION) {
784 if (spec->precision < 0)
785 spec->precision = 0;
786
787 spec->type = FORMAT_TYPE_NONE;
788 goto qualifier;
789 }
790
791 /* By default */
792 spec->type = FORMAT_TYPE_NONE;
793
794 for (; *fmt ; ++fmt) {
795 if (*fmt == '%')
796 break;
797 }
798
799 /* Return the current non-format string */
800 if (fmt != start || !*fmt)
801 return fmt - start;
802
803 /* Process flags */
804 spec->flags = 0;
805
806 while (1) { /* this also skips first '%' */
807 bool found = true;
808
809 ++fmt;
810
811 switch (*fmt) {
812 case '-': spec->flags |= LEFT; break;
813 case '+': spec->flags |= PLUS; break;
814 case ' ': spec->flags |= SPACE; break;
815 case '#': spec->flags |= SPECIAL; break;
816 case '0': spec->flags |= ZEROPAD; break;
817 default: found = false;
818 }
819
820 if (!found)
821 break;
822 }
823
824 /* get field width */
825 spec->field_width = -1;
826
827 if (isdigit(*fmt))
828 spec->field_width = skip_atoi(&fmt);
829 else if (*fmt == '*') {
830 /* it's the next argument */
831 spec->type = FORMAT_TYPE_WIDTH;
832 return ++fmt - start;
833 }
834
835precision:
836 /* get the precision */
837 spec->precision = -1;
838 if (*fmt == '.') {
839 ++fmt;
840 if (isdigit(*fmt)) {
841 spec->precision = skip_atoi(&fmt);
842 if (spec->precision < 0)
843 spec->precision = 0;
844 } else if (*fmt == '*') {
845 /* it's the next argument */
846 spec->type = FORMAT_TYPE_PRECISION;
847 return ++fmt - start;
848 }
849 }
850
851qualifier:
852 /* get the conversion qualifier */
853 spec->qualifier = -1;
854 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
855 *fmt == 'Z' || *fmt == 'z' || *fmt == 't') {
856 spec->qualifier = *fmt;
857 ++fmt;
858 if (spec->qualifier == 'l' && *fmt == 'l') {
859 spec->qualifier = 'L';
860 ++fmt;
861 }
862 }
863
864 /* default base */
865 spec->base = 10;
866 switch (*fmt) {
867 case 'c':
868 spec->type = FORMAT_TYPE_CHAR;
869 return ++fmt - start;
870
871 case 's':
872 spec->type = FORMAT_TYPE_STR;
873 return ++fmt - start;
874
875 case 'p':
876 spec->type = FORMAT_TYPE_PTR;
877 return fmt - start;
878 /* skip alnum */
879
880 case 'n':
881 spec->type = FORMAT_TYPE_NRCHARS;
882 return ++fmt - start;
883
884 case '%':
885 spec->type = FORMAT_TYPE_PERCENT_CHAR;
886 return ++fmt - start;
887
888 /* integer number formats - set up the flags and "break" */
889 case 'o':
890 spec->base = 8;
891 break;
892
893 case 'x':
894 spec->flags |= SMALL;
895
896 case 'X':
897 spec->base = 16;
898 break;
899
900 case 'd':
901 case 'i':
902 spec->flags |= SIGN;
903 case 'u':
904 break;
905
906 default:
907 spec->type = FORMAT_TYPE_INVALID;
908 return fmt - start;
909 }
910
911 if (spec->qualifier == 'L')
912 spec->type = FORMAT_TYPE_LONG_LONG;
913 else if (spec->qualifier == 'l') {
914 if (spec->flags & SIGN)
915 spec->type = FORMAT_TYPE_LONG;
916 else
917 spec->type = FORMAT_TYPE_ULONG;
918 } else if (spec->qualifier == 'Z' || spec->qualifier == 'z') {
919 spec->type = FORMAT_TYPE_SIZE_T;
920 } else if (spec->qualifier == 't') {
921 spec->type = FORMAT_TYPE_PTRDIFF;
922 } else if (spec->qualifier == 'h') {
923 if (spec->flags & SIGN)
924 spec->type = FORMAT_TYPE_SHORT;
925 else
926 spec->type = FORMAT_TYPE_USHORT;
927 } else {
928 if (spec->flags & SIGN)
929 spec->type = FORMAT_TYPE_INT;
930 else
931 spec->type = FORMAT_TYPE_UINT;
932 }
933
934 return ++fmt - start;
701} 935}
702 936
703/** 937/**
@@ -726,18 +960,9 @@ static char *pointer(const char *fmt, char *buf, char *end, void *ptr, int field
726int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) 960int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
727{ 961{
728 unsigned long long num; 962 unsigned long long num;
729 int base;
730 char *str, *end, c; 963 char *str, *end, c;
731 964 int read;
732 int flags; /* flags to number() */ 965 struct printf_spec spec = {0};
733
734 int field_width; /* width of output field */
735 int precision; /* min. # of digits for integers; max
736 number of chars for from string */
737 int qualifier; /* 'h', 'l', or 'L' for integer fields */
738 /* 'z' support added 23/7/1999 S.H. */
739 /* 'z' changed to 'Z' --davidm 1/25/99 */
740 /* 't' added for ptrdiff_t */
741 966
742 /* Reject out-of-range values early. Large positive sizes are 967 /* Reject out-of-range values early. Large positive sizes are
743 used for unknown buffer sizes. */ 968 used for unknown buffer sizes. */
@@ -758,184 +983,137 @@ int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
758 size = end - buf; 983 size = end - buf;
759 } 984 }
760 985
761 for (; *fmt ; ++fmt) { 986 while (*fmt) {
762 if (*fmt != '%') { 987 const char *old_fmt = fmt;
763 if (str < end)
764 *str = *fmt;
765 ++str;
766 continue;
767 }
768 988
769 /* process flags */ 989 read = format_decode(fmt, &spec);
770 flags = 0;
771 repeat:
772 ++fmt; /* this also skips first '%' */
773 switch (*fmt) {
774 case '-': flags |= LEFT; goto repeat;
775 case '+': flags |= PLUS; goto repeat;
776 case ' ': flags |= SPACE; goto repeat;
777 case '#': flags |= SPECIAL; goto repeat;
778 case '0': flags |= ZEROPAD; goto repeat;
779 }
780 990
781 /* get field width */ 991 fmt += read;
782 field_width = -1;
783 if (isdigit(*fmt))
784 field_width = skip_atoi(&fmt);
785 else if (*fmt == '*') {
786 ++fmt;
787 /* it's the next argument */
788 field_width = va_arg(args, int);
789 if (field_width < 0) {
790 field_width = -field_width;
791 flags |= LEFT;
792 }
793 }
794 992
795 /* get the precision */ 993 switch (spec.type) {
796 precision = -1; 994 case FORMAT_TYPE_NONE: {
797 if (*fmt == '.') { 995 int copy = read;
798 ++fmt; 996 if (str < end) {
799 if (isdigit(*fmt)) 997 if (copy > end - str)
800 precision = skip_atoi(&fmt); 998 copy = end - str;
801 else if (*fmt == '*') { 999 memcpy(str, old_fmt, copy);
802 ++fmt;
803 /* it's the next argument */
804 precision = va_arg(args, int);
805 } 1000 }
806 if (precision < 0) 1001 str += read;
807 precision = 0; 1002 break;
808 } 1003 }
809 1004
810 /* get the conversion qualifier */ 1005 case FORMAT_TYPE_WIDTH:
811 qualifier = -1; 1006 spec.field_width = va_arg(args, int);
812 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' || 1007 break;
813 *fmt =='Z' || *fmt == 'z' || *fmt == 't') {
814 qualifier = *fmt;
815 ++fmt;
816 if (qualifier == 'l' && *fmt == 'l') {
817 qualifier = 'L';
818 ++fmt;
819 }
820 }
821 1008
822 /* default base */ 1009 case FORMAT_TYPE_PRECISION:
823 base = 10; 1010 spec.precision = va_arg(args, int);
1011 break;
824 1012
825 switch (*fmt) { 1013 case FORMAT_TYPE_CHAR:
826 case 'c': 1014 if (!(spec.flags & LEFT)) {
827 if (!(flags & LEFT)) { 1015 while (--spec.field_width > 0) {
828 while (--field_width > 0) {
829 if (str < end)
830 *str = ' ';
831 ++str;
832 }
833 }
834 c = (unsigned char) va_arg(args, int);
835 if (str < end)
836 *str = c;
837 ++str;
838 while (--field_width > 0) {
839 if (str < end) 1016 if (str < end)
840 *str = ' '; 1017 *str = ' ';
841 ++str; 1018 ++str;
842 }
843 continue;
844
845 case 's':
846 str = string(str, end, va_arg(args, char *), field_width, precision, flags);
847 continue;
848
849 case 'p':
850 str = pointer(fmt+1, str, end,
851 va_arg(args, void *),
852 field_width, precision, flags);
853 /* Skip all alphanumeric pointer suffixes */
854 while (isalnum(fmt[1]))
855 fmt++;
856 continue;
857
858 case 'n':
859 /* FIXME:
860 * What does C99 say about the overflow case here? */
861 if (qualifier == 'l') {
862 long * ip = va_arg(args, long *);
863 *ip = (str - buf);
864 } else if (qualifier == 'Z' || qualifier == 'z') {
865 size_t * ip = va_arg(args, size_t *);
866 *ip = (str - buf);
867 } else {
868 int * ip = va_arg(args, int *);
869 *ip = (str - buf);
870 }
871 continue;
872 1019
873 case '%': 1020 }
1021 }
1022 c = (unsigned char) va_arg(args, int);
1023 if (str < end)
1024 *str = c;
1025 ++str;
1026 while (--spec.field_width > 0) {
874 if (str < end) 1027 if (str < end)
875 *str = '%'; 1028 *str = ' ';
876 ++str; 1029 ++str;
877 continue; 1030 }
1031 break;
878 1032
879 /* integer number formats - set up the flags and "break" */ 1033 case FORMAT_TYPE_STR:
880 case 'o': 1034 str = string(str, end, va_arg(args, char *), spec);
881 base = 8; 1035 break;
882 break;
883 1036
884 case 'x': 1037 case FORMAT_TYPE_PTR:
885 flags |= SMALL; 1038 str = pointer(fmt+1, str, end, va_arg(args, void *),
886 case 'X': 1039 spec);
887 base = 16; 1040 while (isalnum(*fmt))
888 break; 1041 fmt++;
1042 break;
889 1043
890 case 'd': 1044 case FORMAT_TYPE_PERCENT_CHAR:
891 case 'i': 1045 if (str < end)
892 flags |= SIGN; 1046 *str = '%';
893 case 'u': 1047 ++str;
894 break; 1048 break;
895 1049
896 default: 1050 case FORMAT_TYPE_INVALID:
897 if (str < end) 1051 if (str < end)
898 *str = '%'; 1052 *str = '%';
899 ++str; 1053 ++str;
900 if (*fmt) { 1054 break;
901 if (str < end) 1055
902 *str = *fmt; 1056 case FORMAT_TYPE_NRCHARS: {
903 ++str; 1057 int qualifier = spec.qualifier;
904 } else { 1058
905 --fmt; 1059 if (qualifier == 'l') {
906 } 1060 long *ip = va_arg(args, long *);
907 continue; 1061 *ip = (str - buf);
1062 } else if (qualifier == 'Z' ||
1063 qualifier == 'z') {
1064 size_t *ip = va_arg(args, size_t *);
1065 *ip = (str - buf);
1066 } else {
1067 int *ip = va_arg(args, int *);
1068 *ip = (str - buf);
1069 }
1070 break;
908 } 1071 }
909 if (qualifier == 'L') 1072
910 num = va_arg(args, long long); 1073 default:
911 else if (qualifier == 'l') { 1074 switch (spec.type) {
912 num = va_arg(args, unsigned long); 1075 case FORMAT_TYPE_LONG_LONG:
913 if (flags & SIGN) 1076 num = va_arg(args, long long);
914 num = (signed long) num; 1077 break;
915 } else if (qualifier == 'Z' || qualifier == 'z') { 1078 case FORMAT_TYPE_ULONG:
916 num = va_arg(args, size_t); 1079 num = va_arg(args, unsigned long);
917 } else if (qualifier == 't') { 1080 break;
918 num = va_arg(args, ptrdiff_t); 1081 case FORMAT_TYPE_LONG:
919 } else if (qualifier == 'h') { 1082 num = va_arg(args, long);
920 num = (unsigned short) va_arg(args, int); 1083 break;
921 if (flags & SIGN) 1084 case FORMAT_TYPE_SIZE_T:
922 num = (signed short) num; 1085 num = va_arg(args, size_t);
923 } else { 1086 break;
924 num = va_arg(args, unsigned int); 1087 case FORMAT_TYPE_PTRDIFF:
925 if (flags & SIGN) 1088 num = va_arg(args, ptrdiff_t);
926 num = (signed int) num; 1089 break;
1090 case FORMAT_TYPE_USHORT:
1091 num = (unsigned short) va_arg(args, int);
1092 break;
1093 case FORMAT_TYPE_SHORT:
1094 num = (short) va_arg(args, int);
1095 break;
1096 case FORMAT_TYPE_INT:
1097 num = (int) va_arg(args, int);
1098 break;
1099 default:
1100 num = va_arg(args, unsigned int);
1101 }
1102
1103 str = number(str, end, num, spec);
927 } 1104 }
928 str = number(str, end, num, base,
929 field_width, precision, flags);
930 } 1105 }
1106
931 if (size > 0) { 1107 if (size > 0) {
932 if (str < end) 1108 if (str < end)
933 *str = '\0'; 1109 *str = '\0';
934 else 1110 else
935 end[-1] = '\0'; 1111 end[-1] = '\0';
936 } 1112 }
1113
937 /* the trailing null byte doesn't count towards the total */ 1114 /* the trailing null byte doesn't count towards the total */
938 return str-buf; 1115 return str-buf;
1116
939} 1117}
940EXPORT_SYMBOL(vsnprintf); 1118EXPORT_SYMBOL(vsnprintf);
941 1119
@@ -1058,6 +1236,363 @@ int sprintf(char * buf, const char *fmt, ...)
1058} 1236}
1059EXPORT_SYMBOL(sprintf); 1237EXPORT_SYMBOL(sprintf);
1060 1238
1239#ifdef CONFIG_BINARY_PRINTF
1240/*
1241 * bprintf service:
1242 * vbin_printf() - VA arguments to binary data
1243 * bstr_printf() - Binary data to text string
1244 */
1245
1246/**
1247 * vbin_printf - Parse a format string and place args' binary value in a buffer
1248 * @bin_buf: The buffer to place args' binary value
1249 * @size: The size of the buffer(by words(32bits), not characters)
1250 * @fmt: The format string to use
1251 * @args: Arguments for the format string
1252 *
1253 * The format follows C99 vsnprintf, except %n is ignored, and its argument
1254 * is skiped.
1255 *
1256 * The return value is the number of words(32bits) which would be generated for
1257 * the given input.
1258 *
1259 * NOTE:
1260 * If the return value is greater than @size, the resulting bin_buf is NOT
1261 * valid for bstr_printf().
1262 */
1263int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
1264{
1265 struct printf_spec spec = {0};
1266 char *str, *end;
1267 int read;
1268
1269 str = (char *)bin_buf;
1270 end = (char *)(bin_buf + size);
1271
1272#define save_arg(type) \
1273do { \
1274 if (sizeof(type) == 8) { \
1275 unsigned long long value; \
1276 str = PTR_ALIGN(str, sizeof(u32)); \
1277 value = va_arg(args, unsigned long long); \
1278 if (str + sizeof(type) <= end) { \
1279 *(u32 *)str = *(u32 *)&value; \
1280 *(u32 *)(str + 4) = *((u32 *)&value + 1); \
1281 } \
1282 } else { \
1283 unsigned long value; \
1284 str = PTR_ALIGN(str, sizeof(type)); \
1285 value = va_arg(args, int); \
1286 if (str + sizeof(type) <= end) \
1287 *(typeof(type) *)str = (type)value; \
1288 } \
1289 str += sizeof(type); \
1290} while (0)
1291
1292
1293 while (*fmt) {
1294 read = format_decode(fmt, &spec);
1295
1296 fmt += read;
1297
1298 switch (spec.type) {
1299 case FORMAT_TYPE_NONE:
1300 break;
1301
1302 case FORMAT_TYPE_WIDTH:
1303 case FORMAT_TYPE_PRECISION:
1304 save_arg(int);
1305 break;
1306
1307 case FORMAT_TYPE_CHAR:
1308 save_arg(char);
1309 break;
1310
1311 case FORMAT_TYPE_STR: {
1312 const char *save_str = va_arg(args, char *);
1313 size_t len;
1314 if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
1315 || (unsigned long)save_str < PAGE_SIZE)
1316 save_str = "<NULL>";
1317 len = strlen(save_str);
1318 if (str + len + 1 < end)
1319 memcpy(str, save_str, len + 1);
1320 str += len + 1;
1321 break;
1322 }
1323
1324 case FORMAT_TYPE_PTR:
1325 save_arg(void *);
1326 /* skip all alphanumeric pointer suffixes */
1327 while (isalnum(*fmt))
1328 fmt++;
1329 break;
1330
1331 case FORMAT_TYPE_PERCENT_CHAR:
1332 break;
1333
1334 case FORMAT_TYPE_INVALID:
1335 break;
1336
1337 case FORMAT_TYPE_NRCHARS: {
1338 /* skip %n 's argument */
1339 int qualifier = spec.qualifier;
1340 void *skip_arg;
1341 if (qualifier == 'l')
1342 skip_arg = va_arg(args, long *);
1343 else if (qualifier == 'Z' || qualifier == 'z')
1344 skip_arg = va_arg(args, size_t *);
1345 else
1346 skip_arg = va_arg(args, int *);
1347 break;
1348 }
1349
1350 default:
1351 switch (spec.type) {
1352
1353 case FORMAT_TYPE_LONG_LONG:
1354 save_arg(long long);
1355 break;
1356 case FORMAT_TYPE_ULONG:
1357 case FORMAT_TYPE_LONG:
1358 save_arg(unsigned long);
1359 break;
1360 case FORMAT_TYPE_SIZE_T:
1361 save_arg(size_t);
1362 break;
1363 case FORMAT_TYPE_PTRDIFF:
1364 save_arg(ptrdiff_t);
1365 break;
1366 case FORMAT_TYPE_USHORT:
1367 case FORMAT_TYPE_SHORT:
1368 save_arg(short);
1369 break;
1370 default:
1371 save_arg(int);
1372 }
1373 }
1374 }
1375 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
1376
1377#undef save_arg
1378}
1379EXPORT_SYMBOL_GPL(vbin_printf);
1380
1381/**
1382 * bstr_printf - Format a string from binary arguments and place it in a buffer
1383 * @buf: The buffer to place the result into
1384 * @size: The size of the buffer, including the trailing null space
1385 * @fmt: The format string to use
1386 * @bin_buf: Binary arguments for the format string
1387 *
1388 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
1389 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
1390 * a binary buffer that generated by vbin_printf.
1391 *
1392 * The format follows C99 vsnprintf, but has some extensions:
1393 * %pS output the name of a text symbol
1394 * %pF output the name of a function pointer
1395 * %pR output the address range in a struct resource
1396 * %n is ignored
1397 *
1398 * The return value is the number of characters which would
1399 * be generated for the given input, excluding the trailing
1400 * '\0', as per ISO C99. If you want to have the exact
1401 * number of characters written into @buf as return value
1402 * (not including the trailing '\0'), use vscnprintf(). If the
1403 * return is greater than or equal to @size, the resulting
1404 * string is truncated.
1405 */
1406int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
1407{
1408 unsigned long long num;
1409 char *str, *end, c;
1410 const char *args = (const char *)bin_buf;
1411
1412 struct printf_spec spec = {0};
1413
1414 if (unlikely((int) size < 0)) {
1415 /* There can be only one.. */
1416 static char warn = 1;
1417 WARN_ON(warn);
1418 warn = 0;
1419 return 0;
1420 }
1421
1422 str = buf;
1423 end = buf + size;
1424
1425#define get_arg(type) \
1426({ \
1427 typeof(type) value; \
1428 if (sizeof(type) == 8) { \
1429 args = PTR_ALIGN(args, sizeof(u32)); \
1430 *(u32 *)&value = *(u32 *)args; \
1431 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
1432 } else { \
1433 args = PTR_ALIGN(args, sizeof(type)); \
1434 value = *(typeof(type) *)args; \
1435 } \
1436 args += sizeof(type); \
1437 value; \
1438})
1439
1440 /* Make sure end is always >= buf */
1441 if (end < buf) {
1442 end = ((void *)-1);
1443 size = end - buf;
1444 }
1445
1446 while (*fmt) {
1447 int read;
1448 const char *old_fmt = fmt;
1449
1450 read = format_decode(fmt, &spec);
1451
1452 fmt += read;
1453
1454 switch (spec.type) {
1455 case FORMAT_TYPE_NONE: {
1456 int copy = read;
1457 if (str < end) {
1458 if (copy > end - str)
1459 copy = end - str;
1460 memcpy(str, old_fmt, copy);
1461 }
1462 str += read;
1463 break;
1464 }
1465
1466 case FORMAT_TYPE_WIDTH:
1467 spec.field_width = get_arg(int);
1468 break;
1469
1470 case FORMAT_TYPE_PRECISION:
1471 spec.precision = get_arg(int);
1472 break;
1473
1474 case FORMAT_TYPE_CHAR:
1475 if (!(spec.flags & LEFT)) {
1476 while (--spec.field_width > 0) {
1477 if (str < end)
1478 *str = ' ';
1479 ++str;
1480 }
1481 }
1482 c = (unsigned char) get_arg(char);
1483 if (str < end)
1484 *str = c;
1485 ++str;
1486 while (--spec.field_width > 0) {
1487 if (str < end)
1488 *str = ' ';
1489 ++str;
1490 }
1491 break;
1492
1493 case FORMAT_TYPE_STR: {
1494 const char *str_arg = args;
1495 size_t len = strlen(str_arg);
1496 args += len + 1;
1497 str = string(str, end, (char *)str_arg, spec);
1498 break;
1499 }
1500
1501 case FORMAT_TYPE_PTR:
1502 str = pointer(fmt+1, str, end, get_arg(void *), spec);
1503 while (isalnum(*fmt))
1504 fmt++;
1505 break;
1506
1507 case FORMAT_TYPE_PERCENT_CHAR:
1508 if (str < end)
1509 *str = '%';
1510 ++str;
1511 break;
1512
1513 case FORMAT_TYPE_INVALID:
1514 if (str < end)
1515 *str = '%';
1516 ++str;
1517 break;
1518
1519 case FORMAT_TYPE_NRCHARS:
1520 /* skip */
1521 break;
1522
1523 default:
1524 switch (spec.type) {
1525
1526 case FORMAT_TYPE_LONG_LONG:
1527 num = get_arg(long long);
1528 break;
1529 case FORMAT_TYPE_ULONG:
1530 num = get_arg(unsigned long);
1531 break;
1532 case FORMAT_TYPE_LONG:
1533 num = get_arg(unsigned long);
1534 break;
1535 case FORMAT_TYPE_SIZE_T:
1536 num = get_arg(size_t);
1537 break;
1538 case FORMAT_TYPE_PTRDIFF:
1539 num = get_arg(ptrdiff_t);
1540 break;
1541 case FORMAT_TYPE_USHORT:
1542 num = get_arg(unsigned short);
1543 break;
1544 case FORMAT_TYPE_SHORT:
1545 num = get_arg(short);
1546 break;
1547 case FORMAT_TYPE_UINT:
1548 num = get_arg(unsigned int);
1549 break;
1550 default:
1551 num = get_arg(int);
1552 }
1553
1554 str = number(str, end, num, spec);
1555 }
1556 }
1557
1558 if (size > 0) {
1559 if (str < end)
1560 *str = '\0';
1561 else
1562 end[-1] = '\0';
1563 }
1564
1565#undef get_arg
1566
1567 /* the trailing null byte doesn't count towards the total */
1568 return str - buf;
1569}
1570EXPORT_SYMBOL_GPL(bstr_printf);
1571
1572/**
1573 * bprintf - Parse a format string and place args' binary value in a buffer
1574 * @bin_buf: The buffer to place args' binary value
1575 * @size: The size of the buffer(by words(32bits), not characters)
1576 * @fmt: The format string to use
1577 * @...: Arguments for the format string
1578 *
1579 * The function returns the number of words(u32) written
1580 * into @bin_buf.
1581 */
1582int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
1583{
1584 va_list args;
1585 int ret;
1586
1587 va_start(args, fmt);
1588 ret = vbin_printf(bin_buf, size, fmt, args);
1589 va_end(args);
1590 return ret;
1591}
1592EXPORT_SYMBOL_GPL(bprintf);
1593
1594#endif /* CONFIG_BINARY_PRINTF */
1595
1061/** 1596/**
1062 * vsscanf - Unformat a buffer into a list of arguments 1597 * vsscanf - Unformat a buffer into a list of arguments
1063 * @buf: input buffer 1598 * @buf: input buffer