Received: by 2002:a05:7412:bbc7:b0:fc:a2b0:25d7 with SMTP id kh7csp2935886rdb; Tue, 6 Feb 2024 02:09:32 -0800 (PST) X-Google-Smtp-Source: AGHT+IHAcNj9ZyYS8EGgvhL23ixNLHuFePtWAeEFuEvjsNC6hmUhPwYxBzjvS3Cq//CaAR/8ak/u X-Received: by 2002:a17:90b:3657:b0:296:a288:4e17 with SMTP id nh23-20020a17090b365700b00296a2884e17mr2102991pjb.2.1707214172679; Tue, 06 Feb 2024 02:09:32 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707214172; cv=pass; d=google.com; s=arc-20160816; b=FKUhrhk6z+um1CK46R6UaECPn4+qw8uHE45al6Tj7I1VvApWbbuVtSn0rv9CwH/pY0 Y1BBrSlWtGJ3hRN0Gsq9bWjiF1+ULApqx2Kng3cuGNEqZOEdXExcWTJmO+jhK/bei3X+ OTXCGFS/iWrylHRfFa9YPv+VClBqH+hBkWrwi3vtiWjziZ2H4xkRnQVnQw+VQLsP2XCN JaR7QIvN0gYtlyA8dIwb2yi3dmIQ2g7VISpuVoCka8Dgj7yPQUHUQYm3z/s6bB+VpHd7 kkaTf4/4FTn1nuVEkAsfeYAsI9BL1S5FfpW5F5BZytqDgoxhBcmDcjYpn7ttkoL3dTQn mkPw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :references:message-id:subject:cc:to:from:date:dkim-signature :dkim-signature; bh=bojZVVLtPOGoLK1sA3uiRrSi5nB3wv52+1HQCOxhP3Y=; fh=HUKEJqXjMzNQJn3DnmgXfrsC7sMmQU8MKa43XBkQzXg=; b=H1I7UMZuwVi1D7qwpoA55sqYcPoqJKq4KtCBmbRK/R1Wr96hAQ9O9igs0aLjlMkdJz YqYtK2LCdMbisJDY7SpCXPqHTlmMktfd7IjUEOcANwX1ngWCnKXHEt/w7RkeSrw/ocsV ay1JHaWxbn8aJVWdlx7Te7Uo6hS0m09TuEaUwB9V73H6VJ1CuHqun83PIN1tq97xUEIX 07bUS4r6hDmc5l7QS36ZGbzW4MQacxVrKPndqbbAQmQ9OILXKBx6UjPWL5zz0UQDwQXv igThTG4iGbw9lfteJo8Ve4cCzcwmmJa3atu7ytfITLTjlpk9JjiSElf4OjZXfXazB9Pz liaQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@suse.com header.s=susede1 header.b=S9luWhIu; dkim=pass header.i=@suse.com header.s=susede1 header.b=RWRxrMyN; arc=pass (i=1 spf=pass spfdomain=suse.com dkim=pass dkdomain=suse.com dkim=pass dkdomain=suse.com dmarc=pass fromdomain=suse.com); spf=pass (google.com: domain of linux-kernel+bounces-54652-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-54652-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=suse.com X-Forwarded-Encrypted: i=1; AJvYcCUsDaOc+g2VpEa1uHIc5U2px8FqkODGN+A5akiPW4Q+z7MPmjjISsKSLVa8G3LyscinKtQ80QewmNFqvb8q9tehtsTF90m1ecnc94yzTg== Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id in18-20020a17090b439200b00296a2ce4d8csi899694pjb.114.2024.02.06.02.09.32 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Feb 2024 02:09:32 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-54652-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.com header.s=susede1 header.b=S9luWhIu; dkim=pass header.i=@suse.com header.s=susede1 header.b=RWRxrMyN; arc=pass (i=1 spf=pass spfdomain=suse.com dkim=pass dkdomain=suse.com dkim=pass dkdomain=suse.com dmarc=pass fromdomain=suse.com); spf=pass (google.com: domain of linux-kernel+bounces-54652-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-54652-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=suse.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 51AE2282775 for ; Tue, 6 Feb 2024 10:09:32 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0FE6312DDB3; Tue, 6 Feb 2024 10:09:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="S9luWhIu"; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="RWRxrMyN" Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 30C3A12D74F for ; Tue, 6 Feb 2024 10:09:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=195.135.223.130 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707214167; cv=none; b=nBAtxcdt0n1GbQC4F2VmQqfoGlIPB9fuXO43OCZB232WFHzW8i4SLCvS1oWIMcUYawX85HSLnK7bJKtiQ158h20zc5XNCYW5DSFppfNY4NUM2sBg7a6vio5zRM9h3LifeS9V/e0DTT48ZRK5ZvWt2Ag5uUKk6M7LIk5H+EucaCw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707214167; c=relaxed/simple; bh=6LFp5U27NfxLRXdMn0u8CmFGxJPFXl9ATDKfDRjMdcE=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=mBFtVuBwCPKB6lAc0PV4jcbsV/pJKWcGdFzWvCbqScSMr2KWX08BKH8lpv8mp/AlYlTdT1NmutElpBi5pFwmux4AwGjCQulW6thuDVudUCDz4XysIEAQATpI1AI8TNTpj/OxKnQTbfJoQx6uNTlvulc47V4Garm61a4bhe9LDI0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com; spf=pass smtp.mailfrom=suse.com; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b=S9luWhIu; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b=RWRxrMyN; arc=none smtp.client-ip=195.135.223.130 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.com 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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: 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-Spam-Level: Authentication-Results: smtp-out1.suse.de; dkim=pass header.d=suse.com header.s=susede1 header.b=RWRxrMyN X-Rspamd-Server: rspamd2.dmz-prg2.suse.org X-Spamd-Result: default: False [-6.01 / 50.00]; ARC_NA(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; R_DKIM_ALLOW(-0.20)[suse.com:s=susede1]; SPAMHAUS_XBL(0.00)[2a07:de40:b281:104:10:150:64:97:from]; FROM_HAS_DN(0.00)[]; TO_DN_SOME(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; NEURAL_HAM_LONG(-1.00)[-1.000]; MIME_GOOD(-0.10)[text/plain]; DWL_DNSWL_MED(-2.00)[suse.com:dkim]; NEURAL_HAM_SHORT(-0.20)[-1.000]; RCVD_COUNT_THREE(0.00)[3]; DKIM_SIGNED(0.00)[suse.com:s=susede1]; DKIM_TRACE(0.00)[suse.com:+]; MX_GOOD(-0.01)[]; RCPT_COUNT_SEVEN(0.00)[7]; DBL_BLOCKED_OPENRESOLVER(0.00)[suse.com:dkim,suse.com:email]; FUZZY_BLOCKED(0.00)[rspamd.com]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+]; MID_RHS_NOT_FQDN(0.50)[]; RCVD_TLS_ALL(0.00)[]; BAYES_HAM(-3.00)[100.00%] X-Spam-Score: -6.01 X-Rspamd-Queue-Id: E460B220ED X-Spam-Flag: NO 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