Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 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 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 | /* * Copyright (c) 2006-2020 Apple Inc. All rights reserved. * * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. The rights granted to you under the License * may not be used to create, or enable the creation or redistribution of, * unlawful or unlicensed copies of an Apple operating system, or to * circumvent, violate, or enable the circumvention or violation of, any * terms of an Apple operating system software license agreement. * * Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ */ /* * Memory allocator with per-CPU caching, derived from the kmem magazine * concept and implementation as described in the following paper: * http://www.usenix.org/events/usenix01/full_papers/bonwick/bonwick.pdf * That implementation is Copyright 2006 Sun Microsystems, Inc. All rights * reserved. Use is subject to license terms. * * There are several major differences between this and the original kmem * magazine: this derivative implementation allows for multiple objects to * be allocated and freed from/to the object cache in one call; in addition, * it provides for better flexibility where the user is allowed to define * its own slab allocator (instead of the default zone allocator). Finally, * no object construction/destruction takes place at the moment, although * this could be added in future to improve efficiency. */ #include <sys/param.h> #include <sys/types.h> #include <sys/malloc.h> #include <sys/mbuf.h> #include <sys/queue.h> #include <sys/kernel.h> #include <sys/systm.h> #include <kern/debug.h> #include <kern/zalloc.h> #include <kern/cpu_number.h> #include <kern/locks.h> #include <kern/thread_call.h> #include <libkern/libkern.h> #include <libkern/OSAtomic.h> #include <libkern/OSDebug.h> #include <mach/vm_param.h> #include <machine/limits.h> #include <machine/machine_routines.h> #include <string.h> #include <sys/mcache.h> #define MCACHE_SIZE(n) \ __builtin_offsetof(mcache_t, mc_cpu[n]) /* Allocate extra in case we need to manually align the pointer */ #define MCACHE_ALLOC_SIZE \ (sizeof (void *) + MCACHE_SIZE(ncpu) + CPU_CACHE_LINE_SIZE) #define MCACHE_CPU(c) \ (mcache_cpu_t *)((void *)((char *)(c) + MCACHE_SIZE(cpu_number()))) /* * MCACHE_LIST_LOCK() and MCACHE_LIST_UNLOCK() are macros used * to serialize accesses to the global list of caches in the system. * They also record the thread currently running in the critical * section, so that we can avoid recursive requests to reap the * caches when memory runs low. */ #define MCACHE_LIST_LOCK() { \ lck_mtx_lock(mcache_llock); \ mcache_llock_owner = current_thread(); \ } #define MCACHE_LIST_UNLOCK() { \ mcache_llock_owner = NULL; \ lck_mtx_unlock(mcache_llock); \ } #define MCACHE_LOCK(l) lck_mtx_lock(l) #define MCACHE_UNLOCK(l) lck_mtx_unlock(l) #define MCACHE_LOCK_TRY(l) lck_mtx_try_lock(l) static unsigned int ncpu; static unsigned int cache_line_size; static lck_mtx_t *mcache_llock; static struct thread *mcache_llock_owner; static lck_attr_t *mcache_llock_attr; static lck_grp_t *mcache_llock_grp; static lck_grp_attr_t *mcache_llock_grp_attr; static struct zone *mcache_zone; static const uint32_t mcache_reap_interval = 15; static const uint32_t mcache_reap_interval_leeway = 2; static UInt32 mcache_reaping; static int mcache_ready; static int mcache_updating; static int mcache_bkt_contention = 3; #if DEBUG static unsigned int mcache_flags = MCF_DEBUG; #else static unsigned int mcache_flags = 0; #endif int mca_trn_max = MCA_TRN_MAX; #define DUMP_MCA_BUF_SIZE 512 static char *mca_dump_buf; static mcache_bkttype_t mcache_bkttype[] = { { 1, 4096, 32768, NULL }, { 3, 2048, 16384, NULL }, { 7, 1024, 12288, NULL }, { 15, 256, 8192, NULL }, { 31, 64, 4096, NULL }, { 47, 0, 2048, NULL }, { 63, 0, 1024, NULL }, { 95, 0, 512, NULL }, { 143, 0, 256, NULL }, { 165, 0, 0, NULL }, }; static mcache_t *mcache_create_common(const char *, size_t, size_t, mcache_allocfn_t, mcache_freefn_t, mcache_auditfn_t, mcache_logfn_t, mcache_notifyfn_t, void *, u_int32_t, int, int); static unsigned int mcache_slab_alloc(void *, mcache_obj_t ***, unsigned int, int); static void mcache_slab_free(void *, mcache_obj_t *, boolean_t); static void mcache_slab_audit(void *, mcache_obj_t *, boolean_t); static void mcache_cpu_refill(mcache_cpu_t *, mcache_bkt_t *, int); static mcache_bkt_t *mcache_bkt_alloc(mcache_t *, mcache_bktlist_t *); static void mcache_bkt_free(mcache_t *, mcache_bktlist_t *, mcache_bkt_t *); static void mcache_cache_bkt_enable(mcache_t *); static void mcache_bkt_purge(mcache_t *); static void mcache_bkt_destroy(mcache_t *, mcache_bkt_t *, int); static void mcache_bkt_ws_update(mcache_t *); static void mcache_bkt_ws_zero(mcache_t *); static void mcache_bkt_ws_reap(mcache_t *); static void mcache_dispatch(void (*)(void *), void *); static void mcache_cache_reap(mcache_t *); static void mcache_cache_update(mcache_t *); static void mcache_cache_bkt_resize(void *); static void mcache_cache_enable(void *); static void mcache_update(thread_call_param_t __unused, thread_call_param_t __unused); static void mcache_update_timeout(void *); static void mcache_applyall(void (*)(mcache_t *)); static void mcache_reap_start(void *); static void mcache_reap_done(void *); static void mcache_reap_timeout(thread_call_param_t __unused, thread_call_param_t); static void mcache_notify(mcache_t *, u_int32_t); static void mcache_purge(void *); static LIST_HEAD(, mcache) mcache_head; mcache_t *mcache_audit_cache; static thread_call_t mcache_reap_tcall; static thread_call_t mcache_update_tcall; /* * Initialize the framework; this is currently called as part of BSD init. */ __private_extern__ void mcache_init(void) { mcache_bkttype_t *btp; unsigned int i; char name[32]; VERIFY(mca_trn_max >= 2); ncpu = ml_wait_max_cpus(); (void) mcache_cache_line_size(); /* prime it */ mcache_llock_grp_attr = lck_grp_attr_alloc_init(); mcache_llock_grp = lck_grp_alloc_init("mcache.list", mcache_llock_grp_attr); mcache_llock_attr = lck_attr_alloc_init(); mcache_llock = lck_mtx_alloc_init(mcache_llock_grp, mcache_llock_attr); mcache_reap_tcall = thread_call_allocate(mcache_reap_timeout, NULL); mcache_update_tcall = thread_call_allocate(mcache_update, NULL); if (mcache_reap_tcall == NULL || mcache_update_tcall == NULL) { panic("mcache_init: thread_call_allocate failed"); /* NOTREACHED */ __builtin_unreachable(); } mcache_zone = zone_create("mcache", MCACHE_ALLOC_SIZE, ZC_DESTRUCTIBLE); LIST_INIT(&mcache_head); for (i = 0; i < sizeof(mcache_bkttype) / sizeof(*btp); i++) { btp = &mcache_bkttype[i]; (void) snprintf(name, sizeof(name), "bkt_%d", btp->bt_bktsize); btp->bt_cache = mcache_create(name, (btp->bt_bktsize + 1) * sizeof(void *), 0, 0, MCR_SLEEP); } PE_parse_boot_argn("mcache_flags", &mcache_flags, sizeof(mcache_flags)); mcache_flags &= MCF_FLAGS_MASK; mcache_audit_cache = mcache_create("audit", sizeof(mcache_audit_t), 0, 0, MCR_SLEEP); mcache_applyall(mcache_cache_bkt_enable); mcache_ready = 1; printf("mcache: %d CPU(s), %d bytes CPU cache line size\n", ncpu, CPU_CACHE_LINE_SIZE); } /* * Return the global mcache flags. */ __private_extern__ unsigned int mcache_getflags(void) { return mcache_flags; } /* * Return the CPU cache line size. */ __private_extern__ unsigned int mcache_cache_line_size(void) { if (cache_line_size == 0) { ml_cpu_info_t cpu_info; ml_cpu_get_info(&cpu_info); cache_line_size = (unsigned int)cpu_info.cache_line_size; } return cache_line_size; } /* * Create a cache using the zone allocator as the backend slab allocator. * The caller may specify any alignment for the object; if it specifies 0 * the default alignment (MCACHE_ALIGN) will be used. */ __private_extern__ mcache_t * mcache_create(const char *name, size_t bufsize, size_t align, u_int32_t flags, int wait) { return mcache_create_common(name, bufsize, align, mcache_slab_alloc, mcache_slab_free, mcache_slab_audit, NULL, NULL, NULL, flags, 1, wait); } /* * Create a cache using a custom backend slab allocator. Since the caller * is responsible for allocation, no alignment guarantee will be provided * by this framework. */ __private_extern__ mcache_t * mcache_create_ext(const char *name, size_t bufsize, mcache_allocfn_t allocfn, mcache_freefn_t freefn, mcache_auditfn_t auditfn, mcache_logfn_t logfn, mcache_notifyfn_t notifyfn, void *arg, u_int32_t flags, int wait) { return mcache_create_common(name, bufsize, 0, allocfn, freefn, auditfn, logfn, notifyfn, arg, flags, 0, wait); } /* * Common cache creation routine. */ static mcache_t * mcache_create_common(const char *name, size_t bufsize, size_t align, mcache_allocfn_t allocfn, mcache_freefn_t freefn, mcache_auditfn_t auditfn, mcache_logfn_t logfn, mcache_notifyfn_t notifyfn, void *arg, u_int32_t flags, int need_zone, int wait) { mcache_bkttype_t *btp; mcache_t *cp = NULL; size_t chunksize; void *buf, **pbuf; unsigned int c; char lck_name[64]; /* If auditing is on and print buffer is NULL, allocate it now */ if ((flags & MCF_DEBUG) && mca_dump_buf == NULL) { int malloc_wait = (wait & MCR_NOSLEEP) ? M_NOWAIT : M_WAITOK; MALLOC(mca_dump_buf, char *, DUMP_MCA_BUF_SIZE, M_TEMP, malloc_wait | M_ZERO); if (mca_dump_buf == NULL) { return NULL; } } buf = zalloc(mcache_zone); if (buf == NULL) { goto fail; } bzero(buf, MCACHE_ALLOC_SIZE); /* * In case we didn't get a cache-aligned memory, round it up * accordingly. This is needed in order to get the rest of * structure members aligned properly. It also means that * the memory span gets shifted due to the round up, but it * is okay since we've allocated extra space for this. */ cp = (mcache_t *) P2ROUNDUP((intptr_t)buf + sizeof(void *), CPU_CACHE_LINE_SIZE); pbuf = (void **)((intptr_t)cp - sizeof(void *)); *pbuf = buf; /* * Guaranteed alignment is valid only when we use the internal * slab allocator (currently set to use the zone allocator). */ if (!need_zone) { align = 1; } else { /* Enforce 64-bit minimum alignment for zone-based buffers */ if (align == 0) { align = MCACHE_ALIGN; } align = P2ROUNDUP(align, MCACHE_ALIGN); } if ((align & (align - 1)) != 0) { panic("mcache_create: bad alignment %lu", align); /* NOTREACHED */ __builtin_unreachable(); } cp->mc_align = align; cp->mc_slab_alloc = allocfn; cp->mc_slab_free = freefn; cp->mc_slab_audit = auditfn; cp->mc_slab_log = logfn; cp->mc_slab_notify = notifyfn; cp->mc_private = need_zone ? cp : arg; cp->mc_bufsize = bufsize; cp->mc_flags = (flags & MCF_FLAGS_MASK) | mcache_flags; (void) snprintf(cp->mc_name, sizeof(cp->mc_name), "mcache.%s", name); (void) snprintf(lck_name, sizeof(lck_name), "%s.cpu", cp->mc_name); cp->mc_cpu_lock_grp_attr = lck_grp_attr_alloc_init(); cp->mc_cpu_lock_grp = lck_grp_alloc_init(lck_name, cp->mc_cpu_lock_grp_attr); cp->mc_cpu_lock_attr = lck_attr_alloc_init(); /* * Allocation chunk size is the object's size plus any extra size * needed to satisfy the object's alignment. It is enforced to be * at least the size of an LP64 pointer to simplify auditing and to * handle multiple-element allocation requests, where the elements * returned are linked together in a list. */ chunksize = MAX(bufsize, sizeof(u_int64_t)); if (need_zone) { VERIFY(align != 0 && (align % MCACHE_ALIGN) == 0); chunksize += sizeof(uint64_t) + align; chunksize = P2ROUNDUP(chunksize, align); cp->mc_slab_zone = zone_create(cp->mc_name, chunksize, ZC_DESTRUCTIBLE); } cp->mc_chunksize = chunksize; /* * Initialize the bucket layer. */ (void) snprintf(lck_name, sizeof(lck_name), "%s.bkt", cp->mc_name); cp->mc_bkt_lock_grp_attr = lck_grp_attr_alloc_init(); cp->mc_bkt_lock_grp = lck_grp_alloc_init(lck_name, cp->mc_bkt_lock_grp_attr); cp->mc_bkt_lock_attr = lck_attr_alloc_init(); lck_mtx_init(&cp->mc_bkt_lock, cp->mc_bkt_lock_grp, cp->mc_bkt_lock_attr); (void) snprintf(lck_name, sizeof(lck_name), "%s.sync", cp->mc_name); cp->mc_sync_lock_grp_attr = lck_grp_attr_alloc_init(); cp->mc_sync_lock_grp = lck_grp_alloc_init(lck_name, cp->mc_sync_lock_grp_attr); cp->mc_sync_lock_attr = lck_attr_alloc_init(); lck_mtx_init(&cp->mc_sync_lock, cp->mc_sync_lock_grp, cp->mc_sync_lock_attr); for (btp = mcache_bkttype; chunksize <= btp->bt_minbuf; btp++) { continue; } cp->cache_bkttype = btp; /* * Initialize the CPU layer. Each per-CPU structure is aligned * on the CPU cache line boundary to prevent false sharing. */ for (c = 0; c < ncpu; c++) { mcache_cpu_t *ccp = &cp->mc_cpu[c]; VERIFY(IS_P2ALIGNED(ccp, CPU_CACHE_LINE_SIZE)); lck_mtx_init(&ccp->cc_lock, cp->mc_cpu_lock_grp, cp->mc_cpu_lock_attr); ccp->cc_objs = -1; ccp->cc_pobjs = -1; } if (mcache_ready) { mcache_cache_bkt_enable(cp); } /* TODO: dynamically create sysctl for stats */ MCACHE_LIST_LOCK(); LIST_INSERT_HEAD(&mcache_head, cp, mc_list); MCACHE_LIST_UNLOCK(); /* * If cache buckets are enabled and this is the first cache * created, start the periodic cache update. */ if (!(mcache_flags & MCF_NOCPUCACHE) && !mcache_updating) { mcache_updating = 1; mcache_update_timeout(NULL); } if (cp->mc_flags & MCF_DEBUG) { printf("mcache_create: %s (%s) arg %p bufsize %lu align %lu " "chunksize %lu bktsize %d\n", name, need_zone ? "i" : "e", arg, bufsize, cp->mc_align, chunksize, btp->bt_bktsize); } return cp; fail: if (buf != NULL) { zfree(mcache_zone, buf); } return NULL; } /* * Allocate one or more objects from a cache. */ __private_extern__ unsigned int mcache_alloc_ext(mcache_t *cp, mcache_obj_t **list, unsigned int num, int wait) { mcache_cpu_t *ccp; mcache_obj_t **top = &(*list); mcache_bkt_t *bkt; unsigned int need = num; boolean_t nwretry = FALSE; /* MCR_NOSLEEP and MCR_FAILOK are mutually exclusive */ VERIFY((wait & (MCR_NOSLEEP | MCR_FAILOK)) != (MCR_NOSLEEP | MCR_FAILOK)); ASSERT(list != NULL); *list = NULL; if (num == 0) { return 0; } retry_alloc: /* We may not always be running in the same CPU in case of retries */ ccp = MCACHE_CPU(cp); MCACHE_LOCK(&ccp->cc_lock); for (;;) { /* * If we have an object in the current CPU's filled bucket, * chain the object to any previous objects and return if * we've satisfied the number of requested objects. */ if (ccp->cc_objs > 0) { mcache_obj_t *tail; int objs; /* * Objects in the bucket are already linked together * with the most recently freed object at the head of * the list; grab as many objects as we can. */ objs = MIN((unsigned int)ccp->cc_objs, need); *list = ccp->cc_filled->bkt_obj[ccp->cc_objs - 1]; ccp->cc_objs -= objs; ccp->cc_alloc += objs; tail = ccp->cc_filled->bkt_obj[ccp->cc_objs]; list = &tail->obj_next; *list = NULL; /* If we got them all, return to caller */ if ((need -= objs) == 0) { MCACHE_UNLOCK(&ccp->cc_lock); if (!(cp->mc_flags & MCF_NOLEAKLOG) && cp->mc_slab_log != NULL) { (*cp->mc_slab_log)(num, *top, TRUE); } if (cp->mc_flags & MCF_DEBUG) { goto debug_alloc; } return num; } } /* * The CPU's filled bucket is empty. If the previous filled * bucket was full, exchange and try again. */ if (ccp->cc_pobjs > 0) { mcache_cpu_refill(ccp, ccp->cc_pfilled, ccp->cc_pobjs); continue; } /* * If the bucket layer is disabled, allocate from slab. This * can happen either because MCF_NOCPUCACHE is set, or because * the bucket layer is currently being resized. */ if (ccp->cc_bktsize == 0) { break; } /* * Both of the CPU's buckets are empty; try to get a full * bucket from the bucket layer. Upon success, refill this * CPU and place any empty bucket into the empty list. */ bkt = mcache_bkt_alloc(cp, &cp->mc_full); if (bkt != NULL) { if (ccp->cc_pfilled != NULL) { mcache_bkt_free(cp, &cp->mc_empty, ccp->cc_pfilled); } mcache_cpu_refill(ccp, bkt, ccp->cc_bktsize); continue; } /* * The bucket layer has no full buckets; allocate the * object(s) directly from the slab layer. */ break; } MCACHE_UNLOCK(&ccp->cc_lock); need -= (*cp->mc_slab_alloc)(cp->mc_private, &list, need, wait); /* * If this is a blocking allocation, or if it is non-blocking and * the cache's full bucket is non-empty, then retry the allocation. */ if (need > 0) { if (!(wait & MCR_NONBLOCKING)) { atomic_add_32(&cp->mc_wretry_cnt, 1); goto retry_alloc; } else if ((wait & (MCR_NOSLEEP | MCR_TRYHARD)) && !mcache_bkt_isempty(cp)) { if (!nwretry) { nwretry = TRUE; } atomic_add_32(&cp->mc_nwretry_cnt, 1); goto retry_alloc; } else if (nwretry) { atomic_add_32(&cp->mc_nwfail_cnt, 1); } } if (!(cp->mc_flags & MCF_NOLEAKLOG) && cp->mc_slab_log != NULL) { (*cp->mc_slab_log)((num - need), *top, TRUE); } if (!(cp->mc_flags & MCF_DEBUG)) { return num - need; } debug_alloc: if (cp->mc_flags & MCF_DEBUG) { mcache_obj_t **o = top; unsigned int n; n = 0; /* * Verify that the chain of objects have the same count as * what we are about to report to the caller. Any mismatch * here means that the object list is insanely broken and * therefore we must panic. */ while (*o != NULL) { o = &(*o)->obj_next; ++n; } if (n != (num - need)) { panic("mcache_alloc_ext: %s cp %p corrupted list " "(got %d actual %d)\n", cp->mc_name, (void *)cp, num - need, n); /* NOTREACHED */ __builtin_unreachable(); } } /* Invoke the slab layer audit callback if auditing is enabled */ if ((cp->mc_flags & MCF_DEBUG) && cp->mc_slab_audit != NULL) { (*cp->mc_slab_audit)(cp->mc_private, *top, TRUE); } return num - need; } /* * Allocate a single object from a cache. */ __private_extern__ void * mcache_alloc(mcache_t *cp, int wait) { mcache_obj_t *buf; (void) mcache_alloc_ext(cp, &buf, 1, wait); return buf; } __private_extern__ void mcache_waiter_inc(mcache_t *cp) { atomic_add_32(&cp->mc_waiter_cnt, 1); } __private_extern__ void mcache_waiter_dec(mcache_t *cp) { atomic_add_32(&cp->mc_waiter_cnt, -1); } __private_extern__ boolean_t mcache_bkt_isempty(mcache_t *cp) { /* * This isn't meant to accurately tell whether there are * any full buckets in the cache; it is simply a way to * obtain "hints" about the state of the cache. */ return cp->mc_full.bl_total == 0; } /* * Notify the slab layer about an event. */ static void mcache_notify(mcache_t *cp, u_int32_t event) { if (cp->mc_slab_notify != NULL) { (*cp->mc_slab_notify)(cp->mc_private, event); } } /* * Purge the cache and disable its buckets. */ static void mcache_purge(void *arg) { mcache_t *cp = arg; mcache_bkt_purge(cp); /* * We cannot simply call mcache_cache_bkt_enable() from here as * a bucket resize may be in flight and we would cause the CPU * layers of the cache to point to different sizes. Therefore, * we simply increment the enable count so that during the next * periodic cache update the buckets can be reenabled. */ lck_mtx_lock_spin(&cp->mc_sync_lock); cp->mc_enable_cnt++; lck_mtx_unlock(&cp->mc_sync_lock); } __private_extern__ boolean_t mcache_purge_cache(mcache_t *cp, boolean_t async) { /* * Purging a cache that has no per-CPU caches or is already * in the process of being purged is rather pointless. */ if (cp->mc_flags & MCF_NOCPUCACHE) { return FALSE; } lck_mtx_lock_spin(&cp->mc_sync_lock); if (cp->mc_purge_cnt > 0) { lck_mtx_unlock(&cp->mc_sync_lock); return FALSE; } cp->mc_purge_cnt++; lck_mtx_unlock(&cp->mc_sync_lock); if (async) { mcache_dispatch(mcache_purge, cp); } else { mcache_purge(cp); } return TRUE; } /* * Free a single object to a cache. */ __private_extern__ void mcache_free(mcache_t *cp, void *buf) { ((mcache_obj_t *)buf)->obj_next = NULL; mcache_free_ext(cp, (mcache_obj_t *)buf); } /* * Free one or more objects to a cache. */ __private_extern__ void mcache_free_ext(mcache_t *cp, mcache_obj_t *list) { mcache_cpu_t *ccp = MCACHE_CPU(cp); mcache_bkttype_t *btp; mcache_obj_t *nlist; mcache_bkt_t *bkt; if (!(cp->mc_flags & MCF_NOLEAKLOG) && cp->mc_slab_log != NULL) { (*cp->mc_slab_log)(0, list, FALSE); } /* Invoke the slab layer audit callback if auditing is enabled */ if ((cp->mc_flags & MCF_DEBUG) && cp->mc_slab_audit != NULL) { (*cp->mc_slab_audit)(cp->mc_private, list, FALSE); } MCACHE_LOCK(&ccp->cc_lock); for (;;) { /* * If there is space in the current CPU's filled bucket, put * the object there and return once all objects are freed. * Note the cast to unsigned integer takes care of the case * where the bucket layer is disabled (when cc_objs is -1). */ if ((unsigned int)ccp->cc_objs < (unsigned int)ccp->cc_bktsize) { /* * Reverse the list while we place the object into the * bucket; this effectively causes the most recently * freed object(s) to be reused during allocation. */ nlist = list->obj_next; list->obj_next = (ccp->cc_objs == 0) ? NULL : ccp->cc_filled->bkt_obj[ccp->cc_objs - 1]; ccp->cc_filled->bkt_obj[ccp->cc_objs++] = list; ccp->cc_free++; if ((list = nlist) != NULL) { continue; } /* We are done; return to caller */ MCACHE_UNLOCK(&ccp->cc_lock); /* If there is a waiter below, notify it */ if (cp->mc_waiter_cnt > 0) { mcache_notify(cp, MCN_RETRYALLOC); } return; } /* * The CPU's filled bucket is full. If the previous filled * bucket was empty, exchange and try again. */ if (ccp->cc_pobjs == 0) { mcache_cpu_refill(ccp, ccp->cc_pfilled, ccp->cc_pobjs); continue; } /* * If the bucket layer is disabled, free to slab. This can * happen either because MCF_NOCPUCACHE is set, or because * the bucket layer is currently being resized. */ if (ccp->cc_bktsize == 0) { break; } /* * Both of the CPU's buckets are full; try to get an empty * bucket from the bucket layer. Upon success, empty this * CPU and place any full bucket into the full list. */ bkt = mcache_bkt_alloc(cp, &cp->mc_empty); if (bkt != NULL) { if (ccp->cc_pfilled != NULL) { mcache_bkt_free(cp, &cp->mc_full, ccp->cc_pfilled); } mcache_cpu_refill(ccp, bkt, 0); continue; } btp = cp->cache_bkttype; /* * We need an empty bucket to put our freed objects into * but couldn't get an empty bucket from the bucket layer; * attempt to allocate one. We do not want to block for * allocation here, and if the bucket allocation fails * we will simply fall through to the slab layer. */ MCACHE_UNLOCK(&ccp->cc_lock); bkt = mcache_alloc(btp->bt_cache, MCR_NOSLEEP); MCACHE_LOCK(&ccp->cc_lock); if (bkt != NULL) { /* * We have an empty bucket, but since we drop the * CPU lock above, the cache's bucket size may have * changed. If so, free the bucket and try again. */ if (ccp->cc_bktsize != btp->bt_bktsize) { MCACHE_UNLOCK(&ccp->cc_lock); mcache_free(btp->bt_cache, bkt); MCACHE_LOCK(&ccp->cc_lock); continue; } /* * Store it in the bucket object since we'll * need to refer to it during bucket destroy; * we can't safely refer to cache_bkttype as * the bucket lock may not be acquired then. */ bkt->bkt_type = btp; /* * We have an empty bucket of the right size; * add it to the bucket layer and try again. */ mcache_bkt_free(cp, &cp->mc_empty, bkt); continue; } /* * The bucket layer has no empty buckets; free the * object(s) directly to the slab layer. */ break; } MCACHE_UNLOCK(&ccp->cc_lock); /* If there is a waiter below, notify it */ if (cp->mc_waiter_cnt > 0) { mcache_notify(cp, MCN_RETRYALLOC); } /* Advise the slab layer to purge the object(s) */ (*cp->mc_slab_free)(cp->mc_private, list, (cp->mc_flags & MCF_DEBUG) || cp->mc_purge_cnt); } /* * Cache destruction routine. */ __private_extern__ void mcache_destroy(mcache_t *cp) { void **pbuf; MCACHE_LIST_LOCK(); LIST_REMOVE(cp, mc_list); MCACHE_LIST_UNLOCK(); mcache_bkt_purge(cp); /* * This cache is dead; there should be no further transaction. * If it's still invoked, make sure that it induces a fault. */ cp->mc_slab_alloc = NULL; cp->mc_slab_free = NULL; cp->mc_slab_audit = NULL; lck_attr_free(cp->mc_bkt_lock_attr); lck_grp_free(cp->mc_bkt_lock_grp); lck_grp_attr_free(cp->mc_bkt_lock_grp_attr); lck_attr_free(cp->mc_cpu_lock_attr); lck_grp_free(cp->mc_cpu_lock_grp); lck_grp_attr_free(cp->mc_cpu_lock_grp_attr); lck_attr_free(cp->mc_sync_lock_attr); lck_grp_free(cp->mc_sync_lock_grp); lck_grp_attr_free(cp->mc_sync_lock_grp_attr); /* * TODO: We need to destroy the zone here, but cannot do it * because there is no such way to achieve that. Until then * the memory allocated for the zone structure is leaked. * Once it is achievable, uncomment these lines: * * if (cp->mc_slab_zone != NULL) { * zdestroy(cp->mc_slab_zone); * cp->mc_slab_zone = NULL; * } */ /* Get the original address since we're about to free it */ pbuf = (void **)((intptr_t)cp - sizeof(void *)); zfree(mcache_zone, *pbuf); } /* * Internal slab allocator used as a backend for simple caches. The current * implementation uses the zone allocator for simplicity reasons. */ static unsigned int mcache_slab_alloc(void *arg, mcache_obj_t ***plist, unsigned int num, int wait) { #pragma unused(wait) mcache_t *cp = arg; unsigned int need = num; size_t rsize = P2ROUNDUP(cp->mc_bufsize, sizeof(u_int64_t)); u_int32_t flags = cp->mc_flags; void *buf, *base, **pbuf; mcache_obj_t **list = *plist; *list = NULL; for (;;) { buf = zalloc(cp->mc_slab_zone); if (buf == NULL) { break; } /* Get the aligned base address for this object */ base = (void *)P2ROUNDUP((intptr_t)buf + sizeof(u_int64_t), cp->mc_align); /* * Wind back a pointer size from the aligned base and * save the original address so we can free it later. */ pbuf = (void **)((intptr_t)base - sizeof(void *)); *pbuf = buf; VERIFY(((intptr_t)base + cp->mc_bufsize) <= ((intptr_t)buf + cp->mc_chunksize)); /* * If auditing is enabled, patternize the contents of * the buffer starting from the 64-bit aligned base to * the end of the buffer; the length is rounded up to * the nearest 64-bit multiply; this is because we use * 64-bit memory access to set/check the pattern. */ if (flags & MCF_DEBUG) { VERIFY(((intptr_t)base + rsize) <= ((intptr_t)buf + cp->mc_chunksize)); mcache_set_pattern(MCACHE_FREE_PATTERN, base, rsize); } VERIFY(IS_P2ALIGNED(base, cp->mc_align)); *list = (mcache_obj_t *)base; (*list)->obj_next = NULL; list = *plist = &(*list)->obj_next; /* If we got them all, return to mcache */ if (--need == 0) { break; } } return num - need; } /* * Internal slab deallocator used as a backend for simple caches. */ static void mcache_slab_free(void *arg, mcache_obj_t *list, __unused boolean_t purged) { mcache_t *cp = arg; mcache_obj_t *nlist; size_t rsize = P2ROUNDUP(cp->mc_bufsize, sizeof(u_int64_t)); u_int32_t flags = cp->mc_flags; void *base; void **pbuf; for (;;) { nlist = list->obj_next; list->obj_next = NULL; base = list; VERIFY(IS_P2ALIGNED(base, cp->mc_align)); /* Get the original address since we're about to free it */ pbuf = (void **)((intptr_t)base - sizeof(void *)); VERIFY(((intptr_t)base + cp->mc_bufsize) <= ((intptr_t)*pbuf + cp->mc_chunksize)); if (flags & MCF_DEBUG) { VERIFY(((intptr_t)base + rsize) <= ((intptr_t)*pbuf + cp->mc_chunksize)); mcache_audit_free_verify(NULL, base, 0, rsize); } /* Free it to zone */ zfree(cp->mc_slab_zone, *pbuf); /* No more objects to free; return to mcache */ if ((list = nlist) == NULL) { break; } } } /* * Internal slab auditor for simple caches. */ static void mcache_slab_audit(void *arg, mcache_obj_t *list, boolean_t alloc) { mcache_t *cp = arg; size_t rsize = P2ROUNDUP(cp->mc_bufsize, sizeof(u_int64_t)); void *base, **pbuf; while (list != NULL) { mcache_obj_t *next = list->obj_next; base = list; VERIFY(IS_P2ALIGNED(base, cp->mc_align)); /* Get the original address */ pbuf = (void **)((intptr_t)base - sizeof(void *)); VERIFY(((intptr_t)base + rsize) <= ((intptr_t)*pbuf + cp->mc_chunksize)); if (!alloc) { mcache_set_pattern(MCACHE_FREE_PATTERN, base, rsize); } else { mcache_audit_free_verify_set(NULL, base, 0, rsize); } list = list->obj_next = next; } } /* * Refill the CPU's filled bucket with bkt and save the previous one. */ static void mcache_cpu_refill(mcache_cpu_t *ccp, mcache_bkt_t *bkt, int objs) { ASSERT((ccp->cc_filled == NULL && ccp->cc_objs == -1) || (ccp->cc_filled && ccp->cc_objs + objs == ccp->cc_bktsize)); ASSERT(ccp->cc_bktsize > 0); ccp->cc_pfilled = ccp->cc_filled; ccp->cc_pobjs = ccp->cc_objs; ccp->cc_filled = bkt; ccp->cc_objs = objs; } /* * Allocate a bucket from the bucket layer. */ static mcache_bkt_t * mcache_bkt_alloc(mcache_t *cp, mcache_bktlist_t *blp) { mcache_bkt_t *bkt; if (!MCACHE_LOCK_TRY(&cp->mc_bkt_lock)) { /* * The bucket layer lock is held by another CPU; increase * the contention count so that we can later resize the * bucket size accordingly. */ MCACHE_LOCK(&cp->mc_bkt_lock); cp->mc_bkt_contention++; } if ((bkt = blp->bl_list) != NULL) { blp->bl_list = bkt->bkt_next; if (--blp->bl_total < blp->bl_min) { blp->bl_min = blp->bl_total; } blp->bl_alloc++; } MCACHE_UNLOCK(&cp->mc_bkt_lock); return bkt; } /* * Free a bucket to the bucket layer. */ static void mcache_bkt_free(mcache_t *cp, mcache_bktlist_t *blp, mcache_bkt_t *bkt) { MCACHE_LOCK(&cp->mc_bkt_lock); bkt->bkt_next = blp->bl_list; blp->bl_list = bkt; blp->bl_total++; MCACHE_UNLOCK(&cp->mc_bkt_lock); } /* * Enable the bucket layer of a cache. */ static void mcache_cache_bkt_enable(mcache_t *cp) { mcache_cpu_t *ccp; unsigned int cpu; if (cp->mc_flags & MCF_NOCPUCACHE) { return; } for (cpu = 0; cpu < ncpu; cpu++) { ccp = &cp->mc_cpu[cpu]; MCACHE_LOCK(&ccp->cc_lock); ccp->cc_bktsize = cp->cache_bkttype->bt_bktsize; MCACHE_UNLOCK(&ccp->cc_lock); } } /* * Purge all buckets from a cache and disable its bucket layer. */ static void mcache_bkt_purge(mcache_t *cp) { mcache_cpu_t *ccp; mcache_bkt_t *bp, *pbp; int objs, pobjs; unsigned int cpu; for (cpu = 0; cpu < ncpu; cpu++) { ccp = &cp->mc_cpu[cpu]; MCACHE_LOCK(&ccp->cc_lock); bp = ccp->cc_filled; pbp = ccp->cc_pfilled; objs = ccp->cc_objs; pobjs = ccp->cc_pobjs; ccp->cc_filled = NULL; ccp->cc_pfilled = NULL; ccp->cc_objs = -1; ccp->cc_pobjs = -1; ccp->cc_bktsize = 0; MCACHE_UNLOCK(&ccp->cc_lock); if (bp != NULL) { mcache_bkt_destroy(cp, bp, objs); } if (pbp != NULL) { mcache_bkt_destroy(cp, pbp, pobjs); } } mcache_bkt_ws_zero(cp); mcache_bkt_ws_reap(cp); } /* * Free one or more objects in the bucket to the slab layer, * and also free the bucket itself. */ static void mcache_bkt_destroy(mcache_t *cp, mcache_bkt_t *bkt, int nobjs) { if (nobjs > 0) { mcache_obj_t *top = bkt->bkt_obj[nobjs - 1]; if (cp->mc_flags & MCF_DEBUG) { mcache_obj_t *o = top; int cnt = 0; /* * Verify that the chain of objects in the bucket is * valid. Any mismatch here means a mistake when the * object(s) were freed to the CPU layer, so we panic. */ while (o != NULL) { o = o->obj_next; ++cnt; } if (cnt != nobjs) { panic("mcache_bkt_destroy: %s cp %p corrupted " "list in bkt %p (nobjs %d actual %d)\n", cp->mc_name, (void *)cp, (void *)bkt, nobjs, cnt); /* NOTREACHED */ __builtin_unreachable(); } } /* Advise the slab layer to purge the object(s) */ (*cp->mc_slab_free)(cp->mc_private, top, (cp->mc_flags & MCF_DEBUG) || cp->mc_purge_cnt); } mcache_free(bkt->bkt_type->bt_cache, bkt); } /* * Update the bucket layer working set statistics. */ static void mcache_bkt_ws_update(mcache_t *cp) { MCACHE_LOCK(&cp->mc_bkt_lock); cp->mc_full.bl_reaplimit = cp->mc_full.bl_min; cp->mc_full.bl_min = cp->mc_full.bl_total; cp->mc_empty.bl_reaplimit = cp->mc_empty.bl_min; cp->mc_empty.bl_min = cp->mc_empty.bl_total; MCACHE_UNLOCK(&cp->mc_bkt_lock); } /* * Mark everything as eligible for reaping (working set is zero). */ static void mcache_bkt_ws_zero(mcache_t *cp) { MCACHE_LOCK(&cp->mc_bkt_lock); cp->mc_full.bl_reaplimit = cp->mc_full.bl_total; cp->mc_full.bl_min = cp->mc_full.bl_total; cp->mc_empty.bl_reaplimit = cp->mc_empty.bl_total; cp->mc_empty.bl_min = cp->mc_empty.bl_total; MCACHE_UNLOCK(&cp->mc_bkt_lock); } /* * Reap all buckets that are beyond the working set. */ static void mcache_bkt_ws_reap(mcache_t *cp) { long reap; mcache_bkt_t *bkt; reap = MIN(cp->mc_full.bl_reaplimit, cp->mc_full.bl_min); while (reap-- && (bkt = mcache_bkt_alloc(cp, &cp->mc_full)) != NULL) { mcache_bkt_destroy(cp, bkt, bkt->bkt_type->bt_bktsize); } reap = MIN(cp->mc_empty.bl_reaplimit, cp->mc_empty.bl_min); while (reap-- && (bkt = mcache_bkt_alloc(cp, &cp->mc_empty)) != NULL) { mcache_bkt_destroy(cp, bkt, 0); } } static void mcache_reap_timeout(thread_call_param_t dummy __unused, thread_call_param_t arg) { volatile UInt32 *flag = arg; ASSERT(flag == &mcache_reaping); *flag = 0; } static void mcache_reap_done(void *flag) { uint64_t deadline, leeway; clock_interval_to_deadline(mcache_reap_interval, NSEC_PER_SEC, &deadline); clock_interval_to_absolutetime_interval(mcache_reap_interval_leeway, NSEC_PER_SEC, &leeway); thread_call_enter_delayed_with_leeway(mcache_reap_tcall, flag, deadline, leeway, THREAD_CALL_DELAY_LEEWAY); } static void mcache_reap_start(void *arg) { UInt32 *flag = arg; ASSERT(flag == &mcache_reaping); mcache_applyall(mcache_cache_reap); mcache_dispatch(mcache_reap_done, flag); } __private_extern__ void mcache_reap(void) { UInt32 *flag = &mcache_reaping; if (mcache_llock_owner == current_thread() || !OSCompareAndSwap(0, 1, flag)) { return; } mcache_dispatch(mcache_reap_start, flag); } __private_extern__ void mcache_reap_now(mcache_t *cp, boolean_t purge) { if (purge) { mcache_bkt_purge(cp); mcache_cache_bkt_enable(cp); } else { mcache_bkt_ws_zero(cp); mcache_bkt_ws_reap(cp); } } static void mcache_cache_reap(mcache_t *cp) { mcache_bkt_ws_reap(cp); } /* * Performs period maintenance on a cache. */ static void mcache_cache_update(mcache_t *cp) { int need_bkt_resize = 0; int need_bkt_reenable = 0; lck_mtx_assert(mcache_llock, LCK_MTX_ASSERT_OWNED); mcache_bkt_ws_update(cp); /* * Cache resize and post-purge reenable are mutually exclusive. * If the cache was previously purged, there is no point of * increasing the bucket size as there was an indication of * memory pressure on the system. */ lck_mtx_lock_spin(&cp->mc_sync_lock); if (!(cp->mc_flags & MCF_NOCPUCACHE) && cp->mc_enable_cnt) { need_bkt_reenable = 1; } lck_mtx_unlock(&cp->mc_sync_lock); MCACHE_LOCK(&cp->mc_bkt_lock); /* * If the contention count is greater than the threshold, and if * we are not already at the maximum bucket size, increase it. * Otherwise, if this cache was previously purged by the user * then we simply reenable it. */ if ((unsigned int)cp->mc_chunksize < cp->cache_bkttype->bt_maxbuf && (int)(cp->mc_bkt_contention - cp->mc_bkt_contention_prev) > mcache_bkt_contention && !need_bkt_reenable) { need_bkt_resize = 1; } cp->mc_bkt_contention_prev = cp->mc_bkt_contention; MCACHE_UNLOCK(&cp->mc_bkt_lock); if (need_bkt_resize) { mcache_dispatch(mcache_cache_bkt_resize, cp); } else if (need_bkt_reenable) { mcache_dispatch(mcache_cache_enable, cp); } } /* * Recompute a cache's bucket size. This is an expensive operation * and should not be done frequently; larger buckets provide for a * higher transfer rate with the bucket while smaller buckets reduce * the memory consumption. */ static void mcache_cache_bkt_resize(void *arg) { mcache_t *cp = arg; mcache_bkttype_t *btp = cp->cache_bkttype; if ((unsigned int)cp->mc_chunksize < btp->bt_maxbuf) { mcache_bkt_purge(cp); /* * Upgrade to the next bucket type with larger bucket size; * temporarily set the previous contention snapshot to a * negative number to prevent unnecessary resize request. */ MCACHE_LOCK(&cp->mc_bkt_lock); cp->cache_bkttype = ++btp; cp->mc_bkt_contention_prev = cp->mc_bkt_contention + INT_MAX; MCACHE_UNLOCK(&cp->mc_bkt_lock); mcache_cache_enable(cp); } } /* * Reenable a previously disabled cache due to purge. */ static void mcache_cache_enable(void *arg) { mcache_t *cp = arg; lck_mtx_lock_spin(&cp->mc_sync_lock); cp->mc_purge_cnt = 0; cp->mc_enable_cnt = 0; lck_mtx_unlock(&cp->mc_sync_lock); mcache_cache_bkt_enable(cp); } static void mcache_update_timeout(__unused void *arg) { uint64_t deadline, leeway; clock_interval_to_deadline(mcache_reap_interval, NSEC_PER_SEC, &deadline); clock_interval_to_absolutetime_interval(mcache_reap_interval_leeway, NSEC_PER_SEC, &leeway); thread_call_enter_delayed_with_leeway(mcache_update_tcall, NULL, deadline, leeway, THREAD_CALL_DELAY_LEEWAY); } static void mcache_update(thread_call_param_t arg __unused, thread_call_param_t dummy __unused) { mcache_applyall(mcache_cache_update); mcache_update_timeout(NULL); } static void mcache_applyall(void (*func)(mcache_t *)) { mcache_t *cp; MCACHE_LIST_LOCK(); LIST_FOREACH(cp, &mcache_head, mc_list) { func(cp); } MCACHE_LIST_UNLOCK(); } static void mcache_dispatch(void (*func)(void *), void *arg) { ASSERT(func != NULL); timeout(func, arg, hz / 1000); } __private_extern__ void mcache_buffer_log(mcache_audit_t *mca, void *addr, mcache_t *cp, struct timeval *base_ts) { struct timeval now, base = { .tv_sec = 0, .tv_usec = 0 }; void *stack[MCACHE_STACK_DEPTH + 1]; struct mca_trn *transaction; transaction = &mca->mca_trns[mca->mca_next_trn]; mca->mca_addr = addr; mca->mca_cache = cp; transaction->mca_thread = current_thread(); bzero(stack, sizeof(stack)); transaction->mca_depth = (uint16_t)OSBacktrace(stack, MCACHE_STACK_DEPTH + 1) - 1; bcopy(&stack[1], transaction->mca_stack, sizeof(transaction->mca_stack)); microuptime(&now); if (base_ts != NULL) { base = *base_ts; } /* tstamp is in ms relative to base_ts */ transaction->mca_tstamp = ((now.tv_usec - base.tv_usec) / 1000); if ((now.tv_sec - base.tv_sec) > 0) { transaction->mca_tstamp += ((now.tv_sec - base.tv_sec) * 1000); } mca->mca_next_trn = (mca->mca_next_trn + 1) % mca_trn_max; } /* * N.B.: mcache_set_pattern(), mcache_verify_pattern() and * mcache_verify_set_pattern() are marked as noinline to prevent the * compiler from aliasing pointers when they are inlined inside the callers * (e.g. mcache_audit_free_verify_set()) which would be undefined behavior. */ __private_extern__ OS_NOINLINE void mcache_set_pattern(u_int64_t pattern, void *buf_arg, size_t size) { u_int64_t *buf_end = (u_int64_t *)((void *)((char *)buf_arg + size)); u_int64_t *buf = (u_int64_t *)buf_arg; VERIFY(IS_P2ALIGNED(buf_arg, sizeof(u_int64_t))); VERIFY(IS_P2ALIGNED(size, sizeof(u_int64_t))); while (buf < buf_end) { *buf++ = pattern; } } __private_extern__ OS_NOINLINE void * mcache_verify_pattern(u_int64_t pattern, void *buf_arg, size_t size) { u_int64_t *buf_end = (u_int64_t *)((void *)((char *)buf_arg + size)); u_int64_t *buf; VERIFY(IS_P2ALIGNED(buf_arg, sizeof(u_int64_t))); VERIFY(IS_P2ALIGNED(size, sizeof(u_int64_t))); for (buf = buf_arg; buf < buf_end; buf++) { if (*buf != pattern) { return buf; } } return NULL; } OS_NOINLINE static void * mcache_verify_set_pattern(u_int64_t old, u_int64_t new, void *buf_arg, size_t size) { u_int64_t *buf_end = (u_int64_t *)((void *)((char *)buf_arg + size)); u_int64_t *buf; VERIFY(IS_P2ALIGNED(buf_arg, sizeof(u_int64_t))); VERIFY(IS_P2ALIGNED(size, sizeof(u_int64_t))); for (buf = buf_arg; buf < buf_end; buf++) { if (*buf != old) { mcache_set_pattern(old, buf_arg, (uintptr_t)buf - (uintptr_t)buf_arg); return buf; } *buf = new; } return NULL; } __private_extern__ void mcache_audit_free_verify(mcache_audit_t *mca, void *base, size_t offset, size_t size) { void *addr; u_int64_t *oaddr64; mcache_obj_t *next; addr = (void *)((uintptr_t)base + offset); next = ((mcache_obj_t *)addr)->obj_next; /* For the "obj_next" pointer in the buffer */ oaddr64 = (u_int64_t *)P2ROUNDDOWN(addr, sizeof(u_int64_t)); *oaddr64 = MCACHE_FREE_PATTERN; if ((oaddr64 = mcache_verify_pattern(MCACHE_FREE_PATTERN, (caddr_t)base, size)) != NULL) { mcache_audit_panic(mca, addr, (caddr_t)oaddr64 - (caddr_t)base, (int64_t)MCACHE_FREE_PATTERN, (int64_t)*oaddr64); /* NOTREACHED */ } ((mcache_obj_t *)addr)->obj_next = next; } __private_extern__ void mcache_audit_free_verify_set(mcache_audit_t *mca, void *base, size_t offset, size_t size) { void *addr; u_int64_t *oaddr64; mcache_obj_t *next; addr = (void *)((uintptr_t)base + offset); next = ((mcache_obj_t *)addr)->obj_next; /* For the "obj_next" pointer in the buffer */ oaddr64 = (u_int64_t *)P2ROUNDDOWN(addr, sizeof(u_int64_t)); *oaddr64 = MCACHE_FREE_PATTERN; if ((oaddr64 = mcache_verify_set_pattern(MCACHE_FREE_PATTERN, MCACHE_UNINITIALIZED_PATTERN, (caddr_t)base, size)) != NULL) { mcache_audit_panic(mca, addr, (caddr_t)oaddr64 - (caddr_t)base, (int64_t)MCACHE_FREE_PATTERN, (int64_t)*oaddr64); /* NOTREACHED */ } ((mcache_obj_t *)addr)->obj_next = next; } #undef panic #define DUMP_TRN_FMT() \ "%s transaction thread %p saved PC stack (%d deep):\n" \ "\t%p, %p, %p, %p, %p, %p, %p, %p\n" \ "\t%p, %p, %p, %p, %p, %p, %p, %p\n" #define DUMP_TRN_FIELDS(s, x) \ s, \ mca->mca_trns[x].mca_thread, mca->mca_trns[x].mca_depth, \ mca->mca_trns[x].mca_stack[0], mca->mca_trns[x].mca_stack[1], \ mca->mca_trns[x].mca_stack[2], mca->mca_trns[x].mca_stack[3], \ mca->mca_trns[x].mca_stack[4], mca->mca_trns[x].mca_stack[5], \ mca->mca_trns[x].mca_stack[6], mca->mca_trns[x].mca_stack[7], \ mca->mca_trns[x].mca_stack[8], mca->mca_trns[x].mca_stack[9], \ mca->mca_trns[x].mca_stack[10], mca->mca_trns[x].mca_stack[11], \ mca->mca_trns[x].mca_stack[12], mca->mca_trns[x].mca_stack[13], \ mca->mca_trns[x].mca_stack[14], mca->mca_trns[x].mca_stack[15] #define MCA_TRN_LAST ((mca->mca_next_trn + mca_trn_max) % mca_trn_max) #define MCA_TRN_PREV ((mca->mca_next_trn + mca_trn_max - 1) % mca_trn_max) __private_extern__ char * mcache_dump_mca(mcache_audit_t *mca) { if (mca_dump_buf == NULL) { return NULL; } snprintf(mca_dump_buf, DUMP_MCA_BUF_SIZE, "mca %p: addr %p, cache %p (%s) nxttrn %d\n" DUMP_TRN_FMT() DUMP_TRN_FMT(), mca, mca->mca_addr, mca->mca_cache, mca->mca_cache ? mca->mca_cache->mc_name : "?", mca->mca_next_trn, DUMP_TRN_FIELDS("last", MCA_TRN_LAST), DUMP_TRN_FIELDS("previous", MCA_TRN_PREV)); return mca_dump_buf; } __private_extern__ void mcache_audit_panic(mcache_audit_t *mca, void *addr, size_t offset, int64_t expected, int64_t got) { if (mca == NULL) { panic("mcache_audit: buffer %p modified after free at " "offset 0x%lx (0x%llx instead of 0x%llx)\n", addr, offset, got, expected); /* NOTREACHED */ __builtin_unreachable(); } panic("mcache_audit: buffer %p modified after free at offset 0x%lx " "(0x%llx instead of 0x%llx)\n%s\n", addr, offset, got, expected, mcache_dump_mca(mca)); /* NOTREACHED */ __builtin_unreachable(); } __attribute__((noinline, cold, not_tail_called, noreturn)) __private_extern__ int assfail(const char *a, const char *f, int l) { panic("assertion failed: %s, file: %s, line: %d", a, f, l); /* NOTREACHED */ __builtin_unreachable(); } |