From patchwork Fri Feb 9 15:04:54 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kirill Tkhai X-Patchwork-Id: 10209395 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 D636860236 for ; Fri, 9 Feb 2018 15:05:06 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C2E49284F9 for ; Fri, 9 Feb 2018 15:05:06 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B760228A31; Fri, 9 Feb 2018 15:05:06 +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=-7.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, RCVD_IN_DNSWL_HI 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 942AB284F9 for ; Fri, 9 Feb 2018 15:05:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752227AbeBIPFE (ORCPT ); Fri, 9 Feb 2018 10:05:04 -0500 Received: from mail-db5eur01on0127.outbound.protection.outlook.com ([104.47.2.127]:50917 "EHLO EUR01-DB5-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752201AbeBIPFB (ORCPT ); Fri, 9 Feb 2018 10:05:01 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=virtuozzo.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=Bf1s8HfyaxPnb4+m9xhT1nK53B1HA0uM/wQyzijiYhg=; b=Y6mYRlPNvJPbOby/PjHoigL64rM/YXFFdP+XmUcQWg3cC1RrAg6fUERFNXp2rKWx/9Je3+Y8/htk2ij2p2VVFLc+DLS/cnQcCLvljkidL0NmKZOY3ysCMEPqlBljDC8R63nPVB3kq+qr0rSl5AhrdK71Rj55kHO66AMDJ4QUn88= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=ktkhai@virtuozzo.com; Received: from [172.16.25.196] (195.214.232.6) by AM5PR0801MB1329.eurprd08.prod.outlook.com (2603:10a6:203:1f::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P256) id 15.20.464.11; Fri, 9 Feb 2018 15:04:58 +0000 Subject: [PATCH v2] inotify: Extend ioctl to allow to request id of new watch descriptor To: akpm@linux-foundation.org, jack@suse.cz, amir73il@gmail.com, willy@infradead.org, linux-api@vger.kernel.org, linux-fsdevel@vger.kernel.org, gorcunov@virtuozzo.com References: <151810242614.30935.12876744458891870220.stgit@localhost.localdomain> From: Kirill Tkhai Message-ID: Date: Fri, 9 Feb 2018 18:04:54 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.6.0 MIME-Version: 1.0 In-Reply-To: <151810242614.30935.12876744458891870220.stgit@localhost.localdomain> Content-Language: en-US X-Originating-IP: [195.214.232.6] X-ClientProxiedBy: HE1P192CA0005.EURP192.PROD.OUTLOOK.COM (2603:10a6:3:fe::15) To AM5PR0801MB1329.eurprd08.prod.outlook.com (2603:10a6:203:1f::7) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 02f58da1-49f0-4fb5-5349-08d56fce7c87 X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(7020095)(4652020)(5600026)(4604075)(2017052603307)(7153060)(7193020); SRVR:AM5PR0801MB1329; X-Microsoft-Exchange-Diagnostics: 1; AM5PR0801MB1329; 3:yjD9E+1Xw262JiyN4Cf3v0132A5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HylN3wBDqe/BPIa4AKLJldZkIrY/AxbQP4WKNrKfYqoHFIohd1rD94go9HjF7c72AtckNFv1ydFlB7gc3rASLKMW/B31Y2WH+kIRCCdOd+5/2M9nfia+j4C6I9cEiZG/puPbbCcDKTHbD/f+Gb/o9c28lgiQX2+JnqhvcMLdoxtdvgF+a7z8SCj/W5snKKEWCAUAUoa/P6kTHQD0eaicNxHrhRXEzGpa2SlzHI+d0Q3SXs=; 4:YuracdQ9xNVkRxSZMLJ/pVReBl5H6d3P8ZFy5uycTzUTF7lsy4zYZWq4hGUN2T0kHIdYzoPpTKeKSPO3MWYKEmjk+yW3xgUCAvXgwDUhRyAEPhgexX6vEBTue4SL5GLa09DjS+eH5CMOR3EvYs/Wrzbz9G3S6r2NdH/fYThnaG5ar26vHZ8pWoFVq9WlazLpH7tcp4ONIIUYCPeXwqoC4rCU86DmMXgwe/Cl+bd+BnJbO9hWxm5YW9J8w/St9aFX4cCRw8sIu3bBbJebKT3w076tqQVkAcQnMOH1MWLr1U1u8+hXcgDPCWh8/7yIC+EhWKCz1p+WxK4vuxDKvMjTccCzAyfe+5nFTynV8zNiNeg= X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(166708455590820)(85827821059158); X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(6040501)(2401047)(5005006)(8121501046)(3231101)(2400082)(944501161)(3002001)(93006095)(93001095)(10201501046)(6041288)(20161123560045)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(20161123558120)(20161123562045)(20161123564045)(6072148)(201708071742011); SRVR:AM5PR0801MB1329; BCL:0; PCL:0; RULEID:; SRVR:AM5PR0801MB1329; X-Forefront-PRVS: 057859F9C5 X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10019020)(6049001)(396003)(376002)(39840400004)(346002)(366004)(39380400002)(189003)(199004)(2950100002)(66066001)(50466002)(106356001)(65806001)(65956001)(23676004)(36756003)(2486003)(81156014)(6666003)(6636002)(52146003)(47776003)(83506002)(105586002)(81166006)(305945005)(7736002)(65826007)(25786009)(31686004)(966005)(8676002)(5660300001)(64126003)(478600001)(316002)(16576012)(39060400002)(58126008)(8936002)(230700001)(6486002)(53936002)(26005)(6306002)(86362001)(16526019)(76176011)(386003)(77096007)(6116002)(59450400001)(3846002)(97736004)(2906002)(52116002)(68736007)(31696002); DIR:OUT; SFP:1102; SCL:1; SRVR:AM5PR0801MB1329; H:[172.16.25.196]; FPR:; SPF:None; PTR:InfoNoRecords; MX:1; A:1; LANG:en; Received-SPF: None (protection.outlook.com: virtuozzo.com does not designate permitted sender hosts) X-Microsoft-Exchange-Diagnostics: =?utf-8?B?MTtBTTVQUjA4MDFNQjEzMjk7MjM6T2N0THhVUTVuR2l5LzNUUmliZENXNXJS?= =?utf-8?B?UE43WXllOFoyTVlzaE5QSFVCYUFaRjYyNHBrRTFLVEVvczNWd09pVHVoK2hG?= =?utf-8?B?NjFaY1dHeEo4RldsM216R2Zyd1Bidy9RRlovbzlLeCs5dkxoVmU0VnNyTXlm?= =?utf-8?B?UWUzNWhyK3ZYMGRaZFpPc2x0UUhQMjJtcjRNM3liemovaEJ0N2g5dWplTVFI?= =?utf-8?B?blE0Qm4vN2RRbVVuaWJNZlhmZlFFQlRpZXkyTVdOVU13QzZzbWdXa0NqaExY?= =?utf-8?B?M2UzRXo4TzZJTVRDTUhtMUpLbHl6Q2h1R0FDUExESmVFam1rbStSbGF3b1px?= =?utf-8?B?ZG9lR1hPaXp6TFFtVlkxenp0UGtjenh0aWZGK2pxQjRadGJqbEVoaENNSktB?= =?utf-8?B?U3A1enJBb0Q1Z3NNVjloRTRiNUs3QW5VWVRETDVnN201T2E0b1VLMGpJRFBa?= =?utf-8?B?bThjaHo5RGpiOFJFd256clVmRThRbVExWWVCS2JWUXBmcU5iVFczaEZyTGd6?= =?utf-8?B?akVsbGNiU1NVWXRoUFFpaWl3WllEcGNtZUpPZ1Q4ank4UktzY2ZEMGJZcWZI?= =?utf-8?B?VD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2A6Ds5R7c6rYCWTKavi9WpXM0PDIiwBR4ACpXQRrqdjRFgBNufelNeMZM=; 7:ViK4hCkqSNhT6ZHIA1uLiImbzVuIrdaCB5Nl64QNVCGtZWRR4bFo5UlKNinDKmKORoHCII+d2NcCYsulyiLW3tLryt6gp4b1PYnvZQsTIh0r66NZ5PGEajp2l6IXjiaKGeSqN/q4l+T6t6bXnzkxRWISB7qYa0Ba5/CcZNaaxEsi0Q/Ds8i3BWsEKvsDEINB8OBcE+1cjpwUnkaaLpQKAX2ltziBMTT2Mo9+avd/38QAIBV2kkqsrUz1MxkEZljT SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1; AM5PR0801MB1329; 20:g1JVsVdHgBkJgHIt9Rdjv0cZlvK0GHSJCU2syQuFgip2Km6EtkS9bq5iyCsxy/K+q+x/dsXvs+XnSL5W9gPAqVJWBsoQCND8WZTD2jiI63BzhxoGnyakchKY8w+zSxqj6XppAXkencugVH4ZeaIKNE0Jpz3Xjtx8dJUePAexdqE= X-OriginatorOrg: virtuozzo.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 09 Feb 2018 15:04:58.0228 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 02f58da1-49f0-4fb5-5349-08d56fce7c87 X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 0bc7f26d-0264-416e-a6fc-8352af79c58f X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM5PR0801MB1329 Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Watch descriptor is id of the watch created by inotify_add_watch(). It is allocated in inotify_add_to_idr(), and takes the numbers starting from 1. Every new inotify watch obtains next available number (usually, old + 1), as served by idr_alloc_cyclic(). CRIU (Checkpoint/Restore In Userspace) project supports inotify files, and restores watched descriptors with the same numbers, they had before dump. Since there was no kernel support, we had to use cycle to add a watch with specific descriptor id: while (1) { int wd; wd = inotify_add_watch(inotify_fd, path, mask); if (wd < 0) { break; } else if (wd == desired_wd_id) { ret = 0; break; } inotify_rm_watch(inotify_fd, wd); } (You may find the actual code at the below link: https://github.com/checkpoint-restore/criu/blob/v3.7/criu/fsnotify.c#L577) The cycle is suboptiomal and very expensive, but since there is no better kernel support, it was the only way to restore that. Happily, we had met mostly descriptors with small id, and this approach had worked somehow. But recent time containers with inotify with big watch descriptors begun to come, and this way stopped to work at all. When descriptor id is something about 0x34d71d6, the restoring process spins in busy loop for a long time, and the restore hungs and delay of migration from node to node could easily be watched. This patch aims to solve this problem. It introduces new ioctl INOTIFY_IOC_SETNEXTWD, which allows to request the number of next created watch descriptor from userspace. It simply calls idr_set_cursor() primitive to populate idr::idr_next, so that next idr_alloc_cyclic() allocation will return this id, if it is not occupied. This is the way which is used to restore some other resources from userspace. For example, /proc/sys/kernel/ns_last_pid works the same for task pids. The new code is under CONFIG_CHECKPOINT_RESTORE #define, so small system may exclude it. v2: Use INT_MAX instead of custom definition of max id, as IDR subsystem guarantees id is between 0 and INT_MAX. CC: Jan Kara CC: Matthew Wilcox CC: Andrew Morton CC: Amir Goldstein Signed-off-by: Kirill Tkhai Reviewed-by: Cyrill Gorcunov Reviewed-by: Matthew Wilcox --- fs/notify/inotify/inotify_user.c | 13 +++++++++++++ include/uapi/linux/inotify.h | 8 ++++++++ 2 files changed, 21 insertions(+) diff --git a/fs/notify/inotify/inotify_user.c b/fs/notify/inotify/inotify_user.c index 5c29bf16814f..2a5b806afd09 100644 --- a/fs/notify/inotify/inotify_user.c +++ b/fs/notify/inotify/inotify_user.c @@ -285,6 +285,7 @@ static int inotify_release(struct inode *ignored, struct file *file) static long inotify_ioctl(struct file *file, unsigned int cmd, unsigned long arg) { + struct inotify_group_private_data *data __maybe_unused; struct fsnotify_group *group; struct fsnotify_event *fsn_event; void __user *p; @@ -293,6 +294,7 @@ static long inotify_ioctl(struct file *file, unsigned int cmd, group = file->private_data; p = (void __user *) arg; + data = &group->inotify_data; pr_debug("%s: group=%p cmd=%u\n", __func__, group, cmd); @@ -307,6 +309,17 @@ static long inotify_ioctl(struct file *file, unsigned int cmd, spin_unlock(&group->notification_lock); ret = put_user(send_len, (int __user *) p); break; +#ifdef CONFIG_CHECKPOINT_RESTORE + case INOTIFY_IOC_SETNEXTWD: + ret = -EINVAL; + if (arg >= 1 && arg <= INT_MAX) { + spin_lock(&data->idr_lock); + idr_set_cursor(&data->idr, (unsigned int)arg); + spin_unlock(&data->idr_lock); + ret = 0; + } + break; +#endif /* CONFIG_CHECKPOINT_RESTORE */ } return ret; diff --git a/include/uapi/linux/inotify.h b/include/uapi/linux/inotify.h index 5474461683db..4800bf2a531d 100644 --- a/include/uapi/linux/inotify.h +++ b/include/uapi/linux/inotify.h @@ -71,5 +71,13 @@ struct inotify_event { #define IN_CLOEXEC O_CLOEXEC #define IN_NONBLOCK O_NONBLOCK +/* + * ioctl numbers: inotify uses 'I' prefix for all ioctls, + * except historical FIONREAD, which is based on 'T'. + * + * INOTIFY_IOC_SETNEXTWD: set desired number of next created + * watch descriptor. + */ +#define INOTIFY_IOC_SETNEXTWD _IOW('I', 0, __s32) #endif /* _UAPI_LINUX_INOTIFY_H */