From patchwork Tue Jan 28 07:52:50 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: sooraj X-Patchwork-Id: 13951714 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 DBB69C02188 for ; Mon, 27 Jan 2025 21:22:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 535332801AD; Mon, 27 Jan 2025 16:22:45 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4E6742801A8; Mon, 27 Jan 2025 16:22:45 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3AD162801AD; Mon, 27 Jan 2025 16:22:45 -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 1C0312801A8 for ; Mon, 27 Jan 2025 16:22:45 -0500 (EST) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id C9B081A0725 for ; Mon, 27 Jan 2025 21:22:44 +0000 (UTC) X-FDA: 83054506248.05.164ECB2 Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.179]) by imf26.hostedemail.com (Postfix) with ESMTP id 1065B140007 for ; Mon, 27 Jan 2025 21:22:42 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=fbo3pxv6; spf=pass (imf26.hostedemail.com: domain of sooraj20636@gmail.com designates 209.85.214.179 as permitted sender) smtp.mailfrom=sooraj20636@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1738012963; 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-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=q2c4Y+zJzSFkeoM7BimIXqDazwK2VMilP//BNghBsG8=; b=xjFKRyfiiJry6m0D1nTKh3gQCqjwFTHEVeNIkkUQjtNmQDc5gPAqi1N+f4DKRAgfe1wex9 q1Ipwr0w287G791NWj8irzMo6cPjoGA+MmAMblf9XxLCT7USTGPYTrW+eswOKH0imEYhvU S7cnBsLKMSureTt/YWtQvlnzxya4FJs= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=fbo3pxv6; spf=pass (imf26.hostedemail.com: domain of sooraj20636@gmail.com designates 209.85.214.179 as permitted sender) smtp.mailfrom=sooraj20636@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1738012963; a=rsa-sha256; cv=none; b=mBZh7hk6x2V0xN30AMXM8IOo6h6qJxjqB7j+hJwvsS94NJyKo6J2v+uQJgw4WtjCWBAbfS EONh1Of+9GQV26Pj6jbaYFiu/R3wOsVoQujskH5PphVk2Zbx6dZy6SQuDIoDS0tIQ85fRB 49YunpMK0psFmy+wNmLi4fgKM86imLM= Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-2163b0c09afso90016355ad.0 for ; Mon, 27 Jan 2025 13:22:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1738012961; x=1738617761; darn=kvack.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=q2c4Y+zJzSFkeoM7BimIXqDazwK2VMilP//BNghBsG8=; b=fbo3pxv6uC1zFETwsmU8XHCnjkMgrl6RQRXuwYUkveamkx1Ywzmk1lDBtx3kBWF7II 23XljrIvUh2elOuFVY84PQNafEbnt21l57FvwcEAOKZ2A7jU6265P0CdCVvsp601ldyv nqzFD24vbYeOq5szhuTzsxnkmQKrFw21ZSVKcsKPLG/RZnTSx/Pzl/QzevEpm+TyZncN AF+weXCE6aXRgBQ1SpeEuGhrCYCqbTQ6epZMJeZ4GdG5+LiNkwoGQOZOaEdTQ1+9CPn2 9O6X4NGxYz0hXFaerKBQEq1EYpU8fNbChMY7z5ZnxiOYvHfnAAHV+bUTGPL3edYxaMjB HLWQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738012961; x=1738617761; 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=q2c4Y+zJzSFkeoM7BimIXqDazwK2VMilP//BNghBsG8=; b=w8DWEhgLcgn1AzXzxGtJyJ7sR9znqeZwXHuD/OEgexE4PnPdaYfOsCh29kLh2fIz4O gbBqWnZv0ee0uh18btrAkwtw/uZmVi/fW7opV36MeTyj+KfXNwlmTbaJIHcwbXD71oUn cKGvL5hSAfu1CLGS/pHD4DI3J8KJ62U/utEFQ8v+n0xHwgRJJMEppLYePRrBc35HpE9/ J/Pgweh3fiPdOZeXrZ2GrN0egkoscFuquWf5eJW94zPfgtseFVZx0gh+uVNVc8FpHpml hqBGqqtxQDG0/m6mLqIHGVmfxsH3fCnYVz8CuHVhkP2oYT7WCJoEjYXcqTrHvXg3+C6L TQuw== X-Gm-Message-State: AOJu0Ywh91ZE6tkKUB9YNE/Of99iOXRDpWL48x8dAWYVhE5StHA1awOM 4xoKUyF8hcqQ36gEzA7ZGov6SrYrBVpnU12LnzZ9osaL631vC7xJvcKyzA== X-Gm-Gg: ASbGncu4b8y3ILlFHuadwZPqK1BDVonlGDfZSrifAQChk4Cipmy6M2MFt/FL0Dt84E8 Abpa08+gO/ANNZnoBRb3bb5gn4uPBjdDLd/pcSG+iU/AFUceomX42GabSLqveql/ok36KhdTYPL EghEFFwDjFeLFN7XHiy5gWcfLKa1DZBSO8ek74QrZqs3dNZO7iJ2jetzPqmEEdp+w4yXV8OJlKs o7PKWaVoSXer0CSlJgWPIgEQEsI/hI5UFbqVQRFQ4FsukBDfNhd2fRx1ixBpjAJMwUBL/tT2g4A OFo= X-Google-Smtp-Source: AGHT+IEBf/w+kxzimghj+GslIyvkCmYgwRi9is9CC3pR8qfipmiUKjdYcbNnsxYMVNnKUm36H2xUZA== X-Received: by 2002:a17:902:f54e:b0:216:69ca:7714 with SMTP id d9443c01a7336-21c352c7a86mr629661405ad.11.1738012961353; Mon, 27 Jan 2025 13:22:41 -0800 (PST) Received: from localhost ([103.28.246.23]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21da424ef28sm67967865ad.258.2025.01.27.13.22.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Jan 2025 13:22:40 -0800 (PST) From: sooraj To: linux-mm@kvack.org Cc: sooraj Subject: [PATCH] mm/bdi: fix race between cgwb_create and conflicting blkcg associations Date: Tue, 28 Jan 2025 02:52:50 -0500 Message-ID: <20250128075250.11500-1-sooraj20636@gmail.com> X-Mailer: git-send-email 2.45.2 MIME-Version: 1.0 X-Rspamd-Queue-Id: 1065B140007 X-Stat-Signature: j4j6he9q1fbk36u8drs1sbjg6h6hiqwg X-Rspamd-Server: rspam08 X-Rspam-User: X-HE-Tag: 1738012962-9299 X-HE-Meta: U2FsdGVkX18j9dmVCk8JvLiiX+/b/vRN6xr/3CA2rIurjKgE/CgMaO8C++trRTPavhY0lc8OI/ys2gIEBCmll9DMl/WiNz9jQmqBlptTIrpE6zr+ZThunUWL5ai+so1WApfF2rFcMG2hdoe1z9QLYytS7hwl8+u2reD7I4a4cn2v6X2ANhi/Qra6ZZkti8fqhp+u1g/wJ9tDag7gxoKzEnWEQLi4qFOtsKsVLXsPILFW9Ww2GeSx8vCMSgAoyubOW5vLlPwfOZjP6tVwWbbgS/UQTql+ATNB2eHhU1L4yE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dmIKT24mSTf8UW5XQrfYYtkukFIvI/2aDErms2OGRkIIS3oASUDZtq1zAh7R9Iw/SH/jaPg1LVnWNK2dNbk/MQIO8+p5SjWWkqz/BwYUwqfGu+cJwKwPMnZBiHbVZGhicaCGe51Bva9og8kiFMHBUTIc3MCKhXpRQx//gq0U6D9f6myAhMfqeX8xkl4+iSGWMbErftG4TRIbzFVcTyVgvXWrskb2ZxWMFVC3MyxLNXRSzE/ZGUr5xHV07Bnma/d7ytyMXy0XZYYL9/EWQqEvA0xsEp/Ols6HgQ8HL0= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000053, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Ensure cgwb (cgroup writeback) structures are uniquely associated with a memcg-blkcg pair to prevent inconsistencies when concurrent cgwb_create calls race. This resolves a scenario where two threads creating cgwbs for the same memory cgroup (memcg) but different I/O control groups (blkcg) could insert conflicting entries. The fix rechecks for existing cgwbs under the cgwb_lock spinlock after initial creation. If a conflicting cgwb (same memcg, different blkcg) is found, it is killed before inserting the new entry. This guarantees a 1:1 relationship between memcg-blkcg pairs and their cgwbs, preserving system invariants. Signed-off-by: sooraj --- mm/backing-dev.c | 43 +++++++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/mm/backing-dev.c b/mm/backing-dev.c index e61bbb1bd622..67acb565e9a7 100644 --- a/mm/backing-dev.c +++ b/mm/backing-dev.c @@ -723,24 +723,39 @@ static int cgwb_create(struct backing_dev_info *bdi, spin_lock_irqsave(&cgwb_lock, flags); if (test_bit(WB_registered, &bdi->wb.state) && blkcg_cgwb_list->next && memcg_cgwb_list->next) { - /* we might have raced another instance of this function */ - ret = radix_tree_insert(&bdi->cgwb_tree, memcg_css->id, wb); - if (!ret) { - list_add_tail_rcu(&wb->bdi_node, &bdi->wb_list); - list_add(&wb->memcg_node, memcg_cgwb_list); - list_add(&wb->blkcg_node, blkcg_cgwb_list); - blkcg_pin_online(blkcg_css); - css_get(memcg_css); - css_get(blkcg_css); + /* Re-check under lock to handle races */ + struct bdi_writeback *existing; + + existing = radix_tree_lookup(&bdi->cgwb_tree, memcg_css->id); + if (existing) { + if (existing->blkcg_css != blkcg_css) { + cgwb_kill(existing); + existing = NULL; + } else { + ret = 0; /* Already exists, treat as success */ + } + } + + if (!existing) { + ret = radix_tree_insert(&bdi->cgwb_tree, memcg_css->id, wb); + if (!ret) { + list_add_tail_rcu(&wb->bdi_node, &bdi->wb_list); + list_add(&wb->memcg_node, memcg_cgwb_list); + list_add(&wb->blkcg_node, blkcg_cgwb_list); + blkcg_pin_online(blkcg_css); + css_get(memcg_css); + css_get(blkcg_css); + } } } spin_unlock_irqrestore(&cgwb_lock, flags); - if (ret) { - if (ret == -EEXIST) - ret = 0; + + if (!ret) + goto out_put; + if (ret == -EEXIST) + ret = 0; /* Lost race, another thread created the same wb */ + else goto err_fprop_exit; - } - goto out_put; err_fprop_exit: bdi_put(bdi);