From patchwork Fri Dec 15 17:10:12 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthew Wilcox X-Patchwork-Id: 10115609 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 513F1602C2 for ; Fri, 15 Dec 2017 17:10:50 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 372AE2A07F for ; Fri, 15 Dec 2017 17:10:50 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2B6D62A083; Fri, 15 Dec 2017 17:10:50 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 940AB2A07F for ; Fri, 15 Dec 2017 17:10:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755920AbdLORKr (ORCPT ); Fri, 15 Dec 2017 12:10:47 -0500 Received: from bombadil.infradead.org ([65.50.211.133]:60697 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755495AbdLORKo (ORCPT ); Fri, 15 Dec 2017 12:10:44 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20170209; h=In-Reply-To:Content-Type:MIME-Version :References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: 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=iJH7l8lpuFlxh4QnVPh6zRoyLwVI5jfRKefeePYqrhk=; b=AtBMGvTSOaArkbzVoMwv79tY0 j7xSySmnT9hbS1wwsiW+CIp/klUi9G2CQpyla20/YOI5oz2TUgooWfPViMLYBGdSMBxeHfoqBH1Ce JrCvAbjFNoOD7qZtWDYQmhi0eQ92DqL6tNZNb9jVFOAVpHE7PkipAf7k5I3crNyLzMfbBX/0HlzFQ 8cgZLIjZ9ItFKVS6K2yMZLluz7CvmN+hhGAu3KsR5GRmNM6p/ntSK/IaaQuubr/t/IOxjsfZq9U5m qHH7gpvfWhdWH+w1sNWTCn8LJaV/r+xRW/elP3RM6b5GLpb/t9jYAtzszZ+ISWiLsdokErx9Hjz95 UdKFrBB2Q==; Received: from willy by bombadil.infradead.org with local (Exim 4.87 #1 (Red Hat Linux)) id 1ePtUy-0000bS-8c; Fri, 15 Dec 2017 17:10:12 +0000 Date: Fri, 15 Dec 2017 09:10:12 -0800 From: Matthew Wilcox To: Randy Dunlap Cc: Matthew Wilcox , Ross Zwisler , Jens Axboe , Rehas Sachdeva , 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, linux-kernel@vger.kernel.org Subject: Storing errors in the XArray Message-ID: <20171215171012.GA11918@bombadil.infradead.org> References: <20171206004159.3755-1-willy@infradead.org> <20171206004159.3755-9-willy@infradead.org> <66ad068b-1973-ca41-7bbf-8a0634cc488d@infradead.org> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <66ad068b-1973-ca41-7bbf-8a0634cc488d@infradead.org> User-Agent: Mutt/1.9.1 (2017-09-22) Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP On Mon, Dec 11, 2017 at 03:10:22PM -0800, Randy Dunlap wrote: > > +The XArray does not support storing :c:func:`IS_ERR` pointers; some > > +conflict with data values and others conflict with entries the XArray > > +uses for its own purposes. If you need to store special values which > > +cannot be confused with real kernel pointers, the values 4, 8, ... 4092 > > +are available. > > or if I know that they values are errno-range values, I can just shift them > left by 2 to store them and then shift them right by 2 to use them? On further thought, I like this idea so much, it's worth writing helpers for this usage. And test-suite (also doubles as a demonstration of how to use it). --- To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/include/linux/xarray.h b/include/linux/xarray.h index c616e9319c7c..53aa251df57a 100644 --- a/include/linux/xarray.h +++ b/include/linux/xarray.h @@ -232,6 +232,39 @@ 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. + * + * Return: The error number stored in the XArray entry. + */ +static inline unsigned long xa_to_errno(const void *entry) +{ + return (long)entry >> 2; +} + +/** + * xa_is_errno() - Determine if an entry is an errno. + * @entry: XArray entry. + * + * 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_is_internal() - Is the entry an internal entry? * @entry: Entry retrieved from the XArray diff --git a/tools/testing/radix-tree/xarray-test.c b/tools/testing/radix-tree/xarray-test.c index 43111786ebdd..b843cedf3988 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)