Skip to content

Commit 46ca359

Browse files
Michal HockoJonathan Corbet
authored andcommitted
doc: document scope NOFS, NOIO APIs
Although the api is documented in the source code Ted has pointed out that there is no mention in the core-api Documentation and there are people looking there to find answers how to use a specific API. Requested-by: "Theodore Y. Ts'o" <tytso@mit.edu> Reviewed-by: Dave Chinner <dchinner@redhat.com> Signed-off-by: Michal Hocko <mhocko@suse.com> Signed-off-by: Jonathan Corbet <corbet@lwn.net>
1 parent 8962e40 commit 46ca359

3 files changed

Lines changed: 100 additions & 0 deletions

File tree

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
=================================
2+
GFP masks used from FS/IO context
3+
=================================
4+
5+
:Date: May, 2018
6+
:Author: Michal Hocko <mhocko@kernel.org>
7+
8+
Introduction
9+
============
10+
11+
Code paths in the filesystem and IO stacks must be careful when
12+
allocating memory to prevent recursion deadlocks caused by direct
13+
memory reclaim calling back into the FS or IO paths and blocking on
14+
already held resources (e.g. locks - most commonly those used for the
15+
transaction context).
16+
17+
The traditional way to avoid this deadlock problem is to clear __GFP_FS
18+
respectively __GFP_IO (note the latter implies clearing the first as well) in
19+
the gfp mask when calling an allocator. GFP_NOFS respectively GFP_NOIO can be
20+
used as shortcut. It turned out though that above approach has led to
21+
abuses when the restricted gfp mask is used "just in case" without a
22+
deeper consideration which leads to problems because an excessive use
23+
of GFP_NOFS/GFP_NOIO can lead to memory over-reclaim or other memory
24+
reclaim issues.
25+
26+
New API
27+
========
28+
29+
Since 4.12 we do have a generic scope API for both NOFS and NOIO context
30+
``memalloc_nofs_save``, ``memalloc_nofs_restore`` respectively ``memalloc_noio_save``,
31+
``memalloc_noio_restore`` which allow to mark a scope to be a critical
32+
section from a filesystem or I/O point of view. Any allocation from that
33+
scope will inherently drop __GFP_FS respectively __GFP_IO from the given
34+
mask so no memory allocation can recurse back in the FS/IO.
35+
36+
FS/IO code then simply calls the appropriate save function before
37+
any critical section with respect to the reclaim is started - e.g.
38+
lock shared with the reclaim context or when a transaction context
39+
nesting would be possible via reclaim. The restore function should be
40+
called when the critical section ends. All that ideally along with an
41+
explanation what is the reclaim context for easier maintenance.
42+
43+
Please note that the proper pairing of save/restore functions
44+
allows nesting so it is safe to call ``memalloc_noio_save`` or
45+
``memalloc_noio_restore`` respectively from an existing NOIO or NOFS
46+
scope.
47+
48+
What about __vmalloc(GFP_NOFS)
49+
==============================
50+
51+
vmalloc doesn't support GFP_NOFS semantic because there are hardcoded
52+
GFP_KERNEL allocations deep inside the allocator which are quite non-trivial
53+
to fix up. That means that calling ``vmalloc`` with GFP_NOFS/GFP_NOIO is
54+
almost always a bug. The good news is that the NOFS/NOIO semantic can be
55+
achieved by the scope API.
56+
57+
In the ideal world, upper layers should already mark dangerous contexts
58+
and so no special care is required and vmalloc should be called without
59+
any problems. Sometimes if the context is not really clear or there are
60+
layering violations then the recommended way around that is to wrap ``vmalloc``
61+
by the scope API with a comment explaining the problem.

‎Documentation/core-api/index.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Core utilities
2727
errseq
2828
printk-formats
2929
circular-buffers
30+
gfp_mask-from-fs-io
3031

3132
Interfaces for kernel debugging
3233
===============================

‎include/linux/sched/mm.h‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,25 +170,63 @@ static inline void fs_reclaim_acquire(gfp_t gfp_mask) { }
170170
static inline void fs_reclaim_release(gfp_t gfp_mask) { }
171171
#endif
172172

173+
/**
174+
* memalloc_noio_save - Marks implicit GFP_NOIO allocation scope.
175+
*
176+
* This functions marks the beginning of the GFP_NOIO allocation scope.
177+
* All further allocations will implicitly drop __GFP_IO flag and so
178+
* they are safe for the IO critical section from the allocation recursion
179+
* point of view. Use memalloc_noio_restore to end the scope with flags
180+
* returned by this function.
181+
*
182+
* This function is safe to be used from any context.
183+
*/
173184
static inline unsigned int memalloc_noio_save(void)
174185
{
175186
unsigned int flags = current->flags & PF_MEMALLOC_NOIO;
176187
current->flags |= PF_MEMALLOC_NOIO;
177188
return flags;
178189
}
179190

191+
/**
192+
* memalloc_noio_restore - Ends the implicit GFP_NOIO scope.
193+
* @flags: Flags to restore.
194+
*
195+
* Ends the implicit GFP_NOIO scope started by memalloc_noio_save function.
196+
* Always make sure that that the given flags is the return value from the
197+
* pairing memalloc_noio_save call.
198+
*/
180199
static inline void memalloc_noio_restore(unsigned int flags)
181200
{
182201
current->flags = (current->flags & ~PF_MEMALLOC_NOIO) | flags;
183202
}
184203

204+
/**
205+
* memalloc_nofs_save - Marks implicit GFP_NOFS allocation scope.
206+
*
207+
* This functions marks the beginning of the GFP_NOFS allocation scope.
208+
* All further allocations will implicitly drop __GFP_FS flag and so
209+
* they are safe for the FS critical section from the allocation recursion
210+
* point of view. Use memalloc_nofs_restore to end the scope with flags
211+
* returned by this function.
212+
*
213+
* This function is safe to be used from any context.
214+
*/
185215
static inline unsigned int memalloc_nofs_save(void)
186216
{
187217
unsigned int flags = current->flags & PF_MEMALLOC_NOFS;
188218
current->flags |= PF_MEMALLOC_NOFS;
189219
return flags;
190220
}
191221

222+
/**
223+
* memalloc_nofs_restore - Ends the implicit GFP_NOFS scope.
224+
* @flags: Flags to restore.
225+
*
226+
* Ends the implicit GFP_NOFS scope started by memalloc_nofs_save function.
227+
* Always make sure that that the given flags is the return value from the
228+
* pairing memalloc_nofs_save call.
229+
*/
192230
static inline void memalloc_nofs_restore(unsigned int flags)
193231
{
194232
current->flags = (current->flags & ~PF_MEMALLOC_NOFS) | flags;

0 commit comments

Comments
 (0)