Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp242855pxk; Wed, 23 Sep 2020 01:50:56 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxrsu2GUwQv4wpZ1D9WDgYmXZBmjSVWVQ42X7NJSvW9cF1RhYxHT83VhpyWcDCKq1mxRrfk X-Received: by 2002:a05:6402:304f:: with SMTP id bu15mr8688545edb.201.1600851055830; Wed, 23 Sep 2020 01:50:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1600851055; cv=none; d=google.com; s=arc-20160816; b=pMj1eBeQd3XlNosBHl7vLwfKdjWJXXgdXfrRFIAzpX7CELJDzEc5bOjydUA37MNHAJ acjHZcGQiw8HZpGvdzVr8oNMbzlNjupz7nP6si5koHYJOVcE7BgIWtDlrKJwQnxdx43f bVVZa9Fu0XBIJjRfW6MEEStbHAFc7g1abyYfVENrCnMKzryxvCxxGGvj3iPko8/VakGE X2VZ9qB4zmqkHaLYEnN6oZMX9uZa+Edq3S+tQJlpd5W78Qwf2ko/9fiwzu0NiHzsgV99 3A9kaBtzHULHP5BFbbUs82knCCQrG3TZI/9MP5bLovII8bFbAnCsDmAZvHQKWtj8OyMP CxZA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date; bh=OHmiwmqgKyQe9JlXCQ4Jdm8xkVwGfbs4I0qEaCuVquc=; b=u+S21UFK89V7RDZonJ371V6A2T2qZO9YmOZ/PmGoncVrJDh51p0fqLBbC9z6QHbjKE eFZu+rq5H2B0HGQyitTaT+VS2URq6fQ0kReD7MJmNDeoQlBGnsQB7oRSCdHz2Yw1Q5G3 OymG2ztvo69eunhBHWsIIQ0JGC0cm7u8POGURovksCATlxufCrXDWVOIvgi9XQq5jzci lIOV5gtZUj/bxu2CGBWiKqINbJJgviinR14ooPQL7nBnQD2RnhHzFoCQzu7E5rsEV09h A8C+KEVqmFqR936lVdFZNKNQmnwX3qx+5JfHOYlEDCaqvUEgITp4p+3gzx16/T+sq2sk hWzw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id i18si12656855edv.542.2020.09.23.01.50.32; Wed, 23 Sep 2020 01:50:55 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726419AbgIWItf (ORCPT + 99 others); Wed, 23 Sep 2020 04:49:35 -0400 Received: from mail.kernel.org ([198.145.29.99]:58056 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726242AbgIWIte (ORCPT ); Wed, 23 Sep 2020 04:49:34 -0400 Received: from gaia (unknown [31.124.44.166]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 2694B206BE; Wed, 23 Sep 2020 08:49:32 +0000 (UTC) Date: Wed, 23 Sep 2020 09:49:30 +0100 From: Catalin Marinas To: Amit Kachhap Cc: linux-arm-kernel@lists.infradead.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, Shuah Khan , Will Deacon , Vincenzo Frascino Subject: Re: [PATCH 6/6] kselftest/arm64: Check mte tagged user address in kernel Message-ID: <20200923084930.GB13434@gaia> References: <20200901092719.9918-1-amit.kachhap@arm.com> <20200901092719.9918-7-amit.kachhap@arm.com> <20200922104123.GF15643@gaia> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.10.1 (2018-07-13) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Sep 23, 2020 at 12:36:59PM +0530, Amit Kachhap wrote: > On 9/22/20 4:11 PM, Catalin Marinas wrote: > > On Tue, Sep 01, 2020 at 02:57:19PM +0530, Amit Daniel Kachhap wrote: > > > +static int check_usermem_access_fault(int mem_type, int mode, int mapping) > > > +{ > > > + int fd, ret, i, err; > > > + char val = 'A'; > > > + size_t len, read_len; > > > + void *ptr, *ptr_next; > > > + bool fault; > > > + > > > + len = 2 * page_sz; > > > + err = KSFT_FAIL; > > > + /* > > > + * Accessing user memory in kernel with invalid tag should fault in sync > > > + * mode but may not fault in async mode as per the implemented MTE > > > + * support in Arm64 kernel. > > > + */ > > > + if (mode == MTE_ASYNC_ERR) > > > + fault = false; > > > + else > > > + fault = true; > > > + mte_switch_mode(mode, MTE_ALLOW_NON_ZERO_TAG); > > > + fd = create_temp_file(); > > > + if (fd == -1) > > > + return KSFT_FAIL; > > > + for (i = 0; i < len; i++) > > > + write(fd, &val, sizeof(val)); > > > + lseek(fd, 0, 0); > > > + ptr = mte_allocate_memory(len, mem_type, mapping, true); > > > + if (check_allocated_memory(ptr, len, mem_type, true) != KSFT_PASS) { > > > + close(fd); > > > + return KSFT_FAIL; > > > + } > > > + mte_initialize_current_context(mode, (uintptr_t)ptr, len); > > > + /* Copy from file into buffer with valid tag */ > > > + read_len = read(fd, ptr, len); > > > + ret = errno; > > > > My reading of the man page is that errno is set only if read() returns > > -1. > > Yes. The checks should be optimized here. It's not about optimisation but correctness. The errno man page states that errno is only relevant if the syscall returns -1. So it may potentially hold a stale value (e.g. EFAULT) in case of read() success but the check below fails anyway: > > > + mte_wait_after_trig(); > > > + if ((cur_mte_cxt.fault_valid == true) || ret == EFAULT || read_len < len) > > > + goto usermem_acc_err; -- Catalin