mmu_context_hash64.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * MMU context allocation for 64-bit kernels.
  3. *
  4. * Copyright (C) 2004 Anton Blanchard, IBM Corp. <anton@samba.org>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. *
  11. */
  12. #include <linux/sched.h>
  13. #include <linux/kernel.h>
  14. #include <linux/errno.h>
  15. #include <linux/string.h>
  16. #include <linux/types.h>
  17. #include <linux/mm.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/idr.h>
  20. #include <linux/export.h>
  21. #include <linux/gfp.h>
  22. #include <linux/slab.h>
  23. #include <asm/mmu_context.h>
  24. #include <asm/pgalloc.h>
  25. #include "icswx.h"
  26. static DEFINE_SPINLOCK(mmu_context_lock);
  27. static DEFINE_IDA(mmu_context_ida);
  28. int __init_new_context(void)
  29. {
  30. int index;
  31. int err;
  32. again:
  33. if (!ida_pre_get(&mmu_context_ida, GFP_KERNEL))
  34. return -ENOMEM;
  35. spin_lock(&mmu_context_lock);
  36. err = ida_get_new_above(&mmu_context_ida, 1, &index);
  37. spin_unlock(&mmu_context_lock);
  38. if (err == -EAGAIN)
  39. goto again;
  40. else if (err)
  41. return err;
  42. if (index > MAX_USER_CONTEXT) {
  43. spin_lock(&mmu_context_lock);
  44. ida_remove(&mmu_context_ida, index);
  45. spin_unlock(&mmu_context_lock);
  46. return -ENOMEM;
  47. }
  48. return index;
  49. }
  50. EXPORT_SYMBOL_GPL(__init_new_context);
  51. int init_new_context(struct task_struct *tsk, struct mm_struct *mm)
  52. {
  53. int index;
  54. index = __init_new_context();
  55. if (index < 0)
  56. return index;
  57. /* The old code would re-promote on fork, we don't do that
  58. * when using slices as it could cause problem promoting slices
  59. * that have been forced down to 4K
  60. */
  61. if (slice_mm_new_context(mm))
  62. slice_set_user_psize(mm, mmu_virtual_psize);
  63. subpage_prot_init_new_context(mm);
  64. mm->context.id = index;
  65. #ifdef CONFIG_PPC_ICSWX
  66. mm->context.cop_lockp = kmalloc(sizeof(spinlock_t), GFP_KERNEL);
  67. if (!mm->context.cop_lockp) {
  68. __destroy_context(index);
  69. subpage_prot_free(mm);
  70. mm->context.id = MMU_NO_CONTEXT;
  71. return -ENOMEM;
  72. }
  73. spin_lock_init(mm->context.cop_lockp);
  74. #endif /* CONFIG_PPC_ICSWX */
  75. #ifdef CONFIG_PPC_64K_PAGES
  76. mm->context.pte_frag = NULL;
  77. #endif
  78. return 0;
  79. }
  80. void __destroy_context(int context_id)
  81. {
  82. spin_lock(&mmu_context_lock);
  83. ida_remove(&mmu_context_ida, context_id);
  84. spin_unlock(&mmu_context_lock);
  85. }
  86. EXPORT_SYMBOL_GPL(__destroy_context);
  87. #ifdef CONFIG_PPC_64K_PAGES
  88. static void destroy_pagetable_page(struct mm_struct *mm)
  89. {
  90. int count;
  91. void *pte_frag;
  92. struct page *page;
  93. pte_frag = mm->context.pte_frag;
  94. if (!pte_frag)
  95. return;
  96. page = virt_to_page(pte_frag);
  97. /* drop all the pending references */
  98. count = ((unsigned long)pte_frag & ~PAGE_MASK) >> PTE_FRAG_SIZE_SHIFT;
  99. /* We allow PTE_FRAG_NR fragments from a PTE page */
  100. count = atomic_sub_return(PTE_FRAG_NR - count, &page->_count);
  101. if (!count) {
  102. pgtable_page_dtor(page);
  103. free_hot_cold_page(page, 0);
  104. }
  105. }
  106. #else
  107. static inline void destroy_pagetable_page(struct mm_struct *mm)
  108. {
  109. return;
  110. }
  111. #endif
  112. void destroy_context(struct mm_struct *mm)
  113. {
  114. #ifdef CONFIG_PPC_ICSWX
  115. drop_cop(mm->context.acop, mm);
  116. kfree(mm->context.cop_lockp);
  117. mm->context.cop_lockp = NULL;
  118. #endif /* CONFIG_PPC_ICSWX */
  119. destroy_pagetable_page(mm);
  120. __destroy_context(mm->context.id);
  121. subpage_prot_free(mm);
  122. mm->context.id = MMU_NO_CONTEXT;
  123. }