From patchwork Thu Jul 27 11:56:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miaohe Lin X-Patchwork-Id: 126941 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a985:0:b0:3e4:2afc:c1 with SMTP id t5csp1057689vqo; Thu, 27 Jul 2023 05:21:57 -0700 (PDT) X-Google-Smtp-Source: APBJJlHJqonzYp/gutZzC67hytuoemg7wVwjvnYqvmFrRYKC6wva6XUfS0Xgdr/FrjiEqtXsNec8 X-Received: by 2002:a17:906:209a:b0:99b:5abb:8caf with SMTP id 26-20020a170906209a00b0099b5abb8cafmr1656772ejq.44.1690460517181; Thu, 27 Jul 2023 05:21:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690460517; cv=none; d=google.com; s=arc-20160816; b=WJxUeTUKc23HhV1WbOH4BoJM0CclkffM1tjaKAnpDfsyIRk+DV0apuCjZ9QqBBXwjU rE+yORTkqzGw58xgLEz6DDbnasbfwS+sxFk/FNQ2FzJYqmf87PcGlwhYHZdR5Rsx8LGm /XZJulhFyYANfXU31UmfJCJ+B7UTtfNeAjhm4LYVuXPWgoQpDEw6wTufiA8iVLhgwtdK KkohiniTa5xxeKcjMsiPG3aLzdD7ndG/CGDNJAq1eR1+2vCiLRdOGKIGYTXhB3s0RBz0 UykY8vSiMo+KdKpW238bM7lzASzxoThm+vyUXTYw0B2Xwp3TEcyaqNdnDVp6GID1W6ss gqVw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=MvyH4AFrYOxo0odqqElQhuARJ/c2riZFZSUjGAvlpN0=; fh=eUbl4v09I/Ha4OhmJ6gzDr/GCs46/1skqMRau+2m7SY=; b=eK4BaAasV/VR4HNv07sUxTipsojp5cmbqjUj2jIzXYRJSsqv2Pprm8UWPHZ4h4eQP0 4ltPNBjlZJ4mOTUvCHQkOsYhne+sQ96+qWe0/ARkd8PtzQDTQ75ccJtDYh46qTZeZTYk VurtE4iRBTb+QysV++Bztwv0AHvWPO8nOQM+fa0SrtT9JocDv92eOxSq8ltEgyBDQNfI M505/I+pNNbEWxLS2CR4U9h5feP1rip6oJ+MxWpnvz9jk2/rNWs0Dx3vH5MoGzmiXo54 KGpjv57L6A+iPJSmh5Nur8Avtt1fbdgeJKDxlFnC2N46h8ht00uQaYc6ahhUILY3OzzZ A+Tg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id lu22-20020a170906fad600b009938cb10548si933954ejb.549.2023.07.27.05.21.32; Thu, 27 Jul 2023 05:21:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233606AbjG0L5C (ORCPT + 99 others); Thu, 27 Jul 2023 07:57:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44138 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233574AbjG0L44 (ORCPT ); Thu, 27 Jul 2023 07:56:56 -0400 Received: from szxga03-in.huawei.com (szxga03-in.huawei.com [45.249.212.189]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8B1A52D4B for ; Thu, 27 Jul 2023 04:56:53 -0700 (PDT) Received: from canpemm500002.china.huawei.com (unknown [172.30.72.57]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4RBTh66H48zLntw; Thu, 27 Jul 2023 19:54:14 +0800 (CST) Received: from huawei.com (10.174.151.185) by canpemm500002.china.huawei.com (7.192.104.244) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.27; Thu, 27 Jul 2023 19:56:50 +0800 From: Miaohe Lin To: , CC: , , , Subject: [PATCH v2 2/4] mm: memory-failure: fix potential unexpected return value from unpoison_memory() Date: Thu, 27 Jul 2023 19:56:41 +0800 Message-ID: <20230727115643.639741-3-linmiaohe@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20230727115643.639741-1-linmiaohe@huawei.com> References: <20230727115643.639741-1-linmiaohe@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.174.151.185] X-ClientProxiedBy: dggems701-chm.china.huawei.com (10.3.19.178) To canpemm500002.china.huawei.com (7.192.104.244) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1772576327271747850 X-GMAIL-MSGID: 1772576327271747850 If unpoison_memory() fails to clear page hwpoisoned flag, return value ret is expected to be -EBUSY. But when get_hwpoison_page() returns 1 and fails to clear page hwpoisoned flag due to races, return value will be unexpected 1 leading to users being confused. And there's a code smell that the variable "ret" is used not only to save the return value of unpoison_memory(), but also the return value from get_hwpoison_page(). Make a further cleanup by using another auto-variable solely to save the return value of get_hwpoison_page() as suggested by Naoya. Fixes: bf181c582588 ("mm/hwpoison: fix unpoison_memory()") Signed-off-by: Miaohe Lin --- mm/memory-failure.c | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/mm/memory-failure.c b/mm/memory-failure.c index a114c8c3039c..4a3e88c15631 100644 --- a/mm/memory-failure.c +++ b/mm/memory-failure.c @@ -2502,7 +2502,7 @@ int unpoison_memory(unsigned long pfn) { struct folio *folio; struct page *p; - int ret = -EBUSY; + int ret = -EBUSY, ghp; unsigned long count = 1; bool huge = false; static DEFINE_RATELIMIT_STATE(unpoison_rs, DEFAULT_RATELIMIT_INTERVAL, @@ -2550,29 +2550,28 @@ int unpoison_memory(unsigned long pfn) if (folio_test_slab(folio) || PageTable(&folio->page) || folio_test_reserved(folio)) goto unlock_mutex; - ret = get_hwpoison_page(p, MF_UNPOISON); - if (!ret) { + ghp = get_hwpoison_page(p, MF_UNPOISON); + if (!ghp) { if (PageHuge(p)) { huge = true; count = folio_free_raw_hwp(folio, false); - if (count == 0) { - ret = -EBUSY; + if (count == 0) goto unlock_mutex; - } } ret = folio_test_clear_hwpoison(folio) ? 0 : -EBUSY; - } else if (ret < 0) { - if (ret == -EHWPOISON) { + } else if (ghp < 0) { + if (ghp == -EHWPOISON) { ret = put_page_back_buddy(p) ? 0 : -EBUSY; - } else + } else { + ret = ghp; unpoison_pr_info("Unpoison: failed to grab page %#lx\n", pfn, &unpoison_rs); + } } else { if (PageHuge(p)) { huge = true; count = folio_free_raw_hwp(folio, false); if (count == 0) { - ret = -EBUSY; folio_put(folio); goto unlock_mutex; }