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=-3.9 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_PASS,URIBL_BLOCKED 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 4FDEBC07E85 for ; Tue, 11 Dec 2018 14:15:08 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 1071320849 for ; Tue, 11 Dec 2018 14:15:08 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 1071320849 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=virtuozzo.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726631AbeLKOPG (ORCPT ); Tue, 11 Dec 2018 09:15:06 -0500 Received: from relay.sw.ru ([185.231.240.75]:56516 "EHLO relay.sw.ru" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726444AbeLKOPG (ORCPT ); Tue, 11 Dec 2018 09:15:06 -0500 Received: from [172.16.25.169] by relay.sw.ru with esmtp (Exim 4.91) (envelope-from ) id 1gWioP-0000o2-9r; Tue, 11 Dec 2018 17:15:01 +0300 Subject: Re: [PATCH v2] ksm: React on changing "sleep_millisecs" parameter faster To: Cyrill Gorcunov Cc: akpm@linux-foundation.org, mhocko@suse.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, gorcunov@virtuozzo.com References: <20181211100346.GE2342@uranus.lan> <154452399396.4921.3418365102240528000.stgit@localhost.localdomain> <20181211111300.GF2342@uranus.lan> <26fb047f-9164-96ae-a6cd-7a7efa41d43b@virtuozzo.com> <20181211123455.GI2342@uranus.lan> From: Kirill Tkhai Message-ID: <2dd9cc7b-9384-df11-bb5a-3aed45cc914b@virtuozzo.com> Date: Tue, 11 Dec 2018 17:15:00 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.3.1 MIME-Version: 1.0 In-Reply-To: <20181211123455.GI2342@uranus.lan> Content-Type: text/plain; charset=windows-1252 Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 11.12.2018 15:34, Cyrill Gorcunov wrote: > On Tue, Dec 11, 2018 at 03:22:42PM +0300, Kirill Tkhai wrote: >> On 11.12.2018 14:13, Cyrill Gorcunov wrote: >>> On Tue, Dec 11, 2018 at 01:26:59PM +0300, Kirill Tkhai wrote: >>>> ksm thread unconditionally sleeps in ksm_scan_thread() >>>> after each iteration: >>>> >>>> schedule_timeout_interruptible( >>>> msecs_to_jiffies(ksm_thread_sleep_millisecs)) >>>> >>>> The timeout is configured in /sys/kernel/mm/ksm/sleep_millisecs. >>>> >>>> In case of user writes a big value by a mistake, and the thread >>>> enters into schedule_timeout_interruptible(), it's not possible >>>> to cancel the sleep by writing a new smaler value; the thread >>>> is just sleeping till timeout expires. >>>> >>>> The patch fixes the problem by waking the thread each time >>>> after the value is updated. >>>> >>>> This also may be useful for debug purposes; and also for userspace >>>> daemons, which change sleep_millisecs value in dependence of >>>> system load. >>>> >>>> Signed-off-by: Kirill Tkhai >>>> >>>> v2: Use wait_event_interruptible_timeout() instead of unconditional >>>> schedule_timeout(). >>> ... >>>> @@ -2844,7 +2849,10 @@ static ssize_t sleep_millisecs_store(struct kobject *kobj, >>>> if (err || msecs > UINT_MAX) >>>> return -EINVAL; >>>> >>>> + mutex_lock(&ksm_thread_mutex); >>>> ksm_thread_sleep_millisecs = msecs; >>>> + mutex_unlock(&ksm_thread_mutex); >>>> + wake_up_interruptible(&ksm_iter_wait); >>> >>> Btw, just thought -- if we start using this mutex here don't we >>> open a window for force attack on the thread self execution, >>> iow if there gonna be a million of writers do we have a guarantee >>> thread ksm_scan_thread will grab the mutex earlier than writers >>> (or somewhere inbetween)? >> >> This file is permitted for global root only. I don't think there is >> a problem. >> >> If someone wants to make ksm helpless, a person may just write a big >> "sleep_millisecs" value. KSM thread won't be executed almost all the time >> in this case. > > True. Still I think if we can leave without taking a lock it a rule of thumb. > Something like > > if (msecs != ksm_thread_sleep_millisecs) > wake_up_interruptable(&ksm_iter_wait); > > Thoughts? Ok, good idea.