ttm_memory.c 14 KB

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