ttm_memory.c 14 KB

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