From patchwork Fri Jul 7 20:19:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiaqi Yan X-Patchwork-Id: 13305251 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 319C5C001B0 for ; Fri, 7 Jul 2023 20:19:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BE3016B0078; Fri, 7 Jul 2023 16:19:19 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B1A916B007B; Fri, 7 Jul 2023 16:19:19 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 96FA88D0001; Fri, 7 Jul 2023 16:19:19 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 82DCE6B0078 for ; Fri, 7 Jul 2023 16:19:19 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 4DB8514011D for ; Fri, 7 Jul 2023 20:19:19 +0000 (UTC) X-FDA: 80985930438.02.9E62333 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) by imf05.hostedemail.com (Postfix) with ESMTP id 81213100019 for ; Fri, 7 Jul 2023 20:19:17 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=VqyQmTQk; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf05.hostedemail.com: domain of 3RHOoZAgKCPcihZphxZmfnnfkd.bnlkhmtw-lljuZbj.nqf@flex--jiaqiyan.bounces.google.com designates 209.85.214.202 as permitted sender) smtp.mailfrom=3RHOoZAgKCPcihZphxZmfnnfkd.bnlkhmtw-lljuZbj.nqf@flex--jiaqiyan.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1688761157; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=8FNetjBnnWs4aWcoESXEPv5EQNod9M4H8ra/lwVlWng=; b=42Z1q8+KUgpzC/JBoIxVzPilMrWV0YFx6M78vhXStxYDdwXP95saHwEMl4gOIpgUObFC9W GVPxKlNVkGZD14LCoTZ9E1T30wIriiXegvLpJTS96+p9omVRU+ij5El+HVJlhJQ9qN/irE LhKd+nmDUDsNfUrhtgzo6jzk8nV0r1g= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=VqyQmTQk; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf05.hostedemail.com: domain of 3RHOoZAgKCPcihZphxZmfnnfkd.bnlkhmtw-lljuZbj.nqf@flex--jiaqiyan.bounces.google.com designates 209.85.214.202 as permitted sender) smtp.mailfrom=3RHOoZAgKCPcihZphxZmfnnfkd.bnlkhmtw-lljuZbj.nqf@flex--jiaqiyan.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1688761157; a=rsa-sha256; cv=none; b=LeLBOOZWniwDXBTyKFNd/N9R9YvjURhtTlhzGfTUSPUwbFXZl/EUP4XbtDCr+JEPuFVEGG z4vuwJTI/ATm02KBEzcACSXjawW2ReitSMBFRe8sdM3VvOjWwmr7sV2X8YeVTGm2blEpRe S6RDODto3PIrCtX7aw/31Fk4x5TexwI= Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-1b801e6cebaso31199325ad.0 for ; Fri, 07 Jul 2023 13:19:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1688761156; x=1691353156; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=8FNetjBnnWs4aWcoESXEPv5EQNod9M4H8ra/lwVlWng=; b=VqyQmTQkCzL2+2WReICnzA6BIm0pcsZl2UxgOC96YeMxOiWjK9NhMMsd6sh9vgNsyp l48aVL+YSen8q6QehXxVJmNvqqI169EdhdJF1wP18IrISpvNJpDmE8qvdh8iCozeWtb6 JHgOCmY5zsqIie6UwoBTIJKIq84brRDTo5Ou4o4qfUOH/nsVpBgCFa70jcSCS8JQjRGc YPkr1zj/Al/4tkKYO5OxobtQsE4XPWag4pb/fMTB6fXhhvDh+IoKStTY4VGEtEBkAolL +GRm79XEsc5P3ls1k+A19D7pSp5BkV+9M69nyu7mjSupo4UAMKBhqh3ZJivJ7t7YjP3Z tU8A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688761156; x=1691353156; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=8FNetjBnnWs4aWcoESXEPv5EQNod9M4H8ra/lwVlWng=; b=CwbFJBmyBEz0kfyoxTM9gydOI06Ys1nfDmg+jqk4IaHax2VdN0GpXHFRfrrqnY49SW 2iOYGTSUuBFXVogK0gc0HAv7Z+4NjpEftmD4froYvbRF1S558OoKGuQlATKcUH/EPjE7 a5BT/dTe/p3d5eI3Qp0+sa+KgwkYKuybFb1Kz1cwyhkA2psVN5xNz4YSS79Br3Y2t2Io UahthGty1tqMheLtnR5wL6AgWepL6+bq2GfiDVKXpGwNNX0pguxAdQUS+bkqk9eIMr0M xNpwKjU3LTTuU2ePmQV0OYhFMdi5SQTG8QwE6/GiV0POuz95uFy9ttMEzgnMpNjsyeVX BfxQ== X-Gm-Message-State: ABy/qLY6QJmID6++KPfMC8NRmfwepN/C8/VqJR7iE145TP7IVqZCoidI lfArffFlR9/ih5/ZmAcYVKSNbr+CW7KndQ== X-Google-Smtp-Source: APBJJlF/mGcdbnw0xXdoe2sEjVsVM5DocIm81TfZezvsw+ScgL/1ytfRgEc6ZH2UhLuK+hvQLPJDpV1K7/EAPA== X-Received: from yjq3.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:272f]) (user=jiaqiyan job=sendgmr) by 2002:a17:902:ec88:b0:1b7:fa05:e0c7 with SMTP id x8-20020a170902ec8800b001b7fa05e0c7mr5745441plg.13.1688761156325; Fri, 07 Jul 2023 13:19:16 -0700 (PDT) Date: Fri, 7 Jul 2023 20:19:01 +0000 In-Reply-To: <20230707201904.953262-1-jiaqiyan@google.com> Mime-Version: 1.0 References: <20230707201904.953262-1-jiaqiyan@google.com> X-Mailer: git-send-email 2.41.0.255.g8b1d071c50-goog Message-ID: <20230707201904.953262-2-jiaqiyan@google.com> Subject: [PATCH v3 1/4] mm/hwpoison: delete all entries before traversal in __folio_free_raw_hwp From: Jiaqi Yan To: akpm@linux-foundation.org, mike.kravetz@oracle.com, naoya.horiguchi@nec.com Cc: songmuchun@bytedance.com, shy828301@gmail.com, linmiaohe@huawei.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, duenwen@google.com, axelrasmussen@google.com, jthoughton@google.com, Jiaqi Yan X-Rspamd-Queue-Id: 81213100019 X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: higm45kds5xg3d9y373jmw5c6iep5x8z X-HE-Tag: 1688761157-781286 X-HE-Meta: U2FsdGVkX1/wiXmecZwMlDf3EEs60aDnHI7CaW+BlQhdh98sPCRu2xB319zQm3qjnm8Q6cnTUN2o3IX9H/fQBx/Su/426ZvcfXeTQ2jBcEHDOjyBA+1KG0WUe4mVOZXsA1TX77zxYvM7M0iPlObDEZvvJw+fbqGQlg2fdV6bjdFX3IAXopX9lW1MZRj7wqvbEehyA0Enw88tq8aZGbAIuQ7+VhYiIji7v9Ugl4AHusa3xX7rybZMgFUi+FI+5bUZRscV155s+m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t5yBgTYG5Orow5eIGi4qV0mRAP6JhzuMd+JQRRIZmSObwUnTqK/+9fUYlZ2KCJKNenoBbsCTyCgoHB/JVDLy/JcgZ1kYEyPTQq8zo67jE2gDGt1s79WlAi+9/5EKXttII92g2Pmep8b30NapVdlPt/GNb52FkEVY5/UA+XFkuuWy01hIZ5EtjLjBpg+2BhcPyLWwBVWPSbUECOG6T6OBr+7hMili8o5oU8rVryVnVgwPNHhdUPV4Css9r4zTcOgN2/Dhnr7gxHHbdm2DFXoFVIAwVd9Kx28L/kfxP2/D564KhliNg5r0+bCVtLizvCNDTsh/wGElfIXr+x2juq2LCiocMqhiCk4JR6a85utSQPatLni4V+xqKw== X-Bogosity: Ham, tests=bogofilter, spamicity=0.002003, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Traversal on llist (e.g. llist_for_each_safe) is only safe AFTER entries are deleted from the llist. Correct the way __folio_free_raw_hwp deletes and frees raw_hwp_page entries in raw_hwp_list: first llist_del_all, then kfree within llist_for_each_safe. As of today, concurrent adding, deleting, and traversal on raw_hwp_list from hugetlb.c and/or memory-failure.c are fine with each other. Note this is guaranteed partly by the lock-free nature of llist, and partly by holding hugetlb_lock and/or mf_mutex. For example, as llist_del_all is lock-free with itself, folio_clear_hugetlb_hwpoison()s from __update_and_free_hugetlb_folio and memory_failure won't need explicit locking when freeing the raw_hwp_list. New code that manipulates raw_hwp_list must be careful to ensure the concurrency correctness. Acked-by: Mike Kravetz Acked-by: Naoya Horiguchi Signed-off-by: Jiaqi Yan Reviewed-by: Miaohe Lin --- mm/memory-failure.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/mm/memory-failure.c b/mm/memory-failure.c index e245191e6b04..a08677dcf953 100644 --- a/mm/memory-failure.c +++ b/mm/memory-failure.c @@ -1829,12 +1829,11 @@ static inline struct llist_head *raw_hwp_list_head(struct folio *folio) static unsigned long __folio_free_raw_hwp(struct folio *folio, bool move_flag) { - struct llist_head *head; - struct llist_node *t, *tnode; + struct llist_node *t, *tnode, *head; unsigned long count = 0; - head = raw_hwp_list_head(folio); - llist_for_each_safe(tnode, t, head->first) { + head = llist_del_all(raw_hwp_list_head(folio)); + llist_for_each_safe(tnode, t, head) { struct raw_hwp_page *p = container_of(tnode, struct raw_hwp_page, node); if (move_flag) @@ -1844,7 +1843,6 @@ static unsigned long __folio_free_raw_hwp(struct folio *folio, bool move_flag) kfree(p); count++; } - llist_del_all(head); return count; }