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 D65BCC4829A for ; Tue, 6 Feb 2024 10:09:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F2CE86B006E; Tue, 6 Feb 2024 05:09:27 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id EDCF96B0071; Tue, 6 Feb 2024 05:09:27 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DA4D36B0072; Tue, 6 Feb 2024 05:09:27 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id CA3A86B006E for ; Tue, 6 Feb 2024 05:09:27 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 571141C1581 for ; Tue, 6 Feb 2024 10:09:27 +0000 (UTC) X-FDA: 81760956774.25.A99909A Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) by imf26.hostedemail.com (Postfix) with ESMTP id 10748140010 for ; Tue, 6 Feb 2024 10:09:24 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=suse.com header.s=susede1 header.b=S9luWhIu; dkim=pass header.d=suse.com header.s=susede1 header.b=RWRxrMyN; spf=pass (imf26.hostedemail.com: domain of mhocko@suse.com designates 195.135.223.130 as permitted sender) smtp.mailfrom=mhocko@suse.com; dmarc=pass (policy=quarantine) header.from=suse.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1707214165; 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:dkim-signature; bh=bojZVVLtPOGoLK1sA3uiRrSi5nB3wv52+1HQCOxhP3Y=; b=mC0++3jUwnRygpcgWkxfejtvZcJ0/KwKE3VNoYuK87h/azdy0GsNZ8GQE5eop6JAwP1TEx gyHJQA7RGD8pjdfVJmrLQt3EpDPLkO+QrTidY7AzppgDZnzKXSmFLG4SzonuDlGiZ8+aYX AkA2qv4+7rfgKg45bA1zwhQ4LbgmfRA= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=suse.com header.s=susede1 header.b=S9luWhIu; dkim=pass header.d=suse.com header.s=susede1 header.b=RWRxrMyN; spf=pass (imf26.hostedemail.com: domain of mhocko@suse.com designates 195.135.223.130 as permitted sender) smtp.mailfrom=mhocko@suse.com; dmarc=pass (policy=quarantine) header.from=suse.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707214165; a=rsa-sha256; cv=none; b=rWVXgpK4PpJ2ziA7JzkIIb1O5XkWZvAKDU2PSaM74NaOFg4dour+/faiF5uIwiZYfsayjF ZeRTjT6Mviu2l8DgXPms361HHnXDVwzBHUBX9azspifE1FmLq+xCebmCrF8Z9lEZCJcZrh v2WJ9W1Z4UOFslzF3esWrkGIeov1b9I= Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [IPv6:2a07:de40:b281:104:10:150:64:97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id E460B220ED; Tue, 6 Feb 2024 10:09:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1707214163; h=from:from:reply-to: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=bojZVVLtPOGoLK1sA3uiRrSi5nB3wv52+1HQCOxhP3Y=; b=S9luWhIutniTZeTnBsvdZxTF/1ej85F6mm/2EOH0VjIDB9S6YYXW5geqAiM5Q2kCbXvj0H LJoGW5qlpMy2z4P9BE6+7GJskE2ULtXV9TLlgGfD6fMU5osa+OXoltXBonc55QIdZJbssy RfHrSb+QpBYpWtdlVr1c/cuEDfslpEI= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1707214162; h=from:from:reply-to: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=bojZVVLtPOGoLK1sA3uiRrSi5nB3wv52+1HQCOxhP3Y=; b=RWRxrMyNzr3byS4rlwEO8LJn6UETXGxPPqKPvoSPZF1KHNW0kjUu0X2n9BcXUZiRNtHnol htT+K0ej1Dbwn/mMc4Y+gdBq4A5gRrQR/0Lx6S+W7y6yzWRDjZClkgyrWBbapp7w+O/V3J 2D/94a+C2RiWfa/xvPnhPMfSoZ0uXgU= Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id DD240139D8; Tue, 6 Feb 2024 10:09:22 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id G274NVIFwmV8egAAD6G6ig (envelope-from ); Tue, 06 Feb 2024 10:09:22 +0000 Date: Tue, 6 Feb 2024 11:09:18 +0100 From: Michal Hocko To: Baolin Wang Cc: akpm@linux-foundation.org, muchun.song@linux.dev, osalvador@suse.de, david@redhat.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v3] mm: hugetlb: improve the handling of hugetlb allocation failure for freed or in-use hugetlb Message-ID: References: <62890fd60b1ecd5bf1cdc476c973f60fe37aa0cb.1707181934.git.baolin.wang@linux.alibaba.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <62890fd60b1ecd5bf1cdc476c973f60fe37aa0cb.1707181934.git.baolin.wang@linux.alibaba.com> X-Rspamd-Queue-Id: 10748140010 X-Rspam-User: X-Stat-Signature: xxxipokmbanc14ii4g17isdxp5mk55zm X-Rspamd-Server: rspam01 X-HE-Tag: 1707214164-144671 X-HE-Meta: U2FsdGVkX19TfB1EKVgatN4cSuZ72WnNKHpEy0batg3vCmEccCQmAaBN4ynmmsExpeWGgiwjo9avmEG/rtrheoDn7Abd7Popzd7GkJmwDjUadIM5nd1WFUnMrKU+/v5ZkukMdWDeMgwc9yvzO8NVBPaNqzUKoLQtbSDlp3xGhya2YxsZKuxHkqnc5AhLu4MeGsES6QvoBAFarl0oUwWteE5/+JLGf/RwylRj5VHnDY5pKQpjzGVUWG2OnyNYhfuytFwbqnVVq0wgd94NiHoUzDVfTG537g6Uu7g2akeZdncrJfz3391Od+rlagWodG83E0bga/Hakz5yat600JD4Scw34u1QxnB9OTWfTzSzBOjO0+kuiqk8TkG2YdOX+2RS4oqi3EMUA0M4ljoaVIMSjz6B+KWsWGlXzydrj/y4OouWX+JEJr/ItzrlsOySsS2oId4gRz+Xt39MgDJmIenKAKrkxIn5PVBFVwg6BNRIVgHrTTNYmj3koXy0KUkIYqXnlghrh0DGeszix/m865q7XetdNUj+1d5mzDgsBmCzj/QW7W21ZvrvRuivikanmHIDdffDvB1Y8fv2UC6CT/OEdVaWi04YOZ+tnMGvahfVOIrAjO0BRPOXMuOcpnKGXuema5NcXyD1wPIZsrmJ55/agxTPQ+qhHsoyBLkb6om5bUCg2NoRXxaIZaQ4PfhQ5FQfcTjRyHOEYS3Hc75XNGKbdsJgfr+Dkix5yNEYW08V6GMXNu4kwjyzB+ktWjzGdk2h+tRGmdxmuH1P5SGbsgOu+cXKZr+tTmZ6lOh9CYlun0DRtj94k3SyA9G4nYPar/G5PJnP9zoFG2Ux+yGRV2Fkx4q+9rSLnoPkCwDG3vJ+flcRqm/PuFPFauvRhuLjwNgaDWx01iG4wUH3Z0s3CNFy9rqPSaEQMUiOVp6y3MeD4SwdshC4Dijg5uSgDy+H0v83zWd9b835P3mK9bVemea nuZjQDlg L1C+w34jfvrHF+dipwF8CTf6nSsd9AOcvbIjc33n1WmZnX4tisItI5nUZcfjq36/JJzanzA3NJ8qRMO7bfVkfSc4/FT8LaKodyq5DOhCt6v4XMwu9FRhGJDRjHnH7GaEoirz/9t8S83Epia4z7JqAzAMm9wsl1YMZjESFg/Jnsk3ZNvwU08ytjBGd/F5gz0DL0bHNGtYjAkhMEmIXG40l5FtmJOQBOOk5mWUFVssakSKn5+a+BSsr+34m9H5VSv1TR/Il7e3vP9Zs+feAa7H6ySK1F4GAl+6dW6EagCtF3GldLFQ= 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: On Tue 06-02-24 11:08:11, Baolin Wang wrote: > alloc_and_dissolve_hugetlb_folio() preallocates a new hugetlb page before > it takes hugetlb_lock. In 3 out of 4 cases the page is not really used and > therefore the newly allocated page is just freed right away. This is > wasteful and it might cause pre-mature failures in those cases. > > Address that by moving the allocation down to the only case (hugetlb > page is really in the free pages pool). We need to drop hugetlb_lock > to do so and therefore need to recheck the page state after regaining > it. > > The patch is more of a cleanup than an actual fix to an existing > problem. There are no known reports about pre-mature failures. > > Signed-off-by: Baolin Wang Acked-by: Michal Hocko Thanks! > --- > Changes from v2; > - Update the commit message suggested by Michal. > - Remove unnecessary comments. > Changes from v1: > - Update the suject line per Muchun. > - Move the allocation into the free hugetlb handling branch per Michal. > --- > mm/hugetlb.c | 32 ++++++++++++++++---------------- > 1 file changed, 16 insertions(+), 16 deletions(-) > > diff --git a/mm/hugetlb.c b/mm/hugetlb.c > index 9d996fe4ecd9..a05507a2143f 100644 > --- a/mm/hugetlb.c > +++ b/mm/hugetlb.c > @@ -3031,21 +3031,9 @@ static int alloc_and_dissolve_hugetlb_folio(struct hstate *h, > { > gfp_t gfp_mask = htlb_alloc_mask(h) | __GFP_THISNODE; > int nid = folio_nid(old_folio); > - struct folio *new_folio; > + struct folio *new_folio = NULL; > int ret = 0; > > - /* > - * Before dissolving the folio, we need to allocate a new one for the > - * pool to remain stable. Here, we allocate the folio and 'prep' it > - * by doing everything but actually updating counters and adding to > - * the pool. This simplifies and let us do most of the processing > - * under the lock. > - */ > - new_folio = alloc_buddy_hugetlb_folio(h, gfp_mask, nid, NULL, NULL); > - if (!new_folio) > - return -ENOMEM; > - __prep_new_hugetlb_folio(h, new_folio); > - > retry: > spin_lock_irq(&hugetlb_lock); > if (!folio_test_hugetlb(old_folio)) { > @@ -3075,6 +3063,16 @@ static int alloc_and_dissolve_hugetlb_folio(struct hstate *h, > cond_resched(); > goto retry; > } else { > + if (!new_folio) { > + spin_unlock_irq(&hugetlb_lock); > + new_folio = alloc_buddy_hugetlb_folio(h, gfp_mask, nid, > + NULL, NULL); > + if (!new_folio) > + return -ENOMEM; > + __prep_new_hugetlb_folio(h, new_folio); > + goto retry; > + } > + > /* > * Ok, old_folio is still a genuine free hugepage. Remove it from > * the freelist and decrease the counters. These will be > @@ -3102,9 +3100,11 @@ static int alloc_and_dissolve_hugetlb_folio(struct hstate *h, > > free_new: > spin_unlock_irq(&hugetlb_lock); > - /* Folio has a zero ref count, but needs a ref to be freed */ > - folio_ref_unfreeze(new_folio, 1); > - update_and_free_hugetlb_folio(h, new_folio, false); > + if (new_folio) { > + /* Folio has a zero ref count, but needs a ref to be freed */ > + folio_ref_unfreeze(new_folio, 1); > + update_and_free_hugetlb_folio(h, new_folio, false); > + } > > return ret; > } > -- > 2.39.3 > -- Michal Hocko SUSE Labs