@@ -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
113120static 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