bitops: introduce lock bitops
Subsystems (21)
PATCH 1/7 bitops: introduce lock bitops
Hi,
I'd like to get the new lock bitop primitives in if possible. Maybe it is too late for 2.6.23, but at least if I can queue them up in -mm?
They are pretty simple, but at least the page lock and buffer lock conversions touch quite a lot of code. Not sure how to really do this better to reduce merge difficulties.
---
Introduce test_and_set_bit_lock / clear_bit_unlock bitops with lock semantics. Convert all architectures to use the generic implementation.
--- Documentation/atomic_ops.txt | 14 ++++++++++++++ Documentation/memory-barriers.txt | 14 ++++++++++++-- include/asm-alpha/bitops.h | 1 + include/asm-arm/bitops.h | 1 + include/asm-arm26/bitops.h | 1 + include/asm-avr32/bitops.h | 1 + include/asm-blackfin/bitops.h | 1 + include/asm-cris/bitops.h | 1 + include/asm-frv/bitops.h | 1 + include/asm-generic/bitops.h | 1 + include/asm-generic/bitops/lock.h | 19 +++++++++++++++++++ include/asm-h8300/bitops.h | 1 + include/asm-i386/bitops.h | 1 + include/asm-ia64/bitops.h | 2 ++ include/asm-m32r/bitops.h | 1 + include/asm-m68k/bitops.h | 1 + include/asm-m68knommu/bitops.h | 1 + include/asm-mips/bitops.h | 1 + include/asm-parisc/bitops.h | 1 + include/asm-powerpc/bitops.h | 1 + include/asm-s390/bitops.h | 1 + include/asm-sh/bitops.h | 1 + include/asm-sh64/bitops.h | 1 + include/asm-sparc/bitops.h | 1 + include/asm-sparc64/bitops.h | 1 + include/asm-v850/bitops.h | 1 + include/asm-x86_64/bitops.h | 1 + include/asm-xtensa/bitops.h | 1 + 28 files changed, 71 insertions(+), 2 deletions(-)
include/asm-alpha/bitops.h +1 -0
@@ -367,6 +367,7 @@ static inline unsigned int hweight8(unsi#else #include <asm-generic/bitops/hweight.h> #endif +#include <asm-generic/bitops/lock.h> #endif /* __KERNEL__ */
include/asm-arm/bitops.h +1 -0
@@ -286,6 +286,7 @@ static inline int constant_fls(int x)#include <asm-generic/bitops/sched.h> #include <asm-generic/bitops/hweight.h> +#include <asm-generic/bitops/lock.h> /* * Ext2 is defined to use little-endian byte ordering.
include/asm-arm26/bitops.h +1 -0
@@ -167,6 +167,7 @@ extern int _find_next_bit_le(const unsig#include <asm-generic/bitops/ffs.h> #include <asm-generic/bitops/sched.h> #include <asm-generic/bitops/hweight.h> +#include <asm-generic/bitops/lock.h> /* * Ext2 is defined to use little-endian byte ordering.
include/asm-avr32/bitops.h +1 -0
@@ -288,6 +288,7 @@ static inline int ffs(unsigned long word#include <asm-generic/bitops/fls64.h> #include <asm-generic/bitops/sched.h> #include <asm-generic/bitops/hweight.h> +#include <asm-generic/bitops/lock.h> #include <asm-generic/bitops/ext2-non-atomic.h> #include <asm-generic/bitops/ext2-atomic.h>
include/asm-cris/bitops.h +1 -0
@@ -154,6 +154,7 @@ static inline int test_and_change_bit(in#include <asm-generic/bitops/fls64.h> #include <asm-generic/bitops/hweight.h> #include <asm-generic/bitops/find.h> +#include <asm-generic/bitops/lock.h> #include <asm-generic/bitops/ext2-non-atomic.h>
include/asm-frv/bitops.h +1 -0
@@ -302,6 +302,7 @@ int __ilog2_u64(u64 n)#include <asm-generic/bitops/sched.h> #include <asm-generic/bitops/hweight.h> +#include <asm-generic/bitops/lock.h> #include <asm-generic/bitops/ext2-non-atomic.h>
include/asm-generic/bitops.h +1 -0
@@ -22,6 +22,7 @@#include <asm-generic/bitops/sched.h> #include <asm-generic/bitops/ffs.h> #include <asm-generic/bitops/hweight.h> +#include <asm-generic/bitops/lock.h> #include <asm-generic/bitops/ext2-non-atomic.h> #include <asm-generic/bitops/ext2-atomic.h>
include/asm-generic/bitops/lock.h new file +19 -0
@@ -0,0 +1,19 @@ +#ifndef _ASM_GENERIC_BITOPS_LOCK_H_ +#define _ASM_GENERIC_BITOPS_LOCK_H_ + +#define test_and_set_bit_lock (nr, addr) test_and_set_bit(nr, addr) + +#define clear_bit_unlock (nr, addr) \ +do { \ + smp_mb__before_clear_bit(); \ + clear_bit(nr, addr); \ +} while (0) + +#define __clear_bit_unlock (nr, addr) \ +do { \ + smp_mb__before_clear_bit(); \ + __clear_bit(nr, addr); \ +} while (0) + +#endif /* _ASM_GENERIC_BITOPS_LOCK_H_ */ +
include/asm-h8300/bitops.h +1 -0
@@ -194,6 +194,7 @@ static __inline__ unsigned long __ffs(un#include <asm-generic/bitops/find.h> #include <asm-generic/bitops/sched.h> #include <asm-generic/bitops/hweight.h> +#include <asm-generic/bitops/lock.h> #include <asm-generic/bitops/ext2-non-atomic.h> #include <asm-generic/bitops/ext2-atomic.h> #include <asm-generic/bitops/minix.h>
include/asm-i386/bitops.h +1 -0
@@ -402,6 +402,7 @@ static inline int fls(int x) }#include <asm-generic/bitops/hweight.h> +#include <asm-generic/bitops/lock.h> #endif /* __KERNEL__ */
include/asm-m32r/bitops.h +1 -0
@@ -255,6 +255,7 @@ static __inline__ int test_and_change_bi#include <asm-generic/bitops/find.h> #include <asm-generic/bitops/ffs.h> #include <asm-generic/bitops/hweight.h> +#include <asm-generic/bitops/lock.h> #endif /* __KERNEL__ */
include/asm-m68k/bitops.h +1 -0
@@ -314,6 +314,7 @@ static inline int fls(int x)#include <asm-generic/bitops/fls64.h> #include <asm-generic/bitops/sched.h> #include <asm-generic/bitops/hweight.h> +#include <asm-generic/bitops/lock.h> /* Bitmap functions for the minix filesystem */
include/asm-m68knommu/bitops.h +1 -0
@@ -160,6 +160,7 @@ static __inline__ int __test_bit(int nr,#include <asm-generic/bitops/find.h> #include <asm-generic/bitops/hweight.h> +#include <asm-generic/bitops/lock.h> static __inline__int ext2_set_bit (int nr,volatile void * addr) {
include/asm-mips/bitops.h +1 -0
@@ -556,6 +556,7 @@ static inline int ffs(int word)#include <asm-generic/bitops/sched.h> #include <asm-generic/bitops/hweight.h> +#include <asm-generic/bitops/lock.h> #include <asm-generic/bitops/ext2-non-atomic.h> #include <asm-generic/bitops/ext2-atomic.h> #include <asm-generic/bitops/minix.h>
include/asm-parisc/bitops.h +1 -0
@@ -208,6 +208,7 @@ static __inline__ int fls(int x)#include <asm-generic/bitops/fls64.h> #include <asm-generic/bitops/hweight.h> +#include <asm-generic/bitops/lock.h> #include <asm-generic/bitops/sched.h> #endif /* __KERNEL__ */
include/asm-s390/bitops.h +1 -0
@@ -746,6 +746,7 @@ static inline int sched_find_first_bit(u#include <asm-generic/bitops/fls64.h> #include <asm-generic/bitops/hweight.h> +#include <asm-generic/bitops/lock.h> /* * ATTENTION: intel byte ordering convention for ext2 and minix !!
include/asm-sh/bitops.h +1 -0
@@ -137,6 +137,7 @@ static inline unsigned long __ffs(unsign#include <asm-generic/bitops/find.h> #include <asm-generic/bitops/ffs.h> #include <asm-generic/bitops/hweight.h> +#include <asm-generic/bitops/lock.h> #include <asm-generic/bitops/sched.h> #include <asm-generic/bitops/ext2-non-atomic.h> #include <asm-generic/bitops/ext2-atomic.h>
include/asm-sh64/bitops.h +1 -0
@@ -136,6 +136,7 @@ static __inline__ unsigned long ffz(unsi#include <asm-generic/bitops/__ffs.h> #include <asm-generic/bitops/find.h> #include <asm-generic/bitops/hweight.h> +#include <asm-generic/bitops/lock.h> #include <asm-generic/bitops/sched.h> #include <asm-generic/bitops/ffs.h> #include <asm-generic/bitops/ext2-non-atomic.h>
include/asm-sparc/bitops.h +1 -0
@@ -96,6 +96,7 @@ static inline void change_bit(unsigned l#include <asm-generic/bitops/fls.h> #include <asm-generic/bitops/fls64.h> #include <asm-generic/bitops/hweight.h> +#include <asm-generic/bitops/lock.h> #include <asm-generic/bitops/find.h> #include <asm-generic/bitops/ext2-non-atomic.h> #include <asm-generic/bitops/ext2-atomic.h>
include/asm-sparc64/bitops.h +1 -0
@@ -81,6 +81,7 @@ static inline unsigned int hweight8(unsi#include <asm-generic/bitops/hweight.h> #endif +#include <asm-generic/bitops/lock.h> #endif /* __KERNEL__ */ #include <asm-generic/bitops/find.h>
include/asm-v850/bitops.h +1 -0
@@ -145,6 +145,7 @@ static inline int __test_bit (int nr, co#include <asm-generic/bitops/find.h> #include <asm-generic/bitops/sched.h> #include <asm-generic/bitops/hweight.h> +#include <asm-generic/bitops/lock.h> #include <asm-generic/bitops/ext2-non-atomic.h> #define ext2_set_bit_atomic (l,n,a) test_and_set_bit(n,a)
include/asm-xtensa/bitops.h +1 -0
@@ -108,6 +108,7 @@ static inline int fls (unsigned int x)#endif #include <asm-generic/bitops/hweight.h> +#include <asm-generic/bitops/lock.h> #include <asm-generic/bitops/sched.h> #include <asm-generic/bitops/minix.h>
Documentation/atomic_ops.txt +14 -0
@@ -369,6 +369,20 @@ brothers: */ smp_mb__after_clear_bit(); +There are two special bitops with lock barrier semantics (acquire/release, +same as spinlocks). These operate in the same way as their non-_lock/unlock +postfixed variants, except that they are to provide acquire/release semantics, +respectively. This means they can be used for bit_spin_trylock and +bit_spin_unlock type operations without specifying any more barriers. + + int test_and_set_bit_lock(unsigned long nr, unsigned long *addr); + void clear_bit_unlock(unsigned long nr, unsigned long *addr); + void __clear_bit_unlock(unsigned long nr, unsigned long *addr); + +The __clear_bit_unlock version is non-atomic, however it still implements +unlock barrier semantics. This can be useful if the lock itself is protecting +the other bits in the word. + Finally, there are non-atomic versions of the bitmask operations provided. They are used in contexts where some other higher-level SMP locking scheme is being used to protect the bitmask, and thus less
Documentation/memory-barriers.txt +12 -2
@@ -1479,7 +1479,8 @@ kernel. Any atomic operation that modifies some state in memory and returns information about the state (old or new) implies an SMP-conditional general memory barrier -(smp_mb()) on each side of the actual operation. These include: +(smp_mb()) on each side of the actual operation (with the exception of +explicit lock operations, described later). These include: xchg(); cmpxchg();
@@ -1536,10 +1537,19 @@ If they're used for constructing a lock do need memory barriers as a lock primitive generally has to do things in a specific order. - Basically, each usage case has to be carefully considered as to whether memory barriers are needed or not. +The following operations are special locking primitives: + + test_and_set_bit_lock(); + clear_bit_unlock(); + __clear_bit_unlock(); + +These implement LOCK-class and UNLOCK-class operations. These should be used in +preference to other operations when implementing locking primitives, because +their implementations can be optimised on many architectures. + [!] Note that special memory barrier primitives are available for these situations because on some CPUs the atomic instructions used imply full memory barriers, and so barrier instructions are superfluous in conjunction with them,
include/asm-blackfin/bitops.h +1 -0
@@ -199,6 +199,7 @@ static __inline__ int __test_bit(int nr,#include <asm-generic/bitops/find.h> #include <asm-generic/bitops/hweight.h> +#include <asm-generic/bitops/lock.h> #include <asm-generic/bitops/ext2-atomic.h> #include <asm-generic/bitops/ext2-non-atomic.h>
include/asm-x86_64/bitops.h +1 -0
@@ -408,6 +408,7 @@ static __inline__ int fls(int x)#define ARCH_HAS_FAST_MULTIPLIER 1#include <asm-generic/bitops/hweight.h> +#include <asm-generic/bitops/lock.h> #endif /* __KERNEL__ */
include/asm-ia64/bitops.h +2 -0
@@ -371,6 +371,8 @@ hweight64 (unsigned long x)#define hweight16 (x) (unsigned int) hweight64((x) & 0xfffful)#define hweight8 (x) (unsigned int) hweight64((x) & 0xfful) +#include <asm-generic/bitops/lock.h> +#endif /* __KERNEL__ */ #include <asm-generic/bitops/find.h>
include/asm-powerpc/bitops.h +1 -0
@@ -266,6 +266,7 @@ static __inline__ int fls(unsigned int x#include <asm-generic/bitops/fls64.h> #include <asm-generic/bitops/hweight.h> +#include <asm-generic/bitops/lock.h> #define find_first_zero_bit (addr, size) find_next_zero_bit((addr), (size), 0) unsigned long find_next_zero_bit(const unsigned long *addr,
PATCH 2/7 tasklet_lock use lock bitops
Convert tasklet lock to new locking bitops.
--- include/linux/interrupt.h | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
include/linux/interrupt.h +2 -3
@@ -334,13 +334,12 @@ enum#ifdef CONFIG_SMP static inline int tasklet_trylock (struct tasklet_struct *t) { -return !test_and_set_bit (TASKLET_STATE_RUN , &(t)->state); +return !test_and_set_bit_lock (TASKLET_STATE_RUN , &(t)->state); }static inline void tasklet_unlock (struct tasklet_struct *t) { -smp_mb__before_clear_bit (); -clear_bit (TASKLET_STATE_RUN , &(t)->state); +clear_bit_unlock (TASKLET_STATE_RUN , &(t)->state); }static inline void tasklet_unlock_wait (struct tasklet_struct *t)
PATCH 3/7 wait_bit: use lock bitops
In reply to Nick PigginConvert __wait_on_bit_lock to new locking bitops. (OTOH, is it possible that some callers expect a full smp_mb() semantics? I don't think so, but maybe?)
--- kernel/wait.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/wait.c +1 -1
@@ -195,7 +195,7 @@ __wait_on_bit_lock(wait_queue_head_t *wqif ((ret = (*action)(q->key.flags)))break ; } - }while (test_and_set_bit (q->key.bit_nr, q->key.flags)); + }while (test_and_set_bit_lock (q->key.bit_nr, q->key.flags));finish_wait (wq, &q->wait);return ret; }
PATCH 4/7 bit_spinlock: use lock bitops
In reply to Nick PigginConvert bit_spinlock to new locking bitops.
--- include/linux/bit_spinlock.h | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-)
include/linux/bit_spinlock.h +22 -4
@@ -18,7 +18,7 @@ static inline void bit_spin_lock(int bit*/ preempt_disable ();#if defined(CONFIG_SMP ) || defined(CONFIG_DEBUG_SPINLOCK ) -while (test_and_set_bit (bitnum, addr)) { +while (unlikely (test_and_set_bit_lock (bitnum, addr))) {while (test_bit (bitnum, addr)) {preempt_enable ();cpu_relax ();
@@ -36,7 +36,7 @@ static inline int bit_spin_trylock(int b {preempt_disable ();#if defined(CONFIG_SMP ) || defined(CONFIG_DEBUG_SPINLOCK ) -if (test_and_set_bit (bitnum, addr)) { +if (unlikely (test_and_set_bit_lock (bitnum, addr))) {preempt_enable ();return 0 ; }
@@ -50,10 +50,28 @@ static inline int bit_spin_trylock(int b*/ static inline void bit_spin_unlock (int bitnum,unsigned long *addr) { +#ifdef CONFIG_DEBUG_SPINLOCK +BUG_ON (!test_bit (bitnum, addr)); +#endif #if defined(CONFIG_SMP ) || defined(CONFIG_DEBUG_SPINLOCK ) +clear_bit_unlock (bitnum, addr); +#endif +preempt_enable (); +__release (bitlock); +} + +/* +* bit-based spin_unlock() +* non-atomic version, which can be used eg. if the bit lock itself is +* protecting the rest of the flags in the word. +*/ +static inline void __bit_spin_unlock (int bitnum,unsigned long *addr) +{ +#ifdef CONFIG_DEBUG_SPINLOCK BUG_ON (!test_bit (bitnum, addr)); -smp_mb__before_clear_bit (); -clear_bit (bitnum, addr); +#endif +#if defined(CONFIG_SMP ) || defined(CONFIG_DEBUG_SPINLOCK ) +__clear_bit_unlock (bitnum, addr);#endif preempt_enable ();__release (bitlock);
PATCH 5/7 page lock: use lock bitops
In reply to Nick PigginConvert page lock to new locking bitops.
Requires a change of page flag operation naming, so we might as well convert it to something sane while we're here (!TestSetPageLocked_Lock => trylock_page).
--- drivers/scsi/sg.c | 2 +- fs/afs/write.c | 2 +- fs/cifs/file.c | 2 +- fs/jbd/commit.c | 2 +- fs/jbd2/commit.c | 2 +- fs/splice.c | 2 +- fs/xfs/linux-2.6/xfs_aops.c | 4 ++-- include/linux/page-flags.h | 6 ------ include/linux/pagemap.h | 9 +++++++-- mm/filemap.c | 17 +++++++---------- mm/memory.c | 2 +- mm/migrate.c | 4 ++-- mm/rmap.c | 2 +- mm/shmem.c | 4 ++-- mm/swap.c | 2 +- mm/swap_state.c | 2 +- mm/swapfile.c | 2 +- mm/truncate.c | 4 ++-- mm/vmscan.c | 4 ++-- 19 files changed, 35 insertions(+), 39 deletions(-)
include/linux/pagemap.h +7 -2
@@ -157,13 +157,18 @@ extern void FASTCALL(__lock_page(structextern void FASTCALL (__lock_page_nosync (struct page *page));extern void FASTCALL (unlock_page (struct page *page)); +static inline int trylock_page (struct page *page) +{ +return (likely (!test_and_set_bit_lock (PG_locked, &page->flags))); +} +/* * lock_page may only be called if we have the page's inode pinned. */ static inline void lock_page (struct page *page) {might_sleep (); -if (TestSetPageLocked (page)) +if (!trylock_page (page))__lock_page (page); }
@@ -174,7 +179,7 @@ static inline void lock_page(struct pagestatic inline void lock_page_nosync (struct page *page) {might_sleep (); -if (TestSetPageLocked (page)) +if (!trylock_page (page))__lock_page_nosync (page); }
drivers/scsi/sg.c +1 -1
@@ -1739,7 +1739,7 @@ st_map_user_pages(struct scatterlist *sg*/ flush_dcache_page (pages[i]);/* ?? Is locking needed? I don't think so */ -/* if (TestSetPageLocked(pages[i])) +/* if (!trylock_page(pages[i])) goto out_unlock; */ }
fs/cifs/file.c +1 -1
@@ -1205,7 +1205,7 @@ retry:if (first <0 )lock_page (page); -else if (TestSetPageLocked (page)) +else if (!trylock_page (page))break ;if (unlikely (page->mapping != mapping)) {
fs/jbd/commit.c +1 -1
@@ -63,7 +63,7 @@ static void release_buffer_page(struct b goto nope;/* OK, it's a truncated page */ -if (TestSetPageLocked (page)) +if (!trylock_page (page)) goto nope;page_cache_get (page);
fs/jbd2/commit.c +1 -1
@@ -63,7 +63,7 @@ static void release_buffer_page(struct b goto nope;/* OK, it's a truncated page */ -if (TestSetPageLocked (page)) +if (!trylock_page (page)) goto nope;page_cache_get (page);
fs/xfs/linux-2.6/xfs_aops.c +2 -2
@@ -664,7 +664,7 @@ xfs_probe_cluster( }else pg_offset =PAGE_CACHE_SIZE ; -if (page->index == tindex && !TestSetPageLocked (page)) { +if (page->index == tindex &&trylock_page (page)) { len =xfs_probe_page (page, pg_offset, mapped);unlock_page (page); }
@@ -748,7 +748,7 @@ xfs_convert_page(if (page->index != tindex) goto fail; -if (TestSetPageLocked (page)) +if (!trylock_page (page)) goto fail;if (PageWriteback (page)) goto fail_unlock_page;
include/linux/page-flags.h +0 -6
@@ -115,12 +115,6 @@ test_bit(PG_locked, &(page)->flags)#define SetPageLocked (page) \ set_bit(PG_locked, &(page)->flags) -#define TestSetPageLocked (page) \ - test_and_set_bit(PG_locked, &(page)->flags) -#define ClearPageLocked (page) \ - clear_bit(PG_locked, &(page)->flags) -#define TestClearPageLocked (page) \ - test_and_clear_bit(PG_locked, &(page)->flags)#define PageError (page) test_bit(PG_error, &(page)->flags)#define SetPageError (page) set_bit(PG_error, &(page)->flags)
mm/memory.c +1 -1
@@ -1650,7 +1650,7 @@ static int do_wp_page(struct mm_struct ** not dirty accountable. */ if (PageAnon (old_page)) { -if (!TestSetPageLocked (old_page)) { +if (trylock_page (old_page)) { reuse =can_share_swap_page (old_page);unlock_page (old_page); }
mm/migrate.c +2 -2
@@ -569,7 +569,7 @@ static int move_to_new_page(struct page* establishing additional references. We are the only one * holding a reference to the new page at this point. */ -if (TestSetPageLocked (newpage)) +if (!trylock_page (newpage))BUG ();/* Prepare mapping for the new page.*/
@@ -621,7 +621,7 @@ static int unmap_and_move(new_page_t get goto move_newpage; rc = -EAGAIN ; -if (TestSetPageLocked (page)) { +if (!trylock_page (page)) {if (!force) goto move_newpage;lock_page (page);
mm/rmap.c +1 -1
@@ -401,7 +401,7 @@ int page_referenced(struct page *page, i referenced +=page_referenced_anon (page); else if (is_locked) referenced += page_referenced_file(page); - else if (TestSetPageLocked(page)) + else if (!trylock_page(page)) referenced++; else { if (page->mapping)
mm/shmem.c +2 -2
@@ -1165,7 +1165,7 @@ repeat: }/* We have to do this with page locked to prevent races */ -if (TestSetPageLocked (swappage)) { +if (!trylock_page (swappage)) {shmem_swp_unmap (entry);spin_unlock (&info->lock);wait_on_page_locked (swappage);
@@ -1224,7 +1224,7 @@ repeat:shmem_swp_unmap (entry); filepage =find_get_page (mapping, idx);if (filepage && - (!PageUptodate (filepage) ||TestSetPageLocked (filepage))) { + (!PageUptodate (filepage) || !trylock_page (filepage))) {spin_unlock (&info->lock);wait_on_page_locked (filepage);page_cache_release (filepage);
mm/swap.c +1 -1
@@ -412,7 +412,7 @@ void pagevec_strip(struct pagevec *pvec)for (i =0 ; i <pagevec_count (pvec); i++) {struct page *page = pvec->pages[i]; -if (PagePrivate (page) && !TestSetPageLocked (page)) { +if (PagePrivate (page) &&trylock_page (page)) {if (PagePrivate (page))try_to_release_page (page,0 );unlock_page (page);
mm/swap_state.c +1 -1
@@ -252,7 +252,7 @@ int move_from_swap_cache(struct page *pa*/ static inline void free_swap_cache (struct page *page) { -if (PageSwapCache (page) && !TestSetPageLocked (page)) { +if (PageSwapCache (page) &&trylock_page (page)) {remove_exclusive_swap_page (page);unlock_page (page); }
mm/swapfile.c +1 -1
@@ -401,7 +401,7 @@ void free_swap_and_cache(swp_entry_t entif (p) {if (swap_entry_free (p,swp_offset (entry)) ==1 ) { page =find_get_page (&swapper_space, entry.val); -if (page &&unlikely (TestSetPageLocked (page))) { +if (page &&unlikely (!trylock_page (page))) {page_cache_release (page); page =NULL ; }
mm/truncate.c +2 -2
@@ -186,7 +186,7 @@ void truncate_inode_pages_range(struct aif (page_index > next) next = page_index; next++; -if (TestSetPageLocked (page)) +if (!trylock_page (page))continue ;if (PageWriteback (page)) {unlock_page (page);
@@ -279,7 +279,7 @@ unsigned long __invalidate_mapping_pagespgoff_t index;int lock_failed; - lock_failed =TestSetPageLocked (page); + lock_failed = !trylock_page (page);/* * We really shouldn't be looking at the ->index of an
mm/vmscan.c +2 -2
@@ -444,7 +444,7 @@ static unsigned long shrink_page_list(st page =lru_to_page (page_list);list_del (&page->lru); -if (TestSetPageLocked (page)) +if (!trylock_page (page)) goto keep;VM_BUG_ON (PageActive (page));
@@ -517,7 +517,7 @@ static unsigned long shrink_page_list(st* A synchronous write - probably a ramdisk. Go * ahead and try to reclaim the page. */ -if (TestSetPageLocked (page)) +if (!trylock_page (page)) goto keep;if (PageDirty (page) ||PageWriteback (page)) goto keep_locked;
mm/filemap.c +7 -10
@@ -526,17 +526,14 @@ EXPORT_SYMBOL(wait_on_page_bit);* mechananism between PageLocked pages and PageWriteback pages is shared. * But that's OK - sleepers in wait_on_page_writeback() just go back to sleep. * -* The first mb is necessary to safely close the critical section opened by the -* TestSetPageLocked(), the second mb is necessary to enforce ordering between -* the clear_bit and the read of the waitqueue (to avoid SMP races with a -* parallel wait_on_page_locked()). +* The mb is necessary to enforce ordering between the clear_bit and the read +* of the waitqueue (to avoid SMP races with a parallel wait_on_page_locked()). */ void fastcall unlock_page(struct page *page) { -smp_mb__before_clear_bit (); -if (!TestClearPageLocked (page)) -BUG (); -smp_mb__after_clear_bit (); +VM_BUG_ON (!PageLocked (page)); +clear_bit_unlock (PG_locked, &page->flags); +smp_mb__after_clear_bit ();wake_up_page (page, PG_locked); }EXPORT_SYMBOL (unlock_page);
@@ -626,7 +623,7 @@ repeat: page =radix_tree_lookup (&mapping->page_tree, offset);if (page) {page_cache_get (page); -if (TestSetPageLocked (page)) { +if (!trylock_page (page)) {read_unlock_irq (&mapping->tree_lock);__lock_page (page);read_lock_irq (&mapping->tree_lock);
@@ -802,7 +799,7 @@ grab_cache_page_nowait(struct address_spstruct page *page =find_get_page (mapping, index);if (page) { -if (!TestSetPageLocked (page)) +if (trylock_page (page))return page;page_cache_release (page);return NULL ;
fs/splice.c +1 -1
@@ -364,7 +364,7 @@ __generic_file_splice_read(struct file ** for an in-flight io page */ if (flags &SPLICE_F_NONBLOCK ) { -if (TestSetPageLocked (page)) +if (!trylock_page (page))break ; }else lock_page (page);
fs/afs/write.c +1 -1
@@ -404,7 +404,7 @@ static int afs_write_back_from_locked_pa page = pages[loop];if (page->index > wb->last)break ; -if (TestSetPageLocked (page)) +if (!trylock_page (page))break ;if (!PageDirty (page) ||page_private (page) != (unsigned long ) wb) {
PATCH 6/7 buffer lock: use lock bitops
In reply to Nick PigginConvert buffer lock to new locking bitops. Like the page lock change, this also requires name change, so convert the raw bitop to a trylock.
--- fs/buffer.c | 7 +++---- fs/jbd/commit.c | 2 +- fs/jbd2/commit.c | 2 +- fs/ntfs/aops.c | 2 +- fs/ntfs/compress.c | 2 +- fs/ntfs/mft.c | 4 ++-- fs/reiserfs/inode.c | 2 +- fs/reiserfs/journal.c | 4 ++-- include/linux/buffer_head.h | 8 ++++++-- 9 files changed, 18 insertions(+), 15 deletions(-)
fs/jbd/commit.c +1 -1
@@ -208,7 +208,7 @@ write_out_data:* blocking lock_buffer(). */ if (buffer_dirty (bh)) { -if (test_set_buffer_locked (bh)) { +if (!trylock_buffer (bh)) {BUFFER_TRACE (bh,"needs blocking lock" );spin_unlock (&journal->j_list_lock);/* Write out all data to prevent deadlocks */
fs/jbd2/commit.c +1 -1
@@ -208,7 +208,7 @@ write_out_data:* blocking lock_buffer(). */ if (buffer_dirty (bh)) { -if (test_set_buffer_locked (bh)) { +if (!trylock_buffer (bh)) {BUFFER_TRACE (bh,"needs blocking lock" );spin_unlock (&journal->j_list_lock);/* Write out all data to prevent deadlocks */
fs/buffer.c +3 -4
@@ -76,8 +76,7 @@ EXPORT_SYMBOL(__lock_buffer); void fastcall unlock_buffer(struct buffer_head *bh) { -smp_mb__before_clear_bit (); -clear_buffer_locked (bh); +clear_bit_unlock (BH_Lock, &bh->b_state);smp_mb__after_clear_bit ();wake_up_bit (&bh->b_state, BH_Lock); }
@@ -1673,7 +1672,7 @@ static int __block_write_full_page(struc*/ if (wbc->sync_mode !=WB_SYNC_NONE || !wbc->nonblocking) {lock_buffer (bh); - }else if (test_set_buffer_locked (bh)) { + }else if (!trylock_buffer (bh)) {redirty_page_for_writepage (wbc, page);continue ; }
@@ -2734,7 +2733,7 @@ void ll_rw_block(int rw, int nr, structif (rw ==SWRITE )lock_buffer (bh); -else if (test_set_buffer_locked (bh)) +else if (!trylock_buffer (bh))continue ;if (rw ==WRITE || rw ==SWRITE ) {
include/linux/buffer_head.h +6 -2
@@ -115,7 +115,6 @@ BUFFER_FNS(Uptodate, uptodate) BUFFER_FNS(Dirty, dirty) TAS_BUFFER_FNS(Dirty, dirty) BUFFER_FNS(Lock, locked) -TAS_BUFFER_FNS(Lock, locked) BUFFER_FNS(Req, req) TAS_BUFFER_FNS(Req, req)BUFFER_FNS (Mapped, mapped)
@@ -304,10 +303,15 @@ static inline void wait_on_buffer(struct__wait_on_buffer (bh); } +static inline int trylock_buffer (struct buffer_head *bh) +{ +return likely (!test_and_set_bit_lock (BH_Lock, &bh->b_state)); +} +static inline void lock_buffer (struct buffer_head *bh) {might_sleep (); -if (test_set_buffer_locked (bh)) +if (!trylock_buffer (bh))__lock_buffer (bh); }
fs/ntfs/aops.c +1 -1
@@ -1183,7 +1183,7 @@ lock_retry_remap: tbh = bhs[i];if (!tbh)continue ; -if (unlikely (test_set_buffer_locked (tbh))) +if (!trylock_buffer (tbh))BUG ();/* The buffer dirty state is now irrelevant, just clean it. */ clear_buffer_dirty (tbh);
fs/ntfs/compress.c +1 -1
@@ -655,7 +655,7 @@ lock_retry_remap:for (i =0 ; i < nr_bhs; i++) {struct buffer_head *tbh = bhs[i]; -if (unlikely (test_set_buffer_locked (tbh))) +if (!trylock_buffer (tbh))continue ;if (unlikely (buffer_uptodate (tbh))) {unlock_buffer (tbh);
fs/ntfs/mft.c +2 -2
@@ -586,7 +586,7 @@ int ntfs_sync_mft_mirror(ntfs_volume *vofor (i_bhs =0 ; i_bhs < nr_bhs; i_bhs++) {struct buffer_head *tbh = bhs[i_bhs]; -if (unlikely (test_set_buffer_locked (tbh))) +if (!trylock_buffer (tbh))BUG ();BUG_ON (!buffer_uptodate (tbh));clear_buffer_dirty (tbh);
@@ -779,7 +779,7 @@ int write_mft_record_nolock(ntfs_inode *for (i_bhs =0 ; i_bhs < nr_bhs; i_bhs++) {struct buffer_head *tbh = bhs[i_bhs]; -if (unlikely (test_set_buffer_locked (tbh))) +if (!trylock_buffer (tbh))BUG ();BUG_ON (!buffer_uptodate (tbh));clear_buffer_dirty (tbh);
fs/reiserfs/inode.c +1 -1
@@ -2440,7 +2440,7 @@ static int reiserfs_write_full_page(struif (wbc->sync_mode !=WB_SYNC_NONE || !wbc->nonblocking) {lock_buffer (bh); }else { -if (test_set_buffer_locked (bh)) { +if (!trylock_buffer (bh)) {redirty_page_for_writepage (wbc, page);continue ; }
fs/reiserfs/journal.c +2 -2
@@ -830,7 +830,7 @@ static int write_ordered_buffers(spinloc jh =JH_ENTRY (list->next); bh = jh->bh;get_bh (bh); -if (test_set_buffer_locked (bh)) { +if (!trylock_buffer (bh)) {if (!buffer_dirty (bh)) {list_move (&jh->list, &tmp); goto loop_next;
@@ -3838,7 +3838,7 @@ int reiserfs_prepare_for_journal(struct {PROC_INFO_INC (p_s_sb, journal.prepare); -if (test_set_buffer_locked (bh)) { +if (!trylock_buffer (bh)) {if (!wait)return 0 ;lock_buffer (bh);
PATCH 7/7 powerpc: optimised lock bitops
In reply to Nick PigginAdd powerpc optimised lock bitops.
--- include/asm-powerpc/bitops.h | 46 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-)
include/asm-powerpc/bitops.h +45 -1
@@ -86,6 +86,24 @@ static __inline__ void clear_bit(int nr, : "cc" ); } +static __inline__void clear_bit_unlock (int nr,volatile unsigned long *addr) +{ +unsigned long old; +unsigned long mask =BITOP_MASK (nr); +unsigned long *p = ((unsigned long *)addr) +BITOP_WORD (nr); + + __asm__ __volatile__( +LWSYNC_ON_SMP +"1:" PPC_LLARX "%0,0,%3 # clear_bit_unlock\n" +"andc %0,%0,%2\n" + PPC405_ERR77(0,%3) + PPC_STLCX "%0,0,%3\n" + "bne- 1b" + : "=&r" (old), "+m" (*p) + : "r" (mask), "r" (p) + : "cc", "memory"); +} +static __inline__void change_bit (int nr,volatile unsigned long *addr) {unsigned long old;
@@ -125,6 +143,27 @@ static __inline__ int test_and_set_bit(ureturn (old & mask) !=0 ; } +static __inline__int test_and_set_bit_lock (unsigned long nr, +volatile unsigned long *addr) +{ +unsigned long old, t; +unsigned long mask =BITOP_MASK (nr); +unsigned long *p = ((unsigned long *)addr) +BITOP_WORD (nr); + + __asm__ __volatile__( +"1:" PPC_LLARX "%0,0,%3 # test_and_set_bit_lock\n" +"or %1,%0,%2 \n" + PPC405_ERR77(0,%3) + PPC_STLCX "%1,0,%3 \n" + "bne- 1b" + ISYNC_ON_SMP + : "=&r" (old), "=&r" (t) + : "r" (mask), "r" (p) + : "cc", "memory"); + +return (old & mask) !=0 ; +} +static __inline__int test_and_clear_bit (unsigned long nr,volatile unsigned long *addr) {
@@ -185,6 +224,12 @@ static __inline__ void set_bits(unsigned#include <asm-generic/bitops/non-atomic.h> +static __inline__void __clear_bit_unlock (int nr,volatile unsigned long *addr) +{ + __asm__ __volatile__(LWSYNC_ON_SMP ::: "memory"); +__clear_bit (nr, addr); +} +/* * Return the zero-based bit position (LE, not IBM bit numbering) of * the most significant 1-bit in a double word.
@@ -266,7 +311,6 @@ static __inline__ int fls(unsigned int x#include <asm-generic/bitops/fls64.h> #include <asm-generic/bitops/hweight.h> -#include <asm-generic/bitops/lock.h> #define find_first_zero_bit (addr, size) find_next_zero_bit((addr), (size), 0) unsigned long find_next_zero_bit(const unsigned long *addr,
RFC PATCH 1/1 ia64: optimised lock bitops
In reply to Nick PigginJust for comments because I haven't hard anything from the ia64 guys about it.
--- include/asm-ia64/bitops.h | 43 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-)
include/asm-ia64/bitops.h +41 -2
@@ -94,6 +94,38 @@ clear_bit (int nr, volatile void *addr) }/** +* clear_bit_unlock - Clears a bit in memory with release +* @nr: Bit to clear +* @addr: Address to start counting from +* +* clear_bit_unlock() is atomic and may not be reordered. It does +* contain a memory barrier suitable for unlock type operations. +*/ +static __inline__void +clear_bit_unlock (int nr,volatile void *addr) +{ +__u32 mask, old, new; +volatile __u32 *m; +CMPXCHG_BUGCHECK_DECL + + m = (volatile __u32 *) addr + (nr >>5 ); + mask = ~(1 << (nr &31 )); +do { +CMPXCHG_BUGCHECK (m); + old = *m; + new = old & mask; + }while (cmpxchg_rel (m, old, new) != old); +} + +/** +* __clear_bit_unlock - Non-atomically clear a bit with release +* +* This is like clear_bit_unlock, but the implementation may use a non-atomic +* store (this one uses an atomic, however). +*/ +#define __clear_bit_unlock clear_bit_unlock + +/** * __clear_bit - Clears a bit in memory (non-atomic version) */ static __inline__ void
@@ -170,6 +202,15 @@ test_and_set_bit (int nr, volatile void }/** +* test_and_set_bit_lock - Set a bit and return its old value for lock +* @nr: Bit to set +* @addr: Address to count from +* +* This is the same as test_and_set_bit on ia64 +*/ +#define test_and_set_bit_lock test_and_set_bit + +/** * __test_and_set_bit - Set a bit and return its old value * @nr: Bit to set * @addr: Address to count from
@@ -371,8 +412,6 @@ hweight64 (unsigned long x)#define hweight16 (x) (unsigned int) hweight64((x) & 0xfffful)#define hweight8 (x) (unsigned int) hweight64((x) & 0xfful) -#include <asm-generic/bitops/lock.h> -#endif /* __KERNEL__ */ #include <asm-generic/bitops/find.h>
Quoted 6 lines · Nick Piggin
I'd like to get the new lock bitop primitives in if possible. Maybe it is too late for 2.6.23, but at least if I can queue them up in -mm?
They are pretty simple, but at least the page lock and buffer lock conversions touch quite a lot of code. Not sure how to really do this better to reduce merge difficulties.
I like the way we can get away with a memory barrier before calling clear_bit() and not have one after.
This works fine on FRV.
Now all you need to do is add the kerneldoc comments... Contingent on that:
Re: [patch 7/7] powerpc: optimised lock bitops
In reply to Nick PigginQuoted 3 lines · Nick Piggin
Add powerpc optimised lock bitops.
Signed-off-by Nick Piggin <[email protected]>
Quoted 86 lines
--- include/asm-powerpc/bitops.h | 46 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-)Index:
linux-2.6/include/asm-powerpc/bitops.h
===================================================================include/asm-powerpc/bitops.h +45
-2@@ -86,6 +86,24 @@ static __inline__ void clear_bit(int nr, : "cc" ); } +static __inline__void clear_bit_unlock (int nr,volatile unsigned long *addr) +{ +unsigned long old; +unsigned long mask =BITOP_MASK (nr); +unsigned long *p = ((unsigned long *)addr) +BITOP_WORD (nr); + + __asm__ __volatile__( +LWSYNC_ON_SMP +"1:" PPC_LLARX "%0,0,%3 # clear_bit_unlock\n" +"andc %0,%0,%2\n" + PPC405_ERR77(0,%3) + PPC_STLCX "%0,0,%3\n" + "bne- 1b" + : "=&r" (old), "+m" (*p) + : "r" (mask), "r" (p) + : "cc", "memory"); +} +static __inline__void change_bit (int nr,volatile unsigned long *addr) {unsigned long old;@@ -125,6 +143,27 @@ static __inline__ int test_and_set_bit(ureturn (old & mask) !=0 ; } +static __inline__int test_and_set_bit_lock (unsigned long nr, +volatile unsigned long *addr) +{ +unsigned long old, t; +unsigned long mask =BITOP_MASK (nr); +unsigned long *p = ((unsigned long *)addr) +BITOP_WORD (nr); + + __asm__ __volatile__( +"1:" PPC_LLARX "%0,0,%3 # test_and_set_bit_lock\n" +"or %1,%0,%2 \n" + PPC405_ERR77(0,%3) + PPC_STLCX "%1,0,%3 \n" + "bne- 1b" + ISYNC_ON_SMP + : "=&r" (old), "=&r" (t) + : "r" (mask), "r" (p) + : "cc", "memory"); + +return (old & mask) !=0 ; +} +static __inline__int test_and_clear_bit (unsigned long nr,volatile unsigned long *addr) {@@ -185,6 +224,12 @@ static __inline__ void set_bits(unsigned#include <asm-generic/bitops/non-atomic.h> +static __inline__void __clear_bit_unlock (int nr,volatile unsigned long *addr) +{ + __asm__ __volatile__(LWSYNC_ON_SMP ::: "memory"); +__clear_bit (nr, addr); +} +/* * Return the zero-based bit position (LE, not IBM bit numbering) of * the most significant 1-bit in a double word. @@ -266,7 +311,6 @@ static __inline__ int fls(unsigned int x#include <asm-generic/bitops/fls64.h> #include <asm-generic/bitops/hweight.h> -#include <asm-generic/bitops/lock.h> #define find_first_zero_bit (addr, size) find_next_zero_bit((addr), (size), 0) unsigned long find_next_zero_bit(const unsigned long *addr, -To unsubscribe from this list: send the line "unsubscribe linux-arch" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Quoted 9 lines · David Howells
Quoted 6 lines · Nick Piggin
I'd like to get the new lock bitop primitives in if possible. Maybe it is too late for 2.6.23, but at least if I can queue them up in -mm?
They are pretty simple, but at least the page lock and buffer lock conversions touch quite a lot of code. Not sure how to really do this better to reduce merge difficulties.
Quoted 8 lines
I like the way we can get away with a memory barrier before calling
clear_bit()and not have one after.This works fine on FRV.
Now all you need to do is add the kerneldoc comments... Contingent on that:
Acked-by David Howells <[email protected]>
Thanks, I can add a little something...
Andrew Morton
maintainerQuoted 5 lines · On Wed, 25 Jul 2007 13:34:07 +0200 Nick Piggin <[email protected]> wrote:
I'd like to get the new lock bitop primitives in if possible. Maybe it is too late for 2.6.23, but at least if I can queue them up in -mm?
They are pretty simple, but at least the page lock and buffer lock conversions touch quite a lot of code.
The changes look innocuous enough. I'd prefer not to carry this for two months though.
afacit all the operations which got changed were actually renamed, so any unconverted code will reliably fail to compile, yes? (If not, can we find a way to do this?). This means that a) if I _were_ to carry if for two months, any new code which gets added using the old operations will get reliably detected and b) there's not really much benefit in me carrying it all for two months.
Quoted 2 lines
Not sure how to really do this
better to reduce merge difficulties.
Resend around the -rc5/6 timeframe?
Quoted 8 lines · Andrew Morton
Quoted 5 lines · On Wed, 25 Jul 2007 13:34:07 +0200 Nick Piggin <[email protected]> wrote:
I'd like to get the new lock bitop primitives in if possible. Maybe it is too late for 2.6.23, but at least if I can queue them up in -mm?
They are pretty simple, but at least the page lock and buffer lock conversions touch quite a lot of code.
Quoted 6 lines
The changes look innocuous enough. I'd prefer not to carry this for two months though.
afacit all the operations which got changed were actually renamed, so any unconverted code will reliably fail to compile, yes? (If not, can we find a way to do this?).
Yes I think so. I'll double check.
The good thing is that none of the old operations are incompatible either, so just in case something did get through, it won't cause subtle synchronisation problems.
Quoted 4 lines
This means that a) if I _were_ to carry if for two
months, any new code which gets added using the old operations will get reliably detected and b) there's not really much benefit in me carrying it all for two months.
Good points.
Quoted 4 lines
Quoted 2 lines
Not sure how to really do this
better to reduce merge difficulties.Resend around the -rc5/6 timeframe?
OK.