Skip to content
This repository
Browse code

lib/list_sort: test: check element addresses

Improve 'lib_sort()' test and check that:
 o 'cmp()' is called only for elements which were present in the original list,
   i.e., the 'a' and 'b' parameters are valid
 o the resulted (sorted) list consists onlly of the original elements
 o intdoruce "poison" fields to make sure data around 'struc list_head' field
   are not corrupted.

Signed-off-by: Artem Bityutskiy <Artem.Bityutskiy@nokia.com>
Cc: Don Mullis <don.mullis@gmail.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
  • Loading branch information...
commit 041b78f232bb87b2de8ca3fed50384bc7dc9c2de 1 parent 014afa9
Artem Bityutskiy authored torvalds committed

Showing 1 changed file with 67 additions and 8 deletions. Show diff stats Hide diff stats

  1. +67 8 lib/list_sort.c
75 lib/list_sort.c
@@ -145,23 +145,65 @@ EXPORT_SYMBOL(list_sort);
145 145
146 146 #include <linux/random.h>
147 147
  148 +/*
  149 + * The pattern of set bits in the list length determines which cases
  150 + * are hit in list_sort().
  151 + */
  152 +#define TEST_LIST_LEN (512+128+2) /* not including head */
  153 +
  154 +#define TEST_POISON1 0xDEADBEEF
  155 +#define TEST_POISON2 0xA324354C
  156 +
148 157 struct debug_el {
  158 + unsigned int poison1;
149 159 struct list_head list;
  160 + unsigned int poison2;
150 161 int value;
151 162 unsigned serial;
152 163 };
153 164
154   -static int cmp(void *priv, struct list_head *a, struct list_head *b)
  165 +/* Array, containing pointers to all elements in the test list */
  166 +static struct debug_el **elts __initdata;
  167 +
  168 +static int __init check(struct debug_el *ela, struct debug_el *elb)
155 169 {
156   - return container_of(a, struct debug_el, list)->value
157   - - container_of(b, struct debug_el, list)->value;
  170 + if (ela->serial >= TEST_LIST_LEN) {
  171 + printk(KERN_ERR "list_sort_test: error: incorrect serial %d\n",
  172 + ela->serial);
  173 + return -EINVAL;
  174 + }
  175 + if (elb->serial >= TEST_LIST_LEN) {
  176 + printk(KERN_ERR "list_sort_test: error: incorrect serial %d\n",
  177 + elb->serial);
  178 + return -EINVAL;
  179 + }
  180 + if (elts[ela->serial] != ela || elts[elb->serial] != elb) {
  181 + printk(KERN_ERR "list_sort_test: error: phantom element\n");
  182 + return -EINVAL;
  183 + }
  184 + if (ela->poison1 != TEST_POISON1 || ela->poison2 != TEST_POISON2) {
  185 + printk(KERN_ERR "list_sort_test: error: bad poison: %#x/%#x\n",
  186 + ela->poison1, ela->poison2);
  187 + return -EINVAL;
  188 + }
  189 + if (elb->poison1 != TEST_POISON1 || elb->poison2 != TEST_POISON2) {
  190 + printk(KERN_ERR "list_sort_test: error: bad poison: %#x/%#x\n",
  191 + elb->poison1, elb->poison2);
  192 + return -EINVAL;
  193 + }
  194 + return 0;
158 195 }
159 196
160   -/*
161   - * The pattern of set bits in the list length determines which cases
162   - * are hit in list_sort().
163   - */
164   -#define TEST_LIST_LEN (512+128+2) /* not including head */
  197 +static int __init cmp(void *priv, struct list_head *a, struct list_head *b)
  198 +{
  199 + struct debug_el *ela, *elb;
  200 +
  201 + ela = container_of(a, struct debug_el, list);
  202 + elb = container_of(b, struct debug_el, list);
  203 +
  204 + check(ela, elb);
  205 + return ela->value - elb->value;
  206 +}
165 207
166 208 static int __init list_sort_test(void)
167 209 {
@@ -172,6 +214,13 @@ static int __init list_sort_test(void)
172 214
173 215 printk(KERN_DEBUG "list_sort_test: start testing list_sort()\n");
174 216
  217 + elts = kmalloc(sizeof(void *) * TEST_LIST_LEN, GFP_KERNEL);
  218 + if (!elts) {
  219 + printk(KERN_ERR "list_sort_test: error: cannot allocate "
  220 + "memory\n");
  221 + goto exit;
  222 + }
  223 +
175 224 for (i = 0; i < TEST_LIST_LEN; i++) {
176 225 el = kmalloc(sizeof(*el), GFP_KERNEL);
177 226 if (!el) {
@@ -182,6 +231,9 @@ static int __init list_sort_test(void)
182 231 /* force some equivalencies */
183 232 el->value = random32() % (TEST_LIST_LEN/3);
184 233 el->serial = i;
  234 + el->poison1 = TEST_POISON1;
  235 + el->poison2 = TEST_POISON2;
  236 + elts[i] = el;
185 237 list_add_tail(&el->list, &head);
186 238 }
187 239
@@ -211,6 +263,12 @@ static int __init list_sort_test(void)
211 263 "equivalent elements not preserved\n");
212 264 goto exit;
213 265 }
  266 +
  267 + if (check(el, el1)) {
  268 + printk(KERN_ERR "list_sort_test: error: element check "
  269 + "failed\n");
  270 + goto exit;
  271 + }
214 272 count++;
215 273 }
216 274
@@ -222,6 +280,7 @@ static int __init list_sort_test(void)
222 280
223 281 err = 0;
224 282 exit:
  283 + kfree(elts);
225 284 list_for_each_safe(cur, tmp, &head) {
226 285 list_del(cur);
227 286 kfree(container_of(cur, struct debug_el, list));

0 comments on commit 041b78f

Please sign in to comment.
Something went wrong with that request. Please try again.