1616#include <linux/slab.h>
1717
1818#include <linux/scatterlist.h>
19+ #include <linux/swap.h> /* For nr_free_buffer_pages() */
1920
2021#define RESULT_OK 0
2122#define RESULT_FAIL 1
2526#define BUFFER_ORDER 2
2627#define BUFFER_SIZE (PAGE_SIZE << BUFFER_ORDER)
2728
29+ /*
30+ * Limit the test area size to the maximum MMC HC erase group size. Note that
31+ * the maximum SD allocation unit size is just 4MiB.
32+ */
33+ #define TEST_AREA_MAX_SIZE (128 * 1024 * 1024)
34+
2835/**
2936 * struct mmc_test_pages - pages allocated by 'alloc_pages()'.
3037 * @page: first page in the allocation
@@ -47,17 +54,17 @@ struct mmc_test_mem {
4754
4855/**
4956 * struct mmc_test_area - information for performance tests.
50- * @dev_addr: address on card at which to do performance tests
5157 * @max_sz: test area size (in bytes)
58+ * @dev_addr: address on card at which to do performance tests
5259 * @max_segs: maximum segments in scatterlist @sg
5360 * @blocks: number of (512 byte) blocks currently mapped by @sg
5461 * @sg_len: length of currently mapped scatterlist @sg
5562 * @mem: allocated memory
5663 * @sg: scatterlist
5764 */
5865struct mmc_test_area {
66+ unsigned long max_sz ;
5967 unsigned int dev_addr ;
60- unsigned int max_sz ;
6168 unsigned int max_segs ;
6269 unsigned int blocks ;
6370 unsigned int sg_len ;
@@ -238,20 +245,19 @@ static void mmc_test_free_mem(struct mmc_test_mem *mem)
238245
239246/*
240247 * Allocate a lot of memory, preferrably max_sz but at least min_sz. In case
241- * there isn't much memory do not exceed 1/16th total RAM .
248+ * there isn't much memory do not exceed 1/16th total lowmem pages .
242249 */
243- static struct mmc_test_mem * mmc_test_alloc_mem (unsigned int min_sz ,
244- unsigned int max_sz )
250+ static struct mmc_test_mem * mmc_test_alloc_mem (unsigned long min_sz ,
251+ unsigned long max_sz )
245252{
246- unsigned int max_page_cnt = DIV_ROUND_UP (max_sz , PAGE_SIZE );
247- unsigned int min_page_cnt = DIV_ROUND_UP (min_sz , PAGE_SIZE );
248- unsigned int page_cnt = 0 ;
253+ unsigned long max_page_cnt = DIV_ROUND_UP (max_sz , PAGE_SIZE );
254+ unsigned long min_page_cnt = DIV_ROUND_UP (min_sz , PAGE_SIZE );
255+ unsigned long page_cnt = 0 ;
256+ unsigned long limit = nr_free_buffer_pages () >> 4 ;
249257 struct mmc_test_mem * mem ;
250- struct sysinfo si ;
251258
252- si_meminfo (& si );
253- if (max_page_cnt > si .totalram >> 4 )
254- max_page_cnt = si .totalram >> 4 ;
259+ if (max_page_cnt > limit )
260+ max_page_cnt = limit ;
255261 if (max_page_cnt < min_page_cnt )
256262 max_page_cnt = min_page_cnt ;
257263
@@ -270,7 +276,7 @@ static struct mmc_test_mem *mmc_test_alloc_mem(unsigned int min_sz,
270276 gfp_t flags = GFP_KERNEL | GFP_DMA | __GFP_NOWARN |
271277 __GFP_NORETRY ;
272278
273- order = get_order (page_cnt << PAGE_SHIFT );
279+ order = get_order (max_page_cnt << PAGE_SHIFT );
274280 while (1 ) {
275281 page = alloc_pages (flags , order );
276282 if (page || !order )
@@ -285,8 +291,10 @@ static struct mmc_test_mem *mmc_test_alloc_mem(unsigned int min_sz,
285291 mem -> arr [mem -> cnt ].page = page ;
286292 mem -> arr [mem -> cnt ].order = order ;
287293 mem -> cnt += 1 ;
288- max_page_cnt -= 1 << order ;
289- page_cnt += 1 << order ;
294+ if (max_page_cnt <= (1UL << order ))
295+ break ;
296+ max_page_cnt -= 1UL << order ;
297+ page_cnt += 1UL << order ;
290298 }
291299
292300 return mem ;
@@ -300,7 +308,7 @@ static struct mmc_test_mem *mmc_test_alloc_mem(unsigned int min_sz,
300308 * Map memory into a scatterlist. Optionally allow the same memory to be
301309 * mapped more than once.
302310 */
303- static int mmc_test_map_sg (struct mmc_test_mem * mem , unsigned int sz ,
311+ static int mmc_test_map_sg (struct mmc_test_mem * mem , unsigned long sz ,
304312 struct scatterlist * sglist , int repeat ,
305313 unsigned int max_segs , unsigned int * sg_len )
306314{
@@ -312,7 +320,7 @@ static int mmc_test_map_sg(struct mmc_test_mem *mem, unsigned int sz,
312320 * sg_len = 0 ;
313321 do {
314322 for (i = 0 ; i < mem -> cnt ; i ++ ) {
315- unsigned int len = PAGE_SIZE << mem -> arr [i ].order ;
323+ unsigned long len = PAGE_SIZE << mem -> arr [i ].order ;
316324
317325 if (sz < len )
318326 len = sz ;
@@ -344,13 +352,14 @@ static int mmc_test_map_sg(struct mmc_test_mem *mem, unsigned int sz,
344352 * same memory to be mapped more than once.
345353 */
346354static int mmc_test_map_sg_max_scatter (struct mmc_test_mem * mem ,
347- unsigned int sz ,
355+ unsigned long sz ,
348356 struct scatterlist * sglist ,
349357 unsigned int max_segs ,
350358 unsigned int * sg_len )
351359{
352360 struct scatterlist * sg = NULL ;
353- unsigned int i = mem -> cnt , cnt , len ;
361+ unsigned int i = mem -> cnt , cnt ;
362+ unsigned long len ;
354363 void * base , * addr , * last_addr = NULL ;
355364
356365 sg_init_table (sglist , max_segs );
@@ -1202,7 +1211,7 @@ static int mmc_test_no_highmem(struct mmc_test_card *test)
12021211/*
12031212 * Map sz bytes so that it can be transferred.
12041213 */
1205- static int mmc_test_area_map (struct mmc_test_card * test , unsigned int sz ,
1214+ static int mmc_test_area_map (struct mmc_test_card * test , unsigned long sz ,
12061215 int max_scatter )
12071216{
12081217 struct mmc_test_area * t = & test -> area ;
@@ -1233,7 +1242,7 @@ static int mmc_test_area_transfer(struct mmc_test_card *test,
12331242/*
12341243 * Map and transfer bytes.
12351244 */
1236- static int mmc_test_area_io (struct mmc_test_card * test , unsigned int sz ,
1245+ static int mmc_test_area_io (struct mmc_test_card * test , unsigned long sz ,
12371246 unsigned int dev_addr , int write , int max_scatter ,
12381247 int timed )
12391248{
@@ -1308,19 +1317,22 @@ static int mmc_test_area_cleanup(struct mmc_test_card *test)
13081317static int mmc_test_area_init (struct mmc_test_card * test , int erase , int fill )
13091318{
13101319 struct mmc_test_area * t = & test -> area ;
1311- unsigned int min_sz = 64 * 1024 ;
1320+ unsigned long min_sz = 64 * 1024 ;
13121321 int ret ;
13131322
13141323 ret = mmc_test_set_blksize (test , 512 );
13151324 if (ret )
13161325 return ret ;
13171326
1327+ if (test -> card -> pref_erase > TEST_AREA_MAX_SIZE >> 9 )
1328+ t -> max_sz = TEST_AREA_MAX_SIZE ;
1329+ else
1330+ t -> max_sz = (unsigned long )test -> card -> pref_erase << 9 ;
13181331 /*
13191332 * Try to allocate enough memory for the whole area. Less is OK
13201333 * because the same memory can be mapped into the scatterlist more than
13211334 * once.
13221335 */
1323- t -> max_sz = test -> card -> pref_erase << 9 ;
13241336 t -> mem = mmc_test_alloc_mem (min_sz , t -> max_sz );
13251337 if (!t -> mem )
13261338 return - ENOMEM ;
@@ -1430,7 +1442,8 @@ static int mmc_test_best_write_perf_max_scatter(struct mmc_test_card *test)
14301442 */
14311443static int mmc_test_profile_read_perf (struct mmc_test_card * test )
14321444{
1433- unsigned int sz , dev_addr ;
1445+ unsigned long sz ;
1446+ unsigned int dev_addr ;
14341447 int ret ;
14351448
14361449 for (sz = 512 ; sz < test -> area .max_sz ; sz <<= 1 ) {
@@ -1448,7 +1461,8 @@ static int mmc_test_profile_read_perf(struct mmc_test_card *test)
14481461 */
14491462static int mmc_test_profile_write_perf (struct mmc_test_card * test )
14501463{
1451- unsigned int sz , dev_addr ;
1464+ unsigned long sz ;
1465+ unsigned int dev_addr ;
14521466 int ret ;
14531467
14541468 ret = mmc_test_area_erase (test );
@@ -1472,7 +1486,8 @@ static int mmc_test_profile_write_perf(struct mmc_test_card *test)
14721486 */
14731487static int mmc_test_profile_trim_perf (struct mmc_test_card * test )
14741488{
1475- unsigned int sz , dev_addr ;
1489+ unsigned long sz ;
1490+ unsigned int dev_addr ;
14761491 struct timespec ts1 , ts2 ;
14771492 int ret ;
14781493
@@ -1506,7 +1521,8 @@ static int mmc_test_profile_trim_perf(struct mmc_test_card *test)
15061521 */
15071522static int mmc_test_profile_seq_read_perf (struct mmc_test_card * test )
15081523{
1509- unsigned int sz , dev_addr , i , cnt ;
1524+ unsigned long sz ;
1525+ unsigned int dev_addr , i , cnt ;
15101526 struct timespec ts1 , ts2 ;
15111527 int ret ;
15121528
@@ -1531,7 +1547,8 @@ static int mmc_test_profile_seq_read_perf(struct mmc_test_card *test)
15311547 */
15321548static int mmc_test_profile_seq_write_perf (struct mmc_test_card * test )
15331549{
1534- unsigned int sz , dev_addr , i , cnt ;
1550+ unsigned long sz ;
1551+ unsigned int dev_addr , i , cnt ;
15351552 struct timespec ts1 , ts2 ;
15361553 int ret ;
15371554
@@ -1559,7 +1576,8 @@ static int mmc_test_profile_seq_write_perf(struct mmc_test_card *test)
15591576 */
15601577static int mmc_test_profile_seq_trim_perf (struct mmc_test_card * test )
15611578{
1562- unsigned int sz , dev_addr , i , cnt ;
1579+ unsigned long sz ;
1580+ unsigned int dev_addr , i , cnt ;
15631581 struct timespec ts1 , ts2 ;
15641582 int ret ;
15651583
0 commit comments