linux-rdma.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Dan Carpenter <dan.carpenter-QHcLZuEGTsvQT0dZR+AlfA@public.gmane.org>
To: Jubin John <jubin.john-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>
Cc: gregkh-hQyY1W1yCW8ekmWlsbkhG0B+6BGkLq7r@public.gmane.org,
	devel-gWbeCf7V1WCQmaza687I9mD2FQJk+8+b@public.gmane.org,
	linux-rdma-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
	dledford-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org
Subject: Re: [PATCH 12/13] staging/rdma/hfi1: Read EFI variable for device description
Date: Wed, 11 Nov 2015 11:44:39 +0300	[thread overview]
Message-ID: <20151111084439.GA18797@mwanda> (raw)
In-Reply-To: <1447227213-15122-12-git-send-email-jubin.john-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>

On Wed, Nov 11, 2015 at 02:33:32AM -0500, Jubin John wrote:
> +static int read_efi_var(const char *name, unsigned long *size,
> +			void **return_data)
> +{
> +	int ret;
> +
> +	/* set failure return values */
> +	*size = 0;
> +	*return_data = NULL;
> +
> +	/*
> +	 * Use EFI run-time support to obtain an EFI variable.  Support may
> +	 * be compiled out, so declare all variables inside.
> +	 */
> +	if (efi_enabled(EFI_RUNTIME_SERVICES)) {


Flip this around:

	if (!efi_enabled(EFI_RUNTIME_SERVICES))
		return -ENOSYS;


> +		efi_status_t status;
> +		efi_char16_t *uni_name;
> +		efi_guid_t guid;
> +		unsigned long temp_size;
> +		void *temp_buffer;
> +		void *data;
> +		int i;
> +
> +		uni_name = kzalloc(sizeof(efi_char16_t) * (strlen(name) + 1),
> +				   GFP_KERNEL);
> +		temp_buffer = kzalloc(EFI_DATA_SIZE, GFP_KERNEL);
> +		data = NULL;

No need.

> +
> +		if (!uni_name || !temp_buffer) {
> +			ret = -ENOMEM;
> +			goto fail;
> +		}
> +
> +		/* input: the size of the buffer */
> +		temp_size = EFI_DATA_SIZE;
> +
> +		/* convert ASCII to unicode - it is a 1:1 mapping */
> +		for (i = 0; name[i]; i++)
> +			uni_name[i] = name[i];
> +
> +		/* need a variable for our GUID */
> +		guid = HFI1_EFIVAR_GUID;
> +
> +		/* call into EFI runtime services */
> +		status = efi.get_variable(
> +				uni_name,
> +				&guid,
> +				NULL,
> +				&temp_size,
> +				temp_buffer);
> +
> +		/*
> +		 * It would be nice to call efi_status_to_err() here, but that
> +		 * is in the EFIVAR_FS code and may not be compiled in.
> +		 * However, even that is insufficient since it does not cover
> +		 * EFI_BUFFER_TOO_SMALL which could be an important return.
> +		 * For now, just split out succces or not found.
> +		 */
> +		ret = status == EFI_SUCCESS   ? 0 :
> +		      status == EFI_NOT_FOUND ? -ENOENT :
> +						-EINVAL;
> +
> +		if (!ret) {
> +			/*
> +			 * We have successfully read the EFI variable into our
> +			 * temporary buffer.  Now allocate a correctly sized
> +			 * buffer.
> +			 */
> +			data = kmalloc(temp_size, GFP_KERNEL);
> +			if (data) {
> +				memcpy(data, temp_buffer, temp_size);
> +				*size = temp_size;
> +				*return_data = data;
> +			} else {
> +				ret = -ENOMEM;
> +			}
> +		}

People often change the last two conditions in the function from
error handling to success handling.  I have ranted about it before many
times so I should just paste a previous rant instead of commenting here.
:P

http://www.spinics.net/lists/arm-kernel/msg457849.html

Success handling makes this look more complicated than it really is.
This code is just a string of commands in a row with error handling.  No
need for if statements or indenting.  Here is how it looks when it's
pulled in one indent level and changed from success handling to error
handling.

	ret = status == EFI_SUCCESS   ? 0 :
	      status == EFI_NOT_FOUND ? -ENOENT : -EINVAL;
	if (ret)
		goto free;

	data = kmemdup(data, temp_size, GFP_KERNEL);
	if (!data) {
		ret = -ENOMEM;
		goto free;
	}

	*size = temp_size;
	*return_data = data;

free:
	kfree(uni_name);
	kfree(temp_buffer);

	return ret;

> +
> +fail:
> +		kfree(uni_name);
> +		kfree(temp_buffer);
> +	} else {
> +		ret = -ENOSYS;
> +	}
> +
> +	return ret;
> +}
> +
> +/*
> + * Read an HFI1 EFI variable of the form:
> + *	<PCIe address>-<kind>
> + * Return an kalloc'ed array and size of the data.
> + *
> + * Returns 0 on success, -errno on failure.
> + */
> +int read_hfi1_efi_var(struct hfi1_devdata *dd, const char *kind,
> +		      unsigned long *size, void **return_data)
> +{
> +	char name[64];
> +
> +	/* create a common prefix */
> +	snprintf(name, sizeof(name), "%04x:%02x:%02x.%x-%s",
> +		 pci_domain_nr(dd->pcidev->bus),
> +		 dd->pcidev->bus->number,
> +		 PCI_SLOT(dd->pcidev->devfn),
> +		 PCI_FUNC(dd->pcidev->devfn),
> +		 kind);
> +	name[sizeof(name) - 1] = 0; /* make sure the string is terminated */

No need.  snprintf() always puts a NUL terminator (technically it
doesn't if the sizeof(name) is zero, I suppose).

> +
> +	return read_efi_var(name, size, return_data);
> +}

regards,
dan carpenter
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

  parent reply	other threads:[~2015-11-11  8:44 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-11-11  7:33 [PATCH 01/13] staging/rdma/hfi1: Use BIT macro Jubin John
     [not found] ` <1447227213-15122-1-git-send-email-jubin.john-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>
2015-11-11  7:33   ` [PATCH 02/13] staging/rdma/hfi1: Fix downgrade race Jubin John
2015-11-11  7:33   ` [PATCH 03/13] staging/rdma/hfi1: remove RxCtxRHQS from hfi1stats Jubin John
2015-11-11  7:33   ` [PATCH 04/13] staging/rdma/hfi: Remove rcv bubbles code Jubin John
2015-11-11  7:33   ` [PATCH 05/13] staging/rdma/hfi1: Add space between concatenated string elements Jubin John
2015-11-11  7:33   ` [PATCH 06/13] staging/rdma/hfi1: Move s_sde to the read mostly portion of the hfi1_qp structure Jubin John
2015-11-11  7:33   ` [PATCH 07/13] staging/rdma/hfi1: rework is_a0() and is_bx() Jubin John
2015-11-11  7:33   ` [PATCH 08/13] staging/rdma/hfi1: change krcvqs module parameter type from byte to uint Jubin John
2015-11-11  7:33   ` [PATCH 09/13] staging/rdma/hfi1: Change default krcvqs Jubin John
2015-11-11  7:33   ` [PATCH 10/13] staging/rdma/hfi1: adding per SDMA engine stats to hfistats Jubin John
     [not found]     ` <1447227213-15122-10-git-send-email-jubin.john-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>
2015-11-11  8:22       ` Dan Carpenter
2015-11-16 21:33         ` Jubin John
2015-11-11  7:33   ` [PATCH 11/13] staging/rdma/hfi1: Remove unneeded variable index Jubin John
2015-11-11  7:33   ` [PATCH 12/13] staging/rdma/hfi1: Read EFI variable for device description Jubin John
     [not found]     ` <1447227213-15122-12-git-send-email-jubin.john-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>
2015-11-11  8:44       ` Dan Carpenter [this message]
2015-11-11 14:03         ` Luick, Dean
     [not found]           ` <4AF12E8016D2BF46BCDFCE8FAA77A3580BC19F69-AtyAts71sc9cIJlls4ac1rfspsVTdybXVpNB7YpNyf8@public.gmane.org>
2015-11-11 14:39             ` Dan Carpenter
2015-11-11 15:03               ` Luick, Dean
     [not found]                 ` <4AF12E8016D2BF46BCDFCE8FAA77A3580BC19FC8-AtyAts71sc9cIJlls4ac1rfspsVTdybXVpNB7YpNyf8@public.gmane.org>
2015-11-11 17:24                   ` gregkh-hQyY1W1yCW8ekmWlsbkhG0B+6BGkLq7r
     [not found]                     ` <20151111172411.GA20481-U8xfFu+wG4EAvxtiuMwx3w@public.gmane.org>
2015-11-12 12:31                       ` Luick, Dean
2015-11-11  7:33   ` [PATCH 13/13] staging/rdma/hfi1: Adjust EPROM partitions, add EPROM commands Jubin John
2015-11-13 13:15   ` [PATCH 01/13] staging/rdma/hfi1: Use BIT macro Sudip Mukherjee
2015-11-13 16:09     ` Jubin John
     [not found]       ` <20151113160921.GA31028-W4f6Xiosr+yv7QzWx2u06xL4W9x8LtSr@public.gmane.org>
2015-11-13 18:03         ` Jubin John
     [not found]           ` <20151113180344.GA10934-W4f6Xiosr+yv7QzWx2u06xL4W9x8LtSr@public.gmane.org>
2015-11-13 18:25             ` Jubin John
2015-11-14  7:11         ` Sudip Mukherjee
2015-11-16 21:21           ` Jubin John

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20151111084439.GA18797@mwanda \
    --to=dan.carpenter-qhclzuegtsvqt0dzr+alfa@public.gmane.org \
    --cc=devel-gWbeCf7V1WCQmaza687I9mD2FQJk+8+b@public.gmane.org \
    --cc=dledford-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org \
    --cc=gregkh-hQyY1W1yCW8ekmWlsbkhG0B+6BGkLq7r@public.gmane.org \
    --cc=jubin.john-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org \
    --cc=linux-rdma-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).