| @@ -12,6 +12,7 @@ · 11 unmodified lines |
| 12 | 12 | #include <linux/string.h> |
| 13 | 13 | #include <linux/mm.h> |
| 14 | 14 | #include <linux/smp.h> |
| 15 | +#include <linux/slab.h> |
| 15 | 16 | #include <linux/vmalloc.h> |
| 16 | 17 | #include <linux/uaccess.h> |
| 17 | 18 | |
| @@ -20,82 +21,82 @@ · 2 unmodified lines |
| 20 | 21 | #include <asm/mmu_context.h> |
| 21 | 22 | #include <asm/syscalls.h> |
| 22 | 23 | |
| 23 | | −#ifdef CONFIG_SMP |
| 24 | +/* context.lock is held for us, so we don't need any locking. */ |
| 24 | 25 | static void flush_ldt(void *current_mm) |
| 25 | 26 | { |
| 26 | | − if (current->active_mm == current_mm) |
| 27 | | − load_LDT(¤t->active_mm->context); |
| 27 | + mm_context_t *pc; |
| 28 | + |
| 29 | + if (current->active_mm != current_mm) |
| 30 | + return; |
| 31 | + |
| 32 | + pc = ¤t->active_mm->context; |
| 33 | + set_ldt(pc->ldt->entries, pc->ldt->size); |
| 28 | 34 | } |
| 29 | | −#endif |
| 30 | 35 | |
| 31 | | −static int alloc_ldt(mm_context_t *pc, int mincount, int reload) |
| 36 | +/* The caller must call finalize_ldt_struct on the result. LDT starts zeroed. */ |
| 37 | +static struct ldt_struct *alloc_ldt_struct(int size) |
| 32 | 38 | { |
| 33 | | − void *oldldt, *newldt; |
| 34 | | − int oldsize; |
| 35 | | − |
| 36 | | − if (mincount <= pc->size) |
| 37 | | − return 0; |
| 38 | | − oldsize = pc->size; |
| 39 | | − mincount = (mincount + (PAGE_SIZE / LDT_ENTRY_SIZE - 1)) & |
| 40 | | − (~(PAGE_SIZE / LDT_ENTRY_SIZE - 1)); |
| 41 | | − if (mincount * LDT_ENTRY_SIZE > PAGE_SIZE) |
| 42 | | − newldt = vmalloc(mincount * LDT_ENTRY_SIZE); |
| 39 | + struct ldt_struct *new_ldt; |
| 40 | + int alloc_size; |
| 41 | + |
| 42 | + if (size > LDT_ENTRIES) |
| 43 | + return NULL; |
| 44 | + |
| 45 | + new_ldt = kmalloc(sizeof(struct ldt_struct), GFP_KERNEL); |
| 46 | + if (!new_ldt) |
| 47 | + return NULL; |
| 48 | + |
| 49 | + BUILD_BUG_ON(LDT_ENTRY_SIZE != sizeof(struct desc_struct)); |
| 50 | + alloc_size = size * LDT_ENTRY_SIZE; |
| 51 | + |
| 52 | + /* |
| 53 | + * Xen is very picky: it requires a page-aligned LDT that has no |
| 54 | + * trailing nonzero bytes in any page that contains LDT descriptors. |
| 55 | + * Keep it simple: zero the whole allocation and never allocate less |
| 56 | + * than PAGE_SIZE. |
| 57 | + */ |
| 58 | + if (alloc_size > PAGE_SIZE) |
| 59 | + new_ldt->entries = vzalloc(alloc_size); |
| 43 | 60 | else |
| 44 | | − newldt = (void *)__get_free_page(GFP_KERNEL); |
| 45 | | − |
| 46 | | − if (!newldt) |
| 47 | | − return -ENOMEM; |
| 61 | + new_ldt->entries = kzalloc(PAGE_SIZE, GFP_KERNEL); |
| 48 | 62 | |
| 49 | | − if (oldsize) |
| 50 | | − memcpy(newldt, pc->ldt, oldsize * LDT_ENTRY_SIZE); |
| 51 | | − oldldt = pc->ldt; |
| 52 | | − memset(newldt + oldsize * LDT_ENTRY_SIZE, 0, |
| 53 | | − (mincount - oldsize) * LDT_ENTRY_SIZE); |
| 63 | + if (!new_ldt->entries) { |
| 64 | + kfree(new_ldt); |
| 65 | + return NULL; |
| 66 | + } |
| 54 | 67 | |
| 55 | | − paravirt_alloc_ldt(newldt, mincount); |
| 68 | + new_ldt->size = size; |
| 69 | + return new_ldt; |
| 70 | +} |
| 56 | 71 | |
| 57 | | −#ifdef CONFIG_X86_64 |
| 58 | | − /* CHECKME: Do we really need this ? */ |
| 59 | | − wmb(); |
| 60 | | −#endif |
| 61 | | − pc->ldt = newldt; |
| 62 | | − wmb(); |
| 63 | | − pc->size = mincount; |
| 64 | | − wmb(); |
| 65 | | − |
| 66 | | − if (reload) { |
| 67 | | −#ifdef CONFIG_SMP |
| 68 | | − preempt_disable(); |
| 69 | | − load_LDT(pc); |
| 70 | | − if (!cpumask_equal(mm_cpumask(current->mm), |
| 71 | | − cpumask_of(smp_processor_id()))) |
| 72 | | − smp_call_function(flush_ldt, current->mm, 1); |
| 73 | | − preempt_enable(); |
| 74 | | −#else |
| 75 | | − load_LDT(pc); |
| 76 | | −#endif |
| 77 | | − } |
| 78 | | − if (oldsize) { |
| 79 | | − paravirt_free_ldt(oldldt, oldsize); |
| 80 | | − if (oldsize * LDT_ENTRY_SIZE > PAGE_SIZE) |
| 81 | | − vfree(oldldt); |
| 82 | | − else |
| 83 | | − put_page(virt_to_page(oldldt)); |
| 84 | | − } |
| 85 | | − return 0; |
| 72 | +/* After calling this, the LDT is immutable. */ |
| 73 | +static void finalize_ldt_struct(struct ldt_struct *ldt) |
| 74 | +{ |
| 75 | + paravirt_alloc_ldt(ldt->entries, ldt->size); |
| 86 | 76 | } |
| 87 | 77 | |
| 88 | | −static inline int copy_ldt(mm_context_t *new, mm_context_t *old) |
| 78 | +/* context.lock is held */ |
| 79 | +static void install_ldt(struct mm_struct *current_mm, |
| 80 | + struct ldt_struct *ldt) |
| 89 | 81 | { |
| 90 | | − int err = alloc_ldt(new, old->size, 0); |
| 91 | | − int i; |
| 82 | + /* Synchronizes with lockless_dereference in load_mm_ldt. */ |
| 83 | + smp_store_release(¤t_mm->context.ldt, ldt); |
| 84 | + |
| 85 | + /* Activate the LDT for all CPUs using current_mm. */ |
| 86 | + on_each_cpu_mask(mm_cpumask(current_mm), flush_ldt, current_mm, true); |
| 87 | +} |
| 92 | 88 | |
| 93 | | − if (err < 0) |
| 94 | | − return err; |
| 89 | +static void free_ldt_struct(struct ldt_struct *ldt) |
| 90 | +{ |
| 91 | + if (likely(!ldt)) |
| 92 | + return; |
| 95 | 93 | |
| 96 | | − for (i = 0; i < old->size; i++) |
| 97 | | − write_ldt_entry(new->ldt, i, old->ldt + i * LDT_ENTRY_SIZE); |
| 98 | | − return 0; |
| 94 | + paravirt_free_ldt(ldt->entries, ldt->size); |
| 95 | + if (ldt->size * LDT_ENTRY_SIZE > PAGE_SIZE) |
| 96 | + vfree(ldt->entries); |
| 97 | + else |
| 98 | + kfree(ldt->entries); |
| 99 | + kfree(ldt); |
| 99 | 100 | } |
| 100 | 101 | |
| 101 | 102 | /* |
| @@ -104,17 +105,37 @@ static inline int copy_ldt(mm_context_t *new, mm_context_t *old) · 2 unmodified lines |
| 104 | 105 | */ |
| 105 | 106 | int init_new_context(struct task_struct *tsk, struct mm_struct *mm) |
| 106 | 107 | { |
| 108 | + struct ldt_struct *new_ldt; |
| 107 | 109 | struct mm_struct *old_mm; |
| 108 | 110 | int retval = 0; |
| 109 | 111 | |
| 110 | 112 | mutex_init(&mm->context.lock); |
| 111 | | − mm->context.size = 0; |
| 112 | 113 | old_mm = current->mm; |
| 113 | | − if (old_mm && old_mm->context.size > 0) { |
| 114 | | − mutex_lock(&old_mm->context.lock); |
| 115 | | − retval = copy_ldt(&mm->context, &old_mm->context); |
| 116 | | − mutex_unlock(&old_mm->context.lock); |
| 114 | + if (!old_mm) { |
| 115 | + mm->context.ldt = NULL; |
| 116 | + return 0; |
| 117 | 117 | } |
| 118 | + |
| 119 | + mutex_lock(&old_mm->context.lock); |
| 120 | + if (!old_mm->context.ldt) { |
| 121 | + mm->context.ldt = NULL; |
| 122 | + goto out_unlock; |
| 123 | + } |
| 124 | + |
| 125 | + new_ldt = alloc_ldt_struct(old_mm->context.ldt->size); |
| 126 | + if (!new_ldt) { |
| 127 | + retval = -ENOMEM; |
| 128 | + goto out_unlock; |
| 129 | + } |
| 130 | + |
| 131 | + memcpy(new_ldt->entries, old_mm->context.ldt->entries, |
| 132 | + new_ldt->size * LDT_ENTRY_SIZE); |
| 133 | + finalize_ldt_struct(new_ldt); |
| 134 | + |
| 135 | + mm->context.ldt = new_ldt; |
| 136 | + |
| 137 | +out_unlock: |
| 138 | + mutex_unlock(&old_mm->context.lock); |
| 118 | 139 | return retval; |
| 119 | 140 | } |
| 120 | 141 | |
| @@ -125,53 +146,47 @@ int init_new_context(struct task_struct *tsk, struct mm_struct *mm) · 4 unmodified lines |
| 125 | 146 | */ |
| 126 | 147 | void destroy_context(struct mm_struct *mm) |
| 127 | 148 | { |
| 128 | | − if (mm->context.size) { |
| 129 | | −#ifdef CONFIG_X86_32 |
| 130 | | − /* CHECKME: Can this ever happen ? */ |
| 131 | | − if (mm == current->active_mm) |
| 132 | | − clear_LDT(); |
| 133 | | −#endif |
| 134 | | − paravirt_free_ldt(mm->context.ldt, mm->context.size); |
| 135 | | − if (mm->context.size * LDT_ENTRY_SIZE > PAGE_SIZE) |
| 136 | | − vfree(mm->context.ldt); |
| 137 | | − else |
| 138 | | − put_page(virt_to_page(mm->context.ldt)); |
| 139 | | − mm->context.size = 0; |
| 140 | | − } |
| 149 | + free_ldt_struct(mm->context.ldt); |
| 150 | + mm->context.ldt = NULL; |
| 141 | 151 | } |
| 142 | 152 | |
| 143 | 153 | static int read_ldt(void __user *ptr, unsigned long bytecount) |
| 144 | 154 | { |
| 145 | | − int err; |
| 155 | + int retval; |
| 146 | 156 | unsigned long size; |
| 147 | 157 | struct mm_struct *mm = current->mm; |
| 148 | 158 | |
| 149 | | − if (!mm->context.size) |
| 150 | | − return 0; |
| 159 | + mutex_lock(&mm->context.lock); |
| 160 | + |
| 161 | + if (!mm->context.ldt) { |
| 162 | + retval = 0; |
| 163 | + goto out_unlock; |
| 164 | + } |
| 165 | + |
| 151 | 166 | if (bytecount > LDT_ENTRY_SIZE * LDT_ENTRIES) |
| 152 | 167 | bytecount = LDT_ENTRY_SIZE * LDT_ENTRIES; |
| 153 | 168 | |
| 154 | | − mutex_lock(&mm->context.lock); |
| 155 | | − size = mm->context.size * LDT_ENTRY_SIZE; |
| 169 | + size = mm->context.ldt->size * LDT_ENTRY_SIZE; |
| 156 | 170 | if (size > bytecount) |
| 157 | 171 | size = bytecount; |
| 158 | 172 | |
| 159 | | − err = 0; |
| 160 | | − if (copy_to_user(ptr, mm->context.ldt, size)) |
| 161 | | − err = -EFAULT; |
| 162 | | − mutex_unlock(&mm->context.lock); |
| 163 | | − if (err < 0) |
| 164 | | − goto error_return; |
| 173 | + if (copy_to_user(ptr, mm->context.ldt->entries, size)) { |
| 174 | + retval = -EFAULT; |
| 175 | + goto out_unlock; |
| 176 | + } |
| 177 | + |
| 165 | 178 | if (size != bytecount) { |
| 166 | | − /* zero-fill the rest */ |
| 167 | | − if (clear_user(ptr + size, bytecount - size) != 0) { |
| 168 | | − err = -EFAULT; |
| 169 | | − goto error_return; |
| 179 | + /* Zero-fill the rest and pretend we read bytecount bytes. */ |
| 180 | + if (clear_user(ptr + size, bytecount - size)) { |
| 181 | + retval = -EFAULT; |
| 182 | + goto out_unlock; |
| 170 | 183 | } |
| 171 | 184 | } |
| 172 | | − return bytecount; |
| 173 | | −error_return: |
| 174 | | − return err; |
| 185 | + retval = bytecount; |
| 186 | + |
| 187 | +out_unlock: |
| 188 | + mutex_unlock(&mm->context.lock); |
| 189 | + return retval; |
| 175 | 190 | } |
| 176 | 191 | |
| 177 | 192 | static int read_default_ldt(void __user *ptr, unsigned long bytecount) |
| @@ -195,6 +210,8 @@ static int write_ldt(void __user *ptr, unsigned long bytecount, int oldmode) · 17 unmodified lines |
| 195 | 210 | struct desc_struct ldt; |
| 196 | 211 | int error; |
| 197 | 212 | struct user_desc ldt_info; |
| 213 | + int oldsize, newsize; |
| 214 | + struct ldt_struct *new_ldt, *old_ldt; |
| 198 | 215 | |
| 199 | 216 | error = -EINVAL; |
| 200 | 217 | if (bytecount != sizeof(ldt_info)) |
| @@ -213,34 +230,39 @@ static int write_ldt(void __user *ptr, unsigned long bytecount, int oldmode) · 12 unmodified lines |
| 213 | 230 | goto out; |
| 214 | 231 | } |
| 215 | 232 | |
| 216 | | − mutex_lock(&mm->context.lock); |
| 217 | | − if (ldt_info.entry_number >= mm->context.size) { |
| 218 | | − error = alloc_ldt(¤t->mm->context, |
| 219 | | − ldt_info.entry_number + 1, 1); |
| 220 | | − if (error < 0) |
| 221 | | − goto out_unlock; |
| 222 | | − } |
| 223 | | − |
| 224 | | − /* Allow LDTs to be cleared by the user. */ |
| 225 | | − if (ldt_info.base_addr == 0 && ldt_info.limit == 0) { |
| 226 | | − if (oldmode || LDT_empty(&ldt_info)) { |
| 227 | | − memset(&ldt, 0, sizeof(ldt)); |
| 228 | | − goto install; |
| 233 | + if ((oldmode && !ldt_info.base_addr && !ldt_info.limit) || |
| 234 | + LDT_empty(&ldt_info)) { |
| 235 | + /* The user wants to clear the entry. */ |
| 236 | + memset(&ldt, 0, sizeof(ldt)); |
| 237 | + } else { |
| 238 | + if (!IS_ENABLED(CONFIG_X86_16BIT) && !ldt_info.seg_32bit) { |
| 239 | + error = -EINVAL; |
| 240 | + goto out; |
| 229 | 241 | } |
| 242 | + |
| 243 | + fill_ldt(&ldt, &ldt_info); |
| 244 | + if (oldmode) |
| 245 | + ldt.avl = 0; |
| 230 | 246 | } |
| 231 | 247 | |
| 232 | | − if (!IS_ENABLED(CONFIG_X86_16BIT) && !ldt_info.seg_32bit) { |
| 233 | | − error = -EINVAL; |
| 248 | + mutex_lock(&mm->context.lock); |
| 249 | + |
| 250 | + old_ldt = mm->context.ldt; |
| 251 | + oldsize = old_ldt ? old_ldt->size : 0; |
| 252 | + newsize = max((int)(ldt_info.entry_number + 1), oldsize); |
| 253 | + |
| 254 | + error = -ENOMEM; |
| 255 | + new_ldt = alloc_ldt_struct(newsize); |
| 256 | + if (!new_ldt) |
| 234 | 257 | goto out_unlock; |
| 235 | | − } |
| 236 | 258 | |
| 237 | | − fill_ldt(&ldt, &ldt_info); |
| 238 | | − if (oldmode) |
| 239 | | − ldt.avl = 0; |
| 259 | + if (old_ldt) |
| 260 | + memcpy(new_ldt->entries, old_ldt->entries, oldsize * LDT_ENTRY_SIZE); |
| 261 | + new_ldt->entries[ldt_info.entry_number] = ldt; |
| 262 | + finalize_ldt_struct(new_ldt); |
| 240 | 263 | |
| 241 | | − /* Install the new entry ... */ |
| 242 | | −install: |
| 243 | | − write_ldt_entry(mm->context.ldt, ldt_info.entry_number, &ldt); |
| 264 | + install_ldt(mm, new_ldt); |
| 265 | + free_ldt_struct(old_ldt); |
| 244 | 266 | error = 0; |
| 245 | 267 | |
| 246 | 268 | out_unlock: |