mmu_context_hash64.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 <asm/mmu_context.h>
  21. static DEFINE_SPINLOCK(mmu_context_lock);
  22. static DEFINE_IDR(mmu_context_idr);
  23. /*
  24. * The proto-VSID space has 2^35 - 1 segments available for user mappings.
  25. * Each segment contains 2^28 bytes. Each context maps 2^44 bytes,
  26. * so we can support 2^19-1 contexts (19 == 35 + 28 - 44).
  27. */
  28. #define NO_CONTEXT 0
  29. #define MAX_CONTEXT ((1UL << 19) - 1)
  30. int init_new_context(struct task_struct *tsk, struct mm_struct *mm)
  31. {
  32. int index;
  33. int err;
  34. again:
  35. if (!idr_pre_get(&mmu_context_idr, GFP_KERNEL))
  36. return -ENOMEM;
  37. spin_lock(&mmu_context_lock);
  38. err = idr_get_new_above(&mmu_context_idr, NULL, 1, &index);
  39. spin_unlock(&mmu_context_lock);
  40. if (err == -EAGAIN)
  41. goto again;
  42. else if (err)
  43. return err;
  44. if (index > MAX_CONTEXT) {
  45. spin_lock(&mmu_context_lock);
  46. idr_remove(&mmu_context_idr, index);
  47. spin_unlock(&mmu_context_lock);
  48. return -ENOMEM;
  49. }
  50. /* The old code would re-promote on fork, we don't do that
  51. * when using slices as it could cause problem promoting slices
  52. * that have been forced down to 4K
  53. */
  54. if (slice_mm_new_context(mm))
  55. slice_set_user_psize(mm, mmu_virtual_psize);
  56. mm->context.id = index;
  57. return 0;
  58. }
  59. void destroy_context(struct mm_struct *mm)
  60. {
  61. spin_lock(&mmu_context_lock);
  62. idr_remove(&mmu_context_idr, mm->context.id);
  63. spin_unlock(&mmu_context_lock);
  64. mm->context.id = NO_CONTEXT;
  65. }