Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp4837830pxj; Wed, 9 Jun 2021 03:09:59 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzUY/NRuAweo/ElvSdaTvTuy+BZBWOKtUJE2Me3/3SahptlmJsZwoN+T+ekQd8qhJTI2v/B X-Received: by 2002:a50:8e18:: with SMTP id 24mr30150002edw.275.1623233399581; Wed, 09 Jun 2021 03:09:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623233399; cv=none; d=google.com; s=arc-20160816; b=nsv/6JAvI6HpdAaFHojz4c81B0ZC+c3K1bRCfXMDg1q24rxIPKR+T16+0D/HVLCTzV VJVRbQVPgpWMHngynnAw+FTJXF0m0LP0CpKhoZOQHVQvvOHCtRP/V0qKppOxWZ/yaCfU 0aXfjXK0FBB7MiCH3qsMC6keF3lubmlicljb8sY02iWMtrp/VTS4y71d9RhMJ5nwnxkh 7h8VWZn0e2BubI3MeoCBGUrRWVsyLxS67SHJoYTKx8PB9yqajxysVSC6b2W/oEd6d19+ /ZX8nXtzqo+3xO5jTv+TcRff0aFx2Tk4WLmG5p1JkNbq4YAZ5Sc76rncEM/aDvnBP1kS DHJg== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=8e3VIY4TsySVmVKt3wMqcUckpuXYN2s6iiCk7iWbvpw=; b=cFldR2a4v7XvGZZ3p+FQHAo/s8vJfhRU00gJi3Pl7hgrQz9ykZ5P5gwKFGIVXm/oAJ UVPrgWtnTJ5dK9nceJx8sqg1U7+EqYFPeJsKnKP4jg/j0eJPBw+no1AExQU2xEmtzfvu dTDW1VqWtxl/inPQ0xHgEmqoSaPgysU8FBO+b5ZWD4mTgMk4aVJyZPUXmCUP4lyGUFfT dn4ZV57i6atoqWt/cQGdGgcIiWH5nEekiqd/nJD9eFnUOAi7JJjWbTbSZ459C072o7BM uQGChQV92+rQLB3PWu6SOSzV8ehjJ8oni0YWXP2KveZ9skKKbpKNst/uxtydV31tmgiU l5xQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=xfjl0Ifj; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id o2si1972826edc.561.2021.06.09.03.09.36; Wed, 09 Jun 2021 03:09:59 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=xfjl0Ifj; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236292AbhFHSxF (ORCPT + 99 others); Tue, 8 Jun 2021 14:53:05 -0400 Received: from mail.kernel.org ([198.145.29.99]:42400 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235688AbhFHSrI (ORCPT ); Tue, 8 Jun 2021 14:47:08 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 3B11361417; Tue, 8 Jun 2021 18:38:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1623177481; bh=4FcyaguISM1PgqqomZKvZuyeUf7ahnUYCUJk4+qmEZA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=xfjl0IfjYSemaaOqsPEHIRTfUsExeWkQJcfWOumTJX1NSNE7fS7utTZZ9qF22UptG vQIgz0xVj8GXl1IqoJz1GnaU6d6gopucpmQ15ZawOIcL/QIAPcksFBEP7KuDekZU5t iqJJAob60SUKnLY3PF4LLjgQMFvbWXrc4OWH4v+g= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Greg Kroah-Hartman , "Matthew Wilcox (Oracle)" , Andrew Morton , "Kirill A . Shutemov" , Qian Cai , Song Liu , Linus Torvalds Subject: [PATCH 5.4 66/78] XArray: add xa_get_order Date: Tue, 8 Jun 2021 20:27:35 +0200 Message-Id: <20210608175937.496153693@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210608175935.254388043@linuxfoundation.org> References: <20210608175935.254388043@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: "Matthew Wilcox (Oracle)" commit 57417cebc96b57122a2207fc84a6077d20c84b4b upstream Patch series "Fix read-only THP for non-tmpfs filesystems". As described more verbosely in the [3/3] changelog, we can inadvertently put an order-0 page in the page cache which occupies 512 consecutive entries. Users are running into this if they enable the READ_ONLY_THP_FOR_FS config option; see https://bugzilla.kernel.org/show_bug.cgi?id=206569 and Qian Cai has also reported it here: https://lore.kernel.org/lkml/20200616013309.GB815@lca.pw/ This is a rather intrusive way of fixing the problem, but has the advantage that I've actually been testing it with the THP patches, which means that it sees far more use than it does upstream -- indeed, Song has been entirely unable to reproduce it. It also has the advantage that it removes a few patches from my gargantuan backlog of THP patches. This patch (of 3): This function returns the order of the entry at the index. We need this because there isn't space in the shadow entry to encode its order. [akpm@linux-foundation.org: export xa_get_order to modules] Signed-off-by: Matthew Wilcox (Oracle) Signed-off-by: Andrew Morton Cc: "Kirill A . Shutemov" Cc: Qian Cai Cc: Song Liu Link: https://lkml.kernel.org/r/20200903183029.14930-1-willy@infradead.org Link: https://lkml.kernel.org/r/20200903183029.14930-2-willy@infradead.org Signed-off-by: Linus Torvalds Signed-off-by: Greg Kroah-Hartman --- include/linux/xarray.h | 9 +++++++++ lib/test_xarray.c | 21 +++++++++++++++++++++ lib/xarray.c | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+) --- a/include/linux/xarray.h +++ b/include/linux/xarray.h @@ -1470,6 +1470,15 @@ void xas_pause(struct xa_state *); void xas_create_range(struct xa_state *); +#ifdef CONFIG_XARRAY_MULTI +int xa_get_order(struct xarray *, unsigned long index); +#else +static inline int xa_get_order(struct xarray *xa, unsigned long index) +{ + return 0; +} +#endif + /** * xas_reload() - Refetch an entry from the xarray. * @xas: XArray operation state. --- a/lib/test_xarray.c +++ b/lib/test_xarray.c @@ -1649,6 +1649,26 @@ static noinline void check_account(struc #endif } +static noinline void check_get_order(struct xarray *xa) +{ + unsigned int max_order = IS_ENABLED(CONFIG_XARRAY_MULTI) ? 20 : 1; + unsigned int order; + unsigned long i, j; + + for (i = 0; i < 3; i++) + XA_BUG_ON(xa, xa_get_order(xa, i) != 0); + + for (order = 0; order < max_order; order++) { + for (i = 0; i < 10; i++) { + xa_store_order(xa, i << order, order, + xa_mk_index(i << order), GFP_KERNEL); + for (j = i << order; j < (i + 1) << order; j++) + XA_BUG_ON(xa, xa_get_order(xa, j) != order); + xa_erase(xa, i << order); + } + } +} + static noinline void check_destroy(struct xarray *xa) { unsigned long index; @@ -1697,6 +1717,7 @@ static int xarray_checks(void) check_reserve(&array); check_reserve(&xa0); check_multi_store(&array); + check_get_order(&array); check_xa_alloc(); check_find(&array); check_find_entry(&array); --- a/lib/xarray.c +++ b/lib/xarray.c @@ -1592,6 +1592,46 @@ unlock: return xas_result(&xas, NULL); } EXPORT_SYMBOL(xa_store_range); + +/** + * xa_get_order() - Get the order of an entry. + * @xa: XArray. + * @index: Index of the entry. + * + * Return: A number between 0 and 63 indicating the order of the entry. + */ +int xa_get_order(struct xarray *xa, unsigned long index) +{ + XA_STATE(xas, xa, index); + void *entry; + int order = 0; + + rcu_read_lock(); + entry = xas_load(&xas); + + if (!entry) + goto unlock; + + if (!xas.xa_node) + goto unlock; + + for (;;) { + unsigned int slot = xas.xa_offset + (1 << order); + + if (slot >= XA_CHUNK_SIZE) + break; + if (!xa_is_sibling(xas.xa_node->slots[slot])) + break; + order++; + } + + order += xas.xa_node->shift; +unlock: + rcu_read_unlock(); + + return order; +} +EXPORT_SYMBOL(xa_get_order); #endif /* CONFIG_XARRAY_MULTI */ /**