Merge branch 'tizen_2.4' into tizen_2.4_dev
[kernel/swap-modules.git] / us_manager / img / img_proc.c
1 /*
2  *  SWAP uprobe manager
3  *  modules/us_manager/img/img_proc.c
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * Copyright (C) Samsung Electronics, 2013
20  *
21  * 2013  Vyacheslav Cherkashin: SWAP us_manager implement
22  *
23  */
24
25
26 #include <linux/slab.h>
27 #include <linux/list.h>
28 #include <linux/spinlock.h>
29 #include <us_manager/sspt/sspt_proc.h>
30 #include <us_manager/sspt/sspt_file.h>
31 #include "img_ip.h"
32 #include "img_proc.h"
33 #include "img_file.h"
34
35
36 struct img_proc {
37         struct list_head file_list;
38         rwlock_t rwlock;
39 };
40
41
42 static void img_del_file_by_list(struct img_file *file);
43
44 /**
45  * @brief Create img_proc struct
46  *
47  * @return Pointer to the created img_proc struct
48  */
49 struct img_proc *create_img_proc(void)
50 {
51         struct img_proc *proc;
52
53         proc = kmalloc(sizeof(*proc), GFP_ATOMIC);
54         if (proc) {
55                 INIT_LIST_HEAD(&proc->file_list);
56                 rwlock_init(&proc->rwlock);
57         }
58
59         return proc;
60 }
61
62 /**
63  * @brief Remove img_proc struct
64  *
65  * @param file remove object
66  * @return Void
67  */
68 void free_img_proc(struct img_proc *proc)
69 {
70         struct img_file *file, *tmp;
71
72         list_for_each_entry_safe(file, tmp, &proc->file_list, list) {
73                 img_del_file_by_list(file);
74                 free_img_file(file);
75         }
76
77         kfree(proc);
78 }
79
80 /* called with write_[lock/unlock](&proc->rwlock) */
81 static void img_add_file_by_list(struct img_proc *proc, struct img_file *file)
82 {
83         list_add(&file->list, &proc->file_list);
84 }
85
86 /* called with write_[lock/unlock](&proc->rwlock) */
87 static void img_del_file_by_list(struct img_file *file)
88 {
89         list_del(&file->list);
90 }
91
92 /* called with read_[lock/unlock](&proc->rwlock) */
93 static struct img_file *find_img_file(struct img_proc *proc,
94                                       struct dentry *dentry)
95 {
96         struct img_file *file;
97
98         list_for_each_entry(file, &proc->file_list, list) {
99                 if (file->dentry == dentry)
100                         return file;
101         }
102
103         return NULL;
104 }
105
106 /**
107  * @brief Add instrumentation pointer
108  *
109  * @param proc Pointer to the img_proc struct
110  * @param dentry Dentry of file
111  * @param addr Function address
112  * @param probe_i Pointer to a probe_info struct related with the probe
113  * @return Error code
114  */
115 int img_proc_add_ip(struct img_proc *proc, struct dentry *dentry,
116                     unsigned long addr, struct probe_desc *pd)
117 {
118         int ret;
119         struct img_file *file;
120
121         write_lock(&proc->rwlock);
122         file = find_img_file(proc, dentry);
123         if (file) {
124                 ret = img_file_add_ip(file, addr, pd);
125                 goto unlock;
126         }
127
128         file = create_img_file(dentry);
129         if (file == NULL) {
130                 ret = -ENOMEM;
131                 goto unlock;
132         }
133
134         ret = img_file_add_ip(file, addr, pd);
135         if (ret) {
136                 printk(KERN_INFO "Cannot add ip to img file\n");
137                 free_img_file(file);
138         } else {
139                 img_add_file_by_list(proc, file);
140         }
141
142 unlock:
143         write_unlock(&proc->rwlock);
144         return ret;
145 }
146
147 /**
148  * @brief Remove instrumentation pointer
149  *
150  * @param proc Pointer to the img_proc struct
151  * @param dentry Dentry of file
152  * @param args Function address
153  * @return Error code
154  */
155 int img_proc_del_ip(struct img_proc *proc,
156                     struct dentry *dentry,
157                     unsigned long addr,
158                     struct probe_desc *pd)
159 {
160         int ret;
161         struct img_file *file;
162
163         write_lock(&proc->rwlock);
164         file = find_img_file(proc, dentry);
165         if (file == NULL) {
166                 ret = -EINVAL;
167                 goto unlock;
168         }
169
170         ret = img_file_del_ip(file, addr, pd);
171         if (ret == 0 && img_file_empty(file)) {
172                 img_del_file_by_list(file);
173                 free_img_file(file);
174         }
175
176 unlock:
177         write_unlock(&proc->rwlock);
178         return ret;
179 }
180
181 void img_proc_copy_to_sspt(struct img_proc *i_proc, struct sspt_proc *proc)
182 {
183         struct sspt_file *file;
184         struct img_file *i_file;
185
186         read_lock(&i_proc->rwlock);
187         list_for_each_entry(i_file, &i_proc->file_list, list) {
188                 file = sspt_proc_find_file_or_new(proc, i_file->dentry);
189                 if (file) {
190                         struct img_ip *i_ip;
191
192                         list_for_each_entry(i_ip, &i_file->ip_list, list)
193                                 sspt_file_add_ip(file, i_ip);
194                 }
195         }
196         read_unlock(&i_proc->rwlock);
197 }
198
199 /**
200  * @brief For debug
201  *
202  * @param proc Pointer to the img_proc struct
203  * @return Void
204  */
205
206 /* debug */
207 void img_proc_print(struct img_proc *proc)
208 {
209         struct img_file *file;
210
211         printk(KERN_INFO "### img_proc_print:\n");
212
213         read_lock(&proc->rwlock);
214         list_for_each_entry(file, &proc->file_list, list) {
215                 img_file_print(file);
216         }
217         read_unlock(&proc->rwlock);
218 }
219 /* debug */