Received: by 10.223.148.5 with SMTP id 5csp6400249wrq; Wed, 17 Jan 2018 13:10:17 -0800 (PST) X-Google-Smtp-Source: ACJfBotGJNK/l5QYMpVHQalPdjxMU0XevsVIEO35YL//Tcn2DWpA4Jb16KjablTle73mmcYsHuPU X-Received: by 10.99.114.81 with SMTP id c17mr23648898pgn.173.1516223417325; Wed, 17 Jan 2018 13:10:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1516223417; cv=none; d=google.com; s=arc-20160816; b=PD5IZnSk4/hbFKX1dyo37mvh4XESUzE0i9PWVO1rLsZw4KBl27Zk36ITq7EKcvnPE7 fibjzq1cpNrwhxCWQJdwWvPsZTPRl3AVUfOgNEnHMtgLC0US+j1SCkjCdniLSR5rXon+ kW9tWhwuCk5gKnGf5ewW/DSPZtKBME3jFce95HrTn4fRU/LzpMHPMZfEOGsr67i6efdD jlc3Air2M5MekEPlKA3A/03Oi8vlekn+4HluyUJAuo3B3D2sjaq6I23vUqAA6AkfuRPy aRe/zDNxCTA5UJ3+7kr4In5fBARvW29BbJ4hrAp5BpLD1POsa4ojFRPFOL8iD+qoti3+ 9TDQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=yA35bzbc7DQTXUnDdGots2lopsb0vOYzZ8K0lzRGVqA=; b=E2ztjy+e/gY0MfXFbFwoVZXNexSFNOlw77DlhNXdzlyK3eDvKW4kwVgCbILcUrCgMm pI3lA9l4rZTS+dLlRyfkoMzdhWm3JT29jOVTaz9YhF+ZS5bAibhhBdceGqe0ac8CFR80 TJqaBsIKVfxp+t1ZN7dddLly+Y+lzamHAM7dvbHGS95aIFf3/Eti9KgleBh3r1v3Hkak k+p4B722WXHvUZmxOu9qfGT1FJUVXbXfqIjTutT0N8oK/qdNP8qRp0nVwDh4V70xWsUn GXtOSDlDo/+8NWxKIszNleyzEIy48XHwt7shfRG7EJK9SHKKu9kASdhCC1ghIpqZvoY0 2nWQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=KugeWV5h; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 99si5178549plc.368.2018.01.17.13.10.03; Wed, 17 Jan 2018 13:10:17 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=KugeWV5h; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932273AbeAQVIH (ORCPT + 99 others); Wed, 17 Jan 2018 16:08:07 -0500 Received: from bombadil.infradead.org ([65.50.211.133]:37111 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753477AbeAQUWd (ORCPT ); Wed, 17 Jan 2018 15:22:33 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20170209; h=References:In-Reply-To:Message-Id: Date:Subject:Cc:To:From:Sender:Reply-To:MIME-Version:Content-Type: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=yA35bzbc7DQTXUnDdGots2lopsb0vOYzZ8K0lzRGVqA=; b=KugeWV5hP/gMn3e7K0ReKwGtG CBA97L4AF8wXzSzmizMWJ3Mhcc7VXH+gV+XMihfBqucm3bTizPCvLPdyIu2bNL5ss5l/IJFT1Dgf6 vQusdjbwohNrSAtZgWGQ3AssM0g0IbbQvlwBIaIRoaVOlooiHqbCNfPaNQye7CxbTSdJYhO6OSnOZ qz8awXuBLtwoD+gEDzwMKFS8RkenRAVGonEee9D4wjhsA9Pb76ts5jD5jphO8RNqvCN/yafwuINPD mD7a+NKL1kFGxdunU81B3TYnPYAukdPyd8Y0Uy5L4E5hPQy/MrZNBFyBPEibJGIZzEb6wyyxZkmJJ 2GGWN1ezg==; Received: from willy by bombadil.infradead.org with local (Exim 4.89 #1 (Red Hat Linux)) id 1ebuEB-0005l6-Mf; Wed, 17 Jan 2018 20:22:31 +0000 From: Matthew Wilcox To: linux-kernel@vger.kernel.org Cc: Matthew Wilcox , linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-f2fs-devel@lists.sourceforge.net, linux-nilfs@vger.kernel.org, linux-btrfs@vger.kernel.org, linux-xfs@vger.kernel.org, linux-usb@vger.kernel.org, Bjorn Andersson , Stefano Stabellini , iommu@lists.linux-foundation.org, linux-remoteproc@vger.kernel.org, linux-s390@vger.kernel.org, intel-gfx@lists.freedesktop.org, cgroups@vger.kernel.org, linux-sh@vger.kernel.org, David Howells Subject: [PATCH v6 18/99] xarray: Add ability to store errno values Date: Wed, 17 Jan 2018 12:20:42 -0800 Message-Id: <20180117202203.19756-19-willy@infradead.org> X-Mailer: git-send-email 2.14.3 In-Reply-To: <20180117202203.19756-1-willy@infradead.org> References: <20180117202203.19756-1-willy@infradead.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Matthew Wilcox While the radix tree offers no ability to store IS_ERR pointers, documenting that the XArray does not led to some concern. Here is a sanctioned way to store errnos in the XArray. I'm concerned that it will confuse people who can't tell the difference between xa_is_err() and xa_is_errno(), so I've added copious kernel-doc to help them tell the difference. Signed-off-by: Matthew Wilcox --- Documentation/core-api/xarray.rst | 8 +++++-- include/linux/xarray.h | 44 ++++++++++++++++++++++++++++++++++ tools/testing/radix-tree/xarray-test.c | 8 ++++++- 3 files changed, 57 insertions(+), 3 deletions(-) diff --git a/Documentation/core-api/xarray.rst b/Documentation/core-api/xarray.rst index 914999c0bf3f..0172c7d9e6ea 100644 --- a/Documentation/core-api/xarray.rst +++ b/Documentation/core-api/xarray.rst @@ -42,8 +42,12 @@ When you retrieve an entry from the XArray, you can check whether it is a value entry by calling :c:func:`xa_is_value`, and convert it back to an integer by calling :c:func:`xa_to_value`. -The XArray does not support storing :c:func:`IS_ERR` pointers as some -conflict with value entries or internal entries. +The XArray does not support storing :c:func:`IS_ERR` pointers because +some conflict with value entries or internal entries. If you need +to store error numbers in the array, you can encode them into error +entries with :c:func:`xa_mk_errno`, check whether a returned entry is +an error with :c:func:`xa_is_errno` and convert it back into an errno +with :c:func:`xa_to_errno`. An unusual feature of the XArray is the ability to create entries which occupy a range of indices. Once stored to, looking up any index in diff --git a/include/linux/xarray.h b/include/linux/xarray.h index acb6d02ff194..ca6af6dd42c4 100644 --- a/include/linux/xarray.h +++ b/include/linux/xarray.h @@ -75,6 +75,50 @@ static inline bool xa_is_value(const void *entry) return (unsigned long)entry & 1; } +/** + * xa_mk_errno() - Create an XArray entry from an error number. + * @error: Error number to store in XArray. + * + * Return: An entry suitable for storing in the XArray. + */ +static inline void *xa_mk_errno(long error) +{ + return (void *)(error << 2); +} + +/** + * xa_to_errno() - Get error number stored in an XArray entry. + * @entry: XArray entry. + * + * Calling this function on an entry which is not an xa_is_errno() will + * yield unpredictable results. Do not confuse this function with xa_err(); + * this function is for errnos which have been stored in the XArray, and + * that function is for errors returned from the XArray implementation. + * + * Return: The error number stored in the XArray entry. + */ +static inline long xa_to_errno(const void *entry) +{ + return (long)entry >> 2; +} + +/** + * xa_is_errno() - Determine if an entry is an errno. + * @entry: XArray entry. + * + * Do not confuse this function with xa_is_err(); that function tells you + * whether the XArray implementation returned an error; this function + * tells you whether the entry you successfully stored in the XArray + * represented an errno. If you have never stored an errno in the XArray, + * you do not have to check this. + * + * Return: True if the entry is an errno, false if it is a pointer. + */ +static inline bool xa_is_errno(const void *entry) +{ + return (((unsigned long)entry & 3) == 0) && (entry > (void *)-4096); +} + /* * xa_mk_internal() - Create an internal entry. * @v: Value to turn into an internal entry. diff --git a/tools/testing/radix-tree/xarray-test.c b/tools/testing/radix-tree/xarray-test.c index 2ad460c1febf..4d3541ac31e9 100644 --- a/tools/testing/radix-tree/xarray-test.c +++ b/tools/testing/radix-tree/xarray-test.c @@ -29,7 +29,13 @@ void check_xa_err(struct xarray *xa) assert(xa_err(xa_store(xa, 1, xa_mk_value(0), GFP_KERNEL)) == 0); assert(xa_err(xa_store(xa, 1, NULL, 0)) == 0); // kills the test-suite :-( -// assert(xa_err(xa_store(xa, 0, xa_mk_internal(0), 0)) == -EINVAL); +// assert(xa_err(xa_store(xa, 0, xa_mk_internal(0), 0)) == -EINVAL); + + assert(xa_err(xa_store(xa, 0, xa_mk_errno(-ENOMEM), GFP_KERNEL)) == 0); + assert(xa_err(xa_load(xa, 0)) == 0); + assert(xa_is_errno(xa_load(xa, 0)) == true); + assert(xa_to_errno(xa_load(xa, 0)) == -ENOMEM); + xa_erase(xa, 0); } void check_xa_tag(struct xarray *xa) -- 2.15.1