ttm_memory.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  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. #define pr_fmt(fmt) "[TTM] " fmt
  28. #include <drm/ttm/ttm_memory.h>
  29. #include <drm/ttm/ttm_module.h>
  30. #include <drm/ttm/ttm_page_alloc.h>
  31. #include <linux/spinlock.h>
  32. #include <linux/sched.h>
  33. #include <linux/wait.h>
  34. #include <linux/mm.h>
  35. #include <linux/module.h>
  36. #include <linux/slab.h>
  37. #define TTM_MEMORY_ALLOC_RETRIES 4
  38. struct ttm_mem_zone {
  39. struct kobject kobj;
  40. struct ttm_mem_global *glob;
  41. const char *name;
  42. uint64_t zone_mem;
  43. uint64_t emer_mem;
  44. uint64_t max_mem;
  45. uint64_t swap_limit;
  46. uint64_t used_mem;
  47. };
  48. static struct attribute ttm_mem_sys = {
  49. .name = "zone_memory",
  50. .mode = S_IRUGO
  51. };
  52. static struct attribute ttm_mem_emer = {
  53. .name = "emergency_memory",
  54. .mode = S_IRUGO | S_IWUSR
  55. };
  56. static struct attribute ttm_mem_max = {
  57. .name = "available_memory",
  58. .mode = S_IRUGO | S_IWUSR
  59. };
  60. static struct attribute ttm_mem_swap = {
  61. .name = "swap_limit",
  62. .mode = S_IRUGO | S_IWUSR
  63. };
  64. static struct attribute ttm_mem_used = {
  65. .name = "used_memory",
  66. .mode = S_IRUGO
  67. };
  68. static void ttm_mem_zone_kobj_release(struct kobject *kobj)
  69. {
  70. struct ttm_mem_zone *zone =
  71. container_of(kobj, struct ttm_mem_zone, kobj);
  72. pr_info("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, "%s",
  259. zone->name);
  260. if (unlikely(ret != 0)) {
  261. kobject_put(&zone->kobj);
  262. return ret;
  263. }
  264. glob->zones[glob->num_zones++] = zone;
  265. return 0;
  266. }
  267. #else
  268. static int ttm_mem_init_dma32_zone(struct ttm_mem_global *glob,
  269. const struct sysinfo *si)
  270. {
  271. struct ttm_mem_zone *zone = kzalloc(sizeof(*zone), GFP_KERNEL);
  272. uint64_t mem;
  273. int ret;
  274. if (unlikely(!zone))
  275. return -ENOMEM;
  276. mem = si->totalram;
  277. mem *= si->mem_unit;
  278. /**
  279. * No special dma32 zone needed.
  280. */
  281. if (mem <= ((uint64_t) 1ULL << 32)) {
  282. kfree(zone);
  283. return 0;
  284. }
  285. /*
  286. * Limit max dma32 memory to 4GB for now
  287. * until we can figure out how big this
  288. * zone really is.
  289. */
  290. mem = ((uint64_t) 1ULL << 32);
  291. zone->name = "dma32";
  292. zone->zone_mem = mem;
  293. zone->max_mem = mem >> 1;
  294. zone->emer_mem = (mem >> 1) + (mem >> 2);
  295. zone->swap_limit = zone->max_mem - (mem >> 3);
  296. zone->used_mem = 0;
  297. zone->glob = glob;
  298. glob->zone_dma32 = zone;
  299. ret = kobject_init_and_add(
  300. &zone->kobj, &ttm_mem_zone_kobj_type, &glob->kobj, zone->name);
  301. if (unlikely(ret != 0)) {
  302. kobject_put(&zone->kobj);
  303. return ret;
  304. }
  305. glob->zones[glob->num_zones++] = zone;
  306. return 0;
  307. }
  308. #endif
  309. int ttm_mem_global_init(struct ttm_mem_global *glob)
  310. {
  311. struct sysinfo si;
  312. int ret;
  313. int i;
  314. struct ttm_mem_zone *zone;
  315. spin_lock_init(&glob->lock);
  316. glob->swap_queue = create_singlethread_workqueue("ttm_swap");
  317. INIT_WORK(&glob->work, ttm_shrink_work);
  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. pr_info("Zone %7s: Available graphics memory: %llu kiB\n",
  340. zone->name, (unsigned long long)zone->max_mem >> 10);
  341. }
  342. ttm_page_alloc_init(glob, glob->zone_kernel->max_mem/(2*PAGE_SIZE));
  343. ttm_dma_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. ttm_dma_page_alloc_fini();
  357. flush_workqueue(glob->swap_queue);
  358. destroy_workqueue(glob->swap_queue);
  359. glob->swap_queue = NULL;
  360. for (i = 0; i < glob->num_zones; ++i) {
  361. zone = glob->zones[i];
  362. kobject_del(&zone->kobj);
  363. kobject_put(&zone->kobj);
  364. }
  365. kobject_del(&glob->kobj);
  366. kobject_put(&glob->kobj);
  367. }
  368. EXPORT_SYMBOL(ttm_mem_global_release);
  369. static void ttm_check_swapping(struct ttm_mem_global *glob)
  370. {
  371. bool needs_swapping = false;
  372. unsigned int i;
  373. struct ttm_mem_zone *zone;
  374. spin_lock(&glob->lock);
  375. for (i = 0; i < glob->num_zones; ++i) {
  376. zone = glob->zones[i];
  377. if (zone->used_mem > zone->swap_limit) {
  378. needs_swapping = true;
  379. break;
  380. }
  381. }
  382. spin_unlock(&glob->lock);
  383. if (unlikely(needs_swapping))
  384. (void)queue_work(glob->swap_queue, &glob->work);
  385. }
  386. static void ttm_mem_global_free_zone(struct ttm_mem_global *glob,
  387. struct ttm_mem_zone *single_zone,
  388. uint64_t amount)
  389. {
  390. unsigned int i;
  391. struct ttm_mem_zone *zone;
  392. spin_lock(&glob->lock);
  393. for (i = 0; i < glob->num_zones; ++i) {
  394. zone = glob->zones[i];
  395. if (single_zone && zone != single_zone)
  396. continue;
  397. zone->used_mem -= amount;
  398. }
  399. spin_unlock(&glob->lock);
  400. }
  401. void ttm_mem_global_free(struct ttm_mem_global *glob,
  402. uint64_t amount)
  403. {
  404. return ttm_mem_global_free_zone(glob, NULL, amount);
  405. }
  406. EXPORT_SYMBOL(ttm_mem_global_free);
  407. static int ttm_mem_global_reserve(struct ttm_mem_global *glob,
  408. struct ttm_mem_zone *single_zone,
  409. uint64_t amount, bool reserve)
  410. {
  411. uint64_t limit;
  412. int ret = -ENOMEM;
  413. unsigned int i;
  414. struct ttm_mem_zone *zone;
  415. spin_lock(&glob->lock);
  416. for (i = 0; i < glob->num_zones; ++i) {
  417. zone = glob->zones[i];
  418. if (single_zone && zone != single_zone)
  419. continue;
  420. limit = (capable(CAP_SYS_ADMIN)) ?
  421. zone->emer_mem : zone->max_mem;
  422. if (zone->used_mem > limit)
  423. goto out_unlock;
  424. }
  425. if (reserve) {
  426. for (i = 0; i < glob->num_zones; ++i) {
  427. zone = glob->zones[i];
  428. if (single_zone && zone != single_zone)
  429. continue;
  430. zone->used_mem += amount;
  431. }
  432. }
  433. ret = 0;
  434. out_unlock:
  435. spin_unlock(&glob->lock);
  436. ttm_check_swapping(glob);
  437. return ret;
  438. }
  439. static int ttm_mem_global_alloc_zone(struct ttm_mem_global *glob,
  440. struct ttm_mem_zone *single_zone,
  441. uint64_t memory,
  442. bool no_wait, bool interruptible)
  443. {
  444. int count = TTM_MEMORY_ALLOC_RETRIES;
  445. while (unlikely(ttm_mem_global_reserve(glob,
  446. single_zone,
  447. memory, true)
  448. != 0)) {
  449. if (no_wait)
  450. return -ENOMEM;
  451. if (unlikely(count-- == 0))
  452. return -ENOMEM;
  453. ttm_shrink(glob, false, memory + (memory >> 2) + 16);
  454. }
  455. return 0;
  456. }
  457. int ttm_mem_global_alloc(struct ttm_mem_global *glob, uint64_t memory,
  458. bool no_wait, bool interruptible)
  459. {
  460. /**
  461. * Normal allocations of kernel memory are registered in
  462. * all zones.
  463. */
  464. return ttm_mem_global_alloc_zone(glob, NULL, memory, no_wait,
  465. interruptible);
  466. }
  467. EXPORT_SYMBOL(ttm_mem_global_alloc);
  468. int ttm_mem_global_alloc_page(struct ttm_mem_global *glob,
  469. struct page *page,
  470. bool no_wait, bool interruptible)
  471. {
  472. struct ttm_mem_zone *zone = NULL;
  473. /**
  474. * Page allocations may be registed in a single zone
  475. * only if highmem or !dma32.
  476. */
  477. #ifdef CONFIG_HIGHMEM
  478. if (PageHighMem(page) && glob->zone_highmem != NULL)
  479. zone = glob->zone_highmem;
  480. #else
  481. if (glob->zone_dma32 && page_to_pfn(page) > 0x00100000UL)
  482. zone = glob->zone_kernel;
  483. #endif
  484. return ttm_mem_global_alloc_zone(glob, zone, PAGE_SIZE, no_wait,
  485. interruptible);
  486. }
  487. void ttm_mem_global_free_page(struct ttm_mem_global *glob, struct page *page)
  488. {
  489. struct ttm_mem_zone *zone = NULL;
  490. #ifdef CONFIG_HIGHMEM
  491. if (PageHighMem(page) && glob->zone_highmem != NULL)
  492. zone = glob->zone_highmem;
  493. #else
  494. if (glob->zone_dma32 && page_to_pfn(page) > 0x00100000UL)
  495. zone = glob->zone_kernel;
  496. #endif
  497. ttm_mem_global_free_zone(glob, zone, PAGE_SIZE);
  498. }
  499. size_t ttm_round_pot(size_t size)
  500. {
  501. if ((size & (size - 1)) == 0)
  502. return size;
  503. else if (size > PAGE_SIZE)
  504. return PAGE_ALIGN(size);
  505. else {
  506. size_t tmp_size = 4;
  507. while (tmp_size < size)
  508. tmp_size <<= 1;
  509. return tmp_size;
  510. }
  511. return 0;
  512. }
  513. EXPORT_SYMBOL(ttm_round_pot);