From patchwork Mon Nov 19 08:16:29 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rui Xiang X-Patchwork-Id: 199934 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 771A92C0097 for ; Mon, 19 Nov 2012 19:16:44 +1100 (EST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753059Ab2KSIQm (ORCPT ); Mon, 19 Nov 2012 03:16:42 -0500 Received: from mail-pa0-f46.google.com ([209.85.220.46]:43297 "EHLO mail-pa0-f46.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753011Ab2KSIQk (ORCPT ); Mon, 19 Nov 2012 03:16:40 -0500 Received: by mail-pa0-f46.google.com with SMTP id bh2so279329pad.19 for ; Mon, 19 Nov 2012 00:16:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:cc:subject :content-type:content-transfer-encoding; bh=r7BCg/WZZxqfDp8i+sLGRz+SPMH6l72deIArinLKIKk=; b=ClOJrjXNeQsnZlQ0gG9zpLcrrX2fRg5bZ0nfl46BvZLysNRBwf2E7y7/wdIVGbYu+j E3kY2dcfG6HD2rqlHE9lvDUK+CKxuXjQNdTRu8EpqsmB7E8aYY4+RMO9Zock75PUBCO6 zLsa4tdbIxYKY4Bd6HR3gcXGCta591izCl/ANgay+YRuptvYI89I2zsc6lKzS5MoACN5 yOvKNlEirD8SiAqkasr48wKCmUITlaw3poYIQpwZEDWWTf3yhROrx7SVPNoDa23+/UKQ H4Qr7x2pUAUyp5fCiOq/UdoLsENGG0u/KrQBwDfCxi5OReEn1GG+mBP+mGwv+qhabG9j HNdA== Received: by 10.68.248.1 with SMTP id yi1mr31454497pbc.93.1353313000655; Mon, 19 Nov 2012 00:16:40 -0800 (PST) Received: from [192.168.204.180] ([218.17.215.175]) by mx.google.com with ESMTPS id ms11sm5829698pbc.74.2012.11.19.00.16.36 (version=SSLv3 cipher=OTHER); Mon, 19 Nov 2012 00:16:40 -0800 (PST) Message-ID: <50A9EADD.4040709@gmail.com> Date: Mon, 19 Nov 2012 16:16:29 +0800 From: Rui Xiang User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:16.0) Gecko/20121026 Thunderbird/16.0.2 MIME-Version: 1.0 To: serge.hallyn@canonical.com, containers@lists.linux-foundation.org CC: "Eric W. Biederman" , netdev@vger.kernel.org Subject: [PATCH RFC 1/5] Syslog_ns: add syslog_namespace struct and API Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org From: Xiang Rui This patch add a struct syslog_namespace which contains the necessary member when handling syslog. We realize gut_syslog_ns and put_syslog_ns API, and syslog_ns is initialized by init_syslog_ns. CONFIG_SYSLOG_NS is defined to allow to create syslog_ns. Signed-off-by: Xiang Rui Signed-off-by: Libo Chen --- include/linux/syslog_namespace.h | 78 ++++++++++++++++++++++++++++++++++++++ init/Kconfig | 7 +++ kernel/Makefile | 1 + kernel/syslog_namespace.c | 31 +++++++++++++++ 4 files changed, 117 insertions(+), 0 deletions(-) create mode 100644 include/linux/syslog_namespace.h create mode 100644 kernel/syslog_namespace.c diff --git a/include/linux/syslog_namespace.h b/include/linux/syslog_namespace.h new file mode 100644 index 0000000..8c8ac5a --- /dev/null +++ b/include/linux/syslog_namespace.h @@ -0,0 +1,78 @@ +#ifndef _LINUX_SYSLOG_NAMESPACE_H +#define _LINUX_SYSLOG_NAMESPACE_H + +#include + +/* record buffer */ +#if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) +#define LOG_ALIGN 4 +#else +#define LOG_ALIGN __alignof__(struct log) +#endif + +#define CONTAINER_BUF_LEN 4096 + +#define __LOG_BUF_LEN (1 << CONFIG_LOG_BUF_SHIFT) + +struct syslog_namespace { + struct kref kref; /* syslog_ns reference count & control */ + + raw_spinlock_t logbuf_lock; /* access conflict locker */ + + /* index and sequence number of the first record stored in the buffer */ + u64 log_first_seq; + u32 log_first_idx; + + /* index and sequence number of the next record stored in the buffer */ + u64 log_next_seq; + u32 log_next_idx; + + /* the next printk record to read after the last 'clear' command */ + u64 clear_seq; + u32 clear_idx; + + char *log_buf; + u32 log_buf_len; + + /* the next printk record to write to the console */ + u64 console_seq; + u32 console_idx; + + /* the next printk record to read by syslog(READ) or /proc/kmsg */ + u64 syslog_seq; + u32 syslog_idx; + int syslog_prev; + size_t syslog_partial; +}; + +extern struct syslog_namespace init_syslog_ns; + +#ifdef CONFIG_SYSLOG_NS +extern void free_syslog_ns(struct kref *kref); +static inline struct syslog_namespace *get_syslog_ns( + struct syslog_namespace *ns) +{ + if (ns != &init_syslog_ns) + kref_get(&ns->kref); + return ns; +} + +static inline void put_syslog_ns(struct syslog_namespace *ns) +{ + if (ns != &init_syslog_ns) + kref_put(&ns->kref, free_syslog_ns); +} + +#else +static inline struct syslog_namespace *get_syslog_ns( + struct syslog_namespace *ns) +{ + return ns; +} + +static inline void put_syslog_ns(struct syslog_namespace *ns) +{ +} +#endif + +#endif diff --git a/init/Kconfig b/init/Kconfig index 6fdd6e3..82771e0 100644 --- a/init/Kconfig +++ b/init/Kconfig @@ -988,6 +988,13 @@ config NET_NS Allow user space to create what appear to be multiple instances of the network stack. +config SYSLOG_NS + bool "Syslog namespace" + default y + help + Allow containers to use syslog namespaces to provide different + syslog for containers. + endif # NAMESPACES config UIDGID_CONVERTED diff --git a/kernel/Makefile b/kernel/Makefile index 0dfeca4..cb3cba0 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -28,6 +28,7 @@ obj-y += power/ ifeq ($(CONFIG_CHECKPOINT_RESTORE),y) obj-$(CONFIG_X86) += kcmp.o endif +obj-$(CONFIG_SYSLOG_NS) += syslog_namespace.o obj-$(CONFIG_FREEZER) += freezer.o obj-$(CONFIG_PROFILING) += profile.o obj-$(CONFIG_STACKTRACE) += stacktrace.o diff --git a/kernel/syslog_namespace.c b/kernel/syslog_namespace.c new file mode 100644 index 0000000..9482927 --- /dev/null +++ b/kernel/syslog_namespace.c @@ -0,0 +1,31 @@ +/* + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation, version 2 of the + * License. + */ + +#include +#include +#include + +static char __log_buf[__LOG_BUF_LEN] __aligned(LOG_ALIGN); + +struct syslog_namespace init_syslog_ns = { + .kref = { + .refcount = ATOMIC_INIT(2), + }, + .logbuf_lock = __RAW_SPIN_LOCK_UNLOCKED(init_syslog_ns.logbuf_lock), + .log_buf_len = __LOG_BUF_LEN, + .log_buf = __log_buf, +}; +EXPORT_SYMBOL_GPL(init_syslog_ns); + +void free_syslog_ns(struct kref *kref) +{ + struct syslog_namespace *ns; + ns = container_of(kref, struct syslog_namespace, kref); + + kfree(ns->log_buf); + kfree(ns); +}