[PATCH] removes filp_count_lock and changes nr_files type to atomic_t

From: Eric Dumazet (dada1_at_cosmosbay.com)
Date: 08/25/05

  • Next message: Denis Vlasenko: "Re: question on memory barrier"
    Date:	Thu, 25 Aug 2005 10:45:12 +0200
    To: Andrew Morton <akpm@osdl.org>, linux-kernel@vger.kernel.org
    
    
    

    This patch removes filp_count_lock spinlock, used to protect files_stat.nr_files.

    Just use atomic_t type and atomic_inc()/atomic_dec() operations.

    This patch assumes that atomic_read() is a plain {return v->counter;} on all
    architectures. (keywords : sysctl, /proc/sys/fs/file-nr, proc_dointvec)

    Signed-off-by: Eric Dumazet <dada1@cosmosbay.com>

    
    

    diff -Nru linux-2.6.13-rc7/fs/file_table.c linux-2.6.13-rc7-ed/fs/file_table.c
    --- linux-2.6.13-rc7/fs/file_table.c 2005-08-24 05:39:14.000000000 +0200
    +++ linux-2.6.13-rc7-ed/fs/file_table.c 2005-08-25 10:41:00.000000000 +0200
    @@ -28,29 +28,21 @@
     /* public. Not pretty! */
      __cacheline_aligned_in_smp DEFINE_SPINLOCK(files_lock);
     
    -static DEFINE_SPINLOCK(filp_count_lock);
     
     /* slab constructors and destructors are called from arbitrary
    - * context and must be fully threaded - use a local spinlock
    - * to protect files_stat.nr_files
    + * context and must be fully threaded
      */
     void filp_ctor(void * objp, struct kmem_cache_s *cachep, unsigned long cflags)
     {
             if ((cflags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
                 SLAB_CTOR_CONSTRUCTOR) {
    - unsigned long flags;
    - spin_lock_irqsave(&filp_count_lock, flags);
    - files_stat.nr_files++;
    - spin_unlock_irqrestore(&filp_count_lock, flags);
    + atomic_inc(&files_stat.nr_files);
             }
     }
     
     void filp_dtor(void * objp, struct kmem_cache_s *cachep, unsigned long dflags)
     {
    - unsigned long flags;
    - spin_lock_irqsave(&filp_count_lock, flags);
    - files_stat.nr_files--;
    - spin_unlock_irqrestore(&filp_count_lock, flags);
    + atomic_dec(&files_stat.nr_files);
     }
     
     static inline void file_free(struct file *f)
    @@ -70,7 +62,7 @@
             /*
              * Privileged users can go above max_files
              */
    - if (files_stat.nr_files >= files_stat.max_files &&
    + if (atomic_read(&files_stat.nr_files) >= files_stat.max_files &&
                                     !capable(CAP_SYS_ADMIN))
                     goto over;
     
    @@ -94,10 +86,10 @@
     
     over:
             /* Ran out of filps - report that */
    - if (files_stat.nr_files > old_max) {
    + if (atomic_read(&files_stat.nr_files) > old_max) {
                     printk(KERN_INFO "VFS: file-max limit %d reached\n",
                                             files_stat.max_files);
    - old_max = files_stat.nr_files;
    + old_max = atomic_read(&files_stat.nr_files);
             }
             goto fail;
     
    diff -Nru linux-2.6.13-rc7/fs/xfs/linux-2.6/xfs_linux.h linux-2.6.13-rc7-ed/fs/xfs/linux-2.6/xfs_linux.h
    --- linux-2.6.13-rc7/fs/xfs/linux-2.6/xfs_linux.h 2005-08-24 05:39:14.000000000 +0200
    +++ linux-2.6.13-rc7-ed/fs/xfs/linux-2.6/xfs_linux.h 2005-08-25 10:41:00.000000000 +0200
    @@ -242,7 +242,7 @@
     
     /* IRIX uses the current size of the name cache to guess a good value */
     /* - this isn't the same but is a good enough starting point for now. */
    -#define DQUOT_HASH_HEURISTIC files_stat.nr_files
    +#define DQUOT_HASH_HEURISTIC atomic_read(&files_stat.nr_files)
     
     /* IRIX inodes maintain the project ID also, zero this field on Linux */
     #define DEFAULT_PROJID 0
    diff -Nru linux-2.6.13-rc7/include/linux/fs.h linux-2.6.13-rc7-ed/include/linux/fs.h
    --- linux-2.6.13-rc7/include/linux/fs.h 2005-08-24 05:39:14.000000000 +0200
    +++ linux-2.6.13-rc7-ed/include/linux/fs.h 2005-08-25 10:41:00.000000000 +0200
    @@ -30,7 +30,7 @@
     
     /* And dynamically-tunable limits and defaults: */
     struct files_stat_struct {
    - int nr_files; /* read only */
    + atomic_t nr_files; /* read only */
             int nr_free_files; /* read only */
             int max_files; /* tunable */
     };

    -
    To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
    the body of a message to majordomo@vger.kernel.org
    More majordomo info at http://vger.kernel.org/majordomo-info.html
    Please read the FAQ at http://www.tux.org/lkml/


  • Next message: Denis Vlasenko: "Re: question on memory barrier"

    Relevant Pages

    • Re: [PATCH] cowlinks v2
      ... >> spinlock. ... Now you're using i_lock to protect i_flags ... you'll need to hnt down all the other i_flags users and make them use ... send the line "unsubscribe linux-kernel" in ...
      (Linux-Kernel)
    • Re: [PATCH 2/2] dmaengine i.MX SDMA: protect channel0
      ... other channels, thus we have to protect it with a mutex. ... Currently it is only called from non interrupt context. ... so a spinlock might be a better weapon here. ...
      (Linux-Kernel)
    • Re: LinuxPPS & spinlocks
      ... does nothing (if we are already in an interrupt handler). ... context, but doing like this I will disable interrupts ... protect syscalls from each others and from pps_register/unregister, ... Note that *data* is protected by a lock, ...
      (Linux-Kernel)
    • Re: Generic Repository ... How can I do this?
      ... protected readonly AppEntities context; ... protect against the derived classes from messing with the variable; you may actually want to include that in your own Repositoryclass simply because it's good form to put "readonly" on member fields that you know should never change after object construction. ... You can leave that as "private" in your own version, assuming you have no plans to inherit the Repositoryclass in a way that would require it. ...
      (microsoft.public.dotnet.languages.csharp)
    • [BUG 2.6.36-rc6] list corruption in module_bug_finalize
      ... Current mainline triggers a list corruption bug in ... Strictly speaking this should have a spinlock to protect against ... 8139too Fast Ethernet driver 0.9.28 ...
      (Linux-Kernel)