Skip to content

Commit 892ad5a

Browse files
committed
Merge branch 'locking-core-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
Pull locking updates from Ingo Molnar: "The main changes in this cycle were: - Add CONFIG_REFCOUNT_FULL=y to allow the disabling of the 'full' (robustness checked) refcount_t implementation with slightly lower runtime overhead. (Kees Cook) The lighter weight variant is the default. The two variants use the same API. Having this variant was a precondition by some maintainers to merge refcount_t cleanups. - Add lockdep support for rtmutexes (Peter Zijlstra) - liblockdep fixes and improvements (Sasha Levin, Ben Hutchings) - ... misc fixes and improvements" * 'locking-core-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip: (30 commits) locking/refcount: Remove the half-implemented refcount_sub() API locking/refcount: Create unchecked atomic_t implementation locking/rtmutex: Don't initialize lockdep when not required locking/selftest: Add RT-mutex support locking/selftest: Remove the bad unlock ordering test rt_mutex: Add lockdep annotations MAINTAINERS: Claim atomic*_t maintainership locking/x86: Remove the unused atomic_inc_short() methd tools/lib/lockdep: Remove private kernel headers tools/lib/lockdep: Hide liblockdep output from test results tools/lib/lockdep: Add dummy current_gfp_context() tools/include: Add IS_ERR_OR_NULL to err.h tools/lib/lockdep: Add empty __is_[module,kernel]_percpu_address tools/lib/lockdep: Include err.h tools/include: Add (mostly) empty include/linux/sched/mm.h tools/lib/lockdep: Use LDFLAGS tools/lib/lockdep: Remove double-quotes from soname tools/lib/lockdep: Fix object file paths used in an out-of-tree build tools/lib/lockdep: Fix compilation for 4.11 tools/lib/lockdep: Don't mix fd-based and stream IO ...
2 parents 162b246 + 5d6dec6 commit 892ad5a

72 files changed

Lines changed: 641 additions & 234 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎MAINTAINERS‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2322,6 +2322,15 @@ F: Documentation/devicetree/bindings/input/atmel,maxtouch.txt
23222322
F: drivers/input/touchscreen/atmel_mxt_ts.c
23232323
F: include/linux/platform_data/atmel_mxt_ts.h
23242324

2325+
ATOMIC INFRASTRUCTURE
2326+
M: Will Deacon <will.deacon@arm.com>
2327+
M: Peter Zijlstra <peterz@infradead.org>
2328+
R: Boqun Feng <boqun.feng@gmail.com>
2329+
L: linux-kernel@vger.kernel.org
2330+
S: Maintained
2331+
F: arch/*/include/asm/atomic*.h
2332+
F: include/*/atomic*.h
2333+
23252334
ATTO EXPRESSSAS SAS/SATA RAID SCSI DRIVER
23262335
M: Bradley Grove <linuxdrivers@attotech.com>
23272336
L: linux-scsi@vger.kernel.org
@@ -7555,7 +7564,7 @@ S: Maintained
75557564
F: drivers/ata/sata_promise.*
75567565

75577566
LIBLOCKDEP
7558-
M: Sasha Levin <sasha.levin@oracle.com>
7567+
M: Sasha Levin <alexander.levin@verizon.com>
75597568
S: Maintained
75607569
F: tools/lib/lockdep/
75617570

‎arch/Kconfig‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,4 +867,13 @@ config STRICT_MODULE_RWX
867867
config ARCH_WANT_RELAX_ORDER
868868
bool
869869

870+
config REFCOUNT_FULL
871+
bool "Perform full reference count validation at the expense of speed"
872+
help
873+
Enabling this switches the refcounting infrastructure from a fast
874+
unchecked atomic_t implementation to a fully state checked
875+
implementation, which can be (slightly) slower but provides protections
876+
against various use-after-free conditions that can be used in
877+
security flaw exploits.
878+
870879
source "kernel/gcov/Kconfig"

‎arch/tile/lib/atomic_asm_32.S‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424
* has an opportunity to return -EFAULT to the user if needed.
2525
* The 64-bit routines just return a "long long" with the value,
2626
* since they are only used from kernel space and don't expect to fault.
27-
* Support for 16-bit ops is included in the framework but we don't provide
28-
* any (x86_64 has an atomic_inc_short(), so we might want to some day).
27+
* Support for 16-bit ops is included in the framework but we don't provide any.
2928
*
3029
* Note that the caller is advised to issue a suitable L1 or L2
3130
* prefetch on the address being manipulated to avoid extra stalls.

‎arch/x86/include/asm/atomic.h‎

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -246,19 +246,6 @@ static __always_inline int __atomic_add_unless(atomic_t *v, int a, int u)
246246
return c;
247247
}
248248

249-
/**
250-
* atomic_inc_short - increment of a short integer
251-
* @v: pointer to type int
252-
*
253-
* Atomically adds 1 to @v
254-
* Returns the new value of @u
255-
*/
256-
static __always_inline short int atomic_inc_short(short int *v)
257-
{
258-
asm(LOCK_PREFIX "addw $1, %0" : "+m" (*v));
259-
return *v;
260-
}
261-
262249
#ifdef CONFIG_X86_32
263250
# include <asm/atomic64_32.h>
264251
#else

‎include/linux/refcount.h‎

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,53 @@ static inline unsigned int refcount_read(const refcount_t *r)
4141
return atomic_read(&r->refs);
4242
}
4343

44+
#ifdef CONFIG_REFCOUNT_FULL
4445
extern __must_check bool refcount_add_not_zero(unsigned int i, refcount_t *r);
4546
extern void refcount_add(unsigned int i, refcount_t *r);
4647

4748
extern __must_check bool refcount_inc_not_zero(refcount_t *r);
4849
extern void refcount_inc(refcount_t *r);
4950

5051
extern __must_check bool refcount_sub_and_test(unsigned int i, refcount_t *r);
51-
extern void refcount_sub(unsigned int i, refcount_t *r);
5252

5353
extern __must_check bool refcount_dec_and_test(refcount_t *r);
5454
extern void refcount_dec(refcount_t *r);
55+
#else
56+
static inline __must_check bool refcount_add_not_zero(unsigned int i, refcount_t *r)
57+
{
58+
return atomic_add_unless(&r->refs, i, 0);
59+
}
60+
61+
static inline void refcount_add(unsigned int i, refcount_t *r)
62+
{
63+
atomic_add(i, &r->refs);
64+
}
65+
66+
static inline __must_check bool refcount_inc_not_zero(refcount_t *r)
67+
{
68+
return atomic_add_unless(&r->refs, 1, 0);
69+
}
70+
71+
static inline void refcount_inc(refcount_t *r)
72+
{
73+
atomic_inc(&r->refs);
74+
}
75+
76+
static inline __must_check bool refcount_sub_and_test(unsigned int i, refcount_t *r)
77+
{
78+
return atomic_sub_and_test(i, &r->refs);
79+
}
80+
81+
static inline __must_check bool refcount_dec_and_test(refcount_t *r)
82+
{
83+
return atomic_dec_and_test(&r->refs);
84+
}
85+
86+
static inline void refcount_dec(refcount_t *r)
87+
{
88+
atomic_dec(&r->refs);
89+
}
90+
#endif /* CONFIG_REFCOUNT_FULL */
5591

5692
extern __must_check bool refcount_dec_if_one(refcount_t *r);
5793
extern __must_check bool refcount_dec_not_one(refcount_t *r);

‎include/linux/rtmutex.h‎

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ struct rt_mutex {
3737
int line;
3838
void *magic;
3939
#endif
40+
#ifdef CONFIG_DEBUG_LOCK_ALLOC
41+
struct lockdep_map dep_map;
42+
#endif
4043
};
4144

4245
struct rt_mutex_waiter;
@@ -58,19 +61,33 @@ struct hrtimer_sleeper;
5861
#ifdef CONFIG_DEBUG_RT_MUTEXES
5962
# define __DEBUG_RT_MUTEX_INITIALIZER(mutexname) \
6063
, .name = #mutexname, .file = __FILE__, .line = __LINE__
61-
# define rt_mutex_init(mutex) __rt_mutex_init(mutex, __func__)
64+
65+
# define rt_mutex_init(mutex) \
66+
do { \
67+
static struct lock_class_key __key; \
68+
__rt_mutex_init(mutex, __func__, &__key); \
69+
} while (0)
70+
6271
extern void rt_mutex_debug_task_free(struct task_struct *tsk);
6372
#else
6473
# define __DEBUG_RT_MUTEX_INITIALIZER(mutexname)
65-
# define rt_mutex_init(mutex) __rt_mutex_init(mutex, NULL)
74+
# define rt_mutex_init(mutex) __rt_mutex_init(mutex, NULL, NULL)
6675
# define rt_mutex_debug_task_free(t) do { } while (0)
6776
#endif
6877

78+
#ifdef CONFIG_DEBUG_LOCK_ALLOC
79+
#define __DEP_MAP_RT_MUTEX_INITIALIZER(mutexname) \
80+
, .dep_map = { .name = #mutexname }
81+
#else
82+
#define __DEP_MAP_RT_MUTEX_INITIALIZER(mutexname)
83+
#endif
84+
6985
#define __RT_MUTEX_INITIALIZER(mutexname) \
7086
{ .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(mutexname.wait_lock) \
7187
, .waiters = RB_ROOT \
7288
, .owner = NULL \
73-
__DEBUG_RT_MUTEX_INITIALIZER(mutexname)}
89+
__DEBUG_RT_MUTEX_INITIALIZER(mutexname) \
90+
__DEP_MAP_RT_MUTEX_INITIALIZER(mutexname)}
7491

7592
#define DEFINE_RT_MUTEX(mutexname) \
7693
struct rt_mutex mutexname = __RT_MUTEX_INITIALIZER(mutexname)
@@ -86,7 +103,7 @@ static inline int rt_mutex_is_locked(struct rt_mutex *lock)
86103
return lock->owner != NULL;
87104
}
88105

89-
extern void __rt_mutex_init(struct rt_mutex *lock, const char *name);
106+
extern void __rt_mutex_init(struct rt_mutex *lock, const char *name, struct lock_class_key *key);
90107
extern void rt_mutex_destroy(struct rt_mutex *lock);
91108

92109
extern void rt_mutex_lock(struct rt_mutex *lock);

‎kernel/locking/rtmutex-debug.c‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,16 @@ void debug_rt_mutex_free_waiter(struct rt_mutex_waiter *waiter)
166166
memset(waiter, 0x22, sizeof(*waiter));
167167
}
168168

169-
void debug_rt_mutex_init(struct rt_mutex *lock, const char *name)
169+
void debug_rt_mutex_init(struct rt_mutex *lock, const char *name, struct lock_class_key *key)
170170
{
171171
/*
172172
* Make sure we are not reinitializing a held lock:
173173
*/
174174
debug_check_no_locks_freed((void *)lock, sizeof(*lock));
175175
lock->name = name;
176+
177+
#ifdef CONFIG_DEBUG_LOCK_ALLOC
178+
lockdep_init_map(&lock->dep_map, name, key, 0);
179+
#endif
176180
}
177181

‎kernel/locking/rtmutex-debug.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
extern void debug_rt_mutex_init_waiter(struct rt_mutex_waiter *waiter);
1313
extern void debug_rt_mutex_free_waiter(struct rt_mutex_waiter *waiter);
14-
extern void debug_rt_mutex_init(struct rt_mutex *lock, const char *name);
14+
extern void debug_rt_mutex_init(struct rt_mutex *lock, const char *name, struct lock_class_key *key);
1515
extern void debug_rt_mutex_lock(struct rt_mutex *lock);
1616
extern void debug_rt_mutex_unlock(struct rt_mutex *lock);
1717
extern void debug_rt_mutex_proxy_lock(struct rt_mutex *lock,

‎kernel/locking/rtmutex.c‎

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1481,6 +1481,7 @@ void __sched rt_mutex_lock(struct rt_mutex *lock)
14811481
{
14821482
might_sleep();
14831483

1484+
mutex_acquire(&lock->dep_map, 0, 0, _RET_IP_);
14841485
rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, rt_mutex_slowlock);
14851486
}
14861487
EXPORT_SYMBOL_GPL(rt_mutex_lock);
@@ -1496,9 +1497,16 @@ EXPORT_SYMBOL_GPL(rt_mutex_lock);
14961497
*/
14971498
int __sched rt_mutex_lock_interruptible(struct rt_mutex *lock)
14981499
{
1500+
int ret;
1501+
14991502
might_sleep();
15001503

1501-
return rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE, rt_mutex_slowlock);
1504+
mutex_acquire(&lock->dep_map, 0, 0, _RET_IP_);
1505+
ret = rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE, rt_mutex_slowlock);
1506+
if (ret)
1507+
mutex_release(&lock->dep_map, 1, _RET_IP_);
1508+
1509+
return ret;
15021510
}
15031511
EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible);
15041512

@@ -1526,11 +1534,18 @@ int __sched rt_mutex_futex_trylock(struct rt_mutex *lock)
15261534
int
15271535
rt_mutex_timed_lock(struct rt_mutex *lock, struct hrtimer_sleeper *timeout)
15281536
{
1537+
int ret;
1538+
15291539
might_sleep();
15301540

1531-
return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
1541+
mutex_acquire(&lock->dep_map, 0, 0, _RET_IP_);
1542+
ret = rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
15321543
RT_MUTEX_MIN_CHAINWALK,
15331544
rt_mutex_slowlock);
1545+
if (ret)
1546+
mutex_release(&lock->dep_map, 1, _RET_IP_);
1547+
1548+
return ret;
15341549
}
15351550
EXPORT_SYMBOL_GPL(rt_mutex_timed_lock);
15361551

@@ -1547,10 +1562,16 @@ EXPORT_SYMBOL_GPL(rt_mutex_timed_lock);
15471562
*/
15481563
int __sched rt_mutex_trylock(struct rt_mutex *lock)
15491564
{
1565+
int ret;
1566+
15501567
if (WARN_ON_ONCE(in_irq() || in_nmi() || in_serving_softirq()))
15511568
return 0;
15521569

1553-
return rt_mutex_fasttrylock(lock, rt_mutex_slowtrylock);
1570+
ret = rt_mutex_fasttrylock(lock, rt_mutex_slowtrylock);
1571+
if (ret)
1572+
mutex_acquire(&lock->dep_map, 0, 1, _RET_IP_);
1573+
1574+
return ret;
15541575
}
15551576
EXPORT_SYMBOL_GPL(rt_mutex_trylock);
15561577

@@ -1561,6 +1582,7 @@ EXPORT_SYMBOL_GPL(rt_mutex_trylock);
15611582
*/
15621583
void __sched rt_mutex_unlock(struct rt_mutex *lock)
15631584
{
1585+
mutex_release(&lock->dep_map, 1, _RET_IP_);
15641586
rt_mutex_fastunlock(lock, rt_mutex_slowunlock);
15651587
}
15661588
EXPORT_SYMBOL_GPL(rt_mutex_unlock);
@@ -1620,7 +1642,6 @@ void rt_mutex_destroy(struct rt_mutex *lock)
16201642
lock->magic = NULL;
16211643
#endif
16221644
}
1623-
16241645
EXPORT_SYMBOL_GPL(rt_mutex_destroy);
16251646

16261647
/**
@@ -1632,14 +1653,16 @@ EXPORT_SYMBOL_GPL(rt_mutex_destroy);
16321653
*
16331654
* Initializing of a locked rt lock is not allowed
16341655
*/
1635-
void __rt_mutex_init(struct rt_mutex *lock, const char *name)
1656+
void __rt_mutex_init(struct rt_mutex *lock, const char *name,
1657+
struct lock_class_key *key)
16361658
{
16371659
lock->owner = NULL;
16381660
raw_spin_lock_init(&lock->wait_lock);
16391661
lock->waiters = RB_ROOT;
16401662
lock->waiters_leftmost = NULL;
16411663

1642-
debug_rt_mutex_init(lock, name);
1664+
if (name && key)
1665+
debug_rt_mutex_init(lock, name, key);
16431666
}
16441667
EXPORT_SYMBOL_GPL(__rt_mutex_init);
16451668

@@ -1660,7 +1683,7 @@ EXPORT_SYMBOL_GPL(__rt_mutex_init);
16601683
void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
16611684
struct task_struct *proxy_owner)
16621685
{
1663-
__rt_mutex_init(lock, NULL);
1686+
__rt_mutex_init(lock, NULL, NULL);
16641687
debug_rt_mutex_proxy_lock(lock, proxy_owner);
16651688
rt_mutex_set_owner(lock, proxy_owner);
16661689
}

‎kernel/locking/rtmutex.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#define debug_rt_mutex_proxy_lock(l,p) do { } while (0)
1818
#define debug_rt_mutex_proxy_unlock(l) do { } while (0)
1919
#define debug_rt_mutex_unlock(l) do { } while (0)
20-
#define debug_rt_mutex_init(m, n) do { } while (0)
20+
#define debug_rt_mutex_init(m, n, k) do { } while (0)
2121
#define debug_rt_mutex_deadlock(d, a ,l) do { } while (0)
2222
#define debug_rt_mutex_print_deadlock(w) do { } while (0)
2323
#define debug_rt_mutex_reset_waiter(w) do { } while (0)

0 commit comments

Comments
 (0)