Skip to content
Snippets Groups Projects
slab.c 82.1 KiB
Newer Older
  • Learn to ignore specific revisions
  • Linus Torvalds's avatar
    Linus Torvalds committed
    1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671
    	int size = obj_reallen(cachep);
    	addr = &((char*)addr)[obj_dbghead(cachep)];
    
    	memset(addr, val, size);
    	*(unsigned char *)(addr+size-1) = POISON_END;
    }
    
    static void dump_line(char *data, int offset, int limit)
    {
    	int i;
    	printk(KERN_ERR "%03x:", offset);
    	for (i=0;i<limit;i++) {
    		printk(" %02x", (unsigned char)data[offset+i]);
    	}
    	printk("\n");
    }
    #endif
    
    #if DEBUG
    
    static void print_objinfo(kmem_cache_t *cachep, void *objp, int lines)
    {
    	int i, size;
    	char *realobj;
    
    	if (cachep->flags & SLAB_RED_ZONE) {
    		printk(KERN_ERR "Redzone: 0x%lx/0x%lx.\n",
    			*dbg_redzone1(cachep, objp),
    			*dbg_redzone2(cachep, objp));
    	}
    
    	if (cachep->flags & SLAB_STORE_USER) {
    		printk(KERN_ERR "Last user: [<%p>]",
    				*dbg_userword(cachep, objp));
    		print_symbol("(%s)",
    				(unsigned long)*dbg_userword(cachep, objp));
    		printk("\n");
    	}
    	realobj = (char*)objp+obj_dbghead(cachep);
    	size = obj_reallen(cachep);
    	for (i=0; i<size && lines;i+=16, lines--) {
    		int limit;
    		limit = 16;
    		if (i+limit > size)
    			limit = size-i;
    		dump_line(realobj, i, limit);
    	}
    }
    
    static void check_poison_obj(kmem_cache_t *cachep, void *objp)
    {
    	char *realobj;
    	int size, i;
    	int lines = 0;
    
    	realobj = (char*)objp+obj_dbghead(cachep);
    	size = obj_reallen(cachep);
    
    	for (i=0;i<size;i++) {
    		char exp = POISON_FREE;
    		if (i == size-1)
    			exp = POISON_END;
    		if (realobj[i] != exp) {
    			int limit;
    			/* Mismatch ! */
    			/* Print header */
    			if (lines == 0) {
    				printk(KERN_ERR "Slab corruption: start=%p, len=%d\n",
    						realobj, size);
    				print_objinfo(cachep, objp, 0);
    			}
    			/* Hexdump the affected line */
    			i = (i/16)*16;
    			limit = 16;
    			if (i+limit > size)
    				limit = size-i;
    			dump_line(realobj, i, limit);
    			i += 16;
    			lines++;
    			/* Limit to 5 lines */
    			if (lines > 5)
    				break;
    		}
    	}
    	if (lines != 0) {
    		/* Print some data about the neighboring objects, if they
    		 * exist:
    		 */
    		struct slab *slabp = GET_PAGE_SLAB(virt_to_page(objp));
    		int objnr;
    
    		objnr = (objp-slabp->s_mem)/cachep->objsize;
    		if (objnr) {
    			objp = slabp->s_mem+(objnr-1)*cachep->objsize;
    			realobj = (char*)objp+obj_dbghead(cachep);
    			printk(KERN_ERR "Prev obj: start=%p, len=%d\n",
    						realobj, size);
    			print_objinfo(cachep, objp, 2);
    		}
    		if (objnr+1 < cachep->num) {
    			objp = slabp->s_mem+(objnr+1)*cachep->objsize;
    			realobj = (char*)objp+obj_dbghead(cachep);
    			printk(KERN_ERR "Next obj: start=%p, len=%d\n",
    						realobj, size);
    			print_objinfo(cachep, objp, 2);
    		}
    	}
    }
    #endif
    
    /* Destroy all the objs in a slab, and release the mem back to the system.
     * Before calling the slab must have been unlinked from the cache.
     * The cache-lock is not held/needed.
     */
    static void slab_destroy (kmem_cache_t *cachep, struct slab *slabp)
    {
    	void *addr = slabp->s_mem - slabp->colouroff;
    
    #if DEBUG
    	int i;
    	for (i = 0; i < cachep->num; i++) {
    		void *objp = slabp->s_mem + cachep->objsize * i;
    
    		if (cachep->flags & SLAB_POISON) {
    #ifdef CONFIG_DEBUG_PAGEALLOC
    			if ((cachep->objsize%PAGE_SIZE)==0 && OFF_SLAB(cachep))
    				kernel_map_pages(virt_to_page(objp), cachep->objsize/PAGE_SIZE,1);
    			else
    				check_poison_obj(cachep, objp);
    #else
    			check_poison_obj(cachep, objp);
    #endif
    		}
    		if (cachep->flags & SLAB_RED_ZONE) {
    			if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
    				slab_error(cachep, "start of a freed object "
    							"was overwritten");
    			if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)
    				slab_error(cachep, "end of a freed object "
    							"was overwritten");
    		}
    		if (cachep->dtor && !(cachep->flags & SLAB_POISON))
    			(cachep->dtor)(objp+obj_dbghead(cachep), cachep, 0);
    	}
    #else
    	if (cachep->dtor) {
    		int i;
    		for (i = 0; i < cachep->num; i++) {
    			void* objp = slabp->s_mem+cachep->objsize*i;
    			(cachep->dtor)(objp, cachep, 0);
    		}
    	}
    #endif
    
    	if (unlikely(cachep->flags & SLAB_DESTROY_BY_RCU)) {
    		struct slab_rcu *slab_rcu;
    
    		slab_rcu = (struct slab_rcu *) slabp;
    		slab_rcu->cachep = cachep;
    		slab_rcu->addr = addr;
    		call_rcu(&slab_rcu->head, kmem_rcu_free);
    	} else {
    		kmem_freepages(cachep, addr);
    		if (OFF_SLAB(cachep))
    			kmem_cache_free(cachep->slabp_cache, slabp);
    	}
    }
    
    /**
     * kmem_cache_create - Create a cache.
     * @name: A string which is used in /proc/slabinfo to identify this cache.
     * @size: The size of objects to be created in this cache.
     * @align: The required alignment for the objects.
     * @flags: SLAB flags
     * @ctor: A constructor for the objects.
     * @dtor: A destructor for the objects.
     *
     * Returns a ptr to the cache on success, NULL on failure.
     * Cannot be called within a int, but can be interrupted.
     * The @ctor is run when new pages are allocated by the cache
     * and the @dtor is run before the pages are handed back.
     *
     * @name must be valid until the cache is destroyed. This implies that
     * the module calling this has to destroy the cache before getting 
     * unloaded.
     * 
     * The flags are
     *
     * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
     * to catch references to uninitialised memory.
     *
     * %SLAB_RED_ZONE - Insert `Red' zones around the allocated memory to check
     * for buffer overruns.
     *
     * %SLAB_NO_REAP - Don't automatically reap this cache when we're under
     * memory pressure.
     *
     * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
     * cacheline.  This can be beneficial if you're counting cycles as closely
     * as davem.
     */
    kmem_cache_t *
    kmem_cache_create (const char *name, size_t size, size_t align,
    	unsigned long flags, void (*ctor)(void*, kmem_cache_t *, unsigned long),
    	void (*dtor)(void*, kmem_cache_t *, unsigned long))
    {
    	size_t left_over, slab_size, ralign;
    	kmem_cache_t *cachep = NULL;
    
    	/*
    	 * Sanity checks... these are all serious usage bugs.
    	 */
    	if ((!name) ||
    		in_interrupt() ||
    		(size < BYTES_PER_WORD) ||
    		(size > (1<<MAX_OBJ_ORDER)*PAGE_SIZE) ||
    		(dtor && !ctor)) {
    			printk(KERN_ERR "%s: Early error in slab %s\n",
    					__FUNCTION__, name);
    			BUG();
    		}
    
    #if DEBUG
    	WARN_ON(strchr(name, ' '));	/* It confuses parsers */
    	if ((flags & SLAB_DEBUG_INITIAL) && !ctor) {
    		/* No constructor, but inital state check requested */
    		printk(KERN_ERR "%s: No con, but init state check "
    				"requested - %s\n", __FUNCTION__, name);
    		flags &= ~SLAB_DEBUG_INITIAL;
    	}
    
    #if FORCED_DEBUG
    	/*
    	 * Enable redzoning and last user accounting, except for caches with
    	 * large objects, if the increased size would increase the object size
    	 * above the next power of two: caches with object sizes just above a
    	 * power of two have a significant amount of internal fragmentation.
    	 */
    	if ((size < 4096 || fls(size-1) == fls(size-1+3*BYTES_PER_WORD)))
    		flags |= SLAB_RED_ZONE|SLAB_STORE_USER;
    	if (!(flags & SLAB_DESTROY_BY_RCU))
    		flags |= SLAB_POISON;
    #endif
    	if (flags & SLAB_DESTROY_BY_RCU)
    		BUG_ON(flags & SLAB_POISON);
    #endif
    	if (flags & SLAB_DESTROY_BY_RCU)
    		BUG_ON(dtor);
    
    	/*
    	 * Always checks flags, a caller might be expecting debug
    	 * support which isn't available.
    	 */
    	if (flags & ~CREATE_MASK)
    		BUG();
    
    	/* Check that size is in terms of words.  This is needed to avoid
    	 * unaligned accesses for some archs when redzoning is used, and makes
    	 * sure any on-slab bufctl's are also correctly aligned.
    	 */
    	if (size & (BYTES_PER_WORD-1)) {
    		size += (BYTES_PER_WORD-1);
    		size &= ~(BYTES_PER_WORD-1);
    	}
    
    	/* calculate out the final buffer alignment: */
    	/* 1) arch recommendation: can be overridden for debug */
    	if (flags & SLAB_HWCACHE_ALIGN) {
    		/* Default alignment: as specified by the arch code.
    		 * Except if an object is really small, then squeeze multiple
    		 * objects into one cacheline.
    		 */
    		ralign = cache_line_size();
    		while (size <= ralign/2)
    			ralign /= 2;
    	} else {
    		ralign = BYTES_PER_WORD;
    	}
    	/* 2) arch mandated alignment: disables debug if necessary */
    	if (ralign < ARCH_SLAB_MINALIGN) {
    		ralign = ARCH_SLAB_MINALIGN;
    		if (ralign > BYTES_PER_WORD)
    			flags &= ~(SLAB_RED_ZONE|SLAB_STORE_USER);
    	}
    	/* 3) caller mandated alignment: disables debug if necessary */
    	if (ralign < align) {
    		ralign = align;
    		if (ralign > BYTES_PER_WORD)
    			flags &= ~(SLAB_RED_ZONE|SLAB_STORE_USER);
    	}
    	/* 4) Store it. Note that the debug code below can reduce
    	 *    the alignment to BYTES_PER_WORD.
    	 */
    	align = ralign;
    
    	/* Get cache's description obj. */
    	cachep = (kmem_cache_t *) kmem_cache_alloc(&cache_cache, SLAB_KERNEL);
    	if (!cachep)
    		goto opps;
    	memset(cachep, 0, sizeof(kmem_cache_t));
    
    #if DEBUG
    	cachep->reallen = size;
    
    	if (flags & SLAB_RED_ZONE) {
    		/* redzoning only works with word aligned caches */
    		align = BYTES_PER_WORD;
    
    		/* add space for red zone words */
    		cachep->dbghead += BYTES_PER_WORD;
    		size += 2*BYTES_PER_WORD;
    	}
    	if (flags & SLAB_STORE_USER) {
    		/* user store requires word alignment and
    		 * one word storage behind the end of the real
    		 * object.
    		 */
    		align = BYTES_PER_WORD;
    		size += BYTES_PER_WORD;
    	}
    #if FORCED_DEBUG && defined(CONFIG_DEBUG_PAGEALLOC)
    	if (size > 128 && cachep->reallen > cache_line_size() && size < PAGE_SIZE) {
    		cachep->dbghead += PAGE_SIZE - size;
    		size = PAGE_SIZE;
    	}
    #endif
    #endif
    
    	/* Determine if the slab management is 'on' or 'off' slab. */
    	if (size >= (PAGE_SIZE>>3))
    		/*
    		 * Size is large, assume best to place the slab management obj
    		 * off-slab (should allow better packing of objs).
    		 */
    		flags |= CFLGS_OFF_SLAB;
    
    	size = ALIGN(size, align);
    
    	if ((flags & SLAB_RECLAIM_ACCOUNT) && size <= PAGE_SIZE) {
    		/*
    		 * A VFS-reclaimable slab tends to have most allocations
    		 * as GFP_NOFS and we really don't want to have to be allocating
    		 * higher-order pages when we are unable to shrink dcache.
    		 */
    		cachep->gfporder = 0;
    		cache_estimate(cachep->gfporder, size, align, flags,
    					&left_over, &cachep->num);
    	} else {
    		/*
    		 * Calculate size (in pages) of slabs, and the num of objs per
    		 * slab.  This could be made much more intelligent.  For now,
    		 * try to avoid using high page-orders for slabs.  When the
    		 * gfp() funcs are more friendly towards high-order requests,
    		 * this should be changed.
    		 */
    		do {
    			unsigned int break_flag = 0;
    cal_wastage:
    			cache_estimate(cachep->gfporder, size, align, flags,
    						&left_over, &cachep->num);
    			if (break_flag)
    				break;
    			if (cachep->gfporder >= MAX_GFP_ORDER)
    				break;
    			if (!cachep->num)
    				goto next;
    			if (flags & CFLGS_OFF_SLAB &&
    					cachep->num > offslab_limit) {
    				/* This num of objs will cause problems. */
    				cachep->gfporder--;
    				break_flag++;
    				goto cal_wastage;
    			}
    
    			/*
    			 * Large num of objs is good, but v. large slabs are
    			 * currently bad for the gfp()s.
    			 */
    			if (cachep->gfporder >= slab_break_gfp_order)
    				break;
    
    			if ((left_over*8) <= (PAGE_SIZE<<cachep->gfporder))
    				break;	/* Acceptable internal fragmentation. */
    next:
    			cachep->gfporder++;
    		} while (1);
    	}
    
    	if (!cachep->num) {
    		printk("kmem_cache_create: couldn't create cache %s.\n", name);
    		kmem_cache_free(&cache_cache, cachep);
    		cachep = NULL;
    		goto opps;
    	}
    	slab_size = ALIGN(cachep->num*sizeof(kmem_bufctl_t)
    				+ sizeof(struct slab), align);
    
    	/*
    	 * If the slab has been placed off-slab, and we have enough space then
    	 * move it on-slab. This is at the expense of any extra colouring.
    	 */
    	if (flags & CFLGS_OFF_SLAB && left_over >= slab_size) {
    		flags &= ~CFLGS_OFF_SLAB;
    		left_over -= slab_size;
    	}
    
    	if (flags & CFLGS_OFF_SLAB) {
    		/* really off slab. No need for manual alignment */
    		slab_size = cachep->num*sizeof(kmem_bufctl_t)+sizeof(struct slab);
    	}
    
    	cachep->colour_off = cache_line_size();
    	/* Offset must be a multiple of the alignment. */
    	if (cachep->colour_off < align)
    		cachep->colour_off = align;
    	cachep->colour = left_over/cachep->colour_off;
    	cachep->slab_size = slab_size;
    	cachep->flags = flags;
    	cachep->gfpflags = 0;
    	if (flags & SLAB_CACHE_DMA)
    		cachep->gfpflags |= GFP_DMA;
    	spin_lock_init(&cachep->spinlock);
    	cachep->objsize = size;
    	/* NUMA */
    	INIT_LIST_HEAD(&cachep->lists.slabs_full);
    	INIT_LIST_HEAD(&cachep->lists.slabs_partial);
    	INIT_LIST_HEAD(&cachep->lists.slabs_free);
    
    	if (flags & CFLGS_OFF_SLAB)
    		cachep->slabp_cache = kmem_find_general_cachep(slab_size,0);
    	cachep->ctor = ctor;
    	cachep->dtor = dtor;
    	cachep->name = name;
    
    	/* Don't let CPUs to come and go */
    	lock_cpu_hotplug();
    
    	if (g_cpucache_up == FULL) {
    		enable_cpucache(cachep);
    	} else {
    		if (g_cpucache_up == NONE) {
    			/* Note: the first kmem_cache_create must create
    			 * the cache that's used by kmalloc(24), otherwise
    			 * the creation of further caches will BUG().
    			 */
    			cachep->array[smp_processor_id()] = &initarray_generic.cache;
    			g_cpucache_up = PARTIAL;
    		} else {
    			cachep->array[smp_processor_id()] = kmalloc(sizeof(struct arraycache_init),GFP_KERNEL);
    		}
    		BUG_ON(!ac_data(cachep));
    		ac_data(cachep)->avail = 0;
    		ac_data(cachep)->limit = BOOT_CPUCACHE_ENTRIES;
    		ac_data(cachep)->batchcount = 1;
    		ac_data(cachep)->touched = 0;
    		cachep->batchcount = 1;
    		cachep->limit = BOOT_CPUCACHE_ENTRIES;
    		cachep->free_limit = (1+num_online_cpus())*cachep->batchcount
    					+ cachep->num;
    	} 
    
    	cachep->lists.next_reap = jiffies + REAPTIMEOUT_LIST3 +
    					((unsigned long)cachep)%REAPTIMEOUT_LIST3;
    
    	/* Need the semaphore to access the chain. */
    	down(&cache_chain_sem);
    	{
    		struct list_head *p;
    		mm_segment_t old_fs;
    
    		old_fs = get_fs();
    		set_fs(KERNEL_DS);
    		list_for_each(p, &cache_chain) {
    			kmem_cache_t *pc = list_entry(p, kmem_cache_t, next);
    			char tmp;
    			/* This happens when the module gets unloaded and doesn't
    			   destroy its slab cache and noone else reuses the vmalloc
    			   area of the module. Print a warning. */
    			if (__get_user(tmp,pc->name)) { 
    				printk("SLAB: cache with size %d has lost its name\n", 
    					pc->objsize); 
    				continue; 
    			} 	
    			if (!strcmp(pc->name,name)) { 
    				printk("kmem_cache_create: duplicate cache %s\n",name); 
    				up(&cache_chain_sem); 
    				unlock_cpu_hotplug();
    				BUG(); 
    			}	
    		}
    		set_fs(old_fs);
    	}
    
    	/* cache setup completed, link it into the list */
    	list_add(&cachep->next, &cache_chain);
    	up(&cache_chain_sem);
    	unlock_cpu_hotplug();
    opps:
    	if (!cachep && (flags & SLAB_PANIC))
    		panic("kmem_cache_create(): failed to create slab `%s'\n",
    			name);
    	return cachep;
    }
    EXPORT_SYMBOL(kmem_cache_create);
    
    #if DEBUG
    static void check_irq_off(void)
    {
    	BUG_ON(!irqs_disabled());
    }
    
    static void check_irq_on(void)
    {
    	BUG_ON(irqs_disabled());
    }
    
    static void check_spinlock_acquired(kmem_cache_t *cachep)
    {
    #ifdef CONFIG_SMP
    	check_irq_off();
    	BUG_ON(spin_trylock(&cachep->spinlock));
    #endif
    }
    #else
    #define check_irq_off()	do { } while(0)
    #define check_irq_on()	do { } while(0)
    #define check_spinlock_acquired(x) do { } while(0)
    #endif
    
    /*
     * Waits for all CPUs to execute func().
     */
    static void smp_call_function_all_cpus(void (*func) (void *arg), void *arg)
    {
    	check_irq_on();
    	preempt_disable();
    
    	local_irq_disable();
    	func(arg);
    	local_irq_enable();
    
    	if (smp_call_function(func, arg, 1, 1))
    		BUG();
    
    	preempt_enable();
    }
    
    static void drain_array_locked(kmem_cache_t* cachep,
    				struct array_cache *ac, int force);
    
    static void do_drain(void *arg)
    {
    	kmem_cache_t *cachep = (kmem_cache_t*)arg;
    	struct array_cache *ac;
    
    	check_irq_off();
    	ac = ac_data(cachep);
    	spin_lock(&cachep->spinlock);
    	free_block(cachep, &ac_entry(ac)[0], ac->avail);
    	spin_unlock(&cachep->spinlock);
    	ac->avail = 0;
    }
    
    static void drain_cpu_caches(kmem_cache_t *cachep)
    {
    	smp_call_function_all_cpus(do_drain, cachep);
    	check_irq_on();
    	spin_lock_irq(&cachep->spinlock);
    	if (cachep->lists.shared)
    		drain_array_locked(cachep, cachep->lists.shared, 1);
    	spin_unlock_irq(&cachep->spinlock);
    }
    
    
    /* NUMA shrink all list3s */
    static int __cache_shrink(kmem_cache_t *cachep)
    {
    	struct slab *slabp;
    	int ret;
    
    	drain_cpu_caches(cachep);
    
    	check_irq_on();
    	spin_lock_irq(&cachep->spinlock);
    
    	for(;;) {
    		struct list_head *p;
    
    		p = cachep->lists.slabs_free.prev;
    		if (p == &cachep->lists.slabs_free)
    			break;
    
    		slabp = list_entry(cachep->lists.slabs_free.prev, struct slab, list);
    #if DEBUG
    		if (slabp->inuse)
    			BUG();
    #endif
    		list_del(&slabp->list);
    
    		cachep->lists.free_objects -= cachep->num;
    		spin_unlock_irq(&cachep->spinlock);
    		slab_destroy(cachep, slabp);
    		spin_lock_irq(&cachep->spinlock);
    	}
    	ret = !list_empty(&cachep->lists.slabs_full) ||
    		!list_empty(&cachep->lists.slabs_partial);
    	spin_unlock_irq(&cachep->spinlock);
    	return ret;
    }
    
    /**
     * kmem_cache_shrink - Shrink a cache.
     * @cachep: The cache to shrink.
     *
     * Releases as many slabs as possible for a cache.
     * To help debugging, a zero exit status indicates all slabs were released.
     */
    int kmem_cache_shrink(kmem_cache_t *cachep)
    {
    	if (!cachep || in_interrupt())
    		BUG();
    
    	return __cache_shrink(cachep);
    }
    EXPORT_SYMBOL(kmem_cache_shrink);
    
    /**
     * kmem_cache_destroy - delete a cache
     * @cachep: the cache to destroy
     *
     * Remove a kmem_cache_t object from the slab cache.
     * Returns 0 on success.
     *
     * It is expected this function will be called by a module when it is
     * unloaded.  This will remove the cache completely, and avoid a duplicate
     * cache being allocated each time a module is loaded and unloaded, if the
     * module doesn't have persistent in-kernel storage across loads and unloads.
     *
     * The cache must be empty before calling this function.
     *
     * The caller must guarantee that noone will allocate memory from the cache
     * during the kmem_cache_destroy().
     */
    int kmem_cache_destroy(kmem_cache_t * cachep)
    {
    	int i;
    
    	if (!cachep || in_interrupt())
    		BUG();
    
    	/* Don't let CPUs to come and go */
    	lock_cpu_hotplug();
    
    	/* Find the cache in the chain of caches. */
    	down(&cache_chain_sem);
    	/*
    	 * the chain is never empty, cache_cache is never destroyed
    	 */
    	list_del(&cachep->next);
    	up(&cache_chain_sem);
    
    	if (__cache_shrink(cachep)) {
    		slab_error(cachep, "Can't free all objects");
    		down(&cache_chain_sem);
    		list_add(&cachep->next,&cache_chain);
    		up(&cache_chain_sem);
    		unlock_cpu_hotplug();
    		return 1;
    	}
    
    	if (unlikely(cachep->flags & SLAB_DESTROY_BY_RCU))
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000
    
    	/* no cpu_online check required here since we clear the percpu
    	 * array on cpu offline and set this to NULL.
    	 */
    	for (i = 0; i < NR_CPUS; i++)
    		kfree(cachep->array[i]);
    
    	/* NUMA: free the list3 structures */
    	kfree(cachep->lists.shared);
    	cachep->lists.shared = NULL;
    	kmem_cache_free(&cache_cache, cachep);
    
    	unlock_cpu_hotplug();
    
    	return 0;
    }
    EXPORT_SYMBOL(kmem_cache_destroy);
    
    /* Get the memory for a slab management obj. */
    static struct slab* alloc_slabmgmt(kmem_cache_t *cachep,
    			void *objp, int colour_off, unsigned int __nocast local_flags)
    {
    	struct slab *slabp;
    	
    	if (OFF_SLAB(cachep)) {
    		/* Slab management obj is off-slab. */
    		slabp = kmem_cache_alloc(cachep->slabp_cache, local_flags);
    		if (!slabp)
    			return NULL;
    	} else {
    		slabp = objp+colour_off;
    		colour_off += cachep->slab_size;
    	}
    	slabp->inuse = 0;
    	slabp->colouroff = colour_off;
    	slabp->s_mem = objp+colour_off;
    
    	return slabp;
    }
    
    static inline kmem_bufctl_t *slab_bufctl(struct slab *slabp)
    {
    	return (kmem_bufctl_t *)(slabp+1);
    }
    
    static void cache_init_objs(kmem_cache_t *cachep,
    			struct slab *slabp, unsigned long ctor_flags)
    {
    	int i;
    
    	for (i = 0; i < cachep->num; i++) {
    		void* objp = slabp->s_mem+cachep->objsize*i;
    #if DEBUG
    		/* need to poison the objs? */
    		if (cachep->flags & SLAB_POISON)
    			poison_obj(cachep, objp, POISON_FREE);
    		if (cachep->flags & SLAB_STORE_USER)
    			*dbg_userword(cachep, objp) = NULL;
    
    		if (cachep->flags & SLAB_RED_ZONE) {
    			*dbg_redzone1(cachep, objp) = RED_INACTIVE;
    			*dbg_redzone2(cachep, objp) = RED_INACTIVE;
    		}
    		/*
    		 * Constructors are not allowed to allocate memory from
    		 * the same cache which they are a constructor for.
    		 * Otherwise, deadlock. They must also be threaded.
    		 */
    		if (cachep->ctor && !(cachep->flags & SLAB_POISON))
    			cachep->ctor(objp+obj_dbghead(cachep), cachep, ctor_flags);
    
    		if (cachep->flags & SLAB_RED_ZONE) {
    			if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)
    				slab_error(cachep, "constructor overwrote the"
    							" end of an object");
    			if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
    				slab_error(cachep, "constructor overwrote the"
    							" start of an object");
    		}
    		if ((cachep->objsize % PAGE_SIZE) == 0 && OFF_SLAB(cachep) && cachep->flags & SLAB_POISON)
    	       		kernel_map_pages(virt_to_page(objp), cachep->objsize/PAGE_SIZE, 0);
    #else
    		if (cachep->ctor)
    			cachep->ctor(objp, cachep, ctor_flags);
    #endif
    		slab_bufctl(slabp)[i] = i+1;
    	}
    	slab_bufctl(slabp)[i-1] = BUFCTL_END;
    	slabp->free = 0;
    }
    
    static void kmem_flagcheck(kmem_cache_t *cachep, unsigned int flags)
    {
    	if (flags & SLAB_DMA) {
    		if (!(cachep->gfpflags & GFP_DMA))
    			BUG();
    	} else {
    		if (cachep->gfpflags & GFP_DMA)
    			BUG();
    	}
    }
    
    static void set_slab_attr(kmem_cache_t *cachep, struct slab *slabp, void *objp)
    {
    	int i;
    	struct page *page;
    
    	/* Nasty!!!!!! I hope this is OK. */
    	i = 1 << cachep->gfporder;
    	page = virt_to_page(objp);
    	do {
    		SET_PAGE_CACHE(page, cachep);
    		SET_PAGE_SLAB(page, slabp);
    		page++;
    	} while (--i);
    }
    
    /*
     * Grow (by 1) the number of slabs within a cache.  This is called by
     * kmem_cache_alloc() when there are no active objs left in a cache.
     */
    static int cache_grow(kmem_cache_t *cachep, unsigned int __nocast flags, int nodeid)
    {
    	struct slab	*slabp;
    	void		*objp;
    	size_t		 offset;
    	unsigned int	 local_flags;
    	unsigned long	 ctor_flags;
    
    	/* Be lazy and only check for valid flags here,
     	 * keeping it out of the critical path in kmem_cache_alloc().
    	 */
    	if (flags & ~(SLAB_DMA|SLAB_LEVEL_MASK|SLAB_NO_GROW))
    		BUG();
    	if (flags & SLAB_NO_GROW)
    		return 0;
    
    	ctor_flags = SLAB_CTOR_CONSTRUCTOR;
    	local_flags = (flags & SLAB_LEVEL_MASK);
    	if (!(local_flags & __GFP_WAIT))
    		/*
    		 * Not allowed to sleep.  Need to tell a constructor about
    		 * this - it might need to know...
    		 */
    		ctor_flags |= SLAB_CTOR_ATOMIC;
    
    	/* About to mess with non-constant members - lock. */
    	check_irq_off();
    	spin_lock(&cachep->spinlock);
    
    	/* Get colour for the slab, and cal the next value. */
    	offset = cachep->colour_next;
    	cachep->colour_next++;
    	if (cachep->colour_next >= cachep->colour)
    		cachep->colour_next = 0;
    	offset *= cachep->colour_off;
    
    	spin_unlock(&cachep->spinlock);
    
    	if (local_flags & __GFP_WAIT)
    		local_irq_enable();
    
    	/*
    	 * The test for missing atomic flag is performed here, rather than
    	 * the more obvious place, simply to reduce the critical path length
    	 * in kmem_cache_alloc(). If a caller is seriously mis-behaving they
    	 * will eventually be caught here (where it matters).
    	 */
    	kmem_flagcheck(cachep, flags);
    
    
    	/* Get mem for the objs. */
    	if (!(objp = kmem_getpages(cachep, flags, nodeid)))
    		goto failed;
    
    	/* Get slab management. */
    	if (!(slabp = alloc_slabmgmt(cachep, objp, offset, local_flags)))
    		goto opps1;
    
    	set_slab_attr(cachep, slabp, objp);
    
    	cache_init_objs(cachep, slabp, ctor_flags);
    
    	if (local_flags & __GFP_WAIT)
    		local_irq_disable();
    	check_irq_off();
    	spin_lock(&cachep->spinlock);
    
    	/* Make slab active. */
    	list_add_tail(&slabp->list, &(list3_data(cachep)->slabs_free));
    	STATS_INC_GROWN(cachep);
    	list3_data(cachep)->free_objects += cachep->num;
    	spin_unlock(&cachep->spinlock);
    	return 1;
    opps1:
    	kmem_freepages(cachep, objp);
    failed:
    	if (local_flags & __GFP_WAIT)
    		local_irq_disable();
    	return 0;
    }
    
    #if DEBUG
    
    /*
     * Perform extra freeing checks:
     * - detect bad pointers.
     * - POISON/RED_ZONE checking
     * - destructor calls, for caches with POISON+dtor
     */
    static void kfree_debugcheck(const void *objp)
    {
    	struct page *page;
    
    	if (!virt_addr_valid(objp)) {
    		printk(KERN_ERR "kfree_debugcheck: out of range ptr %lxh.\n",
    			(unsigned long)objp);	
    		BUG();	
    	}
    	page = virt_to_page(objp);
    	if (!PageSlab(page)) {
    		printk(KERN_ERR "kfree_debugcheck: bad ptr %lxh.\n", (unsigned long)objp);
    		BUG();
    	}
    }
    
    static void *cache_free_debugcheck(kmem_cache_t *cachep, void *objp,
    					void *caller)
    {
    	struct page *page;
    	unsigned int objnr;
    	struct slab *slabp;
    
    	objp -= obj_dbghead(cachep);
    	kfree_debugcheck(objp);
    	page = virt_to_page(objp);
    
    	if (GET_PAGE_CACHE(page) != cachep) {
    		printk(KERN_ERR "mismatch in kmem_cache_free: expected cache %p, got %p\n",
    				GET_PAGE_CACHE(page),cachep);
    		printk(KERN_ERR "%p is %s.\n", cachep, cachep->name);
    		printk(KERN_ERR "%p is %s.\n", GET_PAGE_CACHE(page), GET_PAGE_CACHE(page)->name);
    		WARN_ON(1);
    	}
    	slabp = GET_PAGE_SLAB(page);
    
    	if (cachep->flags & SLAB_RED_ZONE) {
    		if (*dbg_redzone1(cachep, objp) != RED_ACTIVE || *dbg_redzone2(cachep, objp) != RED_ACTIVE) {
    			slab_error(cachep, "double free, or memory outside"
    						" object was overwritten");
    			printk(KERN_ERR "%p: redzone 1: 0x%lx, redzone 2: 0x%lx.\n",
    					objp, *dbg_redzone1(cachep, objp), *dbg_redzone2(cachep, objp));
    		}
    		*dbg_redzone1(cachep, objp) = RED_INACTIVE;
    		*dbg_redzone2(cachep, objp) = RED_INACTIVE;
    	}
    	if (cachep->flags & SLAB_STORE_USER)
    		*dbg_userword(cachep, objp) = caller;
    
    	objnr = (objp-slabp->s_mem)/cachep->objsize;
    
    	BUG_ON(objnr >= cachep->num);
    	BUG_ON(objp != slabp->s_mem + objnr*cachep->objsize);
    
    	if (cachep->flags & SLAB_DEBUG_INITIAL) {
    		/* Need to call the slab's constructor so the
    		 * caller can perform a verify of its state (debugging).
    		 * Called without the cache-lock held.
    		 */
    		cachep->ctor(objp+obj_dbghead(cachep),
    					cachep, SLAB_CTOR_CONSTRUCTOR|SLAB_CTOR_VERIFY);
    	}
    	if (cachep->flags & SLAB_POISON && cachep->dtor) {
    		/* we want to cache poison the object,
    		 * call the destruction callback
    		 */
    		cachep->dtor(objp+obj_dbghead(cachep), cachep, 0);
    	}
    	if (cachep->flags & SLAB_POISON) {
    #ifdef CONFIG_DEBUG_PAGEALLOC
    		if ((cachep->objsize % PAGE_SIZE) == 0 && OFF_SLAB(cachep)) {
    			store_stackinfo(cachep, objp, (unsigned long)caller);
    	       		kernel_map_pages(virt_to_page(objp), cachep->objsize/PAGE_SIZE, 0);
    		} else {
    			poison_obj(cachep, objp, POISON_FREE);
    		}
    #else
    		poison_obj(cachep, objp, POISON_FREE);
    #endif
    	}
    	return objp;
    }
    
    static void check_slabp(kmem_cache_t *cachep, struct slab *slabp)
    {
    	kmem_bufctl_t i;
    	int entries = 0;
    	
    	check_spinlock_acquired(cachep);
    	/* Check slab's freelist to see if this obj is there. */
    	for (i = slabp->free; i != BUFCTL_END; i = slab_bufctl(slabp)[i]) {
    		entries++;
    		if (entries > cachep->num || i >= cachep->num)
    			goto bad;
    	}
    	if (entries != cachep->num - slabp->inuse) {
    bad:
    		printk(KERN_ERR "slab: Internal list corruption detected in cache '%s'(%d), slabp %p(%d). Hexdump:\n",
    				cachep->name, cachep->num, slabp, slabp->inuse);
    		for (i=0;i<sizeof(slabp)+cachep->num*sizeof(kmem_bufctl_t);i++) {
    			if ((i%16)==0)
    				printk("\n%03x:", i);
    			printk(" %02x", ((unsigned char*)slabp)[i]);
    		}
    		printk("\n");
    		BUG();
    	}
    }
    #else
    #define kfree_debugcheck(x) do { } while(0)
    #define cache_free_debugcheck(x,objp,z) (objp)
    #define check_slabp(x,y) do { } while(0)
    #endif
    
    static void *cache_alloc_refill(kmem_cache_t *cachep, unsigned int __nocast flags)
    {
    	int batchcount;
    	struct kmem_list3 *l3;