License cleanup: add SPDX GPL-2.0 license identifier to files with no license
Many source files in the tree are missing licensing information, which
makes it harder for compliance tools to determine the correct license.
By default all files without license information are under the default
license of the kernel, which is GPL version 2.
Update the files which contain no license information with the 'GPL-2.0'
SPDX license identifier. The SPDX identifier is a legally binding
shorthand, which can be used instead of the full boiler plate text.
This patch is based on work done by Thomas Gleixner and Kate Stewart and
Philippe Ombredanne.
How this work was done:
Patches were generated and checked against linux-4.14-rc6 for a subset of
the use cases:
- file had no licensing information it it.
- file was a */uapi/* one with no licensing information in it,
- file was a */uapi/* one with existing licensing information,
Further patches will be generated in subsequent months to fix up cases
where non-standard license headers were used, and references to license
had to be inferred by heuristics based on keywords.
The analysis to determine which SPDX License Identifier to be applied to
a file was done in a spreadsheet of side by side results from of the
output of two independent scanners (ScanCode & Windriver) producing SPDX
tag:value files created by Philippe Ombredanne. Philippe prepared the
base worksheet, and did an initial spot review of a few 1000 files.
The 4.13 kernel was the starting point of the analysis with 60,537 files
assessed. Kate Stewart did a file by file comparison of the scanner
results in the spreadsheet to determine which SPDX license identifier(s)
to be applied to the file. She confirmed any determination that was not
immediately clear with lawyers working with the Linux Foundation.
Criteria used to select files for SPDX license identifier tagging was:
- Files considered eligible had to be source code files.
- Make and config files were included as candidates if they contained >5
lines of source
- File already had some variant of a license header in it (even if <5
lines).
All documentation files were explicitly excluded.
The following heuristics were used to determine which SPDX license
identifiers to apply.
- when both scanners couldn't find any license traces, file was
considered to have no license information in it, and the top level
COPYING file license applied.
For non */uapi/* files that summary was:
SPDX license identifier # files
---------------------------------------------------|-------
GPL-2.0 11139
and resulted in the first patch in this series.
If that file was a */uapi/* path one, it was "GPL-2.0 WITH
Linux-syscall-note" otherwise it was "GPL-2.0". Results of that was:
SPDX license identifier # files
---------------------------------------------------|-------
GPL-2.0 WITH Linux-syscall-note 930
and resulted in the second patch in this series.
- if a file had some form of licensing information in it, and was one
of the */uapi/* ones, it was denoted with the Linux-syscall-note if
any GPL family license was found in the file or had no licensing in
it (per prior point). Results summary:
SPDX license identifier # files
---------------------------------------------------|------
GPL-2.0 WITH Linux-syscall-note 270
GPL-2.0+ WITH Linux-syscall-note 169
((GPL-2.0 WITH Linux-syscall-note) OR BSD-2-Clause) 21
((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause) 17
LGPL-2.1+ WITH Linux-syscall-note 15
GPL-1.0+ WITH Linux-syscall-note 14
((GPL-2.0+ WITH Linux-syscall-note) OR BSD-3-Clause) 5
LGPL-2.0+ WITH Linux-syscall-note 4
LGPL-2.1 WITH Linux-syscall-note 3
((GPL-2.0 WITH Linux-syscall-note) OR MIT) 3
((GPL-2.0 WITH Linux-syscall-note) AND MIT) 1
and that resulted in the third patch in this series.
- when the two scanners agreed on the detected license(s), that became
the concluded license(s).
- when there was disagreement between the two scanners (one detected a
license but the other didn't, or they both detected different
licenses) a manual inspection of the file occurred.
- In most cases a manual inspection of the information in the file
resulted in a clear resolution of the license that should apply (and
which scanner probably needed to revisit its heuristics).
- When it was not immediately clear, the license identifier was
confirmed with lawyers working with the Linux Foundation.
- If there was any question as to the appropriate license identifier,
the file was flagged for further research and to be revisited later
in time.
In total, over 70 hours of logged manual review was done on the
spreadsheet to determine the SPDX license identifiers to apply to the
source files by Kate, Philippe, Thomas and, in some cases, confirmation
by lawyers working with the Linux Foundation.
Kate also obtained a third independent scan of the 4.13 code base from
FOSSology, and compared selected files where the other two scanners
disagreed against that SPDX file, to see if there was new insights. The
Windriver scanner is based on an older version of FOSSology in part, so
they are related.
Thomas did random spot checks in about 500 files from the spreadsheets
for the uapi headers and agreed with SPDX license identifier in the
files he inspected. For the non-uapi files Thomas did random spot checks
in about 15000 files.
In initial set of patches against 4.14-rc6, 3 files were found to have
copy/paste license identifier errors, and have been fixed to reflect the
correct identifier.
Additionally Philippe spent 10 hours this week doing a detailed manual
inspection and review of the 12,461 patched files from the initial patch
version early this week with:
- a full scancode scan run, collecting the matched texts, detected
license ids and scores
- reviewing anything where there was a license detected (about 500+
files) to ensure that the applied SPDX license was correct
- reviewing anything where there was no detection but the patch license
was not GPL-2.0 WITH Linux-syscall-note to ensure that the applied
SPDX license was correct
This produced a worksheet with 20 files needing minor correction. This
worksheet was then exported into 3 different .csv files for the
different types of files to be modified.
These .csv files were then reviewed by Greg. Thomas wrote a script to
parse the csv files and add the proper SPDX tag to the file, in the
format that the file expected. This script was further refined by Greg
based on the output to detect more types of files automatically and to
distinguish between header and source .c files (which need different
comment types.) Finally Greg ran the script using the .csv files to
generate the patches.
Reviewed-by: Kate Stewart <kstewart@linuxfoundation.org>
Reviewed-by: Philippe Ombredanne <pombredanne@nexb.com>
Reviewed-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-11-01 17:07:57 +03:00
// SPDX-License-Identifier: GPL-2.0
2007-02-14 11:34:12 +03:00
/*
* / proc / sys support
*/
2008-10-17 05:07:44 +04:00
# include <linux/init.h>
2007-02-14 11:34:12 +03:00
# include <linux/sysctl.h>
2011-11-03 00:39:22 +04:00
# include <linux/poll.h>
2007-02-14 11:34:12 +03:00
# include <linux/proc_fs.h>
2013-02-28 05:03:16 +04:00
# include <linux/printk.h>
2007-02-14 11:34:12 +03:00
# include <linux/security.h>
2012-02-13 07:58:52 +04:00
# include <linux/sched.h>
2017-02-02 19:54:15 +03:00
# include <linux/cred.h>
2011-01-07 09:49:57 +03:00
# include <linux/namei.h>
2012-02-13 07:58:52 +04:00
# include <linux/mm.h>
2020-09-03 17:22:32 +03:00
# include <linux/uio.h>
2012-01-06 16:07:15 +04:00
# include <linux/module.h>
2019-02-27 23:59:24 +03:00
# include <linux/bpf-cgroup.h>
kernel/sysctl: support setting sysctl parameters from kernel command line
Patch series "support setting sysctl parameters from kernel command line", v3.
This series adds support for something that seems like many people
always wanted but nobody added it yet, so here's the ability to set
sysctl parameters via kernel command line options in the form of
sysctl.vm.something=1
The important part is Patch 1. The second, not so important part is an
attempt to clean up legacy one-off parameters that do the same thing as
a sysctl. I don't want to remove them completely for compatibility
reasons, but with generic sysctl support the idea is to remove the
one-off param handlers and treat the parameters as aliases for the
sysctl variants.
I have identified several parameters that mention sysctl counterparts in
Documentation/admin-guide/kernel-parameters.txt but there might be more.
The conversion also has varying level of success:
- numa_zonelist_order is converted in Patch 2 together with adding the
necessary infrastructure. It's easy as it doesn't really do anything
but warn on deprecated value these days.
- hung_task_panic is converted in Patch 3, but there's a downside that
now it only accepts 0 and 1, while previously it was any integer
value
- nmi_watchdog maps to two sysctls nmi_watchdog and hardlockup_panic,
so there's no straighforward conversion possible
- traceoff_on_warning is a flag without value and it would be required
to handle that somehow in the conversion infractructure, which seems
pointless for a single flag
This patch (of 5):
A recently proposed patch to add vm_swappiness command line parameter in
addition to existing sysctl [1] made me wonder why we don't have a
general support for passing sysctl parameters via command line.
Googling found only somebody else wondering the same [2], but I haven't
found any prior discussion with reasons why not to do this.
Settings the vm_swappiness issue aside (the underlying issue might be
solved in a different way), quick search of kernel-parameters.txt shows
there are already some that exist as both sysctl and kernel parameter -
hung_task_panic, nmi_watchdog, numa_zonelist_order, traceoff_on_warning.
A general mechanism would remove the need to add more of those one-offs
and might be handy in situations where configuration by e.g.
/etc/sysctl.d/ is impractical.
Hence, this patch adds a new parse_args() pass that looks for parameters
prefixed by 'sysctl.' and tries to interpret them as writes to the
corresponding sys/ files using an temporary in-kernel procfs mount.
This mechanism was suggested by Eric W. Biederman [3], as it handles
all dynamically registered sysctl tables, even though we don't handle
modular sysctls. Errors due to e.g. invalid parameter name or value
are reported in the kernel log.
The processing is hooked right before the init process is loaded, as
some handlers might be more complicated than simple setters and might
need some subsystems to be initialized. At the moment the init process
can be started and eventually execute a process writing to /proc/sys/
then it should be also fine to do that from the kernel.
Sysctls registered later on module load time are not set by this
mechanism - it's expected that in such scenarios, setting sysctl values
from userspace is practical enough.
[1] https://lore.kernel.org/r/BL0PR02MB560167492CA4094C91589930E9FC0@BL0PR02MB5601.namprd02.prod.outlook.com/
[2] https://unix.stackexchange.com/questions/558802/how-to-set-sysctl-using-kernel-command-line-parameter
[3] https://lore.kernel.org/r/87bloj2skm.fsf@x220.int.ebiederm.org/
Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Reviewed-by: Luis Chamberlain <mcgrof@kernel.org>
Reviewed-by: Masami Hiramatsu <mhiramat@kernel.org>
Acked-by: Kees Cook <keescook@chromium.org>
Acked-by: Michal Hocko <mhocko@suse.com>
Cc: Iurii Zaikin <yzaikin@google.com>
Cc: Ivan Teterevkov <ivan.teterevkov@nutanix.com>
Cc: Michal Hocko <mhocko@kernel.org>
Cc: David Rientjes <rientjes@google.com>
Cc: Matthew Wilcox <willy@infradead.org>
Cc: "Eric W . Biederman" <ebiederm@xmission.com>
Cc: "Guilherme G . Piccoli" <gpiccoli@canonical.com>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Christian Brauner <christian.brauner@ubuntu.com>
Link: http://lkml.kernel.org/r/20200427180433.7029-1-vbabka@suse.cz
Link: http://lkml.kernel.org/r/20200427180433.7029-2-vbabka@suse.cz
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-06-08 07:40:24 +03:00
# include <linux/mount.h>
2022-01-22 09:10:50 +03:00
# include <linux/kmemleak.h>
2007-02-14 11:34:12 +03:00
# include "internal.h"
2022-04-11 08:12:05 +03:00
# define list_for_each_table_entry(entry, table) \
for ( ( entry ) = ( table ) ; ( entry ) - > procname ; ( entry ) + + )
2009-02-20 08:58:47 +03:00
static const struct dentry_operations proc_sys_dentry_operations ;
2007-02-14 11:34:12 +03:00
static const struct file_operations proc_sys_file_operations ;
2008-02-08 15:21:19 +03:00
static const struct inode_operations proc_sys_inode_operations ;
2008-07-15 16:54:06 +04:00
static const struct file_operations proc_sys_dir_file_operations ;
static const struct inode_operations proc_sys_dir_operations ;
2007-02-14 11:34:12 +03:00
2015-05-10 06:09:14 +03:00
/* Support for permanently empty directories */
struct ctl_table sysctl_mount_point [ ] = {
{ }
} ;
2022-01-22 09:12:23 +03:00
/**
* register_sysctl_mount_point ( ) - registers a sysctl mount point
* @ path : path for the mount point
*
* Used to create a permanently empty directory to serve as mount point .
* There are some subtle but important permission checks this allows in the
* case of unprivileged mounts .
*/
struct ctl_table_header * register_sysctl_mount_point ( const char * path )
{
return register_sysctl ( path , sysctl_mount_point ) ;
}
EXPORT_SYMBOL ( register_sysctl_mount_point ) ;
2015-05-10 06:09:14 +03:00
static bool is_empty_dir ( struct ctl_table_header * head )
{
return head - > ctl_table [ 0 ] . child = = sysctl_mount_point ;
}
static void set_empty_dir ( struct ctl_dir * dir )
{
dir - > header . ctl_table [ 0 ] . child = sysctl_mount_point ;
}
static void clear_empty_dir ( struct ctl_dir * dir )
{
dir - > header . ctl_table [ 0 ] . child = NULL ;
}
2011-11-03 00:39:22 +04:00
void proc_sys_poll_notify ( struct ctl_table_poll * poll )
{
if ( ! poll )
return ;
atomic_inc ( & poll - > event ) ;
wake_up_interruptible ( & poll - > wait ) ;
}
2012-01-22 05:51:48 +04:00
static struct ctl_table root_table [ ] = {
{
. procname = " " ,
2011-12-29 20:24:29 +04:00
. mode = S_IFDIR | S_IRUGO | S_IXUGO ,
2012-01-22 05:51:48 +04:00
} ,
{ }
} ;
2012-01-08 11:24:30 +04:00
static struct ctl_table_root sysctl_table_root = {
. default_set . dir . header = {
2011-12-29 20:24:29 +04:00
{ { . count = 1 ,
. nreg = 1 ,
2012-01-10 05:24:30 +04:00
. ctl_table = root_table } } ,
2012-01-08 11:24:30 +04:00
. ctl_table_arg = root_table ,
2011-12-29 20:24:29 +04:00
. root = & sysctl_table_root ,
. set = & sysctl_table_root . default_set ,
} ,
2012-01-06 16:07:15 +04:00
} ;
static DEFINE_SPINLOCK ( sysctl_lock ) ;
2011-12-29 20:24:29 +04:00
static void drop_sysctl_table ( struct ctl_table_header * header ) ;
2012-01-08 11:24:30 +04:00
static int sysctl_follow_link ( struct ctl_table_header * * phead ,
2016-07-16 23:22:55 +03:00
struct ctl_table * * pentry ) ;
2012-01-08 11:24:30 +04:00
static int insert_links ( struct ctl_table_header * head ) ;
static void put_links ( struct ctl_table_header * header ) ;
2011-12-29 20:24:29 +04:00
2012-01-22 08:09:45 +04:00
static void sysctl_print_dir ( struct ctl_dir * dir )
{
if ( dir - > header . parent )
sysctl_print_dir ( dir - > header . parent ) ;
2013-02-28 05:03:16 +04:00
pr_cont ( " %s/ " , dir - > header . ctl_table [ 0 ] . procname ) ;
2012-01-22 08:09:45 +04:00
}
2012-01-10 09:42:02 +04:00
static int namecmp ( const char * name1 , int len1 , const char * name2 , int len2 )
{
int cmp ;
2021-01-04 11:32:21 +03:00
cmp = memcmp ( name1 , name2 , min ( len1 , len2 ) ) ;
2012-01-10 09:42:02 +04:00
if ( cmp = = 0 )
cmp = len1 - len2 ;
return cmp ;
}
2012-01-31 09:23:52 +04:00
/* Called under sysctl_lock */
2012-01-10 09:42:02 +04:00
static struct ctl_table * find_entry ( struct ctl_table_header * * phead ,
2012-01-08 11:24:30 +04:00
struct ctl_dir * dir , const char * name , int namelen )
2012-01-10 09:42:02 +04:00
{
struct ctl_table_header * head ;
struct ctl_table * entry ;
2012-01-10 05:24:30 +04:00
struct rb_node * node = dir - > root . rb_node ;
2012-01-10 09:42:02 +04:00
2012-01-10 05:24:30 +04:00
while ( node )
{
struct ctl_node * ctl_node ;
const char * procname ;
int cmp ;
ctl_node = rb_entry ( node , struct ctl_node , node ) ;
head = ctl_node - > header ;
entry = & head - > ctl_table [ ctl_node - head - > node ] ;
procname = entry - > procname ;
cmp = namecmp ( name , namelen , procname , strlen ( procname ) ) ;
if ( cmp < 0 )
node = node - > rb_left ;
else if ( cmp > 0 )
node = node - > rb_right ;
else {
* phead = head ;
return entry ;
2012-01-10 09:42:02 +04:00
}
}
return NULL ;
}
2012-01-10 05:24:30 +04:00
static int insert_entry ( struct ctl_table_header * head , struct ctl_table * entry )
{
struct rb_node * node = & head - > node [ entry - head - > ctl_table ] . node ;
struct rb_node * * p = & head - > parent - > root . rb_node ;
struct rb_node * parent = NULL ;
const char * name = entry - > procname ;
int namelen = strlen ( name ) ;
while ( * p ) {
struct ctl_table_header * parent_head ;
struct ctl_table * parent_entry ;
struct ctl_node * parent_node ;
const char * parent_name ;
int cmp ;
parent = * p ;
parent_node = rb_entry ( parent , struct ctl_node , node ) ;
parent_head = parent_node - > header ;
parent_entry = & parent_head - > ctl_table [ parent_node - parent_head - > node ] ;
parent_name = parent_entry - > procname ;
cmp = namecmp ( name , namelen , parent_name , strlen ( parent_name ) ) ;
if ( cmp < 0 )
p = & ( * p ) - > rb_left ;
else if ( cmp > 0 )
p = & ( * p ) - > rb_right ;
else {
2013-02-28 05:03:16 +04:00
pr_err ( " sysctl duplicate entry: " ) ;
2012-01-10 05:24:30 +04:00
sysctl_print_dir ( head - > parent ) ;
2022-01-20 05:08:06 +03:00
pr_cont ( " %s \n " , entry - > procname ) ;
2012-01-10 05:24:30 +04:00
return - EEXIST ;
}
}
rb_link_node ( node , parent , p ) ;
2012-10-09 03:30:35 +04:00
rb_insert_color ( node , & head - > parent - > root ) ;
2012-01-10 05:24:30 +04:00
return 0 ;
}
static void erase_entry ( struct ctl_table_header * head , struct ctl_table * entry )
{
struct rb_node * node = & head - > node [ entry - head - > ctl_table ] . node ;
rb_erase ( node , & head - > parent - > root ) ;
}
2012-01-10 10:36:41 +04:00
static void init_header ( struct ctl_table_header * head ,
struct ctl_table_root * root , struct ctl_table_set * set ,
2012-01-10 05:24:30 +04:00
struct ctl_node * node , struct ctl_table * table )
2012-01-10 10:36:41 +04:00
{
2011-12-29 20:24:29 +04:00
head - > ctl_table = table ;
2012-01-10 10:36:41 +04:00
head - > ctl_table_arg = table ;
head - > used = 0 ;
head - > count = 1 ;
head - > nreg = 1 ;
head - > unregistering = NULL ;
head - > root = root ;
head - > set = set ;
head - > parent = NULL ;
2012-01-10 05:24:30 +04:00
head - > node = node ;
2017-07-06 16:41:06 +03:00
INIT_HLIST_HEAD ( & head - > inodes ) ;
2012-01-10 05:24:30 +04:00
if ( node ) {
struct ctl_table * entry ;
2022-04-11 08:12:05 +03:00
list_for_each_table_entry ( entry , table ) {
2012-01-10 05:24:30 +04:00
node - > header = head ;
2022-04-11 08:12:05 +03:00
node + + ;
}
2012-01-10 05:24:30 +04:00
}
2012-01-10 10:36:41 +04:00
}
2012-01-10 05:35:01 +04:00
static void erase_header ( struct ctl_table_header * head )
{
2012-01-10 05:24:30 +04:00
struct ctl_table * entry ;
2022-04-11 08:12:05 +03:00
list_for_each_table_entry ( entry , head - > ctl_table )
2012-01-10 05:24:30 +04:00
erase_entry ( head , entry ) ;
2012-01-10 05:35:01 +04:00
}
2012-01-08 11:24:30 +04:00
static int insert_header ( struct ctl_dir * dir , struct ctl_table_header * header )
2012-01-10 05:35:01 +04:00
{
2012-01-10 05:24:30 +04:00
struct ctl_table * entry ;
2012-01-08 11:24:30 +04:00
int err ;
2015-05-10 06:09:14 +03:00
/* Is this a permanently empty directory? */
if ( is_empty_dir ( & dir - > header ) )
return - EROFS ;
/* Am I creating a permanently empty directory? */
if ( header - > ctl_table = = sysctl_mount_point ) {
if ( ! RB_EMPTY_ROOT ( & dir - > root ) )
return - EINVAL ;
set_empty_dir ( dir ) ;
}
2012-01-08 11:24:30 +04:00
dir - > header . nreg + + ;
2011-12-29 20:24:29 +04:00
header - > parent = dir ;
2012-01-08 11:24:30 +04:00
err = insert_links ( header ) ;
if ( err )
goto fail_links ;
2022-04-11 08:12:05 +03:00
list_for_each_table_entry ( entry , header - > ctl_table ) {
2012-01-10 05:24:30 +04:00
err = insert_entry ( header , entry ) ;
if ( err )
goto fail ;
}
2012-01-08 11:24:30 +04:00
return 0 ;
2012-01-10 05:24:30 +04:00
fail :
erase_header ( header ) ;
put_links ( header ) ;
2012-01-08 11:24:30 +04:00
fail_links :
2015-05-10 06:09:14 +03:00
if ( header - > ctl_table = = sysctl_mount_point )
clear_empty_dir ( dir ) ;
2012-01-08 11:24:30 +04:00
header - > parent = NULL ;
drop_sysctl_table ( & dir - > header ) ;
return err ;
2012-01-10 05:35:01 +04:00
}
2012-01-06 16:07:15 +04:00
/* called under sysctl_lock */
static int use_table ( struct ctl_table_header * p )
{
if ( unlikely ( p - > unregistering ) )
return 0 ;
p - > used + + ;
return 1 ;
}
/* called under sysctl_lock */
static void unuse_table ( struct ctl_table_header * p )
{
if ( ! - - p - > used )
if ( unlikely ( p - > unregistering ) )
complete ( p - > unregistering ) ;
}
2020-02-21 17:43:23 +03:00
static void proc_sys_invalidate_dcache ( struct ctl_table_header * head )
2017-02-10 10:35:02 +03:00
{
2020-02-21 17:43:23 +03:00
proc_invalidate_siblings_dcache ( & head - > inodes , & sysctl_lock ) ;
2017-02-10 10:35:02 +03:00
}
2012-01-06 16:07:15 +04:00
/* called under sysctl_lock, will reacquire if has to wait */
static void start_unregistering ( struct ctl_table_header * p )
{
/*
* if p - > used is 0 , nobody will ever touch that entry again ;
* we ' ll eliminate all paths to it before dropping sysctl_lock
*/
if ( unlikely ( p - > used ) ) {
struct completion wait ;
init_completion ( & wait ) ;
p - > unregistering = & wait ;
spin_unlock ( & sysctl_lock ) ;
wait_for_completion ( & wait ) ;
} else {
/* anything non-NULL; we'll never dereference it */
p - > unregistering = ERR_PTR ( - EINVAL ) ;
2017-02-20 08:17:03 +03:00
spin_unlock ( & sysctl_lock ) ;
2012-01-06 16:07:15 +04:00
}
2017-02-10 10:35:02 +03:00
/*
2020-02-21 17:43:23 +03:00
* Invalidate dentries for unregistered sysctls : namespaced sysctls
2017-02-10 10:35:02 +03:00
* can have duplicate names and contaminate dcache very badly .
*/
2020-02-21 17:43:23 +03:00
proc_sys_invalidate_dcache ( p ) ;
2012-01-06 16:07:15 +04:00
/*
* do not remove from the list until nobody holds it ; walking the
* list in do_sysctl ( ) relies on that .
*/
2017-02-20 08:17:03 +03:00
spin_lock ( & sysctl_lock ) ;
2012-01-10 05:35:01 +04:00
erase_header ( p ) ;
2012-01-06 16:07:15 +04:00
}
static struct ctl_table_header * sysctl_head_grab ( struct ctl_table_header * head )
{
2012-10-05 04:15:45 +04:00
BUG_ON ( ! head ) ;
2012-01-06 16:07:15 +04:00
spin_lock ( & sysctl_lock ) ;
if ( ! use_table ( head ) )
head = ERR_PTR ( - ENOENT ) ;
spin_unlock ( & sysctl_lock ) ;
return head ;
}
static void sysctl_head_finish ( struct ctl_table_header * head )
{
if ( ! head )
return ;
spin_lock ( & sysctl_lock ) ;
unuse_table ( head ) ;
spin_unlock ( & sysctl_lock ) ;
}
static struct ctl_table_set *
2016-07-16 23:22:55 +03:00
lookup_header_set ( struct ctl_table_root * root )
2012-01-06 16:07:15 +04:00
{
struct ctl_table_set * set = & root - > default_set ;
if ( root - > lookup )
2016-07-16 23:22:55 +03:00
set = root - > lookup ( root ) ;
2012-01-06 16:07:15 +04:00
return set ;
}
2012-01-10 09:42:02 +04:00
static struct ctl_table * lookup_entry ( struct ctl_table_header * * phead ,
2011-12-29 20:24:29 +04:00
struct ctl_dir * dir ,
2012-01-10 09:42:02 +04:00
const char * name , int namelen )
{
struct ctl_table_header * head ;
struct ctl_table * entry ;
spin_lock ( & sysctl_lock ) ;
2012-01-08 11:24:30 +04:00
entry = find_entry ( & head , dir , name , namelen ) ;
if ( entry & & use_table ( head ) )
* phead = head ;
else
entry = NULL ;
2012-01-10 09:42:02 +04:00
spin_unlock ( & sysctl_lock ) ;
return entry ;
}
2012-01-10 05:24:30 +04:00
static struct ctl_node * first_usable_entry ( struct rb_node * node )
2012-01-06 16:07:15 +04:00
{
2012-01-10 05:24:30 +04:00
struct ctl_node * ctl_node ;
2012-01-06 16:07:15 +04:00
2012-01-10 05:24:30 +04:00
for ( ; node ; node = rb_next ( node ) ) {
ctl_node = rb_entry ( node , struct ctl_node , node ) ;
if ( use_table ( ctl_node - > header ) )
return ctl_node ;
2012-01-06 16:07:15 +04:00
}
return NULL ;
}
2011-12-29 20:24:29 +04:00
static void first_entry ( struct ctl_dir * dir ,
2012-01-18 15:15:51 +04:00
struct ctl_table_header * * phead , struct ctl_table * * pentry )
{
2012-01-10 05:24:30 +04:00
struct ctl_table_header * head = NULL ;
2011-12-29 20:24:29 +04:00
struct ctl_table * entry = NULL ;
2012-01-10 05:24:30 +04:00
struct ctl_node * ctl_node ;
2012-01-18 15:15:51 +04:00
spin_lock ( & sysctl_lock ) ;
2012-01-10 05:24:30 +04:00
ctl_node = first_usable_entry ( rb_first ( & dir - > root ) ) ;
2012-01-18 15:15:51 +04:00
spin_unlock ( & sysctl_lock ) ;
2012-01-10 05:24:30 +04:00
if ( ctl_node ) {
head = ctl_node - > header ;
entry = & head - > ctl_table [ ctl_node - head - > node ] ;
}
2012-01-18 15:15:51 +04:00
* phead = head ;
* pentry = entry ;
}
2011-12-29 20:24:29 +04:00
static void next_entry ( struct ctl_table_header * * phead , struct ctl_table * * pentry )
2012-01-06 16:07:15 +04:00
{
2012-01-18 15:15:51 +04:00
struct ctl_table_header * head = * phead ;
struct ctl_table * entry = * pentry ;
2012-01-10 05:24:30 +04:00
struct ctl_node * ctl_node = & head - > node [ entry - head - > ctl_table ] ;
2012-01-18 15:15:51 +04:00
2012-01-10 05:24:30 +04:00
spin_lock ( & sysctl_lock ) ;
unuse_table ( head ) ;
ctl_node = first_usable_entry ( rb_next ( & ctl_node - > node ) ) ;
spin_unlock ( & sysctl_lock ) ;
head = NULL ;
if ( ctl_node ) {
head = ctl_node - > header ;
entry = & head - > ctl_table [ ctl_node - head - > node ] ;
2012-01-18 15:15:51 +04:00
}
* phead = head ;
* pentry = entry ;
2012-01-06 16:07:15 +04:00
}
/*
* sysctl_perm does NOT grant the superuser all rights automatically , because
* some sysctl variables are readonly even to root .
*/
static int test_perm ( int mode , int op )
{
2012-02-14 06:02:50 +04:00
if ( uid_eq ( current_euid ( ) , GLOBAL_ROOT_UID ) )
2012-01-06 16:07:15 +04:00
mode > > = 6 ;
2012-02-14 06:02:50 +04:00
else if ( in_egroup_p ( GLOBAL_ROOT_GID ) )
2012-01-06 16:07:15 +04:00
mode > > = 3 ;
if ( ( op & ~ mode & ( MAY_READ | MAY_WRITE | MAY_EXEC ) ) = = 0 )
return 0 ;
return - EACCES ;
}
2012-11-16 07:02:58 +04:00
static int sysctl_perm ( struct ctl_table_header * head , struct ctl_table * table , int op )
2012-01-06 16:07:15 +04:00
{
2012-11-16 07:02:58 +04:00
struct ctl_table_root * root = head - > root ;
2012-01-06 16:07:15 +04:00
int mode ;
if ( root - > permissions )
2012-11-16 07:02:58 +04:00
mode = root - > permissions ( head , table ) ;
2012-01-06 16:07:15 +04:00
else
mode = table - > mode ;
return test_perm ( mode , op ) ;
}
2008-07-15 16:54:06 +04:00
static struct inode * proc_sys_make_inode ( struct super_block * sb ,
struct ctl_table_header * head , struct ctl_table * table )
2007-02-14 11:34:12 +03:00
{
2016-08-11 00:36:02 +03:00
struct ctl_table_root * root = head - > root ;
2007-02-14 11:34:12 +03:00
struct inode * inode ;
2008-07-15 16:54:06 +04:00
struct proc_inode * ei ;
2007-02-14 11:34:12 +03:00
2008-07-15 16:54:06 +04:00
inode = new_inode ( sb ) ;
2007-02-14 11:34:12 +03:00
if ( ! inode )
2018-12-14 02:20:52 +03:00
return ERR_PTR ( - ENOMEM ) ;
2007-02-14 11:34:12 +03:00
2010-10-23 19:19:54 +04:00
inode - > i_ino = get_next_ino ( ) ;
2007-02-14 11:34:12 +03:00
ei = PROC_I ( inode ) ;
2008-07-15 16:54:06 +04:00
2017-02-10 10:35:02 +03:00
spin_lock ( & sysctl_lock ) ;
2017-02-20 08:17:03 +03:00
if ( unlikely ( head - > unregistering ) ) {
spin_unlock ( & sysctl_lock ) ;
iput ( inode ) ;
2018-12-14 02:20:52 +03:00
return ERR_PTR ( - ENOENT ) ;
2017-02-20 08:17:03 +03:00
}
ei - > sysctl = head ;
ei - > sysctl_entry = table ;
2020-02-20 02:17:34 +03:00
hlist_add_head_rcu ( & ei - > sibling_inodes , & head - > inodes ) ;
2017-02-10 10:35:02 +03:00
head - > count + + ;
spin_unlock ( & sysctl_lock ) ;
2016-09-14 17:48:04 +03:00
inode - > i_mtime = inode - > i_atime = inode - > i_ctime = current_time ( inode ) ;
2008-07-15 16:54:06 +04:00
inode - > i_mode = table - > mode ;
2011-12-29 20:24:29 +04:00
if ( ! S_ISDIR ( table - > mode ) ) {
2008-07-15 16:54:06 +04:00
inode - > i_mode | = S_IFREG ;
inode - > i_op = & proc_sys_inode_operations ;
inode - > i_fop = & proc_sys_file_operations ;
} else {
inode - > i_mode | = S_IFDIR ;
inode - > i_op = & proc_sys_dir_operations ;
inode - > i_fop = & proc_sys_dir_file_operations ;
2015-05-10 06:09:14 +03:00
if ( is_empty_dir ( head ) )
make_empty_dir_inode ( inode ) ;
2008-07-15 16:54:06 +04:00
}
2016-08-11 00:36:02 +03:00
if ( root - > set_ownership )
root - > set_ownership ( head , table , & inode - > i_uid , & inode - > i_gid ) ;
fs/proc/proc_sysctl.c: fix the default values of i_uid/i_gid on /proc/sys inodes.
Normally, the inode's i_uid/i_gid are translated relative to s_user_ns,
but this is not a correct behavior for proc. Since sysctl permission
check in test_perm is done against GLOBAL_ROOT_[UG]ID, it makes more
sense to use these values in u_[ug]id of proc inodes. In other words:
although uid/gid in the inode is not read during test_perm, the inode
logically belongs to the root of the namespace. I have confirmed this
with Eric Biederman at LPC and in this thread:
https://lore.kernel.org/lkml/87k1kzjdff.fsf@xmission.com
Consequences
============
Since the i_[ug]id values of proc nodes are not used for permissions
checks, this change usually makes no functional difference. However, it
causes an issue in a setup where:
* a namespace container is created without root user in container -
hence the i_[ug]id of proc nodes are set to INVALID_[UG]ID
* container creator tries to configure it by writing /proc/sys files,
e.g. writing /proc/sys/kernel/shmmax to configure shared memory limit
Kernel does not allow to open an inode for writing if its i_[ug]id are
invalid, making it impossible to write shmmax and thus - configure the
container.
Using a container with no root mapping is apparently rare, but we do use
this configuration at Google. Also, we use a generic tool to configure
the container limits, and the inability to write any of them causes a
failure.
History
=======
The invalid uids/gids in inodes first appeared due to 81754357770e (fs:
Update i_[ug]id_(read|write) to translate relative to s_user_ns).
However, AFAIK, this did not immediately cause any issues. The
inability to write to these "invalid" inodes was only caused by a later
commit 0bd23d09b874 (vfs: Don't modify inodes with a uid or gid unknown
to the vfs).
Tested: Used a repro program that creates a user namespace without any
mapping and stat'ed /proc/$PID/root/proc/sys/kernel/shmmax from outside.
Before the change, it shows the overflow uid, with the change it's 0.
The overflow uid indicates that the uid in the inode is not correct and
thus it is not possible to open the file for writing.
Link: http://lkml.kernel.org/r/20190708115130.250149-1-rburny@google.com
Fixes: 0bd23d09b874 ("vfs: Don't modify inodes with a uid or gid unknown to the vfs")
Signed-off-by: Radoslaw Burny <rburny@google.com>
Acked-by: Luis Chamberlain <mcgrof@kernel.org>
Cc: Kees Cook <keescook@chromium.org>
Cc: "Eric W . Biederman" <ebiederm@xmission.com>
Cc: Seth Forshee <seth.forshee@canonical.com>
Cc: John Sperbeck <jsperbeck@google.com>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: <stable@vger.kernel.org> [4.8+]
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2019-07-17 02:26:51 +03:00
else {
inode - > i_uid = GLOBAL_ROOT_UID ;
inode - > i_gid = GLOBAL_ROOT_GID ;
}
2016-08-11 00:36:02 +03:00
2007-02-14 11:34:12 +03:00
return inode ;
}
2017-02-10 10:35:02 +03:00
void proc_sys_evict_inode ( struct inode * inode , struct ctl_table_header * head )
{
spin_lock ( & sysctl_lock ) ;
2020-02-20 02:17:34 +03:00
hlist_del_init_rcu ( & PROC_I ( inode ) - > sibling_inodes ) ;
2017-02-10 10:35:02 +03:00
if ( ! - - head - > count )
kfree_rcu ( head , rcu ) ;
spin_unlock ( & sysctl_lock ) ;
}
2008-10-03 00:33:54 +04:00
static struct ctl_table_header * grab_header ( struct inode * inode )
2007-02-14 11:34:12 +03:00
{
2012-01-07 18:57:47 +04:00
struct ctl_table_header * head = PROC_I ( inode ) - > sysctl ;
if ( ! head )
2012-01-08 11:24:30 +04:00
head = & sysctl_table_root . default_set . dir . header ;
2012-01-07 18:57:47 +04:00
return sysctl_head_grab ( head ) ;
2008-07-15 16:54:06 +04:00
}
2007-02-14 11:34:12 +03:00
2008-07-15 16:54:06 +04:00
static struct dentry * proc_sys_lookup ( struct inode * dir , struct dentry * dentry ,
2012-06-11 01:13:09 +04:00
unsigned int flags )
2008-07-15 16:54:06 +04:00
{
struct ctl_table_header * head = grab_header ( dir ) ;
struct ctl_table_header * h = NULL ;
2016-07-21 05:41:44 +03:00
const struct qstr * name = & dentry - > d_name ;
2008-07-15 16:54:06 +04:00
struct ctl_table * p ;
struct inode * inode ;
struct dentry * err = ERR_PTR ( - ENOENT ) ;
2011-12-29 20:24:29 +04:00
struct ctl_dir * ctl_dir ;
2012-01-08 11:24:30 +04:00
int ret ;
2007-02-14 11:34:12 +03:00
2008-07-15 16:54:06 +04:00
if ( IS_ERR ( head ) )
return ERR_CAST ( head ) ;
2007-02-14 11:34:12 +03:00
2011-12-29 20:24:29 +04:00
ctl_dir = container_of ( head , struct ctl_dir , header ) ;
2007-02-14 11:34:12 +03:00
2011-12-29 20:24:29 +04:00
p = lookup_entry ( & h , ctl_dir , name - > name , name - > len ) ;
2008-07-15 16:54:06 +04:00
if ( ! p )
2007-02-14 11:34:12 +03:00
goto out ;
2012-01-31 09:24:59 +04:00
if ( S_ISLNK ( p - > mode ) ) {
2016-07-16 23:22:55 +03:00
ret = sysctl_follow_link ( & h , & p ) ;
2012-01-31 09:24:59 +04:00
err = ERR_PTR ( ret ) ;
if ( ret )
goto out ;
}
2012-01-08 11:24:30 +04:00
2008-07-15 16:54:06 +04:00
inode = proc_sys_make_inode ( dir - > i_sb , h ? h : head , p ) ;
2018-12-14 02:20:52 +03:00
if ( IS_ERR ( inode ) ) {
err = ERR_CAST ( inode ) ;
2007-02-14 11:34:12 +03:00
goto out ;
2018-12-14 02:20:52 +03:00
}
2007-02-14 11:34:12 +03:00
2011-01-07 09:49:55 +03:00
d_set_d_op ( dentry , & proc_sys_dentry_operations ) ;
2018-05-03 16:45:06 +03:00
err = d_splice_alias ( inode , dentry ) ;
2007-02-14 11:34:12 +03:00
out :
2012-09-14 02:03:37 +04:00
if ( h )
sysctl_head_finish ( h ) ;
2007-02-14 11:34:12 +03:00
sysctl_head_finish ( head ) ;
return err ;
}
2020-09-03 17:22:32 +03:00
static ssize_t proc_sys_call_handler ( struct kiocb * iocb , struct iov_iter * iter ,
int write )
2007-02-14 11:34:12 +03:00
{
2020-09-03 17:22:32 +03:00
struct inode * inode = file_inode ( iocb - > ki_filp ) ;
2008-07-15 16:54:06 +04:00
struct ctl_table_header * head = grab_header ( inode ) ;
struct ctl_table * table = PROC_I ( inode ) - > sysctl_entry ;
2020-09-03 17:22:32 +03:00
size_t count = iov_iter_count ( iter ) ;
char * kbuf ;
2007-10-25 18:27:40 +04:00
ssize_t error ;
2007-02-14 11:34:12 +03:00
2008-07-15 16:54:06 +04:00
if ( IS_ERR ( head ) )
return PTR_ERR ( head ) ;
2007-02-14 11:34:12 +03:00
/*
* At this point we know that the sysctl was not unregistered
* and won ' t be until we finish .
*/
error = - EPERM ;
2012-11-16 07:02:58 +04:00
if ( sysctl_perm ( head , table , write ? MAY_WRITE : MAY_READ ) )
2007-02-14 11:34:12 +03:00
goto out ;
2008-07-15 16:54:06 +04:00
/* if that can happen at all, it should be -EINVAL, not -EISDIR */
error = - EINVAL ;
if ( ! table - > proc_handler )
goto out ;
2020-06-09 20:08:19 +03:00
/* don't even try if the size is too large */
2020-07-03 21:10:14 +03:00
error = - ENOMEM ;
if ( count > = KMALLOC_MAX_SIZE )
goto out ;
2021-02-26 04:20:49 +03:00
kbuf = kvzalloc ( count + 1 , GFP_KERNEL ) ;
2020-09-03 17:22:32 +03:00
if ( ! kbuf )
goto out ;
2020-06-09 20:08:19 +03:00
2020-04-24 09:43:38 +03:00
if ( write ) {
2020-09-03 17:22:32 +03:00
error = - EFAULT ;
if ( ! copy_from_iter_full ( kbuf , count , iter ) )
goto out_free_buf ;
kbuf [ count ] = ' \0 ' ;
2020-04-24 09:43:38 +03:00
}
error = BPF_CGROUP_RUN_PROG_SYSCTL ( head , table , write , & kbuf , & count ,
2020-09-03 17:22:32 +03:00
& iocb - > ki_pos ) ;
2019-02-27 23:59:24 +03:00
if ( error )
2020-04-24 09:43:38 +03:00
goto out_free_buf ;
2019-02-27 23:59:24 +03:00
2007-02-14 11:34:12 +03:00
/* careful: calling conventions are nasty here */
2020-09-03 17:22:32 +03:00
error = table - > proc_handler ( table , write , kbuf , & count , & iocb - > ki_pos ) ;
2020-04-24 09:43:38 +03:00
if ( error )
goto out_free_buf ;
if ( ! write ) {
error = - EFAULT ;
2020-09-03 17:22:32 +03:00
if ( copy_to_iter ( kbuf , count , iter ) < count )
2020-04-24 09:43:38 +03:00
goto out_free_buf ;
2019-03-08 05:38:43 +03:00
}
2020-04-24 09:43:38 +03:00
error = count ;
out_free_buf :
2021-02-26 04:20:49 +03:00
kvfree ( kbuf ) ;
2007-02-14 11:34:12 +03:00
out :
sysctl_head_finish ( head ) ;
return error ;
}
2020-09-03 17:22:32 +03:00
static ssize_t proc_sys_read ( struct kiocb * iocb , struct iov_iter * iter )
2007-02-14 11:34:12 +03:00
{
2020-09-03 17:22:32 +03:00
return proc_sys_call_handler ( iocb , iter , 0 ) ;
sysctl: merge equal proc_sys_read and proc_sys_write
Many (most of) sysctls do not have a per-container sense. E.g.
kernel.print_fatal_signals, vm.panic_on_oom, net.core.netdev_budget and so on
and so forth. Besides, tuning then from inside a container is not even
secure. On the other hand, hiding them completely from the container's tasks
sometimes causes user-space to stop working.
When developing net sysctl, the common practice was to duplicate a table and
drop the write bits in table->mode, but this approach was not very elegant,
lead to excessive memory consumption and was not suitable in general.
Here's the alternative solution. To facilitate the per-container sysctls
ctl_table_root-s were introduced. Each root contains a list of
ctl_table_header-s that are visible to different namespaces. The idea of this
set is to add the permissions() callback on the ctl_table_root to allow ctl
root limit permissions to the same ctl_table-s.
The main user of this functionality is the net-namespaces code, but later this
will (should) be used by more and more namespaces, containers and control
groups.
Actually, this idea's core is in a single hunk in the third patch. First two
patches are cleanups for sysctl code, while the third one mostly extends the
arguments set of some sysctl functions.
This patch:
These ->read and ->write callbacks act in a very similar way, so merge these
paths to reduce the number of places to patch later and shrink the .text size
(a bit).
Signed-off-by: Pavel Emelyanov <xemul@openvz.org>
Acked-by: "David S. Miller" <davem@davemloft.net>
Cc: "Eric W. Biederman" <ebiederm@xmission.com>
Cc: Alexey Dobriyan <adobriyan@sw.ru>
Cc: Denis V. Lunev <den@openvz.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2008-04-29 12:02:40 +04:00
}
2007-02-14 11:34:12 +03:00
2020-09-03 17:22:32 +03:00
static ssize_t proc_sys_write ( struct kiocb * iocb , struct iov_iter * iter )
sysctl: merge equal proc_sys_read and proc_sys_write
Many (most of) sysctls do not have a per-container sense. E.g.
kernel.print_fatal_signals, vm.panic_on_oom, net.core.netdev_budget and so on
and so forth. Besides, tuning then from inside a container is not even
secure. On the other hand, hiding them completely from the container's tasks
sometimes causes user-space to stop working.
When developing net sysctl, the common practice was to duplicate a table and
drop the write bits in table->mode, but this approach was not very elegant,
lead to excessive memory consumption and was not suitable in general.
Here's the alternative solution. To facilitate the per-container sysctls
ctl_table_root-s were introduced. Each root contains a list of
ctl_table_header-s that are visible to different namespaces. The idea of this
set is to add the permissions() callback on the ctl_table_root to allow ctl
root limit permissions to the same ctl_table-s.
The main user of this functionality is the net-namespaces code, but later this
will (should) be used by more and more namespaces, containers and control
groups.
Actually, this idea's core is in a single hunk in the third patch. First two
patches are cleanups for sysctl code, while the third one mostly extends the
arguments set of some sysctl functions.
This patch:
These ->read and ->write callbacks act in a very similar way, so merge these
paths to reduce the number of places to patch later and shrink the .text size
(a bit).
Signed-off-by: Pavel Emelyanov <xemul@openvz.org>
Acked-by: "David S. Miller" <davem@davemloft.net>
Cc: "Eric W. Biederman" <ebiederm@xmission.com>
Cc: Alexey Dobriyan <adobriyan@sw.ru>
Cc: Denis V. Lunev <den@openvz.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2008-04-29 12:02:40 +04:00
{
2020-09-03 17:22:32 +03:00
return proc_sys_call_handler ( iocb , iter , 1 ) ;
2007-02-14 11:34:12 +03:00
}
2011-11-03 00:39:22 +04:00
static int proc_sys_open ( struct inode * inode , struct file * filp )
{
2012-03-23 01:42:22 +04:00
struct ctl_table_header * head = grab_header ( inode ) ;
2011-11-03 00:39:22 +04:00
struct ctl_table * table = PROC_I ( inode ) - > sysctl_entry ;
2012-03-23 01:42:22 +04:00
/* sysctl was unregistered */
if ( IS_ERR ( head ) )
return PTR_ERR ( head ) ;
2011-11-03 00:39:22 +04:00
if ( table - > poll )
filp - > private_data = proc_sys_poll_event ( table - > poll ) ;
2012-03-23 01:42:22 +04:00
sysctl_head_finish ( head ) ;
2011-11-03 00:39:22 +04:00
return 0 ;
}
2017-07-03 08:02:18 +03:00
static __poll_t proc_sys_poll ( struct file * filp , poll_table * wait )
2011-11-03 00:39:22 +04:00
{
2013-01-24 02:07:38 +04:00
struct inode * inode = file_inode ( filp ) ;
2012-03-23 01:42:22 +04:00
struct ctl_table_header * head = grab_header ( inode ) ;
2011-11-03 00:39:22 +04:00
struct ctl_table * table = PROC_I ( inode ) - > sysctl_entry ;
2017-07-03 08:02:18 +03:00
__poll_t ret = DEFAULT_POLLMASK ;
2012-03-23 01:42:22 +04:00
unsigned long event ;
/* sysctl was unregistered */
if ( IS_ERR ( head ) )
2018-02-12 01:34:03 +03:00
return EPOLLERR | EPOLLHUP ;
2011-11-03 00:39:22 +04:00
if ( ! table - > proc_handler )
goto out ;
if ( ! table - > poll )
goto out ;
2012-03-23 01:42:22 +04:00
event = ( unsigned long ) filp - > private_data ;
2011-11-03 00:39:22 +04:00
poll_wait ( filp , & table - > poll - > wait , wait ) ;
if ( event ! = atomic_read ( & table - > poll - > event ) ) {
filp - > private_data = proc_sys_poll_event ( table - > poll ) ;
2018-02-12 01:34:03 +03:00
ret = EPOLLIN | EPOLLRDNORM | EPOLLERR | EPOLLPRI ;
2011-11-03 00:39:22 +04:00
}
out :
2012-03-23 01:42:22 +04:00
sysctl_head_finish ( head ) ;
2011-11-03 00:39:22 +04:00
return ret ;
}
2007-02-14 11:34:12 +03:00
2013-05-16 20:07:31 +04:00
static bool proc_sys_fill_cache ( struct file * file ,
struct dir_context * ctx ,
2008-07-15 16:54:06 +04:00
struct ctl_table_header * head ,
struct ctl_table * table )
2007-02-14 11:34:12 +03:00
{
2013-05-16 20:07:31 +04:00
struct dentry * child , * dir = file - > f_path . dentry ;
2007-02-14 11:34:12 +03:00
struct inode * inode ;
struct qstr qname ;
ino_t ino = 0 ;
unsigned type = DT_UNKNOWN ;
qname . name = table - > procname ;
qname . len = strlen ( table - > procname ) ;
2016-06-10 17:51:30 +03:00
qname . hash = full_name_hash ( dir , qname . name , qname . len ) ;
2007-02-14 11:34:12 +03:00
child = d_lookup ( dir , & qname ) ;
if ( ! child ) {
2016-04-20 23:36:09 +03:00
DECLARE_WAIT_QUEUE_HEAD_ONSTACK ( wq ) ;
child = d_alloc_parallel ( dir , & qname , & wq ) ;
if ( IS_ERR ( child ) )
return false ;
if ( d_in_lookup ( child ) ) {
2018-05-03 16:45:06 +03:00
struct dentry * res ;
2008-07-15 16:54:06 +04:00
inode = proc_sys_make_inode ( dir - > d_sb , head , table ) ;
2018-12-14 02:20:52 +03:00
if ( IS_ERR ( inode ) ) {
2016-04-20 23:36:09 +03:00
d_lookup_done ( child ) ;
2008-07-15 16:54:06 +04:00
dput ( child ) ;
2013-05-16 20:07:31 +04:00
return false ;
2007-02-14 11:34:12 +03:00
}
2016-04-20 23:36:09 +03:00
d_set_d_op ( child , & proc_sys_dentry_operations ) ;
2018-05-03 16:45:06 +03:00
res = d_splice_alias ( inode , child ) ;
d_lookup_done ( child ) ;
if ( unlikely ( res ) ) {
if ( IS_ERR ( res ) ) {
dput ( child ) ;
return false ;
}
dput ( child ) ;
child = res ;
}
2007-02-14 11:34:12 +03:00
}
}
2015-03-18 01:25:59 +03:00
inode = d_inode ( child ) ;
2008-07-15 16:54:06 +04:00
ino = inode - > i_ino ;
type = inode - > i_mode > > 12 ;
2007-02-14 11:34:12 +03:00
dput ( child ) ;
2013-05-16 20:07:31 +04:00
return dir_emit ( ctx , qname . name , qname . len , ino , type ) ;
2008-07-15 16:54:06 +04:00
}
2013-05-16 20:07:31 +04:00
static bool proc_sys_link_fill_cache ( struct file * file ,
struct dir_context * ctx ,
2012-01-08 11:24:30 +04:00
struct ctl_table_header * head ,
struct ctl_table * table )
{
2013-05-16 20:07:31 +04:00
bool ret = true ;
2018-04-11 02:31:38 +03:00
2012-01-08 11:24:30 +04:00
head = sysctl_head_grab ( head ) ;
2018-04-11 02:31:38 +03:00
if ( IS_ERR ( head ) )
return false ;
2012-01-08 11:24:30 +04:00
2018-04-11 02:31:41 +03:00
/* It is not an error if we can not follow the link ignore it */
if ( sysctl_follow_link ( & head , & table ) )
goto out ;
2012-01-08 11:24:30 +04:00
2013-05-16 20:07:31 +04:00
ret = proc_sys_fill_cache ( file , ctx , head , table ) ;
2012-01-08 11:24:30 +04:00
out :
sysctl_head_finish ( head ) ;
return ret ;
}
2014-08-09 01:22:16 +04:00
static int scan ( struct ctl_table_header * head , struct ctl_table * table ,
2008-07-15 16:54:06 +04:00
unsigned long * pos , struct file * file ,
2013-05-16 20:07:31 +04:00
struct dir_context * ctx )
2008-07-15 16:54:06 +04:00
{
2013-05-16 20:07:31 +04:00
bool res ;
2008-07-15 16:54:06 +04:00
2013-05-16 20:07:31 +04:00
if ( ( * pos ) + + < ctx - > pos )
return true ;
2008-07-15 16:54:06 +04:00
2012-01-08 11:24:30 +04:00
if ( unlikely ( S_ISLNK ( table - > mode ) ) )
2013-05-16 20:07:31 +04:00
res = proc_sys_link_fill_cache ( file , ctx , head , table ) ;
2012-01-08 11:24:30 +04:00
else
2013-05-16 20:07:31 +04:00
res = proc_sys_fill_cache ( file , ctx , head , table ) ;
2008-07-15 16:54:06 +04:00
2013-05-16 20:07:31 +04:00
if ( res )
ctx - > pos = * pos ;
2008-07-15 16:54:06 +04:00
2012-01-18 15:15:51 +04:00
return res ;
2007-02-14 11:34:12 +03:00
}
2013-05-16 20:07:31 +04:00
static int proc_sys_readdir ( struct file * file , struct dir_context * ctx )
2007-02-14 11:34:12 +03:00
{
2013-05-16 20:07:31 +04:00
struct ctl_table_header * head = grab_header ( file_inode ( file ) ) ;
2008-07-15 16:54:06 +04:00
struct ctl_table_header * h = NULL ;
2012-01-18 15:15:51 +04:00
struct ctl_table * entry ;
2011-12-29 20:24:29 +04:00
struct ctl_dir * ctl_dir ;
2007-02-14 11:34:12 +03:00
unsigned long pos ;
2008-07-15 16:54:06 +04:00
if ( IS_ERR ( head ) )
return PTR_ERR ( head ) ;
2007-02-14 11:34:12 +03:00
2011-12-29 20:24:29 +04:00
ctl_dir = container_of ( head , struct ctl_dir , header ) ;
2007-02-14 11:34:12 +03:00
2013-05-16 20:07:31 +04:00
if ( ! dir_emit_dots ( file , ctx ) )
2017-01-06 04:32:32 +03:00
goto out ;
2013-05-16 20:07:31 +04:00
2007-02-14 11:34:12 +03:00
pos = 2 ;
2011-12-29 20:24:29 +04:00
for ( first_entry ( ctl_dir , & h , & entry ) ; h ; next_entry ( & h , & entry ) ) {
2013-05-16 20:07:31 +04:00
if ( ! scan ( h , entry , & pos , file , ctx ) ) {
2008-07-15 16:54:06 +04:00
sysctl_head_finish ( h ) ;
break ;
2007-02-14 11:34:12 +03:00
}
}
2017-01-06 04:32:32 +03:00
out :
2007-02-14 11:34:12 +03:00
sysctl_head_finish ( head ) ;
2013-05-16 20:07:31 +04:00
return 0 ;
2007-02-14 11:34:12 +03:00
}
2023-01-13 14:49:22 +03:00
static int proc_sys_permission ( struct mnt_idmap * idmap ,
2021-01-21 16:19:43 +03:00
struct inode * inode , int mask )
2007-02-14 11:34:12 +03:00
{
/*
* sysctl entries that are not writeable ,
* are _NOT_ writeable , capabilities or not .
*/
2008-07-31 15:41:58 +04:00
struct ctl_table_header * head ;
struct ctl_table * table ;
2007-02-14 11:34:12 +03:00
int error ;
2008-07-31 15:41:58 +04:00
/* Executable files are not allowed under /proc/sys/ */
if ( ( mask & MAY_EXEC ) & & S_ISREG ( inode - > i_mode ) )
return - EACCES ;
head = grab_header ( inode ) ;
2008-07-15 16:54:06 +04:00
if ( IS_ERR ( head ) )
return PTR_ERR ( head ) ;
2007-02-14 11:34:12 +03:00
2008-07-31 15:41:58 +04:00
table = PROC_I ( inode ) - > sysctl_entry ;
2008-07-15 16:54:06 +04:00
if ( ! table ) /* global root - r-xr-xr-x */
error = mask & MAY_WRITE ? - EACCES : 0 ;
else /* Use the permissions on the sysctl table entry */
2012-11-16 07:02:58 +04:00
error = sysctl_perm ( head , table , mask & ~ MAY_NOT_BLOCK ) ;
2007-02-14 11:34:12 +03:00
sysctl_head_finish ( head ) ;
return error ;
}
2023-01-13 14:49:11 +03:00
static int proc_sys_setattr ( struct mnt_idmap * idmap ,
2021-01-21 16:19:43 +03:00
struct dentry * dentry , struct iattr * attr )
2007-02-14 11:34:12 +03:00
{
2015-03-18 01:25:59 +03:00
struct inode * inode = d_inode ( dentry ) ;
2007-02-14 11:34:12 +03:00
int error ;
if ( attr - > ia_valid & ( ATTR_MODE | ATTR_UID | ATTR_GID ) )
return - EPERM ;
2023-01-13 14:49:11 +03:00
error = setattr_prepare ( & nop_mnt_idmap , dentry , attr ) ;
2010-06-04 13:30:02 +04:00
if ( error )
return error ;
2023-01-13 14:49:11 +03:00
setattr_copy ( & nop_mnt_idmap , inode , attr ) ;
2010-06-04 13:30:02 +04:00
return 0 ;
2007-02-14 11:34:12 +03:00
}
2023-01-13 14:49:12 +03:00
static int proc_sys_getattr ( struct mnt_idmap * idmap ,
2021-01-21 16:19:43 +03:00
const struct path * path , struct kstat * stat ,
statx: Add a system call to make enhanced file info available
Add a system call to make extended file information available, including
file creation and some attribute flags where available through the
underlying filesystem.
The getattr inode operation is altered to take two additional arguments: a
u32 request_mask and an unsigned int flags that indicate the
synchronisation mode. This change is propagated to the vfs_getattr*()
function.
Functions like vfs_stat() are now inline wrappers around new functions
vfs_statx() and vfs_statx_fd() to reduce stack usage.
========
OVERVIEW
========
The idea was initially proposed as a set of xattrs that could be retrieved
with getxattr(), but the general preference proved to be for a new syscall
with an extended stat structure.
A number of requests were gathered for features to be included. The
following have been included:
(1) Make the fields a consistent size on all arches and make them large.
(2) Spare space, request flags and information flags are provided for
future expansion.
(3) Better support for the y2038 problem [Arnd Bergmann] (tv_sec is an
__s64).
(4) Creation time: The SMB protocol carries the creation time, which could
be exported by Samba, which will in turn help CIFS make use of
FS-Cache as that can be used for coherency data (stx_btime).
This is also specified in NFSv4 as a recommended attribute and could
be exported by NFSD [Steve French].
(5) Lightweight stat: Ask for just those details of interest, and allow a
netfs (such as NFS) to approximate anything not of interest, possibly
without going to the server [Trond Myklebust, Ulrich Drepper, Andreas
Dilger] (AT_STATX_DONT_SYNC).
(6) Heavyweight stat: Force a netfs to go to the server, even if it thinks
its cached attributes are up to date [Trond Myklebust]
(AT_STATX_FORCE_SYNC).
And the following have been left out for future extension:
(7) Data version number: Could be used by userspace NFS servers [Aneesh
Kumar].
Can also be used to modify fill_post_wcc() in NFSD which retrieves
i_version directly, but has just called vfs_getattr(). It could get
it from the kstat struct if it used vfs_xgetattr() instead.
(There's disagreement on the exact semantics of a single field, since
not all filesystems do this the same way).
(8) BSD stat compatibility: Including more fields from the BSD stat such
as creation time (st_btime) and inode generation number (st_gen)
[Jeremy Allison, Bernd Schubert].
(9) Inode generation number: Useful for FUSE and userspace NFS servers
[Bernd Schubert].
(This was asked for but later deemed unnecessary with the
open-by-handle capability available and caused disagreement as to
whether it's a security hole or not).
(10) Extra coherency data may be useful in making backups [Andreas Dilger].
(No particular data were offered, but things like last backup
timestamp, the data version number and the DOS archive bit would come
into this category).
(11) Allow the filesystem to indicate what it can/cannot provide: A
filesystem can now say it doesn't support a standard stat feature if
that isn't available, so if, for instance, inode numbers or UIDs don't
exist or are fabricated locally...
(This requires a separate system call - I have an fsinfo() call idea
for this).
(12) Store a 16-byte volume ID in the superblock that can be returned in
struct xstat [Steve French].
(Deferred to fsinfo).
(13) Include granularity fields in the time data to indicate the
granularity of each of the times (NFSv4 time_delta) [Steve French].
(Deferred to fsinfo).
(14) FS_IOC_GETFLAGS value. These could be translated to BSD's st_flags.
Note that the Linux IOC flags are a mess and filesystems such as Ext4
define flags that aren't in linux/fs.h, so translation in the kernel
may be a necessity (or, possibly, we provide the filesystem type too).
(Some attributes are made available in stx_attributes, but the general
feeling was that the IOC flags were to ext[234]-specific and shouldn't
be exposed through statx this way).
(15) Mask of features available on file (eg: ACLs, seclabel) [Brad Boyer,
Michael Kerrisk].
(Deferred, probably to fsinfo. Finding out if there's an ACL or
seclabal might require extra filesystem operations).
(16) Femtosecond-resolution timestamps [Dave Chinner].
(A __reserved field has been left in the statx_timestamp struct for
this - if there proves to be a need).
(17) A set multiple attributes syscall to go with this.
===============
NEW SYSTEM CALL
===============
The new system call is:
int ret = statx(int dfd,
const char *filename,
unsigned int flags,
unsigned int mask,
struct statx *buffer);
The dfd, filename and flags parameters indicate the file to query, in a
similar way to fstatat(). There is no equivalent of lstat() as that can be
emulated with statx() by passing AT_SYMLINK_NOFOLLOW in flags. There is
also no equivalent of fstat() as that can be emulated by passing a NULL
filename to statx() with the fd of interest in dfd.
Whether or not statx() synchronises the attributes with the backing store
can be controlled by OR'ing a value into the flags argument (this typically
only affects network filesystems):
(1) AT_STATX_SYNC_AS_STAT tells statx() to behave as stat() does in this
respect.
(2) AT_STATX_FORCE_SYNC will require a network filesystem to synchronise
its attributes with the server - which might require data writeback to
occur to get the timestamps correct.
(3) AT_STATX_DONT_SYNC will suppress synchronisation with the server in a
network filesystem. The resulting values should be considered
approximate.
mask is a bitmask indicating the fields in struct statx that are of
interest to the caller. The user should set this to STATX_BASIC_STATS to
get the basic set returned by stat(). It should be noted that asking for
more information may entail extra I/O operations.
buffer points to the destination for the data. This must be 256 bytes in
size.
======================
MAIN ATTRIBUTES RECORD
======================
The following structures are defined in which to return the main attribute
set:
struct statx_timestamp {
__s64 tv_sec;
__s32 tv_nsec;
__s32 __reserved;
};
struct statx {
__u32 stx_mask;
__u32 stx_blksize;
__u64 stx_attributes;
__u32 stx_nlink;
__u32 stx_uid;
__u32 stx_gid;
__u16 stx_mode;
__u16 __spare0[1];
__u64 stx_ino;
__u64 stx_size;
__u64 stx_blocks;
__u64 __spare1[1];
struct statx_timestamp stx_atime;
struct statx_timestamp stx_btime;
struct statx_timestamp stx_ctime;
struct statx_timestamp stx_mtime;
__u32 stx_rdev_major;
__u32 stx_rdev_minor;
__u32 stx_dev_major;
__u32 stx_dev_minor;
__u64 __spare2[14];
};
The defined bits in request_mask and stx_mask are:
STATX_TYPE Want/got stx_mode & S_IFMT
STATX_MODE Want/got stx_mode & ~S_IFMT
STATX_NLINK Want/got stx_nlink
STATX_UID Want/got stx_uid
STATX_GID Want/got stx_gid
STATX_ATIME Want/got stx_atime{,_ns}
STATX_MTIME Want/got stx_mtime{,_ns}
STATX_CTIME Want/got stx_ctime{,_ns}
STATX_INO Want/got stx_ino
STATX_SIZE Want/got stx_size
STATX_BLOCKS Want/got stx_blocks
STATX_BASIC_STATS [The stuff in the normal stat struct]
STATX_BTIME Want/got stx_btime{,_ns}
STATX_ALL [All currently available stuff]
stx_btime is the file creation time, stx_mask is a bitmask indicating the
data provided and __spares*[] are where as-yet undefined fields can be
placed.
Time fields are structures with separate seconds and nanoseconds fields
plus a reserved field in case we want to add even finer resolution. Note
that times will be negative if before 1970; in such a case, the nanosecond
fields will also be negative if not zero.
The bits defined in the stx_attributes field convey information about a
file, how it is accessed, where it is and what it does. The following
attributes map to FS_*_FL flags and are the same numerical value:
STATX_ATTR_COMPRESSED File is compressed by the fs
STATX_ATTR_IMMUTABLE File is marked immutable
STATX_ATTR_APPEND File is append-only
STATX_ATTR_NODUMP File is not to be dumped
STATX_ATTR_ENCRYPTED File requires key to decrypt in fs
Within the kernel, the supported flags are listed by:
KSTAT_ATTR_FS_IOC_FLAGS
[Are any other IOC flags of sufficient general interest to be exposed
through this interface?]
New flags include:
STATX_ATTR_AUTOMOUNT Object is an automount trigger
These are for the use of GUI tools that might want to mark files specially,
depending on what they are.
Fields in struct statx come in a number of classes:
(0) stx_dev_*, stx_blksize.
These are local system information and are always available.
(1) stx_mode, stx_nlinks, stx_uid, stx_gid, stx_[amc]time, stx_ino,
stx_size, stx_blocks.
These will be returned whether the caller asks for them or not. The
corresponding bits in stx_mask will be set to indicate whether they
actually have valid values.
If the caller didn't ask for them, then they may be approximated. For
example, NFS won't waste any time updating them from the server,
unless as a byproduct of updating something requested.
If the values don't actually exist for the underlying object (such as
UID or GID on a DOS file), then the bit won't be set in the stx_mask,
even if the caller asked for the value. In such a case, the returned
value will be a fabrication.
Note that there are instances where the type might not be valid, for
instance Windows reparse points.
(2) stx_rdev_*.
This will be set only if stx_mode indicates we're looking at a
blockdev or a chardev, otherwise will be 0.
(3) stx_btime.
Similar to (1), except this will be set to 0 if it doesn't exist.
=======
TESTING
=======
The following test program can be used to test the statx system call:
samples/statx/test-statx.c
Just compile and run, passing it paths to the files you want to examine.
The file is built automatically if CONFIG_SAMPLES is enabled.
Here's some example output. Firstly, an NFS directory that crosses to
another FSID. Note that the AUTOMOUNT attribute is set because transiting
this directory will cause d_automount to be invoked by the VFS.
[root@andromeda ~]# /tmp/test-statx -A /warthog/data
statx(/warthog/data) = 0
results=7ff
Size: 4096 Blocks: 8 IO Block: 1048576 directory
Device: 00:26 Inode: 1703937 Links: 125
Access: (3777/drwxrwxrwx) Uid: 0 Gid: 4041
Access: 2016-11-24 09:02:12.219699527+0000
Modify: 2016-11-17 10:44:36.225653653+0000
Change: 2016-11-17 10:44:36.225653653+0000
Attributes: 0000000000001000 (-------- -------- -------- -------- -------- -------- ---m---- --------)
Secondly, the result of automounting on that directory.
[root@andromeda ~]# /tmp/test-statx /warthog/data
statx(/warthog/data) = 0
results=7ff
Size: 4096 Blocks: 8 IO Block: 1048576 directory
Device: 00:27 Inode: 2 Links: 125
Access: (3777/drwxrwxrwx) Uid: 0 Gid: 4041
Access: 2016-11-24 09:02:12.219699527+0000
Modify: 2016-11-17 10:44:36.225653653+0000
Change: 2016-11-17 10:44:36.225653653+0000
Signed-off-by: David Howells <dhowells@redhat.com>
Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
2017-01-31 19:46:22 +03:00
u32 request_mask , unsigned int query_flags )
2008-07-15 16:54:06 +04:00
{
statx: Add a system call to make enhanced file info available
Add a system call to make extended file information available, including
file creation and some attribute flags where available through the
underlying filesystem.
The getattr inode operation is altered to take two additional arguments: a
u32 request_mask and an unsigned int flags that indicate the
synchronisation mode. This change is propagated to the vfs_getattr*()
function.
Functions like vfs_stat() are now inline wrappers around new functions
vfs_statx() and vfs_statx_fd() to reduce stack usage.
========
OVERVIEW
========
The idea was initially proposed as a set of xattrs that could be retrieved
with getxattr(), but the general preference proved to be for a new syscall
with an extended stat structure.
A number of requests were gathered for features to be included. The
following have been included:
(1) Make the fields a consistent size on all arches and make them large.
(2) Spare space, request flags and information flags are provided for
future expansion.
(3) Better support for the y2038 problem [Arnd Bergmann] (tv_sec is an
__s64).
(4) Creation time: The SMB protocol carries the creation time, which could
be exported by Samba, which will in turn help CIFS make use of
FS-Cache as that can be used for coherency data (stx_btime).
This is also specified in NFSv4 as a recommended attribute and could
be exported by NFSD [Steve French].
(5) Lightweight stat: Ask for just those details of interest, and allow a
netfs (such as NFS) to approximate anything not of interest, possibly
without going to the server [Trond Myklebust, Ulrich Drepper, Andreas
Dilger] (AT_STATX_DONT_SYNC).
(6) Heavyweight stat: Force a netfs to go to the server, even if it thinks
its cached attributes are up to date [Trond Myklebust]
(AT_STATX_FORCE_SYNC).
And the following have been left out for future extension:
(7) Data version number: Could be used by userspace NFS servers [Aneesh
Kumar].
Can also be used to modify fill_post_wcc() in NFSD which retrieves
i_version directly, but has just called vfs_getattr(). It could get
it from the kstat struct if it used vfs_xgetattr() instead.
(There's disagreement on the exact semantics of a single field, since
not all filesystems do this the same way).
(8) BSD stat compatibility: Including more fields from the BSD stat such
as creation time (st_btime) and inode generation number (st_gen)
[Jeremy Allison, Bernd Schubert].
(9) Inode generation number: Useful for FUSE and userspace NFS servers
[Bernd Schubert].
(This was asked for but later deemed unnecessary with the
open-by-handle capability available and caused disagreement as to
whether it's a security hole or not).
(10) Extra coherency data may be useful in making backups [Andreas Dilger].
(No particular data were offered, but things like last backup
timestamp, the data version number and the DOS archive bit would come
into this category).
(11) Allow the filesystem to indicate what it can/cannot provide: A
filesystem can now say it doesn't support a standard stat feature if
that isn't available, so if, for instance, inode numbers or UIDs don't
exist or are fabricated locally...
(This requires a separate system call - I have an fsinfo() call idea
for this).
(12) Store a 16-byte volume ID in the superblock that can be returned in
struct xstat [Steve French].
(Deferred to fsinfo).
(13) Include granularity fields in the time data to indicate the
granularity of each of the times (NFSv4 time_delta) [Steve French].
(Deferred to fsinfo).
(14) FS_IOC_GETFLAGS value. These could be translated to BSD's st_flags.
Note that the Linux IOC flags are a mess and filesystems such as Ext4
define flags that aren't in linux/fs.h, so translation in the kernel
may be a necessity (or, possibly, we provide the filesystem type too).
(Some attributes are made available in stx_attributes, but the general
feeling was that the IOC flags were to ext[234]-specific and shouldn't
be exposed through statx this way).
(15) Mask of features available on file (eg: ACLs, seclabel) [Brad Boyer,
Michael Kerrisk].
(Deferred, probably to fsinfo. Finding out if there's an ACL or
seclabal might require extra filesystem operations).
(16) Femtosecond-resolution timestamps [Dave Chinner].
(A __reserved field has been left in the statx_timestamp struct for
this - if there proves to be a need).
(17) A set multiple attributes syscall to go with this.
===============
NEW SYSTEM CALL
===============
The new system call is:
int ret = statx(int dfd,
const char *filename,
unsigned int flags,
unsigned int mask,
struct statx *buffer);
The dfd, filename and flags parameters indicate the file to query, in a
similar way to fstatat(). There is no equivalent of lstat() as that can be
emulated with statx() by passing AT_SYMLINK_NOFOLLOW in flags. There is
also no equivalent of fstat() as that can be emulated by passing a NULL
filename to statx() with the fd of interest in dfd.
Whether or not statx() synchronises the attributes with the backing store
can be controlled by OR'ing a value into the flags argument (this typically
only affects network filesystems):
(1) AT_STATX_SYNC_AS_STAT tells statx() to behave as stat() does in this
respect.
(2) AT_STATX_FORCE_SYNC will require a network filesystem to synchronise
its attributes with the server - which might require data writeback to
occur to get the timestamps correct.
(3) AT_STATX_DONT_SYNC will suppress synchronisation with the server in a
network filesystem. The resulting values should be considered
approximate.
mask is a bitmask indicating the fields in struct statx that are of
interest to the caller. The user should set this to STATX_BASIC_STATS to
get the basic set returned by stat(). It should be noted that asking for
more information may entail extra I/O operations.
buffer points to the destination for the data. This must be 256 bytes in
size.
======================
MAIN ATTRIBUTES RECORD
======================
The following structures are defined in which to return the main attribute
set:
struct statx_timestamp {
__s64 tv_sec;
__s32 tv_nsec;
__s32 __reserved;
};
struct statx {
__u32 stx_mask;
__u32 stx_blksize;
__u64 stx_attributes;
__u32 stx_nlink;
__u32 stx_uid;
__u32 stx_gid;
__u16 stx_mode;
__u16 __spare0[1];
__u64 stx_ino;
__u64 stx_size;
__u64 stx_blocks;
__u64 __spare1[1];
struct statx_timestamp stx_atime;
struct statx_timestamp stx_btime;
struct statx_timestamp stx_ctime;
struct statx_timestamp stx_mtime;
__u32 stx_rdev_major;
__u32 stx_rdev_minor;
__u32 stx_dev_major;
__u32 stx_dev_minor;
__u64 __spare2[14];
};
The defined bits in request_mask and stx_mask are:
STATX_TYPE Want/got stx_mode & S_IFMT
STATX_MODE Want/got stx_mode & ~S_IFMT
STATX_NLINK Want/got stx_nlink
STATX_UID Want/got stx_uid
STATX_GID Want/got stx_gid
STATX_ATIME Want/got stx_atime{,_ns}
STATX_MTIME Want/got stx_mtime{,_ns}
STATX_CTIME Want/got stx_ctime{,_ns}
STATX_INO Want/got stx_ino
STATX_SIZE Want/got stx_size
STATX_BLOCKS Want/got stx_blocks
STATX_BASIC_STATS [The stuff in the normal stat struct]
STATX_BTIME Want/got stx_btime{,_ns}
STATX_ALL [All currently available stuff]
stx_btime is the file creation time, stx_mask is a bitmask indicating the
data provided and __spares*[] are where as-yet undefined fields can be
placed.
Time fields are structures with separate seconds and nanoseconds fields
plus a reserved field in case we want to add even finer resolution. Note
that times will be negative if before 1970; in such a case, the nanosecond
fields will also be negative if not zero.
The bits defined in the stx_attributes field convey information about a
file, how it is accessed, where it is and what it does. The following
attributes map to FS_*_FL flags and are the same numerical value:
STATX_ATTR_COMPRESSED File is compressed by the fs
STATX_ATTR_IMMUTABLE File is marked immutable
STATX_ATTR_APPEND File is append-only
STATX_ATTR_NODUMP File is not to be dumped
STATX_ATTR_ENCRYPTED File requires key to decrypt in fs
Within the kernel, the supported flags are listed by:
KSTAT_ATTR_FS_IOC_FLAGS
[Are any other IOC flags of sufficient general interest to be exposed
through this interface?]
New flags include:
STATX_ATTR_AUTOMOUNT Object is an automount trigger
These are for the use of GUI tools that might want to mark files specially,
depending on what they are.
Fields in struct statx come in a number of classes:
(0) stx_dev_*, stx_blksize.
These are local system information and are always available.
(1) stx_mode, stx_nlinks, stx_uid, stx_gid, stx_[amc]time, stx_ino,
stx_size, stx_blocks.
These will be returned whether the caller asks for them or not. The
corresponding bits in stx_mask will be set to indicate whether they
actually have valid values.
If the caller didn't ask for them, then they may be approximated. For
example, NFS won't waste any time updating them from the server,
unless as a byproduct of updating something requested.
If the values don't actually exist for the underlying object (such as
UID or GID on a DOS file), then the bit won't be set in the stx_mask,
even if the caller asked for the value. In such a case, the returned
value will be a fabrication.
Note that there are instances where the type might not be valid, for
instance Windows reparse points.
(2) stx_rdev_*.
This will be set only if stx_mode indicates we're looking at a
blockdev or a chardev, otherwise will be 0.
(3) stx_btime.
Similar to (1), except this will be set to 0 if it doesn't exist.
=======
TESTING
=======
The following test program can be used to test the statx system call:
samples/statx/test-statx.c
Just compile and run, passing it paths to the files you want to examine.
The file is built automatically if CONFIG_SAMPLES is enabled.
Here's some example output. Firstly, an NFS directory that crosses to
another FSID. Note that the AUTOMOUNT attribute is set because transiting
this directory will cause d_automount to be invoked by the VFS.
[root@andromeda ~]# /tmp/test-statx -A /warthog/data
statx(/warthog/data) = 0
results=7ff
Size: 4096 Blocks: 8 IO Block: 1048576 directory
Device: 00:26 Inode: 1703937 Links: 125
Access: (3777/drwxrwxrwx) Uid: 0 Gid: 4041
Access: 2016-11-24 09:02:12.219699527+0000
Modify: 2016-11-17 10:44:36.225653653+0000
Change: 2016-11-17 10:44:36.225653653+0000
Attributes: 0000000000001000 (-------- -------- -------- -------- -------- -------- ---m---- --------)
Secondly, the result of automounting on that directory.
[root@andromeda ~]# /tmp/test-statx /warthog/data
statx(/warthog/data) = 0
results=7ff
Size: 4096 Blocks: 8 IO Block: 1048576 directory
Device: 00:27 Inode: 2 Links: 125
Access: (3777/drwxrwxrwx) Uid: 0 Gid: 4041
Access: 2016-11-24 09:02:12.219699527+0000
Modify: 2016-11-17 10:44:36.225653653+0000
Change: 2016-11-17 10:44:36.225653653+0000
Signed-off-by: David Howells <dhowells@redhat.com>
Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
2017-01-31 19:46:22 +03:00
struct inode * inode = d_inode ( path - > dentry ) ;
2008-07-15 16:54:06 +04:00
struct ctl_table_header * head = grab_header ( inode ) ;
struct ctl_table * table = PROC_I ( inode ) - > sysctl_entry ;
if ( IS_ERR ( head ) )
return PTR_ERR ( head ) ;
2023-01-13 14:49:12 +03:00
generic_fillattr ( & nop_mnt_idmap , inode , stat ) ;
2008-07-15 16:54:06 +04:00
if ( table )
stat - > mode = ( stat - > mode & S_IFMT ) | table - > mode ;
sysctl_head_finish ( head ) ;
return 0 ;
}
2007-02-14 11:34:12 +03:00
static const struct file_operations proc_sys_file_operations = {
2011-11-03 00:39:22 +04:00
. open = proc_sys_open ,
. poll = proc_sys_poll ,
2020-09-03 17:22:32 +03:00
. read_iter = proc_sys_read ,
. write_iter = proc_sys_write ,
. splice_read = generic_file_splice_read ,
. splice_write = iter_file_splice_write ,
llseek: automatically add .llseek fop
All file_operations should get a .llseek operation so we can make
nonseekable_open the default for future file operations without a
.llseek pointer.
The three cases that we can automatically detect are no_llseek, seq_lseek
and default_llseek. For cases where we can we can automatically prove that
the file offset is always ignored, we use noop_llseek, which maintains
the current behavior of not returning an error from a seek.
New drivers should normally not use noop_llseek but instead use no_llseek
and call nonseekable_open at open time. Existing drivers can be converted
to do the same when the maintainer knows for certain that no user code
relies on calling seek on the device file.
The generated code is often incorrectly indented and right now contains
comments that clarify for each added line why a specific variant was
chosen. In the version that gets submitted upstream, the comments will
be gone and I will manually fix the indentation, because there does not
seem to be a way to do that using coccinelle.
Some amount of new code is currently sitting in linux-next that should get
the same modifications, which I will do at the end of the merge window.
Many thanks to Julia Lawall for helping me learn to write a semantic
patch that does all this.
===== begin semantic patch =====
// This adds an llseek= method to all file operations,
// as a preparation for making no_llseek the default.
//
// The rules are
// - use no_llseek explicitly if we do nonseekable_open
// - use seq_lseek for sequential files
// - use default_llseek if we know we access f_pos
// - use noop_llseek if we know we don't access f_pos,
// but we still want to allow users to call lseek
//
@ open1 exists @
identifier nested_open;
@@
nested_open(...)
{
<+...
nonseekable_open(...)
...+>
}
@ open exists@
identifier open_f;
identifier i, f;
identifier open1.nested_open;
@@
int open_f(struct inode *i, struct file *f)
{
<+...
(
nonseekable_open(...)
|
nested_open(...)
)
...+>
}
@ read disable optional_qualifier exists @
identifier read_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
expression E;
identifier func;
@@
ssize_t read_f(struct file *f, char *p, size_t s, loff_t *off)
{
<+...
(
*off = E
|
*off += E
|
func(..., off, ...)
|
E = *off
)
...+>
}
@ read_no_fpos disable optional_qualifier exists @
identifier read_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
@@
ssize_t read_f(struct file *f, char *p, size_t s, loff_t *off)
{
... when != off
}
@ write @
identifier write_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
expression E;
identifier func;
@@
ssize_t write_f(struct file *f, const char *p, size_t s, loff_t *off)
{
<+...
(
*off = E
|
*off += E
|
func(..., off, ...)
|
E = *off
)
...+>
}
@ write_no_fpos @
identifier write_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
@@
ssize_t write_f(struct file *f, const char *p, size_t s, loff_t *off)
{
... when != off
}
@ fops0 @
identifier fops;
@@
struct file_operations fops = {
...
};
@ has_llseek depends on fops0 @
identifier fops0.fops;
identifier llseek_f;
@@
struct file_operations fops = {
...
.llseek = llseek_f,
...
};
@ has_read depends on fops0 @
identifier fops0.fops;
identifier read_f;
@@
struct file_operations fops = {
...
.read = read_f,
...
};
@ has_write depends on fops0 @
identifier fops0.fops;
identifier write_f;
@@
struct file_operations fops = {
...
.write = write_f,
...
};
@ has_open depends on fops0 @
identifier fops0.fops;
identifier open_f;
@@
struct file_operations fops = {
...
.open = open_f,
...
};
// use no_llseek if we call nonseekable_open
////////////////////////////////////////////
@ nonseekable1 depends on !has_llseek && has_open @
identifier fops0.fops;
identifier nso ~= "nonseekable_open";
@@
struct file_operations fops = {
... .open = nso, ...
+.llseek = no_llseek, /* nonseekable */
};
@ nonseekable2 depends on !has_llseek @
identifier fops0.fops;
identifier open.open_f;
@@
struct file_operations fops = {
... .open = open_f, ...
+.llseek = no_llseek, /* open uses nonseekable */
};
// use seq_lseek for sequential files
/////////////////////////////////////
@ seq depends on !has_llseek @
identifier fops0.fops;
identifier sr ~= "seq_read";
@@
struct file_operations fops = {
... .read = sr, ...
+.llseek = seq_lseek, /* we have seq_read */
};
// use default_llseek if there is a readdir
///////////////////////////////////////////
@ fops1 depends on !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier readdir_e;
@@
// any other fop is used that changes pos
struct file_operations fops = {
... .readdir = readdir_e, ...
+.llseek = default_llseek, /* readdir is present */
};
// use default_llseek if at least one of read/write touches f_pos
/////////////////////////////////////////////////////////////////
@ fops2 depends on !fops1 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read.read_f;
@@
// read fops use offset
struct file_operations fops = {
... .read = read_f, ...
+.llseek = default_llseek, /* read accesses f_pos */
};
@ fops3 depends on !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier write.write_f;
@@
// write fops use offset
struct file_operations fops = {
... .write = write_f, ...
+ .llseek = default_llseek, /* write accesses f_pos */
};
// Use noop_llseek if neither read nor write accesses f_pos
///////////////////////////////////////////////////////////
@ fops4 depends on !fops1 && !fops2 && !fops3 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read_no_fpos.read_f;
identifier write_no_fpos.write_f;
@@
// write fops use offset
struct file_operations fops = {
...
.write = write_f,
.read = read_f,
...
+.llseek = noop_llseek, /* read and write both use no f_pos */
};
@ depends on has_write && !has_read && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier write_no_fpos.write_f;
@@
struct file_operations fops = {
... .write = write_f, ...
+.llseek = noop_llseek, /* write uses no f_pos */
};
@ depends on has_read && !has_write && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read_no_fpos.read_f;
@@
struct file_operations fops = {
... .read = read_f, ...
+.llseek = noop_llseek, /* read uses no f_pos */
};
@ depends on !has_read && !has_write && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
@@
struct file_operations fops = {
...
+.llseek = noop_llseek, /* no read or write fn */
};
===== End semantic patch =====
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Cc: Julia Lawall <julia@diku.dk>
Cc: Christoph Hellwig <hch@infradead.org>
2010-08-15 20:52:59 +04:00
. llseek = default_llseek ,
2008-07-15 16:54:06 +04:00
} ;
static const struct file_operations proc_sys_dir_file_operations = {
2011-11-03 00:38:42 +04:00
. read = generic_read_dir ,
2016-04-21 00:13:54 +03:00
. iterate_shared = proc_sys_readdir ,
2008-09-03 23:53:01 +04:00
. llseek = generic_file_llseek ,
2007-02-14 11:34:12 +03:00
} ;
2008-02-08 15:21:19 +03:00
static const struct inode_operations proc_sys_inode_operations = {
2008-07-15 16:54:06 +04:00
. permission = proc_sys_permission ,
. setattr = proc_sys_setattr ,
. getattr = proc_sys_getattr ,
} ;
static const struct inode_operations proc_sys_dir_operations = {
2007-02-14 11:34:12 +03:00
. lookup = proc_sys_lookup ,
. permission = proc_sys_permission ,
. setattr = proc_sys_setattr ,
2008-07-15 16:54:06 +04:00
. getattr = proc_sys_getattr ,
2007-02-14 11:34:12 +03:00
} ;
2012-06-11 00:03:43 +04:00
static int proc_sys_revalidate ( struct dentry * dentry , unsigned int flags )
2007-02-14 11:34:12 +03:00
{
2012-06-11 00:03:43 +04:00
if ( flags & LOOKUP_RCU )
2011-01-07 09:49:57 +03:00
return - ECHILD ;
2015-03-18 01:25:59 +03:00
return ! PROC_I ( d_inode ( dentry ) ) - > sysctl - > unregistering ;
2008-07-15 16:54:06 +04:00
}
2011-01-07 09:49:23 +03:00
static int proc_sys_delete ( const struct dentry * dentry )
2008-07-15 16:54:06 +04:00
{
2015-03-18 01:25:59 +03:00
return ! ! PROC_I ( d_inode ( dentry ) ) - > sysctl - > unregistering ;
2008-07-15 16:54:06 +04:00
}
2012-01-06 16:07:15 +04:00
static int sysctl_is_seen ( struct ctl_table_header * p )
{
struct ctl_table_set * set = p - > set ;
int res ;
spin_lock ( & sysctl_lock ) ;
if ( p - > unregistering )
res = 0 ;
else if ( ! set - > is_seen )
res = 1 ;
else
res = set - > is_seen ( set ) ;
spin_unlock ( & sysctl_lock ) ;
return res ;
}
2016-07-31 23:37:25 +03:00
static int proc_sys_compare ( const struct dentry * dentry ,
2011-01-07 09:49:27 +03:00
unsigned int len , const char * str , const struct qstr * name )
2008-07-15 16:54:06 +04:00
{
2011-03-08 09:25:28 +03:00
struct ctl_table_header * head ;
2013-05-22 02:22:44 +04:00
struct inode * inode ;
fs: rcu-walk for path lookup
Perform common cases of path lookups without any stores or locking in the
ancestor dentry elements. This is called rcu-walk, as opposed to the current
algorithm which is a refcount based walk, or ref-walk.
This results in far fewer atomic operations on every path element,
significantly improving path lookup performance. It also avoids cacheline
bouncing on common dentries, significantly improving scalability.
The overall design is like this:
* LOOKUP_RCU is set in nd->flags, which distinguishes rcu-walk from ref-walk.
* Take the RCU lock for the entire path walk, starting with the acquiring
of the starting path (eg. root/cwd/fd-path). So now dentry refcounts are
not required for dentry persistence.
* synchronize_rcu is called when unregistering a filesystem, so we can
access d_ops and i_ops during rcu-walk.
* Similarly take the vfsmount lock for the entire path walk. So now mnt
refcounts are not required for persistence. Also we are free to perform mount
lookups, and to assume dentry mount points and mount roots are stable up and
down the path.
* Have a per-dentry seqlock to protect the dentry name, parent, and inode,
so we can load this tuple atomically, and also check whether any of its
members have changed.
* Dentry lookups (based on parent, candidate string tuple) recheck the parent
sequence after the child is found in case anything changed in the parent
during the path walk.
* inode is also RCU protected so we can load d_inode and use the inode for
limited things.
* i_mode, i_uid, i_gid can be tested for exec permissions during path walk.
* i_op can be loaded.
When we reach the destination dentry, we lock it, recheck lookup sequence,
and increment its refcount and mountpoint refcount. RCU and vfsmount locks
are dropped. This is termed "dropping rcu-walk". If the dentry refcount does
not match, we can not drop rcu-walk gracefully at the current point in the
lokup, so instead return -ECHILD (for want of a better errno). This signals the
path walking code to re-do the entire lookup with a ref-walk.
Aside from the final dentry, there are other situations that may be encounted
where we cannot continue rcu-walk. In that case, we drop rcu-walk (ie. take
a reference on the last good dentry) and continue with a ref-walk. Again, if
we can drop rcu-walk gracefully, we return -ECHILD and do the whole lookup
using ref-walk. But it is very important that we can continue with ref-walk
for most cases, particularly to avoid the overhead of double lookups, and to
gain the scalability advantages on common path elements (like cwd and root).
The cases where rcu-walk cannot continue are:
* NULL dentry (ie. any uncached path element)
* parent with d_inode->i_op->permission or ACLs
* dentries with d_revalidate
* Following links
In future patches, permission checks and d_revalidate become rcu-walk aware. It
may be possible eventually to make following links rcu-walk aware.
Uncached path elements will always require dropping to ref-walk mode, at the
very least because i_mutex needs to be grabbed, and objects allocated.
Signed-off-by: Nick Piggin <npiggin@kernel.dk>
2011-01-07 09:49:52 +03:00
/* Although proc doesn't have negative dentries, rcu-walk means
* that inode here can be NULL */
2011-03-08 09:25:28 +03:00
/* AV: can it, indeed? */
2015-03-18 01:25:59 +03:00
inode = d_inode_rcu ( dentry ) ;
fs: rcu-walk for path lookup
Perform common cases of path lookups without any stores or locking in the
ancestor dentry elements. This is called rcu-walk, as opposed to the current
algorithm which is a refcount based walk, or ref-walk.
This results in far fewer atomic operations on every path element,
significantly improving path lookup performance. It also avoids cacheline
bouncing on common dentries, significantly improving scalability.
The overall design is like this:
* LOOKUP_RCU is set in nd->flags, which distinguishes rcu-walk from ref-walk.
* Take the RCU lock for the entire path walk, starting with the acquiring
of the starting path (eg. root/cwd/fd-path). So now dentry refcounts are
not required for dentry persistence.
* synchronize_rcu is called when unregistering a filesystem, so we can
access d_ops and i_ops during rcu-walk.
* Similarly take the vfsmount lock for the entire path walk. So now mnt
refcounts are not required for persistence. Also we are free to perform mount
lookups, and to assume dentry mount points and mount roots are stable up and
down the path.
* Have a per-dentry seqlock to protect the dentry name, parent, and inode,
so we can load this tuple atomically, and also check whether any of its
members have changed.
* Dentry lookups (based on parent, candidate string tuple) recheck the parent
sequence after the child is found in case anything changed in the parent
during the path walk.
* inode is also RCU protected so we can load d_inode and use the inode for
limited things.
* i_mode, i_uid, i_gid can be tested for exec permissions during path walk.
* i_op can be loaded.
When we reach the destination dentry, we lock it, recheck lookup sequence,
and increment its refcount and mountpoint refcount. RCU and vfsmount locks
are dropped. This is termed "dropping rcu-walk". If the dentry refcount does
not match, we can not drop rcu-walk gracefully at the current point in the
lokup, so instead return -ECHILD (for want of a better errno). This signals the
path walking code to re-do the entire lookup with a ref-walk.
Aside from the final dentry, there are other situations that may be encounted
where we cannot continue rcu-walk. In that case, we drop rcu-walk (ie. take
a reference on the last good dentry) and continue with a ref-walk. Again, if
we can drop rcu-walk gracefully, we return -ECHILD and do the whole lookup
using ref-walk. But it is very important that we can continue with ref-walk
for most cases, particularly to avoid the overhead of double lookups, and to
gain the scalability advantages on common path elements (like cwd and root).
The cases where rcu-walk cannot continue are:
* NULL dentry (ie. any uncached path element)
* parent with d_inode->i_op->permission or ACLs
* dentries with d_revalidate
* Following links
In future patches, permission checks and d_revalidate become rcu-walk aware. It
may be possible eventually to make following links rcu-walk aware.
Uncached path elements will always require dropping to ref-walk mode, at the
very least because i_mutex needs to be grabbed, and objects allocated.
Signed-off-by: Nick Piggin <npiggin@kernel.dk>
2011-01-07 09:49:52 +03:00
if ( ! inode )
2011-03-08 09:25:28 +03:00
return 1 ;
2011-01-07 09:49:27 +03:00
if ( name - > len ! = len )
2008-07-15 16:54:06 +04:00
return 1 ;
2011-01-07 09:49:27 +03:00
if ( memcmp ( name - > name , str , len ) )
2008-07-15 16:54:06 +04:00
return 1 ;
2011-03-08 09:25:28 +03:00
head = rcu_dereference ( PROC_I ( inode ) - > sysctl ) ;
return ! head | | ! sysctl_is_seen ( head ) ;
2007-02-14 11:34:12 +03:00
}
2009-02-20 08:58:47 +03:00
static const struct dentry_operations proc_sys_dentry_operations = {
2007-02-14 11:34:12 +03:00
. d_revalidate = proc_sys_revalidate ,
2008-07-15 16:54:06 +04:00
. d_delete = proc_sys_delete ,
. d_compare = proc_sys_compare ,
2007-02-14 11:34:12 +03:00
} ;
2012-01-08 11:24:30 +04:00
static struct ctl_dir * find_subdir ( struct ctl_dir * dir ,
const char * name , int namelen )
2012-01-06 16:07:15 +04:00
{
2011-12-29 20:24:29 +04:00
struct ctl_table_header * head ;
struct ctl_table * entry ;
2012-01-06 16:07:15 +04:00
2012-01-08 11:24:30 +04:00
entry = find_entry ( & head , dir , name , namelen ) ;
2011-12-29 20:24:29 +04:00
if ( ! entry )
return ERR_PTR ( - ENOENT ) ;
2012-01-31 08:09:33 +04:00
if ( ! S_ISDIR ( entry - > mode ) )
return ERR_PTR ( - ENOTDIR ) ;
return container_of ( head , struct ctl_dir , header ) ;
2011-12-29 20:24:29 +04:00
}
static struct ctl_dir * new_dir ( struct ctl_table_set * set ,
2012-01-08 11:24:30 +04:00
const char * name , int namelen )
2011-12-29 20:24:29 +04:00
{
struct ctl_table * table ;
struct ctl_dir * new ;
2012-01-10 05:24:30 +04:00
struct ctl_node * node ;
2011-12-29 20:24:29 +04:00
char * new_name ;
2012-01-06 16:07:15 +04:00
2012-01-10 05:24:30 +04:00
new = kzalloc ( sizeof ( * new ) + sizeof ( struct ctl_node ) +
sizeof ( struct ctl_table ) * 2 + namelen + 1 ,
GFP_KERNEL ) ;
2011-12-29 20:24:29 +04:00
if ( ! new )
2012-01-06 16:07:15 +04:00
return NULL ;
2012-01-10 05:24:30 +04:00
node = ( struct ctl_node * ) ( new + 1 ) ;
table = ( struct ctl_table * ) ( node + 1 ) ;
2011-12-29 20:24:29 +04:00
new_name = ( char * ) ( table + 2 ) ;
memcpy ( new_name , name , namelen ) ;
table [ 0 ] . procname = new_name ;
table [ 0 ] . mode = S_IFDIR | S_IRUGO | S_IXUGO ;
2012-01-10 05:24:30 +04:00
init_header ( & new - > header , set - > dir . header . root , set , node , table ) ;
2011-12-29 20:24:29 +04:00
return new ;
2012-01-06 16:07:15 +04:00
}
2012-01-31 09:23:52 +04:00
/**
* get_subdir - find or create a subdir with the specified name .
* @ dir : Directory to create the subdirectory in
* @ name : The name of the subdirectory to find or create
* @ namelen : The length of name
*
* Takes a directory with an elevated reference count so we know that
* if we drop the lock the directory will not go away . Upon success
* the reference is moved from @ dir to the returned subdirectory .
* Upon error an error code is returned and the reference on @ dir is
* simply dropped .
*/
2012-01-08 11:24:30 +04:00
static struct ctl_dir * get_subdir ( struct ctl_dir * dir ,
const char * name , int namelen )
2012-01-06 16:07:15 +04:00
{
2012-01-08 11:24:30 +04:00
struct ctl_table_set * set = dir - > header . set ;
2011-12-29 20:24:29 +04:00
struct ctl_dir * subdir , * new = NULL ;
2012-01-31 08:37:51 +04:00
int err ;
2012-01-06 16:07:15 +04:00
2011-12-29 20:24:29 +04:00
spin_lock ( & sysctl_lock ) ;
2012-01-08 11:24:30 +04:00
subdir = find_subdir ( dir , name , namelen ) ;
2011-12-29 20:24:29 +04:00
if ( ! IS_ERR ( subdir ) )
goto found ;
if ( PTR_ERR ( subdir ) ! = - ENOENT )
goto failed ;
spin_unlock ( & sysctl_lock ) ;
new = new_dir ( set , name , namelen ) ;
spin_lock ( & sysctl_lock ) ;
subdir = ERR_PTR ( - ENOMEM ) ;
if ( ! new )
goto failed ;
2012-01-31 09:23:52 +04:00
/* Was the subdir added while we dropped the lock? */
2012-01-08 11:24:30 +04:00
subdir = find_subdir ( dir , name , namelen ) ;
2011-12-29 20:24:29 +04:00
if ( ! IS_ERR ( subdir ) )
goto found ;
if ( PTR_ERR ( subdir ) ! = - ENOENT )
goto failed ;
2012-01-31 09:23:52 +04:00
/* Nope. Use the our freshly made directory entry. */
2012-01-31 08:37:51 +04:00
err = insert_header ( dir , & new - > header ) ;
subdir = ERR_PTR ( err ) ;
if ( err )
2012-01-08 11:24:30 +04:00
goto failed ;
2011-12-29 20:24:29 +04:00
subdir = new ;
found :
subdir - > header . nreg + + ;
failed :
2015-08-12 13:29:44 +03:00
if ( IS_ERR ( subdir ) ) {
2013-02-28 05:03:16 +04:00
pr_err ( " sysctl could not get directory: " ) ;
2012-01-22 08:09:45 +04:00
sysctl_print_dir ( dir ) ;
2022-01-20 05:08:06 +03:00
pr_cont ( " %*.*s %ld \n " , namelen , namelen , name ,
PTR_ERR ( subdir ) ) ;
2012-01-06 16:07:15 +04:00
}
2011-12-29 20:24:29 +04:00
drop_sysctl_table ( & dir - > header ) ;
if ( new )
drop_sysctl_table ( & new - > header ) ;
spin_unlock ( & sysctl_lock ) ;
return subdir ;
2012-01-06 16:07:15 +04:00
}
2012-01-08 11:24:30 +04:00
static struct ctl_dir * xlate_dir ( struct ctl_table_set * set , struct ctl_dir * dir )
{
struct ctl_dir * parent ;
const char * procname ;
if ( ! dir - > header . parent )
return & set - > dir ;
parent = xlate_dir ( set , dir - > header . parent ) ;
if ( IS_ERR ( parent ) )
return parent ;
procname = dir - > header . ctl_table [ 0 ] . procname ;
return find_subdir ( parent , procname , strlen ( procname ) ) ;
}
static int sysctl_follow_link ( struct ctl_table_header * * phead ,
2016-07-16 23:22:55 +03:00
struct ctl_table * * pentry )
2012-01-08 11:24:30 +04:00
{
struct ctl_table_header * head ;
struct ctl_table_root * root ;
struct ctl_table_set * set ;
struct ctl_table * entry ;
struct ctl_dir * dir ;
int ret ;
spin_lock ( & sysctl_lock ) ;
root = ( * pentry ) - > data ;
2016-07-16 23:22:55 +03:00
set = lookup_header_set ( root ) ;
2012-01-08 11:24:30 +04:00
dir = xlate_dir ( set , ( * phead ) - > parent ) ;
if ( IS_ERR ( dir ) )
ret = PTR_ERR ( dir ) ;
else {
const char * procname = ( * pentry ) - > procname ;
head = NULL ;
entry = find_entry ( & head , dir , procname , strlen ( procname ) ) ;
ret = - ENOENT ;
if ( entry & & use_table ( head ) ) {
unuse_table ( * phead ) ;
* phead = head ;
* pentry = entry ;
ret = 0 ;
}
}
spin_unlock ( & sysctl_lock ) ;
return ret ;
}
2012-01-22 01:34:05 +04:00
static int sysctl_err ( const char * path , struct ctl_table * table , char * fmt , . . . )
2012-01-06 16:07:15 +04:00
{
2012-01-22 01:34:05 +04:00
struct va_format vaf ;
va_list args ;
2012-01-06 16:07:15 +04:00
2012-01-22 01:34:05 +04:00
va_start ( args , fmt ) ;
vaf . fmt = fmt ;
vaf . va = & args ;
2013-02-28 05:03:16 +04:00
pr_err ( " sysctl table check failed: %s/%s %pV \n " ,
path , table - > procname , & vaf ) ;
2012-01-06 16:07:15 +04:00
2012-01-22 01:34:05 +04:00
va_end ( args ) ;
return - EINVAL ;
2012-01-06 16:07:15 +04:00
}
2017-07-13 00:33:36 +03:00
static int sysctl_check_table_array ( const char * path , struct ctl_table * table )
{
int err = 0 ;
2017-07-13 00:33:40 +03:00
if ( ( table - > proc_handler = = proc_douintvec ) | |
( table - > proc_handler = = proc_douintvec_minmax ) ) {
2017-07-13 00:33:36 +03:00
if ( table - > maxlen ! = sizeof ( unsigned int ) )
fs/proc/proc_sysctl.c: fix typo in sysctl_check_table_array()
Patch series "ipc: Clamp *mni to the real IPCMNI limit", v3.
The sysctl parameters msgmni, shmmni and semmni have an inherent limit
of IPC_MNI (32k). However, users may not be aware of that because they
can write a value much higher than that without getting any error or
notification. Reading the parameters back will show the newly written
values which are not real.
Enforcing the limit by failing sysctl parameter write, however, can
break existing user applications. To address this delemma, a new flags
field is introduced into the ctl_table. The value CTL_FLAGS_CLAMP_RANGE
can be added to any ctl_table entries to enable a looser range clamping
without returning any error. For example,
.flags = CTL_FLAGS_CLAMP_RANGE,
This flags value are now used for the range checking of shmmni, msgmni
and semmni without breaking existing applications. If any out of range
value is written to those sysctl parameters, the following warning will
be printed instead.
Kernel parameter "shmmni" was set out of range [0, 32768], clamped to 32768.
Reading the values back will show 32768 instead of some fake values.
This patch (of 6):
Fix a typo.
Link: http://lkml.kernel.org/r/1519926220-7453-2-git-send-email-longman@redhat.com
Signed-off-by: Waiman Long <longman@redhat.com>
Reviewed-by: Andrew Morton <akpm@linux-foundation.org>
Acked-by: Luis R. Rodriguez <mcgrof@kernel.org>
Cc: Davidlohr Bueso <dave@stgolabs.net>
Cc: Manfred Spraul <manfred@colorfullife.com>
Cc: Kees Cook <keescook@chromium.org>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Cc: Matthew Wilcox <willy@infradead.org>
Cc: Randy Dunlap <rdunlap@infradead.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2018-04-11 02:35:35 +03:00
err | = sysctl_err ( path , table , " array not allowed " ) ;
2017-07-13 00:33:36 +03:00
}
2021-03-25 21:08:13 +03:00
if ( table - > proc_handler = = proc_dou8vec_minmax ) {
if ( table - > maxlen ! = sizeof ( u8 ) )
err | = sysctl_err ( path , table , " array not allowed " ) ;
}
2023-02-10 17:58:23 +03:00
if ( table - > proc_handler = = proc_dobool ) {
if ( table - > maxlen ! = sizeof ( bool ) )
err | = sysctl_err ( path , table , " array not allowed " ) ;
}
2017-07-13 00:33:36 +03:00
return err ;
}
2012-01-22 01:34:05 +04:00
static int sysctl_check_table ( const char * path , struct ctl_table * table )
2012-01-06 16:07:15 +04:00
{
2022-04-11 08:12:05 +03:00
struct ctl_table * entry ;
2012-01-22 01:34:05 +04:00
int err = 0 ;
2022-04-11 08:12:05 +03:00
list_for_each_table_entry ( entry , table ) {
if ( entry - > child )
err | = sysctl_err ( path , entry , " Not a file " ) ;
if ( ( entry - > proc_handler = = proc_dostring ) | |
2023-02-10 17:58:23 +03:00
( entry - > proc_handler = = proc_dobool ) | |
2022-04-11 08:12:05 +03:00
( entry - > proc_handler = = proc_dointvec ) | |
( entry - > proc_handler = = proc_douintvec ) | |
( entry - > proc_handler = = proc_douintvec_minmax ) | |
( entry - > proc_handler = = proc_dointvec_minmax ) | |
( entry - > proc_handler = = proc_dou8vec_minmax ) | |
( entry - > proc_handler = = proc_dointvec_jiffies ) | |
( entry - > proc_handler = = proc_dointvec_userhz_jiffies ) | |
( entry - > proc_handler = = proc_dointvec_ms_jiffies ) | |
( entry - > proc_handler = = proc_doulongvec_minmax ) | |
( entry - > proc_handler = = proc_doulongvec_ms_jiffies_minmax ) ) {
if ( ! entry - > data )
err | = sysctl_err ( path , entry , " No data " ) ;
if ( ! entry - > maxlen )
err | = sysctl_err ( path , entry , " No maxlen " ) ;
2017-07-13 00:33:36 +03:00
else
2022-04-11 08:12:05 +03:00
err | = sysctl_check_table_array ( path , entry ) ;
2012-01-22 01:34:05 +04:00
}
2022-04-11 08:12:05 +03:00
if ( ! entry - > proc_handler )
err | = sysctl_err ( path , entry , " No proc_handler " ) ;
2012-01-22 01:34:05 +04:00
2022-04-11 08:12:05 +03:00
if ( ( entry - > mode & ( S_IRUGO | S_IWUGO ) ) ! = entry - > mode )
err | = sysctl_err ( path , entry , " bogus .mode 0%o " ,
entry - > mode ) ;
2012-01-06 16:07:15 +04:00
}
2012-01-22 01:34:05 +04:00
return err ;
2012-01-06 16:07:15 +04:00
}
2012-01-08 11:24:30 +04:00
static struct ctl_table_header * new_links ( struct ctl_dir * dir , struct ctl_table * table ,
struct ctl_table_root * link_root )
{
struct ctl_table * link_table , * entry , * link ;
struct ctl_table_header * links ;
2012-01-10 05:24:30 +04:00
struct ctl_node * node ;
2012-01-08 11:24:30 +04:00
char * link_name ;
int nr_entries , name_bytes ;
name_bytes = 0 ;
nr_entries = 0 ;
2022-04-11 08:12:05 +03:00
list_for_each_table_entry ( entry , table ) {
2012-01-08 11:24:30 +04:00
nr_entries + + ;
name_bytes + = strlen ( entry - > procname ) + 1 ;
}
links = kzalloc ( sizeof ( struct ctl_table_header ) +
2012-01-10 05:24:30 +04:00
sizeof ( struct ctl_node ) * nr_entries +
2012-01-08 11:24:30 +04:00
sizeof ( struct ctl_table ) * ( nr_entries + 1 ) +
name_bytes ,
GFP_KERNEL ) ;
if ( ! links )
return NULL ;
2012-01-10 05:24:30 +04:00
node = ( struct ctl_node * ) ( links + 1 ) ;
link_table = ( struct ctl_table * ) ( node + nr_entries ) ;
2012-01-08 11:24:30 +04:00
link_name = ( char * ) & link_table [ nr_entries + 1 ] ;
2022-04-11 08:12:05 +03:00
link = link_table ;
2012-01-08 11:24:30 +04:00
2022-04-11 08:12:05 +03:00
list_for_each_table_entry ( entry , table ) {
2012-01-08 11:24:30 +04:00
int len = strlen ( entry - > procname ) + 1 ;
memcpy ( link_name , entry - > procname , len ) ;
link - > procname = link_name ;
link - > mode = S_IFLNK | S_IRWXUGO ;
link - > data = link_root ;
link_name + = len ;
2022-04-11 08:12:05 +03:00
link + + ;
2012-01-08 11:24:30 +04:00
}
2012-01-10 05:24:30 +04:00
init_header ( links , dir - > header . root , dir - > header . set , node , link_table ) ;
2012-01-08 11:24:30 +04:00
links - > nreg = nr_entries ;
return links ;
}
static bool get_links ( struct ctl_dir * dir ,
struct ctl_table * table , struct ctl_table_root * link_root )
{
struct ctl_table_header * head ;
struct ctl_table * entry , * link ;
/* Are there links available for every entry in table? */
2022-04-11 08:12:05 +03:00
list_for_each_table_entry ( entry , table ) {
2012-01-08 11:24:30 +04:00
const char * procname = entry - > procname ;
link = find_entry ( & head , dir , procname , strlen ( procname ) ) ;
if ( ! link )
return false ;
if ( S_ISDIR ( link - > mode ) & & S_ISDIR ( entry - > mode ) )
continue ;
if ( S_ISLNK ( link - > mode ) & & ( link - > data = = link_root ) )
continue ;
return false ;
}
/* The checks passed. Increase the registration count on the links */
2022-04-11 08:12:05 +03:00
list_for_each_table_entry ( entry , table ) {
2012-01-08 11:24:30 +04:00
const char * procname = entry - > procname ;
link = find_entry ( & head , dir , procname , strlen ( procname ) ) ;
head - > nreg + + ;
}
return true ;
}
static int insert_links ( struct ctl_table_header * head )
{
struct ctl_table_set * root_set = & sysctl_table_root . default_set ;
2022-08-01 11:51:17 +03:00
struct ctl_dir * core_parent ;
2012-01-08 11:24:30 +04:00
struct ctl_table_header * links ;
int err ;
if ( head - > set = = root_set )
return 0 ;
core_parent = xlate_dir ( root_set , head - > parent ) ;
if ( IS_ERR ( core_parent ) )
return 0 ;
if ( get_links ( core_parent , head - > ctl_table , head - > root ) )
return 0 ;
core_parent - > header . nreg + + ;
spin_unlock ( & sysctl_lock ) ;
links = new_links ( core_parent , head - > ctl_table , head - > root ) ;
spin_lock ( & sysctl_lock ) ;
err = - ENOMEM ;
if ( ! links )
goto out ;
err = 0 ;
if ( get_links ( core_parent , head - > ctl_table , head - > root ) ) {
kfree ( links ) ;
goto out ;
}
err = insert_header ( core_parent , links ) ;
if ( err )
kfree ( links ) ;
out :
drop_sysctl_table ( & core_parent - > header ) ;
return err ;
}
2023-03-02 23:28:17 +03:00
/* Find the directory for the ctl_table. If one is not found create it. */
static struct ctl_dir * sysctl_mkdir_p ( struct ctl_dir * dir , const char * path )
{
const char * name , * nextname ;
for ( name = path ; name ; name = nextname ) {
int namelen ;
nextname = strchr ( name , ' / ' ) ;
if ( nextname ) {
namelen = nextname - name ;
nextname + + ;
} else {
namelen = strlen ( name ) ;
}
if ( namelen = = 0 )
continue ;
/*
* namelen ensures if name is " foo/bar/yay " only foo is
* registered first . We traverse as if using mkdir - p and
* return a ctl_dir for the last directory entry .
*/
dir = get_subdir ( dir , name , namelen ) ;
if ( IS_ERR ( dir ) )
break ;
}
return dir ;
}
2012-01-06 16:07:15 +04:00
/**
2012-01-23 06:22:05 +04:00
* __register_sysctl_table - register a leaf sysctl table
2012-01-08 12:02:37 +04:00
* @ set : Sysctl tree to register on
2012-01-06 16:07:15 +04:00
* @ path : The path to the directory the sysctl table is in .
2023-03-11 00:00:16 +03:00
* @ table : the top - level table structure without any child . This table
* should not be free ' d after registration . So it should not be
* used on stack . It can either be a global or dynamically allocated
* by the caller and free ' d later after sysctl unregistration .
2012-01-06 16:07:15 +04:00
*
* Register a sysctl table hierarchy . @ table should be a filled in ctl_table
* array . A completely 0 filled entry terminates the table .
*
* The members of the & struct ctl_table structure are used as follows :
*
* procname - the name of the sysctl file under / proc / sys . Set to % NULL to not
* enter a sysctl file
*
* data - a pointer to data for use by proc_handler
*
* maxlen - the maximum size in bytes of the data
*
2012-01-23 06:22:05 +04:00
* mode - the file permissions for the / proc / sys file
2012-01-06 16:07:15 +04:00
*
2012-01-23 06:22:05 +04:00
* child - must be % NULL .
2012-01-06 16:07:15 +04:00
*
* proc_handler - the text handler routine ( described below )
*
* extra1 , extra2 - extra pointers usable by the proc handler routines
2023-03-02 23:28:16 +03:00
* XXX : we should eventually modify these to use long min / max [ 0 ]
* [ 0 ] https : //lkml.kernel.org/87zgpte9o4.fsf@email.froward.int.ebiederm.org
2012-01-06 16:07:15 +04:00
*
* Leaf nodes in the sysctl tree will be represented by a single file
2023-03-02 23:28:16 +03:00
* under / proc ; non - leaf nodes ( where child is not NULL ) are not allowed ,
* sysctl_check_table ( ) verifies this .
2012-01-06 16:07:15 +04:00
*
2012-01-23 06:22:05 +04:00
* There must be a proc_handler routine for any terminal nodes .
* Several default handlers are available to cover common cases -
2012-01-06 16:07:15 +04:00
*
* proc_dostring ( ) , proc_dointvec ( ) , proc_dointvec_jiffies ( ) ,
* proc_dointvec_userhz_jiffies ( ) , proc_dointvec_minmax ( ) ,
* proc_doulongvec_ms_jiffies_minmax ( ) , proc_doulongvec_minmax ( )
*
* It is the handler ' s job to read the input buffer from user memory
* and process it . The handler should return 0 on success .
*
* This routine returns % NULL on a failure to register , and a pointer
* to the table header on success .
*/
2012-01-21 22:26:26 +04:00
struct ctl_table_header * __register_sysctl_table (
2012-01-08 12:02:37 +04:00
struct ctl_table_set * set ,
2012-01-21 22:26:26 +04:00
const char * path , struct ctl_table * table )
2012-01-06 16:07:15 +04:00
{
2012-01-08 12:02:37 +04:00
struct ctl_table_root * root = set - > dir . header . root ;
2012-01-06 16:07:15 +04:00
struct ctl_table_header * header ;
2011-12-29 20:24:29 +04:00
struct ctl_dir * dir ;
2012-01-10 05:24:30 +04:00
struct ctl_table * entry ;
struct ctl_node * node ;
int nr_entries = 0 ;
2022-04-11 08:12:05 +03:00
list_for_each_table_entry ( entry , table )
2012-01-10 05:24:30 +04:00
nr_entries + + ;
2012-01-06 16:07:15 +04:00
2012-01-10 05:24:30 +04:00
header = kzalloc ( sizeof ( struct ctl_table_header ) +
2022-05-02 15:15:51 +03:00
sizeof ( struct ctl_node ) * nr_entries , GFP_KERNEL_ACCOUNT ) ;
2012-01-06 16:07:15 +04:00
if ( ! header )
return NULL ;
2012-01-10 05:24:30 +04:00
node = ( struct ctl_node * ) ( header + 1 ) ;
init_header ( header , root , set , node , table ) ;
2011-12-29 20:24:29 +04:00
if ( sysctl_check_table ( path , table ) )
goto fail ;
spin_lock ( & sysctl_lock ) ;
2012-01-08 11:24:30 +04:00
dir = & set - > dir ;
2023-03-02 23:28:16 +03:00
/* Reference moved down the directory tree get_subdir */
2011-12-29 20:24:29 +04:00
dir - > header . nreg + + ;
spin_unlock ( & sysctl_lock ) ;
2012-01-06 16:07:15 +04:00
2023-03-02 23:28:17 +03:00
dir = sysctl_mkdir_p ( dir , path ) ;
if ( IS_ERR ( dir ) )
goto fail ;
2012-01-06 16:07:15 +04:00
spin_lock ( & sysctl_lock ) ;
2012-01-08 11:24:30 +04:00
if ( insert_header ( dir , header ) )
2011-12-29 20:24:29 +04:00
goto fail_put_dir_locked ;
2012-01-08 11:24:30 +04:00
2011-12-29 20:24:29 +04:00
drop_sysctl_table ( & dir - > header ) ;
2012-01-06 16:07:15 +04:00
spin_unlock ( & sysctl_lock ) ;
return header ;
2012-01-08 11:24:30 +04:00
2011-12-29 20:24:29 +04:00
fail_put_dir_locked :
drop_sysctl_table ( & dir - > header ) ;
2012-01-22 01:34:05 +04:00
spin_unlock ( & sysctl_lock ) ;
fail :
kfree ( header ) ;
dump_stack ( ) ;
return NULL ;
2012-01-06 16:07:15 +04:00
}
2012-01-21 09:47:03 +04:00
/**
* register_sysctl - register a sysctl table
2023-03-11 00:00:16 +03:00
* @ path : The path to the directory the sysctl table is in . If the path
* doesn ' t exist we will create it for you .
* @ table : the table structure . The calller must ensure the life of the @ table
* will be kept during the lifetime use of the syctl . It must not be freed
* until unregister_sysctl_table ( ) is called with the given returned table
* with this registration . If your code is non modular then you don ' t need
* to call unregister_sysctl_table ( ) and can instead use something like
* register_sysctl_init ( ) which does not care for the result of the syctl
* registration .
2012-01-21 09:47:03 +04:00
*
* Register a sysctl table . @ table should be a filled in ctl_table
* array . A completely 0 filled entry terminates the table .
*
* See __register_sysctl_table for more details .
*/
struct ctl_table_header * register_sysctl ( const char * path , struct ctl_table * table )
{
return __register_sysctl_table ( & sysctl_table_root . default_set ,
path , table ) ;
}
EXPORT_SYMBOL ( register_sysctl ) ;
2022-01-22 09:10:50 +03:00
/**
* __register_sysctl_init ( ) - register sysctl table to path
2023-03-11 00:00:16 +03:00
* @ path : path name for sysctl base . If that path doesn ' t exist we will create
* it for you .
* @ table : This is the sysctl table that needs to be registered to the path .
* The caller must ensure the life of the @ table will be kept during the
* lifetime use of the sysctl .
2022-01-22 09:10:50 +03:00
* @ table_name : The name of sysctl table , only used for log printing when
* registration fails
*
* The sysctl interface is used by userspace to query or modify at runtime
* a predefined value set on a variable . These variables however have default
* values pre - set . Code which depends on these variables will always work even
* if register_sysctl ( ) fails . If register_sysctl ( ) fails you ' d just loose the
* ability to query or modify the sysctls dynamically at run time . Chances of
* register_sysctl ( ) failing on init are extremely low , and so for both reasons
* this function does not return any error as it is used by initialization code .
*
2023-03-02 23:28:18 +03:00
* Context : if your base directory does not exist it will be created for you .
2022-01-22 09:10:50 +03:00
*/
void __init __register_sysctl_init ( const char * path , struct ctl_table * table ,
const char * table_name )
{
struct ctl_table_header * hdr = register_sysctl ( path , table ) ;
if ( unlikely ( ! hdr ) ) {
pr_err ( " failed when register_sysctl %s to %s \n " , table_name , path ) ;
return ;
}
kmemleak_not_leak ( hdr ) ;
}
2012-01-21 22:26:26 +04:00
static char * append_path ( const char * path , char * pos , const char * name )
{
int namelen ;
namelen = strlen ( name ) ;
if ( ( ( pos - path ) + namelen + 2 ) > = PATH_MAX )
return NULL ;
memcpy ( pos , name , namelen ) ;
pos [ namelen ] = ' / ' ;
pos [ namelen + 1 ] = ' \0 ' ;
pos + = namelen + 1 ;
return pos ;
}
2012-01-23 06:22:05 +04:00
static int count_subheaders ( struct ctl_table * table )
{
int has_files = 0 ;
int nr_subheaders = 0 ;
struct ctl_table * entry ;
/* special case: no directory and empty directory */
if ( ! table | | ! table - > procname )
return 1 ;
2022-04-11 08:12:05 +03:00
list_for_each_table_entry ( entry , table ) {
2012-01-23 06:22:05 +04:00
if ( entry - > child )
nr_subheaders + = count_subheaders ( entry - > child ) ;
else
has_files = 1 ;
}
return nr_subheaders + has_files ;
}
static int register_leaf_sysctl_tables ( const char * path , char * pos ,
2012-01-08 12:02:37 +04:00
struct ctl_table_header * * * subheader , struct ctl_table_set * set ,
2012-01-23 06:22:05 +04:00
struct ctl_table * table )
{
struct ctl_table * ctl_table_arg = NULL ;
struct ctl_table * entry , * files ;
int nr_files = 0 ;
int nr_dirs = 0 ;
int err = - ENOMEM ;
2022-04-11 08:12:05 +03:00
list_for_each_table_entry ( entry , table ) {
2012-01-23 06:22:05 +04:00
if ( entry - > child )
nr_dirs + + ;
else
nr_files + + ;
}
files = table ;
/* If there are mixed files and directories we need a new table */
if ( nr_dirs & & nr_files ) {
struct ctl_table * new ;
treewide: kzalloc() -> kcalloc()
The kzalloc() function has a 2-factor argument form, kcalloc(). This
patch replaces cases of:
kzalloc(a * b, gfp)
with:
kcalloc(a * b, gfp)
as well as handling cases of:
kzalloc(a * b * c, gfp)
with:
kzalloc(array3_size(a, b, c), gfp)
as it's slightly less ugly than:
kzalloc_array(array_size(a, b), c, gfp)
This does, however, attempt to ignore constant size factors like:
kzalloc(4 * 1024, gfp)
though any constants defined via macros get caught up in the conversion.
Any factors with a sizeof() of "unsigned char", "char", and "u8" were
dropped, since they're redundant.
The Coccinelle script used for this was:
// Fix redundant parens around sizeof().
@@
type TYPE;
expression THING, E;
@@
(
kzalloc(
- (sizeof(TYPE)) * E
+ sizeof(TYPE) * E
, ...)
|
kzalloc(
- (sizeof(THING)) * E
+ sizeof(THING) * E
, ...)
)
// Drop single-byte sizes and redundant parens.
@@
expression COUNT;
typedef u8;
typedef __u8;
@@
(
kzalloc(
- sizeof(u8) * (COUNT)
+ COUNT
, ...)
|
kzalloc(
- sizeof(__u8) * (COUNT)
+ COUNT
, ...)
|
kzalloc(
- sizeof(char) * (COUNT)
+ COUNT
, ...)
|
kzalloc(
- sizeof(unsigned char) * (COUNT)
+ COUNT
, ...)
|
kzalloc(
- sizeof(u8) * COUNT
+ COUNT
, ...)
|
kzalloc(
- sizeof(__u8) * COUNT
+ COUNT
, ...)
|
kzalloc(
- sizeof(char) * COUNT
+ COUNT
, ...)
|
kzalloc(
- sizeof(unsigned char) * COUNT
+ COUNT
, ...)
)
// 2-factor product with sizeof(type/expression) and identifier or constant.
@@
type TYPE;
expression THING;
identifier COUNT_ID;
constant COUNT_CONST;
@@
(
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * (COUNT_ID)
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * COUNT_ID
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * (COUNT_CONST)
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * COUNT_CONST
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * (COUNT_ID)
+ COUNT_ID, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * COUNT_ID
+ COUNT_ID, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * (COUNT_CONST)
+ COUNT_CONST, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * COUNT_CONST
+ COUNT_CONST, sizeof(THING)
, ...)
)
// 2-factor product, only identifiers.
@@
identifier SIZE, COUNT;
@@
- kzalloc
+ kcalloc
(
- SIZE * COUNT
+ COUNT, SIZE
, ...)
// 3-factor product with 1 sizeof(type) or sizeof(expression), with
// redundant parens removed.
@@
expression THING;
identifier STRIDE, COUNT;
type TYPE;
@@
(
kzalloc(
- sizeof(TYPE) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kzalloc(
- sizeof(TYPE) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kzalloc(
- sizeof(TYPE) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kzalloc(
- sizeof(TYPE) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kzalloc(
- sizeof(THING) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kzalloc(
- sizeof(THING) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kzalloc(
- sizeof(THING) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kzalloc(
- sizeof(THING) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
)
// 3-factor product with 2 sizeof(variable), with redundant parens removed.
@@
expression THING1, THING2;
identifier COUNT;
type TYPE1, TYPE2;
@@
(
kzalloc(
- sizeof(TYPE1) * sizeof(TYPE2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
kzalloc(
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
kzalloc(
- sizeof(THING1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
kzalloc(
- sizeof(THING1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
kzalloc(
- sizeof(TYPE1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
|
kzalloc(
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
)
// 3-factor product, only identifiers, with redundant parens removed.
@@
identifier STRIDE, SIZE, COUNT;
@@
(
kzalloc(
- (COUNT) * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- COUNT * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- COUNT * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- (COUNT) * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- COUNT * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- (COUNT) * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- (COUNT) * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- COUNT * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
)
// Any remaining multi-factor products, first at least 3-factor products,
// when they're not all constants...
@@
expression E1, E2, E3;
constant C1, C2, C3;
@@
(
kzalloc(C1 * C2 * C3, ...)
|
kzalloc(
- (E1) * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
|
kzalloc(
- (E1) * (E2) * E3
+ array3_size(E1, E2, E3)
, ...)
|
kzalloc(
- (E1) * (E2) * (E3)
+ array3_size(E1, E2, E3)
, ...)
|
kzalloc(
- E1 * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
)
// And then all remaining 2 factors products when they're not all constants,
// keeping sizeof() as the second factor argument.
@@
expression THING, E1, E2;
type TYPE;
constant C1, C2, C3;
@@
(
kzalloc(sizeof(THING) * C2, ...)
|
kzalloc(sizeof(TYPE) * C2, ...)
|
kzalloc(C1 * C2 * C3, ...)
|
kzalloc(C1 * C2, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * (E2)
+ E2, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * E2
+ E2, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * (E2)
+ E2, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * E2
+ E2, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- (E1) * E2
+ E1, E2
, ...)
|
- kzalloc
+ kcalloc
(
- (E1) * (E2)
+ E1, E2
, ...)
|
- kzalloc
+ kcalloc
(
- E1 * E2
+ E1, E2
, ...)
)
Signed-off-by: Kees Cook <keescook@chromium.org>
2018-06-13 00:03:40 +03:00
files = kcalloc ( nr_files + 1 , sizeof ( struct ctl_table ) ,
2012-01-23 06:22:05 +04:00
GFP_KERNEL ) ;
if ( ! files )
goto out ;
ctl_table_arg = files ;
2022-04-11 08:12:05 +03:00
new = files ;
list_for_each_table_entry ( entry , table ) {
2012-01-23 06:22:05 +04:00
if ( entry - > child )
continue ;
* new = * entry ;
new + + ;
}
}
/* Register everything except a directory full of subdirectories */
if ( nr_files | | ! nr_dirs ) {
struct ctl_table_header * header ;
2012-01-08 12:02:37 +04:00
header = __register_sysctl_table ( set , path , files ) ;
2012-01-23 06:22:05 +04:00
if ( ! header ) {
kfree ( ctl_table_arg ) ;
goto out ;
}
/* Remember if we need to free the file table */
header - > ctl_table_arg = ctl_table_arg ;
* * subheader = header ;
( * subheader ) + + ;
}
/* Recurse into the subdirectories. */
2022-04-11 08:12:05 +03:00
list_for_each_table_entry ( entry , table ) {
2012-01-23 06:22:05 +04:00
char * child_pos ;
if ( ! entry - > child )
continue ;
err = - ENAMETOOLONG ;
child_pos = append_path ( path , pos , entry - > procname ) ;
if ( ! child_pos )
goto out ;
err = register_leaf_sysctl_tables ( path , child_pos , subheader ,
2012-01-08 12:02:37 +04:00
set , entry - > child ) ;
2012-01-23 06:22:05 +04:00
pos [ 0 ] = ' \0 ' ;
if ( err )
goto out ;
}
err = 0 ;
out :
/* On failure our caller will unregister all registered subheaders */
return err ;
}
2012-01-21 22:26:26 +04:00
/**
* __register_sysctl_paths - register a sysctl table hierarchy
2012-01-08 12:02:37 +04:00
* @ set : Sysctl tree to register on
2012-01-21 22:26:26 +04:00
* @ path : The path to the directory the sysctl table is in .
* @ table : the top - level table structure
*
* Register a sysctl table hierarchy . @ table should be a filled in ctl_table
* array . A completely 0 filled entry terminates the table .
2023-03-11 00:00:16 +03:00
* We are slowly deprecating this call so avoid its use .
2012-01-21 22:26:26 +04:00
*
* See __register_sysctl_table for more details .
*/
struct ctl_table_header * __register_sysctl_paths (
2012-01-08 12:02:37 +04:00
struct ctl_table_set * set ,
2012-01-21 22:26:26 +04:00
const struct ctl_path * path , struct ctl_table * table )
{
2012-01-22 00:35:23 +04:00
struct ctl_table * ctl_table_arg = table ;
2012-01-23 06:22:05 +04:00
int nr_subheaders = count_subheaders ( table ) ;
struct ctl_table_header * header = NULL , * * subheaders , * * subheader ;
2012-01-21 22:26:26 +04:00
const struct ctl_path * component ;
char * new_path , * pos ;
pos = new_path = kmalloc ( PATH_MAX , GFP_KERNEL ) ;
if ( ! new_path )
return NULL ;
pos [ 0 ] = ' \0 ' ;
for ( component = path ; component - > procname ; component + + ) {
pos = append_path ( new_path , pos , component - > procname ) ;
if ( ! pos )
goto out ;
}
2012-01-22 00:35:23 +04:00
while ( table - > procname & & table - > child & & ! table [ 1 ] . procname ) {
pos = append_path ( new_path , pos , table - > procname ) ;
if ( ! pos )
goto out ;
table = table - > child ;
}
2012-01-23 06:22:05 +04:00
if ( nr_subheaders = = 1 ) {
2012-01-08 12:02:37 +04:00
header = __register_sysctl_table ( set , new_path , table ) ;
2012-01-23 06:22:05 +04:00
if ( header )
header - > ctl_table_arg = ctl_table_arg ;
} else {
header = kzalloc ( sizeof ( * header ) +
sizeof ( * subheaders ) * nr_subheaders , GFP_KERNEL ) ;
if ( ! header )
goto out ;
subheaders = ( struct ctl_table_header * * ) ( header + 1 ) ;
subheader = subheaders ;
2012-01-22 00:35:23 +04:00
header - > ctl_table_arg = ctl_table_arg ;
2012-01-23 06:22:05 +04:00
if ( register_leaf_sysctl_tables ( new_path , pos , & subheader ,
2012-01-08 12:02:37 +04:00
set , table ) )
2012-01-23 06:22:05 +04:00
goto err_register_leaves ;
}
2012-01-21 22:26:26 +04:00
out :
kfree ( new_path ) ;
return header ;
2012-01-23 06:22:05 +04:00
err_register_leaves :
while ( subheader > subheaders ) {
struct ctl_table_header * subh = * ( - - subheader ) ;
struct ctl_table * table = subh - > ctl_table_arg ;
unregister_sysctl_table ( subh ) ;
kfree ( table ) ;
}
kfree ( header ) ;
header = NULL ;
goto out ;
2012-01-21 22:26:26 +04:00
}
2012-01-06 16:07:15 +04:00
/**
2021-05-07 04:02:24 +03:00
* register_sysctl_paths - register a sysctl table hierarchy
2012-01-06 16:07:15 +04:00
* @ path : The path to the directory the sysctl table is in .
* @ table : the top - level table structure
*
* Register a sysctl table hierarchy . @ table should be a filled in ctl_table
* array . A completely 0 filled entry terminates the table .
2023-03-11 00:00:16 +03:00
* We are slowly deprecating this caller so avoid future uses of it .
2012-01-06 16:07:15 +04:00
*
* See __register_sysctl_paths for more details .
*/
struct ctl_table_header * register_sysctl_paths ( const struct ctl_path * path ,
struct ctl_table * table )
{
2012-01-08 12:02:37 +04:00
return __register_sysctl_paths ( & sysctl_table_root . default_set ,
2012-01-06 16:07:15 +04:00
path , table ) ;
}
EXPORT_SYMBOL ( register_sysctl_paths ) ;
/**
* register_sysctl_table - register a sysctl table hierarchy
* @ table : the top - level table structure
*
* Register a sysctl table hierarchy . @ table should be a filled in ctl_table
* array . A completely 0 filled entry terminates the table .
*
* See register_sysctl_paths for more details .
*/
struct ctl_table_header * register_sysctl_table ( struct ctl_table * table )
{
static const struct ctl_path null_path [ ] = { { } } ;
return register_sysctl_paths ( null_path , table ) ;
}
EXPORT_SYMBOL ( register_sysctl_table ) ;
2022-01-22 09:13:24 +03:00
int __register_sysctl_base ( struct ctl_table * base_table )
{
struct ctl_table_header * hdr ;
hdr = register_sysctl_table ( base_table ) ;
kmemleak_not_leak ( hdr ) ;
return 0 ;
}
2012-01-08 11:24:30 +04:00
static void put_links ( struct ctl_table_header * header )
{
struct ctl_table_set * root_set = & sysctl_table_root . default_set ;
struct ctl_table_root * root = header - > root ;
struct ctl_dir * parent = header - > parent ;
struct ctl_dir * core_parent ;
struct ctl_table * entry ;
if ( header - > set = = root_set )
return ;
core_parent = xlate_dir ( root_set , parent ) ;
if ( IS_ERR ( core_parent ) )
return ;
2022-04-11 08:12:05 +03:00
list_for_each_table_entry ( entry , header - > ctl_table ) {
2012-01-08 11:24:30 +04:00
struct ctl_table_header * link_head ;
struct ctl_table * link ;
const char * name = entry - > procname ;
link = find_entry ( & link_head , core_parent , name , strlen ( name ) ) ;
if ( link & &
( ( S_ISDIR ( link - > mode ) & & S_ISDIR ( entry - > mode ) ) | |
( S_ISLNK ( link - > mode ) & & ( link - > data = = root ) ) ) ) {
drop_sysctl_table ( link_head ) ;
}
else {
2013-02-28 05:03:16 +04:00
pr_err ( " sysctl link missing during unregister: " ) ;
2012-01-08 11:24:30 +04:00
sysctl_print_dir ( parent ) ;
2022-01-20 05:08:06 +03:00
pr_cont ( " %s \n " , name ) ;
2012-01-08 11:24:30 +04:00
}
}
}
2012-01-10 05:24:30 +04:00
static void drop_sysctl_table ( struct ctl_table_header * header )
{
2011-12-29 20:24:29 +04:00
struct ctl_dir * parent = header - > parent ;
2012-01-10 05:24:30 +04:00
if ( - - header - > nreg )
return ;
2019-04-26 08:24:05 +03:00
if ( parent ) {
2019-03-29 06:44:40 +03:00
put_links ( header ) ;
2019-04-26 08:24:05 +03:00
start_unregistering ( header ) ;
}
2012-01-10 05:24:30 +04:00
if ( ! - - header - > count )
kfree_rcu ( header , rcu ) ;
2011-12-29 20:24:29 +04:00
if ( parent )
drop_sysctl_table ( & parent - > header ) ;
2012-01-10 05:24:30 +04:00
}
2012-01-06 16:07:15 +04:00
/**
* unregister_sysctl_table - unregister a sysctl table hierarchy
* @ header : the header returned from register_sysctl_table
*
* Unregisters the sysctl table and all children . proc entries may not
* actually be removed until they are no longer used by anyone .
*/
void unregister_sysctl_table ( struct ctl_table_header * header )
{
2012-01-23 06:22:05 +04:00
int nr_subheaders ;
2012-01-06 16:07:15 +04:00
might_sleep ( ) ;
if ( header = = NULL )
return ;
2012-01-23 06:22:05 +04:00
nr_subheaders = count_subheaders ( header - > ctl_table_arg ) ;
if ( unlikely ( nr_subheaders > 1 ) ) {
struct ctl_table_header * * subheaders ;
int i ;
subheaders = ( struct ctl_table_header * * ) ( header + 1 ) ;
for ( i = nr_subheaders - 1 ; i > = 0 ; i - - ) {
struct ctl_table_header * subh = subheaders [ i ] ;
struct ctl_table * table = subh - > ctl_table_arg ;
unregister_sysctl_table ( subh ) ;
kfree ( table ) ;
}
kfree ( header ) ;
return ;
}
2012-01-06 16:07:15 +04:00
spin_lock ( & sysctl_lock ) ;
2012-01-10 05:24:30 +04:00
drop_sysctl_table ( header ) ;
2012-01-06 16:07:15 +04:00
spin_unlock ( & sysctl_lock ) ;
}
EXPORT_SYMBOL ( unregister_sysctl_table ) ;
2012-01-08 11:24:30 +04:00
void setup_sysctl_set ( struct ctl_table_set * set ,
2012-01-23 09:26:00 +04:00
struct ctl_table_root * root ,
2012-01-06 16:07:15 +04:00
int ( * is_seen ) ( struct ctl_table_set * ) )
{
2012-01-30 17:40:29 +04:00
memset ( set , 0 , sizeof ( * set ) ) ;
2012-01-08 11:24:30 +04:00
set - > is_seen = is_seen ;
2012-01-10 05:24:30 +04:00
init_header ( & set - > dir . header , root , set , NULL , root_table ) ;
2012-01-06 16:07:15 +04:00
}
2012-01-10 10:19:13 +04:00
void retire_sysctl_set ( struct ctl_table_set * set )
{
2012-01-10 05:24:30 +04:00
WARN_ON ( ! RB_EMPTY_ROOT ( & set - > dir . root ) ) ;
2012-01-10 10:19:13 +04:00
}
2012-01-06 16:07:15 +04:00
2008-10-17 05:07:44 +04:00
int __init proc_sys_init ( void )
2007-02-14 11:34:12 +03:00
{
2008-10-03 00:23:32 +04:00
struct proc_dir_entry * proc_sys_root ;
2007-02-14 11:34:12 +03:00
proc_sys_root = proc_mkdir ( " sys " , NULL ) ;
2008-07-15 16:54:06 +04:00
proc_sys_root - > proc_iops = & proc_sys_dir_operations ;
proc: decouple proc from VFS with "struct proc_ops"
Currently core /proc code uses "struct file_operations" for custom hooks,
however, VFS doesn't directly call them. Every time VFS expands
file_operations hook set, /proc code bloats for no reason.
Introduce "struct proc_ops" which contains only those hooks which /proc
allows to call into (open, release, read, write, ioctl, mmap, poll). It
doesn't contain module pointer as well.
Save ~184 bytes per usage:
add/remove: 26/26 grow/shrink: 1/4 up/down: 1922/-6674 (-4752)
Function old new delta
sysvipc_proc_ops - 72 +72
...
config_gz_proc_ops - 72 +72
proc_get_inode 289 339 +50
proc_reg_get_unmapped_area 110 107 -3
close_pdeo 227 224 -3
proc_reg_open 289 284 -5
proc_create_data 60 53 -7
rt_cpu_seq_fops 256 - -256
...
default_affinity_proc_fops 256 - -256
Total: Before=5430095, After=5425343, chg -0.09%
Link: http://lkml.kernel.org/r/20191225172228.GA13378@avx2
Signed-off-by: Alexey Dobriyan <adobriyan@gmail.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-02-04 04:37:14 +03:00
proc_sys_root - > proc_dir_ops = & proc_sys_dir_file_operations ;
2007-02-14 11:34:12 +03:00
proc_sys_root - > nlink = 0 ;
2012-01-06 15:34:20 +04:00
2022-01-22 09:13:31 +03:00
return sysctl_init_bases ( ) ;
2007-02-14 11:34:12 +03:00
}
kernel/sysctl: support setting sysctl parameters from kernel command line
Patch series "support setting sysctl parameters from kernel command line", v3.
This series adds support for something that seems like many people
always wanted but nobody added it yet, so here's the ability to set
sysctl parameters via kernel command line options in the form of
sysctl.vm.something=1
The important part is Patch 1. The second, not so important part is an
attempt to clean up legacy one-off parameters that do the same thing as
a sysctl. I don't want to remove them completely for compatibility
reasons, but with generic sysctl support the idea is to remove the
one-off param handlers and treat the parameters as aliases for the
sysctl variants.
I have identified several parameters that mention sysctl counterparts in
Documentation/admin-guide/kernel-parameters.txt but there might be more.
The conversion also has varying level of success:
- numa_zonelist_order is converted in Patch 2 together with adding the
necessary infrastructure. It's easy as it doesn't really do anything
but warn on deprecated value these days.
- hung_task_panic is converted in Patch 3, but there's a downside that
now it only accepts 0 and 1, while previously it was any integer
value
- nmi_watchdog maps to two sysctls nmi_watchdog and hardlockup_panic,
so there's no straighforward conversion possible
- traceoff_on_warning is a flag without value and it would be required
to handle that somehow in the conversion infractructure, which seems
pointless for a single flag
This patch (of 5):
A recently proposed patch to add vm_swappiness command line parameter in
addition to existing sysctl [1] made me wonder why we don't have a
general support for passing sysctl parameters via command line.
Googling found only somebody else wondering the same [2], but I haven't
found any prior discussion with reasons why not to do this.
Settings the vm_swappiness issue aside (the underlying issue might be
solved in a different way), quick search of kernel-parameters.txt shows
there are already some that exist as both sysctl and kernel parameter -
hung_task_panic, nmi_watchdog, numa_zonelist_order, traceoff_on_warning.
A general mechanism would remove the need to add more of those one-offs
and might be handy in situations where configuration by e.g.
/etc/sysctl.d/ is impractical.
Hence, this patch adds a new parse_args() pass that looks for parameters
prefixed by 'sysctl.' and tries to interpret them as writes to the
corresponding sys/ files using an temporary in-kernel procfs mount.
This mechanism was suggested by Eric W. Biederman [3], as it handles
all dynamically registered sysctl tables, even though we don't handle
modular sysctls. Errors due to e.g. invalid parameter name or value
are reported in the kernel log.
The processing is hooked right before the init process is loaded, as
some handlers might be more complicated than simple setters and might
need some subsystems to be initialized. At the moment the init process
can be started and eventually execute a process writing to /proc/sys/
then it should be also fine to do that from the kernel.
Sysctls registered later on module load time are not set by this
mechanism - it's expected that in such scenarios, setting sysctl values
from userspace is practical enough.
[1] https://lore.kernel.org/r/BL0PR02MB560167492CA4094C91589930E9FC0@BL0PR02MB5601.namprd02.prod.outlook.com/
[2] https://unix.stackexchange.com/questions/558802/how-to-set-sysctl-using-kernel-command-line-parameter
[3] https://lore.kernel.org/r/87bloj2skm.fsf@x220.int.ebiederm.org/
Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Reviewed-by: Luis Chamberlain <mcgrof@kernel.org>
Reviewed-by: Masami Hiramatsu <mhiramat@kernel.org>
Acked-by: Kees Cook <keescook@chromium.org>
Acked-by: Michal Hocko <mhocko@suse.com>
Cc: Iurii Zaikin <yzaikin@google.com>
Cc: Ivan Teterevkov <ivan.teterevkov@nutanix.com>
Cc: Michal Hocko <mhocko@kernel.org>
Cc: David Rientjes <rientjes@google.com>
Cc: Matthew Wilcox <willy@infradead.org>
Cc: "Eric W . Biederman" <ebiederm@xmission.com>
Cc: "Guilherme G . Piccoli" <gpiccoli@canonical.com>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Christian Brauner <christian.brauner@ubuntu.com>
Link: http://lkml.kernel.org/r/20200427180433.7029-1-vbabka@suse.cz
Link: http://lkml.kernel.org/r/20200427180433.7029-2-vbabka@suse.cz
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-06-08 07:40:24 +03:00
2020-06-08 07:40:27 +03:00
struct sysctl_alias {
const char * kernel_param ;
const char * sysctl_param ;
} ;
/*
* Historically some settings had both sysctl and a command line parameter .
* With the generic sysctl . parameter support , we can handle them at a single
* place and only keep the historical name for compatibility . This is not meant
* to add brand new aliases . When adding existing aliases , consider whether
* the possibly different moment of changing the value ( e . g . from early_param
* to the moment do_sysctl_args ( ) is called ) is an issue for the specific
* parameter .
*/
static const struct sysctl_alias sysctl_aliases [ ] = {
2020-06-08 07:40:42 +03:00
{ " hardlockup_all_cpu_backtrace " , " kernel.hardlockup_all_cpu_backtrace " } ,
{ " hung_task_panic " , " kernel.hung_task_panic " } ,
{ " numa_zonelist_order " , " vm.numa_zonelist_order " } ,
{ " softlockup_all_cpu_backtrace " , " kernel.softlockup_all_cpu_backtrace " } ,
{ " softlockup_panic " , " kernel.softlockup_panic " } ,
2020-06-08 07:40:27 +03:00
{ }
} ;
static const char * sysctl_find_alias ( char * param )
{
const struct sysctl_alias * alias ;
for ( alias = & sysctl_aliases [ 0 ] ; alias - > kernel_param ! = NULL ; alias + + ) {
if ( strcmp ( alias - > kernel_param , param ) = = 0 )
return alias - > sysctl_param ;
}
return NULL ;
}
kernel/sysctl: support setting sysctl parameters from kernel command line
Patch series "support setting sysctl parameters from kernel command line", v3.
This series adds support for something that seems like many people
always wanted but nobody added it yet, so here's the ability to set
sysctl parameters via kernel command line options in the form of
sysctl.vm.something=1
The important part is Patch 1. The second, not so important part is an
attempt to clean up legacy one-off parameters that do the same thing as
a sysctl. I don't want to remove them completely for compatibility
reasons, but with generic sysctl support the idea is to remove the
one-off param handlers and treat the parameters as aliases for the
sysctl variants.
I have identified several parameters that mention sysctl counterparts in
Documentation/admin-guide/kernel-parameters.txt but there might be more.
The conversion also has varying level of success:
- numa_zonelist_order is converted in Patch 2 together with adding the
necessary infrastructure. It's easy as it doesn't really do anything
but warn on deprecated value these days.
- hung_task_panic is converted in Patch 3, but there's a downside that
now it only accepts 0 and 1, while previously it was any integer
value
- nmi_watchdog maps to two sysctls nmi_watchdog and hardlockup_panic,
so there's no straighforward conversion possible
- traceoff_on_warning is a flag without value and it would be required
to handle that somehow in the conversion infractructure, which seems
pointless for a single flag
This patch (of 5):
A recently proposed patch to add vm_swappiness command line parameter in
addition to existing sysctl [1] made me wonder why we don't have a
general support for passing sysctl parameters via command line.
Googling found only somebody else wondering the same [2], but I haven't
found any prior discussion with reasons why not to do this.
Settings the vm_swappiness issue aside (the underlying issue might be
solved in a different way), quick search of kernel-parameters.txt shows
there are already some that exist as both sysctl and kernel parameter -
hung_task_panic, nmi_watchdog, numa_zonelist_order, traceoff_on_warning.
A general mechanism would remove the need to add more of those one-offs
and might be handy in situations where configuration by e.g.
/etc/sysctl.d/ is impractical.
Hence, this patch adds a new parse_args() pass that looks for parameters
prefixed by 'sysctl.' and tries to interpret them as writes to the
corresponding sys/ files using an temporary in-kernel procfs mount.
This mechanism was suggested by Eric W. Biederman [3], as it handles
all dynamically registered sysctl tables, even though we don't handle
modular sysctls. Errors due to e.g. invalid parameter name or value
are reported in the kernel log.
The processing is hooked right before the init process is loaded, as
some handlers might be more complicated than simple setters and might
need some subsystems to be initialized. At the moment the init process
can be started and eventually execute a process writing to /proc/sys/
then it should be also fine to do that from the kernel.
Sysctls registered later on module load time are not set by this
mechanism - it's expected that in such scenarios, setting sysctl values
from userspace is practical enough.
[1] https://lore.kernel.org/r/BL0PR02MB560167492CA4094C91589930E9FC0@BL0PR02MB5601.namprd02.prod.outlook.com/
[2] https://unix.stackexchange.com/questions/558802/how-to-set-sysctl-using-kernel-command-line-parameter
[3] https://lore.kernel.org/r/87bloj2skm.fsf@x220.int.ebiederm.org/
Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Reviewed-by: Luis Chamberlain <mcgrof@kernel.org>
Reviewed-by: Masami Hiramatsu <mhiramat@kernel.org>
Acked-by: Kees Cook <keescook@chromium.org>
Acked-by: Michal Hocko <mhocko@suse.com>
Cc: Iurii Zaikin <yzaikin@google.com>
Cc: Ivan Teterevkov <ivan.teterevkov@nutanix.com>
Cc: Michal Hocko <mhocko@kernel.org>
Cc: David Rientjes <rientjes@google.com>
Cc: Matthew Wilcox <willy@infradead.org>
Cc: "Eric W . Biederman" <ebiederm@xmission.com>
Cc: "Guilherme G . Piccoli" <gpiccoli@canonical.com>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Christian Brauner <christian.brauner@ubuntu.com>
Link: http://lkml.kernel.org/r/20200427180433.7029-1-vbabka@suse.cz
Link: http://lkml.kernel.org/r/20200427180433.7029-2-vbabka@suse.cz
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-06-08 07:40:24 +03:00
/* Set sysctl value passed on kernel command line. */
static int process_sysctl_arg ( char * param , char * val ,
const char * unused , void * arg )
{
char * path ;
struct vfsmount * * proc_mnt = arg ;
struct file_system_type * proc_fs_type ;
struct file * file ;
int len ;
int err ;
loff_t pos = 0 ;
ssize_t wret ;
2020-06-08 07:40:27 +03:00
if ( strncmp ( param , " sysctl " , sizeof ( " sysctl " ) - 1 ) = = 0 ) {
param + = sizeof ( " sysctl " ) - 1 ;
kernel/sysctl: support setting sysctl parameters from kernel command line
Patch series "support setting sysctl parameters from kernel command line", v3.
This series adds support for something that seems like many people
always wanted but nobody added it yet, so here's the ability to set
sysctl parameters via kernel command line options in the form of
sysctl.vm.something=1
The important part is Patch 1. The second, not so important part is an
attempt to clean up legacy one-off parameters that do the same thing as
a sysctl. I don't want to remove them completely for compatibility
reasons, but with generic sysctl support the idea is to remove the
one-off param handlers and treat the parameters as aliases for the
sysctl variants.
I have identified several parameters that mention sysctl counterparts in
Documentation/admin-guide/kernel-parameters.txt but there might be more.
The conversion also has varying level of success:
- numa_zonelist_order is converted in Patch 2 together with adding the
necessary infrastructure. It's easy as it doesn't really do anything
but warn on deprecated value these days.
- hung_task_panic is converted in Patch 3, but there's a downside that
now it only accepts 0 and 1, while previously it was any integer
value
- nmi_watchdog maps to two sysctls nmi_watchdog and hardlockup_panic,
so there's no straighforward conversion possible
- traceoff_on_warning is a flag without value and it would be required
to handle that somehow in the conversion infractructure, which seems
pointless for a single flag
This patch (of 5):
A recently proposed patch to add vm_swappiness command line parameter in
addition to existing sysctl [1] made me wonder why we don't have a
general support for passing sysctl parameters via command line.
Googling found only somebody else wondering the same [2], but I haven't
found any prior discussion with reasons why not to do this.
Settings the vm_swappiness issue aside (the underlying issue might be
solved in a different way), quick search of kernel-parameters.txt shows
there are already some that exist as both sysctl and kernel parameter -
hung_task_panic, nmi_watchdog, numa_zonelist_order, traceoff_on_warning.
A general mechanism would remove the need to add more of those one-offs
and might be handy in situations where configuration by e.g.
/etc/sysctl.d/ is impractical.
Hence, this patch adds a new parse_args() pass that looks for parameters
prefixed by 'sysctl.' and tries to interpret them as writes to the
corresponding sys/ files using an temporary in-kernel procfs mount.
This mechanism was suggested by Eric W. Biederman [3], as it handles
all dynamically registered sysctl tables, even though we don't handle
modular sysctls. Errors due to e.g. invalid parameter name or value
are reported in the kernel log.
The processing is hooked right before the init process is loaded, as
some handlers might be more complicated than simple setters and might
need some subsystems to be initialized. At the moment the init process
can be started and eventually execute a process writing to /proc/sys/
then it should be also fine to do that from the kernel.
Sysctls registered later on module load time are not set by this
mechanism - it's expected that in such scenarios, setting sysctl values
from userspace is practical enough.
[1] https://lore.kernel.org/r/BL0PR02MB560167492CA4094C91589930E9FC0@BL0PR02MB5601.namprd02.prod.outlook.com/
[2] https://unix.stackexchange.com/questions/558802/how-to-set-sysctl-using-kernel-command-line-parameter
[3] https://lore.kernel.org/r/87bloj2skm.fsf@x220.int.ebiederm.org/
Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Reviewed-by: Luis Chamberlain <mcgrof@kernel.org>
Reviewed-by: Masami Hiramatsu <mhiramat@kernel.org>
Acked-by: Kees Cook <keescook@chromium.org>
Acked-by: Michal Hocko <mhocko@suse.com>
Cc: Iurii Zaikin <yzaikin@google.com>
Cc: Ivan Teterevkov <ivan.teterevkov@nutanix.com>
Cc: Michal Hocko <mhocko@kernel.org>
Cc: David Rientjes <rientjes@google.com>
Cc: Matthew Wilcox <willy@infradead.org>
Cc: "Eric W . Biederman" <ebiederm@xmission.com>
Cc: "Guilherme G . Piccoli" <gpiccoli@canonical.com>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Christian Brauner <christian.brauner@ubuntu.com>
Link: http://lkml.kernel.org/r/20200427180433.7029-1-vbabka@suse.cz
Link: http://lkml.kernel.org/r/20200427180433.7029-2-vbabka@suse.cz
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-06-08 07:40:24 +03:00
2020-06-08 07:40:27 +03:00
if ( param [ 0 ] ! = ' / ' & & param [ 0 ] ! = ' . ' )
return 0 ;
kernel/sysctl: support setting sysctl parameters from kernel command line
Patch series "support setting sysctl parameters from kernel command line", v3.
This series adds support for something that seems like many people
always wanted but nobody added it yet, so here's the ability to set
sysctl parameters via kernel command line options in the form of
sysctl.vm.something=1
The important part is Patch 1. The second, not so important part is an
attempt to clean up legacy one-off parameters that do the same thing as
a sysctl. I don't want to remove them completely for compatibility
reasons, but with generic sysctl support the idea is to remove the
one-off param handlers and treat the parameters as aliases for the
sysctl variants.
I have identified several parameters that mention sysctl counterparts in
Documentation/admin-guide/kernel-parameters.txt but there might be more.
The conversion also has varying level of success:
- numa_zonelist_order is converted in Patch 2 together with adding the
necessary infrastructure. It's easy as it doesn't really do anything
but warn on deprecated value these days.
- hung_task_panic is converted in Patch 3, but there's a downside that
now it only accepts 0 and 1, while previously it was any integer
value
- nmi_watchdog maps to two sysctls nmi_watchdog and hardlockup_panic,
so there's no straighforward conversion possible
- traceoff_on_warning is a flag without value and it would be required
to handle that somehow in the conversion infractructure, which seems
pointless for a single flag
This patch (of 5):
A recently proposed patch to add vm_swappiness command line parameter in
addition to existing sysctl [1] made me wonder why we don't have a
general support for passing sysctl parameters via command line.
Googling found only somebody else wondering the same [2], but I haven't
found any prior discussion with reasons why not to do this.
Settings the vm_swappiness issue aside (the underlying issue might be
solved in a different way), quick search of kernel-parameters.txt shows
there are already some that exist as both sysctl and kernel parameter -
hung_task_panic, nmi_watchdog, numa_zonelist_order, traceoff_on_warning.
A general mechanism would remove the need to add more of those one-offs
and might be handy in situations where configuration by e.g.
/etc/sysctl.d/ is impractical.
Hence, this patch adds a new parse_args() pass that looks for parameters
prefixed by 'sysctl.' and tries to interpret them as writes to the
corresponding sys/ files using an temporary in-kernel procfs mount.
This mechanism was suggested by Eric W. Biederman [3], as it handles
all dynamically registered sysctl tables, even though we don't handle
modular sysctls. Errors due to e.g. invalid parameter name or value
are reported in the kernel log.
The processing is hooked right before the init process is loaded, as
some handlers might be more complicated than simple setters and might
need some subsystems to be initialized. At the moment the init process
can be started and eventually execute a process writing to /proc/sys/
then it should be also fine to do that from the kernel.
Sysctls registered later on module load time are not set by this
mechanism - it's expected that in such scenarios, setting sysctl values
from userspace is practical enough.
[1] https://lore.kernel.org/r/BL0PR02MB560167492CA4094C91589930E9FC0@BL0PR02MB5601.namprd02.prod.outlook.com/
[2] https://unix.stackexchange.com/questions/558802/how-to-set-sysctl-using-kernel-command-line-parameter
[3] https://lore.kernel.org/r/87bloj2skm.fsf@x220.int.ebiederm.org/
Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Reviewed-by: Luis Chamberlain <mcgrof@kernel.org>
Reviewed-by: Masami Hiramatsu <mhiramat@kernel.org>
Acked-by: Kees Cook <keescook@chromium.org>
Acked-by: Michal Hocko <mhocko@suse.com>
Cc: Iurii Zaikin <yzaikin@google.com>
Cc: Ivan Teterevkov <ivan.teterevkov@nutanix.com>
Cc: Michal Hocko <mhocko@kernel.org>
Cc: David Rientjes <rientjes@google.com>
Cc: Matthew Wilcox <willy@infradead.org>
Cc: "Eric W . Biederman" <ebiederm@xmission.com>
Cc: "Guilherme G . Piccoli" <gpiccoli@canonical.com>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Christian Brauner <christian.brauner@ubuntu.com>
Link: http://lkml.kernel.org/r/20200427180433.7029-1-vbabka@suse.cz
Link: http://lkml.kernel.org/r/20200427180433.7029-2-vbabka@suse.cz
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-06-08 07:40:24 +03:00
2020-06-08 07:40:27 +03:00
param + + ;
} else {
param = ( char * ) sysctl_find_alias ( param ) ;
if ( ! param )
return 0 ;
}
kernel/sysctl: support setting sysctl parameters from kernel command line
Patch series "support setting sysctl parameters from kernel command line", v3.
This series adds support for something that seems like many people
always wanted but nobody added it yet, so here's the ability to set
sysctl parameters via kernel command line options in the form of
sysctl.vm.something=1
The important part is Patch 1. The second, not so important part is an
attempt to clean up legacy one-off parameters that do the same thing as
a sysctl. I don't want to remove them completely for compatibility
reasons, but with generic sysctl support the idea is to remove the
one-off param handlers and treat the parameters as aliases for the
sysctl variants.
I have identified several parameters that mention sysctl counterparts in
Documentation/admin-guide/kernel-parameters.txt but there might be more.
The conversion also has varying level of success:
- numa_zonelist_order is converted in Patch 2 together with adding the
necessary infrastructure. It's easy as it doesn't really do anything
but warn on deprecated value these days.
- hung_task_panic is converted in Patch 3, but there's a downside that
now it only accepts 0 and 1, while previously it was any integer
value
- nmi_watchdog maps to two sysctls nmi_watchdog and hardlockup_panic,
so there's no straighforward conversion possible
- traceoff_on_warning is a flag without value and it would be required
to handle that somehow in the conversion infractructure, which seems
pointless for a single flag
This patch (of 5):
A recently proposed patch to add vm_swappiness command line parameter in
addition to existing sysctl [1] made me wonder why we don't have a
general support for passing sysctl parameters via command line.
Googling found only somebody else wondering the same [2], but I haven't
found any prior discussion with reasons why not to do this.
Settings the vm_swappiness issue aside (the underlying issue might be
solved in a different way), quick search of kernel-parameters.txt shows
there are already some that exist as both sysctl and kernel parameter -
hung_task_panic, nmi_watchdog, numa_zonelist_order, traceoff_on_warning.
A general mechanism would remove the need to add more of those one-offs
and might be handy in situations where configuration by e.g.
/etc/sysctl.d/ is impractical.
Hence, this patch adds a new parse_args() pass that looks for parameters
prefixed by 'sysctl.' and tries to interpret them as writes to the
corresponding sys/ files using an temporary in-kernel procfs mount.
This mechanism was suggested by Eric W. Biederman [3], as it handles
all dynamically registered sysctl tables, even though we don't handle
modular sysctls. Errors due to e.g. invalid parameter name or value
are reported in the kernel log.
The processing is hooked right before the init process is loaded, as
some handlers might be more complicated than simple setters and might
need some subsystems to be initialized. At the moment the init process
can be started and eventually execute a process writing to /proc/sys/
then it should be also fine to do that from the kernel.
Sysctls registered later on module load time are not set by this
mechanism - it's expected that in such scenarios, setting sysctl values
from userspace is practical enough.
[1] https://lore.kernel.org/r/BL0PR02MB560167492CA4094C91589930E9FC0@BL0PR02MB5601.namprd02.prod.outlook.com/
[2] https://unix.stackexchange.com/questions/558802/how-to-set-sysctl-using-kernel-command-line-parameter
[3] https://lore.kernel.org/r/87bloj2skm.fsf@x220.int.ebiederm.org/
Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Reviewed-by: Luis Chamberlain <mcgrof@kernel.org>
Reviewed-by: Masami Hiramatsu <mhiramat@kernel.org>
Acked-by: Kees Cook <keescook@chromium.org>
Acked-by: Michal Hocko <mhocko@suse.com>
Cc: Iurii Zaikin <yzaikin@google.com>
Cc: Ivan Teterevkov <ivan.teterevkov@nutanix.com>
Cc: Michal Hocko <mhocko@kernel.org>
Cc: David Rientjes <rientjes@google.com>
Cc: Matthew Wilcox <willy@infradead.org>
Cc: "Eric W . Biederman" <ebiederm@xmission.com>
Cc: "Guilherme G . Piccoli" <gpiccoli@canonical.com>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Christian Brauner <christian.brauner@ubuntu.com>
Link: http://lkml.kernel.org/r/20200427180433.7029-1-vbabka@suse.cz
Link: http://lkml.kernel.org/r/20200427180433.7029-2-vbabka@suse.cz
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-06-08 07:40:24 +03:00
2021-01-24 08:02:16 +03:00
if ( ! val )
return - EINVAL ;
len = strlen ( val ) ;
if ( len = = 0 )
return - EINVAL ;
kernel/sysctl: support setting sysctl parameters from kernel command line
Patch series "support setting sysctl parameters from kernel command line", v3.
This series adds support for something that seems like many people
always wanted but nobody added it yet, so here's the ability to set
sysctl parameters via kernel command line options in the form of
sysctl.vm.something=1
The important part is Patch 1. The second, not so important part is an
attempt to clean up legacy one-off parameters that do the same thing as
a sysctl. I don't want to remove them completely for compatibility
reasons, but with generic sysctl support the idea is to remove the
one-off param handlers and treat the parameters as aliases for the
sysctl variants.
I have identified several parameters that mention sysctl counterparts in
Documentation/admin-guide/kernel-parameters.txt but there might be more.
The conversion also has varying level of success:
- numa_zonelist_order is converted in Patch 2 together with adding the
necessary infrastructure. It's easy as it doesn't really do anything
but warn on deprecated value these days.
- hung_task_panic is converted in Patch 3, but there's a downside that
now it only accepts 0 and 1, while previously it was any integer
value
- nmi_watchdog maps to two sysctls nmi_watchdog and hardlockup_panic,
so there's no straighforward conversion possible
- traceoff_on_warning is a flag without value and it would be required
to handle that somehow in the conversion infractructure, which seems
pointless for a single flag
This patch (of 5):
A recently proposed patch to add vm_swappiness command line parameter in
addition to existing sysctl [1] made me wonder why we don't have a
general support for passing sysctl parameters via command line.
Googling found only somebody else wondering the same [2], but I haven't
found any prior discussion with reasons why not to do this.
Settings the vm_swappiness issue aside (the underlying issue might be
solved in a different way), quick search of kernel-parameters.txt shows
there are already some that exist as both sysctl and kernel parameter -
hung_task_panic, nmi_watchdog, numa_zonelist_order, traceoff_on_warning.
A general mechanism would remove the need to add more of those one-offs
and might be handy in situations where configuration by e.g.
/etc/sysctl.d/ is impractical.
Hence, this patch adds a new parse_args() pass that looks for parameters
prefixed by 'sysctl.' and tries to interpret them as writes to the
corresponding sys/ files using an temporary in-kernel procfs mount.
This mechanism was suggested by Eric W. Biederman [3], as it handles
all dynamically registered sysctl tables, even though we don't handle
modular sysctls. Errors due to e.g. invalid parameter name or value
are reported in the kernel log.
The processing is hooked right before the init process is loaded, as
some handlers might be more complicated than simple setters and might
need some subsystems to be initialized. At the moment the init process
can be started and eventually execute a process writing to /proc/sys/
then it should be also fine to do that from the kernel.
Sysctls registered later on module load time are not set by this
mechanism - it's expected that in such scenarios, setting sysctl values
from userspace is practical enough.
[1] https://lore.kernel.org/r/BL0PR02MB560167492CA4094C91589930E9FC0@BL0PR02MB5601.namprd02.prod.outlook.com/
[2] https://unix.stackexchange.com/questions/558802/how-to-set-sysctl-using-kernel-command-line-parameter
[3] https://lore.kernel.org/r/87bloj2skm.fsf@x220.int.ebiederm.org/
Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Reviewed-by: Luis Chamberlain <mcgrof@kernel.org>
Reviewed-by: Masami Hiramatsu <mhiramat@kernel.org>
Acked-by: Kees Cook <keescook@chromium.org>
Acked-by: Michal Hocko <mhocko@suse.com>
Cc: Iurii Zaikin <yzaikin@google.com>
Cc: Ivan Teterevkov <ivan.teterevkov@nutanix.com>
Cc: Michal Hocko <mhocko@kernel.org>
Cc: David Rientjes <rientjes@google.com>
Cc: Matthew Wilcox <willy@infradead.org>
Cc: "Eric W . Biederman" <ebiederm@xmission.com>
Cc: "Guilherme G . Piccoli" <gpiccoli@canonical.com>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Christian Brauner <christian.brauner@ubuntu.com>
Link: http://lkml.kernel.org/r/20200427180433.7029-1-vbabka@suse.cz
Link: http://lkml.kernel.org/r/20200427180433.7029-2-vbabka@suse.cz
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-06-08 07:40:24 +03:00
/*
* To set sysctl options , we use a temporary mount of proc , look up the
* respective sys / file and write to it . To avoid mounting it when no
* options were given , we mount it only when the first sysctl option is
* found . Why not a persistent mount ? There are problems with a
* persistent mount of proc in that it forces userspace not to use any
* proc mount options .
*/
if ( ! * proc_mnt ) {
proc_fs_type = get_fs_type ( " proc " ) ;
if ( ! proc_fs_type ) {
pr_err ( " Failed to find procfs to set sysctl from command line \n " ) ;
return 0 ;
}
* proc_mnt = kern_mount ( proc_fs_type ) ;
put_filesystem ( proc_fs_type ) ;
if ( IS_ERR ( * proc_mnt ) ) {
pr_err ( " Failed to mount procfs to set sysctl from command line \n " ) ;
return 0 ;
}
}
path = kasprintf ( GFP_KERNEL , " sys/%s " , param ) ;
if ( ! path )
panic ( " %s: Failed to allocate path for %s \n " , __func__ , param ) ;
strreplace ( path , ' . ' , ' / ' ) ;
2021-04-02 02:00:57 +03:00
file = file_open_root_mnt ( * proc_mnt , path , O_WRONLY , 0 ) ;
kernel/sysctl: support setting sysctl parameters from kernel command line
Patch series "support setting sysctl parameters from kernel command line", v3.
This series adds support for something that seems like many people
always wanted but nobody added it yet, so here's the ability to set
sysctl parameters via kernel command line options in the form of
sysctl.vm.something=1
The important part is Patch 1. The second, not so important part is an
attempt to clean up legacy one-off parameters that do the same thing as
a sysctl. I don't want to remove them completely for compatibility
reasons, but with generic sysctl support the idea is to remove the
one-off param handlers and treat the parameters as aliases for the
sysctl variants.
I have identified several parameters that mention sysctl counterparts in
Documentation/admin-guide/kernel-parameters.txt but there might be more.
The conversion also has varying level of success:
- numa_zonelist_order is converted in Patch 2 together with adding the
necessary infrastructure. It's easy as it doesn't really do anything
but warn on deprecated value these days.
- hung_task_panic is converted in Patch 3, but there's a downside that
now it only accepts 0 and 1, while previously it was any integer
value
- nmi_watchdog maps to two sysctls nmi_watchdog and hardlockup_panic,
so there's no straighforward conversion possible
- traceoff_on_warning is a flag without value and it would be required
to handle that somehow in the conversion infractructure, which seems
pointless for a single flag
This patch (of 5):
A recently proposed patch to add vm_swappiness command line parameter in
addition to existing sysctl [1] made me wonder why we don't have a
general support for passing sysctl parameters via command line.
Googling found only somebody else wondering the same [2], but I haven't
found any prior discussion with reasons why not to do this.
Settings the vm_swappiness issue aside (the underlying issue might be
solved in a different way), quick search of kernel-parameters.txt shows
there are already some that exist as both sysctl and kernel parameter -
hung_task_panic, nmi_watchdog, numa_zonelist_order, traceoff_on_warning.
A general mechanism would remove the need to add more of those one-offs
and might be handy in situations where configuration by e.g.
/etc/sysctl.d/ is impractical.
Hence, this patch adds a new parse_args() pass that looks for parameters
prefixed by 'sysctl.' and tries to interpret them as writes to the
corresponding sys/ files using an temporary in-kernel procfs mount.
This mechanism was suggested by Eric W. Biederman [3], as it handles
all dynamically registered sysctl tables, even though we don't handle
modular sysctls. Errors due to e.g. invalid parameter name or value
are reported in the kernel log.
The processing is hooked right before the init process is loaded, as
some handlers might be more complicated than simple setters and might
need some subsystems to be initialized. At the moment the init process
can be started and eventually execute a process writing to /proc/sys/
then it should be also fine to do that from the kernel.
Sysctls registered later on module load time are not set by this
mechanism - it's expected that in such scenarios, setting sysctl values
from userspace is practical enough.
[1] https://lore.kernel.org/r/BL0PR02MB560167492CA4094C91589930E9FC0@BL0PR02MB5601.namprd02.prod.outlook.com/
[2] https://unix.stackexchange.com/questions/558802/how-to-set-sysctl-using-kernel-command-line-parameter
[3] https://lore.kernel.org/r/87bloj2skm.fsf@x220.int.ebiederm.org/
Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Reviewed-by: Luis Chamberlain <mcgrof@kernel.org>
Reviewed-by: Masami Hiramatsu <mhiramat@kernel.org>
Acked-by: Kees Cook <keescook@chromium.org>
Acked-by: Michal Hocko <mhocko@suse.com>
Cc: Iurii Zaikin <yzaikin@google.com>
Cc: Ivan Teterevkov <ivan.teterevkov@nutanix.com>
Cc: Michal Hocko <mhocko@kernel.org>
Cc: David Rientjes <rientjes@google.com>
Cc: Matthew Wilcox <willy@infradead.org>
Cc: "Eric W . Biederman" <ebiederm@xmission.com>
Cc: "Guilherme G . Piccoli" <gpiccoli@canonical.com>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Christian Brauner <christian.brauner@ubuntu.com>
Link: http://lkml.kernel.org/r/20200427180433.7029-1-vbabka@suse.cz
Link: http://lkml.kernel.org/r/20200427180433.7029-2-vbabka@suse.cz
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-06-08 07:40:24 +03:00
if ( IS_ERR ( file ) ) {
err = PTR_ERR ( file ) ;
if ( err = = - ENOENT )
pr_err ( " Failed to set sysctl parameter '%s=%s': parameter not found \n " ,
param , val ) ;
else if ( err = = - EACCES )
pr_err ( " Failed to set sysctl parameter '%s=%s': permission denied (read-only?) \n " ,
param , val ) ;
else
pr_err ( " Error %pe opening proc file to set sysctl parameter '%s=%s' \n " ,
file , param , val ) ;
goto out ;
}
wret = kernel_write ( file , val , len , & pos ) ;
if ( wret < 0 ) {
err = wret ;
if ( err = = - EINVAL )
pr_err ( " Failed to set sysctl parameter '%s=%s': invalid value \n " ,
param , val ) ;
else
pr_err ( " Error %pe writing to proc file to set sysctl parameter '%s=%s' \n " ,
ERR_PTR ( err ) , param , val ) ;
} else if ( wret ! = len ) {
pr_err ( " Wrote only %zd bytes of %d writing to proc file %s to set sysctl parameter '%s=%s \n " ,
wret , len , path , param , val ) ;
}
err = filp_close ( file , NULL ) ;
if ( err )
pr_err ( " Error %pe closing proc file to set sysctl parameter '%s=%s \n " ,
ERR_PTR ( err ) , param , val ) ;
out :
kfree ( path ) ;
return 0 ;
}
void do_sysctl_args ( void )
{
char * command_line ;
struct vfsmount * proc_mnt = NULL ;
command_line = kstrdup ( saved_command_line , GFP_KERNEL ) ;
if ( ! command_line )
panic ( " %s: Failed to allocate copy of command line \n " , __func__ ) ;
parse_args ( " Setting sysctl args " , command_line ,
NULL , 0 , - 1 , - 1 , & proc_mnt , process_sysctl_arg ) ;
if ( proc_mnt )
kern_unmount ( proc_mnt ) ;
kfree ( command_line ) ;
}