Re: [PATCH 1/2] mm/vmscan: add sync_shrinkers function
On Thu, Apr 15, 2021 at 01:56:23PM +0200, Christian König wrote: > To be able to switch to a spinlock and reduce lock contention in the TTM > shrinker we don't want to hold a mutex while unmapping and freeing pages > from the pool. > > But then we somehow need to prevent a race between (for example) the shrinker > trying to free pages and hotplug trying to remove the device which those pages > belong to. > > Taking and releasing the shrinker semaphore on the write side after > unmapping and freeing all pages should make sure that no shrinker is running > in > paralell any more. > > Signed-off-by: Christian König > --- > include/linux/shrinker.h | 1 + > mm/vmscan.c | 10 ++ > 2 files changed, 11 insertions(+) > > diff --git a/include/linux/shrinker.h b/include/linux/shrinker.h > index 0f80123650e2..6b75dc372fce 100644 > --- a/include/linux/shrinker.h > +++ b/include/linux/shrinker.h > @@ -92,4 +92,5 @@ extern void register_shrinker_prepared(struct shrinker > *shrinker); > extern int register_shrinker(struct shrinker *shrinker); > extern void unregister_shrinker(struct shrinker *shrinker); > extern void free_prealloced_shrinker(struct shrinker *shrinker); > +extern void sync_shrinkers(void); > #endif > diff --git a/mm/vmscan.c b/mm/vmscan.c > index 562e87cbd7a1..46cd9c215d73 100644 > --- a/mm/vmscan.c > +++ b/mm/vmscan.c > @@ -408,6 +408,16 @@ void unregister_shrinker(struct shrinker *shrinker) > } > EXPORT_SYMBOL(unregister_shrinker); > > +/** > + * sync_shrinker - Wait for all running shrinkers to complete. Maybe make it clear this is a barrier type thing, it wont stop shrinkers at all, just synchronize with them. Acked-by: Daniel Vetter But needs an ack from Andrew for merging through drm-misc-next before we push it there. -Daniel > + */ > +void sync_shrinkers(void) > +{ > + down_write(&shrinker_rwsem); > + up_write(&shrinker_rwsem); > +} > +EXPORT_SYMBOL(sync_shrinkers); > + > #define SHRINK_BATCH 128 > > static unsigned long do_shrink_slab(struct shrink_control *shrinkctl, > -- > 2.25.1 > -- Daniel Vetter Software Engineer, Intel Corporation http://blog.ffwll.ch
[PATCH 1/2] mm/vmscan: add sync_shrinkers function
To be able to switch to a spinlock and reduce lock contention in the TTM shrinker we don't want to hold a mutex while unmapping and freeing pages from the pool. But then we somehow need to prevent a race between (for example) the shrinker trying to free pages and hotplug trying to remove the device which those pages belong to. Taking and releasing the shrinker semaphore on the write side after unmapping and freeing all pages should make sure that no shrinker is running in paralell any more. Signed-off-by: Christian König --- include/linux/shrinker.h | 1 + mm/vmscan.c | 10 ++ 2 files changed, 11 insertions(+) diff --git a/include/linux/shrinker.h b/include/linux/shrinker.h index 0f80123650e2..6b75dc372fce 100644 --- a/include/linux/shrinker.h +++ b/include/linux/shrinker.h @@ -92,4 +92,5 @@ extern void register_shrinker_prepared(struct shrinker *shrinker); extern int register_shrinker(struct shrinker *shrinker); extern void unregister_shrinker(struct shrinker *shrinker); extern void free_prealloced_shrinker(struct shrinker *shrinker); +extern void sync_shrinkers(void); #endif diff --git a/mm/vmscan.c b/mm/vmscan.c index 562e87cbd7a1..46cd9c215d73 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -408,6 +408,16 @@ void unregister_shrinker(struct shrinker *shrinker) } EXPORT_SYMBOL(unregister_shrinker); +/** + * sync_shrinker - Wait for all running shrinkers to complete. + */ +void sync_shrinkers(void) +{ + down_write(&shrinker_rwsem); + up_write(&shrinker_rwsem); +} +EXPORT_SYMBOL(sync_shrinkers); + #define SHRINK_BATCH 128 static unsigned long do_shrink_slab(struct shrink_control *shrinkctl, -- 2.25.1
Re: [PATCH 1/2] mm/vmscan: add sync_shrinkers function
Am 09.04.21 um 13:00 schrieb Vlastimil Babka: On 4/9/21 9:17 AM, Christian König wrote: To be able to switch to a spinlock and reduce lock contention in the TTM shrinker we don't want to hold a mutex while unmapping and freeing pages from the pool. Does using spinlock instead of mutex really reduce lock contention? Well using the spinlock instead of the mutex is only the cherry on the cake. The real improvement for the contention is the fact that we just grab the next pool and drop the lock again instead of doing the whole IOMMU unmap and flushing of the CPU TLB dance while holding the lock. But then we somehow need to prevent a race between (for example) the shrinker trying to free pages and hotplug trying to remove the device which those pages belong to. Taking and releasing the shrinker semaphore on the write side after unmapping and freeing all pages should make sure that no shrinker is running in paralell any more. So you explain this in this commit log for adding the function, but then the next patch just adds a sync_shrinkers() call without any comment. I would expect there a comment explaining why it's done there - what it protects against, as it's not an obvious pattern IMHO. Good point, going to add a comment. Thanks, Christian. Signed-off-by: Christian König --- include/linux/shrinker.h | 1 + mm/vmscan.c | 10 ++ 2 files changed, 11 insertions(+) diff --git a/include/linux/shrinker.h b/include/linux/shrinker.h index 0f80123650e2..6b75dc372fce 100644 --- a/include/linux/shrinker.h +++ b/include/linux/shrinker.h @@ -92,4 +92,5 @@ extern void register_shrinker_prepared(struct shrinker *shrinker); extern int register_shrinker(struct shrinker *shrinker); extern void unregister_shrinker(struct shrinker *shrinker); extern void free_prealloced_shrinker(struct shrinker *shrinker); +extern void sync_shrinkers(void); #endif diff --git a/mm/vmscan.c b/mm/vmscan.c index 562e87cbd7a1..46cd9c215d73 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -408,6 +408,16 @@ void unregister_shrinker(struct shrinker *shrinker) } EXPORT_SYMBOL(unregister_shrinker); +/** + * sync_shrinker - Wait for all running shrinkers to complete. + */ +void sync_shrinkers(void) +{ + down_write(&shrinker_rwsem); + up_write(&shrinker_rwsem); +} +EXPORT_SYMBOL(sync_shrinkers); + #define SHRINK_BATCH 128 static unsigned long do_shrink_slab(struct shrink_control *shrinkctl,
Re: [PATCH 1/2] mm/vmscan: add sync_shrinkers function
On 4/9/21 9:17 AM, Christian König wrote: > To be able to switch to a spinlock and reduce lock contention in the TTM > shrinker we don't want to hold a mutex while unmapping and freeing pages > from the pool. Does using spinlock instead of mutex really reduce lock contention? > But then we somehow need to prevent a race between (for example) the shrinker > trying to free pages and hotplug trying to remove the device which those pages > belong to. > > Taking and releasing the shrinker semaphore on the write side after > unmapping and freeing all pages should make sure that no shrinker is running > in > paralell any more. So you explain this in this commit log for adding the function, but then the next patch just adds a sync_shrinkers() call without any comment. I would expect there a comment explaining why it's done there - what it protects against, as it's not an obvious pattern IMHO. > Signed-off-by: Christian König > --- > include/linux/shrinker.h | 1 + > mm/vmscan.c | 10 ++ > 2 files changed, 11 insertions(+) > > diff --git a/include/linux/shrinker.h b/include/linux/shrinker.h > index 0f80123650e2..6b75dc372fce 100644 > --- a/include/linux/shrinker.h > +++ b/include/linux/shrinker.h > @@ -92,4 +92,5 @@ extern void register_shrinker_prepared(struct shrinker > *shrinker); > extern int register_shrinker(struct shrinker *shrinker); > extern void unregister_shrinker(struct shrinker *shrinker); > extern void free_prealloced_shrinker(struct shrinker *shrinker); > +extern void sync_shrinkers(void); > #endif > diff --git a/mm/vmscan.c b/mm/vmscan.c > index 562e87cbd7a1..46cd9c215d73 100644 > --- a/mm/vmscan.c > +++ b/mm/vmscan.c > @@ -408,6 +408,16 @@ void unregister_shrinker(struct shrinker *shrinker) > } > EXPORT_SYMBOL(unregister_shrinker); > > +/** > + * sync_shrinker - Wait for all running shrinkers to complete. > + */ > +void sync_shrinkers(void) > +{ > + down_write(&shrinker_rwsem); > + up_write(&shrinker_rwsem); > +} > +EXPORT_SYMBOL(sync_shrinkers); > + > #define SHRINK_BATCH 128 > > static unsigned long do_shrink_slab(struct shrink_control *shrinkctl, >
[PATCH 1/2] mm/vmscan: add sync_shrinkers function
To be able to switch to a spinlock and reduce lock contention in the TTM shrinker we don't want to hold a mutex while unmapping and freeing pages from the pool. But then we somehow need to prevent a race between (for example) the shrinker trying to free pages and hotplug trying to remove the device which those pages belong to. Taking and releasing the shrinker semaphore on the write side after unmapping and freeing all pages should make sure that no shrinker is running in paralell any more. Signed-off-by: Christian König --- include/linux/shrinker.h | 1 + mm/vmscan.c | 10 ++ 2 files changed, 11 insertions(+) diff --git a/include/linux/shrinker.h b/include/linux/shrinker.h index 0f80123650e2..6b75dc372fce 100644 --- a/include/linux/shrinker.h +++ b/include/linux/shrinker.h @@ -92,4 +92,5 @@ extern void register_shrinker_prepared(struct shrinker *shrinker); extern int register_shrinker(struct shrinker *shrinker); extern void unregister_shrinker(struct shrinker *shrinker); extern void free_prealloced_shrinker(struct shrinker *shrinker); +extern void sync_shrinkers(void); #endif diff --git a/mm/vmscan.c b/mm/vmscan.c index 562e87cbd7a1..46cd9c215d73 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -408,6 +408,16 @@ void unregister_shrinker(struct shrinker *shrinker) } EXPORT_SYMBOL(unregister_shrinker); +/** + * sync_shrinker - Wait for all running shrinkers to complete. + */ +void sync_shrinkers(void) +{ + down_write(&shrinker_rwsem); + up_write(&shrinker_rwsem); +} +EXPORT_SYMBOL(sync_shrinkers); + #define SHRINK_BATCH 128 static unsigned long do_shrink_slab(struct shrink_control *shrinkctl, -- 2.25.1