The current mm interface is asymetric. One function allocates a locked buffer, another function only refunds the memory. Change this to have two functions for accounting and refunding locked memory, respectively; and do the actual buffer allocation in ptrace. [ Impact: refactor BTS buffer allocation code ]
Unified · Split
| @@ -617,17 +617,28 @@ struct bts_context { · 616 unmodified lines | ||
| 617 | 617 | struct work_struct work; |
| 618 | 618 | }; |
| 619 | 619 | |
| 620 | −static inline void alloc_bts_buffer(struct bts_context *context, | |
| 621 | − unsigned int size) | |
| 620 | +static int alloc_bts_buffer(struct bts_context *context, unsigned int size) | |
| 622 | 621 | { |
| 623 | − void *buffer; | |
| 622 | + void *buffer = NULL; | |
| 623 | + int err = -ENOMEM; | |
| 624 | 624 | |
| 625 | − buffer = alloc_locked_buffer(size); | |
| 626 | − if (buffer) { | |
| 627 | − context->buffer = buffer; | |
| 628 | − context->size = size; | |
| 629 | − context->mm = get_task_mm(current); | |
| 630 | − } | |
| 625 | + err = account_locked_memory(current->mm, current->signal->rlim, size); | |
| 626 | + if (err < 0) | |
| 627 | + return err; | |
| 628 | + | |
| 629 | + buffer = kzalloc(size, GFP_KERNEL); | |
| 630 | + if (!buffer) | |
| 631 | + goto out_refund; | |
| 632 | + | |
| 633 | + context->buffer = buffer; | |
| 634 | + context->size = size; | |
| 635 | + context->mm = get_task_mm(current); | |
| 636 | + | |
| 637 | + return 0; | |
| 638 | + | |
| 639 | + out_refund: | |
| 640 | + refund_locked_memory(current->mm, size); | |
| 641 | + return err; | |
| 631 | 642 | } |
| 632 | 643 | |
| 633 | 644 | static inline void free_bts_buffer(struct bts_context *context) |
| @@ -638,7 +649,7 @@ static inline void free_bts_buffer(struct bts_context *context) · 4 unmodified lines | ||
| 638 | 649 | kfree(context->buffer); |
| 639 | 650 | context->buffer = NULL; |
| 640 | 651 | |
| 641 | − refund_locked_buffer_memory(context->mm, context->size); | |
| 652 | + refund_locked_memory(context->mm, context->size); | |
| 642 | 653 | context->size = 0; |
| 643 | 654 | |
| 644 | 655 | mmput(context->mm); |
| @@ -786,13 +797,15 @@ static int ptrace_bts_config(struct task_struct *child, · 141 unmodified lines | ||
| 786 | 797 | context->tracer = NULL; |
| 787 | 798 | |
| 788 | 799 | if ((cfg.flags & PTRACE_BTS_O_ALLOC) && (cfg.size != context->size)) { |
| 800 | + int err; | |
| 801 | + | |
| 789 | 802 | free_bts_buffer(context); |
| 790 | 803 | if (!cfg.size) |
| 791 | 804 | return 0; |
| 792 | 805 | |
| 793 | − alloc_bts_buffer(context, cfg.size); | |
| 794 | − if (!context->buffer) | |
| 795 | − return -ENOMEM; | |
| 806 | + err = alloc_bts_buffer(context, cfg.size); | |
| 807 | + if (err < 0) | |
| 808 | + return err; | |
| 796 | 809 | } |
| 797 | 810 | |
| 798 | 811 | if (cfg.flags & PTRACE_BTS_O_TRACE) |
| @@ -19,6 +19,7 @@ struct anon_vma; · 18 unmodified lines | ||
| 19 | 19 | struct file_ra_state; |
| 20 | 20 | struct user_struct; |
| 21 | 21 | struct writeback_control; |
| 22 | +struct rlimit; | |
| 22 | 23 | |
| 23 | 24 | #ifndef CONFIG_DISCONTIGMEM /* Don't use mapnrs, do it properly */ |
| 24 | 25 | extern unsigned long max_mapnr; |
| @@ -1319,7 +1320,8 @@ int vmemmap_populate_basepages(struct page *start_page, · 1294 unmodified lines | ||
| 1319 | 1320 | int vmemmap_populate(struct page *start_page, unsigned long pages, int node); |
| 1320 | 1321 | void vmemmap_populate_print_last(void); |
| 1321 | 1322 | |
| 1322 | −extern void *alloc_locked_buffer(size_t size); | |
| 1323 | −extern void refund_locked_buffer_memory(struct mm_struct *mm, size_t size); | |
| 1323 | +extern int account_locked_memory(struct mm_struct *mm, struct rlimit *rlim, | |
| 1324 | + size_t size); | |
| 1325 | +extern void refund_locked_memory(struct mm_struct *mm, size_t size); | |
| 1324 | 1326 | #endif /* __KERNEL__ */ |
| 1325 | 1327 | #endif /* _LINUX_MM_H */ |
| @@ -629,38 +629,36 @@ void user_shm_unlock(size_t size, struct user_struct *user) · 628 unmodified lines | ||
| 629 | 629 | free_uid(user); |
| 630 | 630 | } |
| 631 | 631 | |
| 632 | −void *alloc_locked_buffer(size_t size) | |
| 632 | +int account_locked_memory(struct mm_struct *mm, struct rlimit *rlim, | |
| 633 | + size_t size) | |
| 633 | 634 | { |
| 634 | − unsigned long rlim, vm, pgsz; | |
| 635 | − void *buffer = NULL; | |
| 635 | + unsigned long lim, vm, pgsz; | |
| 636 | + int error = -ENOMEM; | |
| 636 | 637 | |
| 637 | 638 | pgsz = PAGE_ALIGN(size) >> PAGE_SHIFT; |
| 638 | 639 | |
| 639 | − down_write(¤t->mm->mmap_sem); | |
| 640 | − | |
| 641 | − rlim = current->signal->rlim[RLIMIT_AS].rlim_cur >> PAGE_SHIFT; | |
| 642 | − vm = current->mm->total_vm + pgsz; | |
| 643 | − if (rlim < vm) | |
| 644 | − goto out; | |
| 640 | + down_write(&mm->mmap_sem); | |
| 645 | 641 | |
| 646 | − rlim = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur >> PAGE_SHIFT; | |
| 647 | − vm = current->mm->locked_vm + pgsz; | |
| 648 | − if (rlim < vm) | |
| 642 | + lim = rlim[RLIMIT_AS].rlim_cur >> PAGE_SHIFT; | |
| 643 | + vm = mm->total_vm + pgsz; | |
| 644 | + if (lim < vm) | |
| 649 | 645 | goto out; |
| 650 | 646 | |
| 651 | − buffer = kzalloc(size, GFP_KERNEL); | |
| 652 | − if (!buffer) | |
| 647 | + lim = rlim[RLIMIT_MEMLOCK].rlim_cur >> PAGE_SHIFT; | |
| 648 | + vm = mm->locked_vm + pgsz; | |
| 649 | + if (lim < vm) | |
| 653 | 650 | goto out; |
| 654 | 651 | |
| 655 | − current->mm->total_vm += pgsz; | |
| 656 | − current->mm->locked_vm += pgsz; | |
| 652 | + mm->total_vm += pgsz; | |
| 653 | + mm->locked_vm += pgsz; | |
| 657 | 654 | |
| 655 | + error = 0; | |
| 658 | 656 | out: |
| 659 | − up_write(¤t->mm->mmap_sem); | |
| 660 | − return buffer; | |
| 657 | + up_write(&mm->mmap_sem); | |
| 658 | + return error; | |
| 661 | 659 | } |
| 662 | 660 | |
| 663 | −void refund_locked_buffer_memory(struct mm_struct *mm, size_t size) | |
| 661 | +void refund_locked_memory(struct mm_struct *mm, size_t size) | |
| 664 | 662 | { |
| 665 | 663 | unsigned long pgsz = PAGE_ALIGN(size) >> PAGE_SHIFT; |
| 666 | 664 | |