From patchwork Tue Aug 14 13:02:18 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Wagner X-Patchwork-Id: 177261 X-Patchwork-Delegate: davem@davemloft.net Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 5680D2C0080 for ; Tue, 14 Aug 2012 23:02:59 +1000 (EST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755986Ab2HNNCt (ORCPT ); Tue, 14 Aug 2012 09:02:49 -0400 Received: from hotel311.server4you.de ([85.25.146.15]:54832 "EHLO hotel311.server4you.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755219Ab2HNNC1 (ORCPT ); Tue, 14 Aug 2012 09:02:27 -0400 Received: from candlejack.bmw-carit.intra (mail.bmw-carit.de [62.245.222.98]) (Authenticated sender: wagi) by hotel311.server4you.de (Postfix) with ESMTPSA id 1FB16CBE109; Tue, 14 Aug 2012 15:02:21 +0200 (CEST) From: Daniel Wagner To: netdev@vger.kernel.org, cgroups@vger.kernel.org Cc: Daniel Wagner , "David S. Miller" , Andrew Morton , Eric Dumazet , Gao feng , Glauber Costa , Jamal Hadi Salim , John Fastabend , Kamezawa Hiroyuki , Li Zefan , Neil Horman , Tejun Heo Subject: [PATCH v3 2/6] cgroup: net_cls rework update socket logic Date: Tue, 14 Aug 2012 15:02:18 +0200 Message-Id: <1344949343-26090-3-git-send-email-wagi@monom.org> X-Mailer: git-send-email 1.7.12.rc1.16.g05a20c8 In-Reply-To: <1344949343-26090-1-git-send-email-wagi@monom.org> References: <1344949343-26090-1-git-send-email-wagi@monom.org> Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org From: Daniel Wagner Update net_cls do do the same as net_prio does: commit 406a3c638ce8b17d9704052c07955490f732c2b8 Author: John Fastabend Date: Fri Jul 20 10:39:25 2012 +0000 net: netprio_cgroup: rework update socket logic Instead of updating the sk_cgrp_prioidx struct field on every send this only updates the field when a task is moved via cgroup infrastructure. This allows sockets that may be used by a kernel worker thread to be managed. For example in the iscsi case today a user can put iscsid in a netprio cgroup and control traffic will be sent with the correct sk_cgrp_prioidx value set but as soon as data is sent the kernel worker thread isssues a send and sk_cgrp_prioidx is updated with the kernel worker threads value which is the default case. It seems more correct to only update the field when the user explicitly sets it via control group infrastructure. This allows the users to manage sockets that may be used with other threads. John already send an updated version for the attach function which does not rely on the ugly scanf code, which I am using for this patch here. Signed-off-by: Daniel Wagner Cc: "David S. Miller" Cc: Andrew Morton Cc: Eric Dumazet Cc: Gao feng Cc: Glauber Costa Cc: Jamal Hadi Salim Cc: John Fastabend Cc: Kamezawa Hiroyuki Cc: Li Zefan Cc: Neil Horman Cc: Tejun Heo Cc: netdev@vger.kernel.org Cc: cgroups@vger.kernel.org --- include/net/cls_cgroup.h | 10 ++-------- net/core/netprio_cgroup.c | 2 +- net/core/sock.c | 11 ++++++----- net/sched/cls_cgroup.c | 38 ++++++++++++++++++++++++++++++++++++++ net/socket.c | 8 -------- 5 files changed, 47 insertions(+), 22 deletions(-) diff --git a/include/net/cls_cgroup.h b/include/net/cls_cgroup.h index 5f49b69..fc05f43 100644 --- a/include/net/cls_cgroup.h +++ b/include/net/cls_cgroup.h @@ -24,16 +24,13 @@ struct cgroup_cls_state u32 classid; }; -extern void sock_update_classid(struct sock *sk); +extern void sock_update_classid(struct sock *sk, struct task_struct *task); #ifdef CONFIG_NET_CLS_CGROUP static inline u32 task_cls_classid(struct task_struct *p) { int classid; - if (in_interrupt()) - return 0; - rcu_read_lock(); classid = container_of(task_subsys_state(p, net_cls_subsys_id), struct cgroup_cls_state, css)->classid; @@ -49,9 +46,6 @@ static inline u32 task_cls_classid(struct task_struct *p) int id; u32 classid = 0; - if (in_interrupt()) - return 0; - rcu_read_lock(); id = rcu_dereference_index_check(net_cls_subsys_id, rcu_read_lock_held()); @@ -64,7 +58,7 @@ static inline u32 task_cls_classid(struct task_struct *p) } #endif #else -static inline void sock_update_classid(struct sock *sk) +static inline void sock_update_classid(struct sock *sk, struct task_struct *task) { } diff --git a/net/core/netprio_cgroup.c b/net/core/netprio_cgroup.c index ed0c043..98478aa 100644 --- a/net/core/netprio_cgroup.c +++ b/net/core/netprio_cgroup.c @@ -274,7 +274,7 @@ out_free_devname: return ret; } -void net_prio_attach(struct cgroup *cgrp, struct cgroup_taskset *tset) +static void net_prio_attach(struct cgroup *cgrp, struct cgroup_taskset *tset) { struct task_struct *p; char *tmp = kzalloc(sizeof(char) * PATH_MAX, GFP_KERNEL); diff --git a/net/core/sock.c b/net/core/sock.c index 8f67ced..e08df6b 100644 --- a/net/core/sock.c +++ b/net/core/sock.c @@ -1223,13 +1223,14 @@ static void sk_prot_free(struct proto *prot, struct sock *sk) } #ifdef CONFIG_CGROUPS -void sock_update_classid(struct sock *sk) +void sock_update_classid(struct sock *sk, struct task_struct *task) { u32 classid; - rcu_read_lock(); /* doing current task, which cannot vanish. */ - classid = task_cls_classid(current); - rcu_read_unlock(); + if (in_interrupt()) + return; + + classid = task_cls_classid(task); if (classid && classid != sk->sk_classid) sk->sk_classid = classid; } @@ -1269,7 +1270,7 @@ struct sock *sk_alloc(struct net *net, int family, gfp_t priority, sock_net_set(sk, get_net(net)); atomic_set(&sk->sk_wmem_alloc, 1); - sock_update_classid(sk); + sock_update_classid(sk, current); sock_update_netprioidx(sk, current); } diff --git a/net/sched/cls_cgroup.c b/net/sched/cls_cgroup.c index 7743ea8..3535771 100644 --- a/net/sched/cls_cgroup.c +++ b/net/sched/cls_cgroup.c @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -53,6 +54,42 @@ static void cgrp_destroy(struct cgroup *cgrp) kfree(cgrp_cls_state(cgrp)); } +static void cgrp_attach(struct cgroup *cgrp, struct cgroup_taskset *tset) +{ + struct task_struct *p; + + cgroup_taskset_for_each(p, cgrp, tset) { + unsigned int fd; + struct fdtable *fdt; + struct files_struct *files; + + task_lock(p); + files = p->files; + if (!files) { + task_unlock(p); + continue; + } + + spin_lock(&files->file_lock); + fdt = files_fdtable(files); + for (fd = 0; fd < fdt->max_fds; fd++) { + struct file *file; + struct socket *sock; + int err; + + file = fcheck_files(files, fd); + if (!file) + continue; + + sock = sock_from_file(file, &err); + if (sock) + sock_update_netprioidx(sock->sk, p); + } + spin_unlock(&files->file_lock); + task_unlock(p); + } +} + static u64 read_classid(struct cgroup *cgrp, struct cftype *cft) { return cgrp_cls_state(cgrp)->classid; @@ -77,6 +114,7 @@ struct cgroup_subsys net_cls_subsys = { .name = "net_cls", .create = cgrp_create, .destroy = cgrp_destroy, + .attach = cgrp_attach, #ifdef CONFIG_NET_CLS_CGROUP .subsys_id = net_cls_subsys_id, #endif diff --git a/net/socket.c b/net/socket.c index dfe5b66..f06c8c4 100644 --- a/net/socket.c +++ b/net/socket.c @@ -553,8 +553,6 @@ static inline int __sock_sendmsg_nosec(struct kiocb *iocb, struct socket *sock, { struct sock_iocb *si = kiocb_to_siocb(iocb); - sock_update_classid(sock->sk); - si->sock = sock; si->scm = NULL; si->msg = msg; @@ -717,8 +715,6 @@ static inline int __sock_recvmsg_nosec(struct kiocb *iocb, struct socket *sock, { struct sock_iocb *si = kiocb_to_siocb(iocb); - sock_update_classid(sock->sk); - si->sock = sock; si->scm = NULL; si->msg = msg; @@ -829,8 +825,6 @@ static ssize_t sock_splice_read(struct file *file, loff_t *ppos, if (unlikely(!sock->ops->splice_read)) return -EINVAL; - sock_update_classid(sock->sk); - return sock->ops->splice_read(sock, ppos, pipe, len, flags); } @@ -3353,8 +3347,6 @@ EXPORT_SYMBOL(kernel_setsockopt); int kernel_sendpage(struct socket *sock, struct page *page, int offset, size_t size, int flags) { - sock_update_classid(sock->sk); - if (sock->ops->sendpage) return sock->ops->sendpage(sock, page, offset, size, flags);