From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0553AC5475B for ; Fri, 8 Mar 2024 11:44:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 62E7A6B0374; Fri, 8 Mar 2024 06:44:42 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 5DD9E6B0375; Fri, 8 Mar 2024 06:44:42 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4CD536B0376; Fri, 8 Mar 2024 06:44:42 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 3988B6B0374 for ; Fri, 8 Mar 2024 06:44:42 -0500 (EST) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 97709C03A2 for ; Fri, 8 Mar 2024 11:44:41 +0000 (UTC) X-FDA: 81873689562.07.E7EDE8F Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf19.hostedemail.com (Postfix) with ESMTP id BBFB41A0014 for ; Fri, 8 Mar 2024 11:44:39 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=none; spf=pass (imf19.hostedemail.com: domain of ryan.roberts@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=ryan.roberts@arm.com; dmarc=pass (policy=none) header.from=arm.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1709898280; a=rsa-sha256; cv=none; b=gyAFiIQ1YSzgaKVOr/c9+io7E/Bjgc8p4TnL5oEEKoBmpXdkMrgxhqRabxm4b3guCvAUaj h4JJp6VXjvtNHmitsd0lswtc8KbL0zI2nyIZ+08/OR9SVEpJnq3dtbjOdqBtnQppYZNBJ4 fpAuoNRzX6teUJO1JRU45CWsN1Csokw= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=none; spf=pass (imf19.hostedemail.com: domain of ryan.roberts@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=ryan.roberts@arm.com; dmarc=pass (policy=none) header.from=arm.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1709898280; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=EhtpLNcNS7MQAFpYuJ5sfzEpqkv0J8MRtIq0AT3a63M=; b=LHPcK0kJLvq+amP2np1eCp/1lNDNo897NCCWxiRLf0raHYxJ8BB32UTUFkUK6DiurcrJ5j tt71m1QG9DbNqP2rM99H0cgDlVwtqderW52q/uAgfTQ6sNsSMrbYxnMN+HsUXK2AK5JGqH tq6sT3pGo7vEOvm4fHZbWRnM5Ig9gfs= Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 3BA84C15; Fri, 8 Mar 2024 03:45:15 -0800 (PST) Received: from [10.57.70.163] (unknown [10.57.70.163]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 55C943F73F; Fri, 8 Mar 2024 03:44:37 -0800 (PST) Message-ID: Date: Fri, 8 Mar 2024 11:44:35 +0000 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v3 10/18] mm: Allow non-hugetlb large folios to be batch processed Content-Language: en-GB To: Matthew Wilcox Cc: Zi Yan , Andrew Morton , linux-mm@kvack.org, Yang Shi , Huang Ying References: <20240227174254.710559-1-willy@infradead.org> <20240227174254.710559-11-willy@infradead.org> <367a14f7-340e-4b29-90ae-bc3fcefdd5f4@arm.com> <85cc26ed-6386-4d6b-b680-1e5fba07843f@arm.com> <36bdda72-2731-440e-ad15-39b845401f50@arm.com> <03CE3A00-917C-48CC-8E1C-6A98713C817C@nvidia.com> From: Ryan Roberts In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: BBFB41A0014 X-Stat-Signature: 5j8cz8hxm5nzry5ecxxup4qat693c4y3 X-HE-Tag: 1709898279-129423 X-HE-Meta: U2FsdGVkX1+4DsEwEBVK7bWNGJQgDQNP7F35pnIdZIIVK517gNg4/dIbsfuE85xMI6w4AS2uJvLNb09h9Gn5d7r6p0vRA2jiBwFekyvVnFGZQTQo9yYOZlGG4akhVezn8hWnYLS9ASRejgGaQ1oerggwndS5AA1jpMNt1qsJ2QBH/OLaEcZ5ZHi2SPJYGdEeaWcKFfkix9OTsXp8iIR6akzAvfsOC1kIcAQzPeSGVODBwvu2F7KeM1wkmkol2Ndh2zGS+KdK5o1wx1BQ9c76CWDP7oB+5RARAkO0MFh/UsqA1bHIkL8IyfBV1hrcpIkyNBOUAiGvM8xIjGylDxmxMLZmVYCVVLlAursGd3v7Ik9IAC7/zLInuJFKX40+OGXXvVJ6tqWgYdZOGQi8gHIsMVAEVDEm4m8TJWEdu9SM5+d73TQGQsz7R1T6M3Pcd5DfJUxkkvj4O4MfOwD9CPL/warPLLa0RFdHONWkAQUEAJfvL2oij0k2Ft7NoRSyP7NEOETIWn1NXV4gjJKfHzi6OhodLoLmHpS17fJu6+6B8RxzAaSRjhkpOF5+aJ1VKHPdZBhiQyR3J+ngl2TQHlmopbAQypu73zRNoNbqxCIRbDwotpvt6xo7uZZic1vQ0Q2Fgyq46ZuJo6uEVCymXLcyr6H8lN3S6us1SQWxf3xA6o7X8hnWiwaSikCoZVGrEvHJ6U/ywLeJ59glFaEbOCRJEIw8jNm9yqW5t+Nut36X1ES9JjjzMGDj7dDj7VKT+d+R7+CG3uiGyOuoEcKWF0xSbZ0lhWseWuQ+rXTsu/ZxJ2aOXqcUP37CcaR1/bNT9dX0hnnTYZOtQSXYsAs2aWxFvFE5T1WgzCWP X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: > The thought occurs that we don't need to take the folios off the list. > I don't know that will fix anything, but this will fix your "running out > of memory" problem -- I forgot to drop the reference if folio_trylock() > failed. Of course, I can't call folio_put() inside the lock, so may > as well move the trylock back to the second loop. > > Again, compile-tessted only. > > diff --git a/mm/huge_memory.c b/mm/huge_memory.c > index fd745bcc97ff..4a2ab17f802d 100644 > --- a/mm/huge_memory.c > +++ b/mm/huge_memory.c > @@ -3312,7 +3312,7 @@ static unsigned long deferred_split_scan(struct shrinker *shrink, > struct pglist_data *pgdata = NODE_DATA(sc->nid); > struct deferred_split *ds_queue = &pgdata->deferred_split_queue; > unsigned long flags; > - LIST_HEAD(list); > + struct folio_batch batch; > struct folio *folio, *next; > int split = 0; > > @@ -3321,36 +3321,31 @@ static unsigned long deferred_split_scan(struct shrinker *shrink, > ds_queue = &sc->memcg->deferred_split_queue; > #endif > > + folio_batch_init(&batch); > spin_lock_irqsave(&ds_queue->split_queue_lock, flags); > - /* Take pin on all head pages to avoid freeing them under us */ > + /* Take ref on all folios to avoid freeing them under us */ > list_for_each_entry_safe(folio, next, &ds_queue->split_queue, > _deferred_list) { > - if (folio_try_get(folio)) { > - list_move(&folio->_deferred_list, &list); > - } else { > - /* We lost race with folio_put() */ > - list_del_init(&folio->_deferred_list); > - ds_queue->split_queue_len--; > + if (!folio_try_get(folio)) > + continue; > + if (folio_batch_add(&batch, folio) == 0) { > + --sc->nr_to_scan; > + break; > } > if (!--sc->nr_to_scan) > break; > } > spin_unlock_irqrestore(&ds_queue->split_queue_lock, flags); > > - list_for_each_entry_safe(folio, next, &list, _deferred_list) { > + while ((folio = folio_batch_next(&batch)) != NULL) { > if (!folio_trylock(folio)) > - goto next; > - /* split_huge_page() removes page from list on success */ > + continue; > if (!split_folio(folio)) > split++; > folio_unlock(folio); > -next: > - folio_put(folio); > } > > - spin_lock_irqsave(&ds_queue->split_queue_lock, flags); > - list_splice_tail(&list, &ds_queue->split_queue); > - spin_unlock_irqrestore(&ds_queue->split_queue_lock, flags); > + folios_put(&batch); > > /* > * Stop shrinker if we didn't split any page, but the queue is empty. OK I've tested this; the good news is that I haven't seen any oopses or memory leaks. The bad news is that it still takes an absolute age (hours) to complete the same test that without "mm: Allow non-hugetlb large folios to be batch processed" took a couple of mins. And during that time, the system is completely unresponsive - serial terminal doesn't work - can't even break in with sysreq. And sometimes I see RCU stall warnings. Dumping all the CPU back traces with gdb, all the cores (except one) are contending on the the deferred split lock. A couple of thoughts: - Since we are now taking a maximum of 15 folios into a batch, deferred_split_scan() is called much more often (in a tight loop from do_shrink_slab()). Could it be that we are just trying to take the lock so much more often now? I don't think it's quite that simple because we take the lock for every single folio when adding it to the queue, so the dequeing cost should still be a factor of 15 locks less. - do_shrink_slab() might be calling deferred_split_scan() in a tight loop with deferred_split_scan() returning 0 most of the time. If there are still folios on the deferred split list but deferred_split_scan() was unable to lock any folios then it will return 0, not SHRINK_STOP, so do_shrink_slab() will keep calling it, essentially live locking. Has your patch changed the duration of the folio being locked? I don't think so... - Ahh, perhaps its as simple as your fix has removed the code that removed the folio from the deferred split queue if it fails to get a reference? That could mean we end up returning 0 instead of SHRINK_STOP too. I'll have play.