From patchwork Wed Feb 22 19:52:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13149542 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 99DF4C636D6 for ; Wed, 22 Feb 2023 19:52:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DAB076B0078; Wed, 22 Feb 2023 14:52:55 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D5B976B007B; Wed, 22 Feb 2023 14:52:55 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BFBBF6B007D; Wed, 22 Feb 2023 14:52:55 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id AE3756B0078 for ; Wed, 22 Feb 2023 14:52:55 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 8654B12019C for ; Wed, 22 Feb 2023 19:52:55 +0000 (UTC) X-FDA: 80495975910.15.961DC1E Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf12.hostedemail.com (Postfix) with ESMTP id 3AB3340012 for ; Wed, 22 Feb 2023 19:52:52 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=PKc9445I; spf=pass (imf12.hostedemail.com: domain of peterx@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1677095573; a=rsa-sha256; cv=none; b=UKrQQcNhQ1Ef+7rxUcOSH7IMk+Nw+bjyv114U4eu7HgBRNkU5cOroa/HRbjb3+QnKGyuB5 5Hh5po6IeduvdBfv7eq8VkNrFRCtHs/rWBtEUXJYK4mb+Spp/hKNseC5O/93DfDWLu2lA7 8zdZ08AeZq5GOXlwRJr0ZeZkrrrZ4BM= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=PKc9445I; spf=pass (imf12.hostedemail.com: domain of peterx@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1677095573; 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: references:dkim-signature; bh=t0NA5RenyfNbff5yQYPO5wNBBKMedrP9MogpxUVd1CU=; b=oNnM25pO9htG/wu47k4t6q441gHgVeLi2MbAHPfRlvg3LCiOX5TZzYjnusTmfqtEiTFa/D iIj3V7Vlc8WA/8tzyn6J+5gQkPmfAFaDyS1d4O3pBufeqnplq3JGKYeTAtkvX2IfU06NH9 2+9E76lMaBcRLSHj/uSnSl2NriGFFEo= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1677095571; h=from:from: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; bh=t0NA5RenyfNbff5yQYPO5wNBBKMedrP9MogpxUVd1CU=; b=PKc9445IeY+zyidS4U9/qgz9zLHwVpJjh6mDYu6d/+Nrb+HySzn4fHhBsL+7rBdHxEffq0 BIOE4FvkXjBnbO43+7mUwxx5B22sdyNr1BTa54bdzBmMmusE+hPnMAOMlFZrUQhv0FLbWU 9Gzd9A2/+CZOlJjf/0cMRSCEeLNau7U= Received: from mail-qk1-f200.google.com (mail-qk1-f200.google.com [209.85.222.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-517-xDiSIVDeMoGN0QkG4XJi9g-1; Wed, 22 Feb 2023 14:52:50 -0500 X-MC-Unique: xDiSIVDeMoGN0QkG4XJi9g-1 Received: by mail-qk1-f200.google.com with SMTP id c9-20020ae9e209000000b0073b344eb74dso4120893qkc.10 for ; Wed, 22 Feb 2023 11:52:50 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=t0NA5RenyfNbff5yQYPO5wNBBKMedrP9MogpxUVd1CU=; b=3GgJb9Dm3wyg8qgbBC6bJJuGsu0Ti94mdotL/nVrZiH9S2rtKPS1DsjHY2m37sPB8T bQHPMfN7UHJnTH+cLXZINF6jTHxnpAkp6devP7LDociNArFRptfVtCXPNI0a6Qp5vnaK OK6c8MnpG1vzOuHkojC/1fxeCKRABcEnEjfa6GbdJPLax7cSENplh/2Q3BCFXHxZ9Aj+ QSvYcE/U5/LHszNlZl2N8bdh/pr7gJm1CGuWEIvGsD6vzOgLtcNeoXuFY7OPIEL8QhzF Cawy5LGTxcEsGHheSdnDfLNQs9EO9q1bDr+3GVmz2lvaqL6t17iwMt1g+1DZYbS9wtkI u+BQ== X-Gm-Message-State: AO0yUKX76IiPIMgELDPWBnjULqyJP7ObQgf31Il1lBSbvxp7zqxRxfVL Wr/i/fFPci9yKOZ+tBl/LSADRgYDvtn9N4Fjbl9nDWMZS2hyzT3GWXNMTpeQNwcTX8ciKL4DTfe /soEiPgcinPw= X-Received: by 2002:a05:622a:1002:b0:3b8:5199:f841 with SMTP id d2-20020a05622a100200b003b85199f841mr16964406qte.0.1677095569799; Wed, 22 Feb 2023 11:52:49 -0800 (PST) X-Google-Smtp-Source: AK7set+i8gH40eG4zEccagdjimlzOlZhlL8oGj47vXuvtA4JJ/FKzBVgYL6RseMyK3jse9jHsqIs0w== X-Received: by 2002:a05:622a:1002:b0:3b8:5199:f841 with SMTP id d2-20020a05622a100200b003b85199f841mr16964375qte.0.1677095569503; Wed, 22 Feb 2023 11:52:49 -0800 (PST) Received: from x1n.redhat.com (bras-base-aurron9127w-grc-56-70-30-145-63.dsl.bell.ca. [70.30.145.63]) by smtp.gmail.com with ESMTPSA id f190-20020a37d2c7000000b00729b7d71ac7sm626022qkj.33.2023.02.22.11.52.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Feb 2023 11:52:48 -0800 (PST) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: peterx@redhat.com, Andrew Morton , Yang Shi , David Stevens , Johannes Weiner Subject: [PATCH v2] mm/khugepaged: alloc_charge_hpage() take care of mem charge errors Date: Wed, 22 Feb 2023 14:52:47 -0500 Message-Id: <20230222195247.791227-1-peterx@redhat.com> X-Mailer: git-send-email 2.39.1 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Rspam-User: X-Rspamd-Queue-Id: 3AB3340012 X-Rspamd-Server: rspam01 X-Stat-Signature: t1nf9ob7esn9qhq6huwpk6nrs4dgr53j X-HE-Tag: 1677095572-451180 X-HE-Meta: U2FsdGVkX18hpGU3KgmOVmr4Z7TKgxPjIRDaU+XpFyqg+Axggzkkbf0kumai2Wvjv7Nbr7L8jdwfD5US3E7x0ixySdLw4fdD2v3Pf7vOEuu2dNbccujyisACSUD7Vbp07Ncu2HDLJljRkbIzi4uO8bKfO6rMSEafu42cPAaLDZx1G5KTKmq7GhRMvneJ88/GziYDak/8hJyHHzQ1tAJw2cIv9Q9TUPVcZCFHYzlHhQgZ91lKEvRloSeYzgMfszgWQsrWrHhNJ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0hHqFr/QRCD8NXKVSNkLKC9DB9Wv7S3oLNWm9kvvWkWVyn1neXuJzvmaVF4f06cmOI1n1Yl7gnTMD2UxcEM6+J/l0dgJBLltJpj+ZZV927goJRWL5X4l1GmEX0zCmUAn02On2uvGR6RJekqO1hL4Aze9uKN7SCGP8dBHiAbeNUIlU82lHM7IS3QrauYjYsyrnVWneunZu4sjVbx0a8SA1NcCxZLEnoXlsA0Olo++QYuCwRtxImMhDzt+daAcSpNN9950Og== 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: If memory charge failed, instead of returning the hpage but with an error, allow the function to cleanup the folio properly, which is normally what a function should do in this case - either return successfully, or return with no side effect of partial runs with an indicated error. This will also avoid the caller calling mem_cgroup_uncharge() unnecessarily with either anon or shmem path (even if it's safe to do so). Cc: Yang Shi Reviewed-by: David Stevens Acked-by: Johannes Weiner Signed-off-by: Peter Xu Reviewed-by: Zach O'Keefe --- v1->v2: - Enhance commit message, drop "Fixes:" and "Cc: stable" tag, add R-bs. --- mm/khugepaged.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/mm/khugepaged.c b/mm/khugepaged.c index 8dbc39896811..941d1c7ea910 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -1063,12 +1063,19 @@ static int alloc_charge_hpage(struct page **hpage, struct mm_struct *mm, gfp_t gfp = (cc->is_khugepaged ? alloc_hugepage_khugepaged_gfpmask() : GFP_TRANSHUGE); int node = hpage_collapse_find_target_node(cc); + struct folio *folio; if (!hpage_collapse_alloc_page(hpage, gfp, node, &cc->alloc_nmask)) return SCAN_ALLOC_HUGE_PAGE_FAIL; - if (unlikely(mem_cgroup_charge(page_folio(*hpage), mm, gfp))) + + folio = page_folio(*hpage); + if (unlikely(mem_cgroup_charge(folio, mm, gfp))) { + folio_put(folio); + *hpage = NULL; return SCAN_CGROUP_CHARGE_FAIL; + } count_memcg_page_event(*hpage, THP_COLLAPSE_ALLOC); + return SCAN_SUCCEED; }