cgroups.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Tejun Heo <tj-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>
To: lizefan-hv44wF8Li93QT0dZR+AlfA@public.gmane.org
Cc: containers-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org,
	cgroups-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
	vgoyal-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org,
	mingo-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org,
	peterz-wEGCiKHe2LqWVfeAwA7xHQ@public.gmane.org,
	hannes-druUgvl0LCNAfugRpC6u6w@public.gmane.org,
	mhocko-AlSwsSmVLrQ@public.gmane.org,
	bsingharora-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org,
	kamezawa.hiroyu-+CUm20s59erQFUHtdCDX3A@public.gmane.org,
	nhorman-2XuSBdqkA4R54TAoqtyWWQ@public.gmane.org,
	daniel.wagner-98C5kh4wR6ohFhg+JK9F0w@public.gmane.org,
	arozansk-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org,
	Tejun Heo <tj-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>
Subject: [PATCH 12/12] cgroup: unify pidlist and other file handling
Date: Wed, 27 Nov 2013 18:42:39 -0500	[thread overview]
Message-ID: <1385595759-17656-13-git-send-email-tj@kernel.org> (raw)
In-Reply-To: <1385595759-17656-1-git-send-email-tj-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>

In preparation of conversion to kernfs, cgroup file handling is
updated so that it can be easily mapped to kernfs.  With the previous
changes, the difference between pidlist and other files are very
small.  Both are served by seq_file in a pretty standard way with the
only difference being !pidlist files use single_open().

This patch adds cftype->seq_start(), ->seq_next and ->seq_stop() and
implements the matching cgroup_seqfile_start/next/stop() which either
emulates single_open() behavior or invokes cftype->seq_*() operations
if specified.  This allows using single seq_operations for both
pidlist and other files and makes cgroup_pidlist_operations and
cgorup_pidlist_open() no longer necessary.  As cgroup_pidlist_open()
was the only user of cftype->open(), the method is dropped together.

This brings cftype file interface very close to kernfs interface and
mapping shouldn't be too difficult.  Once converted to kernfs, most of
the plumbing code including cgroup_seqfile_*() will be removed as
kernfs provides those facilities.

This patch does not introduce any behavior changes.

Signed-off-by: Tejun Heo <tj-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>
---
 include/linux/cgroup.h |   6 ++-
 kernel/cgroup.c        | 109 ++++++++++++++++++++++++++++---------------------
 2 files changed, 68 insertions(+), 47 deletions(-)

diff --git a/include/linux/cgroup.h b/include/linux/cgroup.h
index b32a0f8..8b9a594 100644
--- a/include/linux/cgroup.h
+++ b/include/linux/cgroup.h
@@ -434,7 +434,6 @@ struct cftype {
 	 */
 	struct cgroup_subsys *ss;
 
-	int (*open)(struct inode *inode, struct file *file);
 	/*
 	 * read_u64() is a shortcut for the common case of returning a
 	 * single integer. Use it in place of read()
@@ -448,6 +447,11 @@ struct cftype {
 	/* generic seq_file read interface */
 	int (*seq_show)(struct seq_file *sf, void *v);
 
+	/* optional ops, implement all or none */
+	void *(*seq_start)(struct seq_file *sf, loff_t *ppos);
+	void *(*seq_next)(struct seq_file *sf, void *v, loff_t *ppos);
+	void (*seq_stop)(struct seq_file *sf, void *v);
+
 	/*
 	 * write_u64() is a shortcut for the common case of accepting
 	 * a single integer (as parsed by simple_strtoull) from
diff --git a/kernel/cgroup.c b/kernel/cgroup.c
index 39fdf75..13a3d3b 100644
--- a/kernel/cgroup.c
+++ b/kernel/cgroup.c
@@ -2286,6 +2286,45 @@ out_free:
  * supports string->u64 maps, but can be extended in future.
  */
 
+static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos)
+{
+	struct cftype *cft = seq_cft(seq);
+
+	if (cft->seq_start) {
+		return cft->seq_start(seq, ppos);
+	} else {
+		/*
+		 * The same behavior and code as single_open().  Returns
+		 * !NULL if pos is at the beginning; otherwise, NULL.
+		 */
+		return NULL + !*ppos;
+	}
+}
+
+static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos)
+{
+	struct cftype *cft = seq_cft(seq);
+
+	if (cft->seq_next) {
+		return cft->seq_next(seq, v, ppos);
+	} else {
+		/*
+		 * The same behavior and code as single_open(), always
+		 * terminate after the initial read.
+		 */
+		++*ppos;
+		return NULL;
+	}
+}
+
+static void cgroup_seqfile_stop(struct seq_file *seq, void *v)
+{
+	struct cftype *cft = seq_cft(seq);
+
+	if (cft->seq_stop)
+		cft->seq_stop(seq, v);
+}
+
 static int cgroup_seqfile_show(struct seq_file *m, void *arg)
 {
 	struct cftype *cft = seq_cft(m);
@@ -2303,6 +2342,13 @@ static int cgroup_seqfile_show(struct seq_file *m, void *arg)
 	return 0;
 }
 
+static struct seq_operations cgroup_seq_operations = {
+	.start		= cgroup_seqfile_start,
+	.next		= cgroup_seqfile_next,
+	.stop		= cgroup_seqfile_stop,
+	.show		= cgroup_seqfile_show,
+};
+
 static int cgroup_file_open(struct inode *inode, struct file *file)
 {
 	struct cfent *cfe = __d_cfe(file->f_dentry);
@@ -2338,20 +2384,17 @@ static int cgroup_file_open(struct inode *inode, struct file *file)
 	WARN_ON_ONCE(cfe->css && cfe->css != css);
 	cfe->css = css;
 
-	if (cft->open) {
-		err = cft->open(inode, file);
-	} else {
-		err = single_open_size(file, cgroup_seqfile_show, cfe,
-				       sizeof(struct cgroup_open_file));
-		if (!err) {
-			struct seq_file *sf = file->private_data;
-			struct cgroup_open_file *of = sf->private;
+	err = seq_open_private(file, &cgroup_seq_operations,
+			       sizeof(struct cgroup_open_file));
+	if (!err) {
+		struct seq_file *sf = file->private_data;
+		struct cgroup_open_file *of = sf->private;
 
-			of->cfe = cfe;
-		}
+		of->cfe = cfe;
+		return 0;
 	}
 
-	if (css->ss && err)
+	if (css->ss)
 		css_put(css);
 	return err;
 }
@@ -2363,7 +2406,7 @@ static int cgroup_file_release(struct inode *inode, struct file *file)
 
 	if (css->ss)
 		css_put(css);
-	return single_release(inode, file);
+	return seq_release_private(inode, file);
 }
 
 /*
@@ -3774,38 +3817,6 @@ static const struct seq_operations cgroup_pidlist_seq_operations = {
 	.show = cgroup_pidlist_show,
 };
 
-static const struct file_operations cgroup_pidlist_operations = {
-	.read = seq_read,
-	.llseek = seq_lseek,
-	.write = cgroup_file_write,
-	.release = seq_release_private,
-};
-
-/*
- * The following functions handle opens on a file that displays a pidlist
- * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
- * in the cgroup.
- */
-/* helper function for the two below it */
-static int cgroup_pidlist_open(struct inode *unused, struct file *file)
-{
-	struct cfent *cfe = __d_cfe(file->f_dentry);
-	struct cgroup_open_file *of;
-	int retval;
-
-	/* configure file information */
-	file->f_op = &cgroup_pidlist_operations;
-
-	retval = seq_open_private(file, &cgroup_pidlist_seq_operations,
-				  sizeof(*of));
-	if (retval)
-		return retval;
-
-	of = ((struct seq_file *)file->private_data)->private;
-	of->cfe = cfe;
-	return 0;
-}
-
 static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
 					 struct cftype *cft)
 {
@@ -3859,7 +3870,10 @@ static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
 static struct cftype cgroup_base_files[] = {
 	{
 		.name = "cgroup.procs",
-		.open = cgroup_pidlist_open,
+		.seq_start = cgroup_pidlist_start,
+		.seq_next = cgroup_pidlist_next,
+		.seq_stop = cgroup_pidlist_stop,
+		.seq_show = cgroup_pidlist_show,
 		.private = CGROUP_FILE_PROCS,
 		.write_u64 = cgroup_procs_write,
 		.mode = S_IRUGO | S_IWUSR,
@@ -3884,7 +3898,10 @@ static struct cftype cgroup_base_files[] = {
 	{
 		.name = "tasks",
 		.flags = CFTYPE_INSANE,		/* use "procs" instead */
-		.open = cgroup_pidlist_open,
+		.seq_start = cgroup_pidlist_start,
+		.seq_next = cgroup_pidlist_next,
+		.seq_stop = cgroup_pidlist_stop,
+		.seq_show = cgroup_pidlist_show,
 		.private = CGROUP_FILE_TASKS,
 		.write_u64 = cgroup_tasks_write,
 		.mode = S_IRUGO | S_IWUSR,
-- 
1.8.4.2

  parent reply	other threads:[~2013-11-27 23:42 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-11-27 23:42 [PATCHSET cgroup/for-3.14] cgroup: consolidate file handling Tejun Heo
     [not found] ` <1385595759-17656-1-git-send-email-tj-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>
2013-11-27 23:42   ` [PATCH 01/12] cgroup, sched: convert away from cftype->read_map() Tejun Heo
2013-11-27 23:42   ` [PATCH 02/12] cpuset: convert away from cftype->read() Tejun Heo
2013-11-27 23:42   ` [PATCH 03/12] memcg: convert away from cftype->read() and ->read_map() Tejun Heo
     [not found]     ` <1385595759-17656-4-git-send-email-tj-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>
2013-11-28  8:26       ` Michal Hocko
2013-11-27 23:42   ` [PATCH 04/12] netprio_cgroup: convert away from cftype->read_map() Tejun Heo
     [not found]     ` <1385595759-17656-5-git-send-email-tj-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>
2013-11-29  1:56       ` Neil Horman
2013-11-29  8:52       ` Daniel Wagner
2013-11-27 23:42   ` [PATCH 05/12] hugetlb_cgroup: convert away from cftype->read() Tejun Heo
     [not found]     ` <1385595759-17656-6-git-send-email-tj-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>
2013-11-28  8:29       ` Michal Hocko
2013-11-27 23:42   ` [PATCH 06/12] cgroup: remove cftype->read(), ->read_map() and ->write() Tejun Heo
2013-11-27 23:42   ` [PATCH 07/12] cgroup: unify cgroup_write_X64() and cgroup_write_string() Tejun Heo
     [not found]     ` <1385595759-17656-8-git-send-email-tj-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>
2013-11-28 11:18       ` Michal Hocko
     [not found]         ` <20131128111818.GG2761-2MMpYkNvuYDjFM9bn6wA6Q@public.gmane.org>
2013-11-29 20:05           ` Tejun Heo
     [not found]             ` <20131129200525.GC21755-9pTldWuhBndy/B6EtB590w@public.gmane.org>
2013-12-02  9:54               ` Michal Hocko
     [not found]                 ` <20131202095401.GA18838-2MMpYkNvuYDjFM9bn6wA6Q@public.gmane.org>
2013-12-02 13:30                   ` Tejun Heo
     [not found]                     ` <20131202133059.GA3626-Gd/HAXX7CRxy/B6EtB590w@public.gmane.org>
2013-12-02 14:12                       ` Michal Hocko
     [not found]                         ` <20131202141242.GD18838-2MMpYkNvuYDjFM9bn6wA6Q@public.gmane.org>
2013-12-03 20:41                           ` Tejun Heo
     [not found]                             ` <20131203204155.GL8277-Gd/HAXX7CRxy/B6EtB590w@public.gmane.org>
2013-12-03 21:04                               ` Michal Hocko
2013-12-02 16:44               ` Johannes Weiner
     [not found]                 ` <20131202164406.GP3556-druUgvl0LCNAfugRpC6u6w@public.gmane.org>
2013-12-03  7:35                   ` Li Zefan
2013-11-27 23:42   ` [PATCH 08/12] cgroup: unify read path so that seq_file is always used Tejun Heo
2013-11-27 23:42   ` [PATCH 09/12] cgroup: generalize cgroup_pidlist_open_file Tejun Heo
2013-11-27 23:42   ` [PATCH 10/12] cgroup: attach cgroup_open_file to all cgroup files Tejun Heo
     [not found]     ` <1385595759-17656-11-git-send-email-tj-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>
2013-12-04  6:04       ` Li Zefan
     [not found]         ` <529EC5F4.10708-hv44wF8Li93QT0dZR+AlfA@public.gmane.org>
2013-12-04 13:04           ` Tejun Heo
2013-12-04 15:09       ` [PATCH v2 " Tejun Heo
2013-11-27 23:42   ` [PATCH 11/12] cgroup: replace cftype->read_seq_string() with cftype->seq_show() Tejun Heo
     [not found]     ` <1385595759-17656-12-git-send-email-tj-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>
2013-11-28  9:07       ` Daniel Wagner
2013-11-28 11:25       ` Michal Hocko
2013-12-02 14:41       ` Aristeu Rozanski
2013-12-02 14:52       ` Vivek Goyal
2013-11-27 23:42   ` Tejun Heo [this message]
     [not found]     ` <1385595759-17656-13-git-send-email-tj-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>
2013-12-04  6:20       ` [PATCH 12/12] cgroup: unify pidlist and other file handling Li Zefan
     [not found]         ` <529EC9A6.903-hv44wF8Li93QT0dZR+AlfA@public.gmane.org>
2013-12-04 13:08           ` Tejun Heo
2013-12-04 15:09       ` [PATCH v3 " Tejun Heo
2013-12-05  1:48   ` [PATCHSET cgroup/for-3.14] cgroup: consolidate " Li Zefan
2013-12-05 17:26   ` Tejun Heo

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=1385595759-17656-13-git-send-email-tj@kernel.org \
    --to=tj-dgejt+ai2ygdnm+yrofe0a@public.gmane.org \
    --cc=arozansk-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org \
    --cc=bsingharora-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org \
    --cc=cgroups-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
    --cc=containers-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org \
    --cc=daniel.wagner-98C5kh4wR6ohFhg+JK9F0w@public.gmane.org \
    --cc=hannes-druUgvl0LCNAfugRpC6u6w@public.gmane.org \
    --cc=kamezawa.hiroyu-+CUm20s59erQFUHtdCDX3A@public.gmane.org \
    --cc=lizefan-hv44wF8Li93QT0dZR+AlfA@public.gmane.org \
    --cc=mhocko-AlSwsSmVLrQ@public.gmane.org \
    --cc=mingo-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org \
    --cc=nhorman-2XuSBdqkA4R54TAoqtyWWQ@public.gmane.org \
    --cc=peterz-wEGCiKHe2LqWVfeAwA7xHQ@public.gmane.org \
    --cc=vgoyal-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.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).