#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/sched.h>
+#include <linux/sched/mm.h>
#include <linux/fs.h>
#include <linux/file.h>
#include <linux/stat.h>
static int loop_kthread_worker_fn(void *worker_ptr)
{
- current->flags |= PF_LOCAL_THROTTLE | PF_MEMALLOC_NOIO;
+ set_current_io_flusher();
return kthread_worker_fn(worker_ptr);
}
/*
* Get zone information from disk. Since blkdev_report_zones() uses
- * GFP_KERNEL by default for memory allocations, set the per-task
- * PF_MEMALLOC_NOIO flag so that all allocations are done as if
- * GFP_NOIO was specified.
+ * GFP_KERNEL by default for memory allocations, use
+ * memalloc_noio_save() to prevent recursion into the driver.
*/
noio_flag = memalloc_noio_save();
ret = blkdev_report_zones(dev->bdev, dmz_start_sect(zmd, zone), 1,
/* Stalled due to lack of memory */
unsigned in_memstall:1;
#endif
+ unsigned memalloc_noio:1;
unsigned long atomic_flags; /* Flags requiring atomic access. */
#define PF_FROZEN 0x00010000 /* Frozen for system suspend */
#define PF_KSWAPD 0x00020000 /* I am kswapd */
#define PF_MEMALLOC_NOFS 0x00040000 /* All allocation requests will inherit GFP_NOFS */
-#define PF_MEMALLOC_NOIO 0x00080000 /* All allocation requests will inherit GFP_NOIO */
#define PF_LOCAL_THROTTLE 0x00100000 /* Throttle writes only against the bdi I write to,
* I am cleaning dirty pages from some other bdi. */
#define PF_KTHREAD 0x00200000 /* I am a kernel thread */
/*
* Applies per-task gfp context to the given allocation flags.
- * PF_MEMALLOC_NOIO implies GFP_NOIO
* PF_MEMALLOC_NOFS implies GFP_NOFS
* PF_MEMALLOC_NOCMA implies no allocation from CMA region.
*/
static inline gfp_t current_gfp_context(gfp_t flags)
{
- if (unlikely(current->flags &
- (PF_MEMALLOC_NOIO | PF_MEMALLOC_NOFS | PF_MEMALLOC_NOCMA))) {
+ if (unlikely(current->flags & (PF_MEMALLOC_NOFS | PF_MEMALLOC_NOCMA) ||
+ current->memalloc_noio)) {
/*
* NOIO implies both NOIO and NOFS and it is a weaker context
* so always make sure it makes precedence
*/
- if (current->flags & PF_MEMALLOC_NOIO)
+ if (current->memalloc_noio)
flags &= ~(__GFP_IO | __GFP_FS);
else if (current->flags & PF_MEMALLOC_NOFS)
flags &= ~__GFP_FS;
*/
static inline unsigned int memalloc_noio_save(void)
{
- unsigned int flags = current->flags & PF_MEMALLOC_NOIO;
- current->flags |= PF_MEMALLOC_NOIO;
+ unsigned int flags = current->memalloc_noio;
+ current->memalloc_noio = 1;
return flags;
}
*/
static inline void memalloc_noio_restore(unsigned int flags)
{
- current->flags = (current->flags & ~PF_MEMALLOC_NOIO) | flags;
+ current->memalloc_noio = flags ? 1 : 0;
}
/**
}
#endif
+static inline void set_current_io_flusher(void)
+{
+ current->flags |= PF_LOCAL_THROTTLE;
+ current->memalloc_noio = 1;
+}
+
+static inline void clear_current_io_flusher(void)
+{
+ current->flags &= ~PF_LOCAL_THROTTLE;
+ current->memalloc_noio = 0;
+}
+
+static inline bool get_current_io_flusher(void)
+{
+ return current->flags & PF_LOCAL_THROTTLE;
+}
+
#ifdef CONFIG_MEMCG
/**
* memalloc_use_memcg - Starts the remote memcg charging scope.
return -EINVAL;
}
-#define PR_IO_FLUSHER (PF_MEMALLOC_NOIO | PF_LOCAL_THROTTLE)
-
SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
unsigned long, arg4, unsigned long, arg5)
{
return -EINVAL;
if (arg2 == 1)
- current->flags |= PR_IO_FLUSHER;
+ set_current_io_flusher();
else if (!arg2)
- current->flags &= ~PR_IO_FLUSHER;
+ clear_current_io_flusher();
else
return -EINVAL;
break;
if (arg2 || arg3 || arg4 || arg5)
return -EINVAL;
- error = (current->flags & PR_IO_FLUSHER) == PR_IO_FLUSHER;
+ error = get_current_io_flusher();
break;
default:
error = -EINVAL;