Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp730671pxb; Tue, 2 Feb 2021 16:59:09 -0800 (PST) X-Google-Smtp-Source: ABdhPJzxpuI6dbgIBBty8IfPCyacRfcpK51BRj2BOmZJOuDSdmioOkDiDrG67Zgc60W+0vP+jjt+ X-Received: by 2002:a17:907:da7:: with SMTP id go39mr616310ejc.255.1612313949320; Tue, 02 Feb 2021 16:59:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1612313949; cv=none; d=google.com; s=arc-20160816; b=lqgSUPfnqYfz7AfJU8EeFRrBPO1YuW53RJwRzibEJneOpkYmpTgObL16/9nlzV4Wvf J41pAulnftolQRElMyKDXV60qkwgFgQ7195OF6bT1rb/KHU4cB0Gua+9Zg8vkwNYoBZh khwGjLcUOxNgzTS/mUsK/Z33Nt/1w7yY7iYGsZE5ZuSWx1QsFuV5SYjwov/PXVmpJgs/ 2yunoDOG2qJTUvM2wB4PRK9GDLiyglbt//kWjnuuBKFhuaK6sAbBtL4Qg3dPgKW+0Omf +jHhC7fXHv6IfPWB15fCsL4tdHgTAus2uwCUQnHDErUGy9nIKoX/hD2NiHpIVYWOWC4T HdFQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject:dkim-signature; bh=ENue7s30/AVYjJ+FLuK3utBLdl7fX7Wgo1RZFpgH4vM=; b=yZx4cqL+6jATWlVYbCV+OQYTZvK5ZHgyBpo7Akiytpptdd2/luHH8mPRCOQmA9OIER S00EoS4jpnpg+vWns/XGCiAlVjN/XmwKZV9Nry2RdRENXhSTYdpvuFuRr/kh0gYBJRNv IpLrG7+/5YbWpNKvuJc3Os0N0qlvLP0W1v+SMX4PdCgW1UOvUr7oT7bf49T7HB3y0PUF ew+SbPNVERnf1Cs3esdf/Ys4DxdPxRiU3pDNMNboV+hRK3019Vj01L4LAHuZ7ogLgofa TsCoRKBN6rLb7OcF4NtENcUm4/joN4XXcW6xrU3Sn6mLDsUMi/IBb+8QvpsIk+CrR6z/ /glw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=merlin.20170209 header.b=iCpPPCir; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id c5si305541ejz.259.2021.02.02.16.58.44; Tue, 02 Feb 2021 16:59:09 -0800 (PST) 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=@infradead.org header.s=merlin.20170209 header.b=iCpPPCir; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235887AbhBBW7o (ORCPT + 99 others); Tue, 2 Feb 2021 17:59:44 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52922 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232681AbhBBW7l (ORCPT ); Tue, 2 Feb 2021 17:59:41 -0500 Received: from merlin.infradead.org (merlin.infradead.org [IPv6:2001:8b0:10b:1231::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 75D0AC0613D6; Tue, 2 Feb 2021 14:59:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=merlin.20170209; h=Content-Transfer-Encoding:Content-Type: In-Reply-To:MIME-Version:Date:Message-ID:From:References:Cc:To:Subject:Sender :Reply-To:Content-ID:Content-Description; bh=ENue7s30/AVYjJ+FLuK3utBLdl7fX7Wgo1RZFpgH4vM=; b=iCpPPCirUc9RhjKzpwe3y4FP9W Er4hHDaCbraQPxNrWmL2Yao94VNYmaPyexKnej4DgMa6blaJPt+pVpsgTWVPOEstB1gcQYPe73aAF v9VazIjrtXW9X/ChFtnTI+IpgqygQ3asQ16cZidy4PTIwoXo5z32Kxd87vp9zAXjdKA3kpFJuLBJE geVlY2+N3EIhy5WY1aMvzjCXYGfmzbKT5mCqHMujL6Fv8rV2q4Zib9vzCFy9tEuqFUS1QvISsZGHe WLRuK+q8+3pouAGoC2JuWkD8JsJGjQyF2NKqJlTbJhN+2kMaeBEtjP/6600EQAYF8StNRlB7VhEEF rYfAw2Fg==; Received: from [2601:1c0:6280:3f0::2a53] by merlin.infradead.org with esmtpsa (Exim 4.92.3 #3 (Red Hat Linux)) id 1l74cg-0001Wj-VY; Tue, 02 Feb 2021 22:58:15 +0000 Subject: Re: [PATCH v5 1/2] drivers/misc: sysgenid: add system generation id driver To: Adrian Catangiu , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, qemu-devel@nongnu.org, kvm@vger.kernel.org, linux-s390@vger.kernel.org Cc: gregkh@linuxfoundation.org, graf@amazon.com, arnd@arndb.de, ebiederm@xmission.com, rppt@kernel.org, 0x7f454c46@gmail.com, borntraeger@de.ibm.com, Jason@zx2c4.com, jannh@google.com, w@1wt.eu, colmmacc@amazon.com, luto@kernel.org, tytso@mit.edu, ebiggers@kernel.org, dwmw@amazon.co.uk, bonzini@gnu.org, sblbir@amazon.com, raduweis@amazon.com, corbet@lwn.net, mst@redhat.com, mhocko@kernel.org, rafael@kernel.org, pavel@ucw.cz, mpe@ellerman.id.au, areber@redhat.com, ovzxemul@gmail.com, avagin@gmail.com, ptikhomirov@virtuozzo.com, gil@azul.com, asmehra@redhat.com, dgunigun@redhat.com, vijaysun@ca.ibm.com, oridgar@gmail.com, ghammer@redhat.com References: <1612200294-17561-1-git-send-email-acatan@amazon.com> <1612200294-17561-2-git-send-email-acatan@amazon.com> From: Randy Dunlap Message-ID: <5290f6f5-396f-aa47-3b74-8d50c2434a04@infradead.org> Date: Tue, 2 Feb 2021 14:58:02 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.4.0 MIME-Version: 1.0 In-Reply-To: <1612200294-17561-2-git-send-email-acatan@amazon.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi-- On 2/1/21 9:24 AM, Adrian Catangiu wrote: > - Background and problem > > The System Generation ID feature is required in virtualized or > containerized environments by applications that work with local copies > or caches of world-unique data such as random values, uuids, > monotonically increasing counters, etc. ... if those applications want to comply with . > Such applications can be negatively affected by VM or container > snapshotting when the VM or container is either cloned or returned to > an earlier point in time. > Signed-off-by: Adrian Catangiu > --- > Documentation/misc-devices/sysgenid.rst | 236 ++++++++++++++++ > Documentation/userspace-api/ioctl/ioctl-number.rst | 1 + > MAINTAINERS | 8 + > drivers/misc/Kconfig | 16 ++ > drivers/misc/Makefile | 1 + > drivers/misc/sysgenid.c | 307 +++++++++++++++++++++ > include/uapi/linux/sysgenid.h | 17 ++ > 7 files changed, 586 insertions(+) > create mode 100644 Documentation/misc-devices/sysgenid.rst > create mode 100644 drivers/misc/sysgenid.c > create mode 100644 include/uapi/linux/sysgenid.h > > diff --git a/Documentation/misc-devices/sysgenid.rst b/Documentation/misc-devices/sysgenid.rst > new file mode 100644 > index 0000000..4337ca0 > --- /dev/null > +++ b/Documentation/misc-devices/sysgenid.rst > @@ -0,0 +1,236 @@ > +.. SPDX-License-Identifier: GPL-2.0 > + > +======== > +SYSGENID > +======== > + > +The System Generation ID feature is required in virtualized or > +containerized environments by applications that work with local copies > +or caches of world-unique data such as random values, UUIDs, > +monotonically increasing counters, etc. > +Such applications can be negatively affected by VM or container > +snapshotting when the VM or container is either cloned or returned to > +an earlier point in time. > + > +The System Generation ID is a simple concept meant to alleviate the > +issue by providing a monotonically increasing counter that changes > +each time the VM or container is restored from a snapshot. > +The driver for it lives at ``drivers/misc/sysgenid.c``. > + > +The ``sysgenid`` driver exposes a monotonic incremental System > +Generation u32 counter via a char-dev FS interface accessible through s/FS/filesystem/ > +``/dev/sysgenid`` that provides sync and async SysGen counter update > +notifications. It also provides SysGen counter retrieval and > +confirmation mechanisms. > + > +The counter starts from zero when the driver is initialized and > +monotonically increments every time the system generation changes. > + > +The ``sysgenid`` driver exports the ``void sysgenid_bump_generation()`` > +symbol which can be used by backend drivers to drive system generation > +changes based on hardware events. > +System generation changes can also be driven by userspace software > +through a dedicated driver ioctl. > + > +Userspace applications or libraries can (a)synchronously consume the > +system generation counter through the provided FS interface, to make s/FS/filesystem/ > +any necessary internal adjustments following a system generation update. > + > +Driver FS interface: > + > +``open()``: > + When the device is opened, a copy of the current Sys-Gen-Id (counter) > + is associated with the open file descriptor. The driver now tracks > + this file as an independent *watcher*. The driver tracks how many > + watchers are aware of the latest Sys-Gen-Id counter and how many of > + them are *outdated*; outdated being those that have lived through > + a Sys-Gen-Id change but not yet confirmed the new generation counter. > + > +``read()``: > + Read is meant to provide the *new* system generation counter when a > + generation change takes place. The read operation blocks until the > + associated counter is no longer up to date, at which point the new > + counter is provided/returned. > + Nonblocking ``read()`` uses ``EAGAIN`` to signal that there is no > + *new* counter value available. The generation counter is considered > + *new* for each open file descriptor that hasn't confirmed the new > + value following a generation change. Therefore, once a generation > + change takes place, all ``read()`` calls will immediately return the > + new generation counter and will continue to do so until the > + new value is confirmed back to the driver through ``write()``. > + Partial reads are not allowed - read buffer needs to be at least > + 32 bits in size. > + > +``write()``: > + Write is used to confirm the up-to-date Sys Gen counter back to the > + driver. > + Following a VM generation change, all existing watchers are marked > + as *outdated*. Each file descriptor will maintain the *outdated* > + status until a ``write()`` confirms the up-to-date counter back to > + the driver. > + Partial writes are not allowed - write buffer should be exactly > + 32 bits in size. > + > +``poll()``: > + Poll is implemented to allow polling for generation counter updates. > + Such updates result in ``EPOLLIN`` polling status until the new > + up-to-date counter is confirmed back to the driver through a > + ``write()``. > + > +``ioctl()``: > + The driver also adds support for waiting on open file descriptors > + that haven't acknowledged a generation counter update, as well as a > + mechanism for userspace to *force* a generation update: > + > + - SYSGENID_WAIT_WATCHERS: blocks until there are no more *outdated* > + watchers, or if a ``timeout`` argument is provided, until the > + timeout expires. > + If the current caller is *outdated* or a generation change happens > + while waiting (thus making current caller *outdated*), the ioctl > + returns ``-EINTR`` to signal the user to handle event and retry. > + - SYSGENID_FORCE_GEN_UPDATE: forces a generation counter increment. > + It takes a ``minimum-generation`` argument which represents the > + minimum value the generation counter will be incremented to. For will be set to. For It's not so much an increment as it is a "set to this value or higher". > + example if current generation is ``5`` and ``SYSGENID_FORCE_GEN_UPDATE(8)`` > + is called, the generation counter will increment to ``8``. > + This IOCTL can only be used by processes with CAP_CHECKPOINT_RESTORE > + or CAP_SYS_ADMIN capabilities. > + > +``mmap()``: > + The driver supports ``PROT_READ, MAP_SHARED`` mmaps of a single page > + in size. The first 4 bytes of the mapped page will contain an > + up-to-date u32 copy of the system generation counter. > + The mapped memory can be used as a low-latency generation counter > + probe mechanism in critical sections - see examples. > + > +``close()``: > + Removes the file descriptor as a system generation counter *watcher*. > + > +Example application workflows > +----------------------------- > + [snip] -- ~Randy