Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp2558887rwr; Fri, 21 Apr 2023 10:26:52 -0700 (PDT) X-Google-Smtp-Source: AKy350ZRqsM/7+Y9dMG4ZOOL0Fbv6Rw43IInijl1eR3EhPPFfszvFeQC1KzDXMKaeUedTzGWyuIA X-Received: by 2002:a05:6a20:12cc:b0:ee:b7ea:5d9c with SMTP id v12-20020a056a2012cc00b000eeb7ea5d9cmr8936880pzg.0.1682098012594; Fri, 21 Apr 2023 10:26:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682098012; cv=none; d=google.com; s=arc-20160816; b=0odpd+kFTPa4r+Q7z1kNzgXW3xl4UBB/EP7BI6xk3FjWEJNa5i+LyeAXhvxfCj6E3k Z9r8tVs+aRvcmNmqtxKvQVkKgkbjuT89JGpUtK4OD7FhdY1Eyr/h6tKEgugq8wUtgdvg lSjxnBaSoGDNPN3HGBX/jg7T9kPtjkjt74iLXIJsGpmzArlM3mRVhpAi0e0IOvvXRDHT RLH+gvCIp1OQIRWqk2QHj5oGLYYrp+lWTQIOx/GddsMQ1A79MNxjedMvMfJqzPQfug0b i/hBKC3BeVAV3uK6vSSOp2KD9E1q8o0EVglcxL2gaYejQJvPuRYRrYKI420X26oEtIML QbYA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:subject :organization:from:references:cc:to:content-language:user-agent :mime-version:date:message-id:dkim-signature; bh=HWRo2mphM3DxKyp/++X+gPOFeElhZDx6hgzjY51IZDg=; b=es6tbIlZoRXeZkYiTrUh90yXxCmO6PErED1mIwaQ57x8LkT30A+wifC7nUoIOMpIGV xChyw/UTwMiIIsNJvPt2d3AaLULLkC5v1bzGbRoJXiVwvtu1cm1LAsFEIIvJoOwFtHG2 YVFxNaeNdvdtZ6FhnkOhU8KB0pXci6ChfB7blLgwmL84iecRPmvezgr7UoaodpqugeeS Xw/nFzUQ2U32XnPwEklHUU7XLjMeVXTnLwpTK7BIWcaPzapfTKz5Eunt4IRv+6voMYiR +WaGGJC3lgtO4IQRqO4BXqLsVVhcWqWVNUueMnn+hrgplhmV4nFxvFY623xFfANKZdyt wb0w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=heB4QSod; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l3-20020a63ea43000000b0051394ccd19csi5009022pgk.55.2023.04.21.10.26.34; Fri, 21 Apr 2023 10:26:52 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=heB4QSod; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233236AbjDURMV (ORCPT + 99 others); Fri, 21 Apr 2023 13:12:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55046 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229641AbjDURMT (ORCPT ); Fri, 21 Apr 2023 13:12:19 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 99CA815A16 for ; Fri, 21 Apr 2023 10:11:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1682097083; 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=HWRo2mphM3DxKyp/++X+gPOFeElhZDx6hgzjY51IZDg=; b=heB4QSodS1u8nIQkKI0KrMYKqFRhpI/Ao+NFY1WCSemxF7T1zD7h610RWCJ7P9HRQ7i2Y6 IcGwy4R6eF7t4mcy8eNrkzEO8H6kfl4O45fOjzMki1SEuvr90mDYld3kAS9q3/4cholcnu fYAt8IaCIs8Zj1Gt4/u4W8G9mzWGKIg= Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-556-UcS97dzlP0OsqZB2VB2sYg-1; Fri, 21 Apr 2023 13:11:22 -0400 X-MC-Unique: UcS97dzlP0OsqZB2VB2sYg-1 Received: by mail-wr1-f71.google.com with SMTP id ffacd0b85a97d-2f6632b85fcso634194f8f.1 for ; Fri, 21 Apr 2023 10:11:22 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682097081; x=1684689081; h=content-transfer-encoding:in-reply-to:subject:organization:from :references:cc:to:content-language:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=HWRo2mphM3DxKyp/++X+gPOFeElhZDx6hgzjY51IZDg=; b=FcQAQantEn11RxT1+xtiVIQwJn0wnknLyAyudJnddHqGGwHCpnGi7W45iMplznXT0N OpptUzgO0yJWR7/eW9D++7GbEmXMrlaA1Bn7zR3tKVGIGjAW5GgXgxRzoXU6SGiZvq4p ISjzsDxPhleOwmpMikXOIsB/K6Go7EV4/jMuIGeMcF6cLFg50j4eKds9rAtjVwAvkhiX NwOVP0HKyLZWcoJYeFoSzKOvPvI6/JoiT8HINnqSUpNmDDasTzGI08ABUiWn83mro+PH rKTgZhxKDdRkFFPRkyiP1I7Bt2R+xxSXUp+LMGj0x6TGfw35GZ32BD/V0pVLIwjxuN8x QGkw== X-Gm-Message-State: AAQBX9e+E+yRAVYAZmzREYMgtRSvMmTcX0rlv+3HnVyiDPgq4fOltGzm 9r3gzep4VkUAfe8nJabl6aAHjvCM4enBT3flATC7Nvi+fp2Yz+vrVok/FZSzTlg9W0TIepZ/Uph ifW63Zct0Xacx1y+x6cGsckmoyDuGVlhV X-Received: by 2002:adf:e803:0:b0:2f7:a333:8cab with SMTP id o3-20020adfe803000000b002f7a3338cabmr4492296wrm.71.1682097081560; Fri, 21 Apr 2023 10:11:21 -0700 (PDT) X-Received: by 2002:adf:e803:0:b0:2f7:a333:8cab with SMTP id o3-20020adfe803000000b002f7a3338cabmr4492269wrm.71.1682097081087; Fri, 21 Apr 2023 10:11:21 -0700 (PDT) Received: from ?IPV6:2003:cb:c717:6a00:e38f:c852:dc11:9146? (p200300cbc7176a00e38fc852dc119146.dip0.t-ipconnect.de. [2003:cb:c717:6a00:e38f:c852:dc11:9146]) by smtp.gmail.com with ESMTPSA id o10-20020a1c750a000000b003f191c9c4b0sm2323556wmc.11.2023.04.21.10.11.18 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 21 Apr 2023 10:11:19 -0700 (PDT) Message-ID: Date: Fri, 21 Apr 2023 19:11:18 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.10.0 Content-Language: en-US To: Stefan Roesch Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-s390@vger.kernel.org, linux-kselftest@vger.kernel.org, Andrew Morton , Rik van Riel , Johannes Weiner , Michal Hocko , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Heiko Carstens , Vasily Gorbik , Sven Schnelle , Shuah Khan References: <20230418051342.1919757-1-shr@devkernel.io> <20230418152849.505124-1-david@redhat.com> <20230418152849.505124-2-david@redhat.com> From: David Hildenbrand Organization: Red Hat Subject: Re: [PATCH v1 1/3] mm/ksm: unmerge and clear VM_MERGEABLE when setting PR_SET_MEMORY_MERGE=0 In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-5.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org [...] >> > nit: > can we do: > > if (arg2) > error = ksm_enable_merge_any(me->mm); > else > error = ksm_disable_merge_any(me->mm); > mmap_write_unlock(me->mm); > break; Indeed, thanks. > >> diff --git a/mm/ksm.c b/mm/ksm.c >> index a959e8925413..813f7fbc1832 100644 >> --- a/mm/ksm.c >> +++ b/mm/ksm.c >> @@ -2520,6 +2520,22 @@ static void __ksm_add_vma(struct vm_area_struct *vma) >> vm_flags_set(vma, VM_MERGEABLE); >> } >> >> +static int __ksm_del_vma(struct vm_area_struct *vma) >> +{ >> + int err; >> + >> + if (!(vma->vm_flags & VM_MERGEABLE)) >> + return 0; >> + >> + if (vma->anon_vma) { >> + err = unmerge_ksm_pages(vma, vma->vm_start, vma->vm_end); >> + if (err) >> + return err; >> + } >> + >> + vm_flags_clear(vma, VM_MERGEABLE); >> + return 0; >> +} >> /** >> * ksm_add_vma - Mark vma as mergeable if compatible >> * >> @@ -2542,6 +2558,20 @@ static void ksm_add_vmas(struct mm_struct *mm) >> __ksm_add_vma(vma); >> } >> >> +static int ksm_del_vmas(struct mm_struct *mm) >> +{ >> + struct vm_area_struct *vma; >> + int err; >> + >> + VMA_ITERATOR(vmi, mm, 0); >> + for_each_vma(vmi, vma) { >> + err = __ksm_del_vma(vma); >> + if (err) >> + return err; >> + } >> + return 0; >> +} >> + >> /** >> * ksm_enable_merge_any - Add mm to mm ksm list and enable merging on all >> * compatible VMA's >> @@ -2569,6 +2599,23 @@ int ksm_enable_merge_any(struct mm_struct *mm) >> return 0; >> } >> >> +int ksm_disable_merge_any(struct mm_struct *mm) >> > > I understand we want to keep the name "symmetric" with > ksm_enable_merge_any, but it also unmerges the ksm pages. Do we want to > reflect that in the function name? ksm_disable_merge_any_umerge() is suboptimal. As ksm_disable_merge_any() now reverts what ksm_enable_merge_any() ended up doing, I think it's just fine. (it would be a different story if we'd be using "set" / "clear" terminology instead of "enable" / "disable"). We can describe that in the comment. > > Can we add a comment for the function? Can do for symmetry with ksm_enable_merge_any(). But note that I don't think documentation for functions is of any help when it takes longer to read the documentation than to read+understand the actual code. > >> +{ >> + int err; >> + >> + if (!test_bit(MMF_VM_MERGE_ANY, &mm->flags)) >> + return 0; >> + >> + err = ksm_del_vmas(mm); >> + if (err) { >> + ksm_add_vmas(mm); >> + return err; >> + } >> + >> + clear_bit(MMF_VM_MERGE_ANY, &mm->flags); >> > > We only clear the MMF_VM_MERGE_ANY flag if there are no errors. Is this I think this is the behavior we want. We tried to disable KSM for the process (previously enabled via the prctl), but cannot disable KSM. So we rollback our changes and return an error. This is similar to trying to set MADV_UNMERGEABLE but failing. We leave the bit set. > what we want? This means that if the process creates new memory regions > they would still be marked as mergeable. Yes, we failed the operation so we keep everything unchanged. -- Thanks, David / dhildenb