From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8472FC433FE for ; Mon, 27 Sep 2021 17:05:19 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 1A84E6108E for ; Mon, 27 Sep 2021 17:05:19 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 1A84E6108E Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id 9A4D7940007; Mon, 27 Sep 2021 13:05:18 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 95458900002; Mon, 27 Sep 2021 13:05:18 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7F5D9940007; Mon, 27 Sep 2021 13:05:18 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0252.hostedemail.com [216.40.44.252]) by kanga.kvack.org (Postfix) with ESMTP id 72F81900002 for ; Mon, 27 Sep 2021 13:05:18 -0400 (EDT) Received: from smtpin37.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 272A6181D68A4 for ; Mon, 27 Sep 2021 17:05:18 +0000 (UTC) X-FDA: 78633979116.37.63BD4A0 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by imf08.hostedemail.com (Postfix) with ESMTP id C83B530000B8 for ; Mon, 27 Sep 2021 17:05:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1632762317; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=OceQJGplaRuhOhJC6xlNCBMIhl52paO/MQX3xAWc2qg=; b=ADArQzj0JPBZL+8LnrGqIv1JQQwhpZfokl89o7PNk5rLdUT71ssX+nlJ3DttpioXVngZBN q6QuSTzZefU4ZFL1RjHFDktPsRiiWM8HoiON2QsmBFKBPmcDgm1ECfGM9LZOq0HRWLRFr8 PvFpGJm3uP5i18Jh5DlbYgXKPBJUZZs= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-559-ZsdnYdkvM-O3FLlIrlecmQ-1; Mon, 27 Sep 2021 13:05:14 -0400 X-MC-Unique: ZsdnYdkvM-O3FLlIrlecmQ-1 Received: by mail-wm1-f72.google.com with SMTP id r66-20020a1c4445000000b0030cf0c97157so509284wma.1 for ; Mon, 27 Sep 2021 10:05:14 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:to:cc:references:from:organization:subject :message-id:date:user-agent:mime-version:in-reply-to :content-language:content-transfer-encoding; bh=OceQJGplaRuhOhJC6xlNCBMIhl52paO/MQX3xAWc2qg=; b=e93wX8P3zd+O3zMtDCLgLBiy77Tf8+LFFsR8hrOf+0OhFniFyO8JZfBhXNIGUrbZoZ afp7ErGZ/n2Hc4CoWCjF+jmuoOpxuHKW2+EXtlwSwIQI4EEgGqfuwkeYFMKzFQU2YxKk 1lxuHHVnGCpL06JCB7dSsz4IflP0ttZm9wI9jVrizY0Gt/iRoQ/6b2DH5lz/PRGmL07b S3vGItJOhBSKmEs0uA4O0SLDeLZt3GDWDSvkTLSMPhteI7gdh9sGgcbqIZ21oBRJAKtt XBWU01g4AlBTlGX0g+SG9fHHsk/aMGB2f3VtvqB23YI32jyuafhQ2/PywW8XIqUtxSbO q+jg== X-Gm-Message-State: AOAM531ya6rAXBCdSl0fta6hCVerialFMbvc1XpUkt877nKJfdqf2the tS3hbpeOPEAJ5Y1VgicSxtSVje+QjNomHh1ax648TBTcQlTR+s7up24Mk9hee/hsHBuZAWZB2NF yMwPuBwLELOc= X-Received: by 2002:a05:600c:4c05:: with SMTP id d5mr172647wmp.2.1632762312820; Mon, 27 Sep 2021 10:05:12 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwKDtyzWxoHjALiR62Xodk8Hky8hrOWja8rHGF9SfvFUqBOH/xqqTAFnMIa6/BzHZRMVRn7LA== X-Received: by 2002:a05:600c:4c05:: with SMTP id d5mr172555wmp.2.1632762311999; Mon, 27 Sep 2021 10:05:11 -0700 (PDT) Received: from [192.168.3.132] (p5b0c654d.dip0.t-ipconnect.de. [91.12.101.77]) by smtp.gmail.com with ESMTPSA id t16sm83386wmi.33.2021.09.27.10.05.10 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 27 Sep 2021 10:05:11 -0700 (PDT) To: Nadav Amit Cc: Andrew Morton , Linux-MM , Linux Kernel Mailing List , Peter Xu , Andrea Arcangeli , Minchan Kim , Colin Cross , Suren Baghdasarya , Mike Rapoport References: <20210926161259.238054-1-namit@vmware.com> <7ce823c8-cfbf-cc59-9fc7-9aa3a79740c3@redhat.com> <6E8A03DD-175F-4A21-BCD7-383D61344521@gmail.com> <2753a311-4d5f-8bc5-ce6f-10063e3c6167@redhat.com> From: David Hildenbrand Organization: Red Hat Subject: Re: [RFC PATCH 0/8] mm/madvise: support process_madvise(MADV_DONTNEED) Message-ID: Date: Mon, 27 Sep 2021 19:05:10 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.11.0 MIME-Version: 1.0 In-Reply-To: X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US X-Rspamd-Queue-Id: C83B530000B8 X-Stat-Signature: 8nihstfax6hgrbyt7yygsctph9tymq6g Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=ADArQzj0; spf=none (imf08.hostedemail.com: domain of david@redhat.com has no SPF policy when checking 216.205.24.124) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-Rspamd-Server: rspam06 X-HE-Tag: 1632762317-266024 Content-Transfer-Encoding: quoted-printable X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On 27.09.21 14:00, Nadav Amit wrote: >=20 >=20 >> On Sep 27, 2021, at 3:58 AM, David Hildenbrand wrot= e: >> >> On 27.09.21 12:41, Nadav Amit wrote: >>>> On Sep 27, 2021, at 2:24 AM, David Hildenbrand wr= ote: >>>> >>>> On 26.09.21 18:12, Nadav Amit wrote: >>>>> From: Nadav Amit >>>>> The goal of these patches is to add support for >>>>> process_madvise(MADV_DONTNEED). Yet, in the process some (arguably) >>>>> useful cleanups, a bug fix and performance enhancements are perform= ed. >>>>> The patches try to consolidate the logic across different behaviors= , and >>>>> to a certain extent overlap/conflict with an outstanding patch that= does >>>>> something similar [1]. This consolidation however is mostly orthogo= nal >>>>> to the aforementioned one and done in order to clarify what is done= in >>>>> respect to locks and TLB for each behavior and to batch these opera= tions >>>>> more efficiently on process_madvise(). >>>>> process_madvise(MADV_DONTNEED) is useful for two reasons: (a) it al= lows >>>>> userfaultfd monitors to unmap memory from monitored processes; and = (b) >>>>> it is more efficient than madvise() since it is vectored and batche= s TLB >>>>> flushes more aggressively. >>>> >>>> MADV_DONTNEED on MAP_PRIVATE memory is a target-visible operation; t= his is very different to all the other process_madvise() calls we allow, = which are merely hints, but the target cannot be broken . I don't think t= his is acceptable. >>> This is a fair point, which I expected, but did not address properly. >>> I guess an additional capability, such as CAP_SYS_PTRACE needs to be >>> required in this case. Would that ease your mind? >> >> I think it would be slightly better, but I'm still missing a clear use= case that justifies messing with the page tables of other processes in t= hat way, especially with MAP_PRIVATE mappings. Can you maybe elaborate a = bit on a) and b)? >> >> Especially, why would a) make sense or be required? When would it be a= good idea to zap random pages of a target process, especially with MAP_P= RIVATE? How would the target use case make sure that the target process d= oesn't suddenly lose data? I would have assume that you can really only d= o something sane with uffd() if 1) the process decided to give up on some= pages (madvise(DONTNEED)) b) the process hasn't touched these pages yet. >> >> Can you also comment a bit more on b)? Who cares about that? And would= we suddenly expect users of madvise() to switch to process_madvise() bec= ause it's more effective? It sounds a bit weird to me TBH, but most proba= bly I am missing details :) >=20 > Ok, ok, your criticism is fair. I tried to hold back some details in or= der to > prevent the discussion from digressing. I am going to focus on (a) whic= h is > what I really have in mind. Thanks for the details! >=20 > The use-case that I explore is a userspace memory manager with some lev= el of > cooperation of the monitored processes. >=20 > The manager is notified on memory regions that it should monitor > (through PTRACE/LD_PRELOAD/explicit-API). It then monitors these region= s > using the remote-userfaultfd that you saw on the second thread. When it= wants > to reclaim (anonymous) memory, it: >=20 > 1. Uses UFFD-WP to protect that memory (and for this matter I got a vec= tored > UFFD-WP to do so efficiently, a patch which I did not send yet). > 2. Calls process_vm_readv() to read that memory of that process. > 3. Write it back to =E2=80=9Cswap=E2=80=9D. > 4. Calls process_madvise(MADV_DONTNEED) to zap it. >=20 > Once the memory is accessed again, the manager uses UFFD-COPY to bring = it > back. This is really work-in-progress, but eventually performance is no= t as > bad as you would imagine (some patches for efficient use of uffd with > iouring are needed for that matter). Again, thanks for the details. I guess this should basically work,=20 although it involves a lot of complexity (read: all flavors of uffd on=20 other processes). And I am no so sure about performance aspects.=20 "Performance is not as bad as you think" doesn't sound like the words=20 you would want to hear from a car dealer ;) So there has to be another=20 big benefit to do such user space swapping. >=20 > I am aware that there are some caveats, as zapping the memory does not > guarantee that the memory would be freed since it might be pinned for a > variety of reasons. That's the reason I mentioned the processes have "s= ome > level of cooperation" with the manager. It is not intended to deal with > adversaries or uncommon corner cases (e.g., processes that use UFFD for > their own reasons). It's not only long-term pinnings. Pages could have been de-duplicated=20 (COW after fork, KSM, shared zeropage). Further, you'll most probably=20 lose any kind of "aging" ("accessed") information on pages, or how would=20 you track that? Although I can see that this might work, I do wonder if it's a use case=20 worth supporting. As Michal correctly raised, we already have other=20 infrastructure in place to trigger swapin/swapout. I recall that also=20 damon wants to let you write advanced policies for that by monitoring=20 actual access characteristics. >=20 > Putting aside my use-case (which I am sure people would be glad to crit= icize), > I can imagine debuggers or emulators may also find use for similar sche= mes > (although I do not have concrete use-cases for them). I'd be curious about use cases for debuggers/emulators. Especially for=20 emulators I'd guess it makes more sense to just do it within the=20 process. And for debuggers, I'm having a hard time why it would make=20 sense to throw away a page instead of just overwriting it with $PATTERN=20 (e.g., 0). But I'm sure people can be creative :) --=20 Thanks, David / dhildenb