mmu_context_hash64.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 "icswx.h"
  25. static DEFINE_SPINLOCK(mmu_context_lock);
  26. static DEFINE_IDA(mmu_context_ida);
  27. int __init_new_context(void)
  28. {
  29. int index;
  30. int err;
  31. again:
  32. if (!ida_pre_get(&mmu_context_ida, GFP_KERNEL))
  33. return -ENOMEM;
  34. spin_lock(&mmu_context_lock);
  35. err = ida_get_new_above(&mmu_context_ida, 1, &index);
  36. spin_unlock(&mmu_context_lock);
  37. if (err == -EAGAIN)
  38. goto again;
  39. else if (err)
  40. return err;
  41. if (index > MAX_USER_CONTEXT) {
  42. spin_lock(&mmu_context_lock);
  43. ida_remove(&mmu_context_ida, index);
  44. spin_unlock(&mmu_context_lock);
  45. return -ENOMEM;
  46. }
  47. return index;
  48. }
  49. EXPORT_SYMBOL_GPL(__init_new_context);
  50. int init_new_context(struct task_struct *tsk, struct mm_struct *mm)
  51. {
  52. int index;
  53. index = __init_new_context();
  54. if (index < 0)
  55. return index;
  56. /* The old code would re-promote on fork, we don't do that
  57. * when using slices as it could cause problem promoting slices
  58. * that have been forced down to 4K
  59. */
  60. if (slice_mm_new_context(mm))
  61. slice_set_user_psize(mm, mmu_virtual_psize);
  62. subpage_prot_init_new_context(mm);
  63. mm->context.id = index;
  64. #ifdef CONFIG_PPC_ICSWX
  65. mm->context.cop_lockp = kmalloc(sizeof(spinlock_t), GFP_KERNEL);
  66. if (!mm->context.cop_lockp) {
  67. __destroy_context(index);
  68. subpage_prot_free(mm);
  69. mm->context.id = MMU_NO_CONTEXT;
  70. return -ENOMEM;
  71. }
  72. spin_lock_init(mm->context.cop_lockp);
  73. #endif /* CONFIG_PPC_ICSWX */
  74. return 0;
  75. }
  76. void __destroy_context(int context_id)
  77. {
  78. spin_lock(&mmu_context_lock);
  79. ida_remove(&mmu_context_ida, context_id);
  80. spin_unlock(&mmu_context_lock);
  81. }
  82. EXPORT_SYMBOL_GPL(__destroy_context);
  83. void destroy_context(struct mm_struct *mm)
  84. {
  85. #ifdef CONFIG_PPC_ICSWX
  86. drop_cop(mm->context.acop, mm);
  87. kfree(mm->context.cop_lockp);
  88. mm->context.cop_lockp = NULL;
  89. #endif /* CONFIG_PPC_ICSWX */
  90. __destroy_context(mm->context.id);
  91. subpage_prot_free(mm);
  92. mm->context.id = MMU_NO_CONTEXT;
  93. }