From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on gnuweeb.org X-Spam-Level: X-Spam-Status: No, score=-1.8 required=5.0 tests=ALL_TRUSTED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A,NO_DNS_FOR_FROM, URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 Received: from [192.168.88.87] (unknown [180.242.99.67]) by gnuweeb.org (Postfix) with ESMTPSA id 08DE87F61A; Wed, 11 May 2022 07:59:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1652255952; bh=Fw2qZDFCMmqG0M/lJabaILxfE+Gi6Tl3+WvT87pP3r8=; h=Date:Subject:To:Cc:References:From:In-Reply-To:From; b=JoBjT/ovjOaGCKw98GwB+oUVIZJd1BNr1hAVPEmvAZTZXbHlMdQXp+nEJlWv0SkMK uMLW+g35pjyiVP4LcynhQXjIC4Udhbqxyc4/lQGYkEt06XqrXiWXtfkXqZlC1WZLY8 eP0Zh2vP2PQhbmLFW00GGNK6SQPRfhX4MOr+yd6aDHf3AfOfp3l5jyRweisIzJJMuy jg6iIB/UT3qRcl5PHXDHRxFwe3ldog6TL7RPs3TSWcOPEy8lxcXIAdU/XFOFEX4KhE Hfm4j4803+DW01AO+a7yk5F2oxbMSfMVAX6WddrCKBLL0Du9c6+2tNcL8QyeWTZ/nK C8m59fGl2mVCw== Message-ID: Date: Wed, 11 May 2022 14:58:56 +0700 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.8.1 Subject: Re: [PATCH v5] mm/ksm: introduce ksm_force for each process Content-Language: en-US To: Andrew Morton Cc: cgel.zte@gmail.com, Kees Cook , Matthew Wilcox , Yang Yang , Ran Xiaokai , Yunkai Zhang , xu xin , wangyong , Linux MM Mailing List , Linux fsdevel Mailing List , Linux Kernel Mailing List References: <20220507105926.d4423601230f698b0f5228d1@linux-foundation.org> <20220508092710.930126-1-xu.xin16@zte.com.cn> <435b5f7a-fcbd-f7ae-b66f-670e5997aa1b@gnuweeb.org> <20220510133016.9feff1aeec1a7a9ae137a8c3@linux-foundation.org> From: Ammar Faizi In-Reply-To: <20220510133016.9feff1aeec1a7a9ae137a8c3@linux-foundation.org> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit List-Id: On 5/11/22 3:30 AM, Andrew Morton wrote: > On Wed, 11 May 2022 03:10:31 +0700 Ammar Faizi wrote: > >> On 5/8/22 4:27 PM, cgel.zte@gmail.com wrote: >>> +static ssize_t ksm_force_write(struct file *file, const char __user *buf, >>> + size_t count, loff_t *ppos) >>> +{ >>> + struct task_struct *task; >>> + struct mm_struct *mm; >>> + char buffer[PROC_NUMBUF]; >>> + int force; >>> + int err = 0; >>> + >>> + memset(buffer, 0, sizeof(buffer)); >>> + if (count > sizeof(buffer) - 1) >>> + count = sizeof(buffer) - 1; >>> + if (copy_from_user(buffer, buf, count)) { >>> + err = -EFAULT; >>> + goto out_return; >>> + } >> >> This one looks like over-zeroing to me. You don't need to zero >> all elements in the array. You're going to overwrite it with >> `copy_from_user()` anyway. >> >> Just zero the last potentially useful element by using @count >> as the index. It can be like this: >> >> ``` >> char buffer[PROC_NUMBUF]; >> >> if (count > sizeof(buffer) - 1) >> count = sizeof(buffer) - 1; >> if (copy_from_user(buffer, buf, count)) >> return -EFAULT; >> buffer[count] = '\0'; >> ``` > > Use strncpy_from_user()? Sounds better. > Can this code use proc_dointvec_minmax() or similar? Not familiar with that API at all. Leaving it to other participants... -- Ammar Faizi