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 X-Spam-Level: X-Spam-Status: No, score=-6.9 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH, MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 28908C10F27 for ; Mon, 9 Mar 2020 13:11:27 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id CECFE22522 for ; Mon, 9 Mar 2020 13:11:26 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="g2Q6yMj0" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org CECFE22522 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 5F0CA6B0003; Mon, 9 Mar 2020 09:11:26 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5A2416B0006; Mon, 9 Mar 2020 09:11:26 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 48F7A6B0007; Mon, 9 Mar 2020 09:11:26 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0110.hostedemail.com [216.40.44.110]) by kanga.kvack.org (Postfix) with ESMTP id 31DA86B0003 for ; Mon, 9 Mar 2020 09:11:26 -0400 (EDT) Received: from smtpin04.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id D0E9A181AEF07 for ; Mon, 9 Mar 2020 13:11:25 +0000 (UTC) X-FDA: 76575860130.04.suit87_468aaf9380f44 X-HE-Tag: suit87_468aaf9380f44 X-Filterd-Recvd-Size: 9301 Received: from us-smtp-delivery-1.mimecast.com (us-smtp-2.mimecast.com [205.139.110.61]) by imf13.hostedemail.com (Postfix) with ESMTP for ; Mon, 9 Mar 2020 13:11:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1583759484; 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=lA9eY9OEGwn3gVQ7wAi0UK/p0gU8MjFEcaXTHOPUbD4=; b=g2Q6yMj0yz6Z5h+X5n192FSYMpHoGVPFOfGNucKydHiVjqp0Of9ZNQvdEEitiSju2jn5W+ ZRtJNspuvmzy+cmfhcoU9OcWiaBTKNMPU3Csm6IZfIGU1fkDmB4ZskXC7PQE1IUGUczQhp ZSIFTlCdAbx1Wvd5mamr4MBY3F06s1g= Received: from mail-wr1-f69.google.com (mail-wr1-f69.google.com [209.85.221.69]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-272-JTilrp9nN860wV9vpmOc-A-1; Mon, 09 Mar 2020 09:11:20 -0400 X-MC-Unique: JTilrp9nN860wV9vpmOc-A-1 Received: by mail-wr1-f69.google.com with SMTP id b11so2941829wru.21 for ; Mon, 09 Mar 2020 06:11:20 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=xRJIUHA2CnOOeiHBMD4y+dr1ZXYznLSuwH0WgiBK5ck=; b=PLyoESP8huqCJ+RCsuaQ29+apGDYcSHMWsrSM6eTqNCXAkJtSqGA9b+5dCpoPJeuYt s8VRINp13Acnn1uHk8tWJ1jCkxJ9nYZ+YUzMDGSwUgromB6F8SOEmNRdKGa+oPxVuHbV /id27NrRLPMIN0KaJloBw6EoaRgrTJ5wHPAhVytRR3VeR7OtHoAe8bIBNJldx4VIp2pf AnMsMnXtlL25Dct12zuCDIpq5x11Wf4uVsZ/MRez9RkZ30e8QLwVJcNO+zi33jttvEhk JtU+fljgdid/wd/0E75yDMxNACf/s7lUqBZfnugYWEDU1pAZkUR/jjWPAkIExYqFOLSZ ngOQ== X-Gm-Message-State: ANhLgQ34XimEB/1JumyKpKfTaA+0pdNAvVl2V3r/2LJxaY3rLpOpz9iO nM1FEokJZULQ4noc5A/F560e2+eqYxrYbrM2ctvfoYrjZ2m070C1HLjIBWe5aIxB0xLxqdHU11X GdfOl9ZEoBPQ= X-Received: by 2002:adf:e506:: with SMTP id j6mr20733605wrm.309.1583759479390; Mon, 09 Mar 2020 06:11:19 -0700 (PDT) X-Google-Smtp-Source: ADFU+vuCDJFZ7JTpz5uoFvZAVeKx+9vsgLNPQPPkBhoFe61RXjhOioyK15/Hhufdoq2jrHxA0uFd5Q== X-Received: by 2002:adf:e506:: with SMTP id j6mr20733564wrm.309.1583759479021; Mon, 09 Mar 2020 06:11:19 -0700 (PDT) Received: from localhost (nat-pool-brq-t.redhat.com. [213.175.37.10]) by smtp.gmail.com with ESMTPSA id y15sm5710650wrq.89.2020.03.09.06.11.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Mar 2020 06:11:18 -0700 (PDT) Date: Mon, 9 Mar 2020 14:11:17 +0100 From: Oleksandr Natalenko To: Vlastimil Babka Cc: Minchan Kim , Andrew Morton , LKML , linux-mm , linux-api@vger.kernel.org, Suren Baghdasaryan , Tim Murray , Daniel Colascione , Sandeep Patil , Sonny Rao , Brian Geffon , Michal Hocko , Johannes Weiner , Shakeel Butt , John Dias , Joel Fernandes , Jann Horn , alexander.h.duyck@linux.intel.com, sj38.park@gmail.com, SeongJae Park Subject: Re: [PATCH v7 7/7] mm/madvise: allow KSM hints for remote API Message-ID: <20200309131117.anvyjszaigpoz2kp@butterfly.localdomain> References: <20200302193630.68771-1-minchan@kernel.org> <20200302193630.68771-8-minchan@kernel.org> <2a66abd8-4103-f11b-06d1-07762667eee6@suse.cz> <20200306134146.mqiyvsdnqty7so53@butterfly.localdomain> MIME-Version: 1.0 In-Reply-To: X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable Content-Disposition: inline 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 Fri, Mar 06, 2020 at 05:08:18PM +0100, Vlastimil Babka wrote: > On 3/6/20 2:41 PM, Oleksandr Natalenko wrote: > > On Fri, Mar 06, 2020 at 02:13:49PM +0100, Vlastimil Babka wrote: > >> On 3/2/20 8:36 PM, Minchan Kim wrote: > >> > From: Oleksandr Natalenko > >> >=20 > >> > It all began with the fact that KSM works only on memory that is mar= ked > >> > by madvise(). And the only way to get around that is to either: > >> >=20 > >> > * use LD_PRELOAD; or > >> > * patch the kernel with something like UKSM or PKSM. > >> >=20 > >> > (i skip ptrace can of worms here intentionally) > >> >=20 > >> > To overcome this restriction, lets employ a new remote madvise API. = This > >> > can be used by some small userspace helper daemon that will do auto-= KSM > >> > job for us. > >> >=20 > >> > I think of two major consumers of remote KSM hints: > >> >=20 > >> > * hosts, that run containers, especially similar ones and especial= ly in > >> > a trusted environment, sharing the same runtime like Node.js; >=20 > Ah, I forgot to ask, given the discussion of races in patch 2 (Question 2= ), > where android can stop the tasks to apply the madvise hints in a race-fre= e > manner, how does that work for remote KSM hints in your scenarios, especi= ally > the one above? We have cgroup.freeze for that. >=20 > >> >=20 > >> > * heavy applications, that can be run in multiple instances, not > >> > limited to opensource ones like Firefox, but also those that can= not be > >> > modified since they are binary-only and, maybe, statically linke= d. > >> >=20 > >> > Speaking of statistics, more numbers can be found in the very first > >> > submission, that is related to this one [1]. For my current setup wi= th > >> > two Firefox instances I get 100 to 200 MiB saved for the second inst= ance > >> > depending on the amount of tabs. > >> >=20 > >> > 1 FF instance with 15 tabs: > >> >=20 > >> > $ echo "$(cat /sys/kernel/mm/ksm/pages_sharing) * 4 / 1024" | bc > >> > 410 > >> >=20 > >> > 2 FF instances, second one has 12 tabs (all the tabs are different): > >> >=20 > >> > $ echo "$(cat /sys/kernel/mm/ksm/pages_sharing) * 4 / 1024" | bc > >> > 592 > >> >=20 > >> > At the very moment I do not have specific numbers for containerised > >> > workload, but those should be comparable in case the containers shar= e > >> > similar/same runtime. > >> >=20 > >> > [1] https://lore.kernel.org/patchwork/patch/1012142/ > >> >=20 > >> > Reviewed-by: SeongJae Park > >> > Signed-off-by: Oleksandr Natalenko > >> > Signed-off-by: Minchan Kim > >>=20 > >> This will lead to one process calling unmerge_ksm_pages() of another. = There's a > >> (signal_pending(current)) test there, should it check also the other t= ask, > >> analogically to task 3? > >=20 > > Do we care about current there then? Shall we just pass mm into unmerge= _ksm_pages and check the signals of the target task only, be it current or = something else? >=20 > Dunno, it's nice to react to signals quickly, for any proces that gets th= em, no? So, do you mean something like this? =3D=3D=3D diff --git a/mm/ksm.c b/mm/ksm.c index 363ec8189561..b39c237cfcf4 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -849,7 +849,8 @@ static int unmerge_ksm_pages(struct vm_area_struct *vma= , =09for (addr =3D start; addr < end && !err; addr +=3D PAGE_SIZE) { =09=09if (ksm_test_exit(vma->vm_mm)) =09=09=09break; -=09=09if (signal_pending(current)) +=09=09if (signal_pending(current) || +=09=09 signal_pending(rcu_dereference(vma->vm_mm->owner))) =09=09=09err =3D -ERESTARTSYS; =09=09else =09=09=09err =3D break_ksm(vma, addr); =3D=3D=3D BTW, this won't work with !CONFIG_MEMCG, so probably task_struct should be passed through instead. IIUC, this would also require amending struct mm_slot in order to share the same code path with ksmd. I'm not sure I've seen such a culprit anywhere else, so I'm in doubt this would be a correct thing to do. Ideas? >=20 > >> Then break_ksm() is fine as it is, as ksmd also calls it, right? > >=20 > > I think break_ksm() cares only about mmap_sem protection, so we should > > be fine here. > >=20 > >>=20 > >> > --- > >> > mm/madvise.c | 4 ++++ > >> > 1 file changed, 4 insertions(+) > >> >=20 > >> > diff --git a/mm/madvise.c b/mm/madvise.c > >> > index e77c6c1fad34..f4fa962ee74d 100644 > >> > --- a/mm/madvise.c > >> > +++ b/mm/madvise.c > >> > @@ -1005,6 +1005,10 @@ process_madvise_behavior_valid(int behavior) > >> > =09switch (behavior) { > >> > =09case MADV_COLD: > >> > =09case MADV_PAGEOUT: > >> > +#ifdef CONFIG_KSM > >> > +=09case MADV_MERGEABLE: > >> > +=09case MADV_UNMERGEABLE: > >> > +#endif > >> > =09=09return true; > >> > =09default: > >> > =09=09return false; > >> >=20 > >>=20 > >=20 >=20 --=20 Best regards, Oleksandr Natalenko (post-factum) Principal Software Maintenance Engineer