linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Mike Christie <mchristi@redhat.com>
To: linux-mm@kvack.org, linux-kernel@vger.kernel.org,
	linux-scsi@vger.kernel.org, linux-fsdevel@vger.kernel.org,
	linux-block@vger.kernel.org, martin@urbackup.org,
	Damien.LeMoal@wdc.com
Cc: Mike Christie <mchristi@redhat.com>
Subject: [PATCH] Add prctl support for controlling PF_MEMALLOC V2
Date: Mon, 21 Oct 2019 16:41:37 -0500	[thread overview]
Message-ID: <20191021214137.8172-1-mchristi@redhat.com> (raw)

There are several storage drivers like dm-multipath, iscsi, tcmu-runner,
amd nbd that have userspace components that can run in the IO path. For
example, iscsi and nbd's userspace deamons may need to recreate a socket
and/or send IO on it, and dm-multipath's daemon multipathd may need to
send IO to figure out the state of paths and re-set them up.

In the kernel these drivers have access to GFP_NOIO/GFP_NOFS and the
memalloc_*_save/restore functions to control the allocation behavior,
but for userspace we would end up hitting a allocation that ended up
writing data back to the same device we are trying to allocate for.

This patch allows the userspace deamon to set the PF_MEMALLOC* flags
with prctl during their initialization so later allocations cannot
calling back into them.

Signed-off-by: Mike Christie <mchristi@redhat.com>
---

V2:
- Use prctl instead of procfs.
- Add support for NOFS for fuse.
- Check permissions.

 include/uapi/linux/prctl.h |  8 +++++++
 kernel/sys.c               | 44 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 52 insertions(+)

diff --git a/include/uapi/linux/prctl.h b/include/uapi/linux/prctl.h
index 7da1b37b27aa..6f6b3af6633a 100644
--- a/include/uapi/linux/prctl.h
+++ b/include/uapi/linux/prctl.h
@@ -234,4 +234,12 @@ struct prctl_mm_map {
 #define PR_GET_TAGGED_ADDR_CTRL		56
 # define PR_TAGGED_ADDR_ENABLE		(1UL << 0)
 
+/* Control reclaim behavior when allocating memory */
+#define PR_SET_MEMALLOC			57
+#define PR_GET_MEMALLOC			58
+#define PR_MEMALLOC_SET_NOIO		(1UL << 0)
+#define PR_MEMALLOC_CLEAR_NOIO		(1UL << 1)
+#define PR_MEMALLOC_SET_NOFS		(1UL << 2)
+#define PR_MEMALLOC_CLEAR_NOFS		(1UL << 3)
+
 #endif /* _LINUX_PRCTL_H */
diff --git a/kernel/sys.c b/kernel/sys.c
index a611d1d58c7d..34fedc9fc7e4 100644
--- a/kernel/sys.c
+++ b/kernel/sys.c
@@ -2486,6 +2486,50 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
 			return -EINVAL;
 		error = GET_TAGGED_ADDR_CTRL();
 		break;
+	case PR_SET_MEMALLOC:
+		if (!capable(CAP_SYS_ADMIN))
+			return -EPERM;
+
+		if (arg3 || arg4 || arg5)
+			return -EINVAL;
+
+		switch (arg2) {
+		case PR_MEMALLOC_SET_NOIO:
+			if (current->flags & PF_MEMALLOC_NOFS)
+				return -EINVAL;
+
+			current->flags |= PF_MEMALLOC_NOIO;
+			break;
+		case PR_MEMALLOC_CLEAR_NOIO:
+			current->flags &= ~PF_MEMALLOC_NOIO;
+			break;
+		case PR_MEMALLOC_SET_NOFS:
+			if (current->flags & PF_MEMALLOC_NOIO)
+				return -EINVAL;
+
+			current->flags |= PF_MEMALLOC_NOFS;
+			break;
+		case PR_MEMALLOC_CLEAR_NOFS:
+			current->flags &= ~PF_MEMALLOC_NOFS;
+			break;
+		default:
+			return -EINVAL;
+		}
+		break;
+	case PR_GET_MEMALLOC:
+		if (!capable(CAP_SYS_ADMIN))
+			return -EPERM;
+
+		if (arg2 || arg3 || arg4 || arg5)
+			return -EINVAL;
+
+		if (current->flags & PF_MEMALLOC_NOIO)
+			error = PR_MEMALLOC_SET_NOIO;
+		else if (current->flags & PF_MEMALLOC_NOFS)
+			error = PR_MEMALLOC_SET_NOFS;
+		else
+			error = 0;
+		break;
 	default:
 		error = -EINVAL;
 		break;
-- 
2.20.1


             reply	other threads:[~2019-10-21 21:41 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-10-21 21:41 Mike Christie [this message]
2019-10-21 22:52 ` [PATCH] Add prctl support for controlling PF_MEMALLOC V2 Dave Chinner
2019-10-22 15:42   ` Mike Christie
2019-10-22 11:24 ` Michal Hocko
2019-10-22 16:13   ` Mike Christie
2019-10-22 16:33     ` Michal Hocko
2019-10-22 20:43       ` Dave Chinner
2019-10-23  7:11         ` Michal Hocko
2019-10-23 17:27           ` Mike Christie
2019-10-23 17:35             ` Michal Hocko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20191021214137.8172-1-mchristi@redhat.com \
    --to=mchristi@redhat.com \
    --cc=Damien.LeMoal@wdc.com \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=linux-scsi@vger.kernel.org \
    --cc=martin@urbackup.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).