Skip to content

Commit 25f2f3d

Browse files
rjwysockiAndi Kleen
authored andcommitted
snapshot: Use pm_mutex for mutual exclusion
We can avoid taking the BKL in snapshot_ioctl() if pm_mutex is used to prevent the ioctls from being executed concurrently. In addition, although it is only possible to open /dev/snapshot once, the task which has done that may spawn a child that will inherit the open descriptor, so in theory they can call snapshot_write(), snapshot_read() and snapshot_release() concurrently. pm_mutex can also be used for mutual exclusion in such cases. Signed-off-by: Rafael J. Wysocki <rjw@sisk.pl> Signed-off-by: Andi Kleen <ak@linux.intel.com> Acked-by: Pavel Machek <pavel@suse.cz> Signed-off-by: Len Brown <len.brown@intel.com>
1 parent 52d1102 commit 25f2f3d

1 file changed

Lines changed: 42 additions & 26 deletions

File tree

‎kernel/power/user.c‎

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,22 @@ static int snapshot_open(struct inode *inode, struct file *filp)
7070
struct snapshot_data *data;
7171
int error;
7272

73-
if (!atomic_add_unless(&snapshot_device_available, -1, 0))
74-
return -EBUSY;
73+
mutex_lock(&pm_mutex);
74+
75+
if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
76+
error = -EBUSY;
77+
goto Unlock;
78+
}
7579

7680
if ((filp->f_flags & O_ACCMODE) == O_RDWR) {
7781
atomic_inc(&snapshot_device_available);
78-
return -ENOSYS;
82+
error = -ENOSYS;
83+
goto Unlock;
7984
}
8085
if(create_basic_memory_bitmaps()) {
8186
atomic_inc(&snapshot_device_available);
82-
return -ENOMEM;
87+
error = -ENOMEM;
88+
goto Unlock;
8389
}
8490
nonseekable_open(inode, filp);
8591
data = &snapshot_state;
@@ -99,33 +105,36 @@ static int snapshot_open(struct inode *inode, struct file *filp)
99105
if (error)
100106
pm_notifier_call_chain(PM_POST_HIBERNATION);
101107
}
102-
if (error) {
108+
if (error)
103109
atomic_inc(&snapshot_device_available);
104-
return error;
105-
}
106110
data->frozen = 0;
107111
data->ready = 0;
108112
data->platform_support = 0;
109113

110-
return 0;
114+
Unlock:
115+
mutex_unlock(&pm_mutex);
116+
117+
return error;
111118
}
112119

113120
static int snapshot_release(struct inode *inode, struct file *filp)
114121
{
115122
struct snapshot_data *data;
116123

124+
mutex_lock(&pm_mutex);
125+
117126
swsusp_free();
118127
free_basic_memory_bitmaps();
119128
data = filp->private_data;
120129
free_all_swap_pages(data->swap);
121-
if (data->frozen) {
122-
mutex_lock(&pm_mutex);
130+
if (data->frozen)
123131
thaw_processes();
124-
mutex_unlock(&pm_mutex);
125-
}
126132
pm_notifier_call_chain(data->mode == O_WRONLY ?
127133
PM_POST_HIBERNATION : PM_POST_RESTORE);
128134
atomic_inc(&snapshot_device_available);
135+
136+
mutex_unlock(&pm_mutex);
137+
129138
return 0;
130139
}
131140

@@ -135,16 +144,24 @@ static ssize_t snapshot_read(struct file *filp, char __user *buf,
135144
struct snapshot_data *data;
136145
ssize_t res;
137146

147+
mutex_lock(&pm_mutex);
148+
138149
data = filp->private_data;
139-
if (!data->ready)
140-
return -ENODATA;
150+
if (!data->ready) {
151+
res = -ENODATA;
152+
goto Unlock;
153+
}
141154
res = snapshot_read_next(&data->handle, count);
142155
if (res > 0) {
143156
if (copy_to_user(buf, data_of(data->handle), res))
144157
res = -EFAULT;
145158
else
146159
*offp = data->handle.offset;
147160
}
161+
162+
Unlock:
163+
mutex_unlock(&pm_mutex);
164+
148165
return res;
149166
}
150167

@@ -154,6 +171,8 @@ static ssize_t snapshot_write(struct file *filp, const char __user *buf,
154171
struct snapshot_data *data;
155172
ssize_t res;
156173

174+
mutex_lock(&pm_mutex);
175+
157176
data = filp->private_data;
158177
res = snapshot_write_next(&data->handle, count);
159178
if (res > 0) {
@@ -162,6 +181,9 @@ static ssize_t snapshot_write(struct file *filp, const char __user *buf,
162181
else
163182
*offp = data->handle.offset;
164183
}
184+
185+
mutex_unlock(&pm_mutex);
186+
165187
return res;
166188
}
167189

@@ -180,34 +202,31 @@ static long snapshot_ioctl(struct file *filp, unsigned int cmd,
180202
if (!capable(CAP_SYS_ADMIN))
181203
return -EPERM;
182204

183-
data = filp->private_data;
205+
if (!mutex_trylock(&pm_mutex))
206+
return -EBUSY;
184207

185-
lock_kernel();
208+
data = filp->private_data;
186209

187210
switch (cmd) {
188211

189212
case SNAPSHOT_FREEZE:
190213
if (data->frozen)
191214
break;
192-
mutex_lock(&pm_mutex);
193215
printk("Syncing filesystems ... ");
194216
sys_sync();
195217
printk("done.\n");
196218

197219
error = freeze_processes();
198220
if (error)
199221
thaw_processes();
200-
mutex_unlock(&pm_mutex);
201222
if (!error)
202223
data->frozen = 1;
203224
break;
204225

205226
case SNAPSHOT_UNFREEZE:
206227
if (!data->frozen || data->ready)
207228
break;
208-
mutex_lock(&pm_mutex);
209229
thaw_processes();
210-
mutex_unlock(&pm_mutex);
211230
data->frozen = 0;
212231
break;
213232

@@ -310,16 +329,11 @@ static long snapshot_ioctl(struct file *filp, unsigned int cmd,
310329
error = -EPERM;
311330
break;
312331
}
313-
if (!mutex_trylock(&pm_mutex)) {
314-
error = -EBUSY;
315-
break;
316-
}
317332
/*
318333
* Tasks are frozen and the notifiers have been called with
319334
* PM_HIBERNATION_PREPARE
320335
*/
321336
error = suspend_devices_and_enter(PM_SUSPEND_MEM);
322-
mutex_unlock(&pm_mutex);
323337
break;
324338

325339
case SNAPSHOT_PLATFORM_SUPPORT:
@@ -392,7 +406,9 @@ static long snapshot_ioctl(struct file *filp, unsigned int cmd,
392406
error = -ENOTTY;
393407

394408
}
395-
unlock_kernel();
409+
410+
mutex_unlock(&pm_mutex);
411+
396412
return error;
397413
}
398414

0 commit comments

Comments
 (0)