ttm_memory.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. /**************************************************************************
  2. *
  3. * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
  4. * All Rights Reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sub license, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice (including the
  15. * next paragraph) shall be included in all copies or substantial portions
  16. * of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  21. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  22. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  23. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  24. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. *
  26. **************************************************************************/
  27. #include "ttm/ttm_memory.h"
  28. #include "ttm/ttm_module.h"
  29. #include <linux/spinlock.h>
  30. #include <linux/sched.h>
  31. #include <linux/wait.h>
  32. #include <linux/mm.h>
  33. #include <linux/module.h>
  34. #define TTM_MEMORY_ALLOC_RETRIES 4
  35. struct ttm_mem_zone {
  36. struct kobject kobj;
  37. struct ttm_mem_global *glob;
  38. const char *name;
  39. uint64_t zone_mem;
  40. uint64_t emer_mem;
  41. uint64_t max_mem;
  42. uint64_t swap_limit;
  43. uint64_t used_mem;
  44. };
  45. static struct attribute ttm_mem_sys = {
  46. .name = "zone_memory",
  47. .mode = S_IRUGO
  48. };
  49. static struct attribute ttm_mem_emer = {
  50. .name = "emergency_memory",
  51. .mode = S_IRUGO | S_IWUSR
  52. };
  53. static struct attribute ttm_mem_max = {
  54. .name = "available_memory",
  55. .mode = S_IRUGO | S_IWUSR
  56. };
  57. static struct attribute ttm_mem_swap = {
  58. .name = "swap_limit",
  59. .mode = S_IRUGO | S_IWUSR
  60. };
  61. static struct attribute ttm_mem_used = {
  62. .name = "used_memory",
  63. .mode = S_IRUGO
  64. };
  65. static void ttm_mem_zone_kobj_release(struct kobject *kobj)
  66. {
  67. struct ttm_mem_zone *zone =
  68. container_of(kobj, struct ttm_mem_zone, kobj);
  69. printk(KERN_INFO TTM_PFX
  70. "Zone %7s: Used memory at exit: %llu kiB.\n",
  71. zone->name, (unsigned long long) zone->used_mem >> 10);
  72. kfree(zone);
  73. }
  74. static ssize_t ttm_mem_zone_show(struct kobject *kobj,
  75. struct attribute *attr,
  76. char *buffer)
  77. {
  78. struct ttm_mem_zone *zone =
  79. container_of(kobj, struct ttm_mem_zone, kobj);
  80. uint64_t val = 0;
  81. spin_lock(&zone->glob->lock);
  82. if (attr == &ttm_mem_sys)
  83. val = zone->zone_mem;
  84. else if (attr == &ttm_mem_emer)
  85. val = zone->emer_mem;
  86. else if (attr == &ttm_mem_max)
  87. val = zone->max_mem;
  88. else if (attr == &ttm_mem_swap)
  89. val = zone->swap_limit;
  90. else if (attr == &ttm_mem_used)
  91. val = zone->used_mem;
  92. spin_unlock(&zone->glob->lock);
  93. return snprintf(buffer, PAGE_SIZE, "%llu\n",
  94. (unsigned long long) val >> 10);
  95. }
  96. static void ttm_check_swapping(struct ttm_mem_global *glob);
  97. static ssize_t ttm_mem_zone_store(struct kobject *kobj,
  98. struct attribute *attr,
  99. const char *buffer,
  100. size_t size)
  101. {
  102. struct ttm_mem_zone *zone =
  103. container_of(kobj, struct ttm_mem_zone, kobj);
  104. int chars;
  105. unsigned long val;
  106. uint64_t val64;
  107. chars = sscanf(buffer, "%lu", &val);
  108. if (chars == 0)
  109. return size;
  110. val64 = val;
  111. val64 <<= 10;
  112. spin_lock(&zone->glob->lock);
  113. if (val64 > zone->zone_mem)
  114. val64 = zone->zone_mem;
  115. if (attr == &ttm_mem_emer) {
  116. zone->emer_mem = val64;
  117. if (zone->max_mem > val64)
  118. zone->max_mem = val64;
  119. } else if (attr == &ttm_mem_max) {
  120. zone->max_mem = val64;
  121. if (zone->emer_mem < val64)
  122. zone->emer_mem = val64;
  123. } else if (attr == &ttm_mem_swap)
  124. zone->swap_limit = val64;
  125. spin_unlock(&zone->glob->lock);
  126. ttm_check_swapping(zone->glob);
  127. return size;
  128. }
  129. static struct attribute *ttm_mem_zone_attrs[] = {
  130. &ttm_mem_sys,
  131. &ttm_mem_emer,
  132. &ttm_mem_max,
  133. &ttm_mem_swap,
  134. &ttm_mem_used,
  135. NULL
  136. };
  137. static struct sysfs_ops ttm_mem_zone_ops = {
  138. .show = &ttm_mem_zone_show,
  139. .store = &ttm_mem_zone_store
  140. };
  141. static struct kobj_type ttm_mem_zone_kobj_type = {
  142. .release = &ttm_mem_zone_kobj_release,
  143. .sysfs_ops = &ttm_mem_zone_ops,
  144. .default_attrs = ttm_mem_zone_attrs,
  145. };
  146. static void ttm_mem_global_kobj_release(struct kobject *kobj)
  147. {
  148. struct ttm_mem_global *glob =
  149. container_of(kobj, struct ttm_mem_global, kobj);
  150. kfree(glob);
  151. }
  152. static struct kobj_type ttm_mem_glob_kobj_type = {
  153. .release = &ttm_mem_global_kobj_release,
  154. };
  155. static bool ttm_zones_above_swap_target(struct ttm_mem_global *glob,
  156. bool from_wq, uint64_t extra)
  157. {
  158. unsigned int i;
  159. struct ttm_mem_zone *zone;
  160. uint64_t target;
  161. for (i = 0; i < glob->num_zones; ++i) {
  162. zone = glob->zones[i];
  163. if (from_wq)
  164. target = zone->swap_limit;
  165. else if (capable(CAP_SYS_ADMIN))
  166. target = zone->emer_mem;
  167. else
  168. target = zone->max_mem;
  169. target = (extra > target) ? 0ULL : target;
  170. if (zone->used_mem > target)
  171. return true;
  172. }
  173. return false;
  174. }
  175. /**
  176. * At this point we only support a single shrink callback.
  177. * Extend this if needed, perhaps using a linked list of callbacks.
  178. * Note that this function is reentrant:
  179. * many threads may try to swap out at any given time.
  180. */
  181. static void ttm_shrink(struct ttm_mem_global *glob, bool from_wq,
  182. uint64_t extra)
  183. {
  184. int ret;
  185. struct ttm_mem_shrink *shrink;
  186. spin_lock(&glob->lock);
  187. if (glob->shrink == NULL)
  188. goto out;
  189. while (ttm_zones_above_swap_target(glob, from_wq, extra)) {
  190. shrink = glob->shrink;
  191. spin_unlock(&glob->lock);
  192. ret = shrink->do_shrink(shrink);
  193. spin_lock(&glob->lock);
  194. if (unlikely(ret != 0))
  195. goto out;
  196. }
  197. out:
  198. spin_unlock(&glob->lock);
  199. }
  200. static void ttm_shrink_work(struct work_struct *work)
  201. {
  202. struct ttm_mem_global *glob =
  203. container_of(work, struct ttm_mem_global, work);
  204. ttm_shrink(glob, true, 0ULL);
  205. }
  206. static int ttm_mem_init_kernel_zone(struct ttm_mem_global *glob,
  207. const struct sysinfo *si)
  208. {
  209. struct ttm_mem_zone *zone = kzalloc(sizeof(*zone), GFP_KERNEL);
  210. uint64_t mem;
  211. int ret;
  212. if (unlikely(!zone))
  213. return -ENOMEM;
  214. mem = si->totalram - si->totalhigh;
  215. mem *= si->mem_unit;
  216. zone->name = "kernel";
  217. zone->zone_mem = mem;
  218. zone->max_mem = mem >> 1;
  219. zone->emer_mem = (mem >> 1) + (mem >> 2);
  220. zone->swap_limit = zone->max_mem - (mem >> 3);
  221. zone->used_mem = 0;
  222. zone->glob = glob;
  223. glob->zone_kernel = zone;
  224. kobject_init(&zone->kobj, &ttm_mem_zone_kobj_type);
  225. ret = kobject_add(&zone->kobj, &glob->kobj, zone->name);
  226. if (unlikely(ret != 0)) {
  227. kobject_put(&zone->kobj);
  228. return ret;
  229. }
  230. glob->zones[glob->num_zones++] = zone;
  231. return 0;
  232. }
  233. #ifdef CONFIG_HIGHMEM
  234. static int ttm_mem_init_highmem_zone(struct ttm_mem_global *glob,
  235. const struct sysinfo *si)
  236. {
  237. struct ttm_mem_zone *zone;
  238. uint64_t mem;
  239. int ret;
  240. if (si->totalhigh == 0)
  241. return 0;
  242. zone = kzalloc(sizeof(*zone), GFP_KERNEL);
  243. if (unlikely(!zone))
  244. return -ENOMEM;
  245. mem = si->totalram;
  246. mem *= si->mem_unit;
  247. zone->name = "highmem";
  248. zone->zone_mem = mem;
  249. zone->max_mem = mem >> 1;
  250. zone->emer_mem = (mem >> 1) + (mem >> 2);
  251. zone->swap_limit = zone->max_mem - (mem >> 3);
  252. zone->used_mem = 0;
  253. zone->glob = glob;
  254. glob->zone_highmem = zone;
  255. kobject_init(&zone->kobj, &ttm_mem_zone_kobj_type);
  256. ret = kobject_add(&zone->kobj, &glob->kobj, zone->name);
  257. if (unlikely(ret != 0)) {
  258. kobject_put(&zone->kobj);
  259. return ret;
  260. }
  261. glob->zones[glob->num_zones++] = zone;
  262. return 0;
  263. }
  264. #else
  265. static int ttm_mem_init_dma32_zone(struct ttm_mem_global *glob,
  266. const struct sysinfo *si)
  267. {
  268. struct ttm_mem_zone *zone = kzalloc(sizeof(*zone), GFP_KERNEL);
  269. uint64_t mem;
  270. int ret;
  271. if (unlikely(!zone))
  272. return -ENOMEM;
  273. mem = si->totalram;
  274. mem *= si->mem_unit;
  275. /**
  276. * No special dma32 zone needed.
  277. */
  278. if (mem <= ((uint64_t) 1ULL << 32)) {
  279. kfree(zone);
  280. return 0;
  281. }
  282. /*
  283. * Limit max dma32 memory to 4GB for now
  284. * until we can figure out how big this
  285. * zone really is.
  286. */
  287. mem = ((uint64_t) 1ULL << 32);
  288. zone->name = "dma32";
  289. zone->zone_mem = mem;
  290. zone->max_mem = mem >> 1;
  291. zone->emer_mem = (mem >> 1) + (mem >> 2);
  292. zone->swap_limit = zone->max_mem - (mem >> 3);
  293. zone->used_mem = 0;
  294. zone->glob = glob;
  295. glob->zone_dma32 = zone;
  296. kobject_init(&zone->kobj, &ttm_mem_zone_kobj_type);
  297. ret = kobject_add(&zone->kobj, &glob->kobj, zone->name);
  298. if (unlikely(ret != 0)) {
  299. kobject_put(&zone->kobj);
  300. return ret;
  301. }
  302. glob->zones[glob->num_zones++] = zone;
  303. return 0;
  304. }
  305. #endif
  306. int ttm_mem_global_init(struct ttm_mem_global *glob)
  307. {
  308. struct sysinfo si;
  309. int ret;
  310. int i;
  311. struct ttm_mem_zone *zone;
  312. spin_lock_init(&glob->lock);
  313. glob->swap_queue = create_singlethread_workqueue("ttm_swap");
  314. INIT_WORK(&glob->work, ttm_shrink_work);
  315. init_waitqueue_head(&glob->queue);
  316. kobject_init(&glob->kobj, &ttm_mem_glob_kobj_type);
  317. ret = kobject_add(&glob->kobj,
  318. ttm_get_kobj(),
  319. "memory_accounting");
  320. if (unlikely(ret != 0)) {
  321. kobject_put(&glob->kobj);
  322. return ret;
  323. }
  324. si_meminfo(&si);
  325. ret = ttm_mem_init_kernel_zone(glob, &si);
  326. if (unlikely(ret != 0))
  327. goto out_no_zone;
  328. #ifdef CONFIG_HIGHMEM
  329. ret = ttm_mem_init_highmem_zone(glob, &si);
  330. if (unlikely(ret != 0))
  331. goto out_no_zone;
  332. #else
  333. ret = ttm_mem_init_dma32_zone(glob, &si);
  334. if (unlikely(ret != 0))
  335. goto out_no_zone;
  336. #endif
  337. for (i = 0; i < glob->num_zones; ++i) {
  338. zone = glob->zones[i];
  339. printk(KERN_INFO TTM_PFX
  340. "Zone %7s: Available graphics memory: %llu kiB.\n",
  341. zone->name, (unsigned long long) zone->max_mem >> 10);
  342. }
  343. return 0;
  344. out_no_zone:
  345. ttm_mem_global_release(glob);
  346. return ret;
  347. }
  348. EXPORT_SYMBOL(ttm_mem_global_init);
  349. void ttm_mem_global_release(struct ttm_mem_global *glob)
  350. {
  351. unsigned int i;
  352. struct ttm_mem_zone *zone;
  353. flush_workqueue(glob->swap_queue);
  354. destroy_workqueue(glob->swap_queue);
  355. glob->swap_queue = NULL;
  356. for (i = 0; i < glob->num_zones; ++i) {
  357. zone = glob->zones[i];
  358. kobject_del(&zone->kobj);
  359. kobject_put(&zone->kobj);
  360. }
  361. kobject_del(&glob->kobj);
  362. kobject_put(&glob->kobj);
  363. }
  364. EXPORT_SYMBOL(ttm_mem_global_release);
  365. static void ttm_check_swapping(struct ttm_mem_global *glob)
  366. {
  367. bool needs_swapping = false;
  368. unsigned int i;
  369. struct ttm_mem_zone *zone;
  370. spin_lock(&glob->lock);
  371. for (i = 0; i < glob->num_zones; ++i) {
  372. zone = glob->zones[i];
  373. if (zone->used_mem > zone->swap_limit) {
  374. needs_swapping = true;
  375. break;
  376. }
  377. }
  378. spin_unlock(&glob->lock);
  379. if (unlikely(needs_swapping))
  380. (void)queue_work(glob->swap_queue, &glob->work);
  381. }
  382. static void ttm_mem_global_free_zone(struct ttm_mem_global *glob,
  383. struct ttm_mem_zone *single_zone,
  384. uint64_t amount)
  385. {
  386. unsigned int i;
  387. struct ttm_mem_zone *zone;
  388. spin_lock(&glob->lock);
  389. for (i = 0; i < glob->num_zones; ++i) {
  390. zone = glob->zones[i];
  391. if (single_zone && zone != single_zone)
  392. continue;
  393. zone->used_mem -= amount;
  394. }
  395. spin_unlock(&glob->lock);
  396. }
  397. void ttm_mem_global_free(struct ttm_mem_global *glob,
  398. uint64_t amount)
  399. {
  400. return ttm_mem_global_free_zone(glob, NULL, amount);
  401. }
  402. EXPORT_SYMBOL(ttm_mem_global_free);
  403. static int ttm_mem_global_reserve(struct ttm_mem_global *glob,
  404. struct ttm_mem_zone *single_zone,
  405. uint64_t amount, bool reserve)
  406. {
  407. uint64_t limit;
  408. int ret = -ENOMEM;
  409. unsigned int i;
  410. struct ttm_mem_zone *zone;
  411. spin_lock(&glob->lock);
  412. for (i = 0; i < glob->num_zones; ++i) {
  413. zone = glob->zones[i];
  414. if (single_zone && zone != single_zone)
  415. continue;
  416. limit = (capable(CAP_SYS_ADMIN)) ?
  417. zone->emer_mem : zone->max_mem;
  418. if (zone->used_mem > limit)
  419. goto out_unlock;
  420. }
  421. if (reserve) {
  422. for (i = 0; i < glob->num_zones; ++i) {
  423. zone = glob->zones[i];
  424. if (single_zone && zone != single_zone)
  425. continue;
  426. zone->used_mem += amount;
  427. }
  428. }
  429. ret = 0;
  430. out_unlock:
  431. spin_unlock(&glob->lock);
  432. ttm_check_swapping(glob);
  433. return ret;
  434. }
  435. static int ttm_mem_global_alloc_zone(struct ttm_mem_global *glob,
  436. struct ttm_mem_zone *single_zone,
  437. uint64_t memory,
  438. bool no_wait, bool interruptible)
  439. {
  440. int count = TTM_MEMORY_ALLOC_RETRIES;
  441. while (unlikely(ttm_mem_global_reserve(glob,
  442. single_zone,
  443. memory, true)
  444. != 0)) {
  445. if (no_wait)
  446. return -ENOMEM;
  447. if (unlikely(count-- == 0))
  448. return -ENOMEM;
  449. ttm_shrink(glob, false, memory + (memory >> 2) + 16);
  450. }
  451. return 0;
  452. }
  453. int ttm_mem_global_alloc(struct ttm_mem_global *glob, uint64_t memory,
  454. bool no_wait, bool interruptible)
  455. {
  456. /**
  457. * Normal allocations of kernel memory are registered in
  458. * all zones.
  459. */
  460. return ttm_mem_global_alloc_zone(glob, NULL, memory, no_wait,
  461. interruptible);
  462. }
  463. EXPORT_SYMBOL(ttm_mem_global_alloc);
  464. int ttm_mem_global_alloc_page(struct ttm_mem_global *glob,
  465. struct page *page,
  466. bool no_wait, bool interruptible)
  467. {
  468. struct ttm_mem_zone *zone = NULL;
  469. /**
  470. * Page allocations may be registed in a single zone
  471. * only if highmem or !dma32.
  472. */
  473. #ifdef CONFIG_HIGHMEM
  474. if (PageHighMem(page) && glob->zone_highmem != NULL)
  475. zone = glob->zone_highmem;
  476. #else
  477. if (glob->zone_dma32 && page_to_pfn(page) > 0x00100000UL)
  478. zone = glob->zone_kernel;
  479. #endif
  480. return ttm_mem_global_alloc_zone(glob, zone, PAGE_SIZE, no_wait,
  481. interruptible);
  482. }
  483. void ttm_mem_global_free_page(struct ttm_mem_global *glob, struct page *page)
  484. {
  485. struct ttm_mem_zone *zone = NULL;
  486. #ifdef CONFIG_HIGHMEM
  487. if (PageHighMem(page) && glob->zone_highmem != NULL)
  488. zone = glob->zone_highmem;
  489. #else
  490. if (glob->zone_dma32 && page_to_pfn(page) > 0x00100000UL)
  491. zone = glob->zone_kernel;
  492. #endif
  493. ttm_mem_global_free_zone(glob, zone, PAGE_SIZE);
  494. }
  495. size_t ttm_round_pot(size_t size)
  496. {
  497. if ((size & (size - 1)) == 0)
  498. return size;
  499. else if (size > PAGE_SIZE)
  500. return PAGE_ALIGN(size);
  501. else {
  502. size_t tmp_size = 4;
  503. while (tmp_size < size)
  504. tmp_size <<= 1;
  505. return tmp_size;
  506. }
  507. return 0;
  508. }
  509. EXPORT_SYMBOL(ttm_round_pot);