From patchwork Mon Jan 30 20:49:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: andrey.konovalov@linux.dev X-Patchwork-Id: 13121827 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 63ACFC54EAA for ; Mon, 30 Jan 2023 20:52:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 847B56B0080; Mon, 30 Jan 2023 15:52:01 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 70B786B0085; Mon, 30 Jan 2023 15:52:01 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 474F16B0087; Mon, 30 Jan 2023 15:52:01 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 30C3F6B0080 for ; Mon, 30 Jan 2023 15:52:01 -0500 (EST) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id E9B59C03C9 for ; Mon, 30 Jan 2023 20:51:59 +0000 (UTC) X-FDA: 80412662358.29.74F6374 Received: from out-14.mta0.migadu.com (out-14.mta0.migadu.com [91.218.175.14]) by imf24.hostedemail.com (Postfix) with ESMTP id 2E15F180002 for ; Mon, 30 Jan 2023 20:51:57 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=wXtDfrkY; spf=pass (imf24.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.14 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1675111918; 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:in-reply-to:references:references:dkim-signature; bh=HatpIAXF9ywMATjIlRO5zkwkJoWVcZESn/qWFP01cUE=; b=czzdGzdZBnICvRqiavSp1vhttbl4lcauZzthOQgeZZWuv84mUGeJV09vCHQvCtXqTs06M5 v9f1UEGJWJXEXD6tqksffq4vuNEzDdrCJNpf7RkIxGGhhBZBZqVEGD8eO3VrXa4+dWO9+Y gkSaTDY0LeHGIeaPDwqsKFL1eW1Kjjs= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=wXtDfrkY; spf=pass (imf24.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.14 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1675111918; a=rsa-sha256; cv=none; b=DVAjSRdiTkr9kNPjv6xaSv7As/NRXjqakS6P0QBC6c7xef8bP1HzZG95Q+/cq7YrnQc05+ 7iTygQ8IqY0OSyHil7o7B8k3SZLjwifjqbVRMDQ0dNLy6k6bbUAziCDSf5OEYIkSlSuylr msBKXZzLW48bmZNGWMXBNrWDTO58P6Y= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1675111916; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=HatpIAXF9ywMATjIlRO5zkwkJoWVcZESn/qWFP01cUE=; b=wXtDfrkYlD8IpL/lQ8l++bN1YdtjxhnwL1anSh49Js2VOzOsVnOBIPbJSklgemF2REahtq DvdvaNag4m/GqwVZu03XHDBsOWpGjVLbIuihC73epl6av328Yy8aLHk/DlbB90DLAwCqg2 4Z44QfKvcXgws9zhTONc9RpzBkBuxac= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Vlastimil Babka , kasan-dev@googlegroups.com, Evgenii Stepanov , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH 16/18] lib/stackdepot: annotate racy slab_index accesses Date: Mon, 30 Jan 2023 21:49:40 +0100 Message-Id: <19512bb03eed27ced5abeb5bd03f9a8381742cb1.1675111415.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 2E15F180002 X-Stat-Signature: npe6b3bpgrpkpku1yn7eb43o5babqd1p X-Rspam-User: X-HE-Tag: 1675111917-451044 X-HE-Meta: U2FsdGVkX1+v0qAcZlGonVIn1sPsqrdSU65ffC/bS0LmJc4RruTnTJhbw5C2ynlrNMKaIU7/GpMyKUbGwVKSwwoMdgdU+FmYFp2jzVE8byVhM7t/V55/M7NhPsi1RQHD/zrKVo3eIbzS3jRvmQFNesQ/0BKmzvH4yiDzmYt+tD6nVYAs/YgBekdrgfmR+hUrVjisqi+63zbafclFRPUP+hb8epe43a9WT+yHZnM1WVIuLaQpn3B9YubNeEFxbvYvmIUkpyFTuzfWmDlq8V+iOB9xJiO+A3fB7cnTsrZbRLwK15GONW1CsKTyOnVykVav9LQu0jCsDx1OCSEY7088zVWEUHkHmxvDUYR2zk51HgLK6dROb2ZKyniFJh9TVwoRPRtwl33pMn+ExIvF9lsRzlZnLJsgUvegK6wZWjUzu0V3yiJT4T7eSFWjeC/fHQeSRqwFeJ+QUtBuhq29WdGRBu5K6xEb4uODFR1Tk6EPrecHg3uEuZqQ00sCKSlilUtwosPyEzBstK600Y/Y0fMfb5s2yTi13pIpeu42AJLjFjP4bHUvaLhBFkApP+ygZN2+9PJqs+rV5Ivv3sIRsX4B073B87ltqgspimKXnKyprM70cKHGFOSKNejDDds8Ed6TIJL5VbbCnLM3c+8wOvDnSdjHwDTtiwn0SlzOvGjLDQxz6pjpwXOVt2dA6kHtQzQw65fnCn30Bdw6KGdCjosHrdONVjRBRrFxTRAEVCrfEkgmgTzj+9WNQ84uOJkdtgprnceZ7zN4licR6ufXYo00hQMdedhdMUCOP62ynwNjG7NM6659Z17tV2ddANVKugAp+Mc2Cm7mxozJRj3Yui1X6R12L7KUC/tqoUR2++9zNvYhCKWMl0u2D8T8ipFPQtvtpUNBLXLHGQxEeFCVGifPlWIq2glEkNjv1TvWh609tRbq8q5Cf3WlTD/sjYEFlBdufENG/QOPqsqb8pdVAaK lJSZ45gA v4Arzl5wa0x9f3a3nM5/ptFqsDpMMmCtU+oF7jklPA7ynpnTB72EdI/AIX7767XoVOJkUQuaeNmJiMNMzlq1+4uF53VVkgFhVMsi8+L/SsH4Sp4BbbMCgQSyYYKETKwMGjdktx9WkGiTgRqpxYj31pPLvUQ== 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: From: Andrey Konovalov Accesses to slab_index are protected by slab_lock everywhere except in a sanity check in stack_depot_fetch. The read access there can race with the write access in depot_alloc_stack. Use WRITE/READ_ONCE() to annotate the racy accesses. As the sanity check is only used to print a warning in case of a violation of the stack depot interface usage, it does not make a lot of sense to use proper synchronization. Signed-off-by: Andrey Konovalov Signed-off-by: Andrew Morton --- lib/stackdepot.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/stackdepot.c b/lib/stackdepot.c index f291ad6a4e72..cc2fe8563af4 100644 --- a/lib/stackdepot.c +++ b/lib/stackdepot.c @@ -269,8 +269,11 @@ depot_alloc_stack(unsigned long *entries, int size, u32 hash, void **prealloc) return NULL; } - /* Move on to the next slab. */ - slab_index++; + /* + * Move on to the next slab. + * WRITE_ONCE annotates a race with stack_depot_fetch. + */ + WRITE_ONCE(slab_index, slab_index + 1); slab_offset = 0; /* * smp_store_release() here pairs with smp_load_acquire() in @@ -492,6 +495,8 @@ unsigned int stack_depot_fetch(depot_stack_handle_t handle, unsigned long **entries) { union handle_parts parts = { .handle = handle }; + /* READ_ONCE annotates a race with depot_alloc_stack. */ + int slab_index_cached = READ_ONCE(slab_index); void *slab; size_t offset = parts.offset << DEPOT_STACK_ALIGN; struct stack_record *stack; @@ -500,9 +505,9 @@ unsigned int stack_depot_fetch(depot_stack_handle_t handle, if (!handle) return 0; - if (parts.slab_index > slab_index) { + if (parts.slab_index > slab_index_cached) { WARN(1, "slab index %d out of bounds (%d) for stack id %08x\n", - parts.slab_index, slab_index, handle); + parts.slab_index, slab_index_cached, handle); return 0; } slab = stack_slabs[parts.slab_index];