ttm_memory.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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 "ttm/ttm_page_alloc.h"
  30. #include <linux/spinlock.h>
  31. #include <linux/sched.h>
  32. #include <linux/wait.h>
  33. #include <linux/mm.h>
  34. #include <linux/module.h>
  35. #include <linux/slab.h>
  36. #define TTM_MEMORY_ALLOC_RETRIES 4
  37. struct ttm_mem_zone {
  38. struct kobject kobj;
  39. struct ttm_mem_global *glob;
  40. const char *name;
  41. uint64_t zone_mem;
  42. uint64_t emer_mem;
  43. uint64_t max_mem;
  44. uint64_t swap_limit;
  45. uint64_t used_mem;
  46. };
  47. static struct attribute ttm_mem_sys = {
  48. .name = "zone_memory",
  49. .mode = S_IRUGO
  50. };
  51. static struct attribute ttm_mem_emer = {
  52. .name = "emergency_memory",
  53. .mode = S_IRUGO | S_IWUSR
  54. };
  55. static struct attribute ttm_mem_max = {
  56. .name = "available_memory",
  57. .mode = S_IRUGO | S_IWUSR
  58. };
  59. static struct attribute ttm_mem_swap = {
  60. .name = "swap_limit",
  61. .mode = S_IRUGO | S_IWUSR
  62. };
  63. static struct attribute ttm_mem_used = {
  64. .name = "used_memory",
  65. .mode = S_IRUGO
  66. };
  67. static void ttm_mem_zone_kobj_release(struct kobject *kobj)
  68. {
  69. struct ttm_mem_zone *zone =
  70. container_of(kobj, struct ttm_mem_zone, kobj);
  71. printk(KERN_INFO TTM_PFX
  72. "Zone %7s: Used memory at exit: %llu kiB.\n",
  73. zone->name, (unsigned long long) zone->used_mem >> 10);
  74. kfree(zone);
  75. }
  76. static ssize_t ttm_mem_zone_show(struct kobject *kobj,
  77. struct attribute *attr,
  78. char *buffer)
  79. {
  80. struct ttm_mem_zone *zone =
  81. container_of(kobj, struct ttm_mem_zone, kobj);
  82. uint64_t val = 0;
  83. spin_lock(&zone->glob->lock);
  84. if (attr == &ttm_mem_sys)
  85. val = zone->zone_mem;
  86. else if (attr == &ttm_mem_emer)
  87. val = zone->emer_mem;
  88. else if (attr == &ttm_mem_max)
  89. val = zone->max_mem;
  90. else if (attr == &ttm_mem_swap)
  91. val = zone->swap_limit;
  92. else if (attr == &ttm_mem_used)
  93. val = zone->used_mem;
  94. spin_unlock(&zone->glob->lock);
  95. return snprintf(buffer, PAGE_SIZE, "%llu\n",
  96. (unsigned long long) val >> 10);
  97. }
  98. static void ttm_check_swapping(struct ttm_mem_global *glob);
  99. static ssize_t ttm_mem_zone_store(struct kobject *kobj,
  100. struct attribute *attr,
  101. const char *buffer,
  102. size_t size)
  103. {
  104. struct ttm_mem_zone *zone =
  105. container_of(kobj, struct ttm_mem_zone, kobj);
  106. int chars;
  107. unsigned long val;
  108. uint64_t val64;
  109. chars = sscanf(buffer, "%lu", &val);
  110. if (chars == 0)
  111. return size;
  112. val64 = val;
  113. val64 <<= 10;
  114. spin_lock(&zone->glob->lock);
  115. if (val64 > zone->zone_mem)
  116. val64 = zone->zone_mem;
  117. if (attr == &ttm_mem_emer) {
  118. zone->emer_mem = val64;
  119. if (zone->max_mem > val64)
  120. zone->max_mem = val64;
  121. } else if (attr == &ttm_mem_max) {
  122. zone->max_mem = val64;
  123. if (zone->emer_mem < val64)
  124. zone->emer_mem = val64;
  125. } else if (attr == &ttm_mem_swap)
  126. zone->swap_limit = val64;
  127. spin_unlock(&zone->glob->lock);
  128. ttm_check_swapping(zone->glob);
  129. return size;
  130. }
  131. static struct attribute *ttm_mem_zone_attrs[] = {
  132. &ttm_mem_sys,
  133. &ttm_mem_emer,
  134. &ttm_mem_max,
  135. &ttm_mem_swap,
  136. &ttm_mem_used,
  137. NULL
  138. };
  139. static const struct sysfs_ops ttm_mem_zone_ops = {
  140. .show = &ttm_mem_zone_show,
  141. .store = &ttm_mem_zone_store
  142. };
  143. static struct kobj_type ttm_mem_zone_kobj_type = {
  144. .release = &ttm_mem_zone_kobj_release,
  145. .sysfs_ops = &ttm_mem_zone_ops,
  146. .default_attrs = ttm_mem_zone_attrs,
  147. };
  148. static void ttm_mem_global_kobj_release(struct kobject *kobj)
  149. {
  150. struct ttm_mem_global *glob =
  151. container_of(kobj, struct ttm_mem_global, kobj);
  152. kfree(glob);
  153. }
  154. static struct kobj_type ttm_mem_glob_kobj_type = {
  155. .release = &ttm_mem_global_kobj_release,
  156. };
  157. static bool ttm_zones_above_swap_target(struct ttm_mem_global *glob,
  158. bool from_wq, uint64_t extra)
  159. {
  160. unsigned int i;
  161. struct ttm_mem_zone *zone;
  162. uint64_t target;
  163. for (i = 0; i < glob->num_zones; ++i) {
  164. zone = glob->zones[i];
  165. if (from_wq)
  166. target = zone->swap_limit;
  167. else if (capable(CAP_SYS_ADMIN))
  168. target = zone->emer_mem;
  169. else
  170. target = zone->max_mem;
  171. target = (extra > target) ? 0ULL : target;
  172. if (zone->used_mem > target)
  173. return true;
  174. }
  175. return false;
  176. }
  177. /**
  178. * At this point we only support a single shrink callback.
  179. * Extend this if needed, perhaps using a linked list of callbacks.
  180. * Note that this function is reentrant:
  181. * many threads may try to swap out at any given time.
  182. */
  183. static void ttm_shrink(struct ttm_mem_global *glob, bool from_wq,
  184. uint64_t extra)
  185. {
  186. int ret;
  187. struct ttm_mem_shrink *shrink;
  188. spin_lock(&glob->lock);
  189. if (glob->shrink == NULL)
  190. goto out;
  191. while (ttm_zones_above_swap_target(glob, from_wq, extra)) {
  192. shrink = glob->shrink;
  193. spin_unlock(&glob->lock);
  194. ret = shrink->do_shrink(shrink);
  195. spin_lock(&glob->lock);
  196. if (unlikely(ret != 0))
  197. goto out;
  198. }
  199. out:
  200. spin_unlock(&glob->lock);
  201. }
  202. static void ttm_shrink_work(struct work_struct *work)
  203. {
  204. struct ttm_mem_global *glob =
  205. container_of(work, struct ttm_mem_global, work);
  206. ttm_shrink(glob, true, 0ULL);
  207. }
  208. static int ttm_mem_init_kernel_zone(struct ttm_mem_global *glob,
  209. const struct sysinfo *si)
  210. {
  211. struct ttm_mem_zone *zone = kzalloc(sizeof(*zone), GFP_KERNEL);
  212. uint64_t mem;
  213. int ret;
  214. if (unlikely(!zone))
  215. return -ENOMEM;
  216. mem = si->totalram - si->totalhigh;
  217. mem *= si->mem_unit;
  218. zone->name = "kernel";
  219. zone->zone_mem = mem;
  220. zone->max_mem = mem >> 1;
  221. zone->emer_mem = (mem >> 1) + (mem >> 2);
  222. zone->swap_limit = zone->max_mem - (mem >> 3);
  223. zone->used_mem = 0;
  224. zone->glob = glob;
  225. glob->zone_kernel = zone;
  226. ret = kobject_init_and_add(
  227. &zone->kobj, &ttm_mem_zone_kobj_type, &glob->kobj, zone->name);
  228. if (unlikely(ret != 0)) {
  229. kobject_put(&zone->kobj);
  230. return ret;
  231. }
  232. glob->zones[glob->num_zones++] = zone;
  233. return 0;
  234. }
  235. #ifdef CONFIG_HIGHMEM
  236. static int ttm_mem_init_highmem_zone(struct ttm_mem_global *glob,
  237. const struct sysinfo *si)
  238. {
  239. struct ttm_mem_zone *zone;
  240. uint64_t mem;
  241. int ret;
  242. if (si->totalhigh == 0)
  243. return 0;
  244. zone = kzalloc(sizeof(*zone), GFP_KERNEL);
  245. if (unlikely(!zone))
  246. return -ENOMEM;
  247. mem = si->totalram;
  248. mem *= si->mem_unit;
  249. zone->name = "highmem";
  250. zone->zone_mem = mem;
  251. zone->max_mem = mem >> 1;
  252. zone->emer_mem = (mem >> 1) + (mem >> 2);
  253. zone->swap_limit = zone->max_mem - (mem >> 3);
  254. zone->used_mem = 0;
  255. zone->glob = glob;
  256. glob->zone_highmem = zone;
  257. ret = kobject_init_and_add(
  258. &zone->kobj, &ttm_mem_zone_kobj_type, &glob->kobj, zone->name);
  259. if (unlikely(ret != 0)) {
  260. kobject_put(&zone->kobj);
  261. return ret;
  262. }
  263. glob->zones[glob->num_zones++] = zone;
  264. return 0;
  265. }
  266. #else
  267. static int ttm_mem_init_dma32_zone(struct ttm_mem_global *glob,
  268. const struct sysinfo *si)
  269. {
  270. struct ttm_mem_zone *zone = kzalloc(sizeof(*zone), GFP_KERNEL);
  271. uint64_t mem;
  272. int ret;
  273. if (unlikely(!zone))
  274. return -ENOMEM;
  275. mem = si->totalram;
  276. mem *= si->mem_unit;
  277. /**
  278. * No special dma32 zone needed.
  279. */
  280. if (mem <= ((uint64_t) 1ULL << 32)) {
  281. kfree(zone);
  282. return 0;
  283. }
  284. /*
  285. * Limit max dma32 memory to 4GB for now
  286. * until we can figure out how big this
  287. * zone really is.
  288. */
  289. mem = ((uint64_t) 1ULL << 32);
  290. zone->name = "dma32";
  291. zone->zone_mem = mem;
  292. zone->max_mem = mem >> 1;
  293. zone->emer_mem = (mem >> 1) + (mem >> 2);
  294. zone->swap_limit = zone->max_mem - (mem >> 3);
  295. zone->used_mem = 0;
  296. zone->glob = glob;
  297. glob->zone_dma32 = zone;
  298. ret = kobject_init_and_add(
  299. &zone->kobj, &ttm_mem_zone_kobj_type, &glob->kobj, zone->name);
  300. if (unlikely(ret != 0)) {
  301. kobject_put(&zone->kobj);
  302. return ret;
  303. }
  304. glob->zones[glob->num_zones++] = zone;
  305. return 0;
  306. }
  307. #endif
  308. int ttm_mem_global_init(struct ttm_mem_global *glob)
  309. {
  310. struct sysinfo si;
  311. int ret;
  312. int i;
  313. struct ttm_mem_zone *zone;
  314. spin_lock_init(&glob->lock);
  315. glob->swap_queue = create_singlethread_workqueue("ttm_swap");
  316. INIT_WORK(&glob->work, ttm_shrink_work);
  317. init_waitqueue_head(&glob->queue);
  318. ret = kobject_init_and_add(
  319. &glob->kobj, &ttm_mem_glob_kobj_type, ttm_get_kobj(), "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. ttm_page_alloc_init(glob, glob->zone_kernel->max_mem/(2*PAGE_SIZE));
  344. return 0;
  345. out_no_zone:
  346. ttm_mem_global_release(glob);
  347. return ret;
  348. }
  349. EXPORT_SYMBOL(ttm_mem_global_init);
  350. void ttm_mem_global_release(struct ttm_mem_global *glob)
  351. {
  352. unsigned int i;
  353. struct ttm_mem_zone *zone;
  354. /* let the page allocator first stop the shrink work. */
  355. ttm_page_alloc_fini();
  356. flush_workqueue(glob->swap_queue);
  357. destroy_workqueue(glob->swap_queue);
  358. glob->swap_queue = NULL;
  359. for (i = 0; i < glob->num_zones; ++i) {
  360. zone = glob->zones[i];
  361. kobject_del(&zone->kobj);
  362. kobject_put(&zone->kobj);
  363. }
  364. kobject_del(&glob->kobj);
  365. kobject_put(&glob->kobj);
  366. }
  367. EXPORT_SYMBOL(ttm_mem_global_release);
  368. static void ttm_check_swapping(struct ttm_mem_global *glob)
  369. {
  370. bool needs_swapping = false;
  371. unsigned int i;
  372. struct ttm_mem_zone *zone;
  373. spin_lock(&glob->lock);
  374. for (i = 0; i < glob->num_zones; ++i) {
  375. zone = glob->zones[i];
  376. if (zone->used_mem > zone->swap_limit) {
  377. needs_swapping = true;
  378. break;
  379. }
  380. }
  381. spin_unlock(&glob->lock);
  382. if (unlikely(needs_swapping))
  383. (void)queue_work(glob->swap_queue, &glob->work);
  384. }
  385. static void ttm_mem_global_free_zone(struct ttm_mem_global *glob,
  386. struct ttm_mem_zone *single_zone,
  387. uint64_t amount)
  388. {
  389. unsigned int i;
  390. struct ttm_mem_zone *zone;
  391. spin_lock(&glob->lock);
  392. for (i = 0; i < glob->num_zones; ++i) {
  393. zone = glob->zones[i];
  394. if (single_zone && zone != single_zone)
  395. continue;
  396. zone->used_mem -= amount;
  397. }
  398. spin_unlock(&glob->lock);
  399. }
  400. void ttm_mem_global_free(struct ttm_mem_global *glob,
  401. uint64_t amount)
  402. {
  403. return ttm_mem_global_free_zone(glob, NULL, amount);
  404. }
  405. EXPORT_SYMBOL(ttm_mem_global_free);
  406. static int ttm_mem_global_reserve(struct ttm_mem_global *glob,
  407. struct ttm_mem_zone *single_zone,
  408. uint64_t amount, bool reserve)
  409. {
  410. uint64_t limit;
  411. int ret = -ENOMEM;
  412. unsigned int i;
  413. struct ttm_mem_zone *zone;
  414. spin_lock(&glob->lock);
  415. for (i = 0; i < glob->num_zones; ++i) {
  416. zone = glob->zones[i];
  417. if (single_zone && zone != single_zone)
  418. continue;
  419. limit = (capable(CAP_SYS_ADMIN)) ?
  420. zone->emer_mem : zone->max_mem;
  421. if (zone->used_mem > limit)
  422. goto out_unlock;
  423. }
  424. if (reserve) {
  425. for (i = 0; i < glob->num_zones; ++i) {
  426. zone = glob->zones[i];
  427. if (single_zone && zone != single_zone)
  428. continue;
  429. zone->used_mem += amount;
  430. }
  431. }
  432. ret = 0;
  433. out_unlock:
  434. spin_unlock(&glob->lock);
  435. ttm_check_swapping(glob);
  436. return ret;
  437. }
  438. static int ttm_mem_global_alloc_zone(struct ttm_mem_global *glob,
  439. struct ttm_mem_zone *single_zone,
  440. uint64_t memory,
  441. bool no_wait, bool interruptible)
  442. {
  443. int count = TTM_MEMORY_ALLOC_RETRIES;
  444. while (unlikely(ttm_mem_global_reserve(glob,
  445. single_zone,
  446. memory, true)
  447. != 0)) {
  448. if (no_wait)
  449. return -ENOMEM;
  450. if (unlikely(count-- == 0))
  451. return -ENOMEM;
  452. ttm_shrink(glob, false, memory + (memory >> 2) + 16);
  453. }
  454. return 0;
  455. }
  456. int ttm_mem_global_alloc(struct ttm_mem_global *glob, uint64_t memory,
  457. bool no_wait, bool interruptible)
  458. {
  459. /**
  460. * Normal allocations of kernel memory are registered in
  461. * all zones.
  462. */
  463. return ttm_mem_global_alloc_zone(glob, NULL, memory, no_wait,
  464. interruptible);
  465. }
  466. EXPORT_SYMBOL(ttm_mem_global_alloc);
  467. int ttm_mem_global_alloc_page(struct ttm_mem_global *glob,
  468. struct page *page,
  469. bool no_wait, bool interruptible)
  470. {
  471. struct ttm_mem_zone *zone = NULL;
  472. /**
  473. * Page allocations may be registed in a single zone
  474. * only if highmem or !dma32.
  475. */
  476. #ifdef CONFIG_HIGHMEM
  477. if (PageHighMem(page) && glob->zone_highmem != NULL)
  478. zone = glob->zone_highmem;
  479. #else
  480. if (glob->zone_dma32 && page_to_pfn(page) > 0x00100000UL)
  481. zone = glob->zone_kernel;
  482. #endif
  483. return ttm_mem_global_alloc_zone(glob, zone, PAGE_SIZE, no_wait,
  484. interruptible);
  485. }
  486. void ttm_mem_global_free_page(struct ttm_mem_global *glob, struct page *page)
  487. {
  488. struct ttm_mem_zone *zone = NULL;
  489. #ifdef CONFIG_HIGHMEM
  490. if (PageHighMem(page) && glob->zone_highmem != NULL)
  491. zone = glob->zone_highmem;
  492. #else
  493. if (glob->zone_dma32 && page_to_pfn(page) > 0x00100000UL)
  494. zone = glob->zone_kernel;
  495. #endif
  496. ttm_mem_global_free_zone(glob, zone, PAGE_SIZE);
  497. }
  498. size_t ttm_round_pot(size_t size)
  499. {
  500. if ((size & (size - 1)) == 0)
  501. return size;
  502. else if (size > PAGE_SIZE)
  503. return PAGE_ALIGN(size);
  504. else {
  505. size_t tmp_size = 4;
  506. while (tmp_size < size)
  507. tmp_size <<= 1;
  508. return tmp_size;
  509. }
  510. return 0;
  511. }
  512. EXPORT_SYMBOL(ttm_round_pot);