From patchwork Tue Sep 24 12:39:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wei Yang X-Patchwork-Id: 13810898 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 BCA56CF9C6F for ; Tue, 24 Sep 2024 12:41:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7378E6B0098; Tue, 24 Sep 2024 08:41:46 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6E7276B0099; Tue, 24 Sep 2024 08:41:46 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 587DB6B009A; Tue, 24 Sep 2024 08:41:46 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 365316B0098 for ; Tue, 24 Sep 2024 08:41:46 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id E744641A20 for ; Tue, 24 Sep 2024 12:41:45 +0000 (UTC) X-FDA: 82599593370.13.DD43788 Received: from mail-lf1-f51.google.com (mail-lf1-f51.google.com [209.85.167.51]) by imf29.hostedemail.com (Postfix) with ESMTP id 2158712000F for ; Tue, 24 Sep 2024 12:41:43 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=JtQOMD1y; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf29.hostedemail.com: domain of richard.weiyang@gmail.com designates 209.85.167.51 as permitted sender) smtp.mailfrom=richard.weiyang@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1727181689; a=rsa-sha256; cv=none; b=AQ7oJos9eeeBdHls5SNvdZtY3Ci+S6ylHjAXTeGV2nLWfoGJQUlY24cmW5oCK8dG9NI8Wz ukQ6v0J9q06S/3QzR4gqImrlCscluARKQoNzXEVZDYiA3w9tNsMLWM9svvuBEc7ufCzRDl LnbDjLZMYpq/l/dj2pXIOd+ZP6Qcgnw= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=JtQOMD1y; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf29.hostedemail.com: domain of richard.weiyang@gmail.com designates 209.85.167.51 as permitted sender) smtp.mailfrom=richard.weiyang@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1727181689; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:content-type: content-transfer-encoding:in-reply-to:in-reply-to: references:references:dkim-signature; bh=N+Q8uXR3KF+LFZVg2KeHpqpEsXhd/DDR99/6oIBSVFg=; b=n+RkxoHnPA4TrLqSEYXBHNUADS4zFf16B5RUMfvA0Dhu75myFSDDvNbwdAlVtuOALbuA31 9XJC1E+ZzYh+4OL+FyNtyraGrfMyT8LjkSar1akJpI5hMqJE2Y8DOa29ojQ08Iqhg+K7kD 9L1KlanR7eIfGwLWnWgTcPPYsD9dui8= Received: by mail-lf1-f51.google.com with SMTP id 2adb3069b0e04-535be093a43so6721450e87.3 for ; Tue, 24 Sep 2024 05:41:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1727181702; x=1727786502; darn=kvack.org; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=N+Q8uXR3KF+LFZVg2KeHpqpEsXhd/DDR99/6oIBSVFg=; b=JtQOMD1ya3ywG21MwuUE6MPhE4tZloaG2H+mAx1EY07Eh2cdN186sxLyacQoHRrfaL zwRXUrO0MZ86dPFvd5sYqaUGrtOIUY9WMfd+wvi+FIOOpNx28WbM/ydrL4uBRPBI0tmC DHhjJ4VGk4cgYfBGd7qLnZS+d02q1rp/sVLibuMnA1eGcP/OjIsTXBQXml/W6esDhe/q dEuo5/YJNK+fWrpBSH+NnsEiTA9JYImAad0fpqdvaNMPUC4Bxk1bz6jLuyebrzWjZE/e xq7mu1Bk29IEtvQufna+M2oFTGkaD9rccOVO/+8ddiMIjuClniM3X6B0g/HvQ4hYeY/Z unMg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727181702; x=1727786502; h=references:in-reply-to:message-id:date:subject:cc:to:from :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=N+Q8uXR3KF+LFZVg2KeHpqpEsXhd/DDR99/6oIBSVFg=; b=vJsTy9G/APmySPKg9bIGgxnFB8xvfAizDFVzqf95yYTeiWZSaKiF9jnSFWM6NveYTf Ds8RtKalMQJsSzNhFRjCkaNS7NTd6rZcG73uQDDGwFahf9I4e+Ao6VxCeUwMNd4YkvbP vRpJMoqTaYUAKQkWMDYDu+fh/gUazX7LLpYetRPHnIWpLcKJE0m2sV/COBxgO/M4lEFF 079Rys0Cy2qX1bmISjRmVbLb8Gtez7ANDPgIJS400XQ+9CzTy6+wavahPXJ1n6rpKm+Q lwpvsCcZCKrTJjzMin3w2ZUZ5yoj+WDN9xVlMNEV02V4GzFJtNJGxiQW4N6ZDkOWXCUE 2epw== X-Forwarded-Encrypted: i=1; AJvYcCVRyq0++OnMB6FgGMPf3MxT/ue/Z/0thSiX7XxisFf3mj+tz6fIowi/vjHzvVCfzfDm9mKco5BAow==@kvack.org X-Gm-Message-State: AOJu0Yyx8R9el2PmPmKRKZqBMjs3usyzLZrHN4MNeWAOnD+o4fuq6tYk 3E0ht3ojgubgNWxp1ANcyUO11u/ud5C/bJn/OXEiP/TI6CnVCFlt+/ZvYeSq X-Google-Smtp-Source: AGHT+IFC23anDxrBlCXEF3fnkai7bSJ7FqtvuC6+XpVduVYlswcgim0J9o4MTyHSDyJ1LRFZFi8AZQ== X-Received: by 2002:a05:6512:12d1:b0:52c:c032:538d with SMTP id 2adb3069b0e04-536ac2f466amr8144316e87.27.1727181702153; Tue, 24 Sep 2024 05:41:42 -0700 (PDT) Received: from localhost ([185.92.221.13]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-5c5cf4d8960sm686824a12.97.2024.09.24.05.41.40 (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 24 Sep 2024 05:41:41 -0700 (PDT) From: Wei Yang To: Liam.Howlett@oracle.com, akpm@linux-foundation.org Cc: maple-tree@lists.infradead.org, linux-mm@kvack.org, Wei Yang , "Liam R . Howlett" , Sidhartha Kumar Subject: [PATCH 4/4] maple_tree: fix potential allocation failure even has memory Date: Tue, 24 Sep 2024 12:39:54 +0000 Message-Id: <20240924123954.18933-5-richard.weiyang@gmail.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20240924123954.18933-1-richard.weiyang@gmail.com> References: <20240924123954.18933-1-richard.weiyang@gmail.com> X-Rspam-User: X-Stat-Signature: mofbuh4kaw6wgtfwmsuds97p3sh8neut X-Rspamd-Queue-Id: 2158712000F X-Rspamd-Server: rspam02 X-HE-Tag: 1727181703-27281 X-HE-Meta: U2FsdGVkX1/CCmcoqTKFf3Utxfseo+Uc0hWxiL4mhEy61MAr02tu6qNVYfSNsH67an4WTSBu05BnL0jzhgOR6VdEx3FajL+SwyJilGhrUVKPuS8Va9nXyls+rht0JztIAaVVBXoRjZVXR9jwqqD+3c2hq4lqega47EweuShfNQ/bcVudZR770aRlh6EymrcPgOMKLEIK6c2yFgje+Sz+szHDTeXNtniHEVWa6Yqnck5lo6dzZs/fco0roL5gpEPGdeTeZm39F2c7l7kw+xriBVj/oKZt5rU5lODWp+jtEbNDC8WD+77gS5Q7kTq+E96XwX0Eg9FcOSCiiEOOvD89z9SG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ViBa9pqSdENNwToDsguuP8HjqZ7jpYxcm0qDxunzLR2xCajco71v/GbMl4L6/j69cEU0lPEv0Pp+Wz9Ew+z4MQCDiDVuuCp98STaU 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: List-Subscribe: List-Unsubscribe: We got an rare case when mas_node_count() would fail even there is enough memory. The reason is the maple_alloc grows downward. And when hit a full maple_alloc, the max_req would be 0. This leads to mt_alloc_bulk() return 0, which means failure here. For example, here is the test code: expect = MAPLE_ALLOC_SLOTS + 1; mas_node_count(&ms, expect); pr_info("expect %d allocated %lu\n", expect, mas_allocated(&ms)); expect = MAPLE_ALLOC_SLOTS * 2 + 2; mas_node_count(&ms, expect); pr_info("expect %d allocated %lu\n", expect, mas_allocated(&ms)); We will get the following output, which shows we fail to allocate the required number of nodes. expect 31 allocated 31 expect 62 allocated 61 The straight forward way to fix it is go down one level more. Signed-off-by: Wei Yang CC: Liam R. Howlett CC: Sidhartha Kumar --- lib/maple_tree.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/maple_tree.c b/lib/maple_tree.c index 1cbc5f7ca40d..dd33d0793dd1 100644 --- a/lib/maple_tree.c +++ b/lib/maple_tree.c @@ -1253,8 +1253,10 @@ static inline void mas_alloc_nodes(struct ma_state *mas, gfp_t gfp) } node = mas->alloc; - while (requested) { + for (; requested; node = node->slot[0]) { max_req = MAPLE_ALLOC_SLOTS - node->node_count; + if (unlikely(!max_req)) + continue; slots = (void **)&node->slot[node->node_count]; max_req = min(requested, max_req); count = mt_alloc_bulk(gfp, max_req, slots); @@ -1268,7 +1270,6 @@ static inline void mas_alloc_nodes(struct ma_state *mas, gfp_t gfp) node->node_count += count; allocated += count; - node = node->slot[0]; requested -= count; } mas->alloc->total = allocated;