ttm_memory.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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. if (unlikely(!zone))
  212. return -ENOMEM;
  213. mem = si->totalram - si->totalhigh;
  214. mem *= si->mem_unit;
  215. zone->name = "kernel";
  216. zone->zone_mem = mem;
  217. zone->max_mem = mem >> 1;
  218. zone->emer_mem = (mem >> 1) + (mem >> 2);
  219. zone->swap_limit = zone->max_mem - (mem >> 3);
  220. zone->used_mem = 0;
  221. zone->glob = glob;
  222. glob->zone_kernel = zone;
  223. glob->zones[glob->num_zones++] = zone;
  224. kobject_init(&zone->kobj, &ttm_mem_zone_kobj_type);
  225. return kobject_add(&zone->kobj, &glob->kobj, zone->name);
  226. }
  227. #ifdef CONFIG_HIGHMEM
  228. static int ttm_mem_init_highmem_zone(struct ttm_mem_global *glob,
  229. const struct sysinfo *si)
  230. {
  231. struct ttm_mem_zone *zone = kzalloc(sizeof(*zone), GFP_KERNEL);
  232. uint64_t mem;
  233. if (unlikely(!zone))
  234. return -ENOMEM;
  235. if (si->totalhigh == 0)
  236. return 0;
  237. mem = si->totalram;
  238. mem *= si->mem_unit;
  239. zone->name = "highmem";
  240. zone->zone_mem = mem;
  241. zone->max_mem = mem >> 1;
  242. zone->emer_mem = (mem >> 1) + (mem >> 2);
  243. zone->swap_limit = zone->max_mem - (mem >> 3);
  244. zone->used_mem = 0;
  245. zone->glob = glob;
  246. glob->zone_highmem = zone;
  247. glob->zones[glob->num_zones++] = zone;
  248. kobject_init(&zone->kobj, &ttm_mem_zone_kobj_type);
  249. return kobject_add(&zone->kobj, &glob->kobj, zone->name);
  250. }
  251. #else
  252. static int ttm_mem_init_dma32_zone(struct ttm_mem_global *glob,
  253. const struct sysinfo *si)
  254. {
  255. struct ttm_mem_zone *zone = kzalloc(sizeof(*zone), GFP_KERNEL);
  256. uint64_t mem;
  257. if (unlikely(!zone))
  258. return -ENOMEM;
  259. mem = si->totalram;
  260. mem *= si->mem_unit;
  261. /**
  262. * No special dma32 zone needed.
  263. */
  264. if (mem <= ((uint64_t) 1ULL << 32))
  265. return 0;
  266. /*
  267. * Limit max dma32 memory to 4GB for now
  268. * until we can figure out how big this
  269. * zone really is.
  270. */
  271. mem = ((uint64_t) 1ULL << 32);
  272. zone->name = "dma32";
  273. zone->zone_mem = mem;
  274. zone->max_mem = mem >> 1;
  275. zone->emer_mem = (mem >> 1) + (mem >> 2);
  276. zone->swap_limit = zone->max_mem - (mem >> 3);
  277. zone->used_mem = 0;
  278. zone->glob = glob;
  279. glob->zone_dma32 = zone;
  280. glob->zones[glob->num_zones++] = zone;
  281. kobject_init(&zone->kobj, &ttm_mem_zone_kobj_type);
  282. return kobject_add(&zone->kobj, &glob->kobj, zone->name);
  283. }
  284. #endif
  285. int ttm_mem_global_init(struct ttm_mem_global *glob)
  286. {
  287. struct sysinfo si;
  288. int ret;
  289. int i;
  290. struct ttm_mem_zone *zone;
  291. spin_lock_init(&glob->lock);
  292. glob->swap_queue = create_singlethread_workqueue("ttm_swap");
  293. INIT_WORK(&glob->work, ttm_shrink_work);
  294. init_waitqueue_head(&glob->queue);
  295. kobject_init(&glob->kobj, &ttm_mem_glob_kobj_type);
  296. ret = kobject_add(&glob->kobj,
  297. ttm_get_kobj(),
  298. "memory_accounting");
  299. if (unlikely(ret != 0))
  300. goto out_no_zone;
  301. si_meminfo(&si);
  302. ret = ttm_mem_init_kernel_zone(glob, &si);
  303. if (unlikely(ret != 0))
  304. goto out_no_zone;
  305. #ifdef CONFIG_HIGHMEM
  306. ret = ttm_mem_init_highmem_zone(glob, &si);
  307. if (unlikely(ret != 0))
  308. goto out_no_zone;
  309. #else
  310. ret = ttm_mem_init_dma32_zone(glob, &si);
  311. if (unlikely(ret != 0))
  312. goto out_no_zone;
  313. #endif
  314. for (i = 0; i < glob->num_zones; ++i) {
  315. zone = glob->zones[i];
  316. printk(KERN_INFO TTM_PFX
  317. "Zone %7s: Available graphics memory: %llu kiB.\n",
  318. zone->name, (unsigned long long) zone->max_mem >> 10);
  319. }
  320. return 0;
  321. out_no_zone:
  322. ttm_mem_global_release(glob);
  323. return ret;
  324. }
  325. EXPORT_SYMBOL(ttm_mem_global_init);
  326. void ttm_mem_global_release(struct ttm_mem_global *glob)
  327. {
  328. unsigned int i;
  329. struct ttm_mem_zone *zone;
  330. flush_workqueue(glob->swap_queue);
  331. destroy_workqueue(glob->swap_queue);
  332. glob->swap_queue = NULL;
  333. for (i = 0; i < glob->num_zones; ++i) {
  334. zone = glob->zones[i];
  335. kobject_del(&zone->kobj);
  336. kobject_put(&zone->kobj);
  337. }
  338. kobject_del(&glob->kobj);
  339. kobject_put(&glob->kobj);
  340. }
  341. EXPORT_SYMBOL(ttm_mem_global_release);
  342. static void ttm_check_swapping(struct ttm_mem_global *glob)
  343. {
  344. bool needs_swapping = false;
  345. unsigned int i;
  346. struct ttm_mem_zone *zone;
  347. spin_lock(&glob->lock);
  348. for (i = 0; i < glob->num_zones; ++i) {
  349. zone = glob->zones[i];
  350. if (zone->used_mem > zone->swap_limit) {
  351. needs_swapping = true;
  352. break;
  353. }
  354. }
  355. spin_unlock(&glob->lock);
  356. if (unlikely(needs_swapping))
  357. (void)queue_work(glob->swap_queue, &glob->work);
  358. }
  359. static void ttm_mem_global_free_zone(struct ttm_mem_global *glob,
  360. struct ttm_mem_zone *single_zone,
  361. uint64_t amount)
  362. {
  363. unsigned int i;
  364. struct ttm_mem_zone *zone;
  365. spin_lock(&glob->lock);
  366. for (i = 0; i < glob->num_zones; ++i) {
  367. zone = glob->zones[i];
  368. if (single_zone && zone != single_zone)
  369. continue;
  370. zone->used_mem -= amount;
  371. }
  372. spin_unlock(&glob->lock);
  373. }
  374. void ttm_mem_global_free(struct ttm_mem_global *glob,
  375. uint64_t amount)
  376. {
  377. return ttm_mem_global_free_zone(glob, NULL, amount);
  378. }
  379. static int ttm_mem_global_reserve(struct ttm_mem_global *glob,
  380. struct ttm_mem_zone *single_zone,
  381. uint64_t amount, bool reserve)
  382. {
  383. uint64_t limit;
  384. int ret = -ENOMEM;
  385. unsigned int i;
  386. struct ttm_mem_zone *zone;
  387. spin_lock(&glob->lock);
  388. for (i = 0; i < glob->num_zones; ++i) {
  389. zone = glob->zones[i];
  390. if (single_zone && zone != single_zone)
  391. continue;
  392. limit = (capable(CAP_SYS_ADMIN)) ?
  393. zone->emer_mem : zone->max_mem;
  394. if (zone->used_mem > limit)
  395. goto out_unlock;
  396. }
  397. if (reserve) {
  398. for (i = 0; i < glob->num_zones; ++i) {
  399. zone = glob->zones[i];
  400. if (single_zone && zone != single_zone)
  401. continue;
  402. zone->used_mem += amount;
  403. }
  404. }
  405. ret = 0;
  406. out_unlock:
  407. spin_unlock(&glob->lock);
  408. ttm_check_swapping(glob);
  409. return ret;
  410. }
  411. static int ttm_mem_global_alloc_zone(struct ttm_mem_global *glob,
  412. struct ttm_mem_zone *single_zone,
  413. uint64_t memory,
  414. bool no_wait, bool interruptible)
  415. {
  416. int count = TTM_MEMORY_ALLOC_RETRIES;
  417. while (unlikely(ttm_mem_global_reserve(glob,
  418. single_zone,
  419. memory, true)
  420. != 0)) {
  421. if (no_wait)
  422. return -ENOMEM;
  423. if (unlikely(count-- == 0))
  424. return -ENOMEM;
  425. ttm_shrink(glob, false, memory + (memory >> 2) + 16);
  426. }
  427. return 0;
  428. }
  429. int ttm_mem_global_alloc(struct ttm_mem_global *glob, uint64_t memory,
  430. bool no_wait, bool interruptible)
  431. {
  432. /**
  433. * Normal allocations of kernel memory are registered in
  434. * all zones.
  435. */
  436. return ttm_mem_global_alloc_zone(glob, NULL, memory, no_wait,
  437. interruptible);
  438. }
  439. int ttm_mem_global_alloc_page(struct ttm_mem_global *glob,
  440. struct page *page,
  441. bool no_wait, bool interruptible)
  442. {
  443. struct ttm_mem_zone *zone = NULL;
  444. /**
  445. * Page allocations may be registed in a single zone
  446. * only if highmem or !dma32.
  447. */
  448. #ifdef CONFIG_HIGHMEM
  449. if (PageHighMem(page) && glob->zone_highmem != NULL)
  450. zone = glob->zone_highmem;
  451. #else
  452. if (glob->zone_dma32 && page_to_pfn(page) > 0x00100000UL)
  453. zone = glob->zone_kernel;
  454. #endif
  455. return ttm_mem_global_alloc_zone(glob, zone, PAGE_SIZE, no_wait,
  456. interruptible);
  457. }
  458. void ttm_mem_global_free_page(struct ttm_mem_global *glob, struct page *page)
  459. {
  460. struct ttm_mem_zone *zone = NULL;
  461. #ifdef CONFIG_HIGHMEM
  462. if (PageHighMem(page) && glob->zone_highmem != NULL)
  463. zone = glob->zone_highmem;
  464. #else
  465. if (glob->zone_dma32 && page_to_pfn(page) > 0x00100000UL)
  466. zone = glob->zone_kernel;
  467. #endif
  468. ttm_mem_global_free_zone(glob, zone, PAGE_SIZE);
  469. }
  470. size_t ttm_round_pot(size_t size)
  471. {
  472. if ((size & (size - 1)) == 0)
  473. return size;
  474. else if (size > PAGE_SIZE)
  475. return PAGE_ALIGN(size);
  476. else {
  477. size_t tmp_size = 4;
  478. while (tmp_size < size)
  479. tmp_size <<= 1;
  480. return tmp_size;
  481. }
  482. return 0;
  483. }