Re: [PATCH 7/7] mm/page_alloc: Replace local_lock with normal spinlock

From: Yu Zhao
Date: Wed Jun 15 2022 - 23:05:21 EST


On Wed, Jun 15, 2022 at 04:04:46PM -0700, Andrew Morton wrote:
> On Thu, 16 Jun 2022 00:48:55 +0200 Marek Szyprowski <m.szyprowski@xxxxxxxxxxx> wrote:
>
> > In the logs I see lots of errors like:
> >
> > BUG: sleeping function called from invalid context at
> > ./include/linux/sched/mm.h:274
> >
> > BUG: scheduling while atomic: systemd-udevd/288/0x00000002
> >
> > BUG: sleeping function called from invalid context at mm/filemap.c:2647
> >
> > however there are also a fatal ones like:
> >
> > Unable to handle kernel paging request at virtual address 00000000017a87b4
> >
> >
> > The issues seems to be a bit random. Looks like memory trashing.
> > Reverting $subject on top of current linux-next fixes all those issues.
> >
> >
>
> This?
>
> --- a/mm/page_alloc.c~mm-page_alloc-replace-local_lock-with-normal-spinlock-fix
> +++ a/mm/page_alloc.c
> @@ -183,8 +183,10 @@ static DEFINE_MUTEX(pcp_batch_high_lock)
> type *_ret; \
> pcpu_task_pin(); \
> _ret = this_cpu_ptr(ptr); \
> - if (!spin_trylock_irqsave(&_ret->member, flags)) \
> + if (!spin_trylock_irqsave(&_ret->member, flags)) { \
> + pcpu_task_unpin(); \
> _ret = NULL; \
> + } \
> _ret; \
> })
>
>
> I'll drop Mel's patch for next -next.

While we are at it, please consider this cleanup:

mm/page_alloc.c | 48 +++++++++---------------------------------------
1 file changed, 9 insertions(+), 39 deletions(-)

diff --git a/mm/page_alloc.c b/mm/page_alloc.c
index e538dde2c1c0..a1b76d5fdf75 100644
--- a/mm/page_alloc.c
+++ b/mm/page_alloc.c
@@ -160,61 +160,31 @@ static DEFINE_MUTEX(pcp_batch_high_lock);
* Generic helper to lookup and a per-cpu variable with an embedded spinlock.
* Return value should be used with equivalent unlock helper.
*/
-#define pcpu_spin_lock(type, member, ptr) \
-({ \
- type *_ret; \
- pcpu_task_pin(); \
- _ret = this_cpu_ptr(ptr); \
- spin_lock(&_ret->member); \
- _ret; \
-})
-
-#define pcpu_spin_lock_irqsave(type, member, ptr, flags) \
-({ \
- type *_ret; \
- pcpu_task_pin(); \
- _ret = this_cpu_ptr(ptr); \
- spin_lock_irqsave(&_ret->member, flags); \
- _ret; \
-})
-
-#define pcpu_spin_trylock_irqsave(type, member, ptr, flags) \
-({ \
- type *_ret; \
- pcpu_task_pin(); \
- _ret = this_cpu_ptr(ptr); \
- if (!spin_trylock_irqsave(&_ret->member, flags)) \
- _ret = NULL; \
- _ret; \
-})
-
-#define pcpu_spin_unlock(member, ptr) \
-({ \
- spin_unlock(&ptr->member); \
- pcpu_task_unpin(); \
-})
-
-#define pcpu_spin_unlock_irqrestore(member, ptr, flags) \
-({ \
- spin_unlock_irqrestore(&ptr->member, flags); \
- pcpu_task_unpin(); \
-})
-
-/* struct per_cpu_pages specific helpers. */
-#define pcp_spin_lock(ptr) \
- pcpu_spin_lock(struct per_cpu_pages, lock, ptr)
-
#define pcp_spin_lock_irqsave(ptr, flags) \
- pcpu_spin_lock_irqsave(struct per_cpu_pages, lock, ptr, flags)
+({ \
+ struct per_cpu_pages *_ret; \
+ pcpu_task_pin(); \
+ _ret = this_cpu_ptr(ptr); \
+ spin_lock_irqsave(&_ret->lock, flags); \
+ _ret; \
+})

#define pcp_spin_trylock_irqsave(ptr, flags) \
- pcpu_spin_trylock_irqsave(struct per_cpu_pages, lock, ptr, flags)
-
-#define pcp_spin_unlock(ptr) \
- pcpu_spin_unlock(lock, ptr)
+({ \
+ struct per_cpu_pages *_ret; \
+ pcpu_task_pin(); \
+ _ret = this_cpu_ptr(ptr); \
+ if (!spin_trylock_irqsave(&_ret->lock, flags)) \
+ _ret = NULL; \
+ _ret; \
+})

#define pcp_spin_unlock_irqrestore(ptr, flags) \
- pcpu_spin_unlock_irqrestore(lock, ptr, flags)
+({ \
+ spin_unlock_irqrestore(&ptr->lock, flags); \
+ pcpu_task_unpin(); \
+})
+
#ifdef CONFIG_USE_PERCPU_NUMA_NODE_ID
DEFINE_PER_CPU(int, numa_node);
EXPORT_PER_CPU_SYMBOL(numa_node);
@@ -3488,7 +3458,7 @@ void free_unref_page(struct page *page, unsigned int order)

zone = page_zone(page);
pcp_trylock_prepare(UP_flags);
- pcp = pcpu_spin_trylock_irqsave(struct per_cpu_pages, lock, zone->per_cpu_pageset, flags);
+ pcp = pcp_spin_trylock_irqsave(zone->per_cpu_pageset, flags);
if (pcp) {
free_unref_page_commit(pcp, zone, page, migratetype, order);
pcp_spin_unlock_irqrestore(pcp, flags);