Skip to contentSkip to conversation

constifications

PATCH In review Helge Deller 3 participants 5 messages Oct 8, 2006 – Oct 9, 2006

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 constifications

- completely constify string arrays, thus move them to the rodata section
- audit.c: mark some initially initialized variables __read_mostly

 include/linux/relay.h |    2 +-
 kernel/audit.c        |    8 ++++----
 kernel/configs.c      |    2 +-
 kernel/cpuset.c       |    4 ++--
 kernel/dma.c          |    2 +-
 kernel/futex.c        |    2 +-
 kernel/kallsyms.c     |    2 +-
 kernel/lockdep_proc.c |    4 ++--
 kernel/profile.c      |    2 +-
 kernel/relay.c        |    2 +-
 kernel/resource.c     |    4 ++--
 kernel/sched.c        |    2 +-
 12 files changed, 18 insertions(+), 18 deletions(-)
include/linux/relay.h +1 -1
index 24accb4..07c56e6 100644
@@ -274,7 +274,7 @@ static inline void subbuf_start_reserve(
 /*
  * exported relay file operations, kernel/relay.c
  */
-extern struct file_operations relay_file_operations;
+extern const struct file_operations relay_file_operations;
 
 #endif /* _LINUX_RELAY_H */
 
kernel/audit.c +4 -4
index 98106f6..9648dff 100644
@@ -62,13 +62,13 @@
 
 /* No auditing will take place until audit_initialized != 0.
  * (Initialization happens after skb_init is called.) */
-static int	audit_initialized;
+static int	audit_initialized __read_mostly;
 
 /* No syscall auditing will take place unless audit_enabled != 0. */
-int		audit_enabled;
+int		audit_enabled __read_mostly;
 
 /* Default state when kernel boots without any parameters. */
-static int	audit_default;
+static int	audit_default __read_mostly;
 
 /* If auditing cannot proceed, audit_failure selects what happens. */
 static int	audit_failure = AUDIT_FAIL_PRINTK;
@@ -1027,7 +1027,7 @@ void audit_log_hex(struct audit_buffer *
 	int i, avail, new_len;
 	unsigned char *ptr;
 	struct sk_buff *skb;
-	static const unsigned char *hex = "0123456789ABCDEF";
+	static const unsigned char hex[] = "0123456789ABCDEF";
 
 	if (!ab)
 		return;
kernel/configs.c +1 -1
index f9e3197..8fa1fb2 100644
@@ -75,7 +75,7 @@ ikconfig_read_current(struct file *file,
 	return count;
 }
 
-static struct file_operations ikconfig_file_ops = {
+static const struct file_operations ikconfig_file_ops = {
 	.owner = THIS_MODULE,
 	.read = ikconfig_read_current,
 };
kernel/cpuset.c +2 -2
index 9d850ae..b94b8de 100644
@@ -1532,7 +1532,7 @@ static int cpuset_rename(struct inode *o
 	return simple_rename(old_dir, old_dentry, new_dir, new_dentry);
 }
 
-static struct file_operations cpuset_file_operations = {
+static const struct file_operations cpuset_file_operations = {
 	.read = cpuset_file_read,
 	.write = cpuset_file_write,
 	.llseek = generic_file_llseek,
@@ -2610,7 +2610,7 @@ static int cpuset_open(struct inode *ino
 	return single_open(file, proc_cpuset_show, pid);
 }
 
-struct file_operations proc_cpuset_operations = {
+const struct file_operations proc_cpuset_operations = {
 	.open		= cpuset_open,
 	.read		= seq_read,
 	.llseek		= seq_lseek,
kernel/dma.c +1 -1
index 2020644..937b13c 100644
@@ -140,7 +140,7 @@ static int proc_dma_open(struct inode *i
 	return single_open(file, proc_dma_show, NULL);
 }
 
-static struct file_operations proc_dma_operations = {
+static const struct file_operations proc_dma_operations = {
 	.open		= proc_dma_open,
 	.read		= seq_read,
 	.llseek		= seq_lseek,
kernel/futex.c +1 -1
index 4aaf919..0c465c9 100644
@@ -1493,7 +1493,7 @@ static unsigned int futex_poll(struct fi
 	return ret;
 }
 
-static struct file_operations futex_fops = {
+static const struct file_operations futex_fops = {
 	.release	= futex_close,
 	.poll		= futex_poll,
 };
kernel/kallsyms.c +1 -1
index eeac3e3..27f9cb5 100644
@@ -436,7 +436,7 @@ static int kallsyms_release(struct inode
 	return seq_release(inode, file);
 }
 
-static struct file_operations kallsyms_operations = {
+static const struct file_operations kallsyms_operations = {
 	.open = kallsyms_open,
 	.read = seq_read,
 	.llseek = seq_lseek,
kernel/lockdep_proc.c +2 -2
index f6e72ea..5c8738f 100644
@@ -135,7 +135,7 @@ static int lockdep_open(struct inode *in
 	return res;
 }
 
-static struct file_operations proc_lockdep_operations = {
+static const struct file_operations proc_lockdep_operations = {
 	.open		= lockdep_open,
 	.read		= seq_read,
 	.llseek		= seq_lseek,
@@ -319,7 +319,7 @@ static int lockdep_stats_open(struct ino
 	return single_open(file, lockdep_stats_show, NULL);
 }
 
-static struct file_operations proc_lockdep_stats_operations = {
+static const struct file_operations proc_lockdep_stats_operations = {
 	.open		= lockdep_stats_open,
 	.read		= seq_read,
 	.llseek		= seq_lseek,
kernel/profile.c +1 -1
index 857300a..0fafd7b 100644
@@ -480,7 +480,7 @@ static ssize_t write_profile(struct file
 	return count;
 }
 
-static struct file_operations proc_profile_operations = {
+static const struct file_operations proc_profile_operations = {
 	.read		= read_profile,
 	.write		= write_profile,
 };
kernel/relay.c +1 -1
index 1d63ecd..a6b1dcc 100644
@@ -1008,7 +1008,7 @@ static ssize_t relay_file_sendfile(struc
 				       actor, target);
 }
 
-struct file_operations relay_file_operations = {
+const struct file_operations relay_file_operations = {
 	.open		= relay_file_open,
 	.poll		= relay_file_poll,
 	.mmap		= relay_file_mmap,
kernel/resource.c +2 -2
index 6de60c1..f0c2105 100644
@@ -115,14 +115,14 @@ static int iomem_open(struct inode *inod
 	return res;
 }
 
-static struct file_operations proc_ioports_operations = {
+static const struct file_operations proc_ioports_operations = {
 	.open		= ioports_open,
 	.read		= seq_read,
 	.llseek		= seq_lseek,
 	.release	= seq_release,
 };
 
-static struct file_operations proc_iomem_operations = {
+static const struct file_operations proc_iomem_operations = {
 	.open		= iomem_open,
 	.read		= seq_read,
 	.llseek		= seq_lseek,
kernel/sched.c +1 -1
index 53608a5..c9b6edb 100644
@@ -505,7 +505,7 @@ static int schedstat_open(struct inode *
 	return res;
 }
 
-struct file_operations proc_schedstat_operations = {
+const struct file_operations proc_schedstat_operations = {
 	.open    = schedstat_open,
 	.read    = seq_read,
 	.llseek  = seq_lseek,
#1 lore raw
Quoted 1 line · Helge Deller

- completely constify string arrays, thus move them to the rodata section

note that gcc 4.1 and later will do this automatically for static things at least...

#2 lore raw
Quoted 5 lines · Arjan van de Ven
Quoted 1 line · Helge Deller

- completely constify string arrays, thus move them to the rodata section

note that gcc 4.1 and later will do this automatically for static things at least...

Are you sure ?

At least with gcc-4.1.0 from SUSE 10.1 the strings array _pointers_ are not moved into the rodata section without the second "const":
const static char * const x[] = { "value1", "value2" };

Helge

#3 lore raw
Quoted 4 lines · Helge Deller
Quoted 3 lines · Arjan van de Ven
Quoted 1 line · Helge Deller

- completely constify string arrays, thus move them to the rodata section

Quoted 8 lines
Quoted 2 lines

note that gcc 4.1 and later will do this automatically for static things at least...

Are you sure ?

At least with gcc-4.1.0 from SUSE 10.1 the strings array _pointers_ are not moved into the rodata section without the second "const":
const static char * const x[] = { "value1", "value2" };

hmm I could have sworn GCC does this automatic nowadays as long as it can prove you're not writing to the thing (eg static and not passing the pointer to some external function).....

(even if gcc does this perfect I'm still in favor of the explicit const, just to catch stupid code with a warning)

Signature · 1 line
if you want to mail me at work (you don't), use arjan (at) linux.intel.com
#4 lore raw
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines
Quoted 1 line

- completely constify string arrays, thus move them to the rodata section

note that gcc 4.1 and later will do this automatically for static things at least...

Quoted 8 lines
Quoted 4 lines

Are you sure ?

At least with gcc-4.1.0 from SUSE 10.1 the strings array _pointers_ are not moved into the rodata section without the second "const":
const static char * const x[] = { "value1", "value2" };

hmm I could have sworn GCC does this automatic nowadays as long as it can prove you're not writing to the thing (eg static and not passing the pointer to some external function).....

Arjan seems right:
22:07 ichi:/dev/shm > cat test.c
#include <stdio.h>
static const char *x[] = {"0", "1", NULL};

int main(void) {
    int i;
    for(i = 0; i < 3; ++i)
        printf("%s\n", x[i]);
    return 0;
}

22:07 ichi:/dev/shm > cc test.c -c && nm test.o | grep x 00000000 d x
22:07 ichi:/dev/shm > cc test.c -c -O2 && nm test.o | grep x 00000000 r x

Quoted 2 lines

(even if gcc does this perfect I'm still in favor of the explicit const, just to catch stupid code with a warning)

Me² (read: ack)

	-`J'